xref: /freebsd/usr.sbin/config/config.y (revision e28a4053)
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 { newopt(&mkopt, $1, ns(""), 0); } |
265 	Save_id EQUALS Opt_value { newopt(&mkopt, $1, $3, 0); } |
266 	Save_id PLUSEQUALS Opt_value { newopt(&mkopt, $1, $3, 1); } ;
267 
268 Dev:
269 	ID { $$ = $1; }
270 	;
271 
272 Device_spec:
273 	DEVICE Dev_list
274 		|
275 	NODEVICE NoDev_list
276 		;
277 
278 Dev_list:
279 	Dev_list COMMA Device
280 		|
281 	Device
282 		;
283 
284 NoDev_list:
285 	NoDev_list COMMA NoDevice
286 		|
287 	NoDevice
288 		;
289 
290 Device:
291 	Dev {
292 		newopt(&opt, devopt($1), ns("1"), 0);
293 		/* and the device part */
294 		newdev($1);
295 		}
296 
297 NoDevice:
298 	Dev {
299 		char *s = devopt($1);
300 
301 		rmopt_schedule(&opt, s);
302 		free(s);
303 		/* and the device part */
304 		rmdev_schedule(&dtab, $1);
305 		} ;
306 
307 %%
308 
309 void
310 yyerror(const char *s)
311 {
312 
313 	errx(1, "%s:%d: %s", yyfile, yyline + 1, s);
314 }
315 
316 int
317 yywrap(void)
318 {
319 	if (found_defaults) {
320 		if (freopen(PREFIX, "r", stdin) == NULL)
321 			err(2, "%s", PREFIX);
322 		yyfile = PREFIX;
323 		yyline = 0;
324 		found_defaults = 0;
325 		return 0;
326 	}
327 	return 1;
328 }
329 
330 /*
331  * Add a new file to the list of files.
332  */
333 static void
334 newfile(char *name)
335 {
336 	struct files_name *nl;
337 
338 	nl = (struct files_name *) calloc(1, sizeof *nl);
339 	if (nl == NULL)
340 		err(EXIT_FAILURE, "calloc");
341 	nl->f_name = name;
342 	STAILQ_INSERT_TAIL(&fntab, nl, f_next);
343 }
344 
345 /*
346  * Find a device in the list of devices.
347  */
348 static struct device *
349 finddev(struct device_head *dlist, char *name)
350 {
351 	struct device *dp;
352 
353 	STAILQ_FOREACH(dp, dlist, d_next)
354 		if (eq(dp->d_name, name))
355 			return (dp);
356 
357 	return (NULL);
358 }
359 
360 /*
361  * Add a device to the list of devices.
362  */
363 static void
364 newdev(char *name)
365 {
366 	struct device *np;
367 
368 	if (finddev(&dtab, name)) {
369 		fprintf(stderr,
370 		    "WARNING: duplicate device `%s' encountered.\n", name);
371 		return;
372 	}
373 
374 	np = (struct device *) calloc(1, sizeof *np);
375 	if (np == NULL)
376 		err(EXIT_FAILURE, "calloc");
377 	np->d_name = name;
378 	STAILQ_INSERT_TAIL(&dtab, np, d_next);
379 }
380 
381 /*
382  * Schedule a device to removal.
383  */
384 static void
385 rmdev_schedule(struct device_head *dh, char *name)
386 {
387 	struct device *dp;
388 
389 	dp = finddev(dh, name);
390 	if (dp != NULL) {
391 		STAILQ_REMOVE(dh, dp, device, d_next);
392 		free(dp->d_name);
393 		free(dp);
394 	}
395 }
396 
397 /*
398  * Find an option in the list of options.
399  */
400 static struct opt *
401 findopt(struct opt_head *list, char *name)
402 {
403 	struct opt *op;
404 
405 	SLIST_FOREACH(op, list, op_next)
406 		if (eq(op->op_name, name))
407 			return (op);
408 
409 	return (NULL);
410 }
411 
412 /*
413  * Add an option to the list of options.
414  */
415 static void
416 newopt(struct opt_head *list, char *name, char *value, int append)
417 {
418 	struct opt *op, *op2;
419 
420 	/*
421 	 * Ignore inclusions listed explicitly for configuration files.
422 	 */
423 	if (eq(name, OPT_AUTOGEN)) {
424 		incignore = 1;
425 		return;
426 	}
427 
428 	op2 = findopt(list, name);
429 	if (op2 != NULL && !append) {
430 		fprintf(stderr,
431 		    "WARNING: duplicate option `%s' encountered.\n", name);
432 		return;
433 	}
434 
435 	op = (struct opt *)calloc(1, sizeof (struct opt));
436 	if (op == NULL)
437 		err(EXIT_FAILURE, "calloc");
438 	op->op_name = name;
439 	op->op_ownfile = 0;
440 	op->op_value = value;
441 	if (op2 != NULL) {
442 		while (SLIST_NEXT(op2, op_append) != NULL)
443 			op2 = SLIST_NEXT(op2, op_append);
444 		SLIST_NEXT(op2, op_append) = op;
445 	} else
446 		SLIST_INSERT_HEAD(list, op, op_next);
447 }
448 
449 /*
450  * Remove an option from the list of options.
451  */
452 static void
453 rmopt_schedule(struct opt_head *list, char *name)
454 {
455 	struct opt *op;
456 
457 	op = findopt(list, name);
458 	if (op != NULL) {
459 		SLIST_REMOVE(list, op, opt, op_next);
460 		free(op->op_name);
461 		free(op);
462 	}
463 }
464