xref: /openbsd/usr.sbin/config/scan.l (revision 09467b48)
1 %{
2 /*	$OpenBSD: scan.l,v 1.24 2017/07/09 14:04:50 espie 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. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	from: @(#)scan.l	8.1 (Berkeley) 6/6/93
43  */
44 
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include "config.h"
51 #include "gram.h"
52 
53 int	yyline;
54 const char *yyfile;
55 const char *lastfile;
56 
57 /*
58  * Data for returning to previous files from include files.
59  */
60 struct incl {
61 	struct	incl *in_prev;	/* previous includes in effect, if any */
62 	YY_BUFFER_STATE in_buf;	/* previous lex state */
63 	const char *in_fname;	/* previous file name */
64 	int	in_lineno;	/* previous line number */
65 	int	in_ateof;	/* token to insert at EOF */
66 };
67 static struct incl *incl;
68 static int endinclude(void);
69 
70 %}
71 
72 %option noyywrap
73 
74 PATH	[A-Za-z_0-9]*[./][-A-Za-z_0-9./\$\{\}]*
75 WORD	[A-Za-z_][-A-Za-z_0-9]*
76 
77 %%
78 		/* Local variables for yylex() */
79 		int tok;
80 
81 and		return AND;
82 at		return AT;
83 attach		return ATTACH;
84 build		return BUILD;
85 compile-with	return COMPILE_WITH;
86 config		return CONFIG;
87 define		return DEFINE;
88 defopt		return DEFOPT;
89 device		return DEVICE;
90 disable		return DISABLE;
91 enable		return ENABLE;
92 dumps		return DUMPS;
93 file		return XFILE;
94 flags		return FLAGS;
95 include		return INCLUDE;
96 machine		return XMACHINE;
97 major		return MAJOR;
98 makeoptions	return MAKEOPTIONS;
99 makeoption	return MAKEOPTIONS;
100 maxpartitions	return MAXPARTITIONS;
101 maxusers	return MAXUSERS;
102 minor		return MINOR;
103 needs-count	return NEEDS_COUNT;
104 needs-flag	return NEEDS_FLAG;
105 object		return XOBJECT;
106 on		return ON;
107 options		return OPTIONS;
108 option		return OPTIONS;
109 pseudo-device	return PSEUDO_DEVICE;
110 root		return ROOT;
111 source		return SOURCE;
112 swap		return SWAP;
113 with		return WITH;
114 rmoption	return RMOPTIONS;
115 rmoptions	return RMOPTIONS;
116 
117 {PATH}		{
118 		yylval.str = intern(yytext);
119 		return PATHNAME;
120 	}
121 {WORD}		{
122 		yylval.str = intern(yytext);
123 		return WORD;
124 	}
125 
126 \"\"		{
127 		yylval.str = intern("");
128 		return EMPTY;
129 	}
130 \"([^"\n]|\\\")+ {
131 		tok = input();  /* eat closing quote */
132 		yylval.str = intern(yytext + 1);
133 		if (tok != '"') {
134 			error("closing quote missing");
135 			unput(tok);
136 		}
137 		return WORD;
138 	}
139 0[0-7]*	{
140 		yylval.val = strtol(yytext, NULL, 8);
141 		return NUMBER;
142 	}
143 0[xX][0-9a-fA-F]+ {
144 		yylval.val = strtoul(yytext + 2, NULL, 16);
145 		return NUMBER;
146 	}
147 [1-9][0-9]* {
148 		yylval.val = strtol(yytext, NULL, 10);
149 		return NUMBER;
150 	}
151 \n[ \t] {
152 		/*
153 		 * Note: newline followed by whitespace is always a
154 		 * continuation of the previous line, so do NOT
155 		 * return a token in this case.
156 		 */
157 		yyline++;
158 	}
159 \n	{
160 		yyline++;
161 		return '\n';
162 	}
163 #.*	{ /* ignored (comment) */; }
164 [ \t]+	{ /* ignored (white space) */; }
165 .	{ return yytext[0]; }
166 <<EOF>> {
167 		if (incl == NULL)
168 			return YY_NULL;
169 		tok = endinclude();
170 		if (tok)
171 			return tok;
172 		/* otherwise continue scanning */
173 	}
174 
175 %%
176 
177 /*
178  * Open the "main" file (conffile).
179  */
180 int
181 firstfile(fname)
182 	const char *fname;
183 {
184 
185 	if ((yyin = fopen(fname, "r")) == NULL)
186 		return (-1);
187 	yyfile = conffile = fname;
188 	yyline = 1;
189 	return (0);
190 }
191 
192 /*
193  * Open the named file for inclusion at the current point.  Returns 0 on
194  * success (file opened and previous state pushed), nonzero on failure
195  * (fopen failed, complaint made).  The `ateof' parameter controls the
196  * token to be inserted at the end of the include file (i.e. ENDFILE).
197  * If ateof == 0 then nothing is inserted.
198  */
199 int
200 include(fname, ateof)
201 	const char *fname;
202 	int ateof;
203 {
204 	FILE *fp;
205 	struct incl *in;
206 	char *s;
207 	static int havedirs;
208 
209 	if (havedirs == 0) {
210 		havedirs = 1;
211 		setupdirs();
212 	}
213 
214 	/* Kludge until files.* files are fixed. */
215 	if (strncmp(fname, "../../../", 9) == 0)
216 		fname += 9;
217 
218 	s = (*fname == '/') ? strdup(fname) : sourcepath(fname);
219 	if ((fp = fopen(s, "r")) == NULL) {
220 		error("cannot open %s for reading: %s\n", s, strerror(errno));
221 		free(s);
222 		return (-1);
223 	}
224 	in = emalloc(sizeof *in);
225 	in->in_prev = incl;
226 	in->in_buf = YY_CURRENT_BUFFER;
227 	in->in_fname = yyfile;
228 	in->in_lineno = yyline;
229 	in->in_ateof = ateof;
230 	incl = in;
231 	yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
232 	yyfile = intern(s);
233 	yyline = 1;
234 	free(s);
235 	return (0);
236 }
237 
238 /*
239  * Terminate the most recent inclusion.
240  */
241 static int
242 endinclude()
243 {
244 	struct incl *in;
245 	int ateof;
246 
247 	if ((in = incl) == NULL)
248 		panic("endinclude");
249 	incl = in->in_prev;
250 	lastfile = yyfile;
251 	yy_delete_buffer(YY_CURRENT_BUFFER);
252 	(void)fclose(yyin);
253 	yy_switch_to_buffer(in->in_buf);
254 	yyfile = in->in_fname;
255 	yyline = in->in_lineno;
256 	ateof  = in->in_ateof;
257 	free(in);
258 
259 	return (ateof);
260 }
261 
262 /*
263  * Return the current line number.  If yacc has looked ahead and caused
264  * us to consume a newline, we have to subtract one.  yychar is yacc's
265  * token lookahead, so we can tell.
266  */
267 int
268 currentline()
269 {
270 	extern int yychar;
271 
272 	return (yyline - (yychar == '\n'));
273 }
274