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