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