main.c revision 1.34 1 1.34 agc /* $NetBSD: main.c,v 1.34 2003/08/07 11:14:32 agc Exp $ */
2 1.27 tv /* $OpenBSD: main.c,v 1.51 2001/10/06 10:52:25 espie Exp $ */
3 1.9 tls
4 1.8 glass /*-
5 1.8 glass * Copyright (c) 1989, 1993
6 1.8 glass * The Regents of the University of California. All rights reserved.
7 1.8 glass *
8 1.8 glass * This code is derived from software contributed to Berkeley by
9 1.8 glass * Ozan Yigit at York University.
10 1.8 glass *
11 1.8 glass * Redistribution and use in source and binary forms, with or without
12 1.8 glass * modification, are permitted provided that the following conditions
13 1.8 glass * are met:
14 1.8 glass * 1. Redistributions of source code must retain the above copyright
15 1.8 glass * notice, this list of conditions and the following disclaimer.
16 1.8 glass * 2. Redistributions in binary form must reproduce the above copyright
17 1.8 glass * notice, this list of conditions and the following disclaimer in the
18 1.8 glass * documentation and/or other materials provided with the distribution.
19 1.34 agc * 3. Neither the name of the University nor the names of its contributors
20 1.8 glass * may be used to endorse or promote products derived from this software
21 1.8 glass * without specific prior written permission.
22 1.8 glass *
23 1.8 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.8 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.8 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.8 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.8 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.8 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.8 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.8 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.8 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.8 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.8 glass * SUCH DAMAGE.
34 1.8 glass */
35 1.1 cgd
36 1.13 lukem #include <sys/cdefs.h>
37 1.30 tv #if defined(__COPYRIGHT) && !defined(lint)
38 1.13 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
39 1.13 lukem The Regents of the University of California. All rights reserved.\n");
40 1.8 glass #endif /* not lint */
41 1.8 glass
42 1.30 tv #if defined(__RCSID) && !defined(lint)
43 1.9 tls #if 0
44 1.8 glass static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
45 1.9 tls #else
46 1.34 agc __RCSID("$NetBSD: main.c,v 1.34 2003/08/07 11:14:32 agc Exp $");
47 1.9 tls #endif
48 1.8 glass #endif /* not lint */
49 1.1 cgd
50 1.1 cgd /*
51 1.8 glass * main.c
52 1.8 glass * Facility: m4 macro processor
53 1.8 glass * by: oz
54 1.1 cgd */
55 1.1 cgd
56 1.8 glass #include <sys/types.h>
57 1.27 tv #include <assert.h>
58 1.13 lukem #include <ctype.h>
59 1.13 lukem #include <errno.h>
60 1.8 glass #include <signal.h>
61 1.27 tv #include <stddef.h>
62 1.8 glass #include <stdio.h>
63 1.16 kleink #include <stdlib.h>
64 1.8 glass #include <string.h>
65 1.8 glass #include "mdef.h"
66 1.8 glass #include "stdd.h"
67 1.8 glass #include "extern.h"
68 1.8 glass #include "pathnames.h"
69 1.8 glass
70 1.8 glass ndptr hashtab[HASHSIZE]; /* hash table for macros etc. */
71 1.27 tv stae *mstack; /* stack of m4 machine */
72 1.27 tv char *sstack; /* shadow stack, for string space extension */
73 1.27 tv static size_t STACKMAX; /* current maximum size of stack */
74 1.1 cgd int sp; /* current m4 stack pointer */
75 1.1 cgd int fp; /* m4 call frame pointer */
76 1.27 tv struct input_file infile[MAXINP];/* input file stack (0=stdin) */
77 1.27 tv FILE **outfile; /* diversion array(0=bitbucket)*/
78 1.27 tv int maxout;
79 1.1 cgd FILE *active; /* active output file pointer */
80 1.1 cgd int ilevel = 0; /* input file stack pointer */
81 1.1 cgd int oindex = 0; /* diversion index.. */
82 1.1 cgd char *null = ""; /* as it says.. just a null.. */
83 1.1 cgd char *m4wraps = ""; /* m4wrap string default.. */
84 1.22 tv int m4prefix = 0; /* prefix keywords with m4_ */
85 1.11 pk char lquote[MAXCCHARS+1] = {LQUOTE}; /* left quote character (`) */
86 1.11 pk char rquote[MAXCCHARS+1] = {RQUOTE}; /* right quote character (') */
87 1.11 pk char scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */
88 1.11 pk char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */
89 1.2 glass
90 1.8 glass struct keyblk keywrds[] = { /* m4 keywords to be installed */
91 1.13 lukem { "include", INCLTYPE },
92 1.13 lukem { "sinclude", SINCTYPE },
93 1.13 lukem { "define", DEFITYPE },
94 1.13 lukem { "defn", DEFNTYPE },
95 1.27 tv { "divert", DIVRTYPE | NOARGS },
96 1.13 lukem { "expr", EXPRTYPE },
97 1.13 lukem { "eval", EXPRTYPE },
98 1.13 lukem { "substr", SUBSTYPE },
99 1.13 lukem { "ifelse", IFELTYPE },
100 1.13 lukem { "ifdef", IFDFTYPE },
101 1.13 lukem { "len", LENGTYPE },
102 1.13 lukem { "incr", INCRTYPE },
103 1.13 lukem { "decr", DECRTYPE },
104 1.27 tv { "dnl", DNLNTYPE | NOARGS },
105 1.27 tv { "changequote", CHNQTYPE | NOARGS },
106 1.27 tv { "changecom", CHNCTYPE | NOARGS },
107 1.13 lukem { "index", INDXTYPE },
108 1.8 glass #ifdef EXTENDED
109 1.13 lukem { "paste", PASTTYPE },
110 1.13 lukem { "spaste", SPASTYPE },
111 1.27 tv /* Newer extensions, needed to handle gnu-m4 scripts */
112 1.27 tv { "indir", INDIRTYPE},
113 1.27 tv { "builtin", BUILTINTYPE},
114 1.27 tv { "patsubst", PATSTYPE},
115 1.27 tv { "regexp", REGEXPTYPE},
116 1.27 tv { "esyscmd", ESYSCMDTYPE},
117 1.27 tv { "__file__", FILENAMETYPE | NOARGS},
118 1.27 tv { "__line__", LINETYPE | NOARGS},
119 1.8 glass #endif
120 1.13 lukem { "popdef", POPDTYPE },
121 1.13 lukem { "pushdef", PUSDTYPE },
122 1.27 tv { "dumpdef", DUMPTYPE | NOARGS },
123 1.27 tv { "shift", SHIFTYPE | NOARGS },
124 1.13 lukem { "translit", TRNLTYPE },
125 1.13 lukem { "undefine", UNDFTYPE },
126 1.27 tv { "undivert", UNDVTYPE | NOARGS },
127 1.27 tv { "divnum", DIVNTYPE | NOARGS },
128 1.13 lukem { "maketemp", MKTMTYPE },
129 1.27 tv { "errprint", ERRPTYPE | NOARGS },
130 1.27 tv { "m4wrap", M4WRTYPE | NOARGS },
131 1.27 tv { "m4exit", EXITTYPE | NOARGS },
132 1.13 lukem { "syscmd", SYSCTYPE },
133 1.27 tv { "sysval", SYSVTYPE | NOARGS },
134 1.27 tv { "traceon", TRACEONTYPE | NOARGS },
135 1.27 tv { "traceoff", TRACEOFFTYPE | NOARGS },
136 1.2 glass
137 1.27 tv #if defined(unix) || defined(__unix__)
138 1.27 tv { "unix", SELFTYPE | NOARGS },
139 1.2 glass #else
140 1.8 glass #ifdef vms
141 1.27 tv { "vms", SELFTYPE | NOARGS },
142 1.1 cgd #endif
143 1.1 cgd #endif
144 1.8 glass };
145 1.8 glass
146 1.8 glass #define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk))
147 1.1 cgd
148 1.27 tv extern int optind;
149 1.27 tv extern char *optarg;
150 1.27 tv
151 1.27 tv #define MAXRECORD 50
152 1.27 tv static struct position {
153 1.27 tv char *name;
154 1.27 tv unsigned long line;
155 1.27 tv } quotes[MAXRECORD], paren[MAXRECORD];
156 1.27 tv
157 1.27 tv static void record __P((struct position *, int));
158 1.27 tv static void dump_stack __P((struct position *, int));
159 1.27 tv
160 1.27 tv static void macro __P((void));
161 1.27 tv static void initkwds __P((void));
162 1.27 tv static ndptr inspect __P((int, char *));
163 1.27 tv static int do_look_ahead __P((int, const char *));
164 1.27 tv
165 1.27 tv static void enlarge_stack __P((void));
166 1.27 tv
167 1.27 tv int main __P((int, char *[]));
168 1.1 cgd
169 1.8 glass int
170 1.8 glass main(argc,argv)
171 1.8 glass int argc;
172 1.8 glass char *argv[];
173 1.8 glass {
174 1.13 lukem int c;
175 1.13 lukem int n;
176 1.8 glass char *p;
177 1.8 glass
178 1.8 glass if (signal(SIGINT, SIG_IGN) != SIG_IGN)
179 1.8 glass signal(SIGINT, onintr);
180 1.1 cgd
181 1.22 tv /*
182 1.22 tv * We need to know if -P is there before checking -D and -U.
183 1.22 tv */
184 1.28 tv while ((c = getopt(argc, argv, "D:I:PU:d:go:t:")) != -1)
185 1.22 tv if (c == 'P')
186 1.22 tv m4prefix = 1;
187 1.22 tv optind = 1;
188 1.22 tv
189 1.8 glass initkwds();
190 1.27 tv initspaces();
191 1.27 tv STACKMAX = INITSTACKMAX;
192 1.27 tv
193 1.27 tv mstack = (stae *)xalloc(sizeof(stae) * STACKMAX);
194 1.27 tv sstack = (char *)xalloc(STACKMAX);
195 1.1 cgd
196 1.27 tv maxout = 0;
197 1.27 tv outfile = NULL;
198 1.27 tv resizedivs(MAXOUT);
199 1.1 cgd
200 1.27 tv while ((c = getopt(argc, argv, "D:I:PU:d:go:t:")) != -1)
201 1.27 tv switch (c) {
202 1.8 glass case 'D': /* define something..*/
203 1.8 glass for (p = optarg; *p; p++)
204 1.8 glass if (*p == '=')
205 1.8 glass break;
206 1.8 glass if (*p)
207 1.8 glass *p++ = EOS;
208 1.8 glass dodefine(optarg, p);
209 1.8 glass break;
210 1.27 tv case 'I':
211 1.27 tv addtoincludepath(optarg);
212 1.27 tv break;
213 1.27 tv case 'P':
214 1.27 tv break;
215 1.8 glass case 'U': /* undefine... */
216 1.8 glass remhash(optarg, TOP);
217 1.8 glass break;
218 1.27 tv case 'd':
219 1.27 tv set_trace_flags(optarg);
220 1.27 tv break;
221 1.27 tv case 'g':
222 1.27 tv mimic_gnu = 1;
223 1.27 tv break;
224 1.27 tv case 'o':
225 1.27 tv trace_file(optarg);
226 1.27 tv break;
227 1.27 tv case 't':
228 1.27 tv mark_traced(optarg, 1);
229 1.22 tv break;
230 1.8 glass case '?':
231 1.22 tv default:
232 1.28 tv usage(argv[0]);
233 1.2 glass }
234 1.1 cgd
235 1.8 glass argc -= optind;
236 1.8 glass argv += optind;
237 1.2 glass
238 1.8 glass active = stdout; /* default active output */
239 1.8 glass bbase[0] = bufbase;
240 1.8 glass if (!argc) {
241 1.8 glass sp = -1; /* stack pointer initialized */
242 1.8 glass fp = 0; /* frame pointer initialized */
243 1.27 tv set_input(infile+0, stdin, "stdin");
244 1.27 tv /* default input (naturally) */
245 1.8 glass macro();
246 1.8 glass } else
247 1.8 glass for (; argc--; ++argv) {
248 1.8 glass p = *argv;
249 1.27 tv if (p[0] == '-' && p[1] == EOS)
250 1.27 tv set_input(infile, stdin, "stdin");
251 1.27 tv else if (fopen_trypath(infile, p) == NULL)
252 1.13 lukem err(1, "%s", p);
253 1.8 glass sp = -1;
254 1.8 glass fp = 0;
255 1.8 glass macro();
256 1.27 tv release_input(infile);
257 1.8 glass }
258 1.2 glass
259 1.8 glass if (*m4wraps) { /* anything for rundown ?? */
260 1.8 glass ilevel = 0; /* in case m4wrap includes.. */
261 1.8 glass bufbase = bp = buf; /* use the entire buffer */
262 1.8 glass pbstr(m4wraps); /* user-defined wrapup act */
263 1.8 glass macro(); /* last will and testament */
264 1.8 glass }
265 1.2 glass
266 1.8 glass if (active != stdout)
267 1.8 glass active = stdout; /* reset output just in case */
268 1.27 tv for (n = 1; n < maxout; n++) /* default wrap-up: undivert */
269 1.8 glass if (outfile[n] != NULL)
270 1.8 glass getdiv(n);
271 1.27 tv /* remove bitbucket if used */
272 1.27 tv if (outfile[0] != NULL) {
273 1.8 glass (void) fclose(outfile[0]);
274 1.27 tv }
275 1.8 glass
276 1.8 glass return 0;
277 1.8 glass }
278 1.2 glass
279 1.8 glass /*
280 1.27 tv * Look ahead for `token'.
281 1.11 pk * (on input `t == token[0]')
282 1.11 pk * Used for comment and quoting delimiters.
283 1.11 pk * Returns 1 if `token' present; copied to output.
284 1.11 pk * 0 if `token' not found; all characters pushed back
285 1.11 pk */
286 1.27 tv static int
287 1.11 pk do_look_ahead(t, token)
288 1.11 pk int t;
289 1.27 tv const char *token;
290 1.11 pk {
291 1.11 pk int i;
292 1.11 pk
293 1.27 tv assert((unsigned char)t == (unsigned char)token[0]);
294 1.11 pk
295 1.11 pk for (i = 1; *++token; i++) {
296 1.11 pk t = gpbc();
297 1.27 tv if (t == EOF || (unsigned char)t != (unsigned char)*token) {
298 1.27 tv putback(t);
299 1.11 pk while (--i)
300 1.11 pk putback(*--token);
301 1.11 pk return 0;
302 1.11 pk }
303 1.11 pk }
304 1.11 pk return 1;
305 1.11 pk }
306 1.11 pk
307 1.27 tv #define LOOK_AHEAD(t, token) (t != EOF && \
308 1.27 tv (unsigned char)(t)==(unsigned char)(token)[0] && \
309 1.27 tv do_look_ahead(t,token))
310 1.11 pk
311 1.11 pk /*
312 1.8 glass * macro - the work horse..
313 1.8 glass */
314 1.27 tv static void
315 1.13 lukem macro()
316 1.13 lukem {
317 1.27 tv char token[MAXTOK+1];
318 1.13 lukem int t, l;
319 1.13 lukem ndptr p;
320 1.13 lukem int nlpar;
321 1.1 cgd
322 1.8 glass cycle {
323 1.11 pk t = gpbc();
324 1.11 pk if (t == '_' || isalpha(t)) {
325 1.27 tv p = inspect(t, token);
326 1.27 tv if (p != nil)
327 1.27 tv putback(l = gpbc());
328 1.27 tv if (p == nil || (l != LPAREN &&
329 1.27 tv (p->type & NEEDARGS) != 0))
330 1.27 tv outputstr(token);
331 1.8 glass else {
332 1.8 glass /*
333 1.8 glass * real thing.. First build a call frame:
334 1.8 glass */
335 1.8 glass pushf(fp); /* previous call frm */
336 1.8 glass pushf(p->type); /* type of the call */
337 1.8 glass pushf(0); /* parenthesis level */
338 1.8 glass fp = sp; /* new frame pointer */
339 1.8 glass /*
340 1.8 glass * now push the string arguments:
341 1.8 glass */
342 1.27 tv pushs1(p->defn); /* defn string */
343 1.27 tv pushs1(p->name); /* macro name */
344 1.27 tv pushs(ep); /* start next..*/
345 1.27 tv
346 1.27 tv if (l != LPAREN && PARLEV == 0) {
347 1.27 tv /* no bracks */
348 1.27 tv chrsave(EOS);
349 1.27 tv
350 1.27 tv if (sp == STACKMAX)
351 1.27 tv errx(1, "internal stack overflow");
352 1.27 tv eval((const char **) mstack+fp+1, 2,
353 1.27 tv CALTYP);
354 1.27 tv
355 1.27 tv ep = PREVEP; /* flush strspace */
356 1.27 tv sp = PREVSP; /* previous sp.. */
357 1.27 tv fp = PREVFP; /* rewind stack...*/
358 1.2 glass }
359 1.8 glass }
360 1.27 tv } else if (t == EOF) {
361 1.27 tv if (sp > -1) {
362 1.27 tv warnx( "unexpected end of input, unclosed parenthesis:");
363 1.27 tv dump_stack(paren, PARLEV);
364 1.27 tv exit(1);
365 1.27 tv }
366 1.8 glass if (ilevel <= 0)
367 1.8 glass break; /* all done thanks.. */
368 1.27 tv release_input(infile+ilevel--);
369 1.8 glass bufbase = bbase[ilevel];
370 1.8 glass continue;
371 1.8 glass }
372 1.8 glass /*
373 1.12 cgd * non-alpha token possibly seen..
374 1.8 glass * [the order of else if .. stmts is important.]
375 1.8 glass */
376 1.11 pk else if (LOOK_AHEAD(t,lquote)) { /* strip quotes */
377 1.27 tv nlpar = 0;
378 1.27 tv record(quotes, nlpar++);
379 1.27 tv /*
380 1.27 tv * Opening quote: scan forward until matching
381 1.27 tv * closing quote has been found.
382 1.27 tv */
383 1.8 glass do {
384 1.12 cgd
385 1.11 pk l = gpbc();
386 1.12 cgd if (LOOK_AHEAD(l,rquote)) {
387 1.27 tv if (--nlpar > 0)
388 1.27 tv outputstr(rquote);
389 1.12 cgd } else if (LOOK_AHEAD(l,lquote)) {
390 1.27 tv record(quotes, nlpar++);
391 1.27 tv outputstr(lquote);
392 1.27 tv } else if (l == EOF) {
393 1.27 tv if (nlpar == 1)
394 1.27 tv warnx("unclosed quote:");
395 1.8 glass else
396 1.27 tv warnx("%d unclosed quotes:", nlpar);
397 1.27 tv dump_stack(quotes, nlpar);
398 1.27 tv exit(1);
399 1.27 tv } else {
400 1.27 tv if (nlpar > 0) {
401 1.27 tv if (sp < 0)
402 1.27 tv putc(l, active);
403 1.27 tv else
404 1.27 tv CHRSAVE(l);
405 1.27 tv }
406 1.2 glass }
407 1.8 glass }
408 1.8 glass while (nlpar != 0);
409 1.8 glass }
410 1.1 cgd
411 1.11 pk else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
412 1.27 tv fputs(scommt, active);
413 1.11 pk
414 1.11 pk for(;;) {
415 1.11 pk t = gpbc();
416 1.11 pk if (LOOK_AHEAD(t, ecommt)) {
417 1.27 tv fputs(ecommt, active);
418 1.11 pk break;
419 1.11 pk }
420 1.11 pk if (t == EOF)
421 1.11 pk break;
422 1.8 glass putc(t, active);
423 1.1 cgd }
424 1.11 pk }
425 1.11 pk
426 1.11 pk else if (sp < 0) { /* not in a macro at all */
427 1.8 glass putc(t, active); /* output directly.. */
428 1.8 glass }
429 1.1 cgd
430 1.8 glass else switch(t) {
431 1.2 glass
432 1.8 glass case LPAREN:
433 1.8 glass if (PARLEV > 0)
434 1.8 glass chrsave(t);
435 1.8 glass while (isspace(l = gpbc()))
436 1.8 glass ; /* skip blank, tab, nl.. */
437 1.8 glass putback(l);
438 1.27 tv record(paren, PARLEV++);
439 1.8 glass break;
440 1.1 cgd
441 1.8 glass case RPAREN:
442 1.8 glass if (--PARLEV > 0)
443 1.8 glass chrsave(t);
444 1.8 glass else { /* end of argument list */
445 1.8 glass chrsave(EOS);
446 1.8 glass
447 1.8 glass if (sp == STACKMAX)
448 1.13 lukem errx(1, "internal stack overflow");
449 1.8 glass
450 1.27 tv eval((const char **) mstack+fp+1, sp-fp,
451 1.27 tv CALTYP);
452 1.8 glass
453 1.8 glass ep = PREVEP; /* flush strspace */
454 1.8 glass sp = PREVSP; /* previous sp.. */
455 1.8 glass fp = PREVFP; /* rewind stack...*/
456 1.8 glass }
457 1.8 glass break;
458 1.1 cgd
459 1.8 glass case COMMA:
460 1.8 glass if (PARLEV == 1) {
461 1.8 glass chrsave(EOS); /* new argument */
462 1.8 glass while (isspace(l = gpbc()))
463 1.8 glass ;
464 1.8 glass putback(l);
465 1.8 glass pushs(ep);
466 1.8 glass } else
467 1.8 glass chrsave(t);
468 1.8 glass break;
469 1.2 glass
470 1.2 glass default:
471 1.27 tv if (LOOK_AHEAD(t, scommt)) {
472 1.27 tv char *p;
473 1.27 tv for (p = scommt; *p; p++)
474 1.27 tv chrsave(*p);
475 1.27 tv for(;;) {
476 1.27 tv t = gpbc();
477 1.27 tv if (LOOK_AHEAD(t, ecommt)) {
478 1.27 tv for (p = ecommt; *p; p++)
479 1.27 tv chrsave(*p);
480 1.27 tv break;
481 1.27 tv }
482 1.27 tv if (t == EOF)
483 1.27 tv break;
484 1.27 tv CHRSAVE(t);
485 1.27 tv }
486 1.27 tv } else
487 1.27 tv CHRSAVE(t); /* stack the char */
488 1.8 glass break;
489 1.8 glass }
490 1.2 glass }
491 1.8 glass }
492 1.2 glass
493 1.27 tv /*
494 1.27 tv * output string directly, without pushing it for reparses.
495 1.27 tv */
496 1.27 tv void
497 1.27 tv outputstr(s)
498 1.27 tv const char *s;
499 1.27 tv {
500 1.27 tv if (sp < 0)
501 1.27 tv while (*s)
502 1.27 tv putc(*s++, active);
503 1.27 tv else
504 1.27 tv while (*s)
505 1.27 tv CHRSAVE(*s++);
506 1.27 tv }
507 1.27 tv
508 1.8 glass /*
509 1.8 glass * build an input token..
510 1.8 glass * consider only those starting with _ or A-Za-z. This is a
511 1.8 glass * combo with lookup to speed things up.
512 1.8 glass */
513 1.27 tv static ndptr
514 1.27 tv inspect(c, tp)
515 1.27 tv int c;
516 1.13 lukem char *tp;
517 1.8 glass {
518 1.13 lukem char *name = tp;
519 1.13 lukem char *etp = tp+MAXTOK;
520 1.13 lukem ndptr p;
521 1.27 tv unsigned int h;
522 1.27 tv
523 1.27 tv h = *tp++ = c;
524 1.2 glass
525 1.27 tv while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
526 1.8 glass h = (h << 5) + h + (*tp++ = c);
527 1.27 tv if (c != EOF)
528 1.27 tv PUTBACK(c);
529 1.8 glass *tp = EOS;
530 1.27 tv /* token is too long, it won't match anything, but it can still
531 1.27 tv * be output. */
532 1.27 tv if (tp == ep) {
533 1.27 tv outputstr(name);
534 1.27 tv while (isalnum(c = gpbc()) || c == '_') {
535 1.27 tv if (sp < 0)
536 1.27 tv putc(c, active);
537 1.27 tv else
538 1.27 tv CHRSAVE(c);
539 1.27 tv }
540 1.27 tv *name = EOS;
541 1.27 tv return nil;
542 1.27 tv }
543 1.1 cgd
544 1.27 tv for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
545 1.27 tv if (h == p->hv && STREQ(name, p->name))
546 1.8 glass break;
547 1.8 glass return p;
548 1.8 glass }
549 1.7 cgd
550 1.8 glass /*
551 1.24 jdolecek * initkwds - initialise m4 keywords as fast as possible.
552 1.8 glass * This very similar to install, but without certain overheads,
553 1.24 jdolecek * such as calling lookup. Malloc is not used for storing the
554 1.13 lukem * keyword strings, since we simply use the static pointers
555 1.8 glass * within keywrds block.
556 1.8 glass */
557 1.27 tv static void
558 1.13 lukem initkwds()
559 1.13 lukem {
560 1.32 tv size_t i;
561 1.27 tv unsigned int h;
562 1.13 lukem ndptr p;
563 1.32 tv char *k;
564 1.1 cgd
565 1.8 glass for (i = 0; i < MAXKEYS; i++) {
566 1.26 wiz k = (char *)keywrds[i].knam;
567 1.27 tv if (m4prefix) {
568 1.32 tv size_t klen = strlen(k);
569 1.32 tv char *newk = malloc(klen + 4);
570 1.32 tv
571 1.33 wiz if (snprintf(newk, klen+4, "m4_%s", k) == -1)
572 1.29 tv err(1, "snprintf");
573 1.29 tv keywrds[i].knam = newk;
574 1.31 lukem k = newk;
575 1.27 tv }
576 1.22 tv h = hash(k);
577 1.8 glass p = (ndptr) xalloc(sizeof(struct ndblock));
578 1.27 tv p->nxtptr = hashtab[h % HASHSIZE];
579 1.27 tv hashtab[h % HASHSIZE] = p;
580 1.27 tv p->name = xstrdup(keywrds[i].knam);
581 1.8 glass p->defn = null;
582 1.27 tv p->hv = h;
583 1.27 tv p->type = keywrds[i].ktyp & TYPEMASK;
584 1.27 tv if ((keywrds[i].ktyp & NOARGS) == 0)
585 1.27 tv p->type |= NEEDARGS;
586 1.1 cgd }
587 1.27 tv }
588 1.27 tv
589 1.27 tv /* Look up a builtin type, even if overridden by the user */
590 1.27 tv int
591 1.27 tv builtin_type(key)
592 1.27 tv const char *key;
593 1.27 tv {
594 1.27 tv int i;
595 1.27 tv
596 1.27 tv for (i = 0; i != MAXKEYS; i++)
597 1.27 tv if (STREQ(keywrds[i].knam, key))
598 1.27 tv return keywrds[i].ktyp;
599 1.27 tv return -1;
600 1.27 tv }
601 1.27 tv
602 1.27 tv const char *
603 1.27 tv builtin_realname(n)
604 1.27 tv int n;
605 1.27 tv {
606 1.27 tv int i;
607 1.27 tv
608 1.27 tv for (i = 0; i != MAXKEYS; i++)
609 1.27 tv if (((keywrds[i].ktyp ^ n) & TYPEMASK) == 0)
610 1.27 tv return keywrds[i].knam;
611 1.27 tv return NULL;
612 1.27 tv }
613 1.27 tv
614 1.27 tv static void
615 1.27 tv record(t, lev)
616 1.27 tv struct position *t;
617 1.27 tv int lev;
618 1.27 tv {
619 1.27 tv if (lev < MAXRECORD) {
620 1.27 tv t[lev].name = CURRENT_NAME;
621 1.27 tv t[lev].line = CURRENT_LINE;
622 1.27 tv }
623 1.27 tv }
624 1.27 tv
625 1.27 tv static void
626 1.27 tv dump_stack(t, lev)
627 1.27 tv struct position *t;
628 1.27 tv int lev;
629 1.27 tv {
630 1.27 tv int i;
631 1.27 tv
632 1.27 tv for (i = 0; i < lev; i++) {
633 1.27 tv if (i == MAXRECORD) {
634 1.27 tv fprintf(stderr, " ...\n");
635 1.27 tv break;
636 1.27 tv }
637 1.27 tv fprintf(stderr, " %s at line %lu\n",
638 1.27 tv t[i].name, t[i].line);
639 1.27 tv }
640 1.27 tv }
641 1.27 tv
642 1.27 tv
643 1.27 tv static void
644 1.27 tv enlarge_stack()
645 1.27 tv {
646 1.27 tv STACKMAX *= 2;
647 1.27 tv mstack = realloc(mstack, sizeof(stae) * STACKMAX);
648 1.27 tv sstack = realloc(sstack, STACKMAX);
649 1.27 tv if (mstack == NULL || sstack == NULL)
650 1.27 tv errx(1, "Evaluation stack overflow (%lu)",
651 1.27 tv (unsigned long)STACKMAX);
652 1.8 glass }
653