1 %{ 2 /* $NetBSD: gram.y,v 1.39 2014/05/29 07:47:45 mrg 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 | NO IDENT { setident(NULL); } 631 | CONFIG conf root_spec sysparam_list 632 { addconf(&conf); } 633 | NO CONFIG WORD { delconf($3); } 634 | NO PSEUDO_DEVICE WORD { delpseudo($3); } 635 | PSEUDO_DEVICE WORD npseudo { addpseudo($2, $3); } 636 | PSEUDO_ROOT device_instance { addpseudoroot($2); } 637 | NO device_instance AT attachment 638 { deldevi($2, $4); } 639 | NO DEVICE AT attachment { deldeva($4); } 640 | NO device_instance { deldev($2); } 641 | device_instance AT attachment locators device_flags 642 { adddev($1, $3, $4, $5); } 643 ; 644 645 /* list of filesystems */ 646 fs_list: 647 fsoption 648 | fs_list ',' fsoption 649 ; 650 651 /* one filesystem */ 652 fsoption: 653 WORD { addfsoption($1); } 654 ; 655 656 /* list of filesystems that had NO in front */ 657 no_fs_list: 658 no_fsoption 659 | no_fs_list ',' no_fsoption 660 ; 661 662 /* one filesystem that had NO in front */ 663 no_fsoption: 664 WORD { delfsoption($1); } 665 ; 666 667 /* list of make options */ 668 /* XXX why is this right-recursive? */ 669 mkopt_list: 670 mkoption 671 | mkopt_list ',' mkoption 672 ; 673 674 /* one make option */ 675 mkoption: 676 mkvarname '=' value { addmkoption($1, $3); } 677 | mkvarname PLUSEQ value { appendmkoption($1, $3); } 678 ; 679 680 /* list of make options that had NO in front */ 681 no_mkopt_list: 682 no_mkoption 683 | no_mkopt_list ',' no_mkoption 684 ; 685 686 /* one make option that had NO in front */ 687 /* XXX shouldn't this be mkvarname rather than WORD? */ 688 no_mkoption: 689 WORD { delmkoption($1); } 690 ; 691 692 /* list of options */ 693 opt_list: 694 option 695 | opt_list ',' option 696 ; 697 698 /* one option */ 699 option: 700 WORD { addoption($1, NULL); } 701 | WORD '=' value { addoption($1, $3); } 702 ; 703 704 /* list of options that had NO in front */ 705 no_opt_list: 706 no_option 707 | no_opt_list ',' no_option 708 ; 709 710 /* one option that had NO in front */ 711 no_option: 712 WORD { deloption($1); } 713 ; 714 715 /* the name in "config name root on ..." */ 716 conf: 717 WORD { 718 conf.cf_name = $1; 719 conf.cf_lineno = currentline(); 720 conf.cf_fstype = NULL; 721 conf.cf_root = NULL; 722 conf.cf_dump = NULL; 723 } 724 ; 725 726 /* root fs specification */ 727 root_spec: 728 ROOT on_opt dev_spec { setconf(&conf.cf_root, "root", $3); } 729 | ROOT on_opt dev_spec fs_spec { setconf(&conf.cf_root, "root", $3); } 730 ; 731 732 /* device for root fs or dump */ 733 dev_spec: 734 '?' { $$ = new_si(intern("?"), NODEV); } 735 | WORD { $$ = new_si($1, NODEV); } 736 | major_minor { $$ = new_si(NULL, $1); } 737 ; 738 739 /* major and minor device number */ 740 major_minor: 741 MAJOR NUMBER MINOR NUMBER { $$ = makedev($2.val, $4.val); } 742 ; 743 744 /* filesystem type for root fs specification */ 745 fs_spec: 746 TYPE '?' { setfstype(&conf.cf_fstype, intern("?")); } 747 | TYPE WORD { setfstype(&conf.cf_fstype, $2); } 748 ; 749 750 /* zero or more additional system parameters */ 751 sysparam_list: 752 /* empty */ 753 | sysparam_list sysparam 754 ; 755 756 /* one additional system parameter (there's only one: dumps) */ 757 sysparam: 758 DUMPS on_opt dev_spec { setconf(&conf.cf_dump, "dumps", $3); } 759 ; 760 761 /* number of pseudo devices to configure (which is optional) */ 762 npseudo: 763 /* empty */ { $$ = 1; } 764 | NUMBER { $$ = $1.val; } 765 ; 766 767 /* name of a device to configure */ 768 device_instance: 769 WORD { $$ = $1; } 770 | WORD '*' { $$ = starref($1); } 771 ; 772 773 /* name of a device to configure an attachment to */ 774 attachment: 775 ROOT { $$ = NULL; } 776 | WORD { $$ = $1; } 777 | WORD '?' { $$ = wildref($1); } 778 ; 779 780 /* zero or more locators */ 781 locators: 782 /* empty */ { $$ = NULL; } 783 | locators locator { $$ = $2; app($2, $1); } 784 ; 785 786 /* one locator */ 787 locator: 788 WORD '?' { $$ = MK3(loc, $1, NULL, 0); } 789 | WORD values { $$ = namelocvals($1, $2); } 790 ; 791 792 /* optional device flags */ 793 device_flags: 794 /* empty */ { $$ = 0; } 795 | FLAGS NUMBER { $$ = $2.val; } 796 ; 797 798 /************************************************************/ 799 800 /* 801 * conditions 802 */ 803 804 805 /* 806 * order of options is important, must use right recursion 807 * 808 * dholland 20120310: wut? 809 */ 810 811 /* expression of conditions */ 812 condexpr: 813 cond_or_expr 814 ; 815 816 cond_or_expr: 817 cond_and_expr 818 | cond_or_expr '|' cond_and_expr { $$ = MKF2(cx, or, $1, $3); } 819 ; 820 821 cond_and_expr: 822 cond_prefix_expr 823 | cond_and_expr '&' cond_prefix_expr { $$ = MKF2(cx, and, $1, $3); } 824 ; 825 826 cond_prefix_expr: 827 cond_base_expr 828 /* XXX notyet - need to strengthen downstream first */ 829 /* | '!' cond_prefix_expr { $$ = MKF1(cx, not, $2); } */ 830 ; 831 832 cond_base_expr: 833 condatom { $$ = $1; } 834 | '!' condatom { $$ = MKF1(cx, not, $2); } 835 | '(' condexpr ')' { $$ = $2; } 836 ; 837 838 /* basic element of config element expression: a config element */ 839 condatom: 840 WORD { $$ = MKF1(cx, atom, $1); } 841 ; 842 843 /************************************************************/ 844 845 /* 846 * Various nonterminals shared between the grammars. 847 */ 848 849 /* variable name for make option */ 850 mkvarname: 851 QSTRING { $$ = $1; } 852 | WORD { $$ = $1; } 853 ; 854 855 /* optional file for an option */ 856 optfile_opt: 857 /* empty */ { $$ = NULL; } 858 | filename { $$ = $1; } 859 ; 860 861 /* filename. */ 862 filename: 863 QSTRING { $$ = $1; } 864 | PATHNAME { $$ = $1; } 865 ; 866 867 /* constant value */ 868 value: 869 QSTRING { $$ = $1; } 870 | WORD { $$ = $1; } 871 | EMPTYSTRING { $$ = $1; } 872 | signed_number { 873 char bf[40]; 874 875 (void)snprintf(bf, sizeof(bf), FORMAT($1), (long long)$1.val); 876 $$ = intern(bf); 877 } 878 ; 879 880 /* constant value that is a string */ 881 stringvalue: 882 QSTRING { $$ = $1; } 883 | WORD { $$ = $1; } 884 ; 885 886 /* comma-separated list of values */ 887 /* XXX why right-recursive? */ 888 values: 889 value { $$ = MKF2(loc, val, $1, NULL); } 890 | value ',' values { $$ = MKF2(loc, val, $1, $3); } 891 ; 892 893 /* possibly negative number */ 894 signed_number: 895 NUMBER { $$ = $1; } 896 | '-' NUMBER { $$.fmt = $2.fmt; $$.val = -$2.val; } 897 ; 898 899 /* optional ON keyword */ 900 on_opt: 901 /* empty */ 902 | ON 903 ; 904 905 %% 906 907 void 908 yyerror(const char *s) 909 { 910 911 cfgerror("%s", s); 912 } 913 914 /************************************************************/ 915 916 /* 917 * Wrap allocations that live on the parser stack so that we can free 918 * them again on error instead of leaking. 919 */ 920 921 #define MAX_WRAP 1000 922 923 struct wrap_entry { 924 void *ptr; 925 unsigned typecode; 926 }; 927 928 static struct wrap_entry wrapstack[MAX_WRAP]; 929 static unsigned wrap_depth; 930 931 /* 932 * Remember pointer PTR with type-code CODE. 933 */ 934 static void 935 wrap_alloc(void *ptr, unsigned code) 936 { 937 unsigned pos; 938 939 if (wrap_depth >= MAX_WRAP) { 940 panic("allocation wrapper stack overflow"); 941 } 942 pos = wrap_depth++; 943 wrapstack[pos].ptr = ptr; 944 wrapstack[pos].typecode = code; 945 } 946 947 /* 948 * We succeeded; commit to keeping everything that's been allocated so 949 * far and clear the stack. 950 */ 951 static void 952 wrap_continue(void) 953 { 954 wrap_depth = 0; 955 } 956 957 /* 958 * We failed; destroy all the objects allocated. 959 */ 960 static void 961 wrap_cleanup(void) 962 { 963 unsigned i; 964 965 /* 966 * Destroy each item. Note that because everything allocated 967 * is entered on the list separately, lists and trees need to 968 * have their links blanked before being destroyed. Also note 969 * that strings are interned elsewhere and not handled by this 970 * mechanism. 971 */ 972 973 for (i=0; i<wrap_depth; i++) { 974 switch (wrapstack[i].typecode) { 975 case WRAP_CODE_nvlist: 976 nvfree(wrapstack[i].ptr); 977 break; 978 case WRAP_CODE_defoptlist: 979 { 980 struct defoptlist *dl = wrapstack[i].ptr; 981 982 dl->dl_next = NULL; 983 defoptlist_destroy(dl); 984 } 985 break; 986 case WRAP_CODE_loclist: 987 { 988 struct loclist *ll = wrapstack[i].ptr; 989 990 ll->ll_next = NULL; 991 loclist_destroy(ll); 992 } 993 break; 994 case WRAP_CODE_attrlist: 995 { 996 struct attrlist *al = wrapstack[i].ptr; 997 998 al->al_next = NULL; 999 al->al_this = NULL; 1000 attrlist_destroy(al); 1001 } 1002 break; 1003 case WRAP_CODE_condexpr: 1004 { 1005 struct condexpr *cx = wrapstack[i].ptr; 1006 1007 cx->cx_type = CX_ATOM; 1008 cx->cx_atom = NULL; 1009 condexpr_destroy(cx); 1010 } 1011 break; 1012 default: 1013 panic("invalid code %u on allocation wrapper stack", 1014 wrapstack[i].typecode); 1015 } 1016 } 1017 1018 wrap_depth = 0; 1019 } 1020 1021 /* 1022 * Instantiate the wrapper functions. 1023 * 1024 * Each one calls wrap_alloc to save the pointer and then returns the 1025 * pointer again; these need to be generated with the preprocessor in 1026 * order to be typesafe. 1027 */ 1028 #define DEF_ALLOCWRAP(t) \ 1029 static struct t * \ 1030 wrap_mk_##t(struct t *arg) \ 1031 { \ 1032 wrap_alloc(arg, WRAP_CODE_##t); \ 1033 return arg; \ 1034 } 1035 1036 DEF_ALLOCWRAP(nvlist); 1037 DEF_ALLOCWRAP(defoptlist); 1038 DEF_ALLOCWRAP(loclist); 1039 DEF_ALLOCWRAP(attrlist); 1040 DEF_ALLOCWRAP(condexpr); 1041 1042 /************************************************************/ 1043 1044 /* 1045 * Data constructors 1046 * 1047 * (These are *beneath* the allocation wrappers.) 1048 */ 1049 1050 static struct defoptlist * 1051 mk_defoptlist(const char *name, const char *val, const char *lintval) 1052 { 1053 return defoptlist_create(name, val, lintval); 1054 } 1055 1056 static struct loclist * 1057 mk_loc(const char *name, const char *str, long long num) 1058 { 1059 return loclist_create(name, str, num); 1060 } 1061 1062 static struct loclist * 1063 mk_loc_val(const char *str, struct loclist *next) 1064 { 1065 struct loclist *ll; 1066 1067 ll = mk_loc(NULL, str, 0); 1068 ll->ll_next = next; 1069 return ll; 1070 } 1071 1072 static struct attrlist * 1073 mk_attrlist(struct attrlist *next, struct attr *a) 1074 { 1075 return attrlist_cons(next, a); 1076 } 1077 1078 static struct condexpr * 1079 mk_cx_atom(const char *s) 1080 { 1081 struct condexpr *cx; 1082 1083 cx = condexpr_create(CX_ATOM); 1084 cx->cx_atom = s; 1085 return cx; 1086 } 1087 1088 static struct condexpr * 1089 mk_cx_not(struct condexpr *sub) 1090 { 1091 struct condexpr *cx; 1092 1093 cx = condexpr_create(CX_NOT); 1094 cx->cx_not = sub; 1095 return cx; 1096 } 1097 1098 static struct condexpr * 1099 mk_cx_and(struct condexpr *left, struct condexpr *right) 1100 { 1101 struct condexpr *cx; 1102 1103 cx = condexpr_create(CX_AND); 1104 cx->cx_and.left = left; 1105 cx->cx_and.right = right; 1106 return cx; 1107 } 1108 1109 static struct condexpr * 1110 mk_cx_or(struct condexpr *left, struct condexpr *right) 1111 { 1112 struct condexpr *cx; 1113 1114 cx = condexpr_create(CX_OR); 1115 cx->cx_or.left = left; 1116 cx->cx_or.right = right; 1117 return cx; 1118 } 1119 1120 /************************************************************/ 1121 1122 static void 1123 setmachine(const char *mch, const char *mcharch, struct nvlist *mchsubarches, 1124 int isioconf) 1125 { 1126 char buf[MAXPATHLEN]; 1127 struct nvlist *nv; 1128 1129 if (isioconf) { 1130 if (include(_PATH_DEVNULL, ENDDEFS, 0, 0) != 0) 1131 exit(1); 1132 ioconfname = mch; 1133 return; 1134 } 1135 1136 machine = mch; 1137 machinearch = mcharch; 1138 machinesubarches = mchsubarches; 1139 1140 /* 1141 * Define attributes for all the given names 1142 */ 1143 if (defattr(machine, NULL, NULL, 0) != 0 || 1144 (machinearch != NULL && 1145 defattr(machinearch, NULL, NULL, 0) != 0)) 1146 exit(1); 1147 for (nv = machinesubarches; nv != NULL; nv = nv->nv_next) { 1148 if (defattr(nv->nv_name, NULL, NULL, 0) != 0) 1149 exit(1); 1150 } 1151 1152 /* 1153 * Set up the file inclusion stack. This empty include tells 1154 * the parser there are no more device definitions coming. 1155 */ 1156 if (include(_PATH_DEVNULL, ENDDEFS, 0, 0) != 0) 1157 exit(1); 1158 1159 /* Include arch/${MACHINE}/conf/files.${MACHINE} */ 1160 (void)snprintf(buf, sizeof(buf), "arch/%s/conf/files.%s", 1161 machine, machine); 1162 if (include(buf, ENDFILE, 0, 0) != 0) 1163 exit(1); 1164 1165 /* Include any arch/${MACHINE_SUBARCH}/conf/files.${MACHINE_SUBARCH} */ 1166 for (nv = machinesubarches; nv != NULL; nv = nv->nv_next) { 1167 (void)snprintf(buf, sizeof(buf), "arch/%s/conf/files.%s", 1168 nv->nv_name, nv->nv_name); 1169 if (include(buf, ENDFILE, 0, 0) != 0) 1170 exit(1); 1171 } 1172 1173 /* Include any arch/${MACHINE_ARCH}/conf/files.${MACHINE_ARCH} */ 1174 if (machinearch != NULL) 1175 (void)snprintf(buf, sizeof(buf), "arch/%s/conf/files.%s", 1176 machinearch, machinearch); 1177 else 1178 strlcpy(buf, _PATH_DEVNULL, sizeof(buf)); 1179 if (include(buf, ENDFILE, 0, 0) != 0) 1180 exit(1); 1181 1182 /* 1183 * Include the global conf/files. As the last thing 1184 * pushed on the stack, it will be processed first. 1185 */ 1186 if (include("conf/files", ENDFILE, 0, 0) != 0) 1187 exit(1); 1188 1189 oktopackage = 1; 1190 } 1191 1192 static void 1193 check_maxpart(void) 1194 { 1195 1196 if (maxpartitions <= 0 && ioconfname == NULL) { 1197 stop("cannot proceed without maxpartitions specifier"); 1198 } 1199 } 1200 1201 static void 1202 check_version(void) 1203 { 1204 /* 1205 * In essence, version is 0 and is not supported anymore 1206 */ 1207 if (version < CONFIG_MINVERSION) 1208 stop("your sources are out of date -- please update."); 1209 } 1210 1211 /* 1212 * Prepend a blank entry to the locator definitions so the code in 1213 * sem.c can distinguish "empty locator list" from "no locator list". 1214 * XXX gross. 1215 */ 1216 static struct loclist * 1217 present_loclist(struct loclist *ll) 1218 { 1219 struct loclist *ret; 1220 1221 ret = MK3(loc, "", NULL, 0); 1222 ret->ll_next = ll; 1223 return ret; 1224 } 1225 1226 static void 1227 app(struct loclist *p, struct loclist *q) 1228 { 1229 while (p->ll_next) 1230 p = p->ll_next; 1231 p->ll_next = q; 1232 } 1233 1234 static struct loclist * 1235 locarray(const char *name, int count, struct loclist *adefs, int opt) 1236 { 1237 struct loclist *defs = adefs; 1238 struct loclist **p; 1239 char buf[200]; 1240 int i; 1241 1242 if (count <= 0) { 1243 fprintf(stderr, "config: array with <= 0 size: %s\n", name); 1244 exit(1); 1245 } 1246 p = &defs; 1247 for(i = 0; i < count; i++) { 1248 if (*p == NULL) 1249 *p = MK3(loc, NULL, "0", 0); 1250 snprintf(buf, sizeof(buf), "%s%c%d", name, ARRCHR, i); 1251 (*p)->ll_name = i == 0 ? name : intern(buf); 1252 (*p)->ll_num = i > 0 || opt; 1253 p = &(*p)->ll_next; 1254 } 1255 *p = 0; 1256 return defs; 1257 } 1258 1259 1260 static struct loclist * 1261 namelocvals(const char *name, struct loclist *vals) 1262 { 1263 struct loclist *p; 1264 char buf[200]; 1265 int i; 1266 1267 for (i = 0, p = vals; p; i++, p = p->ll_next) { 1268 snprintf(buf, sizeof(buf), "%s%c%d", name, ARRCHR, i); 1269 p->ll_name = i == 0 ? name : intern(buf); 1270 } 1271 return vals; 1272 } 1273 1274