1%{
2/*	$OpenBSD: scan.l,v 1.25 2023/06/16 16:14:34 tb Exp $	*/
3/*	$NetBSD: scan.l,v 1.13 1997/02/02 21:12:37 thorpej Exp $	*/
4
5/*
6 * Copyright (c) 1992, 1993
7 *	The Regents of the University of California.  All rights reserved.
8 *
9 * This software was developed by the Computer Systems Engineering group
10 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11 * contributed to Berkeley.
12 *
13 * All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Lawrence Berkeley Laboratories.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 *    may be used to endorse or promote products derived from this software
28 *    without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 *	from: @(#)scan.l	8.1 (Berkeley) 6/6/93
43 */
44
45#include <errno.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <unistd.h>
50#include "config.h"
51#include "gram.h"
52
53int	yyline;
54const char *yyfile;
55const char *lastfile;
56
57/*
58 * Data for returning to previous files from include files.
59 */
60struct incl {
61	struct	incl *in_prev;	/* previous includes in effect, if any */
62	YY_BUFFER_STATE in_buf;	/* previous lex state */
63	const char *in_fname;	/* previous file name */
64	int	in_lineno;	/* previous line number */
65	int	in_ateof;	/* token to insert at EOF */
66};
67static struct incl *incl;
68static int endinclude(void);
69
70%}
71
72%option noyywrap
73
74PATH	[A-Za-z_0-9]*[./][-A-Za-z_0-9./\$\{\}]*
75WORD	[A-Za-z_][-A-Za-z_0-9]*
76
77%%
78		/* Local variables for yylex() */
79		int tok;
80
81and		return AND;
82at		return AT;
83attach		return ATTACH;
84build		return BUILD;
85compile-with	return COMPILE_WITH;
86config		return CONFIG;
87define		return DEFINE;
88defopt		return DEFOPT;
89device		return DEVICE;
90disable		return DISABLE;
91enable		return ENABLE;
92dumps		return DUMPS;
93file		return XFILE;
94flags		return FLAGS;
95include		return INCLUDE;
96machine		return XMACHINE;
97major		return MAJOR;
98makeoptions	return MAKEOPTIONS;
99makeoption	return MAKEOPTIONS;
100maxpartitions	return MAXPARTITIONS;
101maxusers	return MAXUSERS;
102minor		return MINOR;
103needs-count	return NEEDS_COUNT;
104needs-flag	return NEEDS_FLAG;
105object		return XOBJECT;
106on		return ON;
107options		return OPTIONS;
108option		return OPTIONS;
109pseudo-device	return PSEUDO_DEVICE;
110root		return ROOT;
111source		return SOURCE;
112swap		return SWAP;
113with		return WITH;
114rmoption	return RMOPTIONS;
115rmoptions	return RMOPTIONS;
116
117{PATH}		{
118		yylval.str = intern(yytext);
119		return PATHNAME;
120	}
121{WORD}		{
122		yylval.str = intern(yytext);
123		return WORD;
124	}
125
126\"\"		{
127		yylval.str = intern("");
128		return EMPTY;
129	}
130\"([^"\n]|\\\")+ {
131		tok = input();  /* eat closing quote */
132		yylval.str = intern(yytext + 1);
133		if (tok != '"') {
134			error("closing quote missing");
135			unput(tok);
136		}
137		return WORD;
138	}
1390[0-7]*	{
140		yylval.val = strtol(yytext, NULL, 8);
141		return NUMBER;
142	}
1430[xX][0-9a-fA-F]+ {
144		yylval.val = strtoul(yytext + 2, NULL, 16);
145		return NUMBER;
146	}
147[1-9][0-9]* {
148		yylval.val = strtol(yytext, NULL, 10);
149		return NUMBER;
150	}
151\n[ \t] {
152		/*
153		 * Note: newline followed by whitespace is always a
154		 * continuation of the previous line, so do NOT
155		 * return a token in this case.
156		 */
157		yyline++;
158	}
159\n	{
160		yyline++;
161		return '\n';
162	}
163#.*	{ /* ignored (comment) */; }
164[ \t]+	{ /* ignored (white space) */; }
165.	{ return yytext[0]; }
166<<EOF>> {
167		if (incl == NULL)
168			return YY_NULL;
169		tok = endinclude();
170		if (tok)
171			return tok;
172		/* otherwise continue scanning */
173	}
174
175%%
176
177/*
178 * Open the "main" file (conffile).
179 */
180int
181firstfile(const char *fname)
182{
183
184	if ((yyin = fopen(fname, "r")) == NULL)
185		return (-1);
186	yyfile = conffile = fname;
187	yyline = 1;
188	return (0);
189}
190
191/*
192 * Open the named file for inclusion at the current point.  Returns 0 on
193 * success (file opened and previous state pushed), nonzero on failure
194 * (fopen failed, complaint made).  The `ateof' parameter controls the
195 * token to be inserted at the end of the include file (i.e. ENDFILE).
196 * If ateof == 0 then nothing is inserted.
197 */
198int
199include(const char *fname, int ateof)
200{
201	FILE *fp;
202	struct incl *in;
203	char *s;
204	static int havedirs;
205
206	if (havedirs == 0) {
207		havedirs = 1;
208		setupdirs();
209	}
210
211	/* Kludge until files.* files are fixed. */
212	if (strncmp(fname, "../../../", 9) == 0)
213		fname += 9;
214
215	s = (*fname == '/') ? strdup(fname) : sourcepath(fname);
216	if ((fp = fopen(s, "r")) == NULL) {
217		error("cannot open %s for reading: %s\n", s, strerror(errno));
218		free(s);
219		return (-1);
220	}
221	in = emalloc(sizeof *in);
222	in->in_prev = incl;
223	in->in_buf = YY_CURRENT_BUFFER;
224	in->in_fname = yyfile;
225	in->in_lineno = yyline;
226	in->in_ateof = ateof;
227	incl = in;
228	yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
229	yyfile = intern(s);
230	yyline = 1;
231	free(s);
232	return (0);
233}
234
235/*
236 * Terminate the most recent inclusion.
237 */
238static int
239endinclude()
240{
241	struct incl *in;
242	int ateof;
243
244	if ((in = incl) == NULL)
245		panic("endinclude");
246	incl = in->in_prev;
247	lastfile = yyfile;
248	yy_delete_buffer(YY_CURRENT_BUFFER);
249	(void)fclose(yyin);
250	yy_switch_to_buffer(in->in_buf);
251	yyfile = in->in_fname;
252	yyline = in->in_lineno;
253	ateof  = in->in_ateof;
254	free(in);
255
256	return (ateof);
257}
258
259/*
260 * Return the current line number.  If yacc has looked ahead and caused
261 * us to consume a newline, we have to subtract one.  yychar is yacc's
262 * token lookahead, so we can tell.
263 */
264int
265currentline()
266{
267	extern int yychar;
268
269	return (yyline - (yychar == '\n'));
270}
271