xref: /freebsd/usr.sbin/config/lang.l (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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 
33 #include <assert.h>
34 #include <ctype.h>
35 #include <err.h>
36 #include <stdlib.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 	{ "cpu",	CPU },
63 	{ "nocpu",	NOCPU },
64 	{ "device",	DEVICE },
65 	{ "devices",	DEVICE },
66 	{ "nodevice",	NODEVICE },
67 	{ "nodevices",	NODEVICE },
68 	{ "env",	ENV },
69 	{ "envvar",	ENVVAR },
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 	{ "option",	OPTIONS },
79 	{ "options",	OPTIONS },
80 	{ "nooption",	NOOPTION },
81 	{ "nooptions",	NOOPTION },
82 	{ "include",	INCLUDE },
83 	{ "files", 	FILES },
84 	{ 0, 0 },
85 };
86 
87 
88 static int endinclude(void);
89 int include(const char *, int);
90 int kw_lookup(char *);
91 unsigned int octal(const char *);
92 unsigned int hex(const char *);
93 int yyerror(const char *);
94 
95 #define YY_DECL int yylex(void)
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 %START ENVC
105 %%
106 <ENVC>[^#\n]*	{
107 			BEGIN 0;
108 			yylval.str = strdup(yytext);
109 			return ENVLINE;
110 		}
111 {ID}		{
112 			int i;
113 
114 			BEGIN 0;
115 			if ((i = kw_lookup(yytext)) == -1)
116 			{
117 				yylval.str = strdup(yytext);
118 				return ID;
119 			}
120 			/* We'll glom onto the rest of an envvar line */
121 			if (i == ENVVAR)
122 				BEGIN ENVC;
123 			return i;
124 		}
125 \\\"[^"]*\\\"	{
126 			BEGIN 0;
127 			yytext[yyleng-2] = '"';
128 			yytext[yyleng-1] = '\0';
129 			yylval.str = strdup(yytext + 1);
130 			return ID;
131 		}
132 \"[^"]+\"	{
133 			BEGIN 0;
134 			yytext[yyleng-1] = '\0';
135 			yylval.str = strdup(yytext + 1);
136 			return ID;
137 		}
138 <TOEOL>[^# \t\n]*	{
139 			BEGIN 0;
140 			yylval.str = strdup(yytext);
141 			return ID;
142 		}
143 0[0-7]*		{
144 			yylval.val = octal(yytext);
145 			return NUMBER;
146 		}
147 0x[0-9a-fA-F]+	{
148 			yylval.val = hex(yytext);
149 			return NUMBER;
150 		}
151 -?[1-9][0-9]*	{
152 			yylval.val = atoi(yytext);
153 			return NUMBER;
154 		}
155 "?"		{
156 			yylval.val = -1;
157 			return NUMBER;
158 		}
159 \n/[ \t]	{
160 			yyline++;
161 		}
162 \n		{
163 			yyline++;
164 			return SEMICOLON;
165 		}
166 #.*		{	/* Ignored (comment) */;	}
167 [ \t\f]*	{	/* Ignored (white space) */;	}
168 ";"		{	return SEMICOLON;		}
169 ","		{	return COMMA;			}
170 "="		{	BEGIN TOEOL; return EQUALS;	}
171 "+="		{	BEGIN TOEOL; return PLUSEQUALS;	}
172 <<EOF>>		{
173 			int tok;
174 
175 			if (inclp == NULL)
176 				return YY_NULL;
177 			tok = endinclude();
178 			if (tok != 0)
179 				return tok;
180 			/* otherwise continue scanning */
181 		}
182 {PATH}		{
183 			BEGIN 0;
184 			yylval.str = strdup(yytext);
185 			return PATH;
186 		}
187 .		{	return yytext[0];		}
188 
189 %%
190 /*
191  * kw_lookup
192  *	Look up a string in the keyword table.  Returns a -1 if the
193  *	string is not a keyword otherwise it returns the keyword number
194  */
195 
196 int
197 kw_lookup(char *word)
198 {
199 	struct kt *kp;
200 
201 	for (kp = key_words; kp->kt_name != 0; kp++)
202 		if (eq(word, kp->kt_name))
203 			return kp->kt_val;
204 	return -1;
205 }
206 
207 /*
208  * Number conversion routines
209  */
210 
211 unsigned int
212 octal(const char *str)
213 {
214 	unsigned int num;
215 
216 	(void) sscanf(str, "%o", &num);
217 	return num;
218 }
219 
220 unsigned int
221 hex(const char *str)
222 {
223 	unsigned int num;
224 
225 	(void) sscanf(str+2, "%x", &num);
226 	return num;
227 }
228 
229 void
230 cfgfile_add(const char *fname)
231 {
232 	struct cfgfile *cf;
233 
234 	cf = calloc(1, sizeof(*cf));
235 	if (cf == NULL)
236 		err(EXIT_FAILURE, "calloc");
237 	assert(cf != NULL);
238 	asprintf(&cf->cfg_path, "%s", fname);
239 	STAILQ_INSERT_TAIL(&cfgfiles, cf, cfg_next);
240 }
241 
242 void
243 cfgfile_removeall(void)
244 {
245 	struct cfgfile *cf;
246 
247 	while (!STAILQ_EMPTY(&cfgfiles)) {
248 		cf = STAILQ_FIRST(&cfgfiles);
249 		STAILQ_REMOVE_HEAD(&cfgfiles, cfg_next);
250 		if (cf->cfg_path != NULL)
251 			free(cf->cfg_path);
252 		free(cf);
253 	}
254 }
255 
256 /*
257  * Open the named file for inclusion at the current point.  Returns 0 on
258  * success (file opened and previous state pushed), nonzero on failure
259  * (fopen failed, complaint made).  The `ateof' parameter controls the
260  * token to be inserted at the end of the include file. If ateof == 0,
261  * then nothing is inserted.
262  */
263 int
264 include(const char *fname, int ateof)
265 {
266 	FILE *fp;
267 	struct incl *in;
268 	struct includepath* ipath;
269 	char *fnamebuf;
270 
271 	fnamebuf = NULL;
272 	fp = fopen(fname, "r");
273 	if (fp == NULL && fname[0] != '.' && fname[0] != '/') {
274 		asprintf(&fnamebuf, "../../conf/%s", fname);
275 		if (fnamebuf != NULL) {
276 			fp = fopen(fnamebuf, "r");
277 			if (fp == NULL)
278 				free(fnamebuf);
279 		}
280 	}
281 	if (fp == NULL) {
282 		SLIST_FOREACH(ipath, &includepath, path_next) {
283 			asprintf(&fnamebuf, "%s/%s", ipath->path, fname);
284 			if (fnamebuf != NULL) {
285 				fp = fopen(fnamebuf, "r");
286 			}
287 			if (fp != NULL)
288 				break;
289 			free(fnamebuf);
290 		}
291 	}
292 	if (fp == NULL) {
293 		yyerror("cannot open included file");
294 		return (-1);
295 	}
296 	cfgfile_add(fnamebuf == NULL ? fname : fnamebuf);
297 	free(fnamebuf);
298 
299 	in = malloc(sizeof(*in));
300 	assert(in != NULL);
301 	in->in_prev = inclp;
302 	in->in_buf = YY_CURRENT_BUFFER;
303 	in->in_fname = yyfile;
304 	in->in_lineno = yyline;
305 	in->in_ateof = ateof;
306 	inclp = in;
307 	yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
308 	yyfile = fname;
309 	yyline = 0;
310 	return (0);
311 }
312 
313 /*
314  * Terminate the most recent inclusion.
315  */
316 static int
317 endinclude(void)
318 {
319 	struct incl *in;
320 	int ateof;
321 
322 	in = inclp;
323 	assert(in != NULL);
324 	inclp = in->in_prev;
325 	lastfile = yyfile;
326 	yy_delete_buffer(YY_CURRENT_BUFFER);
327 	(void)fclose(yyin);
328 	yy_switch_to_buffer(in->in_buf);
329 	yyfile = in->in_fname;
330 	yyline = in->in_lineno;
331 	ateof  = in->in_ateof;
332 	free(in);
333 
334 	return (ateof);
335 }
336