gram.y revision 1.41 1 %{
2 /* $NetBSD: gram.y,v 1.41 2014/10/09 09:39:24 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/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 selection 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 selection_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 define_file
311 | define_object
312 | define_device_major
313 | define_prefix
314 | define_devclass
315 | define_filesystems
316 | define_attribute
317 | define_option
318 | define_flag
319 | define_obsolete_flag
320 | define_param
321 | define_obsolete_param
322 | define_device
323 | define_device_attachment
324 | define_maxpartitions
325 | define_maxusers
326 | define_makeoptions
327 | define_pseudo
328 | define_pseudodev
329 | define_major
330 | define_version
331 ;
332
333 /* source file: file foo/bar.c bar|baz needs-flag compile-with blah */
334 define_file:
335 XFILE filename fopts fflags rule { addfile($2, $3, $4, $5); }
336 ;
337
338 /* object file: object zot.o foo|zot needs-flag */
339 define_object:
340 XOBJECT filename fopts oflags { addobject($2, $3, $4); }
341 ;
342
343 /* device major declaration */
344 define_device_major:
345 DEVICE_MAJOR WORD device_major_char device_major_block fopts devnodes
346 {
347 adddevm($2, $3, $4, $5, $6);
348 do_devsw = 1;
349 }
350 ;
351
352 /* prefix delimiter */
353 define_prefix:
354 PREFIX filename { prefix_push($2); }
355 | PREFIX { prefix_pop(); }
356 ;
357
358 define_devclass:
359 DEVCLASS WORD { (void)defattr($2, NULL, NULL, 1); }
360 ;
361
362 define_filesystems:
363 DEFFS deffses optdepend_list { deffilesystem($2, $3); }
364 ;
365
366 define_attribute:
367 DEFINE WORD interface_opt depend_list
368 { (void)defattr($2, $3, $4, 0); }
369 ;
370
371 define_option:
372 DEFOPT optfile_opt defopts optdepend_list
373 { defoption($2, $3, $4); }
374 ;
375
376 define_flag:
377 DEFFLAG optfile_opt defopts optdepend_list
378 { defflag($2, $3, $4, 0); }
379 ;
380
381 define_obsolete_flag:
382 OBSOLETE DEFFLAG optfile_opt defopts
383 { defflag($3, $4, NULL, 1); }
384 ;
385
386 define_param:
387 DEFPARAM optfile_opt defopts optdepend_list
388 { defparam($2, $3, $4, 0); }
389 ;
390
391 define_obsolete_param:
392 OBSOLETE DEFPARAM optfile_opt defopts
393 { defparam($3, $4, NULL, 1); }
394 ;
395
396 define_device:
397 DEVICE devbase interface_opt depend_list
398 { defdev($2, $3, $4, 0); }
399 ;
400
401 define_device_attachment:
402 ATTACH devbase AT atlist devattach_opt depend_list
403 { defdevattach($5, $2, $4, $6); }
404 ;
405
406 define_maxpartitions:
407 MAXPARTITIONS NUMBER { maxpartitions = $2.val; }
408 ;
409
410 define_maxusers:
411 MAXUSERS NUMBER NUMBER NUMBER
412 { setdefmaxusers($2.val, $3.val, $4.val); }
413 ;
414
415 define_makeoptions:
416 MAKEOPTIONS condmkopt_list
417 ;
418
419 define_pseudo:
420 /* interface_opt in DEFPSEUDO is for backwards compatibility */
421 DEFPSEUDO devbase interface_opt depend_list
422 { defdev($2, $3, $4, 1); }
423 ;
424
425 define_pseudodev:
426 DEFPSEUDODEV devbase interface_opt depend_list
427 { defdev($2, $3, $4, 2); }
428 ;
429
430 define_major:
431 MAJOR '{' majorlist '}'
432 ;
433
434 define_version:
435 VERSION NUMBER { setversion($2.val); }
436 ;
437
438 /* file options: optional expression of conditions */
439 fopts:
440 /* empty */ { $$ = NULL; }
441 | condexpr { $$ = $1; }
442 ;
443
444 /* zero or more flags for a file */
445 fflags:
446 /* empty */ { $$ = 0; }
447 | fflags fflag { $$ = $1 | $2; }
448 ;
449
450 /* one flag for a file */
451 fflag:
452 NEEDS_COUNT { $$ = FI_NEEDSCOUNT; }
453 | NEEDS_FLAG { $$ = FI_NEEDSFLAG; }
454 ;
455
456 /* extra compile directive for a source file */
457 rule:
458 /* empty */ { $$ = NULL; }
459 | COMPILE_WITH stringvalue { $$ = $2; }
460 ;
461
462 /* zero or more flags for an object file */
463 oflags:
464 /* empty */ { $$ = 0; }
465 | oflags oflag { $$ = $1 | $2; }
466 ;
467
468 /* a single flag for an object file */
469 oflag:
470 NEEDS_FLAG { $$ = OI_NEEDSFLAG; }
471 ;
472
473 /* char 55 */
474 device_major_char:
475 /* empty */ { $$ = -1; }
476 | CHAR NUMBER { $$ = $2.val; }
477 ;
478
479 /* block 33 */
480 device_major_block:
481 /* empty */ { $$ = -1; }
482 | BLOCK NUMBER { $$ = $2.val; }
483 ;
484
485 /* device node specification */
486 devnodes:
487 /* empty */ { $$ = new_s("DEVNODE_DONTBOTHER"); }
488 | devnodetype ',' devnodeflags { $$ = nvcat($1, $3); }
489 | devnodetype { $$ = $1; }
490 ;
491
492 /* device nodes without flags */
493 devnodetype:
494 SINGLE { $$ = new_s("DEVNODE_SINGLE"); }
495 | VECTOR '=' devnode_dims { $$ = nvcat(new_s("DEVNODE_VECTOR"), $3); }
496 ;
497
498 /* dimensions (?) */
499 devnode_dims:
500 NUMBER { $$ = new_i($1.val); }
501 | NUMBER ':' NUMBER {
502 struct nvlist *__nv1, *__nv2;
503
504 __nv1 = new_i($1.val);
505 __nv2 = new_i($3.val);
506 $$ = nvcat(__nv1, __nv2);
507 }
508 ;
509
510 /* flags for device nodes */
511 devnodeflags:
512 LINKZERO { $$ = new_s("DEVNODE_FLAG_LINKZERO");}
513 ;
514
515 /* one or more file system names */
516 deffses:
517 deffs { $$ = new_n($1); }
518 | deffses deffs { $$ = new_nx($2, $1); }
519 ;
520
521 /* a single file system name */
522 deffs:
523 WORD { $$ = $1; }
524 ;
525
526 /* optional locator specification */
527 interface_opt:
528 /* empty */ { $$ = NULL; }
529 | '{' '}' { $$ = present_loclist(NULL); }
530 | '{' loclist '}' { $$ = present_loclist($2); }
531 ;
532
533 /*
534 * loclist order matters, must use right recursion
535 * XXX wot?
536 */
537
538 /* list of locator definitions */
539 loclist:
540 locdef { $$ = $1; }
541 | locdef ',' loclist { $$ = $1; app($1, $3); }
542 ;
543
544 /*
545 * "[ WORD locdefault ]" syntax may be unnecessary...
546 */
547
548 /* one locator definition */
549 locdef:
550 locname locdefault { $$ = MK3(loc, $1, $2, 0); }
551 | locname { $$ = MK3(loc, $1, NULL, 0); }
552 | '[' locname locdefault ']' { $$ = MK3(loc, $2, $3, 1); }
553 | locname '[' NUMBER ']' { $$ = locarray($1, $3.val, NULL, 0); }
554 | locname '[' NUMBER ']' locdefaults
555 { $$ = locarray($1, $3.val, $5, 0); }
556 | '[' locname '[' NUMBER ']' locdefaults ']'
557 { $$ = locarray($2, $4.val, $6, 1); }
558 ;
559
560 /* locator name */
561 locname:
562 WORD { $$ = $1; }
563 | QSTRING { $$ = $1; }
564 ;
565
566 /* locator default value */
567 locdefault:
568 '=' value { $$ = $2; }
569 ;
570
571 /* multiple locator default values */
572 locdefaults:
573 '=' '{' values '}' { $$ = $3; }
574 ;
575
576 /* list of depends, may be empty */
577 depend_list:
578 /* empty */ { $$ = NULL; }
579 | ':' depends { $$ = $2; }
580 ;
581
582 /* one or more depend items */
583 depends:
584 depend { $$ = MK2(attrlist, NULL, $1); }
585 | depends ',' depend { $$ = MK2(attrlist, $1, $3); }
586 ;
587
588 /* one depend item (which is an attribute) */
589 depend:
590 WORD { $$ = getattr($1); }
591 ;
592
593 /* list of option depends, may be empty */
594 optdepend_list:
595 /* empty */ { $$ = NULL; }
596 | ':' optdepends { $$ = $2; }
597 ;
598
599 /* a list of option dependencies */
600 optdepends:
601 optdepend { $$ = new_n($1); }
602 | optdepends ',' optdepend { $$ = new_nx($3, $1); }
603 ;
604
605 /* one option depend, which is an option name */
606 optdepend:
607 WORD { $$ = $1; }
608 ;
609
610
611 /* list of places to attach: attach blah at ... */
612 atlist:
613 atname { $$ = new_n($1); }
614 | atlist ',' atname { $$ = new_nx($3, $1); }
615 ;
616
617 /* a place to attach a device */
618 atname:
619 WORD { $$ = $1; }
620 | ROOT { $$ = NULL; }
621 ;
622
623 /* one or more defined options */
624 defopts:
625 defopt { $$ = $1; }
626 | defopts defopt { $$ = defoptlist_append($2, $1); }
627 ;
628
629 /* one defined option */
630 defopt:
631 WORD { $$ = MK3(defoptlist, $1, NULL, NULL); }
632 | WORD '=' value { $$ = MK3(defoptlist, $1, $3, NULL); }
633 | WORD COLONEQ value { $$ = MK3(defoptlist, $1, NULL, $3); }
634 | WORD '=' value COLONEQ value { $$ = MK3(defoptlist, $1, $3, $5); }
635 ;
636
637 /* list of conditional makeoptions */
638 condmkopt_list:
639 condmkoption
640 | condmkopt_list ',' condmkoption
641 ;
642
643 /* one conditional make option */
644 condmkoption:
645 condexpr mkvarname PLUSEQ value { appendcondmkoption($1, $2, $4); }
646 ;
647
648 /* device name */
649 devbase:
650 WORD { $$ = getdevbase($1); }
651 ;
652
653 /* optional attachment: with foo */
654 devattach_opt:
655 /* empty */ { $$ = NULL; }
656 | WITH WORD { $$ = getdevattach($2); }
657 ;
658
659 /* list of major numbers */
660 /* XXX why is this right-recursive? */
661 majorlist:
662 majordef
663 | majorlist ',' majordef
664 ;
665
666 /* one major number */
667 majordef:
668 devbase '=' NUMBER { setmajor($1, $3.val); }
669 ;
670
671 /************************************************************/
672
673 /*
674 * The selection grammar.
675 */
676
677 /* Complete selection part: all std.* files plus selected config. */
678 selection_part:
679 selections
680 ;
681
682 /* Zero or more config items. Trap errors. */
683 selections:
684 /* empty */
685 | selections '\n'
686 | selections selection '\n' { wrap_continue(); }
687 | selections error '\n' { wrap_cleanup(); }
688 ;
689
690 /* One config item. */
691 selection:
692 definition
693 | select_no_filesystems
694 | select_filesystems
695 | select_no_makeoptions
696 | select_makeoptions
697 | select_no_options
698 | select_options
699 | select_maxusers
700 | select_ident
701 | select_no_ident
702 | select_config
703 | select_no_config
704 | select_no_pseudodev
705 | select_pseudodev
706 | select_pseudoroot
707 | select_no_device_instance_attachment
708 | select_no_device_attachment
709 | select_no_device_instance
710 | select_device_instance
711 ;
712
713 select_no_filesystems:
714 NO FILE_SYSTEM no_fs_list
715 ;
716
717 select_filesystems:
718 FILE_SYSTEM fs_list
719 ;
720
721 select_no_makeoptions:
722 NO MAKEOPTIONS no_mkopt_list
723 ;
724
725 select_makeoptions:
726 MAKEOPTIONS mkopt_list
727 ;
728
729 select_no_options:
730 NO OPTIONS no_opt_list
731 ;
732
733 select_options:
734 OPTIONS opt_list
735 ;
736
737 select_maxusers:
738 MAXUSERS NUMBER { setmaxusers($2.val); }
739 ;
740
741 select_ident:
742 IDENT stringvalue { setident($2); }
743 ;
744
745 select_no_ident:
746 NO IDENT { setident(NULL); }
747 ;
748
749 select_config:
750 CONFIG conf root_spec sysparam_list
751 { addconf(&conf); }
752 ;
753
754 select_no_config:
755 NO CONFIG WORD { delconf($3); }
756 ;
757
758 select_no_pseudodev:
759 NO PSEUDO_DEVICE WORD { delpseudo($3); }
760 ;
761
762 select_pseudodev:
763 PSEUDO_DEVICE WORD npseudo { addpseudo($2, $3); }
764 ;
765
766 select_pseudoroot:
767 PSEUDO_ROOT device_instance { addpseudoroot($2); }
768 ;
769
770 select_no_device_instance_attachment:
771 NO device_instance AT attachment
772 { deldevi($2, $4); }
773 ;
774
775 select_no_device_attachment:
776 NO DEVICE AT attachment { deldeva($4); }
777 ;
778
779 select_no_device_instance:
780 NO device_instance { deldev($2); }
781 ;
782
783 select_device_instance:
784 device_instance AT attachment locators device_flags
785 { adddev($1, $3, $4, $5); }
786 ;
787
788 /* list of filesystems */
789 fs_list:
790 fsoption
791 | fs_list ',' fsoption
792 ;
793
794 /* one filesystem */
795 fsoption:
796 WORD { addfsoption($1); }
797 ;
798
799 /* list of filesystems that had NO in front */
800 no_fs_list:
801 no_fsoption
802 | no_fs_list ',' no_fsoption
803 ;
804
805 /* one filesystem that had NO in front */
806 no_fsoption:
807 WORD { delfsoption($1); }
808 ;
809
810 /* list of make options */
811 /* XXX why is this right-recursive? */
812 mkopt_list:
813 mkoption
814 | mkopt_list ',' mkoption
815 ;
816
817 /* one make option */
818 mkoption:
819 mkvarname '=' value { addmkoption($1, $3); }
820 | mkvarname PLUSEQ value { appendmkoption($1, $3); }
821 ;
822
823 /* list of make options that had NO in front */
824 no_mkopt_list:
825 no_mkoption
826 | no_mkopt_list ',' no_mkoption
827 ;
828
829 /* one make option that had NO in front */
830 /* XXX shouldn't this be mkvarname rather than WORD? */
831 no_mkoption:
832 WORD { delmkoption($1); }
833 ;
834
835 /* list of options */
836 opt_list:
837 option
838 | opt_list ',' option
839 ;
840
841 /* one option */
842 option:
843 WORD { addoption($1, NULL); }
844 | WORD '=' value { addoption($1, $3); }
845 ;
846
847 /* list of options that had NO in front */
848 no_opt_list:
849 no_option
850 | no_opt_list ',' no_option
851 ;
852
853 /* one option that had NO in front */
854 no_option:
855 WORD { deloption($1); }
856 ;
857
858 /* the name in "config name root on ..." */
859 conf:
860 WORD {
861 conf.cf_name = $1;
862 conf.cf_lineno = currentline();
863 conf.cf_fstype = NULL;
864 conf.cf_root = NULL;
865 conf.cf_dump = NULL;
866 }
867 ;
868
869 /* root fs specification */
870 root_spec:
871 ROOT on_opt dev_spec { setconf(&conf.cf_root, "root", $3); }
872 | ROOT on_opt dev_spec fs_spec { setconf(&conf.cf_root, "root", $3); }
873 ;
874
875 /* device for root fs or dump */
876 dev_spec:
877 '?' { $$ = new_si(intern("?"), NODEV); }
878 | WORD { $$ = new_si($1, NODEV); }
879 | major_minor { $$ = new_si(NULL, $1); }
880 ;
881
882 /* major and minor device number */
883 major_minor:
884 MAJOR NUMBER MINOR NUMBER { $$ = makedev($2.val, $4.val); }
885 ;
886
887 /* filesystem type for root fs specification */
888 fs_spec:
889 TYPE '?' { setfstype(&conf.cf_fstype, intern("?")); }
890 | TYPE WORD { setfstype(&conf.cf_fstype, $2); }
891 ;
892
893 /* zero or more additional system parameters */
894 sysparam_list:
895 /* empty */
896 | sysparam_list sysparam
897 ;
898
899 /* one additional system parameter (there's only one: dumps) */
900 sysparam:
901 DUMPS on_opt dev_spec { setconf(&conf.cf_dump, "dumps", $3); }
902 ;
903
904 /* number of pseudo devices to configure (which is optional) */
905 npseudo:
906 /* empty */ { $$ = 1; }
907 | NUMBER { $$ = $1.val; }
908 ;
909
910 /* name of a device to configure */
911 device_instance:
912 WORD { $$ = $1; }
913 | WORD '*' { $$ = starref($1); }
914 ;
915
916 /* name of a device to configure an attachment to */
917 attachment:
918 ROOT { $$ = NULL; }
919 | WORD { $$ = $1; }
920 | WORD '?' { $$ = wildref($1); }
921 ;
922
923 /* zero or more locators */
924 locators:
925 /* empty */ { $$ = NULL; }
926 | locators locator { $$ = $2; app($2, $1); }
927 ;
928
929 /* one locator */
930 locator:
931 WORD '?' { $$ = MK3(loc, $1, NULL, 0); }
932 | WORD values { $$ = namelocvals($1, $2); }
933 ;
934
935 /* optional device flags */
936 device_flags:
937 /* empty */ { $$ = 0; }
938 | FLAGS NUMBER { $$ = $2.val; }
939 ;
940
941 /************************************************************/
942
943 /*
944 * conditions
945 */
946
947
948 /*
949 * order of options is important, must use right recursion
950 *
951 * dholland 20120310: wut?
952 */
953
954 /* expression of conditions */
955 condexpr:
956 cond_or_expr
957 ;
958
959 cond_or_expr:
960 cond_and_expr
961 | cond_or_expr '|' cond_and_expr { $$ = MKF2(cx, or, $1, $3); }
962 ;
963
964 cond_and_expr:
965 cond_prefix_expr
966 | cond_and_expr '&' cond_prefix_expr { $$ = MKF2(cx, and, $1, $3); }
967 ;
968
969 cond_prefix_expr:
970 cond_base_expr
971 /* XXX notyet - need to strengthen downstream first */
972 /* | '!' cond_prefix_expr { $$ = MKF1(cx, not, $2); } */
973 ;
974
975 cond_base_expr:
976 condatom { $$ = $1; }
977 | '!' condatom { $$ = MKF1(cx, not, $2); }
978 | '(' condexpr ')' { $$ = $2; }
979 ;
980
981 /* basic element of config element expression: a config element */
982 condatom:
983 WORD { $$ = MKF1(cx, atom, $1); }
984 ;
985
986 /************************************************************/
987
988 /*
989 * Various nonterminals shared between the grammars.
990 */
991
992 /* variable name for make option */
993 mkvarname:
994 QSTRING { $$ = $1; }
995 | WORD { $$ = $1; }
996 ;
997
998 /* optional file for an option */
999 optfile_opt:
1000 /* empty */ { $$ = NULL; }
1001 | filename { $$ = $1; }
1002 ;
1003
1004 /* filename. */
1005 filename:
1006 QSTRING { $$ = $1; }
1007 | PATHNAME { $$ = $1; }
1008 ;
1009
1010 /* constant value */
1011 value:
1012 QSTRING { $$ = $1; }
1013 | WORD { $$ = $1; }
1014 | EMPTYSTRING { $$ = $1; }
1015 | signed_number {
1016 char bf[40];
1017
1018 (void)snprintf(bf, sizeof(bf), FORMAT($1), (long long)$1.val);
1019 $$ = intern(bf);
1020 }
1021 ;
1022
1023 /* constant value that is a string */
1024 stringvalue:
1025 QSTRING { $$ = $1; }
1026 | WORD { $$ = $1; }
1027 ;
1028
1029 /* comma-separated list of values */
1030 /* XXX why right-recursive? */
1031 values:
1032 value { $$ = MKF2(loc, val, $1, NULL); }
1033 | value ',' values { $$ = MKF2(loc, val, $1, $3); }
1034 ;
1035
1036 /* possibly negative number */
1037 signed_number:
1038 NUMBER { $$ = $1; }
1039 | '-' NUMBER { $$.fmt = $2.fmt; $$.val = -$2.val; }
1040 ;
1041
1042 /* optional ON keyword */
1043 on_opt:
1044 /* empty */
1045 | ON
1046 ;
1047
1048 %%
1049
1050 void
1051 yyerror(const char *s)
1052 {
1053
1054 cfgerror("%s", s);
1055 }
1056
1057 /************************************************************/
1058
1059 /*
1060 * Wrap allocations that live on the parser stack so that we can free
1061 * them again on error instead of leaking.
1062 */
1063
1064 #define MAX_WRAP 1000
1065
1066 struct wrap_entry {
1067 void *ptr;
1068 unsigned typecode;
1069 };
1070
1071 static struct wrap_entry wrapstack[MAX_WRAP];
1072 static unsigned wrap_depth;
1073
1074 /*
1075 * Remember pointer PTR with type-code CODE.
1076 */
1077 static void
1078 wrap_alloc(void *ptr, unsigned code)
1079 {
1080 unsigned pos;
1081
1082 if (wrap_depth >= MAX_WRAP) {
1083 panic("allocation wrapper stack overflow");
1084 }
1085 pos = wrap_depth++;
1086 wrapstack[pos].ptr = ptr;
1087 wrapstack[pos].typecode = code;
1088 }
1089
1090 /*
1091 * We succeeded; commit to keeping everything that's been allocated so
1092 * far and clear the stack.
1093 */
1094 static void
1095 wrap_continue(void)
1096 {
1097 wrap_depth = 0;
1098 }
1099
1100 /*
1101 * We failed; destroy all the objects allocated.
1102 */
1103 static void
1104 wrap_cleanup(void)
1105 {
1106 unsigned i;
1107
1108 /*
1109 * Destroy each item. Note that because everything allocated
1110 * is entered on the list separately, lists and trees need to
1111 * have their links blanked before being destroyed. Also note
1112 * that strings are interned elsewhere and not handled by this
1113 * mechanism.
1114 */
1115
1116 for (i=0; i<wrap_depth; i++) {
1117 switch (wrapstack[i].typecode) {
1118 case WRAP_CODE_nvlist:
1119 nvfree(wrapstack[i].ptr);
1120 break;
1121 case WRAP_CODE_defoptlist:
1122 {
1123 struct defoptlist *dl = wrapstack[i].ptr;
1124
1125 dl->dl_next = NULL;
1126 defoptlist_destroy(dl);
1127 }
1128 break;
1129 case WRAP_CODE_loclist:
1130 {
1131 struct loclist *ll = wrapstack[i].ptr;
1132
1133 ll->ll_next = NULL;
1134 loclist_destroy(ll);
1135 }
1136 break;
1137 case WRAP_CODE_attrlist:
1138 {
1139 struct attrlist *al = wrapstack[i].ptr;
1140
1141 al->al_next = NULL;
1142 al->al_this = NULL;
1143 attrlist_destroy(al);
1144 }
1145 break;
1146 case WRAP_CODE_condexpr:
1147 {
1148 struct condexpr *cx = wrapstack[i].ptr;
1149
1150 cx->cx_type = CX_ATOM;
1151 cx->cx_atom = NULL;
1152 condexpr_destroy(cx);
1153 }
1154 break;
1155 default:
1156 panic("invalid code %u on allocation wrapper stack",
1157 wrapstack[i].typecode);
1158 }
1159 }
1160
1161 wrap_depth = 0;
1162 }
1163
1164 /*
1165 * Instantiate the wrapper functions.
1166 *
1167 * Each one calls wrap_alloc to save the pointer and then returns the
1168 * pointer again; these need to be generated with the preprocessor in
1169 * order to be typesafe.
1170 */
1171 #define DEF_ALLOCWRAP(t) \
1172 static struct t * \
1173 wrap_mk_##t(struct t *arg) \
1174 { \
1175 wrap_alloc(arg, WRAP_CODE_##t); \
1176 return arg; \
1177 }
1178
1179 DEF_ALLOCWRAP(nvlist);
1180 DEF_ALLOCWRAP(defoptlist);
1181 DEF_ALLOCWRAP(loclist);
1182 DEF_ALLOCWRAP(attrlist);
1183 DEF_ALLOCWRAP(condexpr);
1184
1185 /************************************************************/
1186
1187 /*
1188 * Data constructors
1189 *
1190 * (These are *beneath* the allocation wrappers.)
1191 */
1192
1193 static struct defoptlist *
1194 mk_defoptlist(const char *name, const char *val, const char *lintval)
1195 {
1196 return defoptlist_create(name, val, lintval);
1197 }
1198
1199 static struct loclist *
1200 mk_loc(const char *name, const char *str, long long num)
1201 {
1202 return loclist_create(name, str, num);
1203 }
1204
1205 static struct loclist *
1206 mk_loc_val(const char *str, struct loclist *next)
1207 {
1208 struct loclist *ll;
1209
1210 ll = mk_loc(NULL, str, 0);
1211 ll->ll_next = next;
1212 return ll;
1213 }
1214
1215 static struct attrlist *
1216 mk_attrlist(struct attrlist *next, struct attr *a)
1217 {
1218 return attrlist_cons(next, a);
1219 }
1220
1221 static struct condexpr *
1222 mk_cx_atom(const char *s)
1223 {
1224 struct condexpr *cx;
1225
1226 cx = condexpr_create(CX_ATOM);
1227 cx->cx_atom = s;
1228 return cx;
1229 }
1230
1231 static struct condexpr *
1232 mk_cx_not(struct condexpr *sub)
1233 {
1234 struct condexpr *cx;
1235
1236 cx = condexpr_create(CX_NOT);
1237 cx->cx_not = sub;
1238 return cx;
1239 }
1240
1241 static struct condexpr *
1242 mk_cx_and(struct condexpr *left, struct condexpr *right)
1243 {
1244 struct condexpr *cx;
1245
1246 cx = condexpr_create(CX_AND);
1247 cx->cx_and.left = left;
1248 cx->cx_and.right = right;
1249 return cx;
1250 }
1251
1252 static struct condexpr *
1253 mk_cx_or(struct condexpr *left, struct condexpr *right)
1254 {
1255 struct condexpr *cx;
1256
1257 cx = condexpr_create(CX_OR);
1258 cx->cx_or.left = left;
1259 cx->cx_or.right = right;
1260 return cx;
1261 }
1262
1263 /************************************************************/
1264
1265 static void
1266 setmachine(const char *mch, const char *mcharch, struct nvlist *mchsubarches,
1267 int isioconf)
1268 {
1269 char buf[MAXPATHLEN];
1270 struct nvlist *nv;
1271
1272 if (isioconf) {
1273 if (include(_PATH_DEVNULL, ENDDEFS, 0, 0) != 0)
1274 exit(1);
1275 ioconfname = mch;
1276 return;
1277 }
1278
1279 machine = mch;
1280 machinearch = mcharch;
1281 machinesubarches = mchsubarches;
1282
1283 /*
1284 * Define attributes for all the given names
1285 */
1286 if (defattr(machine, NULL, NULL, 0) != 0 ||
1287 (machinearch != NULL &&
1288 defattr(machinearch, NULL, NULL, 0) != 0))
1289 exit(1);
1290 for (nv = machinesubarches; nv != NULL; nv = nv->nv_next) {
1291 if (defattr(nv->nv_name, NULL, NULL, 0) != 0)
1292 exit(1);
1293 }
1294
1295 /*
1296 * Set up the file inclusion stack. This empty include tells
1297 * the parser there are no more device definitions coming.
1298 */
1299 if (include(_PATH_DEVNULL, ENDDEFS, 0, 0) != 0)
1300 exit(1);
1301
1302 /* Include arch/${MACHINE}/conf/files.${MACHINE} */
1303 (void)snprintf(buf, sizeof(buf), "arch/%s/conf/files.%s",
1304 machine, machine);
1305 if (include(buf, ENDFILE, 0, 0) != 0)
1306 exit(1);
1307
1308 /* Include any arch/${MACHINE_SUBARCH}/conf/files.${MACHINE_SUBARCH} */
1309 for (nv = machinesubarches; nv != NULL; nv = nv->nv_next) {
1310 (void)snprintf(buf, sizeof(buf), "arch/%s/conf/files.%s",
1311 nv->nv_name, nv->nv_name);
1312 if (include(buf, ENDFILE, 0, 0) != 0)
1313 exit(1);
1314 }
1315
1316 /* Include any arch/${MACHINE_ARCH}/conf/files.${MACHINE_ARCH} */
1317 if (machinearch != NULL)
1318 (void)snprintf(buf, sizeof(buf), "arch/%s/conf/files.%s",
1319 machinearch, machinearch);
1320 else
1321 strlcpy(buf, _PATH_DEVNULL, sizeof(buf));
1322 if (include(buf, ENDFILE, 0, 0) != 0)
1323 exit(1);
1324
1325 /*
1326 * Include the global conf/files. As the last thing
1327 * pushed on the stack, it will be processed first.
1328 */
1329 if (include("conf/files", ENDFILE, 0, 0) != 0)
1330 exit(1);
1331
1332 oktopackage = 1;
1333 }
1334
1335 static void
1336 check_maxpart(void)
1337 {
1338
1339 if (maxpartitions <= 0 && ioconfname == NULL) {
1340 stop("cannot proceed without maxpartitions specifier");
1341 }
1342 }
1343
1344 static void
1345 check_version(void)
1346 {
1347 /*
1348 * In essence, version is 0 and is not supported anymore
1349 */
1350 if (version < CONFIG_MINVERSION)
1351 stop("your sources are out of date -- please update.");
1352 }
1353
1354 /*
1355 * Prepend a blank entry to the locator definitions so the code in
1356 * sem.c can distinguish "empty locator list" from "no locator list".
1357 * XXX gross.
1358 */
1359 static struct loclist *
1360 present_loclist(struct loclist *ll)
1361 {
1362 struct loclist *ret;
1363
1364 ret = MK3(loc, "", NULL, 0);
1365 ret->ll_next = ll;
1366 return ret;
1367 }
1368
1369 static void
1370 app(struct loclist *p, struct loclist *q)
1371 {
1372 while (p->ll_next)
1373 p = p->ll_next;
1374 p->ll_next = q;
1375 }
1376
1377 static struct loclist *
1378 locarray(const char *name, int count, struct loclist *adefs, int opt)
1379 {
1380 struct loclist *defs = adefs;
1381 struct loclist **p;
1382 char buf[200];
1383 int i;
1384
1385 if (count <= 0) {
1386 fprintf(stderr, "config: array with <= 0 size: %s\n", name);
1387 exit(1);
1388 }
1389 p = &defs;
1390 for(i = 0; i < count; i++) {
1391 if (*p == NULL)
1392 *p = MK3(loc, NULL, "0", 0);
1393 snprintf(buf, sizeof(buf), "%s%c%d", name, ARRCHR, i);
1394 (*p)->ll_name = i == 0 ? name : intern(buf);
1395 (*p)->ll_num = i > 0 || opt;
1396 p = &(*p)->ll_next;
1397 }
1398 *p = 0;
1399 return defs;
1400 }
1401
1402
1403 static struct loclist *
1404 namelocvals(const char *name, struct loclist *vals)
1405 {
1406 struct loclist *p;
1407 char buf[200];
1408 int i;
1409
1410 for (i = 0, p = vals; p; i++, p = p->ll_next) {
1411 snprintf(buf, sizeof(buf), "%s%c%d", name, ARRCHR, i);
1412 p->ll_name = i == 0 ? name : intern(buf);
1413 }
1414 return vals;
1415 }
1416
1417