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