2 /* $NetBSD: scan.l,v 1.12 2009/04/11 12:41:10 lukem Exp $ */
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
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.
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.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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
41 * from: @(#)scan.l 8.1 (Berkeley) 6/6/93
44 #include <sys/param.h>
61 char curinclpath[PATH_MAX];
64 #define IDS_PARENT_DISABLED \
65 ((ifdefstate > 6) && ((((ifdefstate/6)-1) & 1) == 1))
66 #define IDS_MAX_DEPTH 362797056 /* 6^11 */
67 /* States for ifdefstate:
72 3 -> unmatched elifdef
76 Upon "ifdef", add one and multiply by 6.
77 Upon "endif", divide by 6, remove 1.
79 ifdef -> MATCH => continue
81 elifdef -> if (!1) -> MISMATCH
83 MISMATCH => if (2 || 3) set to 3, else set to 1
84 else -> if (1) -> MATCH
88 in each case, if parent & 1 == 1, MISMATCH
92 * Data for returning to previous files from include files.
95 struct incl *in_prev; /* previous includes in effect, if any */
96 YY_BUFFER_STATE in_buf; /* previous lex state */
97 const char *in_fname; /* previous file name */
98 int in_lineno; /* previous line number */
99 int in_ateof; /* token to insert at EOF */
100 int in_interesting; /* previous value for "interesting" */
101 int in_ifdefstate; /* conditional level */
103 static struct incl *incl;
104 static int endinclude(void);
105 static int getincludepath(void);
106 static int getcurifdef(void);
113 PATH [A-Za-z_0-9]*[./][-A-Za-z_0-9./]*
114 QCHARS ([^"\n]|\\\")+
115 WORD [A-Za-z_][-A-Za-z_0-9]*
116 FILENAME ({PATH}|\"{QCHARS}\")
117 RESTOFLINE [ \t]*(#[^\n]*)?\n
122 /* Local variables for yylex() */
127 attach return ATTACH;
131 compile-with return COMPILE_WITH;
132 config return CONFIG;
134 define return DEFINE;
135 defflag return DEFFLAG;
136 defopt return DEFOPT;
137 defparam return DEFPARAM;
138 defpseudo return DEFPSEUDO;
139 defpseudodev return DEFPSEUDODEV;
140 devclass return DEVCLASS;
141 device return DEVICE;
142 device-major return DEVICE_MAJOR;
145 file-system return FILE_SYSTEM;
148 machine return XMACHINE;
150 makeoptions return MAKEOPTIONS;
151 maxpartitions return MAXPARTITIONS;
152 maxusers return MAXUSERS;
154 needs-count return NEEDS_COUNT;
155 needs-flag return NEEDS_FLAG;
157 object return XOBJECT;
158 obsolete return OBSOLETE;
160 options return OPTIONS;
161 prefix return PREFIX;
162 pseudo-device return PSEUDO_DEVICE;
164 source return SOURCE;
166 version return VERSION;
172 <*>ifdef[ \t]+{WORD}{RESTOFLINE} {
173 ifdefstate = (ifdefstate + 1) * 6;
174 if (ifdefstate >= IDS_MAX_DEPTH) {
175 yyerror("too many levels of conditional");
177 if (!IDS_PARENT_DISABLED && getcurifdef()) {
186 <*>ifndef[ \t]+{WORD}{RESTOFLINE} {
187 ifdefstate = (ifdefstate + 1) * 6;
188 if (ifdefstate >= IDS_MAX_DEPTH) {
189 yyerror("too many levels of conditional");
191 if (!IDS_PARENT_DISABLED && !getcurifdef()) {
201 <*>elifdef[ \t]+{WORD}{RESTOFLINE} {
203 if (ifdefstate < 0 || st > 3) {
204 yyerror("mismatched elifdef");
206 if (IDS_PARENT_DISABLED ||
207 st != 1 || !getcurifdef()) {
208 if (st == 2 || st == 3) {
209 ifdefstate += 3 - st;
211 ifdefstate += 1 - st;
221 <*>elifndef[ \t]+{WORD}{RESTOFLINE} {
223 if (ifdefstate < 0 || st > 3) {
224 yyerror("mismatched elifndef");
226 if (IDS_PARENT_DISABLED ||
227 st != 1 || getcurifdef()) {
228 if (st == 2 || st == 3) {
229 ifdefstate += 3 - st;
231 ifdefstate += 1 - st;
241 <*>else{RESTOFLINE} {
243 if (ifdefstate < 0 || st > 3) {
244 yyerror("mismatched else");
246 if (!IDS_PARENT_DISABLED && (st == 1)) {
250 ifdefstate += 5 - st;
256 <*>endif{RESTOFLINE} {
257 if (ifdefstate < 0) {
258 yyerror("mismatched endif");
260 if (!IDS_PARENT_DISABLED) {
263 ifdefstate = (ifdefstate/6) - 1;
271 <IGNORED>. /* ignore */
273 include[ \t]+{FILENAME}{RESTOFLINE} {
275 if (getincludepath()) {
276 include(curinclpath, 0, 0, 1);
278 yyerror("bad include path-name");
282 cinclude[ \t]+{FILENAME}{RESTOFLINE} {
284 if (getincludepath()) {
285 include(curinclpath, 0, 1, 1);
287 yyerror("bad cinclude path-name");
291 package[ \t]+{FILENAME}{RESTOFLINE} {
294 yyerror("package not allowed here");
295 } else if (getincludepath()) {
296 package(curinclpath);
298 yyerror("bad package path-name");
303 yylval.str = intern(yytext);
308 yylval.str = intern(yytext);
313 yylval.str = intern("");
318 tok = input(); /* eat closing quote */
320 cfgerror("closing quote missing\n");
323 yylval.str = intern(yytext + 1);
328 yylval.num.val = strtoll(yytext, NULL, 8);
333 yylval.num.val = strtoull(yytext + 2, NULL, 16);
338 yylval.num.val = strtoll(yytext, NULL, 10);
343 * Note: newline followed by whitespace is always a
344 * continuation of the previous line, so do NOT
345 * return a token in this case.
354 /* Detect NUL characters in the config file and
357 cfgerror("NUL character detected at line %i\n", yyline);
359 #.* { /* ignored (comment) */; }
360 [ \t]+ { /* ignored (white space) */; }
361 . { return yytext[0]; }
363 if (ifdefstate > (incl == NULL ? -1 : incl->in_ifdefstate)) {
364 yyerror("reached EOF while looking for endif");
371 /* otherwise continue scanning */
379 curdir_push(const char *fname)
384 /* Set up the initial "current directory" for include directives. */
385 d = dirname(f = estrdup(fname));
389 char *cwd, buf[PATH_MAX];
391 if ((cwd = getcwd(buf, sizeof(buf))) == NULL) {
395 p = emalloc(strlen(cwd) + strlen(d) + 2);
396 sprintf(p, "%s/%s", cwd, d);
399 pf = ecalloc(1, sizeof(*pf));
401 SLIST_INSERT_HEAD(&curdirs, pf, pf_next);
411 pf = SLIST_FIRST(&curdirs);
412 SLIST_REMOVE_HEAD(&curdirs, pf_next);
413 if (SLIST_EMPTY(&curdirs))
414 panic("curdirs is empty");
415 /* LINTED cast away const (pf_prefix is malloc'd for curdirs) */
416 free((void *)__UNCONST(pf->pf_prefix));
421 * Open the "main" file (conffile).
424 firstfile(const char *fname)
427 #if defined(__NetBSD__)
428 if ((yyin = fopen(fname, "rf")) == NULL)
430 if ((yyin = fopen(fname, "r")) == NULL)
434 if (curdir_push(fname) == -1)
437 yyfile = conffile = fname;
443 * Add a "package" to the configuration. This is essentially
444 * syntactic sugar around the sequence:
446 * prefix ../some/directory
447 * include "files.package"
451 package(const char *fname)
453 char *fname1 = estrdup(fname);
454 char *fname2 = estrdup(fname);
455 char *dir = dirname(fname1);
456 char *file = basename(fname2);
459 * Push the prefix on to the prefix stack and process the include
460 * file. When we reach the end of the include file, inserting
461 * the PREFIX token into the input stream will pop the prefix off
462 * of the prefix stack.
465 (void) include(file, PREFIX, 0, 1);
472 * Open the named file for inclusion at the current point. Returns 0 on
473 * success (file opened and previous state pushed), nonzero on failure
474 * (fopen failed, complaint made). The `ateof' parameter controls the
475 * token to be inserted at the end of the include file (i.e. ENDFILE).
476 * If ateof == 0 then nothing is inserted.
479 include(const char *fname, int ateof, int conditional, int direct)
494 else if (fname[0] == '.' && fname[1] == '/') {
495 struct prefix *pf = SLIST_FIRST(&curdirs);
496 s = emalloc(strlen(pf->pf_prefix) + strlen(fname));
497 sprintf(s, "%s/%s", pf->pf_prefix, fname + 2);
499 s = sourcepath(fname);
500 if ((fp = fopen(s, "r")) == NULL) {
501 if (conditional == 0)
502 cfgerror("cannot open %s for reading: %s\n", s,
505 cfgwarn("cannot open conditional include file %s: %s",
510 if (curdir_push(s) == -1) {
511 cfgerror("cannot record current working directory for %s\n", s);
516 in = ecalloc(1, sizeof *in);
518 in->in_buf = YY_CURRENT_BUFFER;
519 in->in_fname = yyfile;
520 in->in_lineno = yyline;
521 in->in_ateof = ateof;
522 in->in_interesting = interesting;
523 in->in_ifdefstate = ifdefstate;
524 interesting = direct & interesting;
526 logconfig_include(fp, fname);
528 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
536 * Extract the pathname from a include/cinclude/package into curinclpath
541 const char *p = yytext;
545 while (*p && isascii((unsigned int)*p) && !isspace((unsigned int)*p))
547 while (*p && isascii((unsigned int)*p) && isspace((unsigned int)*p))
557 while (*e && isascii((unsigned int)*e)
558 && !isspace((unsigned int)*e))
563 if (len > (ptrdiff_t)sizeof(curinclpath)-1)
564 len = sizeof(curinclpath)-1;
565 strncpy(curinclpath, p, sizeof(curinclpath));
566 curinclpath[len] = '\0';
572 * Terminate the most recent inclusion.
581 if ((in = incl) == NULL)
585 yy_delete_buffer(YY_CURRENT_BUFFER);
587 yy_switch_to_buffer(in->in_buf);
588 yyfile = in->in_fname;
589 yyline = in->in_lineno;
590 ateof = in->in_ateof;
591 interesting = in->in_interesting;
598 * Return the current line number. If yacc has looked ahead and caused
599 * us to consume a newline, we have to subtract one. yychar is yacc's
600 * token lookahead, so we can tell.
607 return (yyline - (yychar == '\n'));
613 char *p = yytext, *q;
615 while (*p && isascii((unsigned int)*p) && !isspace((unsigned int)*p))
617 while (*p && isascii((unsigned int)*p) && isspace((unsigned int)*p))
620 while (*q && isascii((unsigned int)*q) && !isspace((unsigned int)*q))
624 return ht_lookup(attrtab, intern(p)) != NULL;