xref: /freebsd/usr.sbin/config/config.y (revision c0c066f86dd19c4802812b3ae28b019ca4909c56)
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 }
308 
309 int
310 yywrap(void)
311 {
312 	if (found_defaults) {
313 		if (freopen(PREFIX, "r", stdin) == NULL)
314 			err(2, "%s", PREFIX);
315 		yyfile = PREFIX;
316 		yyline = 0;
317 		found_defaults = 0;
318 		return 0;
319 	}
320 	return 1;
321 }
322 
323 /*
324  * Add a new file to the list of files.
325  */
326 static void
327 newfile(char *name)
328 {
329 	struct files_name *nl;
330 
331 	nl = (struct files_name *) calloc(1, sizeof *nl);
332 	if (nl == NULL)
333 		err(EXIT_FAILURE, "calloc");
334 	nl->f_name = name;
335 	STAILQ_INSERT_TAIL(&fntab, nl, f_next);
336 }
337 
338 static void
339 newenvvar(char *name, bool is_file)
340 {
341 	struct envvar *envvar;
342 
343 	envvar = (struct envvar *)calloc(1, sizeof (struct envvar));
344 	if (envvar == NULL)
345 		err(EXIT_FAILURE, "calloc");
346 	envvar->env_str = name;
347 	envvar->env_is_file = is_file;
348 	STAILQ_INSERT_HEAD(&envvars, envvar, envvar_next);
349 }
350 
351 /*
352  * Find a device in the list of devices.
353  */
354 static struct device *
355 finddev(struct device_head *dlist, char *name)
356 {
357 	struct device *dp;
358 
359 	STAILQ_FOREACH(dp, dlist, d_next)
360 		if (eq(dp->d_name, name))
361 			return (dp);
362 
363 	return (NULL);
364 }
365 
366 /*
367  * Add a device to the list of devices.
368  */
369 static void
370 newdev(char *name)
371 {
372 	struct device *np, *dp;
373 
374 	if ((dp = finddev(&dtab, name)) != NULL) {
375 		if (strcmp(dp->yyfile, yyfile) == 0)
376 			fprintf(stderr,
377 			    "WARNING: duplicate device `%s' encountered in %s\n",
378 			    name, yyfile);
379 		return;
380 	}
381 
382 	np = (struct device *) calloc(1, sizeof *np);
383 	if (np == NULL)
384 		err(EXIT_FAILURE, "calloc");
385 	np->d_name = name;
386 	np->yyfile = strdup(yyfile);
387 	STAILQ_INSERT_TAIL(&dtab, np, d_next);
388 }
389 
390 /*
391  * Schedule a device to removal.
392  */
393 static void
394 rmdev_schedule(struct device_head *dh, char *name)
395 {
396 	struct device *dp;
397 
398 	dp = finddev(dh, name);
399 	if (dp != NULL) {
400 		STAILQ_REMOVE(dh, dp, device, d_next);
401 		free(dp->yyfile);
402 		free(dp->d_name);
403 		free(dp);
404 	}
405 }
406 
407 /*
408  * Find an option in the list of options.
409  */
410 static struct opt *
411 findopt(struct opt_head *list, char *name)
412 {
413 	struct opt *op;
414 
415 	SLIST_FOREACH(op, list, op_next)
416 		if (eq(op->op_name, name))
417 			return (op);
418 
419 	return (NULL);
420 }
421 
422 /*
423  * Add an option to the list of options.
424  */
425 static void
426 newopt(struct opt_head *list, char *name, char *value, int append, int dupe)
427 {
428 	struct opt *op, *op2;
429 
430 	/*
431 	 * Ignore inclusions listed explicitly for configuration files.
432 	 */
433 	if (eq(name, OPT_AUTOGEN)) {
434 		incignore = 1;
435 		return;
436 	}
437 
438 	op2 = findopt(list, name);
439 	if (op2 != NULL && !append && !dupe) {
440 		if (strcmp(op2->yyfile, yyfile) == 0)
441 			fprintf(stderr,
442 			    "WARNING: duplicate option `%s' encountered.\n", name);
443 		return;
444 	}
445 
446 	op = (struct opt *)calloc(1, sizeof (struct opt));
447 	if (op == NULL)
448 		err(EXIT_FAILURE, "calloc");
449 	op->op_name = name;
450 	op->op_ownfile = 0;
451 	op->op_value = value;
452 	op->yyfile = strdup(yyfile);
453 	if (op2 != NULL) {
454 		if (append) {
455 			while (SLIST_NEXT(op2, op_append) != NULL)
456 				op2 = SLIST_NEXT(op2, op_append);
457 			SLIST_NEXT(op2, op_append) = op;
458 		} else {
459 			while (SLIST_NEXT(op2, op_next) != NULL)
460 				op2 = SLIST_NEXT(op2, op_next);
461 			SLIST_NEXT(op2, op_next) = op;
462 		}
463 	} else
464 		SLIST_INSERT_HEAD(list, op, op_next);
465 }
466 
467 /*
468  * Remove an option from the list of options.
469  */
470 static void
471 rmopt_schedule(struct opt_head *list, char *name)
472 {
473 	struct opt *op;
474 
475 	while ((op = findopt(list, name)) != NULL) {
476 		SLIST_REMOVE(list, op, opt, op_next);
477 		free(op->yyfile);
478 		free(op->op_name);
479 		free(op);
480 	}
481 }
482