xref: /freebsd/usr.sbin/config/config.y (revision e4e9813eb92cd7c4d4b819a8fbed5cbd3d92f5d8)
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	HINTS
17 %token	IDENT
18 %token	MAXUSERS
19 %token	PROFILE
20 %token	OPTIONS
21 %token	NOOPTION
22 %token	MAKEOPTIONS
23 %token	NOMAKEOPTION
24 %token	SEMICOLON
25 %token	INCLUDE
26 %token	FILES
27 
28 %token	<str>	ID
29 %token	<val>	NUMBER
30 
31 %type	<str>	Save_id
32 %type	<str>	Opt_value
33 %type	<str>	Dev
34 
35 %{
36 
37 /*
38  * Copyright (c) 1988, 1993
39  *	The Regents of the University of California.  All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. All advertising materials mentioning features or use of this software
50  *    must display the following acknowledgement:
51  *	This product includes software developed by the University of
52  *	California, Berkeley and its contributors.
53  * 4. 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 <ctype.h>
74 #include <err.h>
75 #include <stdio.h>
76 #include <string.h>
77 
78 #include "config.h"
79 
80 struct	device_head dtab;
81 char	*ident;
82 char	*env;
83 int	envmode;
84 char	*hints;
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 char *
99 devopt(char *dev)
100 {
101 	char *ret = malloc(strlen(dev) + 5);
102 
103 	sprintf(ret, "DEV_%s", dev);
104 	raisestr(ret);
105 	return ret;
106 }
107 
108 %}
109 %%
110 Configuration:
111 	Many_specs
112 		;
113 
114 Many_specs:
115 	Many_specs Spec
116 		|
117 	/* lambda */
118 		;
119 
120 Spec:
121 	Device_spec SEMICOLON
122 		|
123 	Config_spec SEMICOLON
124 		|
125 	INCLUDE ID SEMICOLON
126 	      = { include($2, 0); };
127 		|
128 	FILES ID SEMICOLON
129 	      = { newfile($2); };
130 	        |
131 	SEMICOLON
132 		|
133 	error SEMICOLON
134 		;
135 
136 Config_spec:
137 	ARCH Save_id
138 	    = {
139 		if (machinename != NULL && !eq($2, machinename))
140 		    errx(1, "%s:%d: only one machine directive is allowed",
141 			yyfile, yyline);
142 		machinename = $2;
143 		machinearch = $2;
144 	      } |
145 	ARCH Save_id Save_id
146 	    = {
147 		if (machinename != NULL &&
148 		    !(eq($2, machinename) && eq($3, machinearch)))
149 		    errx(1, "%s:%d: only one machine directive is allowed",
150 			yyfile, yyline);
151 		machinename = $2;
152 		machinearch = $3;
153 	      } |
154 	CPU Save_id
155 	      = {
156 		struct cputype *cp =
157 		    (struct cputype *)calloc(1, sizeof (struct cputype));
158 		cp->cpu_name = $2;
159 		SLIST_INSERT_HEAD(&cputype, cp, cpu_next);
160 	      } |
161 	NOCPU Save_id
162 	      = {
163 		struct cputype *cp, *cp2;
164 		SLIST_FOREACH_SAFE(cp, &cputype, cpu_next, cp2) {
165 			if (eq(cp->cpu_name, $2)) {
166 				SLIST_REMOVE(&cputype, cp, cputype, cpu_next);
167 				free(cp);
168 			}
169 		}
170 	      } |
171 	OPTIONS Opt_list
172 		|
173 	NOOPTION Save_id
174 	      = { rmopt(&opt, $2); } |
175 	MAKEOPTIONS Mkopt_list
176 		|
177 	NOMAKEOPTION Save_id
178 	      = { rmopt(&mkopt, $2); } |
179 	IDENT ID
180 	      = { ident = $2; } |
181 	System_spec
182 		|
183 	MAXUSERS NUMBER
184 	      = { maxusers = $2; } |
185 	PROFILE NUMBER
186 	      = { profiling = $2; } |
187 	ENV ID
188 	      = {
189 		      env = $2;
190 		      envmode = 1;
191 		} |
192 	HINTS ID
193 	      = {
194 		      hints = $2;
195 		      hintmode = 1;
196 	        }
197 
198 System_spec:
199 	CONFIG System_id System_parameter_list
200 	  = { errx(1, "%s:%d: root/dump/swap specifications obsolete",
201 	      yyfile, yyline);}
202 	  |
203 	CONFIG System_id
204 	  ;
205 
206 System_id:
207 	Save_id
208 	      = { newopt(&mkopt, ns("KERNEL"), $1); };
209 
210 System_parameter_list:
211 	  System_parameter_list ID
212 	| ID
213 	;
214 
215 Opt_list:
216 	Opt_list COMMA Option
217 		|
218 	Option
219 		;
220 
221 Option:
222 	Save_id
223 	      = {
224 		newopt(&opt, $1, NULL);
225 		if (strchr($1, '=') != NULL)
226 			errx(1, "%s:%d: The `=' in options should not be "
227 			    "quoted", yyfile, yyline);
228 	      } |
229 	Save_id EQUALS Opt_value
230 	      = {
231 		newopt(&opt, $1, $3);
232 	      } ;
233 
234 Opt_value:
235 	ID
236 		= { $$ = $1; } |
237 	NUMBER
238 		= {
239 			char buf[80];
240 
241 			(void) snprintf(buf, sizeof(buf), "%d", $1);
242 			$$ = ns(buf);
243 		} ;
244 
245 Save_id:
246 	ID
247 	      = { $$ = $1; }
248 	;
249 
250 Mkopt_list:
251 	Mkopt_list COMMA Mkoption
252 		|
253 	Mkoption
254 		;
255 
256 Mkoption:
257 	Save_id
258 	      = { newopt(&mkopt, $1, ns("")); } |
259 	Save_id EQUALS Opt_value
260 	      = { newopt(&mkopt, $1, $3); } ;
261 
262 Dev:
263 	ID
264 	      = { $$ = $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 	      = {
288 		newopt(&opt, devopt($1), ns("1"));
289 		/* and the device part */
290 		newdev($1);
291 		}
292 
293 NoDevice:
294 	Dev
295 	      = {
296 		char *s = devopt($1);
297 
298 		rmopt(&opt, s);
299 		free(s);
300 		/* and the device part */
301 		rmdev($1);
302 		} ;
303 
304 %%
305 
306 void
307 yyerror(const char *s)
308 {
309 
310 	errx(1, "%s:%d: %s", yyfile, yyline + 1, s);
311 }
312 
313 int
314 yywrap(void)
315 {
316 
317 	if (found_defaults) {
318 		if (freopen(PREFIX, "r", stdin) == NULL)
319 			err(2, "%s", PREFIX);
320 		yyfile = PREFIX;
321 		yyline = 0;
322 		found_defaults = 0;
323 		return 0;
324 	}
325 	return 1;
326 }
327 
328 /*
329  * Add a new file to the list of files.
330  */
331 static void
332 newfile(char *name)
333 {
334 	struct files_name *nl;
335 
336 	nl = (struct files_name *) calloc(1, sizeof *nl);
337 	nl->f_name = name;
338 	STAILQ_INSERT_TAIL(&fntab, nl, f_next);
339 }
340 
341 /*
342  * Find a device in the list of devices.
343  */
344 static struct device *
345 finddev(char *name)
346 {
347 	struct device *dp;
348 
349 	STAILQ_FOREACH(dp, &dtab, d_next)
350 		if (eq(dp->d_name, name))
351 			return (dp);
352 
353 	return (NULL);
354 }
355 
356 /*
357  * Add a device to the list of devices.
358  */
359 static void
360 newdev(char *name)
361 {
362 	struct device *np;
363 
364 	if (finddev(name)) {
365 		printf("WARNING: duplicate device `%s' encountered.\n", name);
366 		return;
367 	}
368 
369 	np = (struct device *) calloc(1, sizeof *np);
370 	np->d_name = name;
371 	STAILQ_INSERT_TAIL(&dtab, np, d_next);
372 }
373 
374 /*
375  * Remove a device from the list of devices.
376  */
377 static void
378 rmdev(char *name)
379 {
380 	struct device *dp;
381 
382 	dp = finddev(name);
383 	if (dp != NULL) {
384 		STAILQ_REMOVE(&dtab, dp, device, d_next);
385 		free(dp->d_name);
386 		free(dp);
387 	}
388 }
389 
390 /*
391  * Find an option in the list of options.
392  */
393 static struct opt *
394 findopt(struct opt_head *list, char *name)
395 {
396 	struct opt *op;
397 
398 	SLIST_FOREACH(op, list, op_next)
399 		if (eq(op->op_name, name))
400 			return (op);
401 
402 	return (NULL);
403 }
404 
405 /*
406  * Add an option to the list of options.
407  */
408 static void
409 newopt(struct opt_head *list, char *name, char *value)
410 {
411 	struct opt *op;
412 
413 	if (findopt(list, name)) {
414 		printf("WARNING: duplicate option `%s' encountered.\n", name);
415 		return;
416 	}
417 
418 	op = (struct opt *)calloc(1, sizeof (struct opt));
419 	op->op_name = name;
420 	op->op_ownfile = 0;
421 	op->op_value = value;
422 	SLIST_INSERT_HEAD(list, op, op_next);
423 }
424 
425 /*
426  * Remove an option from the list of options.
427  */
428 static void
429 rmopt(struct opt_head *list, char *name)
430 {
431 	struct opt *op;
432 
433 	op = findopt(list, name);
434 	if (op != NULL) {
435 		SLIST_REMOVE(list, op, opt, op_next);
436 		free(op->op_name);
437 		if (op->op_value != NULL)
438 			free(op->op_value);
439 		free(op);
440 	}
441 }
442