1 |
/* $MidnightBSD$ */ |
2 |
%x string name charmap defn nchar subs subs2 |
3 |
%{ |
4 |
/*- |
5 |
* Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua> |
6 |
* at Electronni Visti IA, Kiev, Ukraine. |
7 |
* All rights reserved. |
8 |
* |
9 |
* Redistribution and use in source and binary forms, with or without |
10 |
* modification, are permitted provided that the following conditions |
11 |
* are met: |
12 |
* 1. Redistributions of source code must retain the above copyright |
13 |
* notice, this list of conditions and the following disclaimer. |
14 |
* 2. Redistributions in binary form must reproduce the above copyright |
15 |
* notice, this list of conditions and the following disclaimer in the |
16 |
* documentation and/or other materials provided with the distribution. |
17 |
* |
18 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND |
19 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE |
22 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 |
* SUCH DAMAGE. |
29 |
*/ |
30 |
|
31 |
#include <sys/cdefs.h> |
32 |
__FBSDID("$FreeBSD: stable/10/usr.bin/colldef/scan.l 175038 2008-01-01 10:04:10Z imp $"); |
33 |
|
34 |
#include <sys/types.h> |
35 |
#include <ctype.h> |
36 |
#include <err.h> |
37 |
#include <limits.h> |
38 |
#include <unistd.h> |
39 |
#include <string.h> |
40 |
#include <sysexits.h> |
41 |
#include "common.h" |
42 |
#include "y.tab.h" |
43 |
|
44 |
int line_no = 1, save_no, fromsubs; |
45 |
u_char buf[BUFSIZE], *ptr; |
46 |
FILE *map_fp; |
47 |
YY_BUFFER_STATE main_buf, map_buf; |
48 |
#ifdef FLEX_DEBUG |
49 |
YYSTYPE yylval; |
50 |
#endif /* FLEX_DEBUG */ |
51 |
int yylex(void); |
52 |
%} |
53 |
%% |
54 |
<INITIAL,charmap,nchar,subs,subs2>[ \t]+ ; |
55 |
<subs2>\" { ptr = buf; BEGIN(string); } |
56 |
<subs>\< { ptr = buf; fromsubs = 1; BEGIN(name); } |
57 |
<INITIAL>\< { ptr = buf; fromsubs = 0; BEGIN(name); } |
58 |
^#.*\n line_no++; |
59 |
^\n line_no++; |
60 |
<INITIAL>\\\n line_no++; |
61 |
<INITIAL,nchar,subs>\\t { yylval.ch = '\t'; return CHAR; } |
62 |
<INITIAL,nchar,subs>\\n { yylval.ch = '\n'; return CHAR; } |
63 |
<INITIAL,nchar,subs>\\b { yylval.ch = '\b'; return CHAR; } |
64 |
<INITIAL,nchar,subs>\\f { yylval.ch = '\f'; return CHAR; } |
65 |
<INITIAL,nchar,subs>\\v { yylval.ch = '\v'; return CHAR; } |
66 |
<INITIAL,nchar,subs>\\r { yylval.ch = '\r'; return CHAR; } |
67 |
<INITIAL,nchar,subs>\\a { yylval.ch = '\a'; return CHAR; } |
68 |
<subs2>\n { |
69 |
line_no++; |
70 |
BEGIN(INITIAL); |
71 |
return '\n'; |
72 |
} |
73 |
<INITIAL,nchar>\n { |
74 |
line_no++; |
75 |
if (map_fp != NULL) { |
76 |
ptr = buf; |
77 |
BEGIN(defn); |
78 |
} |
79 |
return '\n'; |
80 |
} |
81 |
<INITIAL>[;,{}()] return *yytext; |
82 |
<INITIAL>substitute { BEGIN(subs); return SUBSTITUTE; } |
83 |
<subs>with { BEGIN(subs2); return WITH; } |
84 |
<INITIAL>order return ORDER; |
85 |
<INITIAL>charmap BEGIN(charmap); |
86 |
<INITIAL>;[ \t]*\.\.\.[ \t]*; return RANGE; |
87 |
<INITIAL,nchar,subs>\\[0-7]{3} { |
88 |
u_int v; |
89 |
|
90 |
sscanf(&yytext[1], "%o", &v); |
91 |
yylval.ch = (u_char)v; |
92 |
return CHAR; |
93 |
} |
94 |
<INITIAL,nchar,subs>\\x[0-9a-fA-F]{2} { |
95 |
u_int v; |
96 |
|
97 |
sscanf(&yytext[2], "%x", &v); |
98 |
yylval.ch = (u_char)v; |
99 |
return CHAR; |
100 |
} |
101 |
<INITIAL,nchar,subs>\\. { yylval.ch = yytext[1]; return CHAR; } |
102 |
<INITIAL,nchar,subs>. { yylval.ch = *yytext; return CHAR; } |
103 |
<defn>^#.*\n line_no++; |
104 |
<defn>[ \t]+ { |
105 |
if (ptr == buf) |
106 |
errx(EX_UNAVAILABLE, "map expected near line %u of %s", |
107 |
line_no, map_name); |
108 |
*ptr = '\0'; |
109 |
strcpy(yylval.str, buf); |
110 |
BEGIN(nchar); |
111 |
return DEFN; |
112 |
} |
113 |
<name>\/\/ { |
114 |
if(ptr >= buf + sizeof(buf) - 1) |
115 |
errx(EX_UNAVAILABLE, "name buffer overflow near line %u, character '/'", |
116 |
line_no); |
117 |
*ptr++ = '/'; |
118 |
} |
119 |
<name>\/\> { |
120 |
if(ptr >= buf + sizeof(buf) - 1) |
121 |
errx(EX_UNAVAILABLE, "name buffer overflow near line %u, character '>'", |
122 |
line_no); |
123 |
*ptr++ = '>'; |
124 |
} |
125 |
<string>\\\" { |
126 |
if(ptr >= buf + sizeof(buf) - 1) |
127 |
errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\"'", |
128 |
line_no); |
129 |
*ptr++ = '"'; |
130 |
} |
131 |
<name>\> { |
132 |
u_int i; |
133 |
|
134 |
if (ptr == buf) |
135 |
errx(EX_UNAVAILABLE, "non-empty name expected near line %u", |
136 |
line_no); |
137 |
*ptr = '\0'; |
138 |
for (i = 0; i <= UCHAR_MAX; i++) { |
139 |
if (strcmp(charmap_table[i], buf) == 0) |
140 |
goto findit; |
141 |
} |
142 |
errx(EX_UNAVAILABLE, "name <%s> not 'charmap'-defined near line %u", |
143 |
buf, line_no); |
144 |
findit: |
145 |
yylval.ch = i; |
146 |
if (fromsubs) |
147 |
BEGIN(subs); |
148 |
else |
149 |
BEGIN(INITIAL); |
150 |
return CHAR; |
151 |
} |
152 |
<string>\" { |
153 |
*ptr = '\0'; |
154 |
strcpy(yylval.str, buf); |
155 |
BEGIN(subs2); |
156 |
return STRING; |
157 |
} |
158 |
<name,defn>. { |
159 |
const char *s = (map_fp != NULL) ? map_name : "input"; |
160 |
|
161 |
if (!isascii(*yytext) || !isprint(*yytext)) |
162 |
errx(EX_UNAVAILABLE, "non-ASCII or non-printable character 0x%02x not allowed in the map/name near line %u of %s", |
163 |
*yytext, line_no, s); |
164 |
if(ptr >= buf + sizeof(buf) - 1) |
165 |
errx(EX_UNAVAILABLE, "map/name buffer overflow near line %u of %s, character '%c'", |
166 |
line_no, s, *yytext); |
167 |
*ptr++ = *yytext; |
168 |
} |
169 |
<string>\\t { |
170 |
if(ptr >= buf + sizeof(buf) - 1) |
171 |
errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\t'", |
172 |
line_no); |
173 |
*ptr++ = '\t'; |
174 |
} |
175 |
<string>\\b { |
176 |
if(ptr >= buf + sizeof(buf) - 1) |
177 |
errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\b'", |
178 |
line_no); |
179 |
*ptr++ = '\b'; |
180 |
} |
181 |
<string>\\f { |
182 |
if(ptr >= buf + sizeof(buf) - 1) |
183 |
errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\f'", |
184 |
line_no); |
185 |
*ptr++ = '\f'; |
186 |
} |
187 |
<string>\\v { |
188 |
if(ptr >= buf + sizeof(buf) - 1) |
189 |
errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\v'", |
190 |
line_no); |
191 |
*ptr++ = '\v'; |
192 |
} |
193 |
<string>\\n { |
194 |
if(ptr >= buf + sizeof(buf) - 1) |
195 |
errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\n'", |
196 |
line_no); |
197 |
*ptr++ = '\n'; |
198 |
} |
199 |
<string>\\r { |
200 |
if(ptr >= buf + sizeof(buf) - 1) |
201 |
errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\r'", |
202 |
line_no); |
203 |
*ptr++ = '\r'; |
204 |
} |
205 |
<string>\\a { |
206 |
if(ptr >= buf + sizeof(buf) - 1) |
207 |
errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\a'", |
208 |
line_no); |
209 |
*ptr++ = '\a'; |
210 |
} |
211 |
<name,string,defn>\n { |
212 |
const char *s = (map_fp != NULL) ? map_name : "input"; |
213 |
|
214 |
errx(EX_UNAVAILABLE, "unterminated map/name/string near line %u of %s", line_no, s); |
215 |
} |
216 |
<name,string,nchar><<EOF>> { |
217 |
const char *s = (map_fp != NULL) ? map_name : "input"; |
218 |
|
219 |
errx(EX_UNAVAILABLE, "premature EOF in the name/string/char near line %u of %s", line_no, s); |
220 |
} |
221 |
<string>\\x[0-9a-f]{2} { |
222 |
u_int v; |
223 |
|
224 |
sscanf(&yytext[2], "%x", &v); |
225 |
*ptr++ = (u_char)v; |
226 |
} |
227 |
<string>\\[0-7]{3} { |
228 |
u_int v; |
229 |
|
230 |
sscanf(&yytext[1], "%o", &v); |
231 |
*ptr++ = (u_char)v; |
232 |
} |
233 |
<string>\\. { |
234 |
if(ptr >= buf + sizeof(buf) - 1) |
235 |
errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '%c'", |
236 |
line_no, yytext[1]); |
237 |
*ptr++ = yytext[1]; |
238 |
} |
239 |
<string>. { |
240 |
if(ptr >= buf + sizeof(buf) - 1) |
241 |
errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '%c'", |
242 |
line_no, *yytext); |
243 |
*ptr++ = *yytext; |
244 |
} |
245 |
<charmap>[^ \t\n]+ { |
246 |
strcat(map_name, "/"); |
247 |
strcat(map_name, yytext); |
248 |
if((map_fp = fopen(map_name, "r")) == NULL) |
249 |
err(EX_UNAVAILABLE, "can't open 'charmap' file %s", |
250 |
map_name); |
251 |
save_no = line_no; |
252 |
line_no = 1; |
253 |
map_buf = yy_new_buffer(map_fp, YY_BUF_SIZE); |
254 |
main_buf = YY_CURRENT_BUFFER; |
255 |
yy_switch_to_buffer(map_buf); |
256 |
ptr = buf; |
257 |
BEGIN(defn); |
258 |
} |
259 |
<charmap>\n { |
260 |
errx(EX_UNAVAILABLE, "'charmap' file name expected near line %u", |
261 |
line_no); |
262 |
} |
263 |
<charmap><<EOF>> { |
264 |
errx(EX_UNAVAILABLE, "'charmap' file name expected near line %u", |
265 |
line_no); |
266 |
} |
267 |
<INITIAL,defn><<EOF>> { |
268 |
if(map_fp != NULL) { |
269 |
if (ptr != buf) |
270 |
errx(EX_UNAVAILABLE, "premature EOF in the map near line %u of %s", line_no, map_name); |
271 |
yy_switch_to_buffer(main_buf); |
272 |
yy_delete_buffer(map_buf); |
273 |
fclose(map_fp); |
274 |
map_fp = NULL; |
275 |
line_no = save_no; |
276 |
BEGIN(INITIAL); |
277 |
} else |
278 |
yyterminate(); |
279 |
} |
280 |
%% |
281 |
#ifdef FLEX_DEBUG |
282 |
main() |
283 |
{ |
284 |
while(yylex()) |
285 |
; |
286 |
return 0; |
287 |
} |
288 |
#endif /* FLEX_DEBUG */ |