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