xref: /freebsd/usr.sbin/config/config.y (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 	MAXUSERS NUMBER { maxusers = $2; } |
192 	ENV ID { newenvvar($2, true); } |
193 	ENVVAR ENVLINE { newenvvar($2, false); } |
194 	HINTS ID {
195 		struct hint *hint;
196 
197 		hint = (struct hint *)calloc(1, sizeof (struct hint));
198 		if (hint == NULL)
199 			err(EXIT_FAILURE, "calloc");
200 		hint->hint_name = $2;
201 		STAILQ_INSERT_HEAD(&hints, hint, hint_next);
202 	        }
203 
204 System_id:
205 	Save_id { newopt(&mkopt, ns("KERNEL"), $1, 0, 0); };
206 
207 System_parameter_list:
208 	  System_parameter_list ID
209 	| ID
210 	;
211 
212 Opt_list:
213 	Opt_list COMMA Option
214 		|
215 	Option
216 		;
217 
218 NoOpt_list:
219 	NoOpt_list COMMA NoOption
220 	  	|
221 	NoOption
222 		;
223 Option:
224 	Save_id {
225 		newopt(&opt, $1, NULL, 0, 1);
226 		if (strchr($1, '=') != NULL)
227 			errx(1, "%s:%d: The `=' in options should not be "
228 			    "quoted", yyfile, yyline);
229 	      } |
230 	Save_id EQUALS Opt_value {
231 		newopt(&opt, $1, $3, 0, 1);
232 	      } ;
233 
234 NoOption:
235 	Save_id {
236 		rmopt_schedule(&opt, $1);
237 		};
238 
239 Opt_value:
240 	ID { $$ = $1; } |
241 	NUMBER {
242 			char buf[80];
243 
244 			(void) snprintf(buf, sizeof(buf), "%d", $1);
245 			$$ = ns(buf);
246 		} ;
247 
248 Save_id:
249 	ID { $$ = $1; }
250 	;
251 
252 Mkopt_list:
253 	Mkopt_list COMMA Mkoption
254 		|
255 	Mkoption
256 		;
257 
258 Mkoption:
259 	Save_id { newopt(&mkopt, $1, ns(""), 0, 0); } |
260 	Save_id EQUALS { newopt(&mkopt, $1, ns(""), 0, 0); } |
261 	Save_id EQUALS Opt_value { newopt(&mkopt, $1, $3, 0, 0); } |
262 	Save_id PLUSEQUALS Opt_value { newopt(&mkopt, $1, $3, 1, 0); } ;
263 
264 Dev:
265 	ID { $$ = $1; }
266 	;
267 
268 Device_spec:
269 	DEVICE Dev_list
270 		|
271 	NODEVICE NoDev_list
272 		;
273 
274 Dev_list:
275 	Dev_list COMMA Device
276 		|
277 	Device
278 		;
279 
280 NoDev_list:
281 	NoDev_list COMMA NoDevice
282 		|
283 	NoDevice
284 		;
285 
286 Device:
287 	Dev {
288 		newopt(&opt, devopt($1), ns("1"), 0, 0);
289 		/* and the device part */
290 		newdev($1);
291 		}
292 
293 NoDevice:
294 	Dev {
295 		char *s = devopt($1);
296 
297 		rmopt_schedule(&opt, s);
298 		free(s);
299 		/* and the device part */
300 		rmdev_schedule(&dtab, $1);
301 		} ;
302 
303 %%
304 
305 void
306 yyerror(const char *s)
307 {
308 
309 	errx(1, "%s:%d: %s", yyfile, yyline + 1, s);
310 }
311 
312 int
313 yywrap(void)
314 {
315 	if (found_defaults) {
316 		if (freopen(PREFIX, "r", stdin) == NULL)
317 			err(2, "%s", PREFIX);
318 		yyfile = PREFIX;
319 		yyline = 0;
320 		found_defaults = 0;
321 		return 0;
322 	}
323 	return 1;
324 }
325 
326 /*
327  * Add a new file to the list of files.
328  */
329 static void
330 newfile(char *name)
331 {
332 	struct files_name *nl;
333 
334 	nl = (struct files_name *) calloc(1, sizeof *nl);
335 	if (nl == NULL)
336 		err(EXIT_FAILURE, "calloc");
337 	nl->f_name = name;
338 	STAILQ_INSERT_TAIL(&fntab, nl, f_next);
339 }
340 
341 static void
342 newenvvar(char *name, bool is_file)
343 {
344 	struct envvar *envvar;
345 
346 	envvar = (struct envvar *)calloc(1, sizeof (struct envvar));
347 	if (envvar == NULL)
348 		err(EXIT_FAILURE, "calloc");
349 	envvar->env_str = name;
350 	envvar->env_is_file = is_file;
351 	STAILQ_INSERT_HEAD(&envvars, envvar, envvar_next);
352 }
353 
354 /*
355  * Find a device in the list of devices.
356  */
357 static struct device *
358 finddev(struct device_head *dlist, char *name)
359 {
360 	struct device *dp;
361 
362 	STAILQ_FOREACH(dp, dlist, d_next)
363 		if (eq(dp->d_name, name))
364 			return (dp);
365 
366 	return (NULL);
367 }
368 
369 /*
370  * Add a device to the list of devices.
371  */
372 static void
373 newdev(char *name)
374 {
375 	struct device *np, *dp;
376 
377 	if ((dp = finddev(&dtab, name)) != NULL) {
378 		if (strcmp(dp->yyfile, yyfile) == 0)
379 			fprintf(stderr,
380 			    "WARNING: duplicate device `%s' encountered in %s\n",
381 			    name, yyfile);
382 		return;
383 	}
384 
385 	np = (struct device *) calloc(1, sizeof *np);
386 	if (np == NULL)
387 		err(EXIT_FAILURE, "calloc");
388 	np->d_name = name;
389 	np->yyfile = strdup(yyfile);
390 	STAILQ_INSERT_TAIL(&dtab, np, d_next);
391 }
392 
393 /*
394  * Schedule a device to removal.
395  */
396 static void
397 rmdev_schedule(struct device_head *dh, char *name)
398 {
399 	struct device *dp;
400 
401 	dp = finddev(dh, name);
402 	if (dp != NULL) {
403 		STAILQ_REMOVE(dh, dp, device, d_next);
404 		free(dp->yyfile);
405 		free(dp->d_name);
406 		free(dp);
407 	}
408 }
409 
410 /*
411  * Find an option in the list of options.
412  */
413 static struct opt *
414 findopt(struct opt_head *list, char *name)
415 {
416 	struct opt *op;
417 
418 	SLIST_FOREACH(op, list, op_next)
419 		if (eq(op->op_name, name))
420 			return (op);
421 
422 	return (NULL);
423 }
424 
425 /*
426  * Add an option to the list of options.
427  */
428 static void
429 newopt(struct opt_head *list, char *name, char *value, int append, int dupe)
430 {
431 	struct opt *op, *op2;
432 
433 	/*
434 	 * Ignore inclusions listed explicitly for configuration files.
435 	 */
436 	if (eq(name, OPT_AUTOGEN)) {
437 		incignore = 1;
438 		return;
439 	}
440 
441 	op2 = findopt(list, name);
442 	if (op2 != NULL && !append && !dupe) {
443 		if (strcmp(op2->yyfile, yyfile) == 0)
444 			fprintf(stderr,
445 			    "WARNING: duplicate option `%s' encountered.\n", name);
446 		return;
447 	}
448 
449 	op = (struct opt *)calloc(1, sizeof (struct opt));
450 	if (op == NULL)
451 		err(EXIT_FAILURE, "calloc");
452 	op->op_name = name;
453 	op->op_ownfile = 0;
454 	op->op_value = value;
455 	op->yyfile = strdup(yyfile);
456 	if (op2 != NULL) {
457 		if (append) {
458 			while (SLIST_NEXT(op2, op_append) != NULL)
459 				op2 = SLIST_NEXT(op2, op_append);
460 			SLIST_NEXT(op2, op_append) = op;
461 		} else {
462 			while (SLIST_NEXT(op2, op_next) != NULL)
463 				op2 = SLIST_NEXT(op2, op_next);
464 			SLIST_NEXT(op2, op_next) = op;
465 		}
466 	} else
467 		SLIST_INSERT_HEAD(list, op, op_next);
468 }
469 
470 /*
471  * Remove an option from the list of options.
472  */
473 static void
474 rmopt_schedule(struct opt_head *list, char *name)
475 {
476 	struct opt *op;
477 
478 	while ((op = findopt(list, name)) != NULL) {
479 		SLIST_REMOVE(list, op, opt, op_next);
480 		free(op->yyfile);
481 		free(op->op_name);
482 		free(op);
483 	}
484 }
485