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