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