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 * @(#)lang.l 8.1 (Berkeley) 6/6/93 33 * $FreeBSD$ 34 */ 35 36 #include <assert.h> 37 #include <ctype.h> 38 #include <err.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include "y.tab.h" 42 #include "config.h" 43 44 /* 45 * Data for returning to previous files from include files. 46 */ 47 struct incl { 48 struct incl *in_prev; /* previous includes in effect, if any */ 49 YY_BUFFER_STATE in_buf; /* previous lex state */ 50 const char *in_fname; /* previous file name */ 51 int in_lineno; /* previous line number */ 52 int in_ateof; /* token to insert at EOF */ 53 }; 54 static struct incl *inclp; 55 static const char *lastfile; 56 57 /* 58 * Key word table 59 */ 60 61 struct kt { 62 const char *kt_name; 63 int kt_val; 64 } key_words[] = { 65 { "config", CONFIG }, 66 { "cpu", CPU }, 67 { "nocpu", NOCPU }, 68 { "device", DEVICE }, 69 { "devices", DEVICE }, 70 { "nodevice", NODEVICE }, 71 { "nodevices", NODEVICE }, 72 { "env", ENV }, 73 { "hints", HINTS }, 74 { "ident", IDENT }, 75 { "machine", ARCH }, /* MACHINE is defined in /sys/param.h */ 76 { "makeoption", MAKEOPTIONS }, 77 { "makeoptions", MAKEOPTIONS }, 78 { "nomakeoption", NOMAKEOPTION }, 79 { "nomakeoptions", NOMAKEOPTION }, 80 { "maxusers", MAXUSERS }, 81 { "profile", PROFILE }, 82 { "option", OPTIONS }, 83 { "options", OPTIONS }, 84 { "nooption", NOOPTION }, 85 { "nooptions", NOOPTION }, 86 { "include", INCLUDE }, 87 { "files", FILES }, 88 { 0, 0 }, 89 }; 90 91 92 static int endinclude(void); 93 int include(const char *, int); 94 int kw_lookup(char *); 95 unsigned int octal(const char *); 96 unsigned int hex(const char *); 97 int yyerror(const char *); 98 99 #define YY_DECL int yylex(void) 100 %} 101 102 %option nounput 103 %option noinput 104 105 ID [A-Za-z_][-A-Za-z_0-9]* 106 PATH [./][-/.%^A-Za-z_0-9]+ 107 %START TOEOL 108 %% 109 {ID} { 110 int i; 111 112 BEGIN 0; 113 if ((i = kw_lookup(yytext)) == -1) 114 { 115 yylval.str = strdup(yytext); 116 return ID; 117 } 118 return i; 119 } 120 \\\"[^"]+\\\" { 121 BEGIN 0; 122 yytext[yyleng-2] = '"'; 123 yytext[yyleng-1] = '\0'; 124 yylval.str = strdup(yytext + 1); 125 return ID; 126 } 127 \"[^"]+\" { 128 BEGIN 0; 129 yytext[yyleng-1] = '\0'; 130 yylval.str = strdup(yytext + 1); 131 return ID; 132 } 133 <TOEOL>[^# \t\n]* { 134 BEGIN 0; 135 yylval.str = strdup(yytext); 136 return ID; 137 } 138 0[0-7]* { 139 yylval.val = octal(yytext); 140 return NUMBER; 141 } 142 0x[0-9a-fA-F]+ { 143 yylval.val = hex(yytext); 144 return NUMBER; 145 } 146 -?[1-9][0-9]* { 147 yylval.val = atoi(yytext); 148 return NUMBER; 149 } 150 "?" { 151 yylval.val = -1; 152 return NUMBER; 153 } 154 \n/[ \t] { 155 yyline++; 156 } 157 \n { 158 yyline++; 159 return SEMICOLON; 160 } 161 #.* { /* Ignored (comment) */; } 162 [ \t\f]* { /* Ignored (white space) */; } 163 ";" { return SEMICOLON; } 164 "," { return COMMA; } 165 "=" { BEGIN TOEOL; return EQUALS; } 166 "+=" { BEGIN TOEOL; return PLUSEQUALS; } 167 <<EOF>> { 168 int tok; 169 170 if (inclp == NULL) 171 return YY_NULL; 172 tok = endinclude(); 173 if (tok != 0) 174 return tok; 175 /* otherwise continue scanning */ 176 } 177 {PATH} { 178 BEGIN 0; 179 yylval.str = strdup(yytext); 180 return PATH; 181 } 182 . { return yytext[0]; } 183 184 %% 185 /* 186 * kw_lookup 187 * Look up a string in the keyword table. Returns a -1 if the 188 * string is not a keyword otherwise it returns the keyword number 189 */ 190 191 int 192 kw_lookup(char *word) 193 { 194 struct kt *kp; 195 196 for (kp = key_words; kp->kt_name != 0; kp++) 197 if (eq(word, kp->kt_name)) 198 return kp->kt_val; 199 return -1; 200 } 201 202 /* 203 * Number conversion routines 204 */ 205 206 unsigned int 207 octal(const char *str) 208 { 209 unsigned int num; 210 211 (void) sscanf(str, "%o", &num); 212 return num; 213 } 214 215 unsigned int 216 hex(const char *str) 217 { 218 unsigned int num; 219 220 (void) sscanf(str+2, "%x", &num); 221 return num; 222 } 223 224 void 225 cfgfile_add(const char *fname) 226 { 227 struct cfgfile *cf; 228 229 cf = calloc(1, sizeof(*cf)); 230 if (cf == NULL) 231 err(EXIT_FAILURE, "calloc"); 232 assert(cf != NULL); 233 asprintf(&cf->cfg_path, "%s", fname); 234 STAILQ_INSERT_TAIL(&cfgfiles, cf, cfg_next); 235 } 236 237 void 238 cfgfile_removeall(void) 239 { 240 struct cfgfile *cf; 241 242 while (!STAILQ_EMPTY(&cfgfiles)) { 243 cf = STAILQ_FIRST(&cfgfiles); 244 STAILQ_REMOVE_HEAD(&cfgfiles, cfg_next); 245 if (cf->cfg_path != NULL) 246 free(cf->cfg_path); 247 free(cf); 248 } 249 } 250 251 /* 252 * Open the named file for inclusion at the current point. Returns 0 on 253 * success (file opened and previous state pushed), nonzero on failure 254 * (fopen failed, complaint made). The `ateof' parameter controls the 255 * token to be inserted at the end of the include file. If ateof == 0, 256 * then nothing is inserted. 257 */ 258 int 259 include(const char *fname, int ateof) 260 { 261 FILE *fp; 262 struct incl *in; 263 struct includepath* ipath; 264 char *fnamebuf; 265 266 fnamebuf = NULL; 267 fp = fopen(fname, "r"); 268 if (fp == NULL && fname[0] != '.' && fname[0] != '/') { 269 asprintf(&fnamebuf, "../../conf/%s", fname); 270 if (fnamebuf != NULL) { 271 fp = fopen(fnamebuf, "r"); 272 if (fp == NULL) 273 free(fnamebuf); 274 } 275 } 276 if (fp == NULL) { 277 SLIST_FOREACH(ipath, &includepath, path_next) { 278 asprintf(&fnamebuf, "%s/%s", ipath->path, fname); 279 if (fnamebuf != NULL) { 280 fp = fopen(fnamebuf, "r"); 281 } 282 if (fp != NULL) 283 break; 284 free(fnamebuf); 285 } 286 } 287 if (fp == NULL) { 288 yyerror("cannot open included file"); 289 return (-1); 290 } 291 cfgfile_add(fnamebuf == NULL ? fname : fnamebuf); 292 in = malloc(sizeof(*in)); 293 assert(in != NULL); 294 in->in_prev = inclp; 295 in->in_buf = YY_CURRENT_BUFFER; 296 in->in_fname = yyfile; 297 in->in_lineno = yyline; 298 in->in_ateof = ateof; 299 inclp = in; 300 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE)); 301 yyfile = fname; 302 yyline = 0; 303 return (0); 304 } 305 306 /* 307 * Terminate the most recent inclusion. 308 */ 309 static int 310 endinclude(void) 311 { 312 struct incl *in; 313 int ateof; 314 315 in = inclp; 316 assert(in != NULL); 317 inclp = in->in_prev; 318 lastfile = yyfile; 319 yy_delete_buffer(YY_CURRENT_BUFFER); 320 (void)fclose(yyin); 321 yy_switch_to_buffer(in->in_buf); 322 yyfile = in->in_fname; 323 yyline = in->in_lineno; 324 ateof = in->in_ateof; 325 free(in); 326 327 return (ateof); 328 } 329