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