xref: /openbsd/usr.sbin/config/scan.l (revision 78b63d65)
1 %{
2 /*	$OpenBSD: scan.l,v 1.14 2001/01/23 06:02:59 angelos Exp $	*/
3 /*	$NetBSD: scan.l,v 1.13 1997/02/02 21:12:37 thorpej 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 /*
63  * Data for returning to previous files from include files.
64  */
65 struct incl {
66 	struct	incl *in_prev;	/* previous includes in effect, if any */
67 	YY_BUFFER_STATE in_buf;	/* previous lex state */
68 	const char *in_fname;	/* previous file name */
69 	int	in_lineno;	/* previous line number */
70 	int	in_ateof;	/* token to insert at EOF */
71 };
72 static struct incl *incl;
73 static int endinclude __P((void));
74 
75 #define	yywrap() 1
76 
77 %}
78 
79 PATH	[A-Za-z_0-9]*[./][-A-Za-z_0-9./]*
80 WORD	[A-Za-z_][-A-Za-z_0-9]*
81 
82 %%
83 		/* Local variables for yylex() */
84 		int tok;
85 
86 and		return AND;
87 at		return AT;
88 attach		return ATTACH;
89 build		return BUILD;
90 compile-with	return COMPILE_WITH;
91 config		return CONFIG;
92 define		return DEFINE;
93 defopt		return DEFOPT;
94 device		return DEVICE;
95 disable		return DISABLE;
96 enable		return ENABLE;
97 dumps		return DUMPS;
98 file		return XFILE;
99 flags		return FLAGS;
100 include		return INCLUDE;
101 machine		return XMACHINE;
102 major		return MAJOR;
103 makeoptions	return MAKEOPTIONS;
104 makeoption	return MAKEOPTIONS;
105 maxpartitions	return MAXPARTITIONS;
106 maxusers	return MAXUSERS;
107 minor		return MINOR;
108 needs-count	return NEEDS_COUNT;
109 needs-flag	return NEEDS_FLAG;
110 object		return XOBJECT;
111 on		return ON;
112 options		return OPTIONS;
113 option		return OPTIONS;
114 pseudo-device	return PSEUDO_DEVICE;
115 root		return ROOT;
116 source		return SOURCE;
117 swap		return SWAP;
118 with		return WITH;
119 rmoption        return RMOPTIONS;
120 rmoptions       return RMOPTIONS;
121 
122 {PATH}		{
123 		yylval.str = intern(yytext);
124 		return PATHNAME;
125 	}
126 {WORD}		{
127 		yylval.str = intern(yytext);
128 		return WORD;
129 	}
130 
131 \"\"		{
132 		yylval.str = intern("");
133 		return EMPTY;
134 	}
135 \"([^"\n]|\\\")+ {
136 		tok = input();  /* eat closing quote */
137 		if (tok != '"') {
138 			error("closing quote missing\n");
139 			unput(tok);
140 		}
141 		yylval.str = intern(yytext + 1);
142 		return WORD;
143 	}
144 0[0-7]*	{
145 		yylval.val = strtol(yytext, NULL, 8);
146 		return NUMBER;
147 	}
148 0[xX][0-9a-fA-F]+ {
149 		yylval.val = strtoul(yytext + 2, NULL, 16);
150 		return NUMBER;
151 	}
152 [1-9][0-9]* {
153 		yylval.val = strtol(yytext, NULL, 10);
154 		return NUMBER;
155 	}
156 \n[ \t] {
157 		/*
158 		 * Note: newline followed by whitespace is always a
159 		 * continuation of the previous line, so do NOT
160 		 * return a token in this case.
161 		 */
162 		yyline++;
163 	}
164 \n	{
165 		yyline++;
166 		return '\n';
167 	}
168 #.*	{ /* ignored (comment) */; }
169 [ \t]+	{ /* ignored (white space) */; }
170 .	{ return yytext[0]; }
171 <<EOF>> {
172 		if (incl == NULL)
173 			return YY_NULL;
174 		tok = endinclude();
175 		if (tok)
176 			return tok;
177 		/* otherwise continue scanning */
178 	}
179 
180 %%
181 
182 /*
183  * Open the "main" file (conffile).
184  */
185 int
186 firstfile(fname)
187 	const char *fname;
188 {
189 
190 	if ((yyin = fopen(fname, "r")) == NULL)
191 		return (-1);
192 	yyfile = conffile = fname;
193 	yyline = 1;
194 	return (0);
195 }
196 
197 /*
198  * Open the named file for inclusion at the current point.  Returns 0 on
199  * success (file opened and previous state pushed), nonzero on failure
200  * (fopen failed, complaint made).  The `ateof' parameter controls the
201  * token to be inserted at the end of the include file (i.e. ENDFILE).
202  * If ateof == 0 then nothing is inserted.
203  */
204 int
205 include(fname, ateof)
206 	const char *fname;
207 	int ateof;
208 {
209 	FILE *fp;
210 	struct incl *in;
211 	char *s;
212 	static int havedirs;
213 
214 	if (havedirs == 0) {
215 		havedirs = 1;
216 		setupdirs();
217 	}
218 
219 	/* Kludge until files.* files are fixed. */
220 	if (strncmp(fname, "../../../", 9) == 0)
221 		fname += 9;
222 
223 	s = (*fname == '/') ? strdup(fname) : sourcepath(fname);
224 	if ((fp = fopen(s, "r")) == NULL) {
225 		error("cannot open %s for reading: %s\n", s, strerror(errno));
226 		free(s);
227 		return (-1);
228 	}
229 	in = emalloc(sizeof *in);
230 	in->in_prev = incl;
231 	in->in_buf = YY_CURRENT_BUFFER;
232 	in->in_fname = yyfile;
233 	in->in_lineno = yyline;
234 	in->in_ateof = ateof;
235 	incl = in;
236 	yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
237 	yyfile = intern(s);
238 	yyline = 1;
239 	free(s);
240 	return (0);
241 }
242 
243 /*
244  * Terminate the most recent inclusion.
245  */
246 static int
247 endinclude()
248 {
249 	struct incl *in;
250 	int ateof;
251 
252 	if ((in = incl) == NULL)
253 		panic("endinclude");
254 	incl = in->in_prev;
255 	lastfile = yyfile;
256 	yy_delete_buffer(YY_CURRENT_BUFFER);
257 	(void)fclose(yyin);
258 	yy_switch_to_buffer(in->in_buf);
259 	yyfile = in->in_fname;
260 	yyline = in->in_lineno;
261 	ateof  = in->in_ateof;
262 	free(in);
263 
264 	return (ateof);
265 }
266 
267 /*
268  * Return the current line number.  If yacc has looked ahead and caused
269  * us to consume a newline, we have to subtract one.  yychar is yacc's
270  * token lookahead, so we can tell.
271  */
272 int
273 currentline()
274 {
275 	extern int yychar;
276 
277 	return (yyline - (yychar == '\n'));
278 }
279