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