xref: /freebsd/usr.sbin/config/lang.l (revision 2710751bc309af25c6dea1171781678258e83840)
1 %{
2 /*-
3  * Copyright (c) 1980, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	@(#)lang.l	8.1 (Berkeley) 6/6/93
31  * $FreeBSD$
32  */
33 
34 #include <assert.h>
35 #include <ctype.h>
36 #include <err.h>
37 #include <string.h>
38 #include "y.tab.h"
39 #include "config.h"
40 
41 /*
42  * Data for returning to previous files from include files.
43  */
44 struct incl {
45 	struct	incl *in_prev; 	/* previous includes in effect, if any */
46 	YY_BUFFER_STATE in_buf;	/* previous lex state */
47 	const	char *in_fname;	/* previous file name */
48 	int	in_lineno;	/* previous line number */
49 	int	in_ateof;	/* token to insert at EOF */
50 };
51 static struct	incl *inclp;
52 static const	char *lastfile;
53 
54 /*
55  * Key word table
56  */
57 
58 struct kt {
59 	const char *kt_name;
60 	int kt_val;
61 } key_words[] = {
62 	{ "config",	CONFIG },
63 	{ "cpu",	CPU },
64 	{ "nocpu",	NOCPU },
65 	{ "device",	DEVICE },
66 	{ "devices",	DEVICE },
67 	{ "nodevice",	NODEVICE },
68 	{ "nodevices",	NODEVICE },
69 	{ "env",	ENV },
70 	{ "hints",	HINTS },
71 	{ "ident",	IDENT },
72 	{ "machine",	ARCH }, /* MACHINE is defined in /sys/param.h */
73 	{ "makeoption",	MAKEOPTIONS },
74 	{ "makeoptions", MAKEOPTIONS },
75 	{ "nomakeoption", NOMAKEOPTION },
76 	{ "nomakeoptions", NOMAKEOPTION },
77 	{ "maxusers",	MAXUSERS },
78 	{ "profile",	PROFILE },
79 	{ "option",	OPTIONS },
80 	{ "options",	OPTIONS },
81 	{ "nooption",	NOOPTION },
82 	{ "nooptions",	NOOPTION },
83 	{ "include",	INCLUDE },
84 	{ "files", 	FILES },
85 	{ 0, 0 },
86 };
87 
88 
89 static int endinclude(void);
90 int include(const char *, int);
91 int kw_lookup(char *);
92 unsigned int octal(const char *);
93 unsigned int hex(const char *);
94 int yyerror(const char *);
95 
96 %}
97 
98 %option nounput
99 %option noinput
100 
101 ID	[A-Za-z_][-A-Za-z_0-9]*
102 PATH	[./][-/.%^A-Za-z_0-9]+
103 %START TOEOL
104 %%
105 {ID}		{
106 			int i;
107 
108 			BEGIN 0;
109 			if ((i = kw_lookup(yytext)) == -1)
110 			{
111 				yylval.str = strdup(yytext);
112 				return ID;
113 			}
114 			return i;
115 		}
116 \\\"[^"]+\\\"	{
117 			BEGIN 0;
118 			yytext[yyleng-2] = '"';
119 			yytext[yyleng-1] = '\0';
120 			yylval.str = strdup(yytext + 1);
121 			return ID;
122 		}
123 \"[^"]+\"	{
124 			BEGIN 0;
125 			yytext[yyleng-1] = '\0';
126 			yylval.str = strdup(yytext + 1);
127 			return ID;
128 		}
129 <TOEOL>[^# \t\n]*	{
130 			BEGIN 0;
131 			yylval.str = strdup(yytext);
132 			return ID;
133 		}
134 0[0-7]*		{
135 			yylval.val = octal(yytext);
136 			return NUMBER;
137 		}
138 0x[0-9a-fA-F]+	{
139 			yylval.val = hex(yytext);
140 			return NUMBER;
141 		}
142 -?[1-9][0-9]*	{
143 			yylval.val = atoi(yytext);
144 			return NUMBER;
145 		}
146 "?"		{
147 			yylval.val = -1;
148 			return NUMBER;
149 		}
150 \n/[ \t]	{
151 			yyline++;
152 		}
153 \n		{
154 			yyline++;
155 			return SEMICOLON;
156 		}
157 #.*		{	/* Ignored (comment) */;	}
158 [ \t\f]*	{	/* Ignored (white space) */;	}
159 ";"		{	return SEMICOLON;		}
160 ","		{	return COMMA;			}
161 "="		{	BEGIN TOEOL; return EQUALS;	}
162 "+="		{	BEGIN TOEOL; return PLUSEQUALS;	}
163 <<EOF>>		{
164 			int tok;
165 
166 			if (inclp == NULL)
167 				return YY_NULL;
168 			tok = endinclude();
169 			if (tok != 0)
170 				return tok;
171 			/* otherwise continue scanning */
172 		}
173 {PATH}		{
174 			BEGIN 0;
175 			yylval.str = strdup(yytext);
176 			return PATH;
177 		}
178 .		{	return yytext[0];		}
179 
180 %%
181 /*
182  * kw_lookup
183  *	Look up a string in the keyword table.  Returns a -1 if the
184  *	string is not a keyword otherwise it returns the keyword number
185  */
186 
187 int
188 kw_lookup(char *word)
189 {
190 	struct kt *kp;
191 
192 	for (kp = key_words; kp->kt_name != 0; kp++)
193 		if (eq(word, kp->kt_name))
194 			return kp->kt_val;
195 	return -1;
196 }
197 
198 /*
199  * Number conversion routines
200  */
201 
202 unsigned int
203 octal(const char *str)
204 {
205 	unsigned int num;
206 
207 	(void) sscanf(str, "%o", &num);
208 	return num;
209 }
210 
211 unsigned int
212 hex(const char *str)
213 {
214 	unsigned int num;
215 
216 	(void) sscanf(str+2, "%x", &num);
217 	return num;
218 }
219 
220 void
221 cfgfile_add(const char *fname)
222 {
223 	struct cfgfile *cf;
224 
225 	cf = calloc(1, sizeof(*cf));
226 	if (cf == NULL)
227 		err(EXIT_FAILURE, "calloc");
228 	assert(cf != NULL);
229 	asprintf(&cf->cfg_path, "%s", fname);
230 	STAILQ_INSERT_TAIL(&cfgfiles, cf, cfg_next);
231 }
232 
233 void
234 cfgfile_removeall(void)
235 {
236 	struct cfgfile *cf;
237 
238 	while (!STAILQ_EMPTY(&cfgfiles)) {
239 		cf = STAILQ_FIRST(&cfgfiles);
240 		STAILQ_REMOVE_HEAD(&cfgfiles, cfg_next);
241 		if (cf->cfg_path != NULL)
242 			free(cf->cfg_path);
243 		free(cf);
244 	}
245 }
246 
247 /*
248  * Open the named file for inclusion at the current point.  Returns 0 on
249  * success (file opened and previous state pushed), nonzero on failure
250  * (fopen failed, complaint made).  The `ateof' parameter controls the
251  * token to be inserted at the end of the include file. If ateof == 0,
252  * then nothing is inserted.
253  */
254 int
255 include(const char *fname, int ateof)
256 {
257 	FILE *fp;
258 	struct incl *in;
259 	char *fnamebuf;
260 
261 	fnamebuf = NULL;
262 	fp = fopen(fname, "r");
263 	if (fp == NULL && fname[0] != '.' && fname[0] != '/') {
264 		asprintf(&fnamebuf, "../../conf/%s", fname);
265 		if (fnamebuf != NULL) {
266 			fp = fopen(fnamebuf, "r");
267 			free(fnamebuf);
268 		}
269 	}
270 	if (fp == NULL) {
271 		yyerror("cannot open included file");
272 		return (-1);
273 	}
274 	cfgfile_add(fnamebuf == NULL ? fname : fnamebuf);
275 	in = malloc(sizeof(*in));
276 	assert(in != NULL);
277 	in->in_prev = inclp;
278 	in->in_buf = YY_CURRENT_BUFFER;
279 	in->in_fname = yyfile;
280 	in->in_lineno = yyline;
281 	in->in_ateof = ateof;
282 	inclp = in;
283 	yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
284 	yyfile = fname;
285 	yyline = 0;
286 	return (0);
287 }
288 
289 /*
290  * Terminate the most recent inclusion.
291  */
292 static int
293 endinclude(void)
294 {
295 	struct incl *in;
296 	int ateof;
297 
298 	in = inclp;
299 	assert(in != NULL);
300 	inclp = in->in_prev;
301 	lastfile = yyfile;
302 	yy_delete_buffer(YY_CURRENT_BUFFER);
303 	(void)fclose(yyin);
304 	yy_switch_to_buffer(in->in_buf);
305 	yyfile = in->in_fname;
306 	yyline = in->in_lineno;
307 	ateof  = in->in_ateof;
308 	free(in);
309 
310 	return (ateof);
311 }
312