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