xref: /openbsd/usr.sbin/config/scan.l (revision 07ea8d15)
1 %{
2 /*	$OpenBSD: scan.l,v 1.6 1996/10/23 22:37:59 niklas Exp $	*/
3 /*	$NetBSD: scan.l,v 1.7 1996/08/31 21:15:13 mycroft Exp $	*/
4 
5 /*
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This software was developed by the Computer Systems Engineering group
10  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11  * contributed to Berkeley.
12  *
13  * All advertising materials mentioning features or use of this software
14  * must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Lawrence Berkeley Laboratories.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. All advertising materials mentioning features or use of this software
27  *    must display the following acknowledgement:
28  *	This product includes software developed by the University of
29  *	California, Berkeley and its contributors.
30  * 4. Neither the name of the University nor the names of its contributors
31  *    may be used to endorse or promote products derived from this software
32  *    without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  *
46  *	from: @(#)scan.l	8.1 (Berkeley) 6/6/93
47  */
48 
49 #include <sys/param.h>
50 #include <errno.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include "config.h"
56 #include "y.tab.h"
57 
58 int	yyline;
59 const char *yyfile;
60 const char *lastfile;
61 
62 int	include __P((const char *, int));
63 
64 /*
65  * Data for returning to previous files from include files.
66  */
67 struct incl {
68 	struct	incl *in_prev;	/* previous includes in effect, if any */
69 	YY_BUFFER_STATE in_buf;	/* previous lex state */
70 	const char *in_fname;	/* previous file name */
71 	int	in_lineno;	/* previous line number */
72 	int	in_preveof;	/* previous eoftoken */
73 };
74 static struct incl *incl;
75 static int eoftoken;		/* current EOF token */
76 static void endinclude __P((void));
77 
78 #define	yywrap() 1
79 
80 %}
81 
82 PATH	[-/A-Za-z0-9_.]*[./][-/A-Za-z0-9_.]*
83 WORD	[A-Za-z_][-A-Za-z_0-9]*
84 
85 %%
86 
87 		/* plain keywords */
88 and		{ return AND; }
89 at		{ return AT; }
90 attach		{ return ATTACH; }
91 build		{ return BUILD; }
92 compile-with	{ return COMPILE_WITH; }
93 config		{ return CONFIG; }
94 define		{ return DEFINE; }
95 device		{ return DEVICE; }
96 disable		{ return DISABLE; }
97 dumps		{ return DUMPS; }
98 flags		{ return FLAGS; }
99 file		{ return XFILE; }
100 include		{ return INCLUDE; }
101 machine		{ return XMACHINE; }
102 major		{ return MAJOR; }
103 makeoptions	{ return MAKEOPTIONS; }
104 maxusers	{ return MAXUSERS; }
105 maxpartitions	{ return MAXPARTITIONS; }
106 minor		{ return MINOR; }
107 on		{ return ON; }
108 options		{ return OPTIONS; }
109 option		{ return OPTIONS; }
110 "pseudo-device"	{ return PSEUDO_DEVICE; }
111 root		{ return ROOT; }
112 source		{ return SOURCE; }
113 swap		{ return SWAP; }
114 with		{ return WITH; }
115 
116 		/* keywords with values */
117 needs-count	{ yylval.val = FI_NEEDSCOUNT; return FFLAG; }
118 needs-flag	{ yylval.val = FI_NEEDSFLAG; return FFLAG; }
119 
120 		/* all the rest */
121 {PATH}		{ yylval.str = intern(yytext); return PATHNAME; }
122 {WORD}		{ yylval.str = intern(yytext); return WORD; }
123 
124 \"([^"]|\\\")*/\" {
125 		yylval.str = intern(yytext + 1);
126 		(void)input();	/* eat closing quote */
127 		return WORD;
128 	}
129 0[0-7]*	{
130 		yylval.val = strtol(yytext, NULL, 8);
131 		return NUMBER;
132 	}
133 0[xX][0-9a-fA-F]+ {
134 		yylval.val = strtoul(yytext + 2, NULL, 16);
135 		return NUMBER;
136 	}
137 [1-9][0-9]* {
138 		yylval.val = strtol(yytext, NULL, 10);
139 		return NUMBER;
140 	}
141 \n/[ \t] {
142 		yyline++;
143 	}
144 \n	{
145 		yyline++;
146 		return '\n';
147 	}
148 #.*	{ /* ignored (comment) */; }
149 [ \t]*	{ /* ignored (white space) */; }
150 .	{ return yytext[0]; }
151 <<EOF>> {
152 		int tok;
153 
154 		tok = eoftoken;
155 		eoftoken = YY_NULL;
156 		if (incl != NULL)
157 			endinclude();
158 		return (tok);
159 	}
160 
161 %%
162 
163 /*
164  * Open the "main" file (conffile).
165  */
166 int
167 firstfile(fname)
168 	const char *fname;
169 {
170 
171 	if ((yyin = fopen(fname, "r")) == NULL)
172 		return (-1);
173 	yyfile = conffile = fname;
174 	yyline = 1;
175 	eoftoken = YY_NULL;
176 	return (0);
177 }
178 
179 /*
180  * Open the named file for inclusion at the current point.  Returns 0 on
181  * success (file opened and previous state pushed), nonzero on failure
182  * (fopen failed, complaint made).  The `ateof' parameter controls the
183  * token to be returned at the end of the include file (typically '\n'
184  * or ENDFILE).
185  */
186 int
187 include(fname, ateof)
188 	const char *fname;
189 	int ateof;
190 {
191 	register FILE *fp;
192 	register struct incl *in;
193 	char *s;
194 
195 	/* Kludge until files.* files are fixed. */
196 	if (strncmp(fname, "../../../", 9) == 0)
197 		fname += 9;
198 
199 	s = (*fname == '/') ? strdup(fname) : sourcepath(fname);
200 	if ((fp = fopen(s, "r")) == NULL) {
201 		error("cannot open %s for reading: %s\n", s, strerror(errno));
202 		free(s);
203 		return (-1);
204 	}
205 	in = emalloc(sizeof *in);
206 	in->in_prev = incl;
207 	in->in_buf = YY_CURRENT_BUFFER;
208 	in->in_fname = yyfile;
209 	in->in_lineno = yyline;
210 	in->in_preveof = eoftoken;
211 	incl = in;
212 	yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
213 	yyfile = intern(s);
214 	yyline = 1;
215 	eoftoken = ateof;
216 	free(s);
217 	return (0);
218 }
219 
220 /*
221  * Terminate the most recent inclusion.
222  */
223 static void
224 endinclude()
225 {
226 	register struct incl *in;
227 
228 	if ((in = incl) == NULL)
229 		panic("endinclude");
230 	incl = in->in_prev;
231 	lastfile = yyfile;
232 	yy_delete_buffer(YY_CURRENT_BUFFER);
233 	(void)fclose(yyin);
234 	yy_switch_to_buffer(in->in_buf);
235 	yyfile = in->in_fname;
236 	yyline = in->in_lineno;
237 	eoftoken = in->in_preveof;
238 	free(in);
239 }
240 
241 /*
242  * Return the current line number.  If yacc has looked ahead and caused
243  * us to consume a newline, we have to subtract one.  yychar is yacc's
244  * token lookahead, so we can tell.
245  */
246 int
247 currentline()
248 {
249 	extern int yychar;
250 
251 	return (yyline - (yychar == '\n'));
252 }
253