]> git.cameronkatri.com Git - apple_cmds.git/blob - adv_cmds/colldef/scan.l
system_cmds: 880.120.1
[apple_cmds.git] / adv_cmds / colldef / scan.l
1 %x string s_name charmap defn nchar subs subs2 ldef elem
2 %{
3 /*-
4 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
5 * at Electronni Visti IA, Kiev, Ukraine.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD: src/usr.bin/colldef/scan.l,v 1.19 2002/08/23 04:18:26 ache Exp $");
32
33 #include <ctype.h>
34 #include <err.h>
35 #include <limits.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <wchar.h>
39 #include <sysexits.h>
40 #include <sys/types.h>
41 #include "collate.h"
42 #include "common.h"
43 #include "y.tab.h"
44
45 int line_no = 1, save_no, fromsubs;
46 wchar_t buf0[BUFSIZE], *ptr;
47 wchar_t *buf = buf0;
48 wchar_t bufstr[BUFSIZE], *ptrsave;
49 FILE *map_fp;
50 YY_BUFFER_STATE main_buf, map_buf;
51 #ifdef FLEX_DEBUG
52 YYSTYPE yylval;
53 #endif /* FLEX_DEBUG */
54 int yylex(void);
55 static int localedefmode = 0;
56 static orderpass = 0;
57 %}
58 %%
59 <INITIAL,charmap,nchar,subs,subs2,ldef>[ \t]+ ;
60 <subs2,ldef>\" { ptr = buf; BEGIN(string); }
61 <string>\< {
62 if(localedefmode) {
63 ptrsave = ptr;
64 ptr = buf = bufstr;
65 BEGIN(s_name);
66 } else {
67 if(ptr >= buf + BUFSIZE - 1)
68 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '<'",
69 line_no);
70 *ptr++ = '<';
71 }
72 }
73 <subs>\< { ptr = buf; fromsubs = 1; BEGIN(s_name); }
74 <ldef>[,;] return *yytext;
75 <ldef>forward { yylval.ch = DIRECTIVE_FORWARD; return ORDER_DIRECTIVE; }
76 <ldef>backward { yylval.ch = DIRECTIVE_BACKWARD; return ORDER_DIRECTIVE; }
77 <ldef>position { yylval.ch = DIRECTIVE_POSITION; return ORDER_DIRECTIVE; }
78 <ldef>collating[-_]element return COLLATING_ELEMENT;
79 <ldef>collating[-_]symbol return COLLATING_SYMBOL;
80 <ldef>from return FROM;
81 <ldef>\.\.\. return ELLIPSIS;
82 <ldef>IGNORE return IGNORE;
83 <ldef>UNDEFINED return UNDEFINED;
84 <ldef>order[-_]start return ORDER_START;
85 <ldef>order[-_]end {
86 char line[YY_BUF_SIZE];
87 if (orderpass)
88 return ORDER_END;
89 /* The first pass only defined the left-most symbol. We reread the
90 * order lines, and forward references should now be resolved. */
91 orderpass++;
92 YY_FLUSH_BUFFER;
93 rewind(yyin);
94 for(;;) {
95 if (fgets(line, sizeof(line), yyin) == NULL)
96 errx(EX_UNAVAILABLE, "EOF rescanning for order_start");
97 if (*line == '#')
98 continue;
99 if (strstr(line, "order_start") != NULL)
100 break;
101 }
102 return ORDER_SECOND_PASS;
103 }
104 <ldef>END[ \t]+LC_COLLATE return END_LC_COLLATE;
105 <ldef>\n {
106 line_no++;
107 return '\n';
108 }
109 <ldef>\< { ptr = buf; BEGIN(elem); }
110 <INITIAL>\< { ptr = buf; fromsubs = 0; BEGIN(s_name); }
111 <*>^#.*\n line_no++;
112 ^\n line_no++;
113 <INITIAL>\\\n line_no++;
114 <INITIAL,nchar,subs>\\t { yylval.ch = '\t'; return CHAR; }
115 <INITIAL,nchar,subs>\\n { yylval.ch = '\n'; return CHAR; }
116 <INITIAL,nchar,subs>\\b { yylval.ch = '\b'; return CHAR; }
117 <INITIAL,nchar,subs>\\f { yylval.ch = '\f'; return CHAR; }
118 <INITIAL,nchar,subs>\\v { yylval.ch = '\v'; return CHAR; }
119 <INITIAL,nchar,subs>\\r { yylval.ch = '\r'; return CHAR; }
120 <INITIAL,nchar,subs>\\a { yylval.ch = '\a'; return CHAR; }
121 <subs2>\n {
122 line_no++;
123 BEGIN(INITIAL);
124 return '\n';
125 }
126 <INITIAL,nchar>\n {
127 line_no++;
128 if (map_fp != NULL) {
129 ptr = buf;
130 BEGIN(defn);
131 }
132 return '\n';
133 }
134 <INITIAL>[;,{}()] return *yytext;
135 <INITIAL>substitute { BEGIN(subs); return SUBSTITUTE; }
136 <INITIAL>LC_COLLATE { BEGIN(ldef); localedefmode++; return START_LC_COLLATE; }
137 <subs>with { BEGIN(subs2); return WITH; }
138 <INITIAL>order return ORDER;
139 <INITIAL,ldef>charmap BEGIN(charmap);
140 <INITIAL>;[ \t]*\.\.\.[ \t]*; return RANGE;
141 <INITIAL,nchar,subs>\\([0-7]{3}) {
142 u_int v;
143
144 sscanf(&yytext[1], "%o", &v);
145 yylval.ch = v;
146 return CHAR;
147 }
148 <INITIAL,nchar,subs>\\x\{([0-9a-fA-F]{2,8})\} {
149 u_int v;
150
151 sscanf(&yytext[3], "%x", &v);
152 yylval.ch = v;
153 return CHAR;
154 }
155 <INITIAL,nchar,subs>\\x([0-9a-fA-F]{2}) {
156 u_int v;
157
158 sscanf(&yytext[2], "%x", &v);
159 yylval.ch = v;
160 return CHAR;
161 }
162 <INITIAL,nchar,subs>\\. { yylval.ch = yytext[1]; return CHAR; }
163 <INITIAL,nchar,subs>. { yylval.ch = *(u_char *)yytext; return CHAR; }
164 <defn>^\n line_no++;
165 <defn>[ \t]+ {
166 if (ptr == buf)
167 errx(EX_UNAVAILABLE, "map expected near line %u of %s",
168 line_no, map_name);
169 *ptr = 0;
170 if (localedefmode && *buf == '<' && ptr[-1] == '>') {
171 if (ptr == buf + 2)
172 errx(EX_UNAVAILABLE, "map expected near line %u of %s",
173 line_no, map_name);
174 *--ptr = 0;
175 wcscpy(yylval.str, buf + 1);
176 } else
177 wcscpy(yylval.str, buf);
178 BEGIN(nchar);
179 return DEFN;
180 }
181 <s_name,elem>\/\/ {
182 if(ptr >= buf + BUFSIZE - 1)
183 errx(EX_UNAVAILABLE, "name buffer overflow near line %u, character '/'",
184 line_no);
185 *ptr++ = '/';
186 }
187 <s_name,elem>\/\> {
188 if(ptr >= buf + BUFSIZE - 1)
189 errx(EX_UNAVAILABLE, "name buffer overflow near line %u, character '>'",
190 line_no);
191 *ptr++ = '>';
192 }
193 <string>\\\" {
194 if(ptr >= buf + BUFSIZE - 1)
195 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\"'",
196 line_no);
197 *ptr++ = '"';
198 }
199 <elem>\> {
200 if (ptr == buf)
201 errx(EX_UNAVAILABLE, "non-empty name expected near line %u",
202 line_no);
203 *ptr = 0;
204 wcscpy(yylval.str, buf);
205 BEGIN(ldef);
206 return ELEM;
207 }
208 <s_name>\> {
209 struct symbol *s;
210
211 if (ptr == buf)
212 errx(EX_UNAVAILABLE, "non-empty name expected near line %u",
213 line_no);
214 *ptr = 0;
215 s = getsymbol(buf, EXISTS);
216 switch (s->type) {
217 case SYMBOL_CHAR:
218 break;
219 case SYMBOL_CHAIN:
220 errx(EX_UNAVAILABLE, "name <%s> is chain type near line %u",
221 showwcs(buf, CHARMAP_SYMBOL_LEN), line_no);
222 case SYMBOL_SYMBOL:
223 errx(EX_UNAVAILABLE, "name <%s> is symbol type near line %u",
224 showwcs(buf, CHARMAP_SYMBOL_LEN), line_no);
225 default:
226 errx(EX_UNAVAILABLE, "name <%s>: unknown symbol type (%d) near line %u",
227 showwcs(buf, CHARMAP_SYMBOL_LEN), s->type, line_no);
228 }
229 if (localedefmode) {
230 ptr = ptrsave;
231 buf = buf0;
232 if(ptr >= buf + BUFSIZE - 1)
233 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character <%s>",
234 line_no, showwcs(bufstr, CHARMAP_SYMBOL_LEN));
235 *ptr++ = s->u.wc;
236 BEGIN(string);
237 } else {
238 yylval.ch = s->u.wc;
239 if (fromsubs)
240 BEGIN(subs);
241 else
242 BEGIN(INITIAL);
243 return CHAR;
244 }
245 }
246 <string>\" {
247 *ptr = 0;
248 wcscpy(yylval.str, buf);
249 if (localedefmode)
250 BEGIN(ldef);
251 else
252 BEGIN(subs2);
253 return STRING;
254 }
255 <s_name,defn,elem>. {
256 const char *s = (map_fp != NULL) ? map_name : "input";
257
258 if (!isascii(*yytext) || !isprint(*yytext))
259 errx(EX_UNAVAILABLE, "non-ASCII or non-printable character 0x%02x not allowed in the map/name near line %u of %s",
260 *yytext, line_no, s);
261 if(ptr >= buf + BUFSIZE - 1)
262 errx(EX_UNAVAILABLE, "map/name buffer overflow near line %u of %s, character '%c'",
263 line_no, s, *yytext);
264 *ptr++ = *yytext;
265 }
266 <string>\\t {
267 if(ptr >= buf + BUFSIZE - 1)
268 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\t'",
269 line_no);
270 *ptr++ = '\t';
271 }
272 <string>\\b {
273 if(ptr >= buf + BUFSIZE - 1)
274 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\b'",
275 line_no);
276 *ptr++ = '\b';
277 }
278 <string>\\f {
279 if(ptr >= buf + BUFSIZE - 1)
280 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\f'",
281 line_no);
282 *ptr++ = '\f';
283 }
284 <string>\\v {
285 if(ptr >= buf + BUFSIZE - 1)
286 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\v'",
287 line_no);
288 *ptr++ = '\v';
289 }
290 <string>\\n {
291 if(ptr >= buf + BUFSIZE - 1)
292 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\n'",
293 line_no);
294 *ptr++ = '\n';
295 }
296 <string>\\r {
297 if(ptr >= buf + BUFSIZE - 1)
298 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\r'",
299 line_no);
300 *ptr++ = '\r';
301 }
302 <string>\\a {
303 if(ptr >= buf + BUFSIZE - 1)
304 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\a'",
305 line_no);
306 *ptr++ = '\a';
307 }
308 <s_name,string,defn,elem>\n {
309 const char *s = (map_fp != NULL) ? map_name : "input";
310
311 errx(EX_UNAVAILABLE, "unterminated map/name/string near line %u of %s", line_no, s);
312 }
313 <s_name,string,nchar,elem><<EOF>> {
314 const char *s = (map_fp != NULL) ? map_name : "input";
315
316 errx(EX_UNAVAILABLE, "premature EOF in the name/string/char near line %u of %s", line_no, s);
317 }
318 <string>\\x\{([0-9a-f]{2,8})\} {
319 u_int v;
320
321 sscanf(&yytext[3], "%x", &v);
322 *ptr++ = v;
323 }
324 <string>\\x([0-9a-f]{2}) {
325 u_int v;
326
327 sscanf(&yytext[2], "%x", &v);
328 *ptr++ = v;
329 }
330 <string>\\([0-7]{3}) {
331 u_int v;
332
333 sscanf(&yytext[1], "%o", &v);
334 *ptr++ = v;
335 }
336 <string>\\. {
337 if(ptr >= buf + BUFSIZE - 1)
338 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '%c'",
339 line_no, yytext[1]);
340 *ptr++ = yytext[1];
341 }
342 <string>. {
343 if(ptr >= buf + BUFSIZE - 1)
344 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '%c'",
345 line_no, *yytext);
346 *ptr++ = *yytext;
347 }
348 <charmap>[^ \t\n]+ {
349 if(*yytext == '/')
350 strcpy(map_name, yytext);
351 else {
352 strcat(map_name, "/");
353 strcat(map_name, yytext);
354 }
355 if((map_fp = fopen(map_name, "r")) == NULL)
356 err(EX_UNAVAILABLE, "can't open 'charmap' file %s",
357 map_name);
358 save_no = line_no;
359 line_no = 1;
360 map_buf = yy_new_buffer(map_fp, YY_BUF_SIZE);
361 main_buf = YY_CURRENT_BUFFER;
362 yy_switch_to_buffer(map_buf);
363 ptr = buf;
364 BEGIN(defn);
365 }
366 <charmap>\n {
367 errx(EX_UNAVAILABLE, "'charmap' file name expected near line %u",
368 line_no);
369 }
370 <charmap><<EOF>> {
371 errx(EX_UNAVAILABLE, "'charmap' file name expected near line %u",
372 line_no);
373 }
374 <INITIAL,defn><<EOF>> {
375 if(map_fp != NULL) {
376 if (ptr != buf)
377 errx(EX_UNAVAILABLE, "premature EOF in the map near line %u of %s", line_no, map_name);
378 yy_switch_to_buffer(main_buf);
379 yy_delete_buffer(map_buf);
380 fclose(map_fp);
381 map_fp = NULL;
382 line_no = save_no;
383 if (localedefmode)
384 BEGIN(ldef);
385 else
386 BEGIN(INITIAL);
387 } else
388 yyterminate();
389 }
390 %%
391 #ifdef FLEX_DEBUG
392 main()
393 {
394 while(yylex())
395 ;
396 return 0;
397 }
398 #endif /* FLEX_DEBUG */