xref: /freebsd/usr.sbin/config/config.y (revision 9768746b)
1 %union {
2 	char	*str;
3 	int	val;
4 	struct	file_list *file;
5 }
6 
7 %token	ARCH
8 %token	COMMA
9 %token	CONFIG
10 %token	CPU
11 %token	NOCPU
12 %token	DEVICE
13 %token	NODEVICE
14 %token	ENV
15 %token	ENVVAR
16 %token	EQUALS
17 %token	PLUSEQUALS
18 %token	HINTS
19 %token	IDENT
20 %token	MAXUSERS
21 %token	OPTIONS
22 %token	NOOPTION
23 %token	MAKEOPTIONS
24 %token	NOMAKEOPTION
25 %token	SEMICOLON
26 %token	INCLUDE
27 %token	FILES
28 
29 %token	<str>	ENVLINE
30 %token	<str>	ID
31 %token	<val>	NUMBER
32 
33 %type	<str>	Save_id
34 %type	<str>	Opt_value
35 %type	<str>	Dev
36 %token	<str>	PATH
37 
38 %{
39 
40 /*-
41  * SPDX-License-Identifier: BSD-3-Clause
42  *
43  * Copyright (c) 1988, 1993
44  *	The Regents of the University of California.  All rights reserved.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  *
70  *	@(#)config.y	8.1 (Berkeley) 6/6/93
71  * $FreeBSD$
72  */
73 
74 #include <assert.h>
75 #include <ctype.h>
76 #include <err.h>
77 #include <stdio.h>
78 #include <string.h>
79 
80 #include "config.h"
81 
82 struct	device_head dtab;
83 char	*ident;
84 char	*env;
85 int	yyline;
86 const	char *yyfile;
87 struct  file_list_head ftab;
88 struct  files_name_head fntab;
89 char	errbuf[80];
90 int	maxusers;
91 
92 #define ns(s)	strdup(s)
93 int include(const char *, int);
94 void yyerror(const char *s);
95 int yywrap(void);
96 
97 static void newdev(char *name);
98 static void newfile(char *name);
99 static void newenvvar(char *name, bool is_file);
100 static void rmdev_schedule(struct device_head *dh, char *name);
101 static void newopt(struct opt_head *list, char *name, char *value, int append, int dupe);
102 static void rmopt_schedule(struct opt_head *list, char *name);
103 
104 static char *
105 devopt(char *dev)
106 {
107 	char *ret = malloc(strlen(dev) + 5);
108 
109 	sprintf(ret, "DEV_%s", dev);
110 	raisestr(ret);
111 	return ret;
112 }
113 
114 %}
115 %%
116 Configuration:
117 	Many_specs
118 		;
119 
120 Many_specs:
121 	Many_specs Spec
122 		|
123 	/* lambda */
124 		;
125 
126 Spec:
127 	Device_spec SEMICOLON
128 		|
129 	Config_spec SEMICOLON
130 		|
131 	INCLUDE PATH SEMICOLON {
132 		if (incignore == 0)
133 			include($2, 0);
134 		};
135 		|
136 	INCLUDE ID SEMICOLON {
137 	          if (incignore == 0)
138 		  	include($2, 0);
139 		};
140 		|
141 	FILES ID SEMICOLON { newfile($2); };
142 	        |
143 	SEMICOLON
144 		|
145 	error SEMICOLON
146 		;
147 
148 Config_spec:
149 	ARCH Save_id {
150 		if (machinename != NULL && !eq($2, machinename))
151 		    errx(1, "%s:%d: only one machine directive is allowed",
152 			yyfile, yyline);
153 		machinename = $2;
154 		machinearch = $2;
155 	      } |
156 	ARCH Save_id Save_id {
157 		/*
158 		 * Allow the machinearch to change with a second machine directive,
159 		 * but still enforce no changes to the machinename.
160 		 */
161 		if (machinename != NULL && !eq($2, machinename))
162 		    errx(1, "%s:%d: only one machine directive is allowed",
163 			yyfile, yyline);
164 		machinename = $2;
165 		machinearch = $3;
166 	      } |
167 	CPU Save_id {
168 		struct cputype *cp =
169 		    (struct cputype *)calloc(1, sizeof (struct cputype));
170 		if (cp == NULL)
171 			err(EXIT_FAILURE, "calloc");
172 		cp->cpu_name = $2;
173 		SLIST_INSERT_HEAD(&cputype, cp, cpu_next);
174 	      } |
175 	NOCPU Save_id {
176 		struct cputype *cp, *cp2;
177 		SLIST_FOREACH_SAFE(cp, &cputype, cpu_next, cp2) {
178 			if (eq(cp->cpu_name, $2)) {
179 				SLIST_REMOVE(&cputype, cp, cputype, cpu_next);
180 				free(cp);
181 			}
182 		}
183 	      } |
184 	OPTIONS Opt_list
185 		|
186 	NOOPTION NoOpt_list |
187 	MAKEOPTIONS Mkopt_list
188 		|
189 	NOMAKEOPTION Save_id { rmopt_schedule(&mkopt, $2); } |
190 	IDENT ID { ident = $2; } |
191 	System_spec
192 		|
193 	MAXUSERS NUMBER { maxusers = $2; } |
194 	ENV ID { newenvvar($2, true); } |
195 	ENVVAR ENVLINE { newenvvar($2, false); } |
196 	HINTS ID {
197 		struct hint *hint;
198 
199 		hint = (struct hint *)calloc(1, sizeof (struct hint));
200 		if (hint == NULL)
201 			err(EXIT_FAILURE, "calloc");
202 		hint->hint_name = $2;
203 		STAILQ_INSERT_HEAD(&hints, hint, hint_next);
204 	        }
205 
206 System_id:
207 	Save_id { newopt(&mkopt, ns("KERNEL"), $1, 0, 0); };
208 
209 System_parameter_list:
210 	  System_parameter_list ID
211 	| ID
212 	;
213 
214 Opt_list:
215 	Opt_list COMMA Option
216 		|
217 	Option
218 		;
219 
220 NoOpt_list:
221 	NoOpt_list COMMA NoOption
222 	  	|
223 	NoOption
224 		;
225 Option:
226 	Save_id {
227 		newopt(&opt, $1, NULL, 0, 1);
228 		if (strchr($1, '=') != NULL)
229 			errx(1, "%s:%d: The `=' in options should not be "
230 			    "quoted", yyfile, yyline);
231 	      } |
232 	Save_id EQUALS Opt_value {
233 		newopt(&opt, $1, $3, 0, 1);
234 	      } ;
235 
236 NoOption:
237 	Save_id {
238 		rmopt_schedule(&opt, $1);
239 		};
240 
241 Opt_value:
242 	ID { $$ = $1; } |
243 	NUMBER {
244 			char buf[80];
245 
246 			(void) snprintf(buf, sizeof(buf), "%d", $1);
247 			$$ = ns(buf);
248 		} ;
249 
250 Save_id:
251 	ID { $$ = $1; }
252 	;
253 
254 Mkopt_list:
255 	Mkopt_list COMMA Mkoption
256 		|
257 	Mkoption
258 		;
259 
260 Mkoption:
261 	Save_id { newopt(&mkopt, $1, ns(""), 0, 0); } |
262 	Save_id EQUALS { newopt(&mkopt, $1, ns(""), 0, 0); } |
263 	Save_id EQUALS Opt_value { newopt(&mkopt, $1, $3, 0, 0); } |
264 	Save_id PLUSEQUALS Opt_value { newopt(&mkopt, $1, $3, 1, 0); } ;
265 
266 Dev:
267 	ID { $$ = $1; }
268 	;
269 
270 Device_spec:
271 	DEVICE Dev_list
272 		|
273 	NODEVICE NoDev_list
274 		;
275 
276 Dev_list:
277 	Dev_list COMMA Device
278 		|
279 	Device
280 		;
281 
282 NoDev_list:
283 	NoDev_list COMMA NoDevice
284 		|
285 	NoDevice
286 		;
287 
288 Device:
289 	Dev {
290 		newopt(&opt, devopt($1), ns("1"), 0, 0);
291 		/* and the device part */
292 		newdev($1);
293 		}
294 
295 NoDevice:
296 	Dev {
297 		char *s = devopt($1);
298 
299 		rmopt_schedule(&opt, s);
300 		free(s);
301 		/* and the device part */
302 		rmdev_schedule(&dtab, $1);
303 		} ;
304 
305 %%
306 
307 void
308 yyerror(const char *s)
309 {
310 
311 	errx(1, "%s:%d: %s", yyfile, yyline + 1, s);
312 }
313 
314 int
315 yywrap(void)
316 {
317 	if (found_defaults) {
318 		if (freopen(PREFIX, "r", stdin) == NULL)
319 			err(2, "%s", PREFIX);
320 		yyfile = PREFIX;
321 		yyline = 0;
322 		found_defaults = 0;
323 		return 0;
324 	}
325 	return 1;
326 }
327 
328 /*
329  * Add a new file to the list of files.
330  */
331 static void
332 newfile(char *name)
333 {
334 	struct files_name *nl;
335 
336 	nl = (struct files_name *) calloc(1, sizeof *nl);
337 	if (nl == NULL)
338 		err(EXIT_FAILURE, "calloc");
339 	nl->f_name = name;
340 	STAILQ_INSERT_TAIL(&fntab, nl, f_next);
341 }
342 
343 static void
344 newenvvar(char *name, bool is_file)
345 {
346 	struct envvar *envvar;
347 
348 	envvar = (struct envvar *)calloc(1, sizeof (struct envvar));
349 	if (envvar == NULL)
350 		err(EXIT_FAILURE, "calloc");
351 	envvar->env_str = name;
352 	envvar->env_is_file = is_file;
353 	STAILQ_INSERT_HEAD(&envvars, envvar, envvar_next);
354 }
355 
356 /*
357  * Find a device in the list of devices.
358  */
359 static struct device *
360 finddev(struct device_head *dlist, char *name)
361 {
362 	struct device *dp;
363 
364 	STAILQ_FOREACH(dp, dlist, d_next)
365 		if (eq(dp->d_name, name))
366 			return (dp);
367 
368 	return (NULL);
369 }
370 
371 /*
372  * Add a device to the list of devices.
373  */
374 static void
375 newdev(char *name)
376 {
377 	struct device *np, *dp;
378 
379 	if ((dp = finddev(&dtab, name)) != NULL) {
380 		if (strcmp(dp->yyfile, yyfile) == 0)
381 			fprintf(stderr,
382 			    "WARNING: duplicate device `%s' encountered in %s\n",
383 			    name, yyfile);
384 		return;
385 	}
386 
387 	np = (struct device *) calloc(1, sizeof *np);
388 	if (np == NULL)
389 		err(EXIT_FAILURE, "calloc");
390 	np->d_name = name;
391 	np->yyfile = strdup(yyfile);
392 	STAILQ_INSERT_TAIL(&dtab, np, d_next);
393 }
394 
395 /*
396  * Schedule a device to removal.
397  */
398 static void
399 rmdev_schedule(struct device_head *dh, char *name)
400 {
401 	struct device *dp;
402 
403 	dp = finddev(dh, name);
404 	if (dp != NULL) {
405 		STAILQ_REMOVE(dh, dp, device, d_next);
406 		free(dp->yyfile);
407 		free(dp->d_name);
408 		free(dp);
409 	}
410 }
411 
412 /*
413  * Find an option in the list of options.
414  */
415 static struct opt *
416 findopt(struct opt_head *list, char *name)
417 {
418 	struct opt *op;
419 
420 	SLIST_FOREACH(op, list, op_next)
421 		if (eq(op->op_name, name))
422 			return (op);
423 
424 	return (NULL);
425 }
426 
427 /*
428  * Add an option to the list of options.
429  */
430 static void
431 newopt(struct opt_head *list, char *name, char *value, int append, int dupe)
432 {
433 	struct opt *op, *op2;
434 
435 	/*
436 	 * Ignore inclusions listed explicitly for configuration files.
437 	 */
438 	if (eq(name, OPT_AUTOGEN)) {
439 		incignore = 1;
440 		return;
441 	}
442 
443 	op2 = findopt(list, name);
444 	if (op2 != NULL && !append && !dupe) {
445 		if (strcmp(op2->yyfile, yyfile) == 0)
446 			fprintf(stderr,
447 			    "WARNING: duplicate option `%s' encountered.\n", name);
448 		return;
449 	}
450 
451 	op = (struct opt *)calloc(1, sizeof (struct opt));
452 	if (op == NULL)
453 		err(EXIT_FAILURE, "calloc");
454 	op->op_name = name;
455 	op->op_ownfile = 0;
456 	op->op_value = value;
457 	op->yyfile = strdup(yyfile);
458 	if (op2 != NULL) {
459 		if (append) {
460 			while (SLIST_NEXT(op2, op_append) != NULL)
461 				op2 = SLIST_NEXT(op2, op_append);
462 			SLIST_NEXT(op2, op_append) = op;
463 		} else {
464 			while (SLIST_NEXT(op2, op_next) != NULL)
465 				op2 = SLIST_NEXT(op2, op_next);
466 			SLIST_NEXT(op2, op_next) = op;
467 		}
468 	} else
469 		SLIST_INSERT_HEAD(list, op, op_next);
470 }
471 
472 /*
473  * Remove an option from the list of options.
474  */
475 static void
476 rmopt_schedule(struct opt_head *list, char *name)
477 {
478 	struct opt *op;
479 
480 	while ((op = findopt(list, name)) != NULL) {
481 		SLIST_REMOVE(list, op, opt, op_next);
482 		free(op->yyfile);
483 		free(op->op_name);
484 		free(op);
485 	}
486 }
487