xref: /freebsd/usr.sbin/config/config.y (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
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  * Copyright (c) 1988, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. 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	envmode;
87 int	hintmode;
88 int	yyline;
89 const	char *yyfile;
90 struct  file_list_head ftab;
91 struct  files_name_head fntab;
92 char	errbuf[80];
93 int	maxusers;
94 
95 #define ns(s)	strdup(s)
96 int include(const char *, int);
97 void yyerror(const char *s);
98 int yywrap(void);
99 
100 static void newdev(char *name);
101 static void newfile(char *name);
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 Save_id { rmopt_schedule(&opt, $2); } |
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 {
195 		env = $2;
196 		envmode = 1;
197 		} |
198 	HINTS ID {
199 		struct hint *hint;
200 
201 		hint = (struct hint *)calloc(1, sizeof (struct hint));
202 		if (hint == NULL)
203 			err(EXIT_FAILURE, "calloc");
204 		hint->hint_name = $2;
205 		STAILQ_INSERT_TAIL(&hints, hint, hint_next);
206 		hintmode = 1;
207 	        }
208 
209 System_spec:
210 	CONFIG System_id System_parameter_list {
211 		errx(1, "%s:%d: root/dump/swap specifications obsolete",
212 		      yyfile, yyline);
213 		}
214 	  |
215 	CONFIG System_id
216 	  ;
217 
218 System_id:
219 	Save_id { newopt(&mkopt, ns("KERNEL"), $1, 0); };
220 
221 System_parameter_list:
222 	  System_parameter_list ID
223 	| ID
224 	;
225 
226 Opt_list:
227 	Opt_list COMMA Option
228 		|
229 	Option
230 		;
231 
232 Option:
233 	Save_id {
234 		newopt(&opt, $1, NULL, 0);
235 		if (strchr($1, '=') != NULL)
236 			errx(1, "%s:%d: The `=' in options should not be "
237 			    "quoted", yyfile, yyline);
238 	      } |
239 	Save_id EQUALS Opt_value {
240 		newopt(&opt, $1, $3, 0);
241 	      } ;
242 
243 Opt_value:
244 	ID { $$ = $1; } |
245 	NUMBER {
246 			char buf[80];
247 
248 			(void) snprintf(buf, sizeof(buf), "%d", $1);
249 			$$ = ns(buf);
250 		} ;
251 
252 Save_id:
253 	ID { $$ = $1; }
254 	;
255 
256 Mkopt_list:
257 	Mkopt_list COMMA Mkoption
258 		|
259 	Mkoption
260 		;
261 
262 Mkoption:
263 	Save_id { newopt(&mkopt, $1, ns(""), 0); } |
264 	Save_id EQUALS Opt_value { newopt(&mkopt, $1, $3, 0); } |
265 	Save_id PLUSEQUALS Opt_value { newopt(&mkopt, $1, $3, 1); } ;
266 
267 Dev:
268 	ID { $$ = $1; }
269 	;
270 
271 Device_spec:
272 	DEVICE Dev_list
273 		|
274 	NODEVICE NoDev_list
275 		;
276 
277 Dev_list:
278 	Dev_list COMMA Device
279 		|
280 	Device
281 		;
282 
283 NoDev_list:
284 	NoDev_list COMMA NoDevice
285 		|
286 	NoDevice
287 		;
288 
289 Device:
290 	Dev {
291 		newopt(&opt, devopt($1), ns("1"), 0);
292 		/* and the device part */
293 		newdev($1);
294 		}
295 
296 NoDevice:
297 	Dev {
298 		char *s = devopt($1);
299 
300 		rmopt_schedule(&opt, s);
301 		free(s);
302 		/* and the device part */
303 		rmdev_schedule(&dtab, $1);
304 		} ;
305 
306 %%
307 
308 void
309 yyerror(const char *s)
310 {
311 
312 	errx(1, "%s:%d: %s", yyfile, yyline + 1, s);
313 }
314 
315 int
316 yywrap(void)
317 {
318 	if (found_defaults) {
319 		if (freopen(PREFIX, "r", stdin) == NULL)
320 			err(2, "%s", PREFIX);
321 		yyfile = PREFIX;
322 		yyline = 0;
323 		found_defaults = 0;
324 		return 0;
325 	}
326 	return 1;
327 }
328 
329 /*
330  * Add a new file to the list of files.
331  */
332 static void
333 newfile(char *name)
334 {
335 	struct files_name *nl;
336 
337 	nl = (struct files_name *) calloc(1, sizeof *nl);
338 	if (nl == NULL)
339 		err(EXIT_FAILURE, "calloc");
340 	nl->f_name = name;
341 	STAILQ_INSERT_TAIL(&fntab, nl, f_next);
342 }
343 
344 /*
345  * Find a device in the list of devices.
346  */
347 static struct device *
348 finddev(struct device_head *dlist, char *name)
349 {
350 	struct device *dp;
351 
352 	STAILQ_FOREACH(dp, dlist, d_next)
353 		if (eq(dp->d_name, name))
354 			return (dp);
355 
356 	return (NULL);
357 }
358 
359 /*
360  * Add a device to the list of devices.
361  */
362 static void
363 newdev(char *name)
364 {
365 	struct device *np;
366 
367 	if (finddev(&dtab, name)) {
368 		fprintf(stderr,
369 		    "WARNING: duplicate device `%s' encountered.\n", name);
370 		return;
371 	}
372 
373 	np = (struct device *) calloc(1, sizeof *np);
374 	if (np == NULL)
375 		err(EXIT_FAILURE, "calloc");
376 	np->d_name = name;
377 	STAILQ_INSERT_TAIL(&dtab, np, d_next);
378 }
379 
380 /*
381  * Schedule a device to removal.
382  */
383 static void
384 rmdev_schedule(struct device_head *dh, char *name)
385 {
386 	struct device *dp;
387 
388 	dp = finddev(dh, name);
389 	if (dp != NULL) {
390 		STAILQ_REMOVE(dh, dp, device, d_next);
391 		free(dp->d_name);
392 		free(dp);
393 	}
394 }
395 
396 /*
397  * Find an option in the list of options.
398  */
399 static struct opt *
400 findopt(struct opt_head *list, char *name)
401 {
402 	struct opt *op;
403 
404 	SLIST_FOREACH(op, list, op_next)
405 		if (eq(op->op_name, name))
406 			return (op);
407 
408 	return (NULL);
409 }
410 
411 /*
412  * Add an option to the list of options.
413  */
414 static void
415 newopt(struct opt_head *list, char *name, char *value, int append)
416 {
417 	struct opt *op, *op2;
418 
419 	/*
420 	 * Ignore inclusions listed explicitly for configuration files.
421 	 */
422 	if (eq(name, OPT_AUTOGEN)) {
423 		incignore = 1;
424 		return;
425 	}
426 
427 	op2 = findopt(list, name);
428 	if (op2 != NULL && !append) {
429 		fprintf(stderr,
430 		    "WARNING: duplicate option `%s' encountered.\n", name);
431 		return;
432 	}
433 
434 	op = (struct opt *)calloc(1, sizeof (struct opt));
435 	if (op == NULL)
436 		err(EXIT_FAILURE, "calloc");
437 	op->op_name = name;
438 	op->op_ownfile = 0;
439 	op->op_value = value;
440 	if (op2 != NULL) {
441 		while (SLIST_NEXT(op2, op_append) != NULL)
442 			op2 = SLIST_NEXT(op2, op_append);
443 		SLIST_NEXT(op2, op_append) = op;
444 	} else
445 		SLIST_INSERT_HEAD(list, op, op_next);
446 }
447 
448 /*
449  * Remove an option from the list of options.
450  */
451 static void
452 rmopt_schedule(struct opt_head *list, char *name)
453 {
454 	struct opt *op;
455 
456 	op = findopt(list, name);
457 	if (op != NULL) {
458 		SLIST_REMOVE(list, op, opt, op_next);
459 		free(op->op_name);
460 		free(op);
461 	}
462 }
463