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