3 * SPDX-License-Identifier: BSD-3-Clause
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * @(#)lang.l 8.1 (Berkeley) 6/6/93
45 * Data for returning to previous files from include files.
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 */
54 static struct incl *inclp;
55 static const char *lastfile;
69 { "devices", DEVICE },
70 { "nodevice", NODEVICE },
71 { "nodevices", NODEVICE },
76 { "machine", ARCH }, /* MACHINE is defined in /sys/param.h */
77 { "makeoption", MAKEOPTIONS },
78 { "makeoptions", MAKEOPTIONS },
79 { "nomakeoption", NOMAKEOPTION },
80 { "nomakeoptions", NOMAKEOPTION },
81 { "maxusers", MAXUSERS },
82 { "option", OPTIONS },
83 { "options", OPTIONS },
84 { "nooption", NOOPTION },
85 { "nooptions", NOOPTION },
86 { "include", INCLUDE },
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 *);
99 #define YY_DECL int yylex(void)
105 ID [A-Za-z_][-A-Za-z_0-9]*
106 PATH [./][-/.%^A-Za-z_0-9]+
112 yylval.str = strdup(yytext);
119 if ((i = kw_lookup(yytext)) == -1)
121 yylval.str = strdup(yytext);
124 /* We'll glom onto the rest of an envvar line */
131 yytext[yyleng-2] = '"';
132 yytext[yyleng-1] = '\0';
133 yylval.str = strdup(yytext + 1);
138 yytext[yyleng-1] = '\0';
139 yylval.str = strdup(yytext + 1);
144 yylval.str = strdup(yytext);
148 yylval.val = octal(yytext);
152 yylval.val = hex(yytext);
156 yylval.val = atoi(yytext);
170 #.* { /* Ignored (comment) */; }
171 [ \t\f]* { /* Ignored (white space) */; }
172 ";" { return SEMICOLON; }
173 "," { return COMMA; }
174 "=" { BEGIN TOEOL; return EQUALS; }
175 "+=" { BEGIN TOEOL; return PLUSEQUALS; }
184 /* otherwise continue scanning */
188 yylval.str = strdup(yytext);
191 . { return yytext[0]; }
196 * Look up a string in the keyword table. Returns a -1 if the
197 * string is not a keyword otherwise it returns the keyword number
201 kw_lookup(char *word)
205 for (kp = key_words; kp->kt_name != 0; kp++)
206 if (eq(word, kp->kt_name))
212 * Number conversion routines
216 octal(const char *str)
220 (void) sscanf(str, "%o", &num);
229 (void) sscanf(str+2, "%x", &num);
234 cfgfile_add(const char *fname)
238 cf = calloc(1, sizeof(*cf));
240 err(EXIT_FAILURE, "calloc");
242 asprintf(&cf->cfg_path, "%s", fname);
243 STAILQ_INSERT_TAIL(&cfgfiles, cf, cfg_next);
247 cfgfile_removeall(void)
251 while (!STAILQ_EMPTY(&cfgfiles)) {
252 cf = STAILQ_FIRST(&cfgfiles);
253 STAILQ_REMOVE_HEAD(&cfgfiles, cfg_next);
254 if (cf->cfg_path != NULL)
261 * Open the named file for inclusion at the current point. Returns 0 on
262 * success (file opened and previous state pushed), nonzero on failure
263 * (fopen failed, complaint made). The `ateof' parameter controls the
264 * token to be inserted at the end of the include file. If ateof == 0,
265 * then nothing is inserted.
268 include(const char *fname, int ateof)
272 struct includepath* ipath;
276 fp = fopen(fname, "r");
277 if (fp == NULL && fname[0] != '.' && fname[0] != '/') {
278 asprintf(&fnamebuf, "../../conf/%s", fname);
279 if (fnamebuf != NULL) {
280 fp = fopen(fnamebuf, "r");
286 SLIST_FOREACH(ipath, &includepath, path_next) {
287 asprintf(&fnamebuf, "%s/%s", ipath->path, fname);
288 if (fnamebuf != NULL) {
289 fp = fopen(fnamebuf, "r");
297 yyerror("cannot open included file");
300 cfgfile_add(fnamebuf == NULL ? fname : fnamebuf);
301 in = malloc(sizeof(*in));
304 in->in_buf = YY_CURRENT_BUFFER;
305 in->in_fname = yyfile;
306 in->in_lineno = yyline;
307 in->in_ateof = ateof;
309 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
316 * Terminate the most recent inclusion.
328 yy_delete_buffer(YY_CURRENT_BUFFER);
330 yy_switch_to_buffer(in->in_buf);
331 yyfile = in->in_fname;
332 yyline = in->in_lineno;
333 ateof = in->in_ateof;