testlang_conf.l revision 1.13 1 %{
2 /* $NetBSD: testlang_conf.l,v 1.13 2021/02/07 19:49:32 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 withough 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 *include_path; /* from director.c */
49 extern char *cur_file; /* from director.c */
50
51 static int include_stack[MAX_INCLUDES];
52 static char *include_files[MAX_INCLUDES];
53 static int include_ptr = 0;
54
55 static char *
56 dequote(const char *s, size_t *len)
57 {
58 const unsigned char *p;
59 char *buf, *q;
60
61 *len = 0;
62 p = (const unsigned char *)s;
63 while (*p) {
64 if (*p == '\\' && *(p+1)) {
65 if (isdigit(*(p+1)) && *(p+2) && isdigit(*(p+2)) &&
66 *(p+3) && isdigit(*(p+3)))
67 p += 3;
68 else
69 ++p;
70 }
71 ++(*len);
72 ++p;
73 }
74
75 buf = malloc(*len + 1);
76 if (buf == NULL)
77 return NULL;
78
79 p = (const unsigned char *)s;
80 q = buf;
81 while (*p) {
82 if (*p == '\\' && *(p+1)) {
83 ++p;
84 if (isdigit(*p)) {
85 if (*(p+1) && isdigit(*(p+1)) && *(p+2) &&
86 isdigit(*(p+2))) {
87 *q++ = ((*p - '0') * 8 + (*(p+1) - '0')) * 8 + (*(p+2) - '0');
88 p += 3;
89 } else {
90 *q++ = *p++;
91 }
92 } else {
93 switch (*p) {
94 case 'e':
95 /* escape */
96 *q++ = '\e';
97 p++;
98 break;
99
100 case 'n':
101 /* newline */
102 *q++ = '\n';
103 p++;
104 break;
105
106 case 'r':
107 /* carriage return */
108 *q++ = '\r';
109 p++;
110 break;
111
112 case 't':
113 /* tab */
114 *q++ = '\t';
115 p++;
116 break;
117
118 case '\\':
119 /* backslash */
120 *q++ = '\\';
121 p++;
122 break;
123
124 default:
125 *q++ = *p++;
126 }
127 }
128 } else
129 *q++ = *p++;
130 }
131 *q++ = '\0';
132
133 return buf;
134 }
135 %}
136
137 HEX 0[xX][0-9a-zA-Z]+
138 STRING [0-9a-z!#-&(-^ \t%._\\]+
139 numeric [-0-9]+
140 PCHAR (\\.|[^ \t\n])
141 ASSIGN assign
142 CALL2 call2
143 CALL3 call3
144 CALL4 call4
145 CALL call
146 CHECK check
147 DELAY delay
148 INPUT input
149 NOINPUT noinput
150 OK_RET OK
151 ERR_RET ERR
152 COMPARE compare
153 COMPAREND comparend
154 FILENAME [A-Za-z0-9.][A-Za-z0-9./_-]+
155 VARNAME [A-Za-z][A-Za-z0-9_-]+
156 NULL_RET NULL
157 NON_NULL NON_NULL
158 CCHAR cchar
159 WCHAR wchar
160 BYTE BYTE
161 OR \|
162 LPAREN \(
163 RPAREN \)
164 LBRACK \[
165 RBRACK \]
166 MULTIPLIER \*
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[MAXPATHLEN];
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 if (yytext[0] != '/') {
187 if (strlcpy(inc_file, include_path, sizeof(inc_file))
188 >= sizeof(inc_file))
189 err(2, "CHECK_PATH too long");
190 if ((include_path[strlen(include_path) - 1] != '/') &&
191 ((strlcat(inc_file, "/", sizeof(inc_file))
192 >= sizeof(inc_file))))
193 err(2, "Could not append / to include file path");
194 } else {
195 inc_file[0] = '\0';
196 }
197
198 if (strlcat(inc_file, yytext, sizeof(inc_file))
199 >= sizeof(inc_file))
200 err(2, "Path to include file path overflowed");
201
202 yyin = fopen(inc_file, "r" );
203
204 if (!yyin)
205 err(1, "Error opening %s", inc_file);
206
207 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
208
209 include_stack[include_ptr] = line;
210 include_files[include_ptr++] = cur_file;
211 cur_file = strdup(inc_file);
212 if (cur_file == NULL)
213 err(2, "Cannot allocate new include file string");
214 line = 0;
215 BEGIN(INITIAL);
216 }
217
218 <<EOF>> {
219 yypop_buffer_state();
220
221 if (!YY_CURRENT_BUFFER)
222 yyterminate();
223
224 if (--include_ptr < 0)
225 err(2, "Include stack underflow");
226
227 free(cur_file);
228 cur_file = include_files[include_ptr];
229 line = include_stack[include_ptr];
230 }
231
232 {ASSIGN} return ASSIGN;
233 {CALL2} return CALL2;
234 {CALL3} return CALL3;
235 {CALL4} return CALL4;
236 {CALL} return CALL;
237 {CHECK} return CHECK;
238 {DELAY} return DELAY;
239 {INPUT} return INPUT;
240 {NOINPUT} return NOINPUT;
241 {COMPARE} return COMPARE;
242 {COMPAREND} return COMPAREND;
243 {NON_NULL} return NON_NULL;
244 {NULL_RET} return NULL_RET;
245 {OK_RET} return OK_RET;
246 {ERR_RET} return ERR_RET;
247 {MULTIPLIER} return MULTIPLIER;
248 {CCHAR} return CCHAR;
249 {WCHAR} return WCHAR;
250 {OR} return OR;
251 {LPAREN} return LPAREN;
252 {RPAREN} return RPAREN;
253 {LBRACK} return LBRACK;
254 {RBRACK} return RBRACK;
255
256 {HEX} {
257 /* Hex value, convert to decimal and return numeric */
258 unsigned long val;
259
260 if (sscanf(yytext, "%lx", &val) != 1)
261 err(1, "Bad hex conversion");
262
263 asprintf(&yylval.string, "%ld", val);
264 return numeric;
265 }
266
267 {numeric} {
268 if ((yylval.string = strdup(yytext)) == NULL)
269 err(1, "Cannot allocate numeric string");
270 return numeric;
271 }
272
273 {VARNAME} {
274 if ((yylval.string = strdup(yytext)) == NULL)
275 err(1, "Cannot allocate string for varname");
276 return VARNAME;
277 }
278
279 {FILENAME} {
280 size_t len;
281
282 if ((yylval.string = dequote(yytext, &len)) == NULL)
283 err(1, "Cannot allocate filename string");
284 return FILENAME;
285 }
286
287 /* path */
288 \/{PCHAR}+ {
289 size_t len;
290 if ((yylval.string = dequote(yytext, &len)) == NULL)
291 err(1, "Cannot allocate string");
292 return PATH;
293 }
294
295 \'{STRING}\' {
296 char *p;
297 size_t len;
298
299 if ((yylval.retval = malloc(sizeof(ct_data_t))) == NULL)
300 err(1, "Cannot allocate return struct");
301 p = yytext;
302 p++; /* skip the leading ' */
303 if ((yylval.retval->data_value = dequote(p, &len))
304 == NULL)
305 err(1, "Cannot allocate string");
306
307 yylval.retval->data_type = data_byte;
308 /* trim trailing ' */
309 yylval.retval->data_len = len - 1;
310 return BYTE;
311 }
312
313 \`{STRING}\` {
314 char *p, *str;
315 size_t len, chlen;
316 size_t i;
317 chtype *rv;
318
319 if ((yylval.retval = malloc(sizeof(ct_data_t))) == NULL)
320 err(1, "Cannot allocate return struct");
321 p = yytext;
322 p++; /* skip the leading ` */
323 if ((str = dequote(p, &len)) == NULL)
324 err(1, "Cannot allocate string");
325 len--; /* trim trailing ` */
326 if ((len % 2) != 0)
327 len--;
328
329 chlen = ((len / 2) + 1) * sizeof(chtype);
330 if ((yylval.retval->data_value = malloc(chlen))
331 == NULL)
332 err(1, "Cannot allocate chtype array");
333
334 rv = yylval.retval->data_value;
335 for (i = 0; i < len; i += 2)
336 *rv++ = (str[i] << 8) | str[i+1];
337 *rv = __NORMAL | '\0'; /* terminates chtype array */
338 yylval.retval->data_type = data_byte;
339 yylval.retval->data_len = chlen;
340 return BYTE;
341 }
342
343 \"{STRING}\" {
344 char *p;
345 size_t len;
346
347 p = yytext;
348 p++; /* skip the leading " */
349 if ((yylval.string = dequote(p, &len)) == NULL)
350 err(1, "Cannot allocate string");
351
352 /* remove trailing " */
353 yylval.string[len - 1] = '\0';
354 return STRING;
355 }
356
357 \${VARNAME} {
358 char *p;
359
360 p = yytext;
361 p++; /* skip $ before var name */
362 if ((yylval.string = strdup(p)) == NULL)
363 err(1, "Cannot allocate string for varname");
364 return VARIABLE;
365 }
366
367 /* whitespace, comments */
368 [ \t\r] |
369 #.* ;
370
371 ^[ \t\r]*#.*\n |
372 \\\n |
373 ^\n line++;
374
375 /* eol on a line with data. need to process, return eol */
376 #.*\n |
377 \n {
378 line++;
379 return EOL;
380 }
381
382 . {
383 /* FIXME: report syntax error */
384 }
385
386 %%
387
388 int
389 yywrap(void)
390 {
391 return 1;
392 }
393