xref: /freebsd/usr.sbin/config/config.y (revision 8847579c57d6aff2b3371c707dce7a2cee8389aa)
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 		char *s;
225 
226 		newopt(&opt, $1, NULL);
227 		if ((s = strchr($1, '=')))
228 			errx(1, "%s:%d: The `=' in options should not be "
229 			    "quoted", yyfile, yyline);
230 	      } |
231 	Save_id EQUALS Opt_value
232 	      = {
233 		newopt(&opt, $1, $3);
234 	      } ;
235 
236 Opt_value:
237 	ID
238 		= { $$ = $1; } |
239 	NUMBER
240 		= {
241 			char buf[80];
242 
243 			(void) snprintf(buf, sizeof(buf), "%d", $1);
244 			$$ = ns(buf);
245 		} ;
246 
247 Save_id:
248 	ID
249 	      = { $$ = $1; }
250 	;
251 
252 Mkopt_list:
253 	Mkopt_list COMMA Mkoption
254 		|
255 	Mkoption
256 		;
257 
258 Mkoption:
259 	Save_id
260 	      = { newopt(&mkopt, $1, ns("")); } |
261 	Save_id EQUALS Opt_value
262 	      = { newopt(&mkopt, $1, $3); } ;
263 
264 Dev:
265 	ID
266 	      = { $$ = $1; }
267 	;
268 
269 Device_spec:
270 	DEVICE Dev_list
271 		|
272 	NODEVICE NoDev_list
273 		;
274 
275 Dev_list:
276 	Dev_list COMMA Device
277 		|
278 	Device
279 		;
280 
281 NoDev_list:
282 	NoDev_list COMMA NoDevice
283 		|
284 	NoDevice
285 		;
286 
287 Device:
288 	Dev
289 	      = {
290 		newopt(&opt, devopt($1), ns("1"));
291 		/* and the device part */
292 		newdev($1);
293 		}
294 
295 NoDevice:
296 	Dev
297 	      = {
298 		char *s = devopt($1);
299 
300 		rmopt(&opt, s);
301 		free(s);
302 		/* and the device part */
303 		rmdev($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 
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 	nl->f_name = name;
340 	STAILQ_INSERT_TAIL(&fntab, nl, f_next);
341 }
342 
343 /*
344  * Find a device in the list of devices.
345  */
346 static struct device *
347 finddev(char *name)
348 {
349 	struct device *dp;
350 
351 	STAILQ_FOREACH(dp, &dtab, d_next)
352 		if (eq(dp->d_name, name))
353 			return (dp);
354 
355 	return (NULL);
356 }
357 
358 /*
359  * Add a device to the list of devices.
360  */
361 static void
362 newdev(char *name)
363 {
364 	struct device *np;
365 
366 	if (finddev(name)) {
367 		printf("WARNING: duplicate device `%s' encountered.\n", name);
368 		return;
369 	}
370 
371 	np = (struct device *) calloc(1, sizeof *np);
372 	np->d_name = name;
373 	STAILQ_INSERT_TAIL(&dtab, np, d_next);
374 }
375 
376 /*
377  * Remove a device from the list of devices.
378  */
379 static void
380 rmdev(char *name)
381 {
382 	struct device *dp;
383 
384 	dp = finddev(name);
385 	if (dp != NULL) {
386 		STAILQ_REMOVE(&dtab, dp, device, d_next);
387 		free(dp->d_name);
388 		free(dp);
389 	}
390 }
391 
392 /*
393  * Find an option in the list of options.
394  */
395 static struct opt *
396 findopt(struct opt_head *list, char *name)
397 {
398 	struct opt *op;
399 
400 	SLIST_FOREACH(op, list, op_next)
401 		if (eq(op->op_name, name))
402 			return (op);
403 
404 	return (NULL);
405 }
406 
407 /*
408  * Add an option to the list of options.
409  */
410 static void
411 newopt(struct opt_head *list, char *name, char *value)
412 {
413 	struct opt *op;
414 
415 	if (findopt(list, name)) {
416 		printf("WARNING: duplicate option `%s' encountered.\n", name);
417 		return;
418 	}
419 
420 	op = (struct opt *)calloc(1, sizeof (struct opt));
421 	op->op_name = name;
422 	op->op_ownfile = 0;
423 	op->op_value = value;
424 	SLIST_INSERT_HEAD(list, op, op_next);
425 }
426 
427 /*
428  * Remove an option from the list of options.
429  */
430 static void
431 rmopt(struct opt_head *list, char *name)
432 {
433 	struct opt *op;
434 
435 	op = findopt(list, name);
436 	if (op != NULL) {
437 		SLIST_REMOVE(list, op, opt, op_next);
438 		free(op->op_name);
439 		if (op->op_value != NULL)
440 			free(op->op_value);
441 		free(op);
442 	}
443 }
444