Annotation of src/usr.bin/m4/main.c, Revision 1.38
1.38 ! aaron 1: /* $OpenBSD: main.c,v 1.37 2001/02/05 01:57:27 deraadt 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.38 ! aaron 50: static char rcsid[] = "$OpenBSD: main.c,v 1.37 2001/02/05 01:57:27 deraadt 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.1 deraadt 139:
1.24 espie 140: #if defined(unix) || defined(__unix__)
141: { "unix", SELFTYPE | NOARGS },
1.1 deraadt 142: #else
143: #ifdef vms
1.24 espie 144: { "vms", SELFTYPE | NOARGS },
1.1 deraadt 145: #endif
146: #endif
147: };
148:
149: #define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk))
150:
151: extern int optind;
152: extern char *optarg;
153:
1.27 espie 154: #define MAXRECORD 50
155: static struct position {
156: char *name;
157: unsigned long line;
158: } quotes[MAXRECORD], paren[MAXRECORD];
159:
160: static void record __P((struct position *, int));
161: static void dump_stack __P((struct position *, int));
162:
1.18 espie 163: static void macro __P((void));
164: static void initkwds __P((void));
1.25 espie 165: static ndptr inspect __P((char, char *));
1.18 espie 166: static int do_look_ahead __P((int, const char *));
167:
1.34 espie 168: static void enlarge_stack __P((void));
169:
1.18 espie 170: int main __P((int, char *[]));
1.1 deraadt 171:
172: int
173: main(argc,argv)
174: int argc;
175: char *argv[];
176: {
1.17 espie 177: int c;
178: int n;
1.1 deraadt 179: char *p;
1.17 espie 180: FILE *ifp;
1.1 deraadt 181:
182: if (signal(SIGINT, SIG_IGN) != SIG_IGN)
183: signal(SIGINT, onintr);
184:
185: initkwds();
1.14 espie 186: initspaces();
1.34 espie 187: STACKMAX = INITSTACKMAX;
188:
189: mstack = (stae *)xalloc(sizeof(stae) * STACKMAX);
190: sstack = (char *)xalloc(STACKMAX);
1.1 deraadt 191:
1.36 espie 192: maxout = 0;
193: outfile = NULL;
194: resizedivs(MAXOUT);
195:
1.32 espie 196: while ((c = getopt(argc, argv, "gtD:U:o:I:")) != -1)
1.1 deraadt 197: switch(c) {
198:
199: case 'D': /* define something..*/
200: for (p = optarg; *p; p++)
201: if (*p == '=')
202: break;
203: if (*p)
204: *p++ = EOS;
205: dodefine(optarg, p);
1.16 espie 206: break;
207: case 'I':
208: addtoincludepath(optarg);
1.1 deraadt 209: break;
210: case 'U': /* undefine... */
211: remhash(optarg, TOP);
1.32 espie 212: break;
213: case 'g':
214: mimic_gnu = 1;
1.1 deraadt 215: break;
1.38 ! aaron 216: case 'o':
! 217: /* XXX accept -o for compatibility */
! 218: break;
1.1 deraadt 219: case '?':
220: usage();
221: }
222:
223: argc -= optind;
224: argv += optind;
225:
226: active = stdout; /* default active output */
227: bbase[0] = bufbase;
228: if (!argc) {
229: sp = -1; /* stack pointer initialized */
230: fp = 0; /* frame pointer initialized */
1.26 espie 231: set_input(infile+0, stdin, "stdin");
232: /* default input (naturally) */
1.1 deraadt 233: macro();
234: } else
235: for (; argc--; ++argv) {
236: p = *argv;
1.13 espie 237: if (p[0] == '-' && p[1] == EOS)
1.26 espie 238: set_input(infile, stdin, "stdin");
239: else if (fopen_trypath(infile, p) == NULL)
1.11 espie 240: err(1, "%s", p);
1.1 deraadt 241: sp = -1;
242: fp = 0;
243: macro();
1.26 espie 244: release_input(infile);
1.1 deraadt 245: }
246:
247: if (*m4wraps) { /* anything for rundown ?? */
248: ilevel = 0; /* in case m4wrap includes.. */
249: bufbase = bp = buf; /* use the entire buffer */
250: pbstr(m4wraps); /* user-defined wrapup act */
251: macro(); /* last will and testament */
252: }
253:
254: if (active != stdout)
255: active = stdout; /* reset output just in case */
1.36 espie 256: for (n = 1; n < maxout; n++) /* default wrap-up: undivert */
1.1 deraadt 257: if (outfile[n] != NULL)
258: getdiv(n);
259: /* remove bitbucket if used */
260: if (outfile[0] != NULL) {
261: (void) fclose(outfile[0]);
262: }
263:
264: return 0;
265: }
266:
267: /*
1.21 espie 268: * Look ahead for `token'.
1.2 deraadt 269: * (on input `t == token[0]')
270: * Used for comment and quoting delimiters.
271: * Returns 1 if `token' present; copied to output.
272: * 0 if `token' not found; all characters pushed back
273: */
1.18 espie 274: static int
1.2 deraadt 275: do_look_ahead(t, token)
276: int t;
1.18 espie 277: const char *token;
1.2 deraadt 278: {
279: int i;
280:
1.21 espie 281: assert(t == token[0]);
1.2 deraadt 282:
283: for (i = 1; *++token; i++) {
284: t = gpbc();
285: if (t == EOF || t != *token) {
1.28 espie 286: putback(t);
1.2 deraadt 287: while (--i)
288: putback(*--token);
289: return 0;
290: }
291: }
292: return 1;
293: }
294:
295: #define LOOK_AHEAD(t, token) ((t)==(token)[0] && do_look_ahead(t,token))
296:
297: /*
1.1 deraadt 298: * macro - the work horse..
299: */
1.18 espie 300: static void
1.17 espie 301: macro()
302: {
1.34 espie 303: char token[MAXTOK+1];
1.17 espie 304: int t, l;
305: ndptr p;
306: int nlpar;
1.1 deraadt 307:
308: cycle {
1.2 deraadt 309: t = gpbc();
310: if (t == '_' || isalpha(t)) {
1.29 espie 311: p = inspect(t, token);
1.24 espie 312: if (p != nil)
313: putback(l = gpbc());
314: if (p == nil || (l != LPAREN &&
315: (p->type & NEEDARGS) != 0))
1.29 espie 316: outputstr(token);
1.1 deraadt 317: else {
318: /*
319: * real thing.. First build a call frame:
320: */
321: pushf(fp); /* previous call frm */
322: pushf(p->type); /* type of the call */
323: pushf(0); /* parenthesis level */
324: fp = sp; /* new frame pointer */
325: /*
326: * now push the string arguments:
327: */
1.34 espie 328: pushs1(p->defn); /* defn string */
329: pushs1(p->name); /* macro name */
330: pushs(ep); /* start next..*/
1.1 deraadt 331:
332: if (l != LPAREN) { /* add bracks */
333: putback(RPAREN);
334: putback(LPAREN);
335: }
336: }
337: }
338: else if (t == EOF) {
1.27 espie 339: if (sp > -1) {
340: warnx( "unexpected end of input, unclosed parenthesis:");
341: dump_stack(paren, PARLEV);
342: exit(1);
343: }
1.1 deraadt 344: if (ilevel <= 0)
345: break; /* all done thanks.. */
1.26 espie 346: release_input(infile+ilevel--);
1.1 deraadt 347: bufbase = bbase[ilevel];
348: continue;
349: }
350: /*
1.7 deraadt 351: * non-alpha token possibly seen..
1.1 deraadt 352: * [the order of else if .. stmts is important.]
353: */
1.2 deraadt 354: else if (LOOK_AHEAD(t,lquote)) { /* strip quotes */
1.27 espie 355: nlpar = 0;
356: record(quotes, nlpar++);
1.30 espie 357: /*
358: * Opening quote: scan forward until matching
359: * closing quote has been found.
360: */
1.1 deraadt 361: do {
1.7 deraadt 362:
1.2 deraadt 363: l = gpbc();
1.7 deraadt 364: if (LOOK_AHEAD(l,rquote)) {
1.29 espie 365: if (--nlpar > 0)
366: outputstr(rquote);
1.7 deraadt 367: } else if (LOOK_AHEAD(l,lquote)) {
1.27 espie 368: record(quotes, nlpar++);
1.29 espie 369: outputstr(lquote);
1.17 espie 370: } else if (l == EOF) {
371: if (nlpar == 1)
1.27 espie 372: warnx("unclosed quote:");
1.17 espie 373: else
1.27 espie 374: warnx("%d unclosed quotes:", nlpar);
375: dump_stack(quotes, nlpar);
376: exit(1);
1.17 espie 377: } else {
1.29 espie 378: if (nlpar > 0) {
379: if (sp < 0)
380: putc(l, active);
381: else
382: chrsave(l);
383: }
1.7 deraadt 384: }
1.1 deraadt 385: }
386: while (nlpar != 0);
387: }
388:
1.2 deraadt 389: else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
1.21 espie 390: fputs(scommt, active);
1.2 deraadt 391:
392: for(;;) {
393: t = gpbc();
394: if (LOOK_AHEAD(t, ecommt)) {
1.21 espie 395: fputs(ecommt, active);
1.2 deraadt 396: break;
397: }
398: if (t == EOF)
399: break;
1.1 deraadt 400: putc(t, active);
401: }
1.2 deraadt 402: }
403:
404: else if (sp < 0) { /* not in a macro at all */
1.1 deraadt 405: putc(t, active); /* output directly.. */
406: }
407:
408: else switch(t) {
409:
410: case LPAREN:
411: if (PARLEV > 0)
412: chrsave(t);
413: while (isspace(l = gpbc()))
414: ; /* skip blank, tab, nl.. */
415: putback(l);
1.27 espie 416: record(paren, PARLEV++);
1.1 deraadt 417: break;
418:
419: case RPAREN:
420: if (--PARLEV > 0)
421: chrsave(t);
422: else { /* end of argument list */
423: chrsave(EOS);
424:
425: if (sp == STACKMAX)
1.11 espie 426: errx(1, "internal stack overflow");
1.1 deraadt 427:
428: if (CALTYP == MACRTYPE)
1.18 espie 429: expand((const char **) mstack+fp+1, sp-fp);
1.1 deraadt 430: else
1.18 espie 431: eval((const char **) mstack+fp+1, sp-fp, CALTYP);
1.1 deraadt 432:
433: ep = PREVEP; /* flush strspace */
434: sp = PREVSP; /* previous sp.. */
435: fp = PREVFP; /* rewind stack...*/
436: }
437: break;
438:
439: case COMMA:
440: if (PARLEV == 1) {
441: chrsave(EOS); /* new argument */
442: while (isspace(l = gpbc()))
443: ;
444: putback(l);
445: pushs(ep);
446: } else
447: chrsave(t);
448: break;
449:
450: default:
1.22 espie 451: if (LOOK_AHEAD(t, scommt)) {
452: char *p;
453: for (p = scommt; *p; p++)
454: chrsave(*p);
455: for(;;) {
456: t = gpbc();
457: if (LOOK_AHEAD(t, ecommt)) {
458: for (p = ecommt; *p; p++)
459: chrsave(*p);
460: break;
461: }
462: if (t == EOF)
463: break;
464: chrsave(t);
465: }
466: } else
467: chrsave(t); /* stack the char */
1.1 deraadt 468: break;
469: }
470: }
471: }
472:
1.24 espie 473: /*
474: * output string directly, without pushing it for reparses.
475: */
476: void
477: outputstr(s)
478: const char *s;
479: {
480: if (sp < 0)
481: while (*s)
482: putc(*s++, active);
483: else
484: while (*s)
485: chrsave(*s++);
486: }
487:
1.1 deraadt 488: /*
489: * build an input token..
490: * consider only those starting with _ or A-Za-z. This is a
491: * combo with lookup to speed things up.
492: */
1.18 espie 493: static ndptr
1.25 espie 494: inspect(c, tp)
495: char c;
1.17 espie 496: char *tp;
1.1 deraadt 497: {
1.17 espie 498: char *name = tp;
499: char *etp = tp+MAXTOK;
500: ndptr p;
1.25 espie 501: unsigned int h;
502:
503: h = *tp++ = c;
1.1 deraadt 504:
505: while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
506: h = (h << 5) + h + (*tp++ = c);
507: putback(c);
508: *tp = EOS;
1.33 espie 509: /* token is too long, it won't match anything, but it can still
510: * be output. */
511: if (tp == ep) {
512: outputstr(name);
513: while (isalnum(c = gpbc()) || c == '_') {
514: if (sp < 0)
515: putc(c, active);
516: else
517: chrsave(c);
518: }
519: *name = EOS;
520: return nil;
521: }
1.1 deraadt 522:
1.19 espie 523: for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
524: if (h == p->hv && STREQ(name, p->name))
1.1 deraadt 525: break;
526: return p;
527: }
528:
529: /*
530: * initkwds - initialise m4 keywords as fast as possible.
531: * This very similar to install, but without certain overheads,
532: * such as calling lookup. Malloc is not used for storing the
1.17 espie 533: * keyword strings, since we simply use the static pointers
1.1 deraadt 534: * within keywrds block.
535: */
1.18 espie 536: static void
1.17 espie 537: initkwds()
538: {
539: size_t i;
1.20 millert 540: unsigned int h;
1.17 espie 541: ndptr p;
1.1 deraadt 542:
543: for (i = 0; i < MAXKEYS; i++) {
544: h = hash(keywrds[i].knam);
545: p = (ndptr) xalloc(sizeof(struct ndblock));
1.19 espie 546: p->nxtptr = hashtab[h % HASHSIZE];
547: hashtab[h % HASHSIZE] = p;
1.1 deraadt 548: p->name = keywrds[i].knam;
549: p->defn = null;
1.19 espie 550: p->hv = h;
1.24 espie 551: p->type = (keywrds[i].ktyp & TYPEMASK) | STATIC;
552: if ((keywrds[i].ktyp & NOARGS) == 0)
553: p->type |= NEEDARGS;
1.1 deraadt 554: }
555: }
1.31 espie 556:
557: /* Look up a builtin type, even if overridden by the user */
558: int
559: builtin_type(key)
560: const char *key;
561: {
562: int i;
563:
564: for (i = 0; i != MAXKEYS; i++)
565: if (STREQ(keywrds[i].knam, key))
566: return keywrds[i].ktyp;
567: return -1;
568: }
569:
1.17 espie 570:
1.27 espie 571: static void
572: record(t, lev)
573: struct position *t;
574: int lev;
575: {
576: if (lev < MAXRECORD) {
577: t[lev].name = CURRENT_NAME;
578: t[lev].line = CURRENT_LINE;
579: }
580: }
581:
582: static void
583: dump_stack(t, lev)
584: struct position *t;
585: int lev;
586: {
587: int i;
588:
589: for (i = 0; i < lev; i++) {
590: if (i == MAXRECORD) {
591: fprintf(stderr, " ...\n");
592: break;
593: }
594: fprintf(stderr, " %s at line %lu\n",
595: t[i].name, t[i].line);
596: }
1.34 espie 597: }
598:
599:
600: static void
601: enlarge_stack()
602: {
603: STACKMAX *= 2;
1.37 deraadt 604: fprintf(stderr, "%lu\n", (unsigned long)STACKMAX);
1.34 espie 605: mstack = realloc(mstack, sizeof(stae) * STACKMAX);
606: sstack = realloc(sstack, STACKMAX);
607: if (mstack == NULL || sstack == NULL)
608: errx(1, "Evaluation stack overflow (%lu)",
609: (unsigned long)STACKMAX);
1.27 espie 610: }