scan.l revision 1.5 1 1.1 thorpej %{
2 1.5 christos /* $NetBSD: scan.l,v 1.5 2006/08/26 18:17:13 christos Exp $ */
3 1.1 thorpej
4 1.1 thorpej /*
5 1.1 thorpej * Copyright (c) 1992, 1993
6 1.1 thorpej * The Regents of the University of California. All rights reserved.
7 1.1 thorpej *
8 1.1 thorpej * This software was developed by the Computer Systems Engineering group
9 1.1 thorpej * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 1.1 thorpej * contributed to Berkeley.
11 1.1 thorpej *
12 1.1 thorpej * All advertising materials mentioning features or use of this software
13 1.1 thorpej * must display the following acknowledgement:
14 1.1 thorpej * This product includes software developed by the University of
15 1.1 thorpej * California, Lawrence Berkeley Laboratories.
16 1.1 thorpej *
17 1.1 thorpej * Redistribution and use in source and binary forms, with or without
18 1.1 thorpej * modification, are permitted provided that the following conditions
19 1.1 thorpej * are met:
20 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
21 1.1 thorpej * notice, this list of conditions and the following disclaimer.
22 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
23 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
24 1.1 thorpej * documentation and/or other materials provided with the distribution.
25 1.1 thorpej * 3. Neither the name of the University nor the names of its contributors
26 1.1 thorpej * may be used to endorse or promote products derived from this software
27 1.1 thorpej * without specific prior written permission.
28 1.1 thorpej *
29 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 1.1 thorpej * SUCH DAMAGE.
40 1.1 thorpej *
41 1.1 thorpej * from: @(#)scan.l 8.1 (Berkeley) 6/6/93
42 1.1 thorpej */
43 1.1 thorpej
44 1.1 thorpej #include <sys/param.h>
45 1.1 thorpej #include <errno.h>
46 1.1 thorpej #include <libgen.h>
47 1.1 thorpej #include <stdio.h>
48 1.1 thorpej #include <stdlib.h>
49 1.1 thorpej #include <string.h>
50 1.1 thorpej #include <unistd.h>
51 1.2 martin #include <stddef.h>
52 1.2 martin #include <ctype.h>
53 1.5 christos #include <util.h>
54 1.5 christos #undef ECHO
55 1.1 thorpej #include "defs.h"
56 1.1 thorpej #include "gram.h"
57 1.1 thorpej
58 1.1 thorpej int yyline;
59 1.1 thorpej const char *yyfile;
60 1.1 thorpej const char *lastfile;
61 1.2 martin char curinclpath[PATH_MAX];
62 1.1 thorpej
63 1.1 thorpej /*
64 1.1 thorpej * Data for returning to previous files from include files.
65 1.1 thorpej */
66 1.1 thorpej struct incl {
67 1.1 thorpej struct incl *in_prev; /* previous includes in effect, if any */
68 1.1 thorpej YY_BUFFER_STATE in_buf; /* previous lex state */
69 1.1 thorpej const char *in_fname; /* previous file name */
70 1.1 thorpej int in_lineno; /* previous line number */
71 1.1 thorpej int in_ateof; /* token to insert at EOF */
72 1.1 thorpej int in_interesting; /* previous value for "interesting" */
73 1.1 thorpej };
74 1.1 thorpej static struct incl *incl;
75 1.1 thorpej static int endinclude(void);
76 1.2 martin static int getincludepath(void);
77 1.1 thorpej
78 1.1 thorpej #define yywrap() 1
79 1.1 thorpej
80 1.1 thorpej %}
81 1.1 thorpej
82 1.1 thorpej PATH [A-Za-z_0-9]*[./][-A-Za-z_0-9./]*
83 1.2 martin QCHARS ([^"\n]|\\\")+
84 1.1 thorpej WORD [A-Za-z_][-A-Za-z_0-9]*
85 1.2 martin FILENAME ({PATH}|\"{QCHARS}\")
86 1.1 thorpej
87 1.1 thorpej %%
88 1.1 thorpej /* Local variables for yylex() */
89 1.1 thorpej int tok;
90 1.1 thorpej
91 1.1 thorpej and return AND;
92 1.1 thorpej at return AT;
93 1.1 thorpej attach return ATTACH;
94 1.1 thorpej block return BLOCK;
95 1.1 thorpej build return BUILD;
96 1.1 thorpej char return CHAR;
97 1.1 thorpej compile-with return COMPILE_WITH;
98 1.1 thorpej config return CONFIG;
99 1.1 thorpej deffs return DEFFS;
100 1.1 thorpej define return DEFINE;
101 1.1 thorpej defflag return DEFFLAG;
102 1.1 thorpej defopt return DEFOPT;
103 1.1 thorpej defparam return DEFPARAM;
104 1.1 thorpej defpseudo return DEFPSEUDO;
105 1.1 thorpej devclass return DEVCLASS;
106 1.1 thorpej device return DEVICE;
107 1.1 thorpej device-major return DEVICE_MAJOR;
108 1.1 thorpej dumps return DUMPS;
109 1.1 thorpej file return XFILE;
110 1.1 thorpej file-system return FILE_SYSTEM;
111 1.1 thorpej flags return FLAGS;
112 1.1 thorpej ident return IDENT;
113 1.1 thorpej machine return XMACHINE;
114 1.1 thorpej major return MAJOR;
115 1.1 thorpej makeoptions return MAKEOPTIONS;
116 1.1 thorpej maxpartitions return MAXPARTITIONS;
117 1.1 thorpej maxusers return MAXUSERS;
118 1.1 thorpej minor return MINOR;
119 1.1 thorpej needs-count return NEEDS_COUNT;
120 1.1 thorpej needs-flag return NEEDS_FLAG;
121 1.1 thorpej no return NO;
122 1.1 thorpej object return XOBJECT;
123 1.4 cube obsolete return OBSOLETE;
124 1.1 thorpej on return ON;
125 1.1 thorpej options return OPTIONS;
126 1.1 thorpej prefix return PREFIX;
127 1.1 thorpej pseudo-device return PSEUDO_DEVICE;
128 1.1 thorpej root return ROOT;
129 1.1 thorpej source return SOURCE;
130 1.1 thorpej type return TYPE;
131 1.3 cube version return VERSION;
132 1.1 thorpej with return WITH;
133 1.1 thorpej
134 1.1 thorpej \+= return PLUSEQ;
135 1.1 thorpej
136 1.2 martin include[ \t]+{FILENAME} {
137 1.2 martin if (getincludepath()) {
138 1.2 martin include(curinclpath, 0, 0, 1);
139 1.2 martin } else {
140 1.2 martin yyerror("bad include path-name");
141 1.2 martin }
142 1.2 martin }
143 1.2 martin
144 1.2 martin cinclude[ \t]+{FILENAME} {
145 1.2 martin if (getincludepath()) {
146 1.2 martin include(curinclpath, 0, 1, 1);
147 1.2 martin } else {
148 1.2 martin yyerror("bad cinclude path-name");
149 1.2 martin }
150 1.2 martin }
151 1.2 martin
152 1.2 martin package[ \t]+{FILENAME} {
153 1.2 martin if (!oktopackage) {
154 1.2 martin yyerror("package not allowed here");
155 1.2 martin } else if (getincludepath()) {
156 1.2 martin package(curinclpath);
157 1.2 martin } else {
158 1.2 martin yyerror("bad package path-name");
159 1.2 martin }
160 1.2 martin }
161 1.2 martin
162 1.1 thorpej {PATH} {
163 1.1 thorpej yylval.str = intern(yytext);
164 1.1 thorpej return PATHNAME;
165 1.1 thorpej }
166 1.1 thorpej
167 1.1 thorpej {WORD} {
168 1.1 thorpej yylval.str = intern(yytext);
169 1.1 thorpej return WORD;
170 1.1 thorpej }
171 1.1 thorpej
172 1.1 thorpej \"\" {
173 1.1 thorpej yylval.str = intern("");
174 1.5 christos return EMPTYSTRING;
175 1.1 thorpej }
176 1.2 martin
177 1.2 martin \"{QCHARS} {
178 1.1 thorpej tok = input(); /* eat closing quote */
179 1.1 thorpej if (tok != '"') {
180 1.1 thorpej error("closing quote missing\n");
181 1.1 thorpej unput(tok);
182 1.1 thorpej }
183 1.1 thorpej yylval.str = intern(yytext + 1);
184 1.1 thorpej return QSTRING;
185 1.1 thorpej }
186 1.1 thorpej 0[0-7]* {
187 1.1 thorpej yylval.num.fmt = 8;
188 1.1 thorpej yylval.num.val = strtoll(yytext, NULL, 8);
189 1.1 thorpej return NUMBER;
190 1.1 thorpej }
191 1.1 thorpej 0[xX][0-9a-fA-F]+ {
192 1.1 thorpej yylval.num.fmt = 16;
193 1.1 thorpej yylval.num.val = strtoull(yytext + 2, NULL, 16);
194 1.1 thorpej return NUMBER;
195 1.1 thorpej }
196 1.1 thorpej [1-9][0-9]* {
197 1.1 thorpej yylval.num.fmt = 10;
198 1.1 thorpej yylval.num.val = strtoll(yytext, NULL, 10);
199 1.1 thorpej return NUMBER;
200 1.1 thorpej }
201 1.1 thorpej \n[ \t] {
202 1.1 thorpej /*
203 1.1 thorpej * Note: newline followed by whitespace is always a
204 1.1 thorpej * continuation of the previous line, so do NOT
205 1.1 thorpej * return a token in this case.
206 1.1 thorpej */
207 1.1 thorpej yyline++;
208 1.1 thorpej }
209 1.1 thorpej \n {
210 1.1 thorpej yyline++;
211 1.1 thorpej return '\n';
212 1.1 thorpej }
213 1.1 thorpej \00 {
214 1.1 thorpej /* Detect NUL characters in the config file and
215 1.1 thorpej * error out.
216 1.1 thorpej */
217 1.1 thorpej error("NUL character detected at line %i\n", yyline);
218 1.1 thorpej }
219 1.1 thorpej #.* { /* ignored (comment) */; }
220 1.1 thorpej [ \t]+ { /* ignored (white space) */; }
221 1.1 thorpej . { return yytext[0]; }
222 1.1 thorpej <<EOF>> {
223 1.1 thorpej if (incl == NULL)
224 1.1 thorpej return YY_NULL;
225 1.1 thorpej tok = endinclude();
226 1.1 thorpej if (tok)
227 1.1 thorpej return tok;
228 1.1 thorpej /* otherwise continue scanning */
229 1.1 thorpej }
230 1.1 thorpej
231 1.1 thorpej %%
232 1.1 thorpej
233 1.1 thorpej int interesting = 1;
234 1.1 thorpej
235 1.1 thorpej static int
236 1.1 thorpej curdir_push(const char *fname)
237 1.1 thorpej {
238 1.1 thorpej struct prefix *pf;
239 1.1 thorpej char *p, *d, *f;
240 1.1 thorpej
241 1.1 thorpej /* Set up the initial "current directory" for include directives. */
242 1.1 thorpej d = dirname(f = estrdup(fname));
243 1.1 thorpej if (*d == '/')
244 1.1 thorpej p = estrdup(d);
245 1.1 thorpej else {
246 1.1 thorpej char *cwd, buf[PATH_MAX];
247 1.1 thorpej
248 1.1 thorpej if ((cwd = getcwd(buf, sizeof(buf))) == NULL)
249 1.1 thorpej return (-1);
250 1.1 thorpej p = emalloc(strlen(cwd) + strlen(d) + 2);
251 1.1 thorpej sprintf(p, "%s/%s", cwd, d);
252 1.1 thorpej }
253 1.1 thorpej free(f);
254 1.1 thorpej pf = ecalloc(1, sizeof(*pf));
255 1.1 thorpej pf->pf_prefix = p;
256 1.1 thorpej SLIST_INSERT_HEAD(&curdirs, pf, pf_next);
257 1.1 thorpej
258 1.1 thorpej return (0);
259 1.1 thorpej }
260 1.1 thorpej
261 1.1 thorpej static void
262 1.1 thorpej curdir_pop(void)
263 1.1 thorpej {
264 1.1 thorpej struct prefix *pf;
265 1.1 thorpej
266 1.1 thorpej pf = SLIST_FIRST(&curdirs);
267 1.1 thorpej SLIST_REMOVE_HEAD(&curdirs, pf_next);
268 1.1 thorpej if (SLIST_EMPTY(&curdirs))
269 1.1 thorpej panic("curdirs is empty");
270 1.1 thorpej /* LINTED cast away const (pf_prefix is malloc'd for curdirs) */
271 1.1 thorpej free((void *)pf->pf_prefix);
272 1.1 thorpej free(pf);
273 1.1 thorpej }
274 1.1 thorpej
275 1.1 thorpej /*
276 1.1 thorpej * Open the "main" file (conffile).
277 1.1 thorpej */
278 1.1 thorpej int
279 1.1 thorpej firstfile(const char *fname)
280 1.1 thorpej {
281 1.1 thorpej
282 1.1 thorpej #if defined(__NetBSD__)
283 1.1 thorpej if ((yyin = fopen(fname, "rf")) == NULL)
284 1.1 thorpej #else
285 1.1 thorpej if ((yyin = fopen(fname, "r")) == NULL)
286 1.1 thorpej #endif
287 1.1 thorpej return (-1);
288 1.1 thorpej
289 1.1 thorpej if (curdir_push(fname) == -1)
290 1.1 thorpej return (-1);
291 1.1 thorpej
292 1.1 thorpej yyfile = conffile = fname;
293 1.1 thorpej yyline = 1;
294 1.1 thorpej return (0);
295 1.1 thorpej }
296 1.1 thorpej
297 1.1 thorpej /*
298 1.1 thorpej * Add a "package" to the configuration. This is essentially
299 1.1 thorpej * syntactic sugar around the sequence:
300 1.1 thorpej *
301 1.1 thorpej * prefix ../some/directory
302 1.1 thorpej * include "files.package"
303 1.1 thorpej * prefix
304 1.1 thorpej */
305 1.1 thorpej void
306 1.1 thorpej package(const char *fname)
307 1.1 thorpej {
308 1.1 thorpej char *fname1 = estrdup(fname);
309 1.1 thorpej char *fname2 = estrdup(fname);
310 1.1 thorpej char *dir = dirname(fname1);
311 1.1 thorpej char *file = basename(fname2);
312 1.1 thorpej
313 1.1 thorpej /*
314 1.1 thorpej * Push the prefix on to the prefix stack and process the include
315 1.1 thorpej * file. When we reach the end of the include file, inserting
316 1.1 thorpej * the PREFIX token into the input stream will pop the prefix off
317 1.1 thorpej * of the prefix stack.
318 1.1 thorpej */
319 1.1 thorpej prefix_push(dir);
320 1.1 thorpej (void) include(file, PREFIX, 0, 1);
321 1.1 thorpej
322 1.1 thorpej free(fname1);
323 1.1 thorpej free(fname2);
324 1.1 thorpej }
325 1.1 thorpej
326 1.1 thorpej /*
327 1.1 thorpej * Open the named file for inclusion at the current point. Returns 0 on
328 1.1 thorpej * success (file opened and previous state pushed), nonzero on failure
329 1.1 thorpej * (fopen failed, complaint made). The `ateof' parameter controls the
330 1.1 thorpej * token to be inserted at the end of the include file (i.e. ENDFILE).
331 1.1 thorpej * If ateof == 0 then nothing is inserted.
332 1.1 thorpej */
333 1.1 thorpej int
334 1.1 thorpej include(const char *fname, int ateof, int conditional, int direct)
335 1.1 thorpej {
336 1.1 thorpej FILE *fp;
337 1.1 thorpej struct incl *in;
338 1.1 thorpej char *s;
339 1.1 thorpej static int havedirs;
340 1.1 thorpej extern int vflag;
341 1.1 thorpej
342 1.1 thorpej if (havedirs == 0) {
343 1.1 thorpej havedirs = 1;
344 1.1 thorpej setupdirs();
345 1.1 thorpej }
346 1.1 thorpej
347 1.1 thorpej if (fname[0] == '/')
348 1.1 thorpej s = estrdup(fname);
349 1.1 thorpej else if (fname[0] == '.' && fname[1] == '/') {
350 1.1 thorpej struct prefix *pf = SLIST_FIRST(&curdirs);
351 1.1 thorpej s = emalloc(strlen(pf->pf_prefix) + strlen(fname));
352 1.1 thorpej sprintf(s, "%s/%s", pf->pf_prefix, fname + 2);
353 1.1 thorpej } else
354 1.1 thorpej s = sourcepath(fname);
355 1.1 thorpej if ((fp = fopen(s, "r")) == NULL) {
356 1.1 thorpej if (conditional == 0)
357 1.1 thorpej error("cannot open %s for reading: %s\n", s,
358 1.1 thorpej strerror(errno));
359 1.1 thorpej else if (vflag)
360 1.1 thorpej warn("cannot open conditional include file %s: %s",
361 1.1 thorpej s, strerror(errno));
362 1.1 thorpej free(s);
363 1.1 thorpej return (-1);
364 1.1 thorpej }
365 1.1 thorpej if (curdir_push(s) == -1) {
366 1.1 thorpej error("cannot record current working directory for %s\n", s);
367 1.1 thorpej fclose(fp);
368 1.1 thorpej free(s);
369 1.1 thorpej return (-1);
370 1.1 thorpej }
371 1.1 thorpej in = ecalloc(1, sizeof *in);
372 1.1 thorpej in->in_prev = incl;
373 1.1 thorpej in->in_buf = YY_CURRENT_BUFFER;
374 1.1 thorpej in->in_fname = yyfile;
375 1.1 thorpej in->in_lineno = yyline;
376 1.1 thorpej in->in_ateof = ateof;
377 1.1 thorpej in->in_interesting = interesting;
378 1.1 thorpej interesting = direct & interesting;
379 1.1 thorpej if (interesting)
380 1.1 thorpej logconfig_include(fp, fname);
381 1.1 thorpej incl = in;
382 1.1 thorpej yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
383 1.1 thorpej yyfile = intern(s);
384 1.1 thorpej yyline = 1;
385 1.1 thorpej free(s);
386 1.1 thorpej return (0);
387 1.1 thorpej }
388 1.1 thorpej
389 1.1 thorpej /*
390 1.2 martin * Extract the pathname from a include/cinclude/package into curinclpath
391 1.2 martin */
392 1.2 martin static int
393 1.2 martin getincludepath()
394 1.2 martin {
395 1.2 martin const char *p = yytext;
396 1.2 martin
397 1.2 martin while (*p && isascii((signed char)*p) && !isspace((signed char)*p))
398 1.2 martin p++;
399 1.2 martin while (*p && isascii((signed char)*p) && isspace((signed char)*p))
400 1.2 martin p++;
401 1.2 martin if (!*p)
402 1.2 martin return 0;
403 1.2 martin if (*p == '"') {
404 1.2 martin ptrdiff_t len;
405 1.2 martin const char *e = strchr(p+1, '"');
406 1.2 martin
407 1.2 martin if (!e) return 0;
408 1.2 martin len = e-p-1;
409 1.2 martin if (len > sizeof(curinclpath)-1)
410 1.2 martin len = sizeof(curinclpath)-1;
411 1.2 martin strncpy(curinclpath, p+1, sizeof(curinclpath));
412 1.2 martin curinclpath[len] = '\0';
413 1.2 martin } else {
414 1.2 martin strncpy(curinclpath, p, sizeof(curinclpath));
415 1.2 martin curinclpath[sizeof(curinclpath)-1] = '\0';
416 1.2 martin }
417 1.2 martin
418 1.2 martin return 1;
419 1.2 martin }
420 1.2 martin
421 1.2 martin /*
422 1.1 thorpej * Terminate the most recent inclusion.
423 1.1 thorpej */
424 1.1 thorpej static int
425 1.1 thorpej endinclude(void)
426 1.1 thorpej {
427 1.1 thorpej struct incl *in;
428 1.1 thorpej int ateof;
429 1.1 thorpej
430 1.1 thorpej curdir_pop();
431 1.1 thorpej if ((in = incl) == NULL)
432 1.1 thorpej panic("endinclude");
433 1.1 thorpej incl = in->in_prev;
434 1.1 thorpej lastfile = yyfile;
435 1.1 thorpej yy_delete_buffer(YY_CURRENT_BUFFER);
436 1.1 thorpej (void)fclose(yyin);
437 1.1 thorpej yy_switch_to_buffer(in->in_buf);
438 1.1 thorpej yyfile = in->in_fname;
439 1.1 thorpej yyline = in->in_lineno;
440 1.1 thorpej ateof = in->in_ateof;
441 1.1 thorpej interesting = in->in_interesting;
442 1.1 thorpej free(in);
443 1.1 thorpej
444 1.1 thorpej return (ateof);
445 1.1 thorpej }
446 1.1 thorpej
447 1.1 thorpej /*
448 1.1 thorpej * Return the current line number. If yacc has looked ahead and caused
449 1.1 thorpej * us to consume a newline, we have to subtract one. yychar is yacc's
450 1.1 thorpej * token lookahead, so we can tell.
451 1.1 thorpej */
452 1.1 thorpej int
453 1.1 thorpej currentline(void)
454 1.1 thorpej {
455 1.1 thorpej extern int yychar;
456 1.1 thorpej
457 1.1 thorpej return (yyline - (yychar == '\n'));
458 1.1 thorpej }
459