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