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