Annotation of src/usr.bin/m4/main.c, Revision 1.70
1.70 ! espie 1: /* $OpenBSD: main.c,v 1.69 2006/01/20 23:10:19 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: /*
37: * main.c
38: * Facility: m4 macro processor
39: * by: oz
40: */
41:
1.21 espie 42: #include <assert.h>
1.1 deraadt 43: #include <signal.h>
44: #include <errno.h>
45: #include <unistd.h>
46: #include <stdio.h>
47: #include <ctype.h>
48: #include <string.h>
1.13 espie 49: #include <stddef.h>
1.69 espie 50: #include <stdint.h>
1.34 espie 51: #include <stdlib.h>
1.63 espie 52: #include <ohash.h>
1.1 deraadt 53: #include "mdef.h"
54: #include "stdd.h"
55: #include "extern.h"
56: #include "pathnames.h"
57:
58: ndptr hashtab[HASHSIZE]; /* hash table for macros etc. */
1.34 espie 59: stae *mstack; /* stack of m4 machine */
60: char *sstack; /* shadow stack, for string space extension */
61: static size_t STACKMAX; /* current maximum size of stack */
1.1 deraadt 62: int sp; /* current m4 stack pointer */
63: int fp; /* m4 call frame pointer */
1.26 espie 64: struct input_file infile[MAXINP];/* input file stack (0=stdin) */
1.36 espie 65: FILE **outfile; /* diversion array(0=bitbucket)*/
66: int maxout;
1.1 deraadt 67: FILE *active; /* active output file pointer */
68: int ilevel = 0; /* input file stack pointer */
69: int oindex = 0; /* diversion index.. */
70: char *null = ""; /* as it says.. just a null.. */
1.66 espie 71: char **m4wraps = NULL; /* m4wraps array. */
72: int maxwraps = 0; /* size of m4wraps array */
73: int wrapindex = 0; /* current offset in m4wraps */
1.2 deraadt 74: char lquote[MAXCCHARS+1] = {LQUOTE}; /* left quote character (`) */
75: char rquote[MAXCCHARS+1] = {RQUOTE}; /* right quote character (') */
76: char scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */
77: char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */
1.54 espie 78: int synch_lines = 0; /* line synchronisation for C preprocessor */
1.1 deraadt 79:
1.63 espie 80: struct keyblk {
81: char *knam; /* keyword name */
82: int ktyp; /* keyword type */
83: };
84:
1.1 deraadt 85: struct keyblk keywrds[] = { /* m4 keywords to be installed */
1.8 millert 86: { "include", INCLTYPE },
87: { "sinclude", SINCTYPE },
88: { "define", DEFITYPE },
89: { "defn", DEFNTYPE },
1.24 espie 90: { "divert", DIVRTYPE | NOARGS },
1.8 millert 91: { "expr", EXPRTYPE },
92: { "eval", EXPRTYPE },
93: { "substr", SUBSTYPE },
94: { "ifelse", IFELTYPE },
95: { "ifdef", IFDFTYPE },
96: { "len", LENGTYPE },
97: { "incr", INCRTYPE },
98: { "decr", DECRTYPE },
1.24 espie 99: { "dnl", DNLNTYPE | NOARGS },
100: { "changequote", CHNQTYPE | NOARGS },
101: { "changecom", CHNCTYPE | NOARGS },
1.8 millert 102: { "index", INDXTYPE },
1.1 deraadt 103: #ifdef EXTENDED
1.8 millert 104: { "paste", PASTTYPE },
105: { "spaste", SPASTYPE },
1.31 espie 106: /* Newer extensions, needed to handle gnu-m4 scripts */
107: { "indir", INDIRTYPE},
108: { "builtin", BUILTINTYPE},
109: { "patsubst", PATSTYPE},
110: { "regexp", REGEXPTYPE},
1.35 espie 111: { "esyscmd", ESYSCMDTYPE},
1.31 espie 112: { "__file__", FILENAMETYPE | NOARGS},
113: { "__line__", LINETYPE | NOARGS},
1.1 deraadt 114: #endif
1.8 millert 115: { "popdef", POPDTYPE },
116: { "pushdef", PUSDTYPE },
1.24 espie 117: { "dumpdef", DUMPTYPE | NOARGS },
118: { "shift", SHIFTYPE | NOARGS },
1.8 millert 119: { "translit", TRNLTYPE },
120: { "undefine", UNDFTYPE },
1.24 espie 121: { "undivert", UNDVTYPE | NOARGS },
122: { "divnum", DIVNTYPE | NOARGS },
1.8 millert 123: { "maketemp", MKTMTYPE },
1.24 espie 124: { "errprint", ERRPTYPE | NOARGS },
125: { "m4wrap", M4WRTYPE | NOARGS },
126: { "m4exit", EXITTYPE | NOARGS },
1.8 millert 127: { "syscmd", SYSCTYPE },
1.24 espie 128: { "sysval", SYSVTYPE | NOARGS },
1.49 espie 129: { "traceon", TRACEONTYPE | NOARGS },
130: { "traceoff", TRACEOFFTYPE | NOARGS },
1.1 deraadt 131:
1.24 espie 132: #if defined(unix) || defined(__unix__)
133: { "unix", SELFTYPE | NOARGS },
1.1 deraadt 134: #else
135: #ifdef vms
1.24 espie 136: { "vms", SELFTYPE | NOARGS },
1.1 deraadt 137: #endif
138: #endif
139: };
140:
141: #define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk))
142:
143: extern int optind;
144: extern char *optarg;
145:
1.27 espie 146: #define MAXRECORD 50
147: static struct position {
148: char *name;
149: unsigned long line;
150: } quotes[MAXRECORD], paren[MAXRECORD];
151:
1.52 millert 152: static void record(struct position *, int);
153: static void dump_stack(struct position *, int);
1.27 espie 154:
1.52 millert 155: static void macro(void);
156: static void initkwds(void);
157: static ndptr inspect(int, char *);
158: static int do_look_ahead(int, const char *);
1.54 espie 159: static void reallyoutputstr(const char *);
160: static void reallyputchar(int);
1.18 espie 161:
1.52 millert 162: static void enlarge_stack(void);
1.34 espie 163:
1.52 millert 164: int main(int, char *[]);
1.1 deraadt 165:
166: int
1.53 espie 167: main(int argc, char *argv[])
1.1 deraadt 168: {
1.17 espie 169: int c;
170: int n;
1.1 deraadt 171: char *p;
172:
173: if (signal(SIGINT, SIG_IGN) != SIG_IGN)
174: signal(SIGINT, onintr);
175:
1.61 espie 176: init_macros();
1.1 deraadt 177: initkwds();
1.14 espie 178: initspaces();
1.34 espie 179: STACKMAX = INITSTACKMAX;
180:
1.64 espie 181: mstack = (stae *)xalloc(sizeof(stae) * STACKMAX, NULL);
182: sstack = (char *)xalloc(STACKMAX, NULL);
1.1 deraadt 183:
1.36 espie 184: maxout = 0;
185: outfile = NULL;
186: resizedivs(MAXOUT);
187:
1.54 espie 188: while ((c = getopt(argc, argv, "gst:d:D:U:o:I:")) != -1)
1.1 deraadt 189: switch(c) {
190:
191: case 'D': /* define something..*/
192: for (p = optarg; *p; p++)
193: if (*p == '=')
194: break;
195: if (*p)
196: *p++ = EOS;
197: dodefine(optarg, p);
1.16 espie 198: break;
199: case 'I':
200: addtoincludepath(optarg);
1.1 deraadt 201: break;
202: case 'U': /* undefine... */
1.59 espie 203: macro_popdef(optarg);
1.32 espie 204: break;
205: case 'g':
206: mimic_gnu = 1;
1.1 deraadt 207: break;
1.46 espie 208: case 'd':
209: set_trace_flags(optarg);
1.47 espie 210: break;
1.54 espie 211: case 's':
212: synch_lines = 1;
213: break;
1.47 espie 214: case 't':
1.49 espie 215: mark_traced(optarg, 1);
1.46 espie 216: break;
1.38 aaron 217: case 'o':
1.46 espie 218: trace_file(optarg);
1.38 aaron 219: break;
1.1 deraadt 220: case '?':
221: usage();
222: }
223:
224: argc -= optind;
225: argv += optind;
226:
227: active = stdout; /* default active output */
228: bbase[0] = bufbase;
229: if (!argc) {
230: sp = -1; /* stack pointer initialized */
231: fp = 0; /* frame pointer initialized */
1.26 espie 232: set_input(infile+0, stdin, "stdin");
233: /* default input (naturally) */
1.1 deraadt 234: macro();
235: } else
236: for (; argc--; ++argv) {
237: p = *argv;
1.13 espie 238: if (p[0] == '-' && p[1] == EOS)
1.26 espie 239: set_input(infile, stdin, "stdin");
240: else if (fopen_trypath(infile, p) == NULL)
1.11 espie 241: err(1, "%s", p);
1.1 deraadt 242: sp = -1;
243: fp = 0;
244: macro();
1.26 espie 245: release_input(infile);
1.1 deraadt 246: }
247:
1.66 espie 248: if (wrapindex) {
249: int i;
250:
1.1 deraadt 251: ilevel = 0; /* in case m4wrap includes.. */
252: bufbase = bp = buf; /* use the entire buffer */
1.66 espie 253: if (mimic_gnu) {
254: while (wrapindex != 0) {
255: for (i = 0; i < wrapindex; i++)
256: pbstr(m4wraps[i]);
257: wrapindex =0;
258: macro();
259: }
260: } else {
261: for (i = 0; i < wrapindex; i++) {
262: pbstr(m4wraps[i]);
263: macro();
264: }
265: }
1.1 deraadt 266: }
267:
268: if (active != stdout)
269: active = stdout; /* reset output just in case */
1.36 espie 270: for (n = 1; n < maxout; n++) /* default wrap-up: undivert */
1.1 deraadt 271: if (outfile[n] != NULL)
272: getdiv(n);
273: /* remove bitbucket if used */
274: if (outfile[0] != NULL) {
275: (void) fclose(outfile[0]);
276: }
277:
278: return 0;
279: }
280:
281: /*
1.21 espie 282: * Look ahead for `token'.
1.2 deraadt 283: * (on input `t == token[0]')
284: * Used for comment and quoting delimiters.
285: * Returns 1 if `token' present; copied to output.
286: * 0 if `token' not found; all characters pushed back
287: */
1.18 espie 288: static int
1.53 espie 289: do_look_ahead(int t, const char *token)
1.2 deraadt 290: {
291: int i;
292:
1.43 espie 293: assert((unsigned char)t == (unsigned char)token[0]);
1.2 deraadt 294:
295: for (i = 1; *++token; i++) {
296: t = gpbc();
1.43 espie 297: if (t == EOF || (unsigned char)t != (unsigned char)*token) {
1.68 espie 298: pushback(t);
1.2 deraadt 299: while (--i)
1.68 espie 300: pushback(*--token);
1.2 deraadt 301: return 0;
302: }
303: }
304: return 1;
305: }
306:
1.43 espie 307: #define LOOK_AHEAD(t, token) (t != EOF && \
308: (unsigned char)(t)==(unsigned char)(token)[0] && \
309: do_look_ahead(t,token))
1.2 deraadt 310:
311: /*
1.1 deraadt 312: * macro - the work horse..
313: */
1.18 espie 314: static void
1.56 deraadt 315: macro(void)
1.17 espie 316: {
1.34 espie 317: char token[MAXTOK+1];
1.17 espie 318: int t, l;
319: ndptr p;
320: int nlpar;
1.1 deraadt 321:
322: cycle {
1.2 deraadt 323: t = gpbc();
1.68 espie 324:
325: if (LOOK_AHEAD(t,lquote)) { /* strip quotes */
326: nlpar = 0;
327: record(quotes, nlpar++);
328: /*
329: * Opening quote: scan forward until matching
330: * closing quote has been found.
331: */
332: do {
333:
334: l = gpbc();
335: if (LOOK_AHEAD(l,rquote)) {
336: if (--nlpar > 0)
337: outputstr(rquote);
338: } else if (LOOK_AHEAD(l,lquote)) {
339: record(quotes, nlpar++);
340: outputstr(lquote);
341: } else if (l == EOF) {
342: if (nlpar == 1)
343: warnx("unclosed quote:");
344: else
345: warnx("%d unclosed quotes:", nlpar);
346: dump_stack(quotes, nlpar);
347: exit(1);
348: } else {
349: if (nlpar > 0) {
350: if (sp < 0)
351: reallyputchar(l);
352: else
353: CHRSAVE(l);
354: }
355: }
356: }
357: while (nlpar != 0);
358: } else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
359: reallyoutputstr(scommt);
360:
361: for(;;) {
362: t = gpbc();
363: if (LOOK_AHEAD(t, ecommt)) {
364: reallyoutputstr(ecommt);
365: break;
366: }
367: if (t == EOF)
368: break;
369: reallyputchar(t);
370: }
371: } else if (t == '_' || isalpha(t)) {
1.29 espie 372: p = inspect(t, token);
1.59 espie 373: if (p != NULL)
1.68 espie 374: pushback(l = gpbc());
1.59 espie 375: if (p == NULL || (l != LPAREN &&
376: (macro_getdef(p)->type & NEEDARGS) != 0))
1.29 espie 377: outputstr(token);
1.1 deraadt 378: else {
379: /*
380: * real thing.. First build a call frame:
381: */
382: pushf(fp); /* previous call frm */
1.59 espie 383: pushf(macro_getdef(p)->type); /* type of the call */
1.63 espie 384: pushf(is_traced(p));
1.1 deraadt 385: pushf(0); /* parenthesis level */
386: fp = sp; /* new frame pointer */
387: /*
388: * now push the string arguments:
389: */
1.59 espie 390: pushs1(macro_getdef(p)->defn); /* defn string */
391: pushs1((char *)macro_name(p)); /* macro name */
1.34 espie 392: pushs(ep); /* start next..*/
1.1 deraadt 393:
1.41 espie 394: if (l != LPAREN && PARLEV == 0) {
395: /* no bracks */
396: chrsave(EOS);
397:
398: if (sp == STACKMAX)
399: errx(1, "internal stack overflow");
1.44 espie 400: eval((const char **) mstack+fp+1, 2,
1.60 espie 401: CALTYP, TRACESTATUS);
1.41 espie 402:
403: ep = PREVEP; /* flush strspace */
404: sp = PREVSP; /* previous sp.. */
405: fp = PREVFP; /* rewind stack...*/
1.1 deraadt 406: }
407: }
1.41 espie 408: } else if (t == EOF) {
1.27 espie 409: if (sp > -1) {
410: warnx( "unexpected end of input, unclosed parenthesis:");
411: dump_stack(paren, PARLEV);
412: exit(1);
413: }
1.1 deraadt 414: if (ilevel <= 0)
415: break; /* all done thanks.. */
1.26 espie 416: release_input(infile+ilevel--);
1.54 espie 417: emit_synchline();
1.1 deraadt 418: bufbase = bbase[ilevel];
419: continue;
1.68 espie 420: } else if (sp < 0) { /* not in a macro at all */
1.54 espie 421: reallyputchar(t); /* output directly.. */
1.1 deraadt 422: }
423:
424: else switch(t) {
425:
426: case LPAREN:
427: if (PARLEV > 0)
428: chrsave(t);
429: while (isspace(l = gpbc()))
430: ; /* skip blank, tab, nl.. */
1.68 espie 431: pushback(l);
1.27 espie 432: record(paren, PARLEV++);
1.1 deraadt 433: break;
434:
435: case RPAREN:
436: if (--PARLEV > 0)
437: chrsave(t);
438: else { /* end of argument list */
439: chrsave(EOS);
440:
441: if (sp == STACKMAX)
1.11 espie 442: errx(1, "internal stack overflow");
1.1 deraadt 443:
1.44 espie 444: eval((const char **) mstack+fp+1, sp-fp,
1.60 espie 445: CALTYP, TRACESTATUS);
1.1 deraadt 446:
447: ep = PREVEP; /* flush strspace */
448: sp = PREVSP; /* previous sp.. */
449: fp = PREVFP; /* rewind stack...*/
450: }
451: break;
452:
453: case COMMA:
454: if (PARLEV == 1) {
455: chrsave(EOS); /* new argument */
456: while (isspace(l = gpbc()))
457: ;
1.68 espie 458: pushback(l);
1.1 deraadt 459: pushs(ep);
460: } else
461: chrsave(t);
462: break;
463:
464: default:
1.22 espie 465: if (LOOK_AHEAD(t, scommt)) {
466: char *p;
467: for (p = scommt; *p; p++)
468: chrsave(*p);
469: for(;;) {
470: t = gpbc();
471: if (LOOK_AHEAD(t, ecommt)) {
472: for (p = ecommt; *p; p++)
473: chrsave(*p);
474: break;
475: }
476: if (t == EOF)
477: break;
1.48 espie 478: CHRSAVE(t);
1.22 espie 479: }
480: } else
1.48 espie 481: CHRSAVE(t); /* stack the char */
1.1 deraadt 482: break;
483: }
484: }
485: }
486:
1.24 espie 487: /*
488: * output string directly, without pushing it for reparses.
489: */
490: void
1.53 espie 491: outputstr(const char *s)
1.24 espie 492: {
493: if (sp < 0)
1.54 espie 494: reallyoutputstr(s);
1.24 espie 495: else
496: while (*s)
1.48 espie 497: CHRSAVE(*s++);
1.24 espie 498: }
499:
1.54 espie 500: void
501: reallyoutputstr(const char *s)
502: {
503: if (synch_lines) {
504: while (*s) {
505: fputc(*s, active);
506: if (*s++ == '\n') {
507: infile[ilevel].synch_lineno++;
508: if (infile[ilevel].synch_lineno !=
509: infile[ilevel].lineno)
510: do_emit_synchline();
511: }
512: }
513: } else
514: fputs(s, active);
515: }
516:
517: void
518: reallyputchar(int c)
519: {
520: putc(c, active);
521: if (synch_lines && c == '\n') {
522: infile[ilevel].synch_lineno++;
523: if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
524: do_emit_synchline();
525: }
526: }
527:
1.1 deraadt 528: /*
529: * build an input token..
1.59 espie 530: * consider only those starting with _ or A-Za-z.
1.1 deraadt 531: */
1.18 espie 532: static ndptr
1.53 espie 533: inspect(int c, char *tp)
1.1 deraadt 534: {
1.17 espie 535: char *name = tp;
536: char *etp = tp+MAXTOK;
537: ndptr p;
1.25 espie 538:
1.59 espie 539: *tp++ = c;
1.1 deraadt 540:
541: while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
1.59 espie 542: *tp++ = c;
1.51 espie 543: if (c != EOF)
1.68 espie 544: PUSHBACK(c);
1.1 deraadt 545: *tp = EOS;
1.33 espie 546: /* token is too long, it won't match anything, but it can still
547: * be output. */
548: if (tp == ep) {
549: outputstr(name);
550: while (isalnum(c = gpbc()) || c == '_') {
551: if (sp < 0)
1.54 espie 552: reallyputchar(c);
1.33 espie 553: else
1.48 espie 554: CHRSAVE(c);
1.33 espie 555: }
556: *name = EOS;
1.59 espie 557: return NULL;
1.33 espie 558: }
1.1 deraadt 559:
1.63 espie 560: p = ohash_find(¯os, ohash_qlookupi(¯os, name, (const char **)&tp));
1.61 espie 561: if (p == NULL)
562: return NULL;
563: if (macro_getdef(p) == NULL)
564: return NULL;
565: return p;
1.1 deraadt 566: }
567:
568: /*
569: * initkwds - initialise m4 keywords as fast as possible.
570: * This very similar to install, but without certain overheads,
571: * such as calling lookup. Malloc is not used for storing the
1.17 espie 572: * keyword strings, since we simply use the static pointers
1.1 deraadt 573: * within keywrds block.
574: */
1.18 espie 575: static void
1.56 deraadt 576: initkwds(void)
1.17 espie 577: {
1.59 espie 578: unsigned int type;
579: int i;
1.1 deraadt 580:
581: for (i = 0; i < MAXKEYS; i++) {
1.59 espie 582: type = keywrds[i].ktyp & TYPEMASK;
1.24 espie 583: if ((keywrds[i].ktyp & NOARGS) == 0)
1.59 espie 584: type |= NEEDARGS;
585: setup_builtin(keywrds[i].knam, type);
1.1 deraadt 586: }
1.45 espie 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
1.56 deraadt 615: enlarge_stack(void)
1.34 espie 616: {
1.64 espie 617: STACKMAX += STACKMAX/2;
618: mstack = xrealloc(mstack, sizeof(stae) * STACKMAX,
619: "Evaluation stack overflow (%lu)",
620: (unsigned long)STACKMAX);
621: sstack = xrealloc(sstack, STACKMAX,
622: "Evaluation stack overflow (%lu)",
623: (unsigned long)STACKMAX);
1.27 espie 624: }