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