xref: /freebsd/usr.sbin/config/config.y (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
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	EQUALS
16 %token	PLUSEQUALS
17 %token	HINTS
18 %token	IDENT
19 %token	MAXUSERS
20 %token	PROFILE
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>	ID
30 %token	<val>	NUMBER
31 
32 %type	<str>	Save_id
33 %type	<str>	Opt_value
34 %type	<str>	Dev
35 %token	<str>	PATH
36 
37 %{
38 
39 /*-
40  * SPDX-License-Identifier: BSD-3-Clause
41  *
42  * Copyright (c) 1988, 1993
43  *	The Regents of the University of California.  All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  *	@(#)config.y	8.1 (Berkeley) 6/6/93
70  * $FreeBSD$
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	envmode;
85 int	hintmode;
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 rmdev_schedule(struct device_head *dh, char *name);
101 static void newopt(struct opt_head *list, char *name, char *value, int append);
102 static void rmopt_schedule(struct opt_head *list, char *name);
103 
104 static char *
105 devopt(char *dev)
106 {
107 	char *ret = malloc(strlen(dev) + 5);
108 
109 	sprintf(ret, "DEV_%s", dev);
110 	raisestr(ret);
111 	return ret;
112 }
113 
114 %}
115 %%
116 Configuration:
117 	Many_specs
118 		;
119 
120 Many_specs:
121 	Many_specs Spec
122 		|
123 	/* lambda */
124 		;
125 
126 Spec:
127 	Device_spec SEMICOLON
128 		|
129 	Config_spec SEMICOLON
130 		|
131 	INCLUDE PATH SEMICOLON {
132 		if (incignore == 0)
133 			include($2, 0);
134 		};
135 		|
136 	INCLUDE ID SEMICOLON {
137 	          if (incignore == 0)
138 		  	include($2, 0);
139 		};
140 		|
141 	FILES ID SEMICOLON { newfile($2); };
142 	        |
143 	SEMICOLON
144 		|
145 	error SEMICOLON
146 		;
147 
148 Config_spec:
149 	ARCH Save_id {
150 		if (machinename != NULL && !eq($2, machinename))
151 		    errx(1, "%s:%d: only one machine directive is allowed",
152 			yyfile, yyline);
153 		machinename = $2;
154 		machinearch = $2;
155 	      } |
156 	ARCH Save_id Save_id {
157 		if (machinename != NULL &&
158 		    !(eq($2, machinename) && eq($3, machinearch)))
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 	System_spec
189 		|
190 	MAXUSERS NUMBER { maxusers = $2; } |
191 	PROFILE NUMBER { profiling = $2; } |
192 	ENV ID {
193 		env = $2;
194 		envmode = 1;
195 		} |
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 /*
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;
375 
376 	if (finddev(&dtab, name)) {
377 		fprintf(stderr,
378 		    "WARNING: duplicate device `%s' encountered.\n", name);
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 	STAILQ_INSERT_TAIL(&dtab, np, d_next);
387 }
388 
389 /*
390  * Schedule a device to removal.
391  */
392 static void
393 rmdev_schedule(struct device_head *dh, char *name)
394 {
395 	struct device *dp;
396 
397 	dp = finddev(dh, name);
398 	if (dp != NULL) {
399 		STAILQ_REMOVE(dh, dp, device, d_next);
400 		free(dp->d_name);
401 		free(dp);
402 	}
403 }
404 
405 /*
406  * Find an option in the list of options.
407  */
408 static struct opt *
409 findopt(struct opt_head *list, char *name)
410 {
411 	struct opt *op;
412 
413 	SLIST_FOREACH(op, list, op_next)
414 		if (eq(op->op_name, name))
415 			return (op);
416 
417 	return (NULL);
418 }
419 
420 /*
421  * Add an option to the list of options.
422  */
423 static void
424 newopt(struct opt_head *list, char *name, char *value, int append)
425 {
426 	struct opt *op, *op2;
427 
428 	/*
429 	 * Ignore inclusions listed explicitly for configuration files.
430 	 */
431 	if (eq(name, OPT_AUTOGEN)) {
432 		incignore = 1;
433 		return;
434 	}
435 
436 	op2 = findopt(list, name);
437 	if (op2 != NULL && !append) {
438 		fprintf(stderr,
439 		    "WARNING: duplicate option `%s' encountered.\n", name);
440 		return;
441 	}
442 
443 	op = (struct opt *)calloc(1, sizeof (struct opt));
444 	if (op == NULL)
445 		err(EXIT_FAILURE, "calloc");
446 	op->op_name = name;
447 	op->op_ownfile = 0;
448 	op->op_value = value;
449 	if (op2 != NULL) {
450 		while (SLIST_NEXT(op2, op_append) != NULL)
451 			op2 = SLIST_NEXT(op2, op_append);
452 		SLIST_NEXT(op2, op_append) = op;
453 	} else
454 		SLIST_INSERT_HEAD(list, op, op_next);
455 }
456 
457 /*
458  * Remove an option from the list of options.
459  */
460 static void
461 rmopt_schedule(struct opt_head *list, char *name)
462 {
463 	struct opt *op;
464 
465 	op = findopt(list, name);
466 	if (op != NULL) {
467 		SLIST_REMOVE(list, op, opt, op_next);
468 		free(op->op_name);
469 		free(op);
470 	}
471 }
472