scan.l revision 1.5       1 %{
      2 /*	$NetBSD: scan.l,v 1.5 2006/08/26 18:17:13 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/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 
     63 /*
     64  * Data for returning to previous files from include files.
     65  */
     66 struct incl {
     67 	struct	incl *in_prev;	/* previous includes in effect, if any */
     68 	YY_BUFFER_STATE in_buf;	/* previous lex state */
     69 	const char *in_fname;	/* previous file name */
     70 	int	in_lineno;	/* previous line number */
     71 	int	in_ateof;	/* token to insert at EOF */
     72 	int	in_interesting;	/* previous value for "interesting" */
     73 };
     74 static struct incl *incl;
     75 static int endinclude(void);
     76 static int getincludepath(void);
     77 
     78 #define	yywrap() 1
     79 
     80 %}
     81 
     82 PATH	[A-Za-z_0-9]*[./][-A-Za-z_0-9./]*
     83 QCHARS	([^"\n]|\\\")+
     84 WORD	[A-Za-z_][-A-Za-z_0-9]*
     85 FILENAME	({PATH}|\"{QCHARS}\")
     86 
     87 %%
     88 		/* Local variables for yylex() */
     89 		int tok;
     90 
     91 and		return AND;
     92 at		return AT;
     93 attach		return ATTACH;
     94 block		return BLOCK;
     95 build		return BUILD;
     96 char		return CHAR;
     97 compile-with	return COMPILE_WITH;
     98 config		return CONFIG;
     99 deffs		return DEFFS;
    100 define		return DEFINE;
    101 defflag		return DEFFLAG;
    102 defopt		return DEFOPT;
    103 defparam	return DEFPARAM;
    104 defpseudo	return DEFPSEUDO;
    105 devclass	return DEVCLASS;
    106 device		return DEVICE;
    107 device-major	return DEVICE_MAJOR;
    108 dumps		return DUMPS;
    109 file		return XFILE;
    110 file-system	return FILE_SYSTEM;
    111 flags		return FLAGS;
    112 ident		return IDENT;
    113 machine		return XMACHINE;
    114 major		return MAJOR;
    115 makeoptions	return MAKEOPTIONS;
    116 maxpartitions	return MAXPARTITIONS;
    117 maxusers	return MAXUSERS;
    118 minor		return MINOR;
    119 needs-count	return NEEDS_COUNT;
    120 needs-flag	return NEEDS_FLAG;
    121 no		return NO;
    122 object		return XOBJECT;
    123 obsolete	return OBSOLETE;
    124 on		return ON;
    125 options		return OPTIONS;
    126 prefix		return PREFIX;
    127 pseudo-device	return PSEUDO_DEVICE;
    128 root		return ROOT;
    129 source		return SOURCE;
    130 type		return TYPE;
    131 version 	return VERSION;
    132 with		return WITH;
    133 
    134 \+=		return PLUSEQ;
    135 
    136 include[ \t]+{FILENAME}	{
    137 		if (getincludepath()) {
    138 			include(curinclpath, 0, 0, 1);
    139 		} else {
    140 			yyerror("bad include path-name");
    141 		}
    142 	}
    143 
    144 cinclude[ \t]+{FILENAME}	{
    145 		if (getincludepath()) {
    146 			include(curinclpath, 0, 1, 1);
    147 		} else {
    148 			yyerror("bad cinclude path-name");
    149 		}
    150 	}
    151 
    152 package[ \t]+{FILENAME}	{
    153 		if (!oktopackage) {
    154 			yyerror("package not allowed here");
    155 		} else if (getincludepath()) {
    156 			package(curinclpath);
    157 		} else {
    158 			yyerror("bad package path-name");
    159 		}
    160 	}
    161 
    162 {PATH}	{
    163 		yylval.str = intern(yytext);
    164 		return PATHNAME;
    165 	}
    166 
    167 {WORD}	{
    168 		yylval.str = intern(yytext);
    169 		return WORD;
    170 	}
    171 
    172 \"\" {
    173 		yylval.str = intern("");
    174 		return EMPTYSTRING;
    175 	}
    176 
    177 \"{QCHARS}	{
    178 		tok = input();  /* eat closing quote */
    179 		if (tok != '"') {
    180 			error("closing quote missing\n");
    181 			unput(tok);
    182 		}
    183 		yylval.str = intern(yytext + 1);
    184 		return QSTRING;
    185 	}
    186 0[0-7]*	{
    187 		yylval.num.fmt = 8;
    188 		yylval.num.val = strtoll(yytext, NULL, 8);
    189 		return NUMBER;
    190 	}
    191 0[xX][0-9a-fA-F]+ {
    192 		yylval.num.fmt = 16;
    193 		yylval.num.val = strtoull(yytext + 2, NULL, 16);
    194 		return NUMBER;
    195 	}
    196 [1-9][0-9]* {
    197 		yylval.num.fmt = 10;
    198 		yylval.num.val = strtoll(yytext, NULL, 10);
    199 		return NUMBER;
    200 	}
    201 \n[ \t] {
    202 		/*
    203 		 * Note: newline followed by whitespace is always a
    204 		 * continuation of the previous line, so do NOT
    205 		 * return a token in this case.
    206 		 */
    207 		yyline++;
    208 	}
    209 \n	{
    210 		yyline++;
    211 		return '\n';
    212 	}
    213 \00	{
    214 		/* Detect NUL characters in the config file and
    215 		 * error out.
    216 		 */
    217 		error("NUL character detected at line %i\n", yyline);
    218 	}
    219 #.*	{ /* ignored (comment) */; }
    220 [ \t]+	{ /* ignored (white space) */; }
    221 .	{ return yytext[0]; }
    222 <<EOF>> {
    223 		if (incl == NULL)
    224 			return YY_NULL;
    225 		tok = endinclude();
    226 		if (tok)
    227 			return tok;
    228 		/* otherwise continue scanning */
    229 	}
    230 
    231 %%
    232 
    233 int interesting = 1;
    234 
    235 static int
    236 curdir_push(const char *fname)
    237 {
    238 	struct prefix *pf;
    239 	char *p, *d, *f;
    240 
    241 	/* Set up the initial "current directory" for include directives. */
    242 	d = dirname(f = estrdup(fname));
    243 	if (*d == '/')
    244 		p = estrdup(d);
    245 	else {
    246 		char *cwd, buf[PATH_MAX];
    247 
    248 		if ((cwd = getcwd(buf, sizeof(buf))) == NULL)
    249 			return (-1);
    250 		p = emalloc(strlen(cwd) + strlen(d) + 2);
    251 		sprintf(p, "%s/%s", cwd, d);
    252 	}
    253 	free(f);
    254 	pf = ecalloc(1, sizeof(*pf));
    255 	pf->pf_prefix = p;
    256 	SLIST_INSERT_HEAD(&curdirs, pf, pf_next);
    257 
    258 	return (0);
    259 }
    260 
    261 static void
    262 curdir_pop(void)
    263 {
    264 	struct prefix *pf;
    265 
    266 	pf = SLIST_FIRST(&curdirs);
    267 	SLIST_REMOVE_HEAD(&curdirs, pf_next);
    268 	if (SLIST_EMPTY(&curdirs))
    269 		panic("curdirs is empty");
    270 	/* LINTED cast away const (pf_prefix is malloc'd for curdirs) */
    271 	free((void *)pf->pf_prefix);
    272 	free(pf);
    273 }
    274 
    275 /*
    276  * Open the "main" file (conffile).
    277  */
    278 int
    279 firstfile(const char *fname)
    280 {
    281 
    282 #if defined(__NetBSD__)
    283 	if ((yyin = fopen(fname, "rf")) == NULL)
    284 #else
    285 	if ((yyin = fopen(fname, "r")) == NULL)
    286 #endif
    287 		return (-1);
    288 
    289 	if (curdir_push(fname) == -1)
    290 		return (-1);
    291 
    292 	yyfile = conffile = fname;
    293 	yyline = 1;
    294 	return (0);
    295 }
    296 
    297 /*
    298  * Add a "package" to the configuration.  This is essentially
    299  * syntactic sugar around the sequence:
    300  *
    301  *	prefix ../some/directory
    302  *	include "files.package"
    303  *	prefix
    304  */
    305 void
    306 package(const char *fname)
    307 {
    308 	char *fname1 = estrdup(fname);
    309 	char *fname2 = estrdup(fname);
    310 	char *dir = dirname(fname1);
    311 	char *file = basename(fname2);
    312 
    313 	/*
    314 	 * Push the prefix on to the prefix stack and process the include
    315 	 * file.  When we reach the end of the include file, inserting
    316 	 * the PREFIX token into the input stream will pop the prefix off
    317 	 * of the prefix stack.
    318 	 */
    319 	prefix_push(dir);
    320 	(void) include(file, PREFIX, 0, 1);
    321 
    322 	free(fname1);
    323 	free(fname2);
    324 }
    325 
    326 /*
    327  * Open the named file for inclusion at the current point.  Returns 0 on
    328  * success (file opened and previous state pushed), nonzero on failure
    329  * (fopen failed, complaint made).  The `ateof' parameter controls the
    330  * token to be inserted at the end of the include file (i.e. ENDFILE).
    331  * If ateof == 0 then nothing is inserted.
    332  */
    333 int
    334 include(const char *fname, int ateof, int conditional, int direct)
    335 {
    336 	FILE *fp;
    337 	struct incl *in;
    338 	char *s;
    339 	static int havedirs;
    340 	extern int vflag;
    341 
    342 	if (havedirs == 0) {
    343 		havedirs = 1;
    344 		setupdirs();
    345 	}
    346 
    347 	if (fname[0] == '/')
    348 		s = estrdup(fname);
    349 	else if (fname[0] == '.' && fname[1] == '/') {
    350 		struct prefix *pf = SLIST_FIRST(&curdirs);
    351 		s = emalloc(strlen(pf->pf_prefix) + strlen(fname));
    352 		sprintf(s, "%s/%s", pf->pf_prefix, fname + 2);
    353 	} else
    354 		s = sourcepath(fname);
    355 	if ((fp = fopen(s, "r")) == NULL) {
    356 		if (conditional == 0)
    357 			error("cannot open %s for reading: %s\n", s,
    358 			    strerror(errno));
    359 		else if (vflag)
    360 			warn("cannot open conditional include file %s: %s",
    361 			     s, strerror(errno));
    362 		free(s);
    363 		return (-1);
    364 	}
    365 	if (curdir_push(s) == -1) {
    366 		error("cannot record current working directory for %s\n", s);
    367 		fclose(fp);
    368 		free(s);
    369 		return (-1);
    370 	}
    371 	in = ecalloc(1, sizeof *in);
    372 	in->in_prev = incl;
    373 	in->in_buf = YY_CURRENT_BUFFER;
    374 	in->in_fname = yyfile;
    375 	in->in_lineno = yyline;
    376 	in->in_ateof = ateof;
    377 	in->in_interesting = interesting;
    378 	interesting = direct & interesting;
    379 	if (interesting)
    380 		logconfig_include(fp, fname);
    381 	incl = in;
    382 	yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
    383 	yyfile = intern(s);
    384 	yyline = 1;
    385 	free(s);
    386 	return (0);
    387 }
    388 
    389 /*
    390  * Extract the pathname from a include/cinclude/package into curinclpath
    391  */
    392 static int
    393 getincludepath()
    394 {
    395 	const char *p = yytext;
    396 
    397 	while (*p && isascii((signed char)*p) && !isspace((signed char)*p))
    398 		p++;
    399 	while (*p && isascii((signed char)*p) && isspace((signed char)*p))
    400 		p++;
    401 	if (!*p)
    402 		return 0;
    403 	if (*p == '"') {
    404 		ptrdiff_t len;
    405 		const char *e = strchr(p+1, '"');
    406 
    407 		if (!e) return 0;
    408 		len = e-p-1;
    409 		if (len > sizeof(curinclpath)-1)
    410 			len = sizeof(curinclpath)-1;
    411 		strncpy(curinclpath, p+1, sizeof(curinclpath));
    412 		curinclpath[len] = '\0';
    413 	} else {
    414 		strncpy(curinclpath, p, sizeof(curinclpath));
    415 		curinclpath[sizeof(curinclpath)-1] = '\0';
    416 	}
    417 
    418 	return 1;
    419 }
    420 
    421 /*
    422  * Terminate the most recent inclusion.
    423  */
    424 static int
    425 endinclude(void)
    426 {
    427 	struct incl *in;
    428 	int ateof;
    429 
    430 	curdir_pop();
    431 	if ((in = incl) == NULL)
    432 		panic("endinclude");
    433 	incl = in->in_prev;
    434 	lastfile = yyfile;
    435 	yy_delete_buffer(YY_CURRENT_BUFFER);
    436 	(void)fclose(yyin);
    437 	yy_switch_to_buffer(in->in_buf);
    438 	yyfile = in->in_fname;
    439 	yyline = in->in_lineno;
    440 	ateof  = in->in_ateof;
    441 	interesting = in->in_interesting;
    442 	free(in);
    443 
    444 	return (ateof);
    445 }
    446 
    447 /*
    448  * Return the current line number.  If yacc has looked ahead and caused
    449  * us to consume a newline, we have to subtract one.  yychar is yacc's
    450  * token lookahead, so we can tell.
    451  */
    452 int
    453 currentline(void)
    454 {
    455 	extern int yychar;
    456 
    457 	return (yyline - (yychar == '\n'));
    458 }
    459