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