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 { "profile", PROFILE },
83 { "option", OPTIONS },
84 { "options", OPTIONS },
85 { "nooption", NOOPTION },
86 { "nooptions", NOOPTION },
87 { "include", INCLUDE },
93 static int endinclude(void);
94 int include(const char *, int);
95 int kw_lookup(char *);
96 unsigned int octal(const char *);
97 unsigned int hex(const char *);
98 int yyerror(const char *);
100 #define YY_DECL int yylex(void)
106 ID [A-Za-z_][-A-Za-z_0-9]*
107 PATH [./][-/.%^A-Za-z_0-9]+
113 yylval.str = strdup(yytext);
120 if ((i = kw_lookup(yytext)) == -1)
122 yylval.str = strdup(yytext);
125 /* We'll glom onto the rest of an envvar line */
132 yytext[yyleng-2] = '"';
133 yytext[yyleng-1] = '\0';
134 yylval.str = strdup(yytext + 1);
139 yytext[yyleng-1] = '\0';
140 yylval.str = strdup(yytext + 1);
145 yylval.str = strdup(yytext);
149 yylval.val = octal(yytext);
153 yylval.val = hex(yytext);
157 yylval.val = atoi(yytext);
171 #.* { /* Ignored (comment) */; }
172 [ \t\f]* { /* Ignored (white space) */; }
173 ";" { return SEMICOLON; }
174 "," { return COMMA; }
175 "=" { BEGIN TOEOL; return EQUALS; }
176 "+=" { BEGIN TOEOL; return PLUSEQUALS; }
185 /* otherwise continue scanning */
189 yylval.str = strdup(yytext);
192 . { return yytext[0]; }
197 * Look up a string in the keyword table. Returns a -1 if the
198 * string is not a keyword otherwise it returns the keyword number
202 kw_lookup(char *word)
206 for (kp = key_words; kp->kt_name != 0; kp++)
207 if (eq(word, kp->kt_name))
213 * Number conversion routines
217 octal(const char *str)
221 (void) sscanf(str, "%o", &num);
230 (void) sscanf(str+2, "%x", &num);
235 cfgfile_add(const char *fname)
239 cf = calloc(1, sizeof(*cf));
241 err(EXIT_FAILURE, "calloc");
243 asprintf(&cf->cfg_path, "%s", fname);
244 STAILQ_INSERT_TAIL(&cfgfiles, cf, cfg_next);
248 cfgfile_removeall(void)
252 while (!STAILQ_EMPTY(&cfgfiles)) {
253 cf = STAILQ_FIRST(&cfgfiles);
254 STAILQ_REMOVE_HEAD(&cfgfiles, cfg_next);
255 if (cf->cfg_path != NULL)
262 * Open the named file for inclusion at the current point. Returns 0 on
263 * success (file opened and previous state pushed), nonzero on failure
264 * (fopen failed, complaint made). The `ateof' parameter controls the
265 * token to be inserted at the end of the include file. If ateof == 0,
266 * then nothing is inserted.
269 include(const char *fname, int ateof)
273 struct includepath* ipath;
277 fp = fopen(fname, "r");
278 if (fp == NULL && fname[0] != '.' && fname[0] != '/') {
279 asprintf(&fnamebuf, "../../conf/%s", fname);
280 if (fnamebuf != NULL) {
281 fp = fopen(fnamebuf, "r");
287 SLIST_FOREACH(ipath, &includepath, path_next) {
288 asprintf(&fnamebuf, "%s/%s", ipath->path, fname);
289 if (fnamebuf != NULL) {
290 fp = fopen(fnamebuf, "r");
298 yyerror("cannot open included file");
301 cfgfile_add(fnamebuf == NULL ? fname : fnamebuf);
302 in = malloc(sizeof(*in));
305 in->in_buf = YY_CURRENT_BUFFER;
306 in->in_fname = yyfile;
307 in->in_lineno = yyline;
308 in->in_ateof = ateof;
310 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
317 * Terminate the most recent inclusion.
329 yy_delete_buffer(YY_CURRENT_BUFFER);
331 yy_switch_to_buffer(in->in_buf);
332 yyfile = in->in_fname;
333 yyline = in->in_lineno;
334 ateof = in->in_ateof;