config.y revision 169647
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 EQUALS 16%token HINTS 17%token IDENT 18%token MAXUSERS 19%token PROFILE 20%token OPTIONS 21%token NOOPTION 22%token MAKEOPTIONS 23%token NOMAKEOPTION 24%token SEMICOLON 25%token INCLUDE 26%token FILES 27 28%token <str> ID 29%token <val> NUMBER 30 31%type <str> Save_id 32%type <str> Opt_value 33%type <str> Dev 34 35%{ 36 37/* 38 * Copyright (c) 1988, 1993 39 * The Regents of the University of California. All rights reserved. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. All advertising materials mentioning features or use of this software 50 * must display the following acknowledgement: 51 * This product includes software developed by the University of 52 * California, Berkeley and its contributors. 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: head/usr.sbin/config/config.y 169647 2007-05-17 04:53:52Z imp $ 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 envmode; 85int hintmode; 86int yyline; 87const char *yyfile; 88struct file_list_head ftab; 89struct files_name_head fntab; 90char errbuf[80]; 91int maxusers; 92 93#define ns(s) strdup(s) 94int include(const char *, int); 95void yyerror(const char *s); 96int yywrap(void); 97 98static char * 99devopt(char *dev) 100{ 101 char *ret = malloc(strlen(dev) + 5); 102 103 sprintf(ret, "DEV_%s", dev); 104 raisestr(ret); 105 return ret; 106} 107 108%} 109%% 110Configuration: 111 Many_specs 112 ; 113 114Many_specs: 115 Many_specs Spec 116 | 117 /* lambda */ 118 ; 119 120Spec: 121 Device_spec SEMICOLON 122 | 123 Config_spec SEMICOLON 124 | 125 INCLUDE ID SEMICOLON 126 = { 127 if (incignore == 0) 128 include($2, 0); 129 }; 130 | 131 FILES ID SEMICOLON 132 = { newfile($2); }; 133 | 134 SEMICOLON 135 | 136 error SEMICOLON 137 ; 138 139Config_spec: 140 ARCH Save_id 141 = { 142 if (machinename != NULL && !eq($2, machinename)) 143 errx(1, "%s:%d: only one machine directive is allowed", 144 yyfile, yyline); 145 machinename = $2; 146 machinearch = $2; 147 } | 148 ARCH Save_id Save_id 149 = { 150 if (machinename != NULL && 151 !(eq($2, machinename) && eq($3, machinearch))) 152 errx(1, "%s:%d: only one machine directive is allowed", 153 yyfile, yyline); 154 machinename = $2; 155 machinearch = $3; 156 } | 157 CPU Save_id 158 = { 159 struct cputype *cp = 160 (struct cputype *)calloc(1, sizeof (struct cputype)); 161 cp->cpu_name = $2; 162 SLIST_INSERT_HEAD(&cputype, cp, cpu_next); 163 } | 164 NOCPU Save_id 165 = { 166 struct cputype *cp, *cp2; 167 SLIST_FOREACH_SAFE(cp, &cputype, cpu_next, cp2) { 168 if (eq(cp->cpu_name, $2)) { 169 SLIST_REMOVE(&cputype, cp, cputype, cpu_next); 170 free(cp); 171 } 172 } 173 } | 174 OPTIONS Opt_list 175 | 176 NOOPTION Save_id 177 = { rmopt_schedule(&opt, $2); } | 178 MAKEOPTIONS Mkopt_list 179 | 180 NOMAKEOPTION Save_id 181 = { rmopt_schedule(&mkopt, $2); } | 182 IDENT ID 183 = { ident = $2; } | 184 System_spec 185 | 186 MAXUSERS NUMBER 187 = { maxusers = $2; } | 188 PROFILE NUMBER 189 = { profiling = $2; } | 190 ENV ID 191 = { 192 env = $2; 193 envmode = 1; 194 } | 195 HINTS ID 196 = { 197 struct hint *hint; 198 199 hint = (struct hint *)calloc(1, sizeof (struct hint)); 200 hint->hint_name = $2; 201 STAILQ_INSERT_TAIL(&hints, hint, hint_next); 202 hintmode = 1; 203 } 204 205System_spec: 206 CONFIG System_id System_parameter_list 207 = { errx(1, "%s:%d: root/dump/swap specifications obsolete", 208 yyfile, yyline);} 209 | 210 CONFIG System_id 211 ; 212 213System_id: 214 Save_id 215 = { newopt(&mkopt, ns("KERNEL"), $1); }; 216 217System_parameter_list: 218 System_parameter_list ID 219 | ID 220 ; 221 222Opt_list: 223 Opt_list COMMA Option 224 | 225 Option 226 ; 227 228Option: 229 Save_id 230 = { 231 newopt(&opt, $1, NULL); 232 if (strchr($1, '=') != NULL) 233 errx(1, "%s:%d: The `=' in options should not be " 234 "quoted", yyfile, yyline); 235 } | 236 Save_id EQUALS Opt_value 237 = { 238 newopt(&opt, $1, $3); 239 } ; 240 241Opt_value: 242 ID 243 = { $$ = $1; } | 244 NUMBER 245 = { 246 char buf[80]; 247 248 (void) snprintf(buf, sizeof(buf), "%d", $1); 249 $$ = ns(buf); 250 } ; 251 252Save_id: 253 ID 254 = { $$ = $1; } 255 ; 256 257Mkopt_list: 258 Mkopt_list COMMA Mkoption 259 | 260 Mkoption 261 ; 262 263Mkoption: 264 Save_id 265 = { newopt(&mkopt, $1, ns("")); } | 266 Save_id EQUALS Opt_value 267 = { newopt(&mkopt, $1, $3); } ; 268 269Dev: 270 ID 271 = { $$ = $1; } 272 ; 273 274Device_spec: 275 DEVICE Dev_list 276 | 277 NODEVICE NoDev_list 278 ; 279 280Dev_list: 281 Dev_list COMMA Device 282 | 283 Device 284 ; 285 286NoDev_list: 287 NoDev_list COMMA NoDevice 288 | 289 NoDevice 290 ; 291 292Device: 293 Dev 294 = { 295 newopt(&opt, devopt($1), ns("1")); 296 /* and the device part */ 297 newdev($1); 298 } 299 300NoDevice: 301 Dev 302 = { 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 nl->f_name = name; 344 STAILQ_INSERT_TAIL(&fntab, nl, f_next); 345} 346 347/* 348 * Find a device in the list of devices. 349 */ 350static struct device * 351finddev(struct device_head *dlist, char *name) 352{ 353 struct device *dp; 354 355 STAILQ_FOREACH(dp, dlist, d_next) 356 if (eq(dp->d_name, name)) 357 return (dp); 358 359 return (NULL); 360} 361 362/* 363 * Add a device to the list of devices. 364 */ 365static void 366newdev(char *name) 367{ 368 struct device *np; 369 370 if (finddev(&dtab, name)) { 371 printf("WARNING: duplicate device `%s' encountered.\n", name); 372 return; 373 } 374 375 np = (struct device *) calloc(1, sizeof *np); 376 np->d_name = name; 377 STAILQ_INSERT_TAIL(&dtab, np, d_next); 378} 379 380/* 381 * Schedule a device to removal. 382 */ 383static void 384rmdev_schedule(struct device_head *dh, char *name) 385{ 386 struct device *dp; 387 388 dp = finddev(dh, name); 389 if (dp != NULL) { 390 STAILQ_REMOVE(dh, dp, device, d_next); 391 free(dp->d_name); 392 free(dp); 393 } 394} 395 396/* 397 * Find an option in the list of options. 398 */ 399static struct opt * 400findopt(struct opt_head *list, char *name) 401{ 402 struct opt *op; 403 404 SLIST_FOREACH(op, list, op_next) 405 if (eq(op->op_name, name)) 406 return (op); 407 408 return (NULL); 409} 410 411/* 412 * Add an option to the list of options. 413 */ 414static void 415newopt(struct opt_head *list, char *name, char *value) 416{ 417 struct opt *op; 418 419 /* 420 * Ignore inclusions listed explicitly for configuration files. 421 */ 422 if (eq(name, OPT_AUTOGEN)) { 423 incignore = 1; 424 return; 425 } 426 427 if (findopt(list, name)) { 428 printf("WARNING: duplicate option `%s' encountered.\n", name); 429 return; 430 } 431 432 op = (struct opt *)calloc(1, sizeof (struct opt)); 433 op->op_name = name; 434 op->op_ownfile = 0; 435 op->op_value = value; 436 SLIST_INSERT_HEAD(list, op, op_next); 437} 438 439/* 440 * Remove an option from the list of options. 441 */ 442static void 443rmopt_schedule(struct opt_head *list, char *name) 444{ 445 struct opt *op; 446 447 op = findopt(list, name); 448 if (op != NULL) { 449 SLIST_REMOVE(list, op, opt, op_next); 450 free(op->op_name); 451 free(op); 452 } 453} 454