xref: /netbsd-src/usr.bin/config/gram.y (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
1 %{
2 /*	$NetBSD: gram.y,v 1.38 2013/08/11 10:37:08 pooka Exp $	*/
3 
4 /*
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This software was developed by the Computer Systems Engineering group
9  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10  * contributed to Berkeley.
11  *
12  * All advertising materials mentioning features or use of this software
13  * must display the following acknowledgement:
14  *	This product includes software developed by the University of
15  *	California, Lawrence Berkeley Laboratories.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	from: @(#)gram.y	8.1 (Berkeley) 6/6/93
42  */
43 
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <ctype.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <errno.h>
51 #include "defs.h"
52 #include "sem.h"
53 
54 #define	FORMAT(n) (((n).fmt == 8 && (n).val != 0) ? "0%llo" : \
55     ((n).fmt == 16) ? "0x%llx" : "%lld")
56 
57 #define	stop(s)	cfgerror(s), exit(1)
58 
59 static	struct	config conf;	/* at most one active at a time */
60 
61 
62 /*
63  * Allocation wrapper functions
64  */
65 static void wrap_alloc(void *ptr, unsigned code);
66 static void wrap_continue(void);
67 static void wrap_cleanup(void);
68 
69 /*
70  * Allocation wrapper type codes
71  */
72 #define WRAP_CODE_nvlist	1
73 #define WRAP_CODE_defoptlist	2
74 #define WRAP_CODE_loclist	3
75 #define WRAP_CODE_attrlist	4
76 #define WRAP_CODE_condexpr	5
77 
78 /*
79  * The allocation wrappers themselves
80  */
81 #define DECL_ALLOCWRAP(t)	static struct t *wrap_mk_##t(struct t *arg)
82 
83 DECL_ALLOCWRAP(nvlist);
84 DECL_ALLOCWRAP(defoptlist);
85 DECL_ALLOCWRAP(loclist);
86 DECL_ALLOCWRAP(attrlist);
87 DECL_ALLOCWRAP(condexpr);
88 
89 /* allow shorter names */
90 #define wrap_mk_loc(p) wrap_mk_loclist(p)
91 #define wrap_mk_cx(p) wrap_mk_condexpr(p)
92 
93 /*
94  * Macros for allocating new objects
95  */
96 
97 /* old-style for struct nvlist */
98 #define	new0(n,s,p,i,x)	wrap_mk_nvlist(newnv(n, s, p, i, x))
99 #define	new_n(n)	new0(n, NULL, NULL, 0, NULL)
100 #define	new_nx(n, x)	new0(n, NULL, NULL, 0, x)
101 #define	new_ns(n, s)	new0(n, s, NULL, 0, NULL)
102 #define	new_si(s, i)	new0(NULL, s, NULL, i, NULL)
103 #define	new_nsi(n,s,i)	new0(n, s, NULL, i, NULL)
104 #define	new_np(n, p)	new0(n, NULL, p, 0, NULL)
105 #define	new_s(s)	new0(NULL, s, NULL, 0, NULL)
106 #define	new_p(p)	new0(NULL, NULL, p, 0, NULL)
107 #define	new_px(p, x)	new0(NULL, NULL, p, 0, x)
108 #define	new_sx(s, x)	new0(NULL, s, NULL, 0, x)
109 #define	new_nsx(n,s,x)	new0(n, s, NULL, 0, x)
110 #define	new_i(i)	new0(NULL, NULL, NULL, i, NULL)
111 
112 /* new style, type-polymorphic; ordinary and for types with multiple flavors */
113 #define MK0(t)		wrap_mk_##t(mk_##t())
114 #define MK1(t, a0)	wrap_mk_##t(mk_##t(a0))
115 #define MK2(t, a0, a1)	wrap_mk_##t(mk_##t(a0, a1))
116 #define MK3(t, a0, a1, a2)	wrap_mk_##t(mk_##t(a0, a1, a2))
117 
118 #define MKF0(t, f)		wrap_mk_##t(mk_##t##_##f())
119 #define MKF1(t, f, a0)		wrap_mk_##t(mk_##t##_##f(a0))
120 #define MKF2(t, f, a0, a1)	wrap_mk_##t(mk_##t##_##f(a0, a1))
121 
122 /*
123  * Data constructors
124  */
125 
126 static struct defoptlist *mk_defoptlist(const char *, const char *,
127 					const char *);
128 static struct loclist *mk_loc(const char *, const char *, long long);
129 static struct loclist *mk_loc_val(const char *, struct loclist *);
130 static struct attrlist *mk_attrlist(struct attrlist *, struct attr *);
131 static struct condexpr *mk_cx_atom(const char *);
132 static struct condexpr *mk_cx_not(struct condexpr *);
133 static struct condexpr *mk_cx_and(struct condexpr *, struct condexpr *);
134 static struct condexpr *mk_cx_or(struct condexpr *, struct condexpr *);
135 
136 /*
137  * Other private functions
138  */
139 
140 static	void	setmachine(const char *, const char *, struct nvlist *, int);
141 static	void	check_maxpart(void);
142 
143 static struct loclist *present_loclist(struct loclist *ll);
144 static void app(struct loclist *, struct loclist *);
145 static struct loclist *locarray(const char *, int, struct loclist *, int);
146 static struct loclist *namelocvals(const char *, struct loclist *);
147 
148 %}
149 
150 %union {
151 	struct	attr *attr;
152 	struct	devbase *devb;
153 	struct	deva *deva;
154 	struct	nvlist *list;
155 	struct defoptlist *defoptlist;
156 	struct loclist *loclist;
157 	struct attrlist *attrlist;
158 	struct condexpr *condexpr;
159 	const char *str;
160 	struct	numconst num;
161 	int64_t	val;
162 }
163 
164 %token	AND AT ATTACH
165 %token	BLOCK BUILD
166 %token	CHAR COLONEQ COMPILE_WITH CONFIG
167 %token	DEFFS DEFINE DEFOPT DEFPARAM DEFFLAG DEFPSEUDO DEFPSEUDODEV
168 %token	DEVICE DEVCLASS DUMPS DEVICE_MAJOR
169 %token	ENDFILE
170 %token	XFILE FILE_SYSTEM FLAGS
171 %token	IDENT IOCONF
172 %token	LINKZERO
173 %token	XMACHINE MAJOR MAKEOPTIONS MAXUSERS MAXPARTITIONS MINOR
174 %token	NEEDS_COUNT NEEDS_FLAG NO
175 %token	XOBJECT OBSOLETE ON OPTIONS
176 %token	PACKAGE PLUSEQ PREFIX PSEUDO_DEVICE PSEUDO_ROOT
177 %token	ROOT
178 %token	SINGLE SOURCE
179 %token	TYPE
180 %token	VECTOR VERSION
181 %token	WITH
182 %token	<num> NUMBER
183 %token	<str> PATHNAME QSTRING WORD EMPTYSTRING
184 %token	ENDDEFS
185 
186 %type	<condexpr>	fopts condexpr condatom
187 %type	<condexpr>	cond_or_expr cond_and_expr cond_prefix_expr
188 %type	<condexpr>	 cond_base_expr
189 %type	<str>	fs_spec
190 %type	<val>	fflags fflag oflags oflag
191 %type	<str>	rule
192 %type	<attr>	depend
193 %type	<devb>	devbase
194 %type	<deva>	devattach_opt
195 %type	<list>	atlist
196 %type	<loclist> interface_opt
197 %type	<str>	atname
198 %type	<loclist>	loclist locdef
199 %type	<str>	locdefault
200 %type	<loclist>	values locdefaults
201 %type	<attrlist>	depend_list depends
202 %type	<loclist>	locators locator
203 %type	<list>	dev_spec
204 %type	<str>	device_instance
205 %type	<str>	attachment
206 %type	<str>	value
207 %type	<val>	major_minor npseudo
208 %type	<num>	signed_number
209 %type	<val>	device_flags
210 %type	<str>	deffs
211 %type	<list>	deffses
212 %type	<defoptlist>	defopt
213 %type	<defoptlist>	defopts
214 %type	<str>	optdepend
215 %type	<list>	optdepends
216 %type	<list>	optdepend_list
217 %type	<str>	optfile_opt
218 %type	<list>	subarches
219 %type	<str>	filename stringvalue locname mkvarname
220 %type	<val>	device_major_block device_major_char
221 %type	<list>	devnodes devnodetype devnodeflags devnode_dims
222 
223 %%
224 
225 /*
226  * A complete configuration consists of both the configuration part (a
227  * kernel config such as GENERIC or SKYNET, plus also the various
228  * std.* files), which selects the material to be in the kernel, and
229  * also the definition part (files, files.*, etc.) that declares what
230  * material is available to be placed in kernels.
231  *
232  * The two parts have almost entirely separate syntaxes. This grammar
233  * covers both of them. When config is run on a kernel configuration
234  * file, the std.* file for the port is included explicitly. The
235  * files.* files are included implicitly when the std.* file declares
236  * the machine type.
237  *
238  * The machine spec, which brings in the definition part, must appear
239  * before all configuration material except for the "topthings"; these
240  * are the "source" and "build" declarations that tell config where
241  * things are. These are not used by default.
242  *
243  * A previous version of this comment contained the following text:
244  *
245  *       Note that we do not have sufficient keywords to enforce any
246  *       order between elements of "topthings" without introducing
247  *       shift/reduce conflicts.  Instead, check order requirements in
248  *       the C code.
249  *
250  * As of March 2012 this comment makes no sense, as there are only two
251  * topthings and no reason for them to be forcibly ordered.
252  * Furthermore, the statement about conflicts is false.
253  */
254 
255 /* Complete configuration. */
256 configuration:
257 	topthings machine_spec definition_part configuration_part
258 ;
259 
260 /* Sequence of zero or more topthings. */
261 topthings:
262 	  /* empty */
263 	| topthings topthing
264 ;
265 
266 /* Directory specification. */
267 topthing:
268 	                  '\n'
269 	| SOURCE filename '\n'		{ if (!srcdir) srcdir = $2; }
270 	| BUILD  filename '\n'		{ if (!builddir) builddir = $2; }
271 ;
272 
273 /* "machine foo" from std.whatever */
274 machine_spec:
275 	  XMACHINE WORD '\n'			{ setmachine($2,NULL,NULL,0); }
276 	| XMACHINE WORD WORD '\n'		{ setmachine($2,$3,NULL,0); }
277 	| XMACHINE WORD WORD subarches '\n'	{ setmachine($2,$3,$4,0); }
278 	| IOCONF WORD '\n'			{ setmachine($2,NULL,NULL,1); }
279 	| error { stop("cannot proceed without machine or ioconf specifier"); }
280 ;
281 
282 /* One or more sub-arches. */
283 subarches:
284 	  WORD				{ $$ = new_n($1); }
285 	| subarches WORD		{ $$ = new_nx($2, $1); }
286 ;
287 
288 /************************************************************/
289 
290 /*
291  * The machine definitions grammar.
292  */
293 
294 /* Complete definition part: the contents of all files.* files. */
295 definition_part:
296 	definitions ENDDEFS		{ check_maxpart(); check_version(); }
297 ;
298 
299 /* Zero or more definitions. Trap errors. */
300 definitions:
301 	  /* empty */
302 	| definitions '\n'
303 	| definitions definition '\n'	{ wrap_continue(); }
304 	| definitions error '\n'	{ wrap_cleanup(); }
305 	| definitions ENDFILE		{ enddefs(); checkfiles(); }
306 ;
307 
308 /* A single definition. */
309 definition:
310 	  file
311 	| object
312 	| device_major			{ do_devsw = 1; }
313 	| prefix
314 	| DEVCLASS WORD			{ (void)defattr($2, NULL, NULL, 1); }
315 	| DEFFS deffses optdepend_list	{ deffilesystem($2, $3); }
316 	| DEFINE WORD interface_opt depend_list
317 					{ (void)defattr($2, $3, $4, 0); }
318 	| DEFOPT optfile_opt defopts optdepend_list
319 					{ defoption($2, $3, $4); }
320 	| DEFFLAG optfile_opt defopts optdepend_list
321 					{ defflag($2, $3, $4, 0); }
322 	| OBSOLETE DEFFLAG optfile_opt defopts
323 					{ defflag($3, $4, NULL, 1); }
324 	| DEFPARAM optfile_opt defopts optdepend_list
325 					{ defparam($2, $3, $4, 0); }
326 	| OBSOLETE DEFPARAM optfile_opt defopts
327 					{ defparam($3, $4, NULL, 1); }
328 	| DEVICE devbase interface_opt depend_list
329 					{ defdev($2, $3, $4, 0); }
330 	| ATTACH devbase AT atlist devattach_opt depend_list
331 					{ defdevattach($5, $2, $4, $6); }
332 	| MAXPARTITIONS NUMBER		{ maxpartitions = $2.val; }
333 	| MAXUSERS NUMBER NUMBER NUMBER
334 				    { setdefmaxusers($2.val, $3.val, $4.val); }
335 	| MAKEOPTIONS condmkopt_list
336 	/* interface_opt in DEFPSEUDO is for backwards compatibility */
337 	| DEFPSEUDO devbase interface_opt depend_list
338 					{ defdev($2, $3, $4, 1); }
339 	| DEFPSEUDODEV devbase interface_opt depend_list
340 					{ defdev($2, $3, $4, 2); }
341 	| MAJOR '{' majorlist '}'
342 	| VERSION NUMBER		{ setversion($2.val); }
343 ;
344 
345 /* source file: file foo/bar.c bar|baz needs-flag compile-with blah */
346 file:
347 	XFILE filename fopts fflags rule	{ addfile($2, $3, $4, $5); }
348 ;
349 
350 /* file options: optional expression of conditions */
351 fopts:
352 	  /* empty */			{ $$ = NULL; }
353 	| condexpr			{ $$ = $1; }
354 ;
355 
356 /* zero or more flags for a file */
357 fflags:
358 	  /* empty */			{ $$ = 0; }
359 	| fflags fflag			{ $$ = $1 | $2; }
360 ;
361 
362 /* one flag for a file */
363 fflag:
364 	  NEEDS_COUNT			{ $$ = FI_NEEDSCOUNT; }
365 	| NEEDS_FLAG			{ $$ = FI_NEEDSFLAG; }
366 ;
367 
368 /* extra compile directive for a source file */
369 rule:
370 	  /* empty */			{ $$ = NULL; }
371 	| COMPILE_WITH stringvalue	{ $$ = $2; }
372 ;
373 
374 /* object file: object zot.o foo|zot needs-flag */
375 object:
376 	XOBJECT filename fopts oflags	{ addobject($2, $3, $4); }
377 ;
378 
379 /* zero or more flags for an object file */
380 oflags:
381 	  /* empty */			{ $$ = 0; }
382 	| oflags oflag			{ $$ = $1 | $2; }
383 ;
384 
385 /* a single flag for an object file */
386 oflag:
387 	NEEDS_FLAG			{ $$ = OI_NEEDSFLAG; }
388 ;
389 
390 /* device major declaration */
391 device_major:
392 	DEVICE_MAJOR WORD device_major_char device_major_block fopts devnodes
393 					{ adddevm($2, $3, $4, $5, $6); }
394 ;
395 
396 /* char 55 */
397 device_major_char:
398 	  /* empty */			{ $$ = -1; }
399 	| CHAR NUMBER			{ $$ = $2.val; }
400 ;
401 
402 /* block 33 */
403 device_major_block:
404 	  /* empty */			{ $$ = -1; }
405 	| BLOCK NUMBER			{ $$ = $2.val; }
406 ;
407 
408 /* device node specification */
409 devnodes:
410 	  /* empty */			{ $$ = new_s("DEVNODE_DONTBOTHER"); }
411 	| devnodetype ',' devnodeflags	{ $$ = nvcat($1, $3); }
412 	| devnodetype			{ $$ = $1; }
413 ;
414 
415 /* device nodes without flags */
416 devnodetype:
417 	  SINGLE			{ $$ = new_s("DEVNODE_SINGLE"); }
418 	| VECTOR '=' devnode_dims  { $$ = nvcat(new_s("DEVNODE_VECTOR"), $3); }
419 ;
420 
421 /* dimensions (?) */
422 devnode_dims:
423 	  NUMBER			{ $$ = new_i($1.val); }
424 	| NUMBER ':' NUMBER		{
425 		struct nvlist *__nv1, *__nv2;
426 
427 		__nv1 = new_i($1.val);
428 		__nv2 = new_i($3.val);
429 		$$ = nvcat(__nv1, __nv2);
430 	  }
431 ;
432 
433 /* flags for device nodes */
434 devnodeflags:
435 	LINKZERO			{ $$ = new_s("DEVNODE_FLAG_LINKZERO");}
436 ;
437 
438 /* prefix delimiter */
439 prefix:
440 	  PREFIX filename		{ prefix_push($2); }
441 	| PREFIX			{ prefix_pop(); }
442 ;
443 
444 /* one or more file system names */
445 deffses:
446 	  deffs				{ $$ = new_n($1); }
447 	| deffses deffs			{ $$ = new_nx($2, $1); }
448 ;
449 
450 /* a single file system name */
451 deffs:
452 	WORD				{ $$ = $1; }
453 ;
454 
455 /* optional locator specification */
456 interface_opt:
457 	  /* empty */			{ $$ = NULL; }
458 	| '{' '}'			{ $$ = present_loclist(NULL); }
459 	| '{' loclist '}'		{ $$ = present_loclist($2); }
460 ;
461 
462 /*
463  * loclist order matters, must use right recursion
464  * XXX wot?
465  */
466 
467 /* list of locator definitions */
468 loclist:
469 	  locdef			{ $$ = $1; }
470 	| locdef ',' loclist		{ $$ = $1; app($1, $3); }
471 ;
472 
473 /*
474  * "[ WORD locdefault ]" syntax may be unnecessary...
475  */
476 
477 /* one locator definition */
478 locdef:
479 	  locname locdefault 		{ $$ = MK3(loc, $1, $2, 0); }
480 	| locname			{ $$ = MK3(loc, $1, NULL, 0); }
481 	| '[' locname locdefault ']'	{ $$ = MK3(loc, $2, $3, 1); }
482 	| locname '[' NUMBER ']'	{ $$ = locarray($1, $3.val, NULL, 0); }
483 	| locname '[' NUMBER ']' locdefaults
484 					{ $$ = locarray($1, $3.val, $5, 0); }
485 	| '[' locname '[' NUMBER ']' locdefaults ']'
486 					{ $$ = locarray($2, $4.val, $6, 1); }
487 ;
488 
489 /* locator name */
490 locname:
491 	  WORD				{ $$ = $1; }
492 	| QSTRING			{ $$ = $1; }
493 ;
494 
495 /* locator default value */
496 locdefault:
497 	'=' value			{ $$ = $2; }
498 ;
499 
500 /* multiple locator default values */
501 locdefaults:
502 	'=' '{' values '}'		{ $$ = $3; }
503 ;
504 
505 /* list of depends, may be empty */
506 depend_list:
507 	  /* empty */			{ $$ = NULL; }
508 	| ':' depends			{ $$ = $2; }
509 ;
510 
511 /* one or more depend items */
512 depends:
513 	  depend			{ $$ = MK2(attrlist, NULL, $1); }
514 	| depends ',' depend		{ $$ = MK2(attrlist, $1, $3); }
515 ;
516 
517 /* one depend item (which is an attribute) */
518 depend:
519 	WORD				{ $$ = getattr($1); }
520 ;
521 
522 /* list of option depends, may be empty */
523 optdepend_list:
524 	  /* empty */			{ $$ = NULL; }
525 	| ':' optdepends		{ $$ = $2; }
526 ;
527 
528 /* a list of option dependencies */
529 optdepends:
530 	  optdepend			{ $$ = new_n($1); }
531 	| optdepends ',' optdepend	{ $$ = new_nx($3, $1); }
532 ;
533 
534 /* one option depend, which is an option name */
535 optdepend:
536 	WORD				{ $$ = $1; }
537 ;
538 
539 
540 /* list of places to attach: attach blah at ... */
541 atlist:
542 	  atname			{ $$ = new_n($1); }
543 	| atlist ',' atname		{ $$ = new_nx($3, $1); }
544 ;
545 
546 /* a place to attach a device */
547 atname:
548 	  WORD				{ $$ = $1; }
549 	| ROOT				{ $$ = NULL; }
550 ;
551 
552 /* one or more defined options */
553 defopts:
554 	  defopt			{ $$ = $1; }
555 	| defopts defopt		{ $$ = defoptlist_append($2, $1); }
556 ;
557 
558 /* one defined option */
559 defopt:
560 	  WORD				{ $$ = MK3(defoptlist, $1, NULL, NULL); }
561 	| WORD '=' value		{ $$ = MK3(defoptlist, $1, $3, NULL); }
562 	| WORD COLONEQ value		{ $$ = MK3(defoptlist, $1, NULL, $3); }
563 	| WORD '=' value COLONEQ value	{ $$ = MK3(defoptlist, $1, $3, $5); }
564 ;
565 
566 /* list of conditional makeoptions */
567 condmkopt_list:
568 	  condmkoption
569 	| condmkopt_list ',' condmkoption
570 ;
571 
572 /* one conditional make option */
573 condmkoption:
574 	condexpr mkvarname PLUSEQ value	{ appendcondmkoption($1, $2, $4); }
575 ;
576 
577 /* device name */
578 devbase:
579 	WORD				{ $$ = getdevbase($1); }
580 ;
581 
582 /* optional attachment: with foo */
583 devattach_opt:
584 	  /* empty */			{ $$ = NULL; }
585 	| WITH WORD			{ $$ = getdevattach($2); }
586 ;
587 
588 /* list of major numbers */
589 /* XXX why is this right-recursive? */
590 majorlist:
591 	  majordef
592 	| majorlist ',' majordef
593 ;
594 
595 /* one major number */
596 majordef:
597 	devbase '=' NUMBER		{ setmajor($1, $3.val); }
598 ;
599 
600 /************************************************************/
601 
602 /*
603  * The configuration grammar.
604  */
605 
606 /* Complete configuration part: all std.* files plus selected config. */
607 configuration_part:
608 	config_items
609 ;
610 
611 /* Zero or more config items. Trap errors. */
612 config_items:
613 	  /* empty */
614 	| config_items '\n'
615 	| config_items config_item '\n'	{ wrap_continue(); }
616 	| config_items error '\n'	{ wrap_cleanup(); }
617 ;
618 
619 /* One config item. */
620 config_item:
621 	  definition
622 	| NO FILE_SYSTEM no_fs_list
623 	| FILE_SYSTEM fs_list
624 	| NO MAKEOPTIONS no_mkopt_list
625 	| MAKEOPTIONS mkopt_list
626 	| NO OPTIONS no_opt_list
627 	| OPTIONS opt_list
628 	| MAXUSERS NUMBER		{ setmaxusers($2.val); }
629 	| IDENT stringvalue		{ setident($2); }
630 	| CONFIG conf root_spec sysparam_list
631 					{ addconf(&conf); }
632 	| NO CONFIG WORD		{ delconf($3); }
633 	| NO PSEUDO_DEVICE WORD		{ delpseudo($3); }
634 	| PSEUDO_DEVICE WORD npseudo	{ addpseudo($2, $3); }
635 	| PSEUDO_ROOT device_instance	{ addpseudoroot($2); }
636 	| NO device_instance AT attachment
637 					{ deldevi($2, $4); }
638 	| NO DEVICE AT attachment	{ deldeva($4); }
639 	| NO device_instance		{ deldev($2); }
640 	| device_instance AT attachment locators device_flags
641 					{ adddev($1, $3, $4, $5); }
642 ;
643 
644 /* list of filesystems */
645 fs_list:
646 	  fsoption
647 	| fs_list ',' fsoption
648 ;
649 
650 /* one filesystem */
651 fsoption:
652 	WORD				{ addfsoption($1); }
653 ;
654 
655 /* list of filesystems that had NO in front */
656 no_fs_list:
657 	  no_fsoption
658 	| no_fs_list ',' no_fsoption
659 ;
660 
661 /* one filesystem that had NO in front */
662 no_fsoption:
663 	WORD				{ delfsoption($1); }
664 ;
665 
666 /* list of make options */
667 /* XXX why is this right-recursive? */
668 mkopt_list:
669 	  mkoption
670 	| mkopt_list ',' mkoption
671 ;
672 
673 /* one make option */
674 mkoption:
675 	  mkvarname '=' value		{ addmkoption($1, $3); }
676 	| mkvarname PLUSEQ value	{ appendmkoption($1, $3); }
677 ;
678 
679 /* list of make options that had NO in front */
680 no_mkopt_list:
681 	  no_mkoption
682 	| no_mkopt_list ',' no_mkoption
683 ;
684 
685 /* one make option that had NO in front */
686 /* XXX shouldn't this be mkvarname rather than WORD? */
687 no_mkoption:
688 	WORD				{ delmkoption($1); }
689 ;
690 
691 /* list of options */
692 opt_list:
693 	  option
694 	| opt_list ',' option
695 ;
696 
697 /* one option */
698 option:
699 	  WORD				{ addoption($1, NULL); }
700 	| WORD '=' value		{ addoption($1, $3); }
701 ;
702 
703 /* list of options that had NO in front */
704 no_opt_list:
705 	  no_option
706 	| no_opt_list ',' no_option
707 ;
708 
709 /* one option that had NO in front */
710 no_option:
711 	WORD				{ deloption($1); }
712 ;
713 
714 /* the name in "config name root on ..." */
715 conf:
716 	WORD				{
717 		conf.cf_name = $1;
718 		conf.cf_lineno = currentline();
719 		conf.cf_fstype = NULL;
720 		conf.cf_root = NULL;
721 		conf.cf_dump = NULL;
722 	}
723 ;
724 
725 /* root fs specification */
726 root_spec:
727 	  ROOT on_opt dev_spec		{ setconf(&conf.cf_root, "root", $3); }
728 	| ROOT on_opt dev_spec fs_spec	{ setconf(&conf.cf_root, "root", $3); }
729 ;
730 
731 /* device for root fs or dump */
732 dev_spec:
733 	  '?'				{ $$ = new_si(intern("?"), NODEV); }
734 	| WORD				{ $$ = new_si($1, NODEV); }
735 	| major_minor			{ $$ = new_si(NULL, $1); }
736 ;
737 
738 /* major and minor device number */
739 major_minor:
740 	MAJOR NUMBER MINOR NUMBER	{ $$ = makedev($2.val, $4.val); }
741 ;
742 
743 /* filesystem type for root fs specification */
744 fs_spec:
745 	  TYPE '?'		   { setfstype(&conf.cf_fstype, intern("?")); }
746 	| TYPE WORD			{ setfstype(&conf.cf_fstype, $2); }
747 ;
748 
749 /* zero or more additional system parameters */
750 sysparam_list:
751 	  /* empty */
752 	| sysparam_list sysparam
753 ;
754 
755 /* one additional system parameter (there's only one: dumps) */
756 sysparam:
757 	DUMPS on_opt dev_spec	       { setconf(&conf.cf_dump, "dumps", $3); }
758 ;
759 
760 /* number of pseudo devices to configure (which is optional) */
761 npseudo:
762 	  /* empty */			{ $$ = 1; }
763 	| NUMBER			{ $$ = $1.val; }
764 ;
765 
766 /* name of a device to configure */
767 device_instance:
768 	  WORD				{ $$ = $1; }
769 	| WORD '*'			{ $$ = starref($1); }
770 ;
771 
772 /* name of a device to configure an attachment to */
773 attachment:
774 	  ROOT				{ $$ = NULL; }
775 	| WORD				{ $$ = $1; }
776 	| WORD '?'			{ $$ = wildref($1); }
777 ;
778 
779 /* zero or more locators */
780 locators:
781 	  /* empty */			{ $$ = NULL; }
782 	| locators locator		{ $$ = $2; app($2, $1); }
783 ;
784 
785 /* one locator */
786 locator:
787 	  WORD '?'			{ $$ = MK3(loc, $1, NULL, 0); }
788 	| WORD values			{ $$ = namelocvals($1, $2); }
789 ;
790 
791 /* optional device flags */
792 device_flags:
793 	  /* empty */			{ $$ = 0; }
794 	| FLAGS NUMBER			{ $$ = $2.val; }
795 ;
796 
797 /************************************************************/
798 
799 /*
800  * conditions
801  */
802 
803 
804 /*
805  * order of options is important, must use right recursion
806  *
807  * dholland 20120310: wut?
808  */
809 
810 /* expression of conditions */
811 condexpr:
812 	cond_or_expr
813 ;
814 
815 cond_or_expr:
816 	  cond_and_expr
817 	| cond_or_expr '|' cond_and_expr	{ $$ = MKF2(cx, or, $1, $3); }
818 ;
819 
820 cond_and_expr:
821 	  cond_prefix_expr
822 	| cond_and_expr '&' cond_prefix_expr	{ $$ = MKF2(cx, and, $1, $3); }
823 ;
824 
825 cond_prefix_expr:
826 	  cond_base_expr
827 /* XXX notyet - need to strengthen downstream first */
828 /*	| '!' cond_prefix_expr			{ $$ = MKF1(cx, not, $2); } */
829 ;
830 
831 cond_base_expr:
832 	  condatom			{ $$ = $1; }
833 	| '!' condatom			{ $$ = MKF1(cx, not, $2); }
834 	| '(' condexpr ')'		{ $$ = $2; }
835 ;
836 
837 /* basic element of config element expression: a config element */
838 condatom:
839 	WORD				{ $$ = MKF1(cx, atom, $1); }
840 ;
841 
842 /************************************************************/
843 
844 /*
845  * Various nonterminals shared between the grammars.
846  */
847 
848 /* variable name for make option */
849 mkvarname:
850 	  QSTRING			{ $$ = $1; }
851 	| WORD				{ $$ = $1; }
852 ;
853 
854 /* optional file for an option */
855 optfile_opt:
856 	  /* empty */			{ $$ = NULL; }
857 	| filename			{ $$ = $1; }
858 ;
859 
860 /* filename. */
861 filename:
862 	  QSTRING			{ $$ = $1; }
863 	| PATHNAME			{ $$ = $1; }
864 ;
865 
866 /* constant value */
867 value:
868 	  QSTRING			{ $$ = $1; }
869 	| WORD				{ $$ = $1; }
870 	| EMPTYSTRING			{ $$ = $1; }
871 	| signed_number			{
872 		char bf[40];
873 
874 		(void)snprintf(bf, sizeof(bf), FORMAT($1), (long long)$1.val);
875 		$$ = intern(bf);
876 	  }
877 ;
878 
879 /* constant value that is a string */
880 stringvalue:
881 	  QSTRING			{ $$ = $1; }
882 	| WORD				{ $$ = $1; }
883 ;
884 
885 /* comma-separated list of values */
886 /* XXX why right-recursive? */
887 values:
888 	  value				{ $$ = MKF2(loc, val, $1, NULL); }
889 	| value ',' values		{ $$ = MKF2(loc, val, $1, $3); }
890 ;
891 
892 /* possibly negative number */
893 signed_number:
894 	  NUMBER			{ $$ = $1; }
895 	| '-' NUMBER			{ $$.fmt = $2.fmt; $$.val = -$2.val; }
896 ;
897 
898 /* optional ON keyword */
899 on_opt:
900 	  /* empty */
901 	| ON
902 ;
903 
904 %%
905 
906 void
907 yyerror(const char *s)
908 {
909 
910 	cfgerror("%s", s);
911 }
912 
913 /************************************************************/
914 
915 /*
916  * Wrap allocations that live on the parser stack so that we can free
917  * them again on error instead of leaking.
918  */
919 
920 #define MAX_WRAP 1000
921 
922 struct wrap_entry {
923 	void *ptr;
924 	unsigned typecode;
925 };
926 
927 static struct wrap_entry wrapstack[MAX_WRAP];
928 static unsigned wrap_depth;
929 
930 /*
931  * Remember pointer PTR with type-code CODE.
932  */
933 static void
934 wrap_alloc(void *ptr, unsigned code)
935 {
936 	unsigned pos;
937 
938 	if (wrap_depth >= MAX_WRAP) {
939 		panic("allocation wrapper stack overflow");
940 	}
941 	pos = wrap_depth++;
942 	wrapstack[pos].ptr = ptr;
943 	wrapstack[pos].typecode = code;
944 }
945 
946 /*
947  * We succeeded; commit to keeping everything that's been allocated so
948  * far and clear the stack.
949  */
950 static void
951 wrap_continue(void)
952 {
953 	wrap_depth = 0;
954 }
955 
956 /*
957  * We failed; destroy all the objects allocated.
958  */
959 static void
960 wrap_cleanup(void)
961 {
962 	unsigned i;
963 
964 	/*
965 	 * Destroy each item. Note that because everything allocated
966 	 * is entered on the list separately, lists and trees need to
967 	 * have their links blanked before being destroyed. Also note
968 	 * that strings are interned elsewhere and not handled by this
969 	 * mechanism.
970 	 */
971 
972 	for (i=0; i<wrap_depth; i++) {
973 		switch (wrapstack[i].typecode) {
974 		    case WRAP_CODE_nvlist:
975 			nvfree(wrapstack[i].ptr);
976 			break;
977 		    case WRAP_CODE_defoptlist:
978 			{
979 				struct defoptlist *dl = wrapstack[i].ptr;
980 
981 				dl->dl_next = NULL;
982 				defoptlist_destroy(dl);
983 			}
984 			break;
985 		    case WRAP_CODE_loclist:
986 			{
987 				struct loclist *ll = wrapstack[i].ptr;
988 
989 				ll->ll_next = NULL;
990 				loclist_destroy(ll);
991 			}
992 			break;
993 		    case WRAP_CODE_attrlist:
994 			{
995 				struct attrlist *al = wrapstack[i].ptr;
996 
997 				al->al_next = NULL;
998 				al->al_this = NULL;
999 				attrlist_destroy(al);
1000 			}
1001 			break;
1002 		    case WRAP_CODE_condexpr:
1003 			{
1004 				struct condexpr *cx = wrapstack[i].ptr;
1005 
1006 				cx->cx_type = CX_ATOM;
1007 				cx->cx_atom = NULL;
1008 				condexpr_destroy(cx);
1009 			}
1010 			break;
1011 		    default:
1012 			panic("invalid code %u on allocation wrapper stack",
1013 			      wrapstack[i].typecode);
1014 		}
1015 	}
1016 
1017 	wrap_depth = 0;
1018 }
1019 
1020 /*
1021  * Instantiate the wrapper functions.
1022  *
1023  * Each one calls wrap_alloc to save the pointer and then returns the
1024  * pointer again; these need to be generated with the preprocessor in
1025  * order to be typesafe.
1026  */
1027 #define DEF_ALLOCWRAP(t) \
1028 	static struct t *				\
1029 	wrap_mk_##t(struct t *arg)			\
1030 	{						\
1031 		wrap_alloc(arg, WRAP_CODE_##t);		\
1032 		return arg;				\
1033 	}
1034 
1035 DEF_ALLOCWRAP(nvlist);
1036 DEF_ALLOCWRAP(defoptlist);
1037 DEF_ALLOCWRAP(loclist);
1038 DEF_ALLOCWRAP(attrlist);
1039 DEF_ALLOCWRAP(condexpr);
1040 
1041 /************************************************************/
1042 
1043 /*
1044  * Data constructors
1045  *
1046  * (These are *beneath* the allocation wrappers.)
1047  */
1048 
1049 static struct defoptlist *
1050 mk_defoptlist(const char *name, const char *val, const char *lintval)
1051 {
1052 	return defoptlist_create(name, val, lintval);
1053 }
1054 
1055 static struct loclist *
1056 mk_loc(const char *name, const char *str, long long num)
1057 {
1058 	return loclist_create(name, str, num);
1059 }
1060 
1061 static struct loclist *
1062 mk_loc_val(const char *str, struct loclist *next)
1063 {
1064 	struct loclist *ll;
1065 
1066 	ll = mk_loc(NULL, str, 0);
1067 	ll->ll_next = next;
1068 	return ll;
1069 }
1070 
1071 static struct attrlist *
1072 mk_attrlist(struct attrlist *next, struct attr *a)
1073 {
1074 	return attrlist_cons(next, a);
1075 }
1076 
1077 static struct condexpr *
1078 mk_cx_atom(const char *s)
1079 {
1080 	struct condexpr *cx;
1081 
1082 	cx = condexpr_create(CX_ATOM);
1083 	cx->cx_atom = s;
1084 	return cx;
1085 }
1086 
1087 static struct condexpr *
1088 mk_cx_not(struct condexpr *sub)
1089 {
1090 	struct condexpr *cx;
1091 
1092 	cx = condexpr_create(CX_NOT);
1093 	cx->cx_not = sub;
1094 	return cx;
1095 }
1096 
1097 static struct condexpr *
1098 mk_cx_and(struct condexpr *left, struct condexpr *right)
1099 {
1100 	struct condexpr *cx;
1101 
1102 	cx = condexpr_create(CX_AND);
1103 	cx->cx_and.left = left;
1104 	cx->cx_and.right = right;
1105 	return cx;
1106 }
1107 
1108 static struct condexpr *
1109 mk_cx_or(struct condexpr *left, struct condexpr *right)
1110 {
1111 	struct condexpr *cx;
1112 
1113 	cx = condexpr_create(CX_OR);
1114 	cx->cx_or.left = left;
1115 	cx->cx_or.right = right;
1116 	return cx;
1117 }
1118 
1119 /************************************************************/
1120 
1121 static void
1122 setmachine(const char *mch, const char *mcharch, struct nvlist *mchsubarches,
1123 	int isioconf)
1124 {
1125 	char buf[MAXPATHLEN];
1126 	struct nvlist *nv;
1127 
1128 	if (isioconf) {
1129 		if (include(_PATH_DEVNULL, ENDDEFS, 0, 0) != 0)
1130 			exit(1);
1131 		ioconfname = mch;
1132 		return;
1133 	}
1134 
1135 	machine = mch;
1136 	machinearch = mcharch;
1137 	machinesubarches = mchsubarches;
1138 
1139 	/*
1140 	 * Define attributes for all the given names
1141 	 */
1142 	if (defattr(machine, NULL, NULL, 0) != 0 ||
1143 	    (machinearch != NULL &&
1144 	     defattr(machinearch, NULL, NULL, 0) != 0))
1145 		exit(1);
1146 	for (nv = machinesubarches; nv != NULL; nv = nv->nv_next) {
1147 		if (defattr(nv->nv_name, NULL, NULL, 0) != 0)
1148 			exit(1);
1149 	}
1150 
1151 	/*
1152 	 * Set up the file inclusion stack.  This empty include tells
1153 	 * the parser there are no more device definitions coming.
1154 	 */
1155 	if (include(_PATH_DEVNULL, ENDDEFS, 0, 0) != 0)
1156 		exit(1);
1157 
1158 	/* Include arch/${MACHINE}/conf/files.${MACHINE} */
1159 	(void)snprintf(buf, sizeof(buf), "arch/%s/conf/files.%s",
1160 	    machine, machine);
1161 	if (include(buf, ENDFILE, 0, 0) != 0)
1162 		exit(1);
1163 
1164 	/* Include any arch/${MACHINE_SUBARCH}/conf/files.${MACHINE_SUBARCH} */
1165 	for (nv = machinesubarches; nv != NULL; nv = nv->nv_next) {
1166 		(void)snprintf(buf, sizeof(buf), "arch/%s/conf/files.%s",
1167 		    nv->nv_name, nv->nv_name);
1168 		if (include(buf, ENDFILE, 0, 0) != 0)
1169 			exit(1);
1170 	}
1171 
1172 	/* Include any arch/${MACHINE_ARCH}/conf/files.${MACHINE_ARCH} */
1173 	if (machinearch != NULL)
1174 		(void)snprintf(buf, sizeof(buf), "arch/%s/conf/files.%s",
1175 		    machinearch, machinearch);
1176 	else
1177 		strlcpy(buf, _PATH_DEVNULL, sizeof(buf));
1178 	if (include(buf, ENDFILE, 0, 0) != 0)
1179 		exit(1);
1180 
1181 	/*
1182 	 * Include the global conf/files.  As the last thing
1183 	 * pushed on the stack, it will be processed first.
1184 	 */
1185 	if (include("conf/files", ENDFILE, 0, 0) != 0)
1186 		exit(1);
1187 
1188 	oktopackage = 1;
1189 }
1190 
1191 static void
1192 check_maxpart(void)
1193 {
1194 
1195 	if (maxpartitions <= 0 && ioconfname == NULL) {
1196 		stop("cannot proceed without maxpartitions specifier");
1197 	}
1198 }
1199 
1200 static void
1201 check_version(void)
1202 {
1203 	/*
1204 	 * In essence, version is 0 and is not supported anymore
1205 	 */
1206 	if (version < CONFIG_MINVERSION)
1207 		stop("your sources are out of date -- please update.");
1208 }
1209 
1210 /*
1211  * Prepend a blank entry to the locator definitions so the code in
1212  * sem.c can distinguish "empty locator list" from "no locator list".
1213  * XXX gross.
1214  */
1215 static struct loclist *
1216 present_loclist(struct loclist *ll)
1217 {
1218 	struct loclist *ret;
1219 
1220 	ret = MK3(loc, "", NULL, 0);
1221 	ret->ll_next = ll;
1222 	return ret;
1223 }
1224 
1225 static void
1226 app(struct loclist *p, struct loclist *q)
1227 {
1228 	while (p->ll_next)
1229 		p = p->ll_next;
1230 	p->ll_next = q;
1231 }
1232 
1233 static struct loclist *
1234 locarray(const char *name, int count, struct loclist *adefs, int opt)
1235 {
1236 	struct loclist *defs = adefs;
1237 	struct loclist **p;
1238 	char buf[200];
1239 	int i;
1240 
1241 	if (count <= 0) {
1242 		fprintf(stderr, "config: array with <= 0 size: %s\n", name);
1243 		exit(1);
1244 	}
1245 	p = &defs;
1246 	for(i = 0; i < count; i++) {
1247 		if (*p == NULL)
1248 			*p = MK3(loc, NULL, "0", 0);
1249 		snprintf(buf, sizeof(buf), "%s%c%d", name, ARRCHR, i);
1250 		(*p)->ll_name = i == 0 ? name : intern(buf);
1251 		(*p)->ll_num = i > 0 || opt;
1252 		p = &(*p)->ll_next;
1253 	}
1254 	*p = 0;
1255 	return defs;
1256 }
1257 
1258 
1259 static struct loclist *
1260 namelocvals(const char *name, struct loclist *vals)
1261 {
1262 	struct loclist *p;
1263 	char buf[200];
1264 	int i;
1265 
1266 	for (i = 0, p = vals; p; i++, p = p->ll_next) {
1267 		snprintf(buf, sizeof(buf), "%s%c%d", name, ARRCHR, i);
1268 		p->ll_name = i == 0 ? name : intern(buf);
1269 	}
1270 	return vals;
1271 }
1272 
1273