1 %x string name charmap defn nchar subs subs2
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
7 * at Electronni Visti IA, Kiev, Ukraine.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
45 int line_no = 1, save_no, fromsubs;
46 u_char buf[BUFSIZE], *ptr;
48 YY_BUFFER_STATE main_buf, map_buf;
51 #endif /* FLEX_DEBUG */
55 <INITIAL,charmap,nchar,subs,subs2>[ \t]+ ;
56 <subs2>\" { ptr = buf; BEGIN(string); }
57 <subs>\< { ptr = buf; fromsubs = 1; BEGIN(name); }
58 <INITIAL>\< { ptr = buf; fromsubs = 0; BEGIN(name); }
61 <INITIAL>\\\n line_no++;
62 <INITIAL,nchar,subs>\\t { yylval.ch = '\t'; return CHAR; }
63 <INITIAL,nchar,subs>\\n { yylval.ch = '\n'; return CHAR; }
64 <INITIAL,nchar,subs>\\b { yylval.ch = '\b'; return CHAR; }
65 <INITIAL,nchar,subs>\\f { yylval.ch = '\f'; return CHAR; }
66 <INITIAL,nchar,subs>\\v { yylval.ch = '\v'; return CHAR; }
67 <INITIAL,nchar,subs>\\r { yylval.ch = '\r'; return CHAR; }
68 <INITIAL,nchar,subs>\\a { yylval.ch = '\a'; return CHAR; }
82 <INITIAL>[;,{}()] return *yytext;
83 <INITIAL>substitute { BEGIN(subs); return SUBSTITUTE; }
84 <subs>with { BEGIN(subs2); return WITH; }
85 <INITIAL>order return ORDER;
86 <INITIAL>charmap BEGIN(charmap);
87 <INITIAL>;[ \t]*\.\.\.[ \t]*; return RANGE;
88 <INITIAL,nchar,subs>\\[0-7]{3} {
91 sscanf(&yytext[1], "%o", &v);
92 yylval.ch = (u_char)v;
95 <INITIAL,nchar,subs>\\x[0-9a-fA-F]{2} {
98 sscanf(&yytext[2], "%x", &v);
99 yylval.ch = (u_char)v;
102 <INITIAL,nchar,subs>\\. { yylval.ch = yytext[1]; return CHAR; }
103 <INITIAL,nchar,subs>. { yylval.ch = *yytext; return CHAR; }
104 <defn>^#.*\n line_no++;
107 errx(EX_UNAVAILABLE, "map expected near line %u of %s",
110 strcpy(yylval.str, buf);
115 if(ptr >= buf + sizeof(buf) - 1)
116 errx(EX_UNAVAILABLE, "name buffer overflow near line %u, character '/'",
121 if(ptr >= buf + sizeof(buf) - 1)
122 errx(EX_UNAVAILABLE, "name buffer overflow near line %u, character '>'",
127 if(ptr >= buf + sizeof(buf) - 1)
128 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\"'",
136 errx(EX_UNAVAILABLE, "non-empty name expected near line %u",
139 for (i = 0; i <= UCHAR_MAX; i++) {
140 if (strcmp(charmap_table[i], buf) == 0)
143 errx(EX_UNAVAILABLE, "name <%s> not 'charmap'-defined near line %u",
155 strcpy(yylval.str, buf);
160 const char *s = (map_fp != NULL) ? map_name : "input";
162 if (!isascii(*yytext) || !isprint(*yytext))
163 errx(EX_UNAVAILABLE, "non-ASCII or non-printable character 0x%02x not allowed in the map/name near line %u of %s",
164 *yytext, line_no, s);
165 if(ptr >= buf + sizeof(buf) - 1)
166 errx(EX_UNAVAILABLE, "map/name buffer overflow near line %u of %s, character '%c'",
167 line_no, s, *yytext);
171 if(ptr >= buf + sizeof(buf) - 1)
172 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\t'",
177 if(ptr >= buf + sizeof(buf) - 1)
178 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\b'",
183 if(ptr >= buf + sizeof(buf) - 1)
184 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\f'",
189 if(ptr >= buf + sizeof(buf) - 1)
190 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\v'",
195 if(ptr >= buf + sizeof(buf) - 1)
196 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\n'",
201 if(ptr >= buf + sizeof(buf) - 1)
202 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\r'",
207 if(ptr >= buf + sizeof(buf) - 1)
208 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\a'",
212 <name,string,defn>\n {
213 const char *s = (map_fp != NULL) ? map_name : "input";
215 errx(EX_UNAVAILABLE, "unterminated map/name/string near line %u of %s", line_no, s);
217 <name,string,nchar><<EOF>> {
218 const char *s = (map_fp != NULL) ? map_name : "input";
220 errx(EX_UNAVAILABLE, "premature EOF in the name/string/char near line %u of %s", line_no, s);
222 <string>\\x[0-9a-f]{2} {
225 sscanf(&yytext[2], "%x", &v);
231 sscanf(&yytext[1], "%o", &v);
235 if(ptr >= buf + sizeof(buf) - 1)
236 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '%c'",
241 if(ptr >= buf + sizeof(buf) - 1)
242 errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '%c'",
247 strcat(map_name, "/");
248 strcat(map_name, yytext);
249 if((map_fp = fopen(map_name, "r")) == NULL)
250 err(EX_UNAVAILABLE, "can't open 'charmap' file %s",
254 map_buf = yy_new_buffer(map_fp, YY_BUF_SIZE);
255 main_buf = YY_CURRENT_BUFFER;
256 yy_switch_to_buffer(map_buf);
261 errx(EX_UNAVAILABLE, "'charmap' file name expected near line %u",
265 errx(EX_UNAVAILABLE, "'charmap' file name expected near line %u",
268 <INITIAL,defn><<EOF>> {
271 errx(EX_UNAVAILABLE, "premature EOF in the map near line %u of %s", line_no, map_name);
272 yy_switch_to_buffer(main_buf);
273 yy_delete_buffer(map_buf);
289 #endif /* FLEX_DEBUG */