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