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