scan.l revision 1.4 1 1.1 thorpej %{
2 1.4 cube /* $NetBSD: scan.l,v 1.4 2006/05/25 22:28:38 cube 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.1 thorpej #include "defs.h"
54 1.1 thorpej #include "gram.h"
55 1.1 thorpej
56 1.1 thorpej int yyline;
57 1.1 thorpej const char *yyfile;
58 1.1 thorpej const char *lastfile;
59 1.2 martin char curinclpath[PATH_MAX];
60 1.1 thorpej
61 1.1 thorpej /*
62 1.1 thorpej * Data for returning to previous files from include files.
63 1.1 thorpej */
64 1.1 thorpej struct incl {
65 1.1 thorpej struct incl *in_prev; /* previous includes in effect, if any */
66 1.1 thorpej YY_BUFFER_STATE in_buf; /* previous lex state */
67 1.1 thorpej const char *in_fname; /* previous file name */
68 1.1 thorpej int in_lineno; /* previous line number */
69 1.1 thorpej int in_ateof; /* token to insert at EOF */
70 1.1 thorpej int in_interesting; /* previous value for "interesting" */
71 1.1 thorpej };
72 1.1 thorpej static struct incl *incl;
73 1.1 thorpej static int endinclude(void);
74 1.2 martin static int getincludepath(void);
75 1.1 thorpej
76 1.1 thorpej #define yywrap() 1
77 1.1 thorpej
78 1.1 thorpej %}
79 1.1 thorpej
80 1.1 thorpej PATH [A-Za-z_0-9]*[./][-A-Za-z_0-9./]*
81 1.2 martin QCHARS ([^"\n]|\\\")+
82 1.1 thorpej WORD [A-Za-z_][-A-Za-z_0-9]*
83 1.2 martin FILENAME ({PATH}|\"{QCHARS}\")
84 1.1 thorpej
85 1.1 thorpej %%
86 1.1 thorpej /* Local variables for yylex() */
87 1.1 thorpej int tok;
88 1.1 thorpej
89 1.1 thorpej and return AND;
90 1.1 thorpej at return AT;
91 1.1 thorpej attach return ATTACH;
92 1.1 thorpej block return BLOCK;
93 1.1 thorpej build return BUILD;
94 1.1 thorpej char return CHAR;
95 1.1 thorpej compile-with return COMPILE_WITH;
96 1.1 thorpej config return CONFIG;
97 1.1 thorpej deffs return DEFFS;
98 1.1 thorpej define return DEFINE;
99 1.1 thorpej defflag return DEFFLAG;
100 1.1 thorpej defopt return DEFOPT;
101 1.1 thorpej defparam return DEFPARAM;
102 1.1 thorpej defpseudo return DEFPSEUDO;
103 1.1 thorpej devclass return DEVCLASS;
104 1.1 thorpej device return DEVICE;
105 1.1 thorpej device-major return DEVICE_MAJOR;
106 1.1 thorpej dumps return DUMPS;
107 1.1 thorpej file return XFILE;
108 1.1 thorpej file-system return FILE_SYSTEM;
109 1.1 thorpej flags return FLAGS;
110 1.1 thorpej ident return IDENT;
111 1.1 thorpej machine return XMACHINE;
112 1.1 thorpej major return MAJOR;
113 1.1 thorpej makeoptions return MAKEOPTIONS;
114 1.1 thorpej maxpartitions return MAXPARTITIONS;
115 1.1 thorpej maxusers return MAXUSERS;
116 1.1 thorpej minor return MINOR;
117 1.1 thorpej needs-count return NEEDS_COUNT;
118 1.1 thorpej needs-flag return NEEDS_FLAG;
119 1.1 thorpej no return NO;
120 1.1 thorpej object return XOBJECT;
121 1.4 cube obsolete return OBSOLETE;
122 1.1 thorpej on return ON;
123 1.1 thorpej options return OPTIONS;
124 1.1 thorpej prefix return PREFIX;
125 1.1 thorpej pseudo-device return PSEUDO_DEVICE;
126 1.1 thorpej root return ROOT;
127 1.1 thorpej source return SOURCE;
128 1.1 thorpej type return TYPE;
129 1.3 cube version return VERSION;
130 1.1 thorpej with return WITH;
131 1.1 thorpej
132 1.1 thorpej \+= return PLUSEQ;
133 1.1 thorpej
134 1.2 martin include[ \t]+{FILENAME} {
135 1.2 martin if (getincludepath()) {
136 1.2 martin include(curinclpath, 0, 0, 1);
137 1.2 martin } else {
138 1.2 martin yyerror("bad include path-name");
139 1.2 martin }
140 1.2 martin }
141 1.2 martin
142 1.2 martin cinclude[ \t]+{FILENAME} {
143 1.2 martin if (getincludepath()) {
144 1.2 martin include(curinclpath, 0, 1, 1);
145 1.2 martin } else {
146 1.2 martin yyerror("bad cinclude path-name");
147 1.2 martin }
148 1.2 martin }
149 1.2 martin
150 1.2 martin package[ \t]+{FILENAME} {
151 1.2 martin if (!oktopackage) {
152 1.2 martin yyerror("package not allowed here");
153 1.2 martin } else if (getincludepath()) {
154 1.2 martin package(curinclpath);
155 1.2 martin } else {
156 1.2 martin yyerror("bad package path-name");
157 1.2 martin }
158 1.2 martin }
159 1.2 martin
160 1.1 thorpej {PATH} {
161 1.1 thorpej yylval.str = intern(yytext);
162 1.1 thorpej return PATHNAME;
163 1.1 thorpej }
164 1.1 thorpej
165 1.1 thorpej {WORD} {
166 1.1 thorpej yylval.str = intern(yytext);
167 1.1 thorpej return WORD;
168 1.1 thorpej }
169 1.1 thorpej
170 1.1 thorpej \"\" {
171 1.1 thorpej yylval.str = intern("");
172 1.1 thorpej return EMPTY;
173 1.1 thorpej }
174 1.2 martin
175 1.2 martin \"{QCHARS} {
176 1.1 thorpej tok = input(); /* eat closing quote */
177 1.1 thorpej if (tok != '"') {
178 1.1 thorpej error("closing quote missing\n");
179 1.1 thorpej unput(tok);
180 1.1 thorpej }
181 1.1 thorpej yylval.str = intern(yytext + 1);
182 1.1 thorpej return QSTRING;
183 1.1 thorpej }
184 1.1 thorpej 0[0-7]* {
185 1.1 thorpej yylval.num.fmt = 8;
186 1.1 thorpej yylval.num.val = strtoll(yytext, NULL, 8);
187 1.1 thorpej return NUMBER;
188 1.1 thorpej }
189 1.1 thorpej 0[xX][0-9a-fA-F]+ {
190 1.1 thorpej yylval.num.fmt = 16;
191 1.1 thorpej yylval.num.val = strtoull(yytext + 2, NULL, 16);
192 1.1 thorpej return NUMBER;
193 1.1 thorpej }
194 1.1 thorpej [1-9][0-9]* {
195 1.1 thorpej yylval.num.fmt = 10;
196 1.1 thorpej yylval.num.val = strtoll(yytext, NULL, 10);
197 1.1 thorpej return NUMBER;
198 1.1 thorpej }
199 1.1 thorpej \n[ \t] {
200 1.1 thorpej /*
201 1.1 thorpej * Note: newline followed by whitespace is always a
202 1.1 thorpej * continuation of the previous line, so do NOT
203 1.1 thorpej * return a token in this case.
204 1.1 thorpej */
205 1.1 thorpej yyline++;
206 1.1 thorpej }
207 1.1 thorpej \n {
208 1.1 thorpej yyline++;
209 1.1 thorpej return '\n';
210 1.1 thorpej }
211 1.1 thorpej \00 {
212 1.1 thorpej /* Detect NUL characters in the config file and
213 1.1 thorpej * error out.
214 1.1 thorpej */
215 1.1 thorpej error("NUL character detected at line %i\n", yyline);
216 1.1 thorpej }
217 1.1 thorpej #.* { /* ignored (comment) */; }
218 1.1 thorpej [ \t]+ { /* ignored (white space) */; }
219 1.1 thorpej . { return yytext[0]; }
220 1.1 thorpej <<EOF>> {
221 1.1 thorpej if (incl == NULL)
222 1.1 thorpej return YY_NULL;
223 1.1 thorpej tok = endinclude();
224 1.1 thorpej if (tok)
225 1.1 thorpej return tok;
226 1.1 thorpej /* otherwise continue scanning */
227 1.1 thorpej }
228 1.1 thorpej
229 1.1 thorpej %%
230 1.1 thorpej
231 1.1 thorpej int interesting = 1;
232 1.1 thorpej
233 1.1 thorpej static int
234 1.1 thorpej curdir_push(const char *fname)
235 1.1 thorpej {
236 1.1 thorpej struct prefix *pf;
237 1.1 thorpej char *p, *d, *f;
238 1.1 thorpej
239 1.1 thorpej /* Set up the initial "current directory" for include directives. */
240 1.1 thorpej d = dirname(f = estrdup(fname));
241 1.1 thorpej if (*d == '/')
242 1.1 thorpej p = estrdup(d);
243 1.1 thorpej else {
244 1.1 thorpej char *cwd, buf[PATH_MAX];
245 1.1 thorpej
246 1.1 thorpej if ((cwd = getcwd(buf, sizeof(buf))) == NULL)
247 1.1 thorpej return (-1);
248 1.1 thorpej p = emalloc(strlen(cwd) + strlen(d) + 2);
249 1.1 thorpej sprintf(p, "%s/%s", cwd, d);
250 1.1 thorpej }
251 1.1 thorpej free(f);
252 1.1 thorpej pf = ecalloc(1, sizeof(*pf));
253 1.1 thorpej pf->pf_prefix = p;
254 1.1 thorpej SLIST_INSERT_HEAD(&curdirs, pf, pf_next);
255 1.1 thorpej
256 1.1 thorpej return (0);
257 1.1 thorpej }
258 1.1 thorpej
259 1.1 thorpej static void
260 1.1 thorpej curdir_pop(void)
261 1.1 thorpej {
262 1.1 thorpej struct prefix *pf;
263 1.1 thorpej
264 1.1 thorpej pf = SLIST_FIRST(&curdirs);
265 1.1 thorpej SLIST_REMOVE_HEAD(&curdirs, pf_next);
266 1.1 thorpej if (SLIST_EMPTY(&curdirs))
267 1.1 thorpej panic("curdirs is empty");
268 1.1 thorpej /* LINTED cast away const (pf_prefix is malloc'd for curdirs) */
269 1.1 thorpej free((void *)pf->pf_prefix);
270 1.1 thorpej free(pf);
271 1.1 thorpej }
272 1.1 thorpej
273 1.1 thorpej /*
274 1.1 thorpej * Open the "main" file (conffile).
275 1.1 thorpej */
276 1.1 thorpej int
277 1.1 thorpej firstfile(const char *fname)
278 1.1 thorpej {
279 1.1 thorpej
280 1.1 thorpej #if defined(__NetBSD__)
281 1.1 thorpej if ((yyin = fopen(fname, "rf")) == NULL)
282 1.1 thorpej #else
283 1.1 thorpej if ((yyin = fopen(fname, "r")) == NULL)
284 1.1 thorpej #endif
285 1.1 thorpej return (-1);
286 1.1 thorpej
287 1.1 thorpej if (curdir_push(fname) == -1)
288 1.1 thorpej return (-1);
289 1.1 thorpej
290 1.1 thorpej yyfile = conffile = fname;
291 1.1 thorpej yyline = 1;
292 1.1 thorpej return (0);
293 1.1 thorpej }
294 1.1 thorpej
295 1.1 thorpej /*
296 1.1 thorpej * Add a "package" to the configuration. This is essentially
297 1.1 thorpej * syntactic sugar around the sequence:
298 1.1 thorpej *
299 1.1 thorpej * prefix ../some/directory
300 1.1 thorpej * include "files.package"
301 1.1 thorpej * prefix
302 1.1 thorpej */
303 1.1 thorpej void
304 1.1 thorpej package(const char *fname)
305 1.1 thorpej {
306 1.1 thorpej char *fname1 = estrdup(fname);
307 1.1 thorpej char *fname2 = estrdup(fname);
308 1.1 thorpej char *dir = dirname(fname1);
309 1.1 thorpej char *file = basename(fname2);
310 1.1 thorpej
311 1.1 thorpej /*
312 1.1 thorpej * Push the prefix on to the prefix stack and process the include
313 1.1 thorpej * file. When we reach the end of the include file, inserting
314 1.1 thorpej * the PREFIX token into the input stream will pop the prefix off
315 1.1 thorpej * of the prefix stack.
316 1.1 thorpej */
317 1.1 thorpej prefix_push(dir);
318 1.1 thorpej (void) include(file, PREFIX, 0, 1);
319 1.1 thorpej
320 1.1 thorpej free(fname1);
321 1.1 thorpej free(fname2);
322 1.1 thorpej }
323 1.1 thorpej
324 1.1 thorpej /*
325 1.1 thorpej * Open the named file for inclusion at the current point. Returns 0 on
326 1.1 thorpej * success (file opened and previous state pushed), nonzero on failure
327 1.1 thorpej * (fopen failed, complaint made). The `ateof' parameter controls the
328 1.1 thorpej * token to be inserted at the end of the include file (i.e. ENDFILE).
329 1.1 thorpej * If ateof == 0 then nothing is inserted.
330 1.1 thorpej */
331 1.1 thorpej int
332 1.1 thorpej include(const char *fname, int ateof, int conditional, int direct)
333 1.1 thorpej {
334 1.1 thorpej FILE *fp;
335 1.1 thorpej struct incl *in;
336 1.1 thorpej char *s;
337 1.1 thorpej static int havedirs;
338 1.1 thorpej extern int vflag;
339 1.1 thorpej
340 1.1 thorpej if (havedirs == 0) {
341 1.1 thorpej havedirs = 1;
342 1.1 thorpej setupdirs();
343 1.1 thorpej }
344 1.1 thorpej
345 1.1 thorpej if (fname[0] == '/')
346 1.1 thorpej s = estrdup(fname);
347 1.1 thorpej else if (fname[0] == '.' && fname[1] == '/') {
348 1.1 thorpej struct prefix *pf = SLIST_FIRST(&curdirs);
349 1.1 thorpej s = emalloc(strlen(pf->pf_prefix) + strlen(fname));
350 1.1 thorpej sprintf(s, "%s/%s", pf->pf_prefix, fname + 2);
351 1.1 thorpej } else
352 1.1 thorpej s = sourcepath(fname);
353 1.1 thorpej if ((fp = fopen(s, "r")) == NULL) {
354 1.1 thorpej if (conditional == 0)
355 1.1 thorpej error("cannot open %s for reading: %s\n", s,
356 1.1 thorpej strerror(errno));
357 1.1 thorpej else if (vflag)
358 1.1 thorpej warn("cannot open conditional include file %s: %s",
359 1.1 thorpej s, strerror(errno));
360 1.1 thorpej free(s);
361 1.1 thorpej return (-1);
362 1.1 thorpej }
363 1.1 thorpej if (curdir_push(s) == -1) {
364 1.1 thorpej error("cannot record current working directory for %s\n", s);
365 1.1 thorpej fclose(fp);
366 1.1 thorpej free(s);
367 1.1 thorpej return (-1);
368 1.1 thorpej }
369 1.1 thorpej in = ecalloc(1, sizeof *in);
370 1.1 thorpej in->in_prev = incl;
371 1.1 thorpej in->in_buf = YY_CURRENT_BUFFER;
372 1.1 thorpej in->in_fname = yyfile;
373 1.1 thorpej in->in_lineno = yyline;
374 1.1 thorpej in->in_ateof = ateof;
375 1.1 thorpej in->in_interesting = interesting;
376 1.1 thorpej interesting = direct & interesting;
377 1.1 thorpej if (interesting)
378 1.1 thorpej logconfig_include(fp, fname);
379 1.1 thorpej incl = in;
380 1.1 thorpej yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
381 1.1 thorpej yyfile = intern(s);
382 1.1 thorpej yyline = 1;
383 1.1 thorpej free(s);
384 1.1 thorpej return (0);
385 1.1 thorpej }
386 1.1 thorpej
387 1.1 thorpej /*
388 1.2 martin * Extract the pathname from a include/cinclude/package into curinclpath
389 1.2 martin */
390 1.2 martin static int
391 1.2 martin getincludepath()
392 1.2 martin {
393 1.2 martin const char *p = yytext;
394 1.2 martin
395 1.2 martin while (*p && isascii((signed char)*p) && !isspace((signed char)*p))
396 1.2 martin p++;
397 1.2 martin while (*p && isascii((signed char)*p) && isspace((signed char)*p))
398 1.2 martin p++;
399 1.2 martin if (!*p)
400 1.2 martin return 0;
401 1.2 martin if (*p == '"') {
402 1.2 martin ptrdiff_t len;
403 1.2 martin const char *e = strchr(p+1, '"');
404 1.2 martin
405 1.2 martin if (!e) return 0;
406 1.2 martin len = e-p-1;
407 1.2 martin if (len > sizeof(curinclpath)-1)
408 1.2 martin len = sizeof(curinclpath)-1;
409 1.2 martin strncpy(curinclpath, p+1, sizeof(curinclpath));
410 1.2 martin curinclpath[len] = '\0';
411 1.2 martin } else {
412 1.2 martin strncpy(curinclpath, p, sizeof(curinclpath));
413 1.2 martin curinclpath[sizeof(curinclpath)-1] = '\0';
414 1.2 martin }
415 1.2 martin
416 1.2 martin return 1;
417 1.2 martin }
418 1.2 martin
419 1.2 martin /*
420 1.1 thorpej * Terminate the most recent inclusion.
421 1.1 thorpej */
422 1.1 thorpej static int
423 1.1 thorpej endinclude(void)
424 1.1 thorpej {
425 1.1 thorpej struct incl *in;
426 1.1 thorpej int ateof;
427 1.1 thorpej
428 1.1 thorpej curdir_pop();
429 1.1 thorpej if ((in = incl) == NULL)
430 1.1 thorpej panic("endinclude");
431 1.1 thorpej incl = in->in_prev;
432 1.1 thorpej lastfile = yyfile;
433 1.1 thorpej yy_delete_buffer(YY_CURRENT_BUFFER);
434 1.1 thorpej (void)fclose(yyin);
435 1.1 thorpej yy_switch_to_buffer(in->in_buf);
436 1.1 thorpej yyfile = in->in_fname;
437 1.1 thorpej yyline = in->in_lineno;
438 1.1 thorpej ateof = in->in_ateof;
439 1.1 thorpej interesting = in->in_interesting;
440 1.1 thorpej free(in);
441 1.1 thorpej
442 1.1 thorpej return (ateof);
443 1.1 thorpej }
444 1.1 thorpej
445 1.1 thorpej /*
446 1.1 thorpej * Return the current line number. If yacc has looked ahead and caused
447 1.1 thorpej * us to consume a newline, we have to subtract one. yychar is yacc's
448 1.1 thorpej * token lookahead, so we can tell.
449 1.1 thorpej */
450 1.1 thorpej int
451 1.1 thorpej currentline(void)
452 1.1 thorpej {
453 1.1 thorpej extern int yychar;
454 1.1 thorpej
455 1.1 thorpej return (yyline - (yychar == '\n'));
456 1.1 thorpej }
457