Annotation of src/usr.bin/m4/main.c, Revision 1.31
1.31 ! espie 1: /* $OpenBSD: main.c,v 1.30 2000/02/02 14:05:22 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.31 ! espie 50: static char rcsid[] = "$OpenBSD: main.c,v 1.30 2000/02/02 14:05:22 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.16 espie 181: while ((c = getopt(argc, argv, "tD: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);
197: break;
198: case 'o': /* specific output */
199: case '?':
200: usage();
201: }
202:
203: argc -= optind;
204: argv += optind;
205:
206: active = stdout; /* default active output */
207: bbase[0] = bufbase;
208: if (!argc) {
209: sp = -1; /* stack pointer initialized */
210: fp = 0; /* frame pointer initialized */
1.26 espie 211: set_input(infile+0, stdin, "stdin");
212: /* default input (naturally) */
1.1 deraadt 213: macro();
214: } else
215: for (; argc--; ++argv) {
216: p = *argv;
1.13 espie 217: if (p[0] == '-' && p[1] == EOS)
1.26 espie 218: set_input(infile, stdin, "stdin");
219: else if (fopen_trypath(infile, p) == NULL)
1.11 espie 220: err(1, "%s", p);
1.1 deraadt 221: sp = -1;
222: fp = 0;
223: macro();
1.26 espie 224: release_input(infile);
1.1 deraadt 225: }
226:
227: if (*m4wraps) { /* anything for rundown ?? */
228: ilevel = 0; /* in case m4wrap includes.. */
229: bufbase = bp = buf; /* use the entire buffer */
230: pbstr(m4wraps); /* user-defined wrapup act */
231: macro(); /* last will and testament */
232: }
233:
234: if (active != stdout)
235: active = stdout; /* reset output just in case */
236: for (n = 1; n < MAXOUT; n++) /* default wrap-up: undivert */
237: if (outfile[n] != NULL)
238: getdiv(n);
239: /* remove bitbucket if used */
240: if (outfile[0] != NULL) {
241: (void) fclose(outfile[0]);
242: }
243:
244: return 0;
245: }
246:
247: /*
1.21 espie 248: * Look ahead for `token'.
1.2 deraadt 249: * (on input `t == token[0]')
250: * Used for comment and quoting delimiters.
251: * Returns 1 if `token' present; copied to output.
252: * 0 if `token' not found; all characters pushed back
253: */
1.18 espie 254: static int
1.2 deraadt 255: do_look_ahead(t, token)
256: int t;
1.18 espie 257: const char *token;
1.2 deraadt 258: {
259: int i;
260:
1.21 espie 261: assert(t == token[0]);
1.2 deraadt 262:
263: for (i = 1; *++token; i++) {
264: t = gpbc();
265: if (t == EOF || t != *token) {
1.28 espie 266: putback(t);
1.2 deraadt 267: while (--i)
268: putback(*--token);
269: return 0;
270: }
271: }
272: return 1;
273: }
274:
275: #define LOOK_AHEAD(t, token) ((t)==(token)[0] && do_look_ahead(t,token))
276:
277: /*
1.1 deraadt 278: * macro - the work horse..
279: */
1.18 espie 280: static void
1.17 espie 281: macro()
282: {
1.29 espie 283: char token[MAXTOK];
1.17 espie 284: int t, l;
285: ndptr p;
286: int nlpar;
1.1 deraadt 287:
288: cycle {
1.2 deraadt 289: t = gpbc();
290: if (t == '_' || isalpha(t)) {
1.29 espie 291: p = inspect(t, token);
1.24 espie 292: if (p != nil)
293: putback(l = gpbc());
294: if (p == nil || (l != LPAREN &&
295: (p->type & NEEDARGS) != 0))
1.29 espie 296: outputstr(token);
1.1 deraadt 297: else {
298: /*
299: * real thing.. First build a call frame:
300: */
301: pushf(fp); /* previous call frm */
302: pushf(p->type); /* type of the call */
303: pushf(0); /* parenthesis level */
304: fp = sp; /* new frame pointer */
305: /*
306: * now push the string arguments:
307: */
308: pushs(p->defn); /* defn string */
309: pushs(p->name); /* macro name */
310: pushs(ep); /* start next..*/
311:
312: if (l != LPAREN) { /* add bracks */
313: putback(RPAREN);
314: putback(LPAREN);
315: }
316: }
317: }
318: else if (t == EOF) {
1.27 espie 319: if (sp > -1) {
320: warnx( "unexpected end of input, unclosed parenthesis:");
321: dump_stack(paren, PARLEV);
322: exit(1);
323: }
1.1 deraadt 324: if (ilevel <= 0)
325: break; /* all done thanks.. */
1.26 espie 326: release_input(infile+ilevel--);
1.1 deraadt 327: bufbase = bbase[ilevel];
328: continue;
329: }
330: /*
1.7 deraadt 331: * non-alpha token possibly seen..
1.1 deraadt 332: * [the order of else if .. stmts is important.]
333: */
1.2 deraadt 334: else if (LOOK_AHEAD(t,lquote)) { /* strip quotes */
1.27 espie 335: nlpar = 0;
336: record(quotes, nlpar++);
1.30 espie 337: /*
338: * Opening quote: scan forward until matching
339: * closing quote has been found.
340: */
1.1 deraadt 341: do {
1.7 deraadt 342:
1.2 deraadt 343: l = gpbc();
1.7 deraadt 344: if (LOOK_AHEAD(l,rquote)) {
1.29 espie 345: if (--nlpar > 0)
346: outputstr(rquote);
1.7 deraadt 347: } else if (LOOK_AHEAD(l,lquote)) {
1.27 espie 348: record(quotes, nlpar++);
1.29 espie 349: outputstr(lquote);
1.17 espie 350: } else if (l == EOF) {
351: if (nlpar == 1)
1.27 espie 352: warnx("unclosed quote:");
1.17 espie 353: else
1.27 espie 354: warnx("%d unclosed quotes:", nlpar);
355: dump_stack(quotes, nlpar);
356: exit(1);
1.17 espie 357: } else {
1.29 espie 358: if (nlpar > 0) {
359: if (sp < 0)
360: putc(l, active);
361: else
362: chrsave(l);
363: }
1.7 deraadt 364: }
1.1 deraadt 365: }
366: while (nlpar != 0);
367: }
368:
1.2 deraadt 369: else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
1.21 espie 370: fputs(scommt, active);
1.2 deraadt 371:
372: for(;;) {
373: t = gpbc();
374: if (LOOK_AHEAD(t, ecommt)) {
1.21 espie 375: fputs(ecommt, active);
1.2 deraadt 376: break;
377: }
378: if (t == EOF)
379: break;
1.1 deraadt 380: putc(t, active);
381: }
1.2 deraadt 382: }
383:
384: else if (sp < 0) { /* not in a macro at all */
1.1 deraadt 385: putc(t, active); /* output directly.. */
386: }
387:
388: else switch(t) {
389:
390: case LPAREN:
391: if (PARLEV > 0)
392: chrsave(t);
393: while (isspace(l = gpbc()))
394: ; /* skip blank, tab, nl.. */
395: putback(l);
1.27 espie 396: record(paren, PARLEV++);
1.1 deraadt 397: break;
398:
399: case RPAREN:
400: if (--PARLEV > 0)
401: chrsave(t);
402: else { /* end of argument list */
403: chrsave(EOS);
404:
405: if (sp == STACKMAX)
1.11 espie 406: errx(1, "internal stack overflow");
1.1 deraadt 407:
408: if (CALTYP == MACRTYPE)
1.18 espie 409: expand((const char **) mstack+fp+1, sp-fp);
1.1 deraadt 410: else
1.18 espie 411: eval((const char **) mstack+fp+1, sp-fp, CALTYP);
1.1 deraadt 412:
413: ep = PREVEP; /* flush strspace */
414: sp = PREVSP; /* previous sp.. */
415: fp = PREVFP; /* rewind stack...*/
416: }
417: break;
418:
419: case COMMA:
420: if (PARLEV == 1) {
421: chrsave(EOS); /* new argument */
422: while (isspace(l = gpbc()))
423: ;
424: putback(l);
425: pushs(ep);
426: } else
427: chrsave(t);
428: break;
429:
430: default:
1.22 espie 431: if (LOOK_AHEAD(t, scommt)) {
432: char *p;
433: for (p = scommt; *p; p++)
434: chrsave(*p);
435: for(;;) {
436: t = gpbc();
437: if (LOOK_AHEAD(t, ecommt)) {
438: for (p = ecommt; *p; p++)
439: chrsave(*p);
440: break;
441: }
442: if (t == EOF)
443: break;
444: chrsave(t);
445: }
446: } else
447: chrsave(t); /* stack the char */
1.1 deraadt 448: break;
449: }
450: }
451: }
452:
1.24 espie 453: /*
454: * output string directly, without pushing it for reparses.
455: */
456: void
457: outputstr(s)
458: const char *s;
459: {
460: if (sp < 0)
461: while (*s)
462: putc(*s++, active);
463: else
464: while (*s)
465: chrsave(*s++);
466: }
467:
1.1 deraadt 468: /*
469: * build an input token..
470: * consider only those starting with _ or A-Za-z. This is a
471: * combo with lookup to speed things up.
472: */
1.18 espie 473: static ndptr
1.25 espie 474: inspect(c, tp)
475: char c;
1.17 espie 476: char *tp;
1.1 deraadt 477: {
1.17 espie 478: char *name = tp;
479: char *etp = tp+MAXTOK;
480: ndptr p;
1.25 espie 481: unsigned int h;
482:
483: h = *tp++ = c;
1.1 deraadt 484:
485: while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
486: h = (h << 5) + h + (*tp++ = c);
487: putback(c);
488: if (tp == etp)
1.11 espie 489: errx(1, "token too long");
1.1 deraadt 490:
491: *tp = EOS;
492:
1.19 espie 493: for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
494: if (h == p->hv && STREQ(name, p->name))
1.1 deraadt 495: break;
496: return p;
497: }
498:
499: /*
500: * initkwds - initialise m4 keywords as fast as possible.
501: * This very similar to install, but without certain overheads,
502: * such as calling lookup. Malloc is not used for storing the
1.17 espie 503: * keyword strings, since we simply use the static pointers
1.1 deraadt 504: * within keywrds block.
505: */
1.18 espie 506: static void
1.17 espie 507: initkwds()
508: {
509: size_t i;
1.20 millert 510: unsigned int h;
1.17 espie 511: ndptr p;
1.1 deraadt 512:
513: for (i = 0; i < MAXKEYS; i++) {
514: h = hash(keywrds[i].knam);
515: p = (ndptr) xalloc(sizeof(struct ndblock));
1.19 espie 516: p->nxtptr = hashtab[h % HASHSIZE];
517: hashtab[h % HASHSIZE] = p;
1.1 deraadt 518: p->name = keywrds[i].knam;
519: p->defn = null;
1.19 espie 520: p->hv = h;
1.24 espie 521: p->type = (keywrds[i].ktyp & TYPEMASK) | STATIC;
522: if ((keywrds[i].ktyp & NOARGS) == 0)
523: p->type |= NEEDARGS;
1.1 deraadt 524: }
525: }
1.31 ! espie 526:
! 527: /* Look up a builtin type, even if overridden by the user */
! 528: int
! 529: builtin_type(key)
! 530: const char *key;
! 531: {
! 532: int i;
! 533:
! 534: for (i = 0; i != MAXKEYS; i++)
! 535: if (STREQ(keywrds[i].knam, key))
! 536: return keywrds[i].ktyp;
! 537: return -1;
! 538: }
! 539:
1.17 espie 540:
1.27 espie 541: static void
542: record(t, lev)
543: struct position *t;
544: int lev;
545: {
546: if (lev < MAXRECORD) {
547: t[lev].name = CURRENT_NAME;
548: t[lev].line = CURRENT_LINE;
549: }
550: }
551:
552: static void
553: dump_stack(t, lev)
554: struct position *t;
555: int lev;
556: {
557: int i;
558:
559: for (i = 0; i < lev; i++) {
560: if (i == MAXRECORD) {
561: fprintf(stderr, " ...\n");
562: break;
563: }
564: fprintf(stderr, " %s at line %lu\n",
565: t[i].name, t[i].line);
566: }
567: }