1 %{ 2 /*- 3 * SPDX-License-Identifier: BSD-3-Clause 4 * 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <assert.h> 34 #include <ctype.h> 35 #include <err.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include "y.tab.h" 39 #include "config.h" 40 41 /* 42 * Data for returning to previous files from include files. 43 */ 44 struct incl { 45 struct incl *in_prev; /* previous includes in effect, if any */ 46 YY_BUFFER_STATE in_buf; /* previous lex state */ 47 const char *in_fname; /* previous file name */ 48 int in_lineno; /* previous line number */ 49 int in_ateof; /* token to insert at EOF */ 50 }; 51 static struct incl *inclp; 52 static const char *lastfile; 53 54 /* 55 * Key word table 56 */ 57 58 struct kt { 59 const char *kt_name; 60 int kt_val; 61 } key_words[] = { 62 { "cpu", CPU }, 63 { "nocpu", NOCPU }, 64 { "device", DEVICE }, 65 { "devices", DEVICE }, 66 { "nodevice", NODEVICE }, 67 { "nodevices", NODEVICE }, 68 { "env", ENV }, 69 { "envvar", ENVVAR }, 70 { "hints", HINTS }, 71 { "ident", IDENT }, 72 { "machine", ARCH }, /* MACHINE is defined in /sys/param.h */ 73 { "makeoption", MAKEOPTIONS }, 74 { "makeoptions", MAKEOPTIONS }, 75 { "nomakeoption", NOMAKEOPTION }, 76 { "nomakeoptions", NOMAKEOPTION }, 77 { "maxusers", MAXUSERS }, 78 { "option", OPTIONS }, 79 { "options", OPTIONS }, 80 { "nooption", NOOPTION }, 81 { "nooptions", NOOPTION }, 82 { "include", INCLUDE }, 83 { "includeoptions", INCLUDEOPTIONS }, 84 { "files", FILES }, 85 { 0, 0 }, 86 }; 87 88 89 static int endinclude(void); 90 int include(const char *, int); 91 int kw_lookup(char *); 92 unsigned int octal(const char *); 93 unsigned int hex(const char *); 94 int yyerror(const char *); 95 96 #define YY_DECL int yylex(void) 97 %} 98 99 %option nounput 100 %option noinput 101 102 ID [A-Za-z_][-A-Za-z_0-9]* 103 PATH [./][-/.%^A-Za-z_0-9]+ 104 %START TOEOL 105 %START ENVC 106 %% 107 <ENVC>[^#\n]* { 108 BEGIN 0; 109 yylval.str = strdup(yytext); 110 return ENVLINE; 111 } 112 {ID} { 113 int i; 114 115 BEGIN 0; 116 if ((i = kw_lookup(yytext)) == -1) 117 { 118 yylval.str = strdup(yytext); 119 return ID; 120 } 121 /* We'll glom onto the rest of an envvar line */ 122 if (i == ENVVAR) 123 BEGIN ENVC; 124 return i; 125 } 126 \\\"[^"]*\\\" { 127 BEGIN 0; 128 yytext[yyleng-2] = '"'; 129 yytext[yyleng-1] = '\0'; 130 yylval.str = strdup(yytext + 1); 131 return ID; 132 } 133 \"[^"]+\" { 134 BEGIN 0; 135 yytext[yyleng-1] = '\0'; 136 yylval.str = strdup(yytext + 1); 137 return ID; 138 } 139 <TOEOL>[^# \t\n]* { 140 BEGIN 0; 141 yylval.str = strdup(yytext); 142 return ID; 143 } 144 0[0-7]* { 145 yylval.val = octal(yytext); 146 return NUMBER; 147 } 148 0x[0-9a-fA-F]+ { 149 yylval.val = hex(yytext); 150 return NUMBER; 151 } 152 -?[1-9][0-9]* { 153 yylval.val = atoi(yytext); 154 return NUMBER; 155 } 156 "?" { 157 yylval.val = -1; 158 return NUMBER; 159 } 160 \n/[ \t] { 161 yyline++; 162 } 163 \n { 164 yyline++; 165 return SEMICOLON; 166 } 167 #.* { /* Ignored (comment) */; } 168 [ \t\f]* { /* Ignored (white space) */; } 169 ";" { return SEMICOLON; } 170 "," { return COMMA; } 171 "=" { BEGIN TOEOL; return EQUALS; } 172 "+=" { BEGIN TOEOL; return PLUSEQUALS; } 173 <<EOF>> { 174 int tok; 175 176 if (inclp == NULL) 177 return YY_NULL; 178 tok = endinclude(); 179 if (tok != 0) 180 return tok; 181 /* otherwise continue scanning */ 182 } 183 {PATH} { 184 BEGIN 0; 185 yylval.str = strdup(yytext); 186 return PATH; 187 } 188 . { return yytext[0]; } 189 190 %% 191 /* 192 * kw_lookup 193 * Look up a string in the keyword table. Returns a -1 if the 194 * string is not a keyword otherwise it returns the keyword number 195 */ 196 197 int 198 kw_lookup(char *word) 199 { 200 struct kt *kp; 201 202 for (kp = key_words; kp->kt_name != 0; kp++) 203 if (eq(word, kp->kt_name)) 204 return kp->kt_val; 205 return -1; 206 } 207 208 /* 209 * Number conversion routines 210 */ 211 212 unsigned int 213 octal(const char *str) 214 { 215 unsigned int num; 216 217 (void) sscanf(str, "%o", &num); 218 return num; 219 } 220 221 unsigned int 222 hex(const char *str) 223 { 224 unsigned int num; 225 226 (void) sscanf(str+2, "%x", &num); 227 return num; 228 } 229 230 void 231 cfgfile_add(const char *fname) 232 { 233 struct cfgfile *cf; 234 235 cf = calloc(1, sizeof(*cf)); 236 if (cf == NULL) 237 err(EXIT_FAILURE, "calloc"); 238 assert(cf != NULL); 239 asprintf(&cf->cfg_path, "%s", fname); 240 STAILQ_INSERT_TAIL(&cfgfiles, cf, cfg_next); 241 } 242 243 void 244 cfgfile_removeall(void) 245 { 246 struct cfgfile *cf; 247 248 while (!STAILQ_EMPTY(&cfgfiles)) { 249 cf = STAILQ_FIRST(&cfgfiles); 250 STAILQ_REMOVE_HEAD(&cfgfiles, cfg_next); 251 if (cf->cfg_path != NULL) 252 free(cf->cfg_path); 253 free(cf); 254 } 255 } 256 257 /* 258 * Open the named file for inclusion at the current point. Returns 0 on 259 * success (file opened and previous state pushed), nonzero on failure 260 * (fopen failed, complaint made). The `ateof' parameter controls the 261 * token to be inserted at the end of the include file. If ateof == 0, 262 * then nothing is inserted. 263 */ 264 int 265 include(const char *fname, int ateof) 266 { 267 FILE *fp; 268 struct incl *in; 269 struct includepath* ipath; 270 char *fnamebuf; 271 272 fnamebuf = NULL; 273 fp = fopen(fname, "r"); 274 if (fp == NULL && fname[0] != '.' && fname[0] != '/') { 275 asprintf(&fnamebuf, "../../conf/%s", fname); 276 if (fnamebuf != NULL) { 277 fp = fopen(fnamebuf, "r"); 278 if (fp == NULL) 279 free(fnamebuf); 280 } 281 } 282 if (fp == NULL) { 283 SLIST_FOREACH(ipath, &includepath, path_next) { 284 asprintf(&fnamebuf, "%s/%s", ipath->path, fname); 285 if (fnamebuf != NULL) { 286 fp = fopen(fnamebuf, "r"); 287 } 288 if (fp != NULL) 289 break; 290 free(fnamebuf); 291 } 292 } 293 if (fp == NULL) { 294 yyerror("cannot open included file"); 295 return (-1); 296 } 297 cfgfile_add(fnamebuf == NULL ? fname : fnamebuf); 298 free(fnamebuf); 299 300 in = malloc(sizeof(*in)); 301 assert(in != NULL); 302 in->in_prev = inclp; 303 in->in_buf = YY_CURRENT_BUFFER; 304 in->in_fname = yyfile; 305 in->in_lineno = yyline; 306 in->in_ateof = ateof; 307 inclp = in; 308 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE)); 309 yyfile = fname; 310 yyline = 0; 311 return (0); 312 } 313 314 /* 315 * Terminate the most recent inclusion. 316 */ 317 static int 318 endinclude(void) 319 { 320 struct incl *in; 321 int ateof; 322 323 in = inclp; 324 assert(in != NULL); 325 inclp = in->in_prev; 326 lastfile = yyfile; 327 yy_delete_buffer(YY_CURRENT_BUFFER); 328 (void)fclose(yyin); 329 yy_switch_to_buffer(in->in_buf); 330 yyfile = in->in_fname; 331 yyline = in->in_lineno; 332 ateof = in->in_ateof; 333 free(in); 334 335 return (ateof); 336 } 337