1 %union { 2 char *str; 3 int val; 4 struct file_list *file; 5 } 6 7 %token ARCH 8 %token COMMA 9 %token CONFIG 10 %token CPU 11 %token NOCPU 12 %token DEVICE 13 %token NODEVICE 14 %token ENV 15 %token ENVVAR 16 %token EQUALS 17 %token PLUSEQUALS 18 %token HINTS 19 %token IDENT 20 %token MAXUSERS 21 %token OPTIONS 22 %token NOOPTION 23 %token MAKEOPTIONS 24 %token NOMAKEOPTION 25 %token SEMICOLON 26 %token INCLUDE 27 %token FILES 28 29 %token <str> ENVLINE 30 %token <str> ID 31 %token <val> NUMBER 32 33 %type <str> Save_id 34 %type <str> Opt_value 35 %type <str> Dev 36 %token <str> PATH 37 38 %{ 39 40 /*- 41 * SPDX-License-Identifier: BSD-3-Clause 42 * 43 * Copyright (c) 1988, 1993 44 * The Regents of the University of California. All rights reserved. 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 3. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 * 70 * @(#)config.y 8.1 (Berkeley) 6/6/93 71 * $FreeBSD$ 72 */ 73 74 #include <assert.h> 75 #include <ctype.h> 76 #include <err.h> 77 #include <stdio.h> 78 #include <string.h> 79 80 #include "config.h" 81 82 struct device_head dtab; 83 char *ident; 84 char *env; 85 int yyline; 86 const char *yyfile; 87 struct file_list_head ftab; 88 struct files_name_head fntab; 89 char errbuf[80]; 90 int maxusers; 91 92 #define ns(s) strdup(s) 93 int include(const char *, int); 94 void yyerror(const char *s); 95 int yywrap(void); 96 97 static void newdev(char *name); 98 static void newfile(char *name); 99 static void newenvvar(char *name, bool is_file); 100 static void rmdev_schedule(struct device_head *dh, char *name); 101 static void newopt(struct opt_head *list, char *name, char *value, int append, int dupe); 102 static void rmopt_schedule(struct opt_head *list, char *name); 103 104 static char * 105 devopt(char *dev) 106 { 107 char *ret = malloc(strlen(dev) + 5); 108 109 sprintf(ret, "DEV_%s", dev); 110 raisestr(ret); 111 return ret; 112 } 113 114 %} 115 %% 116 Configuration: 117 Many_specs 118 ; 119 120 Many_specs: 121 Many_specs Spec 122 | 123 /* lambda */ 124 ; 125 126 Spec: 127 Device_spec SEMICOLON 128 | 129 Config_spec SEMICOLON 130 | 131 INCLUDE PATH SEMICOLON { 132 if (incignore == 0) 133 include($2, 0); 134 }; 135 | 136 INCLUDE ID SEMICOLON { 137 if (incignore == 0) 138 include($2, 0); 139 }; 140 | 141 FILES ID SEMICOLON { newfile($2); }; 142 | 143 SEMICOLON 144 | 145 error SEMICOLON 146 ; 147 148 Config_spec: 149 ARCH Save_id { 150 if (machinename != NULL && !eq($2, machinename)) 151 errx(1, "%s:%d: only one machine directive is allowed", 152 yyfile, yyline); 153 machinename = $2; 154 machinearch = $2; 155 } | 156 ARCH Save_id Save_id { 157 /* 158 * Allow the machinearch to change with a second machine directive, 159 * but still enforce no changes to the machinename. 160 */ 161 if (machinename != NULL && !eq($2, machinename)) 162 errx(1, "%s:%d: only one machine directive is allowed", 163 yyfile, yyline); 164 machinename = $2; 165 machinearch = $3; 166 } | 167 CPU Save_id { 168 struct cputype *cp = 169 (struct cputype *)calloc(1, sizeof (struct cputype)); 170 if (cp == NULL) 171 err(EXIT_FAILURE, "calloc"); 172 cp->cpu_name = $2; 173 SLIST_INSERT_HEAD(&cputype, cp, cpu_next); 174 } | 175 NOCPU Save_id { 176 struct cputype *cp, *cp2; 177 SLIST_FOREACH_SAFE(cp, &cputype, cpu_next, cp2) { 178 if (eq(cp->cpu_name, $2)) { 179 SLIST_REMOVE(&cputype, cp, cputype, cpu_next); 180 free(cp); 181 } 182 } 183 } | 184 OPTIONS Opt_list 185 | 186 NOOPTION NoOpt_list | 187 MAKEOPTIONS Mkopt_list 188 | 189 NOMAKEOPTION Save_id { rmopt_schedule(&mkopt, $2); } | 190 IDENT ID { ident = $2; } | 191 System_spec 192 | 193 MAXUSERS NUMBER { maxusers = $2; } | 194 ENV ID { newenvvar($2, true); } | 195 ENVVAR ENVLINE { newenvvar($2, false); } | 196 HINTS ID { 197 struct hint *hint; 198 199 hint = (struct hint *)calloc(1, sizeof (struct hint)); 200 if (hint == NULL) 201 err(EXIT_FAILURE, "calloc"); 202 hint->hint_name = $2; 203 STAILQ_INSERT_HEAD(&hints, hint, hint_next); 204 } 205 206 System_spec: 207 CONFIG System_id System_parameter_list { 208 errx(1, "%s:%d: root/dump/swap specifications obsolete", 209 yyfile, yyline); 210 } 211 | 212 CONFIG System_id 213 ; 214 215 System_id: 216 Save_id { newopt(&mkopt, ns("KERNEL"), $1, 0, 0); }; 217 218 System_parameter_list: 219 System_parameter_list ID 220 | ID 221 ; 222 223 Opt_list: 224 Opt_list COMMA Option 225 | 226 Option 227 ; 228 229 NoOpt_list: 230 NoOpt_list COMMA NoOption 231 | 232 NoOption 233 ; 234 Option: 235 Save_id { 236 newopt(&opt, $1, NULL, 0, 1); 237 if (strchr($1, '=') != NULL) 238 errx(1, "%s:%d: The `=' in options should not be " 239 "quoted", yyfile, yyline); 240 } | 241 Save_id EQUALS Opt_value { 242 newopt(&opt, $1, $3, 0, 1); 243 } ; 244 245 NoOption: 246 Save_id { 247 rmopt_schedule(&opt, $1); 248 }; 249 250 Opt_value: 251 ID { $$ = $1; } | 252 NUMBER { 253 char buf[80]; 254 255 (void) snprintf(buf, sizeof(buf), "%d", $1); 256 $$ = ns(buf); 257 } ; 258 259 Save_id: 260 ID { $$ = $1; } 261 ; 262 263 Mkopt_list: 264 Mkopt_list COMMA Mkoption 265 | 266 Mkoption 267 ; 268 269 Mkoption: 270 Save_id { newopt(&mkopt, $1, ns(""), 0, 0); } | 271 Save_id EQUALS { newopt(&mkopt, $1, ns(""), 0, 0); } | 272 Save_id EQUALS Opt_value { newopt(&mkopt, $1, $3, 0, 0); } | 273 Save_id PLUSEQUALS Opt_value { newopt(&mkopt, $1, $3, 1, 0); } ; 274 275 Dev: 276 ID { $$ = $1; } 277 ; 278 279 Device_spec: 280 DEVICE Dev_list 281 | 282 NODEVICE NoDev_list 283 ; 284 285 Dev_list: 286 Dev_list COMMA Device 287 | 288 Device 289 ; 290 291 NoDev_list: 292 NoDev_list COMMA NoDevice 293 | 294 NoDevice 295 ; 296 297 Device: 298 Dev { 299 newopt(&opt, devopt($1), ns("1"), 0, 0); 300 /* and the device part */ 301 newdev($1); 302 } 303 304 NoDevice: 305 Dev { 306 char *s = devopt($1); 307 308 rmopt_schedule(&opt, s); 309 free(s); 310 /* and the device part */ 311 rmdev_schedule(&dtab, $1); 312 } ; 313 314 %% 315 316 void 317 yyerror(const char *s) 318 { 319 320 errx(1, "%s:%d: %s", yyfile, yyline + 1, s); 321 } 322 323 int 324 yywrap(void) 325 { 326 if (found_defaults) { 327 if (freopen(PREFIX, "r", stdin) == NULL) 328 err(2, "%s", PREFIX); 329 yyfile = PREFIX; 330 yyline = 0; 331 found_defaults = 0; 332 return 0; 333 } 334 return 1; 335 } 336 337 /* 338 * Add a new file to the list of files. 339 */ 340 static void 341 newfile(char *name) 342 { 343 struct files_name *nl; 344 345 nl = (struct files_name *) calloc(1, sizeof *nl); 346 if (nl == NULL) 347 err(EXIT_FAILURE, "calloc"); 348 nl->f_name = name; 349 STAILQ_INSERT_TAIL(&fntab, nl, f_next); 350 } 351 352 static void 353 newenvvar(char *name, bool is_file) 354 { 355 struct envvar *envvar; 356 357 envvar = (struct envvar *)calloc(1, sizeof (struct envvar)); 358 if (envvar == NULL) 359 err(EXIT_FAILURE, "calloc"); 360 envvar->env_str = name; 361 envvar->env_is_file = is_file; 362 STAILQ_INSERT_HEAD(&envvars, envvar, envvar_next); 363 } 364 365 /* 366 * Find a device in the list of devices. 367 */ 368 static struct device * 369 finddev(struct device_head *dlist, char *name) 370 { 371 struct device *dp; 372 373 STAILQ_FOREACH(dp, dlist, d_next) 374 if (eq(dp->d_name, name)) 375 return (dp); 376 377 return (NULL); 378 } 379 380 /* 381 * Add a device to the list of devices. 382 */ 383 static void 384 newdev(char *name) 385 { 386 struct device *np, *dp; 387 388 if ((dp = finddev(&dtab, name)) != NULL) { 389 if (strcmp(dp->yyfile, yyfile) == 0) 390 fprintf(stderr, 391 "WARNING: duplicate device `%s' encountered in %s\n", 392 name, yyfile); 393 return; 394 } 395 396 np = (struct device *) calloc(1, sizeof *np); 397 if (np == NULL) 398 err(EXIT_FAILURE, "calloc"); 399 np->d_name = name; 400 np->yyfile = strdup(yyfile); 401 STAILQ_INSERT_TAIL(&dtab, np, d_next); 402 } 403 404 /* 405 * Schedule a device to removal. 406 */ 407 static void 408 rmdev_schedule(struct device_head *dh, char *name) 409 { 410 struct device *dp; 411 412 dp = finddev(dh, name); 413 if (dp != NULL) { 414 STAILQ_REMOVE(dh, dp, device, d_next); 415 free(dp->yyfile); 416 free(dp->d_name); 417 free(dp); 418 } 419 } 420 421 /* 422 * Find an option in the list of options. 423 */ 424 static struct opt * 425 findopt(struct opt_head *list, char *name) 426 { 427 struct opt *op; 428 429 SLIST_FOREACH(op, list, op_next) 430 if (eq(op->op_name, name)) 431 return (op); 432 433 return (NULL); 434 } 435 436 /* 437 * Add an option to the list of options. 438 */ 439 static void 440 newopt(struct opt_head *list, char *name, char *value, int append, int dupe) 441 { 442 struct opt *op, *op2; 443 444 /* 445 * Ignore inclusions listed explicitly for configuration files. 446 */ 447 if (eq(name, OPT_AUTOGEN)) { 448 incignore = 1; 449 return; 450 } 451 452 op2 = findopt(list, name); 453 if (op2 != NULL && !append && !dupe) { 454 if (strcmp(op2->yyfile, yyfile) == 0) 455 fprintf(stderr, 456 "WARNING: duplicate option `%s' encountered.\n", name); 457 return; 458 } 459 460 op = (struct opt *)calloc(1, sizeof (struct opt)); 461 if (op == NULL) 462 err(EXIT_FAILURE, "calloc"); 463 op->op_name = name; 464 op->op_ownfile = 0; 465 op->op_value = value; 466 op->yyfile = strdup(yyfile); 467 if (op2 != NULL) { 468 if (append) { 469 while (SLIST_NEXT(op2, op_append) != NULL) 470 op2 = SLIST_NEXT(op2, op_append); 471 SLIST_NEXT(op2, op_append) = op; 472 } else { 473 while (SLIST_NEXT(op2, op_next) != NULL) 474 op2 = SLIST_NEXT(op2, op_next); 475 SLIST_NEXT(op2, op_next) = op; 476 } 477 } else 478 SLIST_INSERT_HEAD(list, op, op_next); 479 } 480 481 /* 482 * Remove an option from the list of options. 483 */ 484 static void 485 rmopt_schedule(struct opt_head *list, char *name) 486 { 487 struct opt *op; 488 489 while ((op = findopt(list, name)) != NULL) { 490 SLIST_REMOVE(list, op, opt, op_next); 491 free(op->yyfile); 492 free(op->op_name); 493 free(op); 494 } 495 } 496