scan.l revision 1.23 1 %{
2 /* $NetBSD: scan.l,v 1.23 2015/06/16 21:12:19 christos Exp $ */
3
4 /*
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This software was developed by the Computer Systems Engineering group
9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 * contributed to Berkeley.
11 *
12 * All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by the University of
15 * California, Lawrence Berkeley Laboratories.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * from: @(#)scan.l 8.1 (Berkeley) 6/6/93
42 */
43
44 #include <sys/cdefs.h>
45 __RCSID("$NetBSD: scan.l,v 1.23 2015/06/16 21:12:19 christos Exp $");
46
47 #include <sys/param.h>
48 #include <errno.h>
49 #include <libgen.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <stddef.h>
55 #include <ctype.h>
56 #include <util.h>
57 #undef ECHO
58 #include "defs.h"
59 #include "gram.h"
60
61 int yyline;
62 const char *yyfile;
63 const char *lastfile;
64 char curinclpath[PATH_MAX];
65 int ifdefstate = -1;
66 int st;
67 #define IDS_PARENT_DISABLED \
68 ((ifdefstate > 6) && ((((ifdefstate/6)-1) & 1) == 1))
69 #define IDS_MAX_DEPTH 362797056 /* 6^11 */
70 /* States for ifdefstate:
71
72 0 -> matched ifdef
73 1 -> unmatched ifdef
74 2 -> matched elifdef
75 3 -> unmatched elifdef
76 4 -> matched else
77 5 -> unmatched else
78
79 Upon "ifdef", add one and multiply by 6.
80 Upon "endif", divide by 6, remove 1.
81
82 ifdef -> MATCH => continue
83 MISMATCH => set to 1
84 elifdef -> if (!1) -> MISMATCH
85 MATCH => set to 2
86 MISMATCH => if (2 || 3) set to 3, else set to 1
87 else -> if (1) -> MATCH
88 MATCH => set to 4
89 MISMATCH => set to 5
90
91 in each case, if parent & 1 == 1, MISMATCH
92 */
93
94 /*
95 * Data for returning to previous files from include files.
96 */
97 struct incl {
98 struct incl *in_prev; /* previous includes in effect, if any */
99 YY_BUFFER_STATE in_buf; /* previous lex state */
100 const char *in_fname; /* previous file name */
101 int in_lineno; /* previous line number */
102 int in_ateof; /* token to insert at EOF */
103 int in_interesting; /* previous value for "interesting" */
104 int in_ifdefstate; /* conditional level */
105 };
106 static struct incl *incl;
107 static int endinclude(void);
108 static int getincludepath(void);
109 static int getcurifdef(void);
110
111
112 %}
113
114 %option noyywrap nounput noinput
115
116 PATH [A-Za-z_0-9]*[./][-A-Za-z_0-9./]*
117 QCHARS \"(\\.|[^\\"])*\"
118 WORD [A-Za-z_][-A-Za-z_0-9]*
119 FILENAME ({PATH}|{QCHARS})
120 RESTOFLINE [ \t]*(#[^\n]*)?\n
121
122 %x IGNORED
123
124 %%
125 /* Local variables for yylex() */
126 int tok;
127
128 and return AND;
129 at return AT;
130 attach return ATTACH;
131 block return BLOCK;
132 build return BUILD;
133 char return CHAR;
134 compile-with return COMPILE_WITH;
135 config return CONFIG;
136 deffs return DEFFS;
137 define return DEFINE;
138 defflag return DEFFLAG;
139 defopt return DEFOPT;
140 defparam return DEFPARAM;
141 defpseudo return DEFPSEUDO;
142 defpseudodev return DEFPSEUDODEV;
143 devclass return DEVCLASS;
144 device return DEVICE;
145 device-major return DEVICE_MAJOR;
146 dumps return DUMPS;
147 file return XFILE;
148 file-system return FILE_SYSTEM;
149 flags return FLAGS;
150 ident return IDENT;
151 ioconf return IOCONF;
152 linkzero return LINKZERO;
153 machine return XMACHINE;
154 major return MAJOR;
155 makeoptions return MAKEOPTIONS;
156 maxpartitions return MAXPARTITIONS;
157 maxusers return MAXUSERS;
158 minor return MINOR;
159 needs-count return NEEDS_COUNT;
160 needs-flag return NEEDS_FLAG;
161 no return NO;
162 object return XOBJECT;
163 obsolete return OBSOLETE;
164 on return ON;
165 options return OPTIONS;
166 prefix return PREFIX;
167 pseudo-device return PSEUDO_DEVICE;
168 pseudo-root return PSEUDO_ROOT;
169 root return ROOT;
170 select return SELECT;
171 single return SINGLE;
172 source return SOURCE;
173 type return TYPE;
174 vector return VECTOR;
175 version return VERSION;
176 with return WITH;
177
178 \+= return PLUSEQ;
179 := return COLONEQ;
180
181 <*>ifdef[ \t]+{WORD}{RESTOFLINE} {
182 ifdefstate = (ifdefstate + 1) * 6;
183 if (ifdefstate >= IDS_MAX_DEPTH) {
184 yyerror("too many levels of conditional");
185 }
186 if (!IDS_PARENT_DISABLED && getcurifdef()) {
187 BEGIN(INITIAL);
188 } else {
189 ifdefstate++;
190 BEGIN(IGNORED);
191 }
192 yyline++;
193 }
194
195 <*>ifndef[ \t]+{WORD}{RESTOFLINE} {
196 ifdefstate = (ifdefstate + 1) * 6;
197 if (ifdefstate >= IDS_MAX_DEPTH) {
198 yyerror("too many levels of conditional");
199 }
200 if (!IDS_PARENT_DISABLED && !getcurifdef()) {
201 BEGIN(INITIAL);
202 } else {
203 ifdefstate++;
204 BEGIN(IGNORED);
205 }
206 yyline++;
207 }
208
209
210 <*>elifdef[ \t]+{WORD}{RESTOFLINE} {
211 st = ifdefstate % 6;
212 if (ifdefstate < 0 || st > 3) {
213 yyerror("mismatched elifdef");
214 }
215 if (IDS_PARENT_DISABLED ||
216 st != 1 || !getcurifdef()) {
217 if (st == 2 || st == 3) {
218 ifdefstate += 3 - st;
219 } else {
220 ifdefstate += 1 - st;
221 }
222 BEGIN(IGNORED);
223 } else {
224 ifdefstate++;
225 BEGIN(INITIAL);
226 }
227 yyline++;
228 }
229
230 <*>elifndef[ \t]+{WORD}{RESTOFLINE} {
231 st = ifdefstate % 6;
232 if (ifdefstate < 0 || st > 3) {
233 yyerror("mismatched elifndef");
234 }
235 if (IDS_PARENT_DISABLED ||
236 st != 1 || getcurifdef()) {
237 if (st == 2 || st == 3) {
238 ifdefstate += 3 - st;
239 } else {
240 ifdefstate += 1 - st;
241 }
242 BEGIN(IGNORED);
243 } else {
244 ifdefstate++;
245 BEGIN(INITIAL);
246 }
247 yyline++;
248 }
249
250 <*>else{RESTOFLINE} {
251 st = ifdefstate % 6;
252 if (ifdefstate < 0 || st > 3) {
253 yyerror("mismatched else");
254 }
255 if (!IDS_PARENT_DISABLED && (st == 1)) {
256 ifdefstate += 3;
257 BEGIN(INITIAL);
258 } else {
259 ifdefstate += 5 - st;
260 BEGIN(IGNORED);
261 }
262 yyline++;
263 }
264
265 <*>endif{RESTOFLINE} {
266 if (ifdefstate < 0) {
267 yyerror("mismatched endif");
268 }
269 if (!IDS_PARENT_DISABLED) {
270 BEGIN(INITIAL);
271 }
272 ifdefstate = (ifdefstate/6) - 1;
273 yyline++;
274 }
275
276 <IGNORED>\n {
277 yyline++;
278 }
279
280 <IGNORED>. /* ignore */
281
282 include[ \t]+{FILENAME}{RESTOFLINE} {
283 yyline++;
284 if (getincludepath()) {
285 include(curinclpath, 0, 0, 1);
286 } else {
287 yyerror("bad include path-name");
288 }
289 }
290
291 cinclude[ \t]+{FILENAME}{RESTOFLINE} {
292 yyline++;
293 if (getincludepath()) {
294 include(curinclpath, 0, 1, 1);
295 } else {
296 yyerror("bad cinclude path-name");
297 }
298 }
299
300 package[ \t]+{FILENAME}{RESTOFLINE} {
301 yyline++;
302 if (!oktopackage) {
303 yyerror("package not allowed here");
304 } else if (getincludepath()) {
305 package(curinclpath);
306 } else {
307 yyerror("bad package path-name");
308 }
309 }
310
311 {PATH} {
312 yylval.str = intern(yytext);
313 return PATHNAME;
314 }
315
316 {WORD} {
317 yylval.str = intern(yytext);
318 return WORD;
319 }
320
321 \"\" {
322 yylval.str = intern("");
323 return EMPTYSTRING;
324 }
325
326 {QCHARS} {
327 size_t l = strlen(yytext);
328 if (l > 1 && yytext[l - 1] == '"')
329 yytext[l - 1] = '\0';
330
331 yylval.str = intern(yytext + 1);
332 return QSTRING;
333 }
334 0[0-7]* {
335 yylval.num.fmt = 8;
336 yylval.num.val = strtoll(yytext, NULL, 8);
337 return NUMBER;
338 }
339 0[xX][0-9a-fA-F]+ {
340 yylval.num.fmt = 16;
341 yylval.num.val = (long long)strtoull(yytext + 2, NULL, 16);
342 return NUMBER;
343 }
344 [1-9][0-9]* {
345 yylval.num.fmt = 10;
346 yylval.num.val = strtoll(yytext, NULL, 10);
347 return NUMBER;
348 }
349 \n[ \t] {
350 /*
351 * Note: newline followed by whitespace is always a
352 * continuation of the previous line, so do NOT
353 * return a token in this case.
354 */
355 yyline++;
356 }
357 \n {
358 yyline++;
359 return '\n';
360 }
361 \00 {
362 /* Detect NUL characters in the config file and
363 * error out.
364 */
365 cfgerror("NUL character detected at line %i", yyline);
366 }
367 #.* { /* ignored (comment) */; }
368 [ \t]+ { /* ignored (white space) */; }
369 . { return yytext[0]; }
370 <*><<EOF>> {
371 if (ifdefstate > (incl == NULL ? -1 : incl->in_ifdefstate)) {
372 yyerror("reached EOF while looking for endif");
373 }
374 if (incl == NULL)
375 return YY_NULL;
376 tok = endinclude();
377 if (tok)
378 return tok;
379 /* otherwise continue scanning */
380 }
381
382 %%
383
384 int interesting = 1;
385
386 static int
387 curdir_push(const char *fname)
388 {
389 struct prefix *pf;
390 char *p, *d, *f;
391
392 /* Set up the initial "current directory" for include directives. */
393 d = dirname(f = estrdup(fname));
394 if (*d == '/')
395 p = estrdup(d);
396 else {
397 char *cwd, buf[PATH_MAX];
398
399 if ((cwd = getcwd(buf, sizeof(buf))) == NULL) {
400 free(f);
401 return (-1);
402 }
403 easprintf(&p, "%s/%s", cwd, d);
404 }
405 free(f);
406 pf = ecalloc(1, sizeof(*pf));
407 pf->pf_prefix = p;
408 SLIST_INSERT_HEAD(&curdirs, pf, pf_next);
409
410 return (0);
411 }
412
413 static void
414 curdir_pop(void)
415 {
416 struct prefix *pf;
417
418 pf = SLIST_FIRST(&curdirs);
419 SLIST_REMOVE_HEAD(&curdirs, pf_next);
420 if (SLIST_EMPTY(&curdirs))
421 panic("curdirs is empty");
422 /* LINTED cast away const (pf_prefix is malloc'd for curdirs) */
423 free((void *)__UNCONST(pf->pf_prefix));
424 free(pf);
425 }
426
427 /*
428 * Open the "main" file (conffile).
429 */
430 int
431 firstfile(const char *fname)
432 {
433
434 #if defined(__NetBSD__)
435 if ((yyin = fopen(fname, "rf")) == NULL)
436 #else
437 if ((yyin = fopen(fname, "r")) == NULL)
438 #endif
439 return (-1);
440
441 if (curdir_push(fname) == -1)
442 return (-1);
443
444 yyfile = conffile = fname;
445 yyline = 1;
446 return (0);
447 }
448
449 /*
450 * Add a "package" to the configuration. This is essentially
451 * syntactic sugar around the sequence:
452 *
453 * prefix ../some/directory
454 * include "files.package"
455 * prefix
456 */
457 void
458 package(const char *fname)
459 {
460 char *fname1 = estrdup(fname);
461 char *fname2 = estrdup(fname);
462 char *dir = dirname(fname1);
463 char *file = basename(fname2);
464
465 /*
466 * Push the prefix on to the prefix stack and process the include
467 * file. When we reach the end of the include file, inserting
468 * the PREFIX token into the input stream will pop the prefix off
469 * of the prefix stack.
470 */
471 prefix_push(dir);
472 (void) include(file, PREFIX, 0, 1);
473
474 free(fname1);
475 free(fname2);
476 }
477
478 /*
479 * Open the named file for inclusion at the current point. Returns 0 on
480 * success (file opened and previous state pushed), nonzero on failure
481 * (fopen failed, complaint made). The `ateof' parameter controls the
482 * token to be inserted at the end of the include file (i.e. ENDFILE).
483 * If ateof == 0 then nothing is inserted.
484 */
485 int
486 include(const char *fname, int ateof, int conditional, int direct)
487 {
488 FILE *fp;
489 struct incl *in;
490 char *s;
491 static int havedirs;
492 extern int vflag;
493
494 if (havedirs == 0) {
495 havedirs = 1;
496 setupdirs();
497 }
498
499 if (fname[0] == '/')
500 s = estrdup(fname);
501 else if (fname[0] == '.' && fname[1] == '/') {
502 struct prefix *pf = SLIST_FIRST(&curdirs);
503 easprintf(&s, "%s/%s", pf->pf_prefix, fname + 2);
504 } else
505 s = sourcepath(fname);
506 if ((fp = fopen(s, "r")) == NULL) {
507 if (conditional == 0)
508 cfgerror("cannot open %s for reading: %s", s,
509 strerror(errno));
510 else if (vflag)
511 cfgwarn("cannot open conditional include file %s: %s",
512 s, strerror(errno));
513 free(s);
514 return (-1);
515 }
516 if (curdir_push(s) == -1) {
517 cfgerror("cannot record current working directory for %s", s);
518 fclose(fp);
519 free(s);
520 return (-1);
521 }
522 in = ecalloc(1, sizeof *in);
523 in->in_prev = incl;
524 in->in_buf = YY_CURRENT_BUFFER;
525 in->in_fname = yyfile;
526 in->in_lineno = yyline;
527 in->in_ateof = ateof;
528 in->in_interesting = interesting;
529 in->in_ifdefstate = ifdefstate;
530 interesting = direct & interesting;
531 if (interesting)
532 logconfig_include(fp, fname);
533 incl = in;
534 CFGDBG(1, "include `%s' from `%s' line %d", fname, yyfile, yyline);
535 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
536 yyfile = intern(s);
537 yyline = 1;
538 free(s);
539 return (0);
540 }
541
542 /*
543 * Extract the pathname from a include/cinclude/package into curinclpath
544 */
545 static int
546 getincludepath(void)
547 {
548 const char *p = yytext;
549 ptrdiff_t len;
550 const char *e;
551
552 while (*p && isascii((unsigned int)*p) && !isspace((unsigned int)*p))
553 p++;
554 while (*p && isascii((unsigned int)*p) && isspace((unsigned int)*p))
555 p++;
556 if (!*p)
557 return 0;
558 if (*p == '"') {
559 p++;
560 e = strchr(p, '"');
561 if (!e) return 0;
562 } else {
563 e = p;
564 while (*e && isascii((unsigned int)*e)
565 && !isspace((unsigned int)*e))
566 e++;
567 }
568
569 len = e-p;
570 if (len > (ptrdiff_t)sizeof(curinclpath)-1)
571 len = sizeof(curinclpath)-1;
572 strncpy(curinclpath, p, sizeof(curinclpath));
573 curinclpath[len] = '\0';
574
575 return 1;
576 }
577
578 /*
579 * Terminate the most recent inclusion.
580 */
581 static int
582 endinclude(void)
583 {
584 struct incl *in;
585 int ateof;
586
587 curdir_pop();
588 if ((in = incl) == NULL)
589 panic("endinclude");
590 incl = in->in_prev;
591 lastfile = yyfile;
592 yy_delete_buffer(YY_CURRENT_BUFFER);
593 (void)fclose(yyin);
594 yy_switch_to_buffer(in->in_buf);
595 yyfile = in->in_fname;
596 yyline = in->in_lineno;
597 ateof = in->in_ateof;
598 interesting = in->in_interesting;
599 free(in);
600
601 return (ateof);
602 }
603
604 /*
605 * Return the current line number. If yacc has looked ahead and caused
606 * us to consume a newline, we have to subtract one. yychar is yacc's
607 * token lookahead, so we can tell.
608 */
609 u_short
610 currentline(void)
611 {
612 extern int yychar;
613
614 return (u_short)(yyline - (yychar == '\n'));
615 }
616
617 static int
618 getcurifdef(void)
619 {
620 char *p = yytext, *q;
621
622 while (*p && isascii((unsigned int)*p) && !isspace((unsigned int)*p))
623 p++;
624 while (*p && isascii((unsigned int)*p) && isspace((unsigned int)*p))
625 p++;
626 q = p;
627 while (*q && isascii((unsigned int)*q) && !isspace((unsigned int)*q))
628 q++;
629 *q = '\0';
630
631 return ht_lookup(attrtab, intern(p)) != NULL;
632 }
633