config.y revision 180922
11553Srgrimes%union {
21553Srgrimes	char	*str;
31553Srgrimes	int	val;
41553Srgrimes	struct	file_list *file;
51553Srgrimes}
61553Srgrimes
752653Smarcel%token	ARCH
81553Srgrimes%token	COMMA
946855Speter%token	CONFIG
101553Srgrimes%token	CPU
11152018Sru%token	NOCPU
121553Srgrimes%token	DEVICE
13111582Sru%token	NODEVICE
1482393Speter%token	ENV
151553Srgrimes%token	EQUALS
1661640Speter%token	HINTS
171553Srgrimes%token	IDENT
181553Srgrimes%token	MAXUSERS
1967109Sphk%token	PROFILE
201553Srgrimes%token	OPTIONS
21111582Sru%token	NOOPTION
221553Srgrimes%token	MAKEOPTIONS
23111582Sru%token	NOMAKEOPTION
241553Srgrimes%token	SEMICOLON
2579607Sdd%token	INCLUDE
26129073Scognet%token	FILES
271553Srgrimes
281553Srgrimes%token	<str>	ID
291553Srgrimes%token	<val>	NUMBER
301553Srgrimes
311553Srgrimes%type	<str>	Save_id
3246104Sluoqi%type	<str>	Opt_value
331553Srgrimes%type	<str>	Dev
34180922Sobrien%token	<str>	PATH
351553Srgrimes
361553Srgrimes%{
371553Srgrimes
381553Srgrimes/*
391553Srgrimes * Copyright (c) 1988, 1993
401553Srgrimes *	The Regents of the University of California.  All rights reserved.
411553Srgrimes *
421553Srgrimes * Redistribution and use in source and binary forms, with or without
431553Srgrimes * modification, are permitted provided that the following conditions
441553Srgrimes * are met:
451553Srgrimes * 1. Redistributions of source code must retain the above copyright
461553Srgrimes *    notice, this list of conditions and the following disclaimer.
471553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
481553Srgrimes *    notice, this list of conditions and the following disclaimer in the
491553Srgrimes *    documentation and/or other materials provided with the distribution.
501553Srgrimes * 3. All advertising materials mentioning features or use of this software
511553Srgrimes *    must display the following acknowledgement:
521553Srgrimes *	This product includes software developed by the University of
531553Srgrimes *	California, Berkeley and its contributors.
541553Srgrimes * 4. Neither the name of the University nor the names of its contributors
551553Srgrimes *    may be used to endorse or promote products derived from this software
561553Srgrimes *    without specific prior written permission.
571553Srgrimes *
581553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
591553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
601553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
611553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
621553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
631553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
641553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
651553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
661553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
671553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
681553Srgrimes * SUCH DAMAGE.
691553Srgrimes *
701553Srgrimes *	@(#)config.y	8.1 (Berkeley) 6/6/93
7152007Speter * $FreeBSD: head/usr.sbin/config/config.y 180922 2008-07-28 17:11:57Z obrien $
721553Srgrimes */
731553Srgrimes
74169507Swkoszek#include <assert.h>
751553Srgrimes#include <ctype.h>
7629451Scharnier#include <err.h>
771553Srgrimes#include <stdio.h>
7812772Speter#include <string.h>
791553Srgrimes
8045775Speter#include "config.h"
8145775Speter
82169647Simpstruct	device_head dtab;
8345744Speterchar	*ident;
8482393Speterchar	*env;
8582393Speterint	envmode;
8665091Speterint	hintmode;
8745744Speterint	yyline;
8879607Sddconst	char *yyfile;
89110895Srustruct  file_list_head ftab;
90129073Scognetstruct  files_name_head fntab;
9145744Speterchar	errbuf[80];
9245744Speterint	maxusers;
9345744Speter
9420458Sjoerg#define ns(s)	strdup(s)
9579607Sddint include(const char *, int);
9679607Sddvoid yyerror(const char *s);
97151744Sjhbint yywrap(void);
9820458Sjoerg
99174892Simpstatic void newdev(char *name);
100174892Simpstatic void newfile(char *name);
101174892Simpstatic void rmdev_schedule(struct device_head *dh, char *name);
102174892Simpstatic void newopt(struct opt_head *list, char *name, char *value);
103174892Simpstatic void rmopt_schedule(struct opt_head *list, char *name);
104174892Simp
10571251Speterstatic char *
10671251Speterdevopt(char *dev)
10771251Speter{
10871251Speter	char *ret = malloc(strlen(dev) + 5);
10971251Speter
11071251Speter	sprintf(ret, "DEV_%s", dev);
11171251Speter	raisestr(ret);
11271251Speter	return ret;
11371251Speter}
11445775Speter
1151553Srgrimes%}
1161553Srgrimes%%
1171553SrgrimesConfiguration:
1181553Srgrimes	Many_specs
1191553Srgrimes		;
1201553Srgrimes
1211553SrgrimesMany_specs:
1221553Srgrimes	Many_specs Spec
1231553Srgrimes		|
1241553Srgrimes	/* lambda */
1251553Srgrimes		;
1261553Srgrimes
1271553SrgrimesSpec:
1281553Srgrimes	Device_spec SEMICOLON
12972841Speter		|
1301553Srgrimes	Config_spec SEMICOLON
1311553Srgrimes		|
132180922Sobrien	INCLUDE PATH SEMICOLON {
133180922Sobrien		if (incignore == 0)
134180922Sobrien			include($2, 0);
135180922Sobrien		};
136180922Sobrien		|
137174892Simp	INCLUDE ID SEMICOLON {
138169507Swkoszek	          if (incignore == 0)
139169507Swkoszek		  	include($2, 0);
140169507Swkoszek		};
141122656Sbde		|
142174892Simp	FILES ID SEMICOLON { newfile($2); };
143129073Scognet	        |
1441553Srgrimes	SEMICOLON
1451553Srgrimes		|
1461553Srgrimes	error SEMICOLON
1471553Srgrimes		;
1481553Srgrimes
1491553SrgrimesConfig_spec:
150174892Simp	ARCH Save_id {
151152865Sru		if (machinename != NULL && !eq($2, machinename))
152117269Sjkoshy		    errx(1, "%s:%d: only one machine directive is allowed",
153117269Sjkoshy			yyfile, yyline);
15472000Speter		machinename = $2;
155144509Simp		machinearch = $2;
1561553Srgrimes	      } |
157174892Simp	ARCH Save_id Save_id {
158152865Sru		if (machinename != NULL &&
159152865Sru		    !(eq($2, machinename) && eq($3, machinearch)))
160144509Simp		    errx(1, "%s:%d: only one machine directive is allowed",
161144509Simp			yyfile, yyline);
162144509Simp		machinename = $2;
163144509Simp		machinearch = $3;
164144509Simp	      } |
165174892Simp	CPU Save_id {
1661553Srgrimes		struct cputype *cp =
167159362Sdelphij		    (struct cputype *)calloc(1, sizeof (struct cputype));
16820458Sjoerg		cp->cpu_name = $2;
169110895Sru		SLIST_INSERT_HEAD(&cputype, cp, cpu_next);
1701553Srgrimes	      } |
171174892Simp	NOCPU Save_id {
172152018Sru		struct cputype *cp, *cp2;
173152018Sru		SLIST_FOREACH_SAFE(cp, &cputype, cpu_next, cp2) {
174152024Sru			if (eq(cp->cpu_name, $2)) {
175152024Sru				SLIST_REMOVE(&cputype, cp, cputype, cpu_next);
176152024Sru				free(cp);
177152024Sru			}
178152018Sru		}
179152018Sru	      } |
1801553Srgrimes	OPTIONS Opt_list
1811553Srgrimes		|
182174892Simp	NOOPTION Save_id { rmopt_schedule(&opt, $2); } |
1831553Srgrimes	MAKEOPTIONS Mkopt_list
1841553Srgrimes		|
185174892Simp	NOMAKEOPTION Save_id { rmopt_schedule(&mkopt, $2); } |
186174892Simp	IDENT ID { ident = $2; } |
18746855Speter	System_spec
18846855Speter		|
189174892Simp	MAXUSERS NUMBER { maxusers = $2; } |
190174892Simp	PROFILE NUMBER { profiling = $2; } |
191174892Simp	ENV ID {
192163637Simp		env = $2;
193163637Simp		envmode = 1;
19482393Speter		} |
195174892Simp	HINTS ID {
196163638Simp		struct hint *hint;
197163638Simp
198163638Simp		hint = (struct hint *)calloc(1, sizeof (struct hint));
199163638Simp		hint->hint_name = $2;
200163638Simp		STAILQ_INSERT_TAIL(&hints, hint, hint_next);
201163637Simp		hintmode = 1;
202122656Sbde	        }
2031553Srgrimes
20446855SpeterSystem_spec:
205174892Simp	CONFIG System_id System_parameter_list {
206174892Simp		errx(1, "%s:%d: root/dump/swap specifications obsolete",
207174892Simp		      yyfile, yyline);
208174892Simp		}
20946855Speter	  |
21046855Speter	CONFIG System_id
21146855Speter	  ;
21246855Speter
21346855SpeterSystem_id:
214174892Simp	Save_id { newopt(&mkopt, ns("KERNEL"), $1); };
21546855Speter
21646855SpeterSystem_parameter_list:
21746855Speter	  System_parameter_list ID
21846855Speter	| ID
21946855Speter	;
22046855Speter
2211553SrgrimesOpt_list:
2221553Srgrimes	Opt_list COMMA Option
2231553Srgrimes		|
2241553Srgrimes	Option
2251553Srgrimes		;
2261553Srgrimes
2271553SrgrimesOption:
228174892Simp	Save_id {
22972844Speter		newopt(&opt, $1, NULL);
230160522Sstefanf		if (strchr($1, '=') != NULL)
23179607Sdd			errx(1, "%s:%d: The `=' in options should not be "
23279607Sdd			    "quoted", yyfile, yyline);
2331553Srgrimes	      } |
234174892Simp	Save_id EQUALS Opt_value {
23572844Speter		newopt(&opt, $1, $3);
2361553Srgrimes	      } ;
2371553Srgrimes
23846104SluoqiOpt_value:
239174892Simp	ID { $$ = $1; } |
240174892Simp	NUMBER {
24146021Speter			char buf[80];
2421553Srgrimes
24346021Speter			(void) snprintf(buf, sizeof(buf), "%d", $1);
24446021Speter			$$ = ns(buf);
24546021Speter		} ;
2461553Srgrimes
2471553SrgrimesSave_id:
248174892Simp	ID { $$ = $1; }
2491553Srgrimes	;
2501553Srgrimes
2511553SrgrimesMkopt_list:
2521553Srgrimes	Mkopt_list COMMA Mkoption
2531553Srgrimes		|
2541553Srgrimes	Mkoption
2551553Srgrimes		;
2561553Srgrimes
2571553SrgrimesMkoption:
258174892Simp	Save_id { newopt(&mkopt, $1, ns("")); } |
259174892Simp	Save_id EQUALS Opt_value { newopt(&mkopt, $1, $3); } ;
2601553Srgrimes
2611553SrgrimesDev:
262174892Simp	ID { $$ = $1; }
2631553Srgrimes	;
2641553Srgrimes
2651553SrgrimesDevice_spec:
266136880Sdes	DEVICE Dev_list
267136880Sdes		|
268136880Sdes	NODEVICE NoDev_list
269136880Sdes		;
270136880Sdes
271136880SdesDev_list:
272136880Sdes	Dev_list COMMA Device
273136880Sdes		|
274136880Sdes	Device
275136880Sdes		;
276136880Sdes
277136880SdesNoDev_list:
278136880Sdes	NoDev_list COMMA NoDevice
279136880Sdes		|
280136880Sdes	NoDevice
281136880Sdes		;
282136880Sdes
283136880SdesDevice:
284174892Simp	Dev {
285136880Sdes		newopt(&opt, devopt($1), ns("1"));
28671251Speter		/* and the device part */
287136880Sdes		newdev($1);
288136880Sdes		}
289136880Sdes
290136880SdesNoDevice:
291174892Simp	Dev {
292136880Sdes		char *s = devopt($1);
293111582Sru
294169647Simp		rmopt_schedule(&opt, s);
295111582Sru		free(s);
296110897Sru		/* and the device part */
297169647Simp		rmdev_schedule(&dtab, $1);
29846021Speter		} ;
2991553Srgrimes
3001553Srgrimes%%
3011553Srgrimes
30279607Sddvoid
30372684Speteryyerror(const char *s)
3041553Srgrimes{
30529493Scharnier
30679607Sdd	errx(1, "%s:%d: %s", yyfile, yyline + 1, s);
3071553Srgrimes}
3081553Srgrimes
309151744Sjhbint
310151744Sjhbyywrap(void)
311151744Sjhb{
312169647Simp	if (found_defaults) {
313169647Simp		if (freopen(PREFIX, "r", stdin) == NULL)
314169647Simp			err(2, "%s", PREFIX);
315169647Simp		yyfile = PREFIX;
316151744Sjhb		yyline = 0;
317169647Simp		found_defaults = 0;
318151744Sjhb		return 0;
319151744Sjhb	}
320151744Sjhb	return 1;
321151744Sjhb}
322151744Sjhb
3231553Srgrimes/*
324129073Scognet * Add a new file to the list of files.
325129073Scognet */
326129073Scognetstatic void
327129073Scognetnewfile(char *name)
328129073Scognet{
329129073Scognet	struct files_name *nl;
330129073Scognet
331159362Sdelphij	nl = (struct files_name *) calloc(1, sizeof *nl);
332129073Scognet	nl->f_name = name;
333129073Scognet	STAILQ_INSERT_TAIL(&fntab, nl, f_next);
334129073Scognet}
335129073Scognet
336129073Scognet/*
337153889Sru * Find a device in the list of devices.
3381553Srgrimes */
339153889Srustatic struct device *
340169507Swkoszekfinddev(struct device_head *dlist, char *name)
341153889Sru{
342153889Sru	struct device *dp;
343153889Sru
344169507Swkoszek	STAILQ_FOREACH(dp, dlist, d_next)
345153889Sru		if (eq(dp->d_name, name))
346153889Sru			return (dp);
347153889Sru
348153889Sru	return (NULL);
349153889Sru}
350153889Sru
351153889Sru/*
352153889Sru * Add a device to the list of devices.
353153889Sru */
35445744Speterstatic void
355134542Speternewdev(char *name)
3561553Srgrimes{
35761640Speter	struct device *np;
3581553Srgrimes
359169507Swkoszek	if (finddev(&dtab, name)) {
360153889Sru		printf("WARNING: duplicate device `%s' encountered.\n", name);
361153889Sru		return;
362153889Sru	}
363153889Sru
364159362Sdelphij	np = (struct device *) calloc(1, sizeof *np);
36572841Speter	np->d_name = name;
366110895Sru	STAILQ_INSERT_TAIL(&dtab, np, d_next);
3671553Srgrimes}
36872841Speter
369110897Sru/*
370169507Swkoszek * Schedule a device to removal.
371110897Sru */
37272841Speterstatic void
373169507Swkoszekrmdev_schedule(struct device_head *dh, char *name)
374110897Sru{
375153889Sru	struct device *dp;
376110897Sru
377169647Simp	dp = finddev(dh, name);
378169647Simp	if (dp != NULL) {
379169647Simp		STAILQ_REMOVE(dh, dp, device, d_next);
380169647Simp		free(dp->d_name);
381153889Sru		free(dp);
382110897Sru	}
383110897Sru}
384110897Sru
385153889Sru/*
386153889Sru * Find an option in the list of options.
387153889Sru */
388153889Srustatic struct opt *
389153889Srufindopt(struct opt_head *list, char *name)
390153889Sru{
391153889Sru	struct opt *op;
392153889Sru
393153889Sru	SLIST_FOREACH(op, list, op_next)
394153889Sru		if (eq(op->op_name, name))
395153889Sru			return (op);
396153889Sru
397153889Sru	return (NULL);
398153889Sru}
399153889Sru
400153889Sru/*
401153889Sru * Add an option to the list of options.
402153889Sru */
403110897Srustatic void
404110895Srunewopt(struct opt_head *list, char *name, char *value)
40572841Speter{
40672841Speter	struct opt *op;
40772841Speter
408169507Swkoszek	/*
409169507Swkoszek	 * Ignore inclusions listed explicitly for configuration files.
410169507Swkoszek	 */
411169507Swkoszek	if (eq(name, OPT_AUTOGEN)) {
412169507Swkoszek		incignore = 1;
413169507Swkoszek		return;
414169507Swkoszek	}
415169507Swkoszek
416153889Sru	if (findopt(list, name)) {
417153889Sru		printf("WARNING: duplicate option `%s' encountered.\n", name);
418153889Sru		return;
419153889Sru	}
420153889Sru
421159362Sdelphij	op = (struct opt *)calloc(1, sizeof (struct opt));
42272841Speter	op->op_name = name;
42372841Speter	op->op_ownfile = 0;
42472841Speter	op->op_value = value;
425110895Sru	SLIST_INSERT_HEAD(list, op, op_next);
42672841Speter}
427110897Sru
428153889Sru/*
429153889Sru * Remove an option from the list of options.
430153889Sru */
431110897Srustatic void
432169507Swkoszekrmopt_schedule(struct opt_head *list, char *name)
433110897Sru{
434153889Sru	struct opt *op;
435110897Sru
436169647Simp	op = findopt(list, name);
437169647Simp	if (op != NULL) {
438169647Simp		SLIST_REMOVE(list, op, opt, op_next);
439169647Simp		free(op->op_name);
440153889Sru		free(op);
441110897Sru	}
442110897Sru}
443