testlang_conf.l revision 1.19 1 %{
2 /* $NetBSD: testlang_conf.l,v 1.19 2021/02/13 08:00:07 rillig Exp $ */
3
4 /*-
5 * Copyright 2009 Brett Lymn <blymn (at) NetBSD.org>
6 *
7 * All rights reserved.
8 *
9 * This code has been donated to The NetBSD Foundation by the Author.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 *
31 */
32
33 #include <curses.h>
34 #include <ctype.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/param.h>
39 #include <err.h>
40 #include "returns.h"
41 #include "testlang_parse.h"
42
43 #define MAX_INCLUDES 32 /* limit for the number of nested includes */
44
45 int yylex(void);
46
47 extern size_t line;
48 extern char *cur_file; /* from director.c */
49
50 static int include_stack[MAX_INCLUDES];
51 static char *include_files[MAX_INCLUDES];
52 static int include_ptr = 0;
53
54 static char *
55 dequote(const char *s, size_t *len)
56 {
57 const unsigned char *p;
58 char *buf, *q;
59
60 *len = 0;
61 p = (const unsigned char *)s;
62 while (*p) {
63 if (*p == '\\' && *(p+1)) {
64 if (isdigit(*(p+1)) && *(p+2) && isdigit(*(p+2)) &&
65 *(p+3) && isdigit(*(p+3)))
66 p += 3;
67 else
68 ++p;
69 }
70 ++(*len);
71 ++p;
72 }
73
74 buf = malloc(*len + 1);
75 if (buf == NULL)
76 return NULL;
77
78 p = (const unsigned char *)s;
79 q = buf;
80 while (*p) {
81 if (*p == '\\' && *(p+1)) {
82 ++p;
83 if (isdigit(*p)) {
84 if (*(p+1) && isdigit(*(p+1)) && *(p+2) &&
85 isdigit(*(p+2))) {
86 *q++ = ((*p - '0') * 8 + (*(p+1) - '0')) * 8 + (*(p+2) - '0');
87 p += 3;
88 } else {
89 *q++ = *p++;
90 }
91 } else {
92 switch (*p) {
93 case 'e':
94 /* escape */
95 *q++ = '\e';
96 p++;
97 break;
98
99 case 'n':
100 /* newline */
101 *q++ = '\n';
102 p++;
103 break;
104
105 case 'r':
106 /* carriage return */
107 *q++ = '\r';
108 p++;
109 break;
110
111 case 't':
112 /* tab */
113 *q++ = '\t';
114 p++;
115 break;
116
117 case '\\':
118 /* backslash */
119 *q++ = '\\';
120 p++;
121 break;
122
123 default:
124 *q++ = *p++;
125 }
126 }
127 } else
128 *q++ = *p++;
129 }
130 *q++ = '\0';
131
132 return buf;
133 }
134 %}
135
136 HEX 0[xX][0-9a-zA-Z]+
137 STRING [0-9a-z!#-&(-^ \t%._\\]+
138 numeric [-0-9]+
139 PCHAR (\\.|[!-~])
140 ASSIGN assign
141 CALL2 call2
142 CALL3 call3
143 CALL4 call4
144 CALL call
145 CHECK check
146 DELAY delay
147 INPUT input
148 NOINPUT noinput
149 OK_RET OK
150 ERR_RET ERR
151 COMPARE compare
152 COMPAREND comparend
153 FILENAME [A-Za-z0-9.][A-Za-z0-9./_-]+
154 VARNAME [A-Za-z][A-Za-z0-9_-]+
155 NULL_RET NULL
156 NON_NULL NON_NULL
157 CCHAR cchar
158 WCHAR wchar
159 BYTE BYTE
160 OR \|
161 LPAREN \(
162 RPAREN \)
163 LBRACK \[
164 RBRACK \]
165 MULTIPLIER \*
166 COMMA ,
167
168 %x incl
169 %option noinput nounput
170
171 %%
172
173 include BEGIN(incl);
174
175 <incl>[ \t]* /* eat the whitespace */
176 <incl>[^ \t\n]+ { /* got the include file name */
177 char *inc_file;
178
179 if (include_ptr > MAX_INCLUDES) {
180 fprintf(stderr,
181 "Maximum number of nested includes exceeded "
182 "at line %zu of file %s\n", line, cur_file);
183 exit(2);
184 }
185
186 const char *dir_begin;
187 int dir_len;
188 if (yytext[0] == '/') {
189 dir_begin = "";
190 dir_len = 0;
191 } else {
192 dir_begin = cur_file;
193 const char *dir_end = strrchr(cur_file, '/');
194 if (dir_end != NULL) {
195 dir_len = (int)(dir_end + 1 - dir_begin);
196 } else {
197 dir_begin = ".";
198 dir_len = 1;
199 }
200 }
201
202 if (asprintf(&inc_file, "%.*s%s",
203 dir_len, dir_begin, yytext) == -1)
204 err(2, "Cannot construct include path");
205
206 yyin = fopen(inc_file, "r");
207
208 if (!yyin)
209 err(1, "Error opening %s", inc_file);
210
211 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
212
213 include_stack[include_ptr] = line;
214 include_files[include_ptr++] = cur_file;
215 cur_file = inc_file;
216 line = 1;
217 BEGIN(INITIAL);
218 }
219
220 <<EOF>> {
221 yypop_buffer_state();
222
223 if (!YY_CURRENT_BUFFER)
224 yyterminate();
225
226 if (--include_ptr < 0)
227 err(2, "Include stack underflow");
228
229 free(cur_file);
230 cur_file = include_files[include_ptr];
231 line = include_stack[include_ptr];
232 }
233
234 {ASSIGN} return ASSIGN;
235 {CALL2} return CALL2;
236 {CALL3} return CALL3;
237 {CALL4} return CALL4;
238 {CALL} return CALL;
239 {CHECK} return CHECK;
240 {DELAY} return DELAY;
241 {INPUT} return INPUT;
242 {NOINPUT} return NOINPUT;
243 {COMPARE} return COMPARE;
244 {COMPAREND} return COMPAREND;
245 {NON_NULL} return NON_NULL;
246 {NULL_RET} return NULL_RET;
247 {OK_RET} return OK_RET;
248 {ERR_RET} return ERR_RET;
249 {MULTIPLIER} return MULTIPLIER;
250 {COMMA} return COMMA;
251 {CCHAR} return CCHAR;
252 {WCHAR} return WCHAR;
253 {OR} return OR;
254 {LPAREN} return LPAREN;
255 {RPAREN} return RPAREN;
256 {LBRACK} return LBRACK;
257 {RBRACK} return RBRACK;
258
259 {HEX} {
260 /* Hex value, convert to decimal and return numeric */
261 unsigned long val;
262
263 if (sscanf(yytext, "%lx", &val) != 1)
264 err(1, "Bad hex conversion");
265
266 asprintf(&yylval.string, "%ld", val);
267 return numeric;
268 }
269
270 {numeric} {
271 if ((yylval.string = strdup(yytext)) == NULL)
272 err(1, "Cannot allocate numeric string");
273 return numeric;
274 }
275
276 {VARNAME} {
277 if ((yylval.string = strdup(yytext)) == NULL)
278 err(1, "Cannot allocate string for varname");
279 return VARNAME;
280 }
281
282 {FILENAME} {
283 size_t len;
284
285 if ((yylval.string = dequote(yytext, &len)) == NULL)
286 err(1, "Cannot allocate filename string");
287 return FILENAME;
288 }
289
290 /* path */
291 \/{PCHAR}+ {
292 size_t len;
293 if ((yylval.string = dequote(yytext, &len)) == NULL)
294 err(1, "Cannot allocate string");
295 return PATH;
296 }
297
298 \'{STRING}\' {
299 char *p;
300 size_t len;
301
302 if ((yylval.retval = malloc(sizeof(ct_data_t))) == NULL)
303 err(1, "Cannot allocate return struct");
304 p = yytext;
305 p++; /* skip the leading ' */
306 if ((yylval.retval->data_value = dequote(p, &len))
307 == NULL)
308 err(1, "Cannot allocate string");
309
310 yylval.retval->data_type = data_byte;
311 /* trim trailing ' */
312 yylval.retval->data_len = len - 1;
313 return BYTE;
314 }
315
316 \`{STRING}\` {
317 char *p, *str;
318 size_t len, chlen;
319 size_t i;
320 chtype *rv;
321
322 if ((yylval.retval = malloc(sizeof(ct_data_t))) == NULL)
323 err(1, "Cannot allocate return struct");
324 p = yytext;
325 p++; /* skip the leading ` */
326 if ((str = dequote(p, &len)) == NULL)
327 err(1, "Cannot allocate string");
328 len--; /* trim trailing ` */
329 if ((len % 2) != 0)
330 len--;
331
332 chlen = ((len / 2) + 1) * sizeof(chtype);
333 if ((yylval.retval->data_value = malloc(chlen))
334 == NULL)
335 err(1, "Cannot allocate chtype array");
336
337 rv = yylval.retval->data_value;
338 for (i = 0; i < len; i += 2)
339 *rv++ = (str[i] << 8) | str[i+1];
340 *rv = __NORMAL | '\0'; /* terminates chtype array */
341 yylval.retval->data_type = data_byte;
342 yylval.retval->data_len = chlen;
343 return BYTE;
344 }
345
346 \"{STRING}\" {
347 char *p;
348 size_t len;
349
350 p = yytext;
351 p++; /* skip the leading " */
352 if ((yylval.string = dequote(p, &len)) == NULL)
353 err(1, "Cannot allocate string");
354
355 /* remove trailing " */
356 yylval.string[len - 1] = '\0';
357 return STRING;
358 }
359
360 \${VARNAME} {
361 char *p;
362
363 p = yytext;
364 p++; /* skip $ before var name */
365 if ((yylval.string = strdup(p)) == NULL)
366 err(1, "Cannot allocate string for varname");
367 return VARIABLE;
368 }
369
370 /* whitespace, comments */
371 [ \t\r] |
372 #.* ;
373
374 ^[ \t\r]*#.*\n |
375 \\\n |
376 ^\n line++;
377
378 /* eol on a line with data. need to process, return eol */
379 #.*\n |
380 \n {
381 line++;
382 return EOL;
383 }
384
385 . {
386 if (isprint((unsigned char)yytext[0]))
387 errx(1, "%s:%zu: Invalid character '%c'",
388 cur_file, line + 1, yytext[0]);
389 else
390 errx(1, "%s:%zu: Invalid character '0x%02x'",
391 cur_file, line + 1, yytext[0]);
392 }
393
394 %%
395
396 int
397 yywrap(void)
398 {
399 return 1;
400 }
401