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