xref: /freebsd/usr.sbin/config/config.y (revision 55141f2c8991b2a6adbf30bb0fe3e6cbc303f06d)
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  */
72 
73 #include <assert.h>
74 #include <ctype.h>
75 #include <err.h>
76 #include <stdio.h>
77 #include <string.h>
78 
79 #include "config.h"
80 
81 struct	device_head dtab;
82 char	*ident;
83 char	*env;
84 int	yyline;
85 const	char *yyfile;
86 struct  file_list_head ftab;
87 struct  files_name_head fntab;
88 char	errbuf[80];
89 int	maxusers;
90 
91 #define ns(s)	strdup(s)
92 int include(const char *, int);
93 void yyerror(const char *s);
94 int yywrap(void);
95 
96 static void newdev(char *name);
97 static void newfile(char *name);
98 static void newenvvar(char *name, bool is_file);
99 static void rmdev_schedule(struct device_head *dh, char *name);
100 static void newopt(struct opt_head *list, char *name, char *value, int append, int dupe);
101 static void rmopt_schedule(struct opt_head *list, char *name);
102 
103 static char *
104 devopt(char *dev)
105 {
106 	char *ret = malloc(strlen(dev) + 5);
107 
108 	sprintf(ret, "DEV_%s", dev);
109 	raisestr(ret);
110 	return ret;
111 }
112 
113 %}
114 %%
115 Configuration:
116 	Many_specs
117 		;
118 
119 Many_specs:
120 	Many_specs Spec
121 		|
122 	/* lambda */
123 		;
124 
125 Spec:
126 	Device_spec SEMICOLON
127 		|
128 	Config_spec SEMICOLON
129 		|
130 	INCLUDE PATH SEMICOLON {
131 		if (incignore == 0)
132 			include($2, 0);
133 		};
134 		|
135 	INCLUDE ID SEMICOLON {
136 	          if (incignore == 0)
137 		  	include($2, 0);
138 		};
139 		|
140 	FILES ID SEMICOLON { newfile($2); };
141 	        |
142 	SEMICOLON
143 		|
144 	error SEMICOLON
145 		;
146 
147 Config_spec:
148 	ARCH Save_id {
149 		if (machinename != NULL && !eq($2, machinename))
150 		    errx(1, "%s:%d: only one machine directive is allowed",
151 			yyfile, yyline);
152 		machinename = $2;
153 		machinearch = $2;
154 	      } |
155 	ARCH Save_id Save_id {
156 		/*
157 		 * Allow the machinearch to change with a second machine directive,
158 		 * but still enforce no changes to the machinename.
159 		 */
160 		if (machinename != NULL && !eq($2, machinename))
161 		    errx(1, "%s:%d: only one machine directive is allowed",
162 			yyfile, yyline);
163 		machinename = $2;
164 		machinearch = $3;
165 	      } |
166 	CPU Save_id {
167 		struct cputype *cp =
168 		    (struct cputype *)calloc(1, sizeof (struct cputype));
169 		if (cp == NULL)
170 			err(EXIT_FAILURE, "calloc");
171 		cp->cpu_name = $2;
172 		SLIST_INSERT_HEAD(&cputype, cp, cpu_next);
173 	      } |
174 	NOCPU Save_id {
175 		struct cputype *cp, *cp2;
176 		SLIST_FOREACH_SAFE(cp, &cputype, cpu_next, cp2) {
177 			if (eq(cp->cpu_name, $2)) {
178 				SLIST_REMOVE(&cputype, cp, cputype, cpu_next);
179 				free(cp);
180 			}
181 		}
182 	      } |
183 	OPTIONS Opt_list
184 		|
185 	NOOPTION NoOpt_list |
186 	MAKEOPTIONS Mkopt_list
187 		|
188 	NOMAKEOPTION Save_id { rmopt_schedule(&mkopt, $2); } |
189 	IDENT ID { ident = $2; } |
190 	MAXUSERS NUMBER { maxusers = $2; } |
191 	ENV ID { newenvvar($2, true); } |
192 	ENVVAR ENVLINE { newenvvar($2, false); } |
193 	HINTS ID {
194 		struct hint *hint;
195 
196 		hint = (struct hint *)calloc(1, sizeof (struct hint));
197 		if (hint == NULL)
198 			err(EXIT_FAILURE, "calloc");
199 		hint->hint_name = $2;
200 		STAILQ_INSERT_HEAD(&hints, hint, hint_next);
201 	        }
202 
203 System_id:
204 	Save_id { newopt(&mkopt, ns("KERNEL"), $1, 0, 0); };
205 
206 System_parameter_list:
207 	  System_parameter_list ID
208 	| ID
209 	;
210 
211 Opt_list:
212 	Opt_list COMMA Option
213 		|
214 	Option
215 		;
216 
217 NoOpt_list:
218 	NoOpt_list COMMA NoOption
219 	  	|
220 	NoOption
221 		;
222 Option:
223 	Save_id {
224 		newopt(&opt, $1, NULL, 0, 1);
225 		if (strchr($1, '=') != NULL)
226 			errx(1, "%s:%d: The `=' in options should not be "
227 			    "quoted", yyfile, yyline);
228 	      } |
229 	Save_id EQUALS Opt_value {
230 		newopt(&opt, $1, $3, 0, 1);
231 	      } ;
232 
233 NoOption:
234 	Save_id {
235 		rmopt_schedule(&opt, $1);
236 		};
237 
238 Opt_value:
239 	ID { $$ = $1; } |
240 	NUMBER {
241 			char buf[80];
242 
243 			(void) snprintf(buf, sizeof(buf), "%d", $1);
244 			$$ = ns(buf);
245 		} ;
246 
247 Save_id:
248 	ID { $$ = $1; }
249 	;
250 
251 Mkopt_list:
252 	Mkopt_list COMMA Mkoption
253 		|
254 	Mkoption
255 		;
256 
257 Mkoption:
258 	Save_id { newopt(&mkopt, $1, ns(""), 0, 0); } |
259 	Save_id EQUALS { newopt(&mkopt, $1, ns(""), 0, 0); } |
260 	Save_id EQUALS Opt_value { newopt(&mkopt, $1, $3, 0, 0); } |
261 	Save_id PLUSEQUALS Opt_value { newopt(&mkopt, $1, $3, 1, 0); } ;
262 
263 Dev:
264 	ID { $$ = $1; }
265 	;
266 
267 Device_spec:
268 	DEVICE Dev_list
269 		|
270 	NODEVICE NoDev_list
271 		;
272 
273 Dev_list:
274 	Dev_list COMMA Device
275 		|
276 	Device
277 		;
278 
279 NoDev_list:
280 	NoDev_list COMMA NoDevice
281 		|
282 	NoDevice
283 		;
284 
285 Device:
286 	Dev {
287 		newopt(&opt, devopt($1), ns("1"), 0, 0);
288 		/* and the device part */
289 		newdev($1);
290 		}
291 
292 NoDevice:
293 	Dev {
294 		char *s = devopt($1);
295 
296 		rmopt_schedule(&opt, s);
297 		free(s);
298 		/* and the device part */
299 		rmdev_schedule(&dtab, $1);
300 		} ;
301 
302 %%
303 
304 void
305 yyerror(const char *s)
306 {
307 
308 	errx(1, "%s:%d: %s", yyfile, yyline + 1, s);
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