xref: /freebsd/usr.sbin/config/config.y (revision 0d7056458db5b5dd7fdc5ccd8abab73e3ee76a20)
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 
71 #include <assert.h>
72 #include <ctype.h>
73 #include <err.h>
74 #include <stdio.h>
75 #include <string.h>
76 
77 #include "config.h"
78 
79 struct	device_head dtab;
80 char	*ident;
81 char	*env;
82 int	yyline;
83 const	char *yyfile;
84 struct  file_list_head ftab;
85 struct  files_name_head fntab;
86 char	errbuf[80];
87 int	maxusers;
88 
89 #define ns(s)	strdup(s)
90 int include(const char *, int);
91 int yyerror(const char *s);
92 int yywrap(void);
93 
94 static void newdev(char *name);
95 static void newfile(char *name);
96 static void newenvvar(char *name, bool is_file);
97 static void rmdev_schedule(struct device_head *dh, char *name);
98 static void newopt(struct opt_head *list, char *name, char *value, int append, int dupe);
99 static void rmopt_schedule(struct opt_head *list, char *name);
100 
101 static char *
102 devopt(char *dev)
103 {
104 	char *ret = malloc(strlen(dev) + 5);
105 
106 	sprintf(ret, "DEV_%s", dev);
107 	raisestr(ret);
108 	return ret;
109 }
110 
111 %}
112 %%
113 Configuration:
114 	Many_specs
115 		;
116 
117 Many_specs:
118 	Many_specs Spec
119 		|
120 	/* lambda */
121 		;
122 
123 Spec:
124 	Device_spec SEMICOLON
125 		|
126 	Config_spec SEMICOLON
127 		|
128 	INCLUDE PATH SEMICOLON {
129 		if (incignore == 0)
130 			include($2, 0);
131 		};
132 		|
133 	INCLUDE ID SEMICOLON {
134 	          if (incignore == 0)
135 		  	include($2, 0);
136 		};
137 		|
138 	FILES ID SEMICOLON { newfile($2); };
139 	        |
140 	SEMICOLON
141 		|
142 	error SEMICOLON
143 		;
144 
145 Config_spec:
146 	ARCH Save_id {
147 		if (machinename != NULL && !eq($2, machinename))
148 		    errx(1, "%s:%d: only one machine directive is allowed",
149 			yyfile, yyline);
150 		machinename = $2;
151 		machinearch = $2;
152 	      } |
153 	ARCH Save_id Save_id {
154 		/*
155 		 * Allow the machinearch to change with a second machine directive,
156 		 * but still enforce no changes to the machinename.
157 		 */
158 		if (machinename != NULL && !eq($2, machinename))
159 		    errx(1, "%s:%d: only one machine directive is allowed",
160 			yyfile, yyline);
161 		machinename = $2;
162 		machinearch = $3;
163 	      } |
164 	CPU Save_id {
165 		struct cputype *cp =
166 		    (struct cputype *)calloc(1, sizeof (struct cputype));
167 		if (cp == NULL)
168 			err(EXIT_FAILURE, "calloc");
169 		cp->cpu_name = $2;
170 		SLIST_INSERT_HEAD(&cputype, cp, cpu_next);
171 	      } |
172 	NOCPU Save_id {
173 		struct cputype *cp, *cp2;
174 		SLIST_FOREACH_SAFE(cp, &cputype, cpu_next, cp2) {
175 			if (eq(cp->cpu_name, $2)) {
176 				SLIST_REMOVE(&cputype, cp, cputype, cpu_next);
177 				free(cp);
178 			}
179 		}
180 	      } |
181 	OPTIONS Opt_list
182 		|
183 	NOOPTION NoOpt_list |
184 	MAKEOPTIONS Mkopt_list
185 		|
186 	NOMAKEOPTION Save_id { rmopt_schedule(&mkopt, $2); } |
187 	IDENT ID { ident = $2; } |
188 	MAXUSERS NUMBER { maxusers = $2; } |
189 	ENV ID { newenvvar($2, true); } |
190 	ENVVAR ENVLINE { newenvvar($2, false); } |
191 	HINTS ID {
192 		struct hint *hint;
193 
194 		hint = (struct hint *)calloc(1, sizeof (struct hint));
195 		if (hint == NULL)
196 			err(EXIT_FAILURE, "calloc");
197 		hint->hint_name = $2;
198 		STAILQ_INSERT_HEAD(&hints, hint, hint_next);
199 	        }
200 
201 System_id:
202 	Save_id { newopt(&mkopt, ns("KERNEL"), $1, 0, 0); };
203 
204 System_parameter_list:
205 	  System_parameter_list ID
206 	| ID
207 	;
208 
209 Opt_list:
210 	Opt_list COMMA Option
211 		|
212 	Option
213 		;
214 
215 NoOpt_list:
216 	NoOpt_list COMMA NoOption
217 	  	|
218 	NoOption
219 		;
220 Option:
221 	Save_id {
222 		newopt(&opt, $1, NULL, 0, 1);
223 		if (strchr($1, '=') != NULL)
224 			errx(1, "%s:%d: The `=' in options should not be "
225 			    "quoted", yyfile, yyline);
226 	      } |
227 	Save_id EQUALS Opt_value {
228 		newopt(&opt, $1, $3, 0, 1);
229 	      } ;
230 
231 NoOption:
232 	Save_id {
233 		rmopt_schedule(&opt, $1);
234 		};
235 
236 Opt_value:
237 	ID { $$ = $1; } |
238 	NUMBER {
239 			char buf[80];
240 
241 			(void) snprintf(buf, sizeof(buf), "%d", $1);
242 			$$ = ns(buf);
243 		} ;
244 
245 Save_id:
246 	ID { $$ = $1; }
247 	;
248 
249 Mkopt_list:
250 	Mkopt_list COMMA Mkoption
251 		|
252 	Mkoption
253 		;
254 
255 Mkoption:
256 	Save_id { newopt(&mkopt, $1, ns(""), 0, 0); } |
257 	Save_id EQUALS { newopt(&mkopt, $1, ns(""), 0, 0); } |
258 	Save_id EQUALS Opt_value { newopt(&mkopt, $1, $3, 0, 0); } |
259 	Save_id PLUSEQUALS Opt_value { newopt(&mkopt, $1, $3, 1, 0); } ;
260 
261 Dev:
262 	ID { $$ = $1; }
263 	;
264 
265 Device_spec:
266 	DEVICE Dev_list
267 		|
268 	NODEVICE NoDev_list
269 		;
270 
271 Dev_list:
272 	Dev_list COMMA Device
273 		|
274 	Device
275 		;
276 
277 NoDev_list:
278 	NoDev_list COMMA NoDevice
279 		|
280 	NoDevice
281 		;
282 
283 Device:
284 	Dev {
285 		newopt(&opt, devopt($1), ns("1"), 0, 0);
286 		/* and the device part */
287 		newdev($1);
288 		}
289 
290 NoDevice:
291 	Dev {
292 		char *s = devopt($1);
293 
294 		rmopt_schedule(&opt, s);
295 		free(s);
296 		/* and the device part */
297 		rmdev_schedule(&dtab, $1);
298 		} ;
299 
300 %%
301 
302 int
303 yyerror(const char *s)
304 {
305 
306 	errx(1, "%s:%d: %s", yyfile, yyline + 1, s);
307 	__unreachable();
308 	return (0);
309 }
310 
311 int
312 yywrap(void)
313 {
314 	if (found_defaults) {
315 		if (freopen(PREFIX, "r", stdin) == NULL)
316 			err(2, "%s", PREFIX);
317 		yyfile = PREFIX;
318 		yyline = 0;
319 		found_defaults = 0;
320 		return 0;
321 	}
322 	return 1;
323 }
324 
325 /*
326  * Add a new file to the list of files.
327  */
328 static void
329 newfile(char *name)
330 {
331 	struct files_name *nl;
332 
333 	nl = (struct files_name *) calloc(1, sizeof *nl);
334 	if (nl == NULL)
335 		err(EXIT_FAILURE, "calloc");
336 	nl->f_name = name;
337 	STAILQ_INSERT_TAIL(&fntab, nl, f_next);
338 }
339 
340 static void
341 newenvvar(char *name, bool is_file)
342 {
343 	struct envvar *envvar;
344 
345 	envvar = (struct envvar *)calloc(1, sizeof (struct envvar));
346 	if (envvar == NULL)
347 		err(EXIT_FAILURE, "calloc");
348 	envvar->env_str = name;
349 	envvar->env_is_file = is_file;
350 	STAILQ_INSERT_HEAD(&envvars, envvar, envvar_next);
351 }
352 
353 /*
354  * Find a device in the list of devices.
355  */
356 static struct device *
357 finddev(struct device_head *dlist, char *name)
358 {
359 	struct device *dp;
360 
361 	STAILQ_FOREACH(dp, dlist, d_next)
362 		if (eq(dp->d_name, name))
363 			return (dp);
364 
365 	return (NULL);
366 }
367 
368 /*
369  * Add a device to the list of devices.
370  */
371 static void
372 newdev(char *name)
373 {
374 	struct device *np, *dp;
375 
376 	if ((dp = finddev(&dtab, name)) != NULL) {
377 		if (strcmp(dp->yyfile, yyfile) == 0)
378 			fprintf(stderr,
379 			    "WARNING: duplicate device `%s' encountered in %s\n",
380 			    name, yyfile);
381 		return;
382 	}
383 
384 	np = (struct device *) calloc(1, sizeof *np);
385 	if (np == NULL)
386 		err(EXIT_FAILURE, "calloc");
387 	np->d_name = name;
388 	np->yyfile = strdup(yyfile);
389 	STAILQ_INSERT_TAIL(&dtab, np, d_next);
390 }
391 
392 /*
393  * Schedule a device to removal.
394  */
395 static void
396 rmdev_schedule(struct device_head *dh, char *name)
397 {
398 	struct device *dp;
399 
400 	dp = finddev(dh, name);
401 	if (dp != NULL) {
402 		STAILQ_REMOVE(dh, dp, device, d_next);
403 		free(dp->yyfile);
404 		free(dp->d_name);
405 		free(dp);
406 	}
407 }
408 
409 /*
410  * Find an option in the list of options.
411  */
412 static struct opt *
413 findopt(struct opt_head *list, char *name)
414 {
415 	struct opt *op;
416 
417 	SLIST_FOREACH(op, list, op_next)
418 		if (eq(op->op_name, name))
419 			return (op);
420 
421 	return (NULL);
422 }
423 
424 /*
425  * Add an option to the list of options.
426  */
427 static void
428 newopt(struct opt_head *list, char *name, char *value, int append, int dupe)
429 {
430 	struct opt *op, *op2;
431 
432 	/*
433 	 * Ignore inclusions listed explicitly for configuration files.
434 	 */
435 	if (eq(name, OPT_AUTOGEN)) {
436 		incignore = 1;
437 		return;
438 	}
439 
440 	op2 = findopt(list, name);
441 	if (op2 != NULL && !append && !dupe) {
442 		if (strcmp(op2->yyfile, yyfile) == 0)
443 			fprintf(stderr,
444 			    "WARNING: duplicate option `%s' encountered.\n", name);
445 		return;
446 	}
447 
448 	op = (struct opt *)calloc(1, sizeof (struct opt));
449 	if (op == NULL)
450 		err(EXIT_FAILURE, "calloc");
451 	op->op_name = name;
452 	op->op_ownfile = 0;
453 	op->op_value = value;
454 	op->yyfile = strdup(yyfile);
455 	if (op2 != NULL) {
456 		if (append) {
457 			while (SLIST_NEXT(op2, op_append) != NULL)
458 				op2 = SLIST_NEXT(op2, op_append);
459 			SLIST_NEXT(op2, op_append) = op;
460 		} else {
461 			while (SLIST_NEXT(op2, op_next) != NULL)
462 				op2 = SLIST_NEXT(op2, op_next);
463 			SLIST_NEXT(op2, op_next) = op;
464 		}
465 	} else
466 		SLIST_INSERT_HEAD(list, op, op_next);
467 }
468 
469 /*
470  * Remove an option from the list of options.
471  */
472 static void
473 rmopt_schedule(struct opt_head *list, char *name)
474 {
475 	struct opt *op;
476 
477 	while ((op = findopt(list, name)) != NULL) {
478 		SLIST_REMOVE(list, op, opt, op_next);
479 		free(op->yyfile);
480 		free(op->op_name);
481 		free(op);
482 	}
483 }
484