]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/config/lang.l
import nvi 2.2.0-3bbdfe4
[FreeBSD/FreeBSD.git] / usr.sbin / config / lang.l
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         { "envvar",     ENVVAR },
74         { "hints",      HINTS },
75         { "ident",      IDENT },
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 },
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 %START ENVC
109 %%
110 <ENVC>[^#\n]*   {
111                         BEGIN 0;
112                         yylval.str = strdup(yytext);
113                         return ENVLINE;
114                 }
115 {ID}            {
116                         int i;
117
118                         BEGIN 0;
119                         if ((i = kw_lookup(yytext)) == -1)
120                         {
121                                 yylval.str = strdup(yytext);
122                                 return ID;
123                         }
124                         /* We'll glom onto the rest of an envvar line */
125                         if (i == ENVVAR)
126                                 BEGIN ENVC;
127                         return i;
128                 }
129 \\\"[^"]*\\\"   {
130                         BEGIN 0;
131                         yytext[yyleng-2] = '"';
132                         yytext[yyleng-1] = '\0';
133                         yylval.str = strdup(yytext + 1);
134                         return ID;
135                 }
136 \"[^"]+\"       {
137                         BEGIN 0;
138                         yytext[yyleng-1] = '\0';
139                         yylval.str = strdup(yytext + 1);
140                         return ID;
141                 }
142 <TOEOL>[^# \t\n]*       {
143                         BEGIN 0;
144                         yylval.str = strdup(yytext);
145                         return ID;
146                 }
147 0[0-7]*         {
148                         yylval.val = octal(yytext);
149                         return NUMBER;
150                 }
151 0x[0-9a-fA-F]+  {
152                         yylval.val = hex(yytext);
153                         return NUMBER;
154                 }
155 -?[1-9][0-9]*   {
156                         yylval.val = atoi(yytext);
157                         return NUMBER;
158                 }
159 "?"             {
160                         yylval.val = -1;
161                         return NUMBER;
162                 }
163 \n/[ \t]        {
164                         yyline++;
165                 }
166 \n              {
167                         yyline++;
168                         return SEMICOLON;
169                 }
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; }
176 <<EOF>>         {
177                         int tok;
178
179                         if (inclp == NULL)
180                                 return YY_NULL;
181                         tok = endinclude();
182                         if (tok != 0)
183                                 return tok;
184                         /* otherwise continue scanning */
185                 }
186 {PATH}          {
187                         BEGIN 0;
188                         yylval.str = strdup(yytext);
189                         return PATH;
190                 }
191 .               {       return yytext[0];               }
192
193 %%
194 /*
195  * kw_lookup
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
198  */
199
200 int
201 kw_lookup(char *word)
202 {
203         struct kt *kp;
204
205         for (kp = key_words; kp->kt_name != 0; kp++)
206                 if (eq(word, kp->kt_name))
207                         return kp->kt_val;
208         return -1;
209 }
210
211 /*
212  * Number conversion routines
213  */
214
215 unsigned int
216 octal(const char *str)
217 {
218         unsigned int num;
219
220         (void) sscanf(str, "%o", &num);
221         return num;
222 }
223
224 unsigned int
225 hex(const char *str)
226 {
227         unsigned int num;
228
229         (void) sscanf(str+2, "%x", &num);
230         return num;
231 }
232
233 void
234 cfgfile_add(const char *fname)
235 {
236         struct cfgfile *cf;
237
238         cf = calloc(1, sizeof(*cf));
239         if (cf == NULL)
240                 err(EXIT_FAILURE, "calloc");
241         assert(cf != NULL);
242         asprintf(&cf->cfg_path, "%s", fname);
243         STAILQ_INSERT_TAIL(&cfgfiles, cf, cfg_next);
244 }
245
246 void
247 cfgfile_removeall(void)
248 {
249         struct cfgfile *cf;
250
251         while (!STAILQ_EMPTY(&cfgfiles)) {
252                 cf = STAILQ_FIRST(&cfgfiles);
253                 STAILQ_REMOVE_HEAD(&cfgfiles, cfg_next);
254                 if (cf->cfg_path != NULL)
255                         free(cf->cfg_path);
256                 free(cf);
257         }
258 }
259
260 /*
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.
266  */
267 int
268 include(const char *fname, int ateof)
269 {
270         FILE *fp;
271         struct incl *in;
272         struct includepath* ipath;
273         char *fnamebuf;
274
275         fnamebuf = NULL;
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");
281                         if (fp == NULL)
282                                 free(fnamebuf);
283                 }
284         }
285         if (fp == NULL) {
286                 SLIST_FOREACH(ipath, &includepath, path_next) {
287                         asprintf(&fnamebuf, "%s/%s", ipath->path, fname);
288                         if (fnamebuf != NULL) {
289                                 fp = fopen(fnamebuf, "r");
290                         }
291                         if (fp != NULL)
292                                 break;
293                         free(fnamebuf);
294                 }
295         }
296         if (fp == NULL) {
297                 yyerror("cannot open included file");
298                 return (-1);
299         }
300         cfgfile_add(fnamebuf == NULL ? fname : fnamebuf);
301         in = malloc(sizeof(*in));
302         assert(in != NULL);
303         in->in_prev = inclp;
304         in->in_buf = YY_CURRENT_BUFFER;
305         in->in_fname = yyfile;
306         in->in_lineno = yyline;
307         in->in_ateof = ateof;
308         inclp = in;
309         yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
310         yyfile = fname;
311         yyline = 0;
312         return (0);
313 }
314
315 /*
316  * Terminate the most recent inclusion.
317  */
318 static int
319 endinclude(void)
320 {
321         struct incl *in;
322         int ateof;
323
324         in = inclp;
325         assert(in != NULL);
326         inclp = in->in_prev;
327         lastfile = yyfile;
328         yy_delete_buffer(YY_CURRENT_BUFFER);
329         (void)fclose(yyin);
330         yy_switch_to_buffer(in->in_buf);
331         yyfile = in->in_fname;
332         yyline = in->in_lineno;
333         ateof  = in->in_ateof;
334         free(in);
335
336         return (ateof);
337 }