collate 3/4: Bring in input files for new LC_COLLATE format
[dragonfly.git] / usr.bin / colldef / scan.l
CommitLineData
984263bc
MD
1%x string name charmap defn nchar subs subs2
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.
1de703da 28 *
0d5acd74 29 * $FreeBSD: head/usr.bin/colldef/scan.l 175038 2008-01-01 10:04:10Z imp $
984263bc
MD
30 */
31
0d5acd74 32
984263bc
MD
33#include <ctype.h>
34#include <err.h>
35#include <limits.h>
0d5acd74 36#include <unistd.h>
984263bc
MD
37#include <string.h>
38#include <sysexits.h>
39#include "common.h"
40#include "y.tab.h"
41
42int line_no = 1, save_no, fromsubs;
0d5acd74 43u_char buff[BUFSIZE], *zptr;
984263bc
MD
44FILE *map_fp;
45YY_BUFFER_STATE main_buf, map_buf;
46#ifdef FLEX_DEBUG
47YYSTYPE yylval;
48#endif /* FLEX_DEBUG */
984263bc 49%}
eb74dec6
JM
50%option noinput
51%option nounput
984263bc
MD
52%%
53<INITIAL,charmap,nchar,subs,subs2>[ \t]+ ;
0d5acd74
JM
54<subs2>\" { zptr = buff; BEGIN(string); }
55<subs>\< { zptr = buff; fromsubs = 1; BEGIN(name); }
56<INITIAL>\< { zptr = buff; fromsubs = 0; BEGIN(name); }
984263bc
MD
57^#.*\n line_no++;
58^\n line_no++;
59<INITIAL>\\\n line_no++;
60<INITIAL,nchar,subs>\\t { yylval.ch = '\t'; return CHAR; }
61<INITIAL,nchar,subs>\\n { yylval.ch = '\n'; return CHAR; }
62<INITIAL,nchar,subs>\\b { yylval.ch = '\b'; return CHAR; }
63<INITIAL,nchar,subs>\\f { yylval.ch = '\f'; return CHAR; }
64<INITIAL,nchar,subs>\\v { yylval.ch = '\v'; return CHAR; }
65<INITIAL,nchar,subs>\\r { yylval.ch = '\r'; return CHAR; }
66<INITIAL,nchar,subs>\\a { yylval.ch = '\a'; return CHAR; }
67<subs2>\n {
68 line_no++;
69 BEGIN(INITIAL);
70 return '\n';
71}
72<INITIAL,nchar>\n {
73 line_no++;
74 if (map_fp != NULL) {
0d5acd74 75 zptr = buff;
984263bc
MD
76 BEGIN(defn);
77 }
78 return '\n';
79}
80<INITIAL>[;,{}()] return *yytext;
81<INITIAL>substitute { BEGIN(subs); return SUBSTITUTE; }
82<subs>with { BEGIN(subs2); return WITH; }
83<INITIAL>order return ORDER;
84<INITIAL>charmap BEGIN(charmap);
85<INITIAL>;[ \t]*\.\.\.[ \t]*; return RANGE;
86<INITIAL,nchar,subs>\\[0-7]{3} {
87 u_int v;
88
89 sscanf(&yytext[1], "%o", &v);
90 yylval.ch = (u_char)v;
91 return CHAR;
92}
93<INITIAL,nchar,subs>\\x[0-9a-fA-F]{2} {
94 u_int v;
95
96 sscanf(&yytext[2], "%x", &v);
97 yylval.ch = (u_char)v;
98 return CHAR;
99}
100<INITIAL,nchar,subs>\\. { yylval.ch = yytext[1]; return CHAR; }
101<INITIAL,nchar,subs>. { yylval.ch = *yytext; return CHAR; }
102<defn>^#.*\n line_no++;
103<defn>[ \t]+ {
0d5acd74 104 if (zptr == buff)
984263bc
MD
105 errx(EX_UNAVAILABLE, "map expected near line %u of %s",
106 line_no, map_name);
0d5acd74
JM
107 *zptr = '\0';
108 strcpy(yylval.str, buff);
984263bc
MD
109 BEGIN(nchar);
110 return DEFN;
111}
112<name>\/\/ {
0d5acd74 113 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
114 errx(EX_UNAVAILABLE, "name buffer overflow near line %u, character '/'",
115 line_no);
0d5acd74 116 *zptr++ = '/';
984263bc
MD
117}
118<name>\/\> {
0d5acd74 119 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
120 errx(EX_UNAVAILABLE, "name buffer overflow near line %u, character '>'",
121 line_no);
0d5acd74 122 *zptr++ = '>';
984263bc
MD
123}
124<string>\\\" {
0d5acd74 125 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
126 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\"'",
127 line_no);
0d5acd74 128 *zptr++ = '"';
984263bc
MD
129}
130<name>\> {
131 u_int i;
132
0d5acd74 133 if (zptr == buff)
984263bc
MD
134 errx(EX_UNAVAILABLE, "non-empty name expected near line %u",
135 line_no);
0d5acd74 136 *zptr = '\0';
984263bc 137 for (i = 0; i <= UCHAR_MAX; i++) {
0d5acd74 138 if (strcmp(charmap_table[i], buff) == 0)
984263bc
MD
139 goto findit;
140 }
141 errx(EX_UNAVAILABLE, "name <%s> not 'charmap'-defined near line %u",
0d5acd74 142 buff, line_no);
984263bc
MD
143 findit:
144 yylval.ch = i;
145 if (fromsubs)
146 BEGIN(subs);
147 else
148 BEGIN(INITIAL);
149 return CHAR;
150}
151<string>\" {
0d5acd74
JM
152 *zptr = '\0';
153 strcpy(yylval.str, buff);
984263bc
MD
154 BEGIN(subs2);
155 return STRING;
156}
157<name,defn>. {
158 const char *s = (map_fp != NULL) ? map_name : "input";
159
160 if (!isascii(*yytext) || !isprint(*yytext))
161 errx(EX_UNAVAILABLE, "non-ASCII or non-printable character 0x%02x not allowed in the map/name near line %u of %s",
162 *yytext, line_no, s);
0d5acd74 163 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
164 errx(EX_UNAVAILABLE, "map/name buffer overflow near line %u of %s, character '%c'",
165 line_no, s, *yytext);
0d5acd74 166 *zptr++ = *yytext;
984263bc
MD
167}
168<string>\\t {
0d5acd74 169 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
170 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\t'",
171 line_no);
0d5acd74 172 *zptr++ = '\t';
984263bc
MD
173}
174<string>\\b {
0d5acd74 175 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
176 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\b'",
177 line_no);
0d5acd74 178 *zptr++ = '\b';
984263bc
MD
179}
180<string>\\f {
0d5acd74 181 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
182 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\f'",
183 line_no);
0d5acd74 184 *zptr++ = '\f';
984263bc
MD
185}
186<string>\\v {
0d5acd74 187 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
188 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\v'",
189 line_no);
0d5acd74 190 *zptr++ = '\v';
984263bc
MD
191}
192<string>\\n {
0d5acd74 193 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
194 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\n'",
195 line_no);
0d5acd74 196 *zptr++ = '\n';
984263bc
MD
197}
198<string>\\r {
0d5acd74 199 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
200 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\r'",
201 line_no);
0d5acd74 202 *zptr++ = '\r';
984263bc
MD
203}
204<string>\\a {
0d5acd74 205 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
206 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\a'",
207 line_no);
0d5acd74 208 *zptr++ = '\a';
984263bc
MD
209}
210<name,string,defn>\n {
211 const char *s = (map_fp != NULL) ? map_name : "input";
212
213 errx(EX_UNAVAILABLE, "unterminated map/name/string near line %u of %s", line_no, s);
214}
215<name,string,nchar><<EOF>> {
216 const char *s = (map_fp != NULL) ? map_name : "input";
217
218 errx(EX_UNAVAILABLE, "premature EOF in the name/string/char near line %u of %s", line_no, s);
219}
220<string>\\x[0-9a-f]{2} {
221 u_int v;
222
223 sscanf(&yytext[2], "%x", &v);
0d5acd74 224 *zptr++ = (u_char)v;
984263bc
MD
225}
226<string>\\[0-7]{3} {
227 u_int v;
228
229 sscanf(&yytext[1], "%o", &v);
0d5acd74 230 *zptr++ = (u_char)v;
984263bc
MD
231}
232<string>\\. {
0d5acd74 233 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
234 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '%c'",
235 line_no, yytext[1]);
0d5acd74 236 *zptr++ = yytext[1];
984263bc
MD
237}
238<string>. {
0d5acd74 239 if(zptr >= buff + sizeof(buff) - 1)
984263bc
MD
240 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '%c'",
241 line_no, *yytext);
0d5acd74 242 *zptr++ = *yytext;
984263bc
MD
243}
244<charmap>[^ \t\n]+ {
245 strcat(map_name, "/");
246 strcat(map_name, yytext);
247 if((map_fp = fopen(map_name, "r")) == NULL)
248 err(EX_UNAVAILABLE, "can't open 'charmap' file %s",
249 map_name);
250 save_no = line_no;
251 line_no = 1;
252 map_buf = yy_new_buffer(map_fp, YY_BUF_SIZE);
253 main_buf = YY_CURRENT_BUFFER;
254 yy_switch_to_buffer(map_buf);
0d5acd74 255 zptr = buff;
984263bc
MD
256 BEGIN(defn);
257}
258<charmap>\n {
259 errx(EX_UNAVAILABLE, "'charmap' file name expected near line %u",
260 line_no);
261}
262<charmap><<EOF>> {
263 errx(EX_UNAVAILABLE, "'charmap' file name expected near line %u",
264 line_no);
265}
266<INITIAL,defn><<EOF>> {
267 if(map_fp != NULL) {
0d5acd74 268 if (zptr != buff)
984263bc
MD
269 errx(EX_UNAVAILABLE, "premature EOF in the map near line %u of %s", line_no, map_name);
270 yy_switch_to_buffer(main_buf);
271 yy_delete_buffer(map_buf);
272 fclose(map_fp);
273 map_fp = NULL;
274 line_no = save_no;
275 BEGIN(INITIAL);
276 } else
277 yyterminate();
278}
279%%
280#ifdef FLEX_DEBUG
0d5acd74 281main()
984263bc
MD
282{
283 while(yylex())
284 ;
285 return 0;
286}
287#endif /* FLEX_DEBUG */