testlang_conf.l revision 1.3 1 %{
2 /* $NetBSD: testlang_conf.l,v 1.3 2011/05/15 23:56:28 christos 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 char *p;
59 char *buf, *q, c;
60
61 *len = 0;
62 p = 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 = 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 't':
107 /* tab */
108 *q++ = '\t';
109 p++;
110 break;
111
112 case '\\':
113 /* backslash */
114 *q++ = '\\';
115 p++;
116 break;
117
118 default:
119 *q++ = *p++;
120 }
121 }
122 } else
123 *q++ = *p++;
124 }
125 *q++ = '\0';
126
127 return buf;
128 }
129 %}
130
131 HEX 0[xX][0-9a-zA-Z]+
132 STRING [0-9a-z!#-&(-^ \t%._\\]+
133 numeric [-0-9]+
134 PCHAR (\\.|[^ \t\n])
135 ASSIGN [aA][sS][sS][iI][gG][nN]
136 CALL2 [cC][aA][lL][lL]2
137 CALL3 [cC][aA][lL][lL]3
138 CALL4 [cC][aA][lL][lL]4
139 CALL [cC][aA][lL][lL]
140 CHECK [cC][hH][eE][cC][kK]
141 DELAY [dD][eE][lL][aA][yY]
142 INPUT [iI][nN][pP][uU][tT]
143 NOINPUT [nN][oO][iI][nN][pP][uU][tT]
144 OK_RET [oO][kK]
145 ERR_RET [eE][rR][rR]
146 COMPARE [cC][oO][mM][pP][aA][rR][eE]
147 COMPAREND [cC][oO][mM][pP][aA][rR][eE][Nn][Dd]
148 FILENAME [A-Za-z0-9.][A-Za-z0-9./_-]+
149 VARNAME [A-Za-z][A-Za-z0-9_-]+
150 NULL_RET NULL
151 NON_NULL NON_NULL
152 BYTE BYTE
153 OR \|
154 LHB \(
155 RHB \)
156
157 %x incl
158
159 %%
160
161 include BEGIN(incl);
162
163 <incl>[ \t]* /* eat the whitespace */
164 <incl>[^ \t\n]+ { /* got the include file name */
165 char inc_file[MAXPATHLEN];
166
167 if (include_ptr > MAX_INCLUDES) {
168 fprintf(stderr,
169 "Maximum number of nested includes exceeded "
170 "at line %d of file %s\n", line, cur_file);
171 exit(2);
172 }
173
174 if (yytext[0] != '/') {
175 if (strlcpy(inc_file, include_path, sizeof(inc_file))
176 >= sizeof(inc_file))
177 err(2, "CHECK_PATH too long");
178 if ((include_path[strlen(include_path) - 1] != '/') &&
179 ((strlcat(inc_file, "/", sizeof(inc_file))
180 >= sizeof(inc_file))))
181 err(2, "Could not append / to include file path");
182 } else {
183 inc_file[0] = '\0';
184 }
185
186 if (strlcat(inc_file, yytext, sizeof(inc_file))
187 >= sizeof(inc_file))
188 err(2, "Path to include file path overflowed");
189
190 yyin = fopen(inc_file, "r" );
191
192 if (!yyin) {
193 fprintf(stderr, "Error opening %s: ", inc_file);
194 perror(NULL);
195 exit(2);
196 }
197
198 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
199
200 include_stack[include_ptr] = line;
201 include_files[include_ptr++] = cur_file;
202 cur_file = malloc(strlen(inc_file));
203 if (cur_file == NULL)
204 err(2, "Cannot allocate new include file string");
205
206 strlcpy(cur_file, inc_file, sizeof(inc_file));
207 line = 0;
208 BEGIN(INITIAL);
209 }
210
211 <<EOF>> {
212 yypop_buffer_state();
213
214 if ( !YY_CURRENT_BUFFER )
215 {
216 yyterminate();
217 }
218
219 if (--include_ptr < 0)
220 err(2, "Include stack underflow");
221
222 free(cur_file);
223 cur_file = include_files[include_ptr];
224 line = include_stack[include_ptr];
225 }
226
227 {ASSIGN} {
228 return ASSIGN;
229 }
230
231 {CALL2} {
232 return CALL2;
233 }
234
235 {CALL3} {
236 return CALL3;
237 }
238
239 {CALL4} {
240 return CALL4;
241 }
242
243 {CALL} {
244 return CALL;
245 }
246
247 {CHECK} {
248 return CHECK;
249 }
250
251 {DELAY} {
252 return DELAY;
253 }
254
255 {INPUT} {
256 return INPUT;
257 }
258
259 {NOINPUT} {
260 return NOINPUT;
261 }
262
263 {COMPARE} {
264 return COMPARE;
265 }
266
267 {COMPAREND} {
268 return COMPAREND;
269 }
270
271 {NON_NULL} {
272 return NON_NULL;
273 }
274
275 {NULL_RET} {
276 return NULL_RET;
277 }
278
279 {OK_RET} {
280 return OK_RET;
281 }
282
283 {ERR_RET} {
284 return ERR_RET;
285 }
286
287 {OR} {
288 return OR;
289 }
290
291 {LHB} {
292 return LHB;
293 }
294
295 {RHB} {
296 return RHB;
297 }
298
299 {HEX} {
300 /* Hex value, convert to decimal and return numeric */
301 unsigned long val;
302
303 if (sscanf(yytext, "%lx", &val) != 1)
304 err(1, "Bad hex conversion");
305
306 asprintf(&yylval.string, "%ld", val);
307 return numeric;
308 }
309
310
311 {numeric} {
312 if ((yylval.string = strdup(yytext)) == NULL)
313 err(1, "Cannot allocate numeric string");
314 return numeric;
315 }
316
317 {VARNAME} {
318 if ((yylval.string = strdup(yytext)) == NULL)
319 err(1, "Cannot allocate string for varname");
320 return VARNAME;
321 }
322
323 {FILENAME} {
324 size_t len;
325
326 if ((yylval.string = dequote(yytext, &len)) == NULL)
327 err(1, "Cannot allocate filename string");
328 return FILENAME;
329 }
330
331 /* path */
332 \/{PCHAR}+ {
333 size_t len;
334 if ((yylval.string = dequote(yytext, &len)) == NULL)
335 err(1, "Cannot allocate string");
336 return PATH;
337 }
338
339 \'{STRING}\' {
340 char *p;
341 size_t len;
342
343 if ((yylval.retval = malloc(sizeof(returns_t))) == NULL)
344 err(1, "Cannot allocate return struct");
345 p = yytext;
346 p++; /* skip the leading ' */
347 if ((yylval.retval->return_value = dequote(p, &len))
348 == NULL)
349 err(1, "Cannot allocate string");
350
351 yylval.retval->return_type = ret_byte;
352 /* trim trailing ' */
353 yylval.retval->return_len = len - 1;
354 return BYTE;
355 }
356
357 \`{STRING}\` {
358 char *p, *str;
359 size_t len, chlen;
360 int i;
361 chtype *rv;
362
363 if ((yylval.retval = malloc(sizeof(returns_t))) == NULL)
364 err(1, "Cannot allocate return struct");
365 p = yytext;
366 p++; /* skip the leading ' */
367 if ((str = dequote(p, &len)) == NULL)
368 err(1, "Cannot allocate string");
369 len--; /* trim trailing ` */
370 if ((len % 2) != 0)
371 len--;
372
373 chlen = ((len / 2) + 1) * sizeof(chtype);
374 if ((yylval.retval->return_value = malloc(chlen))
375 == NULL)
376 err(1, "Cannot allocate chtype array");
377
378 rv = yylval.retval->return_value;
379 for (i = 0; i < len; i += 2)
380 *rv++ = (str[i] << 8) | str[i+1];
381 *rv = __NORMAL | '\0'; /* terminates chtype array */
382 yylval.retval->return_type = ret_byte;
383 yylval.retval->return_len = chlen;
384 return BYTE;
385 }
386
387 \"{STRING}\" {
388 char *p;
389 size_t len;
390
391 p = yytext;
392 p++; /* skip the leading " */
393 if ((yylval.string = dequote(p, &len)) == NULL)
394 err(1, "Cannot allocate string");
395
396 /* remove trailing " */
397 yylval.string[len - 1] = '\0';
398 return STRING;
399 }
400
401 \${VARNAME} {
402 char *p;
403
404 p = yytext;
405 p++; /* skip $ before var name */
406 if ((yylval.string = strdup(p)) == NULL)
407 err(1, "Cannot allocate string for varname");
408 return VARIABLE;
409 }
410
411 /* comments, white-outs */
412 [ \t\r] |
413 #.* ;
414 ^#.*\n |
415 #.*\n |
416 \\\n |
417 ^\n {
418 line++; }
419
420 /* eol on a line with data. need to process, return eol */
421 \n {
422 line++;
423 return EOL;
424 }
425
426 . {
427 }
428
429 %%
430
431 int
432 yywrap(void)
433 {
434 return 1;
435 }
436