scan.l revision 1.25 1 %{
2 /* $NetBSD: scan.l,v 1.25 2015/09/04 10:16:35 uebayasi 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.25 2015/09/04 10:16:35 uebayasi 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 buildprefix return BUILDPREFIX;
168 pseudo-device return PSEUDO_DEVICE;
169 pseudo-root return PSEUDO_ROOT;
170 root return ROOT;
171 select return SELECT;
172 single return SINGLE;
173 source return SOURCE;
174 type return TYPE;
175 vector return VECTOR;
176 version return VERSION;
177 with return WITH;
178
179 \+= return PLUSEQ;
180 := return COLONEQ;
181
182 <*>ifdef[ \t]+{WORD}{RESTOFLINE} {
183 ifdefstate = (ifdefstate + 1) * 6;
184 if (ifdefstate >= IDS_MAX_DEPTH) {
185 yyerror("too many levels of conditional");
186 }
187 if (!IDS_PARENT_DISABLED && getcurifdef()) {
188 BEGIN(INITIAL);
189 } else {
190 ifdefstate++;
191 BEGIN(IGNORED);
192 }
193 yyline++;
194 }
195
196 <*>ifndef[ \t]+{WORD}{RESTOFLINE} {
197 ifdefstate = (ifdefstate + 1) * 6;
198 if (ifdefstate >= IDS_MAX_DEPTH) {
199 yyerror("too many levels of conditional");
200 }
201 if (!IDS_PARENT_DISABLED && !getcurifdef()) {
202 BEGIN(INITIAL);
203 } else {
204 ifdefstate++;
205 BEGIN(IGNORED);
206 }
207 yyline++;
208 }
209
210
211 <*>elifdef[ \t]+{WORD}{RESTOFLINE} {
212 st = ifdefstate % 6;
213 if (ifdefstate < 0 || st > 3) {
214 yyerror("mismatched elifdef");
215 }
216 if (IDS_PARENT_DISABLED ||
217 st != 1 || !getcurifdef()) {
218 if (st == 2 || st == 3) {
219 ifdefstate += 3 - st;
220 } else {
221 ifdefstate += 1 - st;
222 }
223 BEGIN(IGNORED);
224 } else {
225 ifdefstate++;
226 BEGIN(INITIAL);
227 }
228 yyline++;
229 }
230
231 <*>elifndef[ \t]+{WORD}{RESTOFLINE} {
232 st = ifdefstate % 6;
233 if (ifdefstate < 0 || st > 3) {
234 yyerror("mismatched elifndef");
235 }
236 if (IDS_PARENT_DISABLED ||
237 st != 1 || getcurifdef()) {
238 if (st == 2 || st == 3) {
239 ifdefstate += 3 - st;
240 } else {
241 ifdefstate += 1 - st;
242 }
243 BEGIN(IGNORED);
244 } else {
245 ifdefstate++;
246 BEGIN(INITIAL);
247 }
248 yyline++;
249 }
250
251 <*>else{RESTOFLINE} {
252 st = ifdefstate % 6;
253 if (ifdefstate < 0 || st > 3) {
254 yyerror("mismatched else");
255 }
256 if (!IDS_PARENT_DISABLED && (st == 1)) {
257 ifdefstate += 3;
258 BEGIN(INITIAL);
259 } else {
260 ifdefstate += 5 - st;
261 BEGIN(IGNORED);
262 }
263 yyline++;
264 }
265
266 <*>endif{RESTOFLINE} {
267 if (ifdefstate < 0) {
268 yyerror("mismatched endif");
269 }
270 if (!IDS_PARENT_DISABLED) {
271 BEGIN(INITIAL);
272 }
273 ifdefstate = (ifdefstate/6) - 1;
274 yyline++;
275 }
276
277 <IGNORED>\n {
278 yyline++;
279 }
280
281 <IGNORED>. /* ignore */
282
283 include[ \t]+{FILENAME}{RESTOFLINE} {
284 yyline++;
285 if (getincludepath()) {
286 include(curinclpath, 0, 0, 1);
287 } else {
288 yyerror("bad include path-name");
289 }
290 }
291
292 cinclude[ \t]+{FILENAME}{RESTOFLINE} {
293 yyline++;
294 if (getincludepath()) {
295 include(curinclpath, 0, 1, 1);
296 } else {
297 yyerror("bad cinclude path-name");
298 }
299 }
300
301 package[ \t]+{FILENAME}{RESTOFLINE} {
302 yyline++;
303 if (!oktopackage) {
304 yyerror("package not allowed here");
305 } else if (getincludepath()) {
306 package(curinclpath);
307 } else {
308 yyerror("bad package path-name");
309 }
310 }
311
312 {PATH} {
313 yylval.str = intern(yytext);
314 return PATHNAME;
315 }
316
317 {WORD} {
318 yylval.str = intern(yytext);
319 return WORD;
320 }
321
322 \"\" {
323 yylval.str = intern("");
324 return EMPTYSTRING;
325 }
326
327 {QCHARS} {
328 size_t l = strlen(yytext);
329 if (l > 1 && yytext[l - 1] == '"')
330 yytext[l - 1] = '\0';
331
332 yylval.str = intern(yytext + 1);
333 return QSTRING;
334 }
335 0[0-7]* {
336 yylval.num.fmt = 8;
337 yylval.num.val = strtoll(yytext, NULL, 8);
338 return NUMBER;
339 }
340 0[xX][0-9a-fA-F]+ {
341 yylval.num.fmt = 16;
342 yylval.num.val = (long long)strtoull(yytext + 2, NULL, 16);
343 return NUMBER;
344 }
345 [1-9][0-9]* {
346 yylval.num.fmt = 10;
347 yylval.num.val = strtoll(yytext, NULL, 10);
348 return NUMBER;
349 }
350 \n[ \t] {
351 /*
352 * Note: newline followed by whitespace is always a
353 * continuation of the previous line, so do NOT
354 * return a token in this case.
355 */
356 yyline++;
357 }
358 \n {
359 yyline++;
360 return '\n';
361 }
362 \00 {
363 /* Detect NUL characters in the config file and
364 * error out.
365 */
366 cfgerror("NUL character detected at line %i", yyline);
367 }
368 #.* { /* ignored (comment) */; }
369 [ \t]+ { /* ignored (white space) */; }
370 . { return yytext[0]; }
371 <*><<EOF>> {
372 if (ifdefstate > (incl == NULL ? -1 : incl->in_ifdefstate)) {
373 yyerror("reached EOF while looking for endif");
374 }
375 if (incl == NULL)
376 return YY_NULL;
377 tok = endinclude();
378 if (tok)
379 return tok;
380 /* otherwise continue scanning */
381 }
382
383 %%
384
385 int interesting = 1;
386
387 static int
388 curdir_push(const char *fname)
389 {
390 struct prefix *pf;
391 char *p, *d, *f;
392
393 /* Set up the initial "current directory" for include directives. */
394 d = dirname(f = estrdup(fname));
395 if (*d == '/')
396 p = estrdup(d);
397 else {
398 char *cwd, buf[PATH_MAX];
399
400 if ((cwd = getcwd(buf, sizeof(buf))) == NULL) {
401 free(f);
402 return (-1);
403 }
404 easprintf(&p, "%s/%s", cwd, d);
405 }
406 free(f);
407 pf = ecalloc(1, sizeof(*pf));
408 pf->pf_prefix = p;
409 SLIST_INSERT_HEAD(&curdirs, pf, pf_next);
410
411 return (0);
412 }
413
414 static void
415 curdir_pop(void)
416 {
417 struct prefix *pf;
418
419 pf = SLIST_FIRST(&curdirs);
420 SLIST_REMOVE_HEAD(&curdirs, pf_next);
421 if (SLIST_EMPTY(&curdirs))
422 panic("curdirs is empty");
423 /* LINTED cast away const (pf_prefix is malloc'd for curdirs) */
424 free((void *)__UNCONST(pf->pf_prefix));
425 free(pf);
426 }
427
428 /*
429 * Open the "main" file (conffile).
430 */
431 int
432 firstfile(const char *fname)
433 {
434
435 #if defined(__NetBSD__)
436 if ((yyin = fopen(fname, "rf")) == NULL)
437 #else
438 if ((yyin = fopen(fname, "r")) == NULL)
439 #endif
440 return (-1);
441
442 if (curdir_push(fname) == -1)
443 return (-1);
444
445 yyfile = conffile = fname;
446 yyline = 1;
447 return (0);
448 }
449
450 /*
451 * Add a "package" to the configuration. This is essentially
452 * syntactic sugar around the sequence:
453 *
454 * prefix ../some/directory
455 * include "files.package"
456 * prefix
457 */
458 void
459 package(const char *fname)
460 {
461 char *fname1 = estrdup(fname);
462 char *fname2 = estrdup(fname);
463 char *dir = dirname(fname1);
464 char *file = basename(fname2);
465
466 /*
467 * Push the prefix on to the prefix stack and process the include
468 * file. When we reach the end of the include file, inserting
469 * the PREFIX token into the input stream will pop the prefix off
470 * of the prefix stack.
471 */
472 prefix_push(dir);
473 (void) include(file, PREFIX, 0, 1);
474
475 free(fname1);
476 free(fname2);
477 }
478
479 int includedepth;
480
481 /*
482 * Open the named file for inclusion at the current point. Returns 0 on
483 * success (file opened and previous state pushed), nonzero on failure
484 * (fopen failed, complaint made). The `ateof' parameter controls the
485 * token to be inserted at the end of the include file (i.e. ENDFILE).
486 * If ateof == 0 then nothing is inserted.
487 */
488 int
489 include(const char *fname, int ateof, int conditional, int direct)
490 {
491 FILE *fp;
492 struct incl *in;
493 char *s;
494 static int havedirs;
495 extern int vflag;
496
497 if (havedirs == 0) {
498 havedirs = 1;
499 setupdirs();
500 }
501
502 if (fname[0] == '/')
503 s = estrdup(fname);
504 else if (fname[0] == '.' && fname[1] == '/') {
505 struct prefix *pf = SLIST_FIRST(&curdirs);
506 easprintf(&s, "%s/%s", pf->pf_prefix, fname + 2);
507 } else
508 s = sourcepath(fname);
509 if ((fp = fopen(s, "r")) == NULL) {
510 if (conditional == 0)
511 cfgerror("cannot open %s for reading: %s", s,
512 strerror(errno));
513 else if (vflag)
514 cfgwarn("cannot open conditional include file %s: %s",
515 s, strerror(errno));
516 free(s);
517 return (-1);
518 }
519 if (curdir_push(s) == -1) {
520 cfgerror("cannot record current working directory for %s", s);
521 fclose(fp);
522 free(s);
523 return (-1);
524 }
525 in = ecalloc(1, sizeof *in);
526 in->in_prev = incl;
527 in->in_buf = YY_CURRENT_BUFFER;
528 in->in_fname = yyfile;
529 in->in_lineno = yyline;
530 in->in_ateof = ateof;
531 in->in_interesting = interesting;
532 in->in_ifdefstate = ifdefstate;
533 interesting = direct & interesting;
534 if (interesting)
535 logconfig_include(fp, fname);
536 incl = in;
537 CFGDBG(1, "include `%s' from `%s' line %d", fname, yyfile, yyline);
538 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
539 yyfile = intern(s);
540 yyline = 1;
541 free(s);
542 includedepth++;
543 return (0);
544 }
545
546 /*
547 * Extract the pathname from a include/cinclude/package into curinclpath
548 */
549 static int
550 getincludepath(void)
551 {
552 const char *p = yytext;
553 ptrdiff_t len;
554 const char *e;
555
556 while (*p && isascii((unsigned int)*p) && !isspace((unsigned int)*p))
557 p++;
558 while (*p && isascii((unsigned int)*p) && isspace((unsigned int)*p))
559 p++;
560 if (!*p)
561 return 0;
562 if (*p == '"') {
563 p++;
564 e = strchr(p, '"');
565 if (!e) return 0;
566 } else {
567 e = p;
568 while (*e && isascii((unsigned int)*e)
569 && !isspace((unsigned int)*e))
570 e++;
571 }
572
573 len = e-p;
574 if (len > (ptrdiff_t)sizeof(curinclpath)-1)
575 len = sizeof(curinclpath)-1;
576 strncpy(curinclpath, p, sizeof(curinclpath));
577 curinclpath[len] = '\0';
578
579 return 1;
580 }
581
582 /*
583 * Terminate the most recent inclusion.
584 */
585 static int
586 endinclude(void)
587 {
588 struct incl *in;
589 int ateof;
590
591 curdir_pop();
592 if ((in = incl) == NULL)
593 panic("endinclude");
594 incl = in->in_prev;
595 lastfile = yyfile;
596 yy_delete_buffer(YY_CURRENT_BUFFER);
597 (void)fclose(yyin);
598 yy_switch_to_buffer(in->in_buf);
599 yyfile = in->in_fname;
600 yyline = in->in_lineno;
601 ateof = in->in_ateof;
602 interesting = in->in_interesting;
603 free(in);
604
605 includedepth--;
606
607 return (ateof);
608 }
609
610 /*
611 * Return the current line number. If yacc has looked ahead and caused
612 * us to consume a newline, we have to subtract one. yychar is yacc's
613 * token lookahead, so we can tell.
614 */
615 u_short
616 currentline(void)
617 {
618 extern int yychar;
619
620 return (u_short)(yyline - (yychar == '\n'));
621 }
622
623 static int
624 getcurifdef(void)
625 {
626 char *p = yytext, *q;
627
628 while (*p && isascii((unsigned int)*p) && !isspace((unsigned int)*p))
629 p++;
630 while (*p && isascii((unsigned int)*p) && isspace((unsigned int)*p))
631 p++;
632 q = p;
633 while (*q && isascii((unsigned int)*q) && !isspace((unsigned int)*q))
634 q++;
635 *q = '\0';
636
637 return ht_lookup(attrtab, intern(p)) != NULL;
638 }
639