Annotation of src/usr.bin/m4/main.c, Revision 1.11
1.11 ! espie 1: /* $OpenBSD: main.c,v 1.10 1998/06/02 20:46:40 deraadt 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.11 ! espie 50: static char rcsid[] = "$OpenBSD: main.c,v 1.10 1998/06/02 20:46:40 deraadt 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>
61: #include <signal.h>
62: #include <errno.h>
63: #include <unistd.h>
64: #include <stdio.h>
65: #include <ctype.h>
66: #include <string.h>
1.11 ! espie 67: #include <err.h>
1.1 deraadt 68: #include "mdef.h"
69: #include "stdd.h"
70: #include "extern.h"
71: #include "pathnames.h"
72:
73: ndptr hashtab[HASHSIZE]; /* hash table for macros etc. */
1.6 deraadt 74: pbent buf[BUFSIZE]; /* push-back buffer */
75: pbent *bufbase = buf; /* the base for current ilevel */
76: pbent *bbase[MAXINP]; /* the base for each ilevel */
77: pbent *bp = buf; /* first available character */
78: pbent *endpbb = buf+BUFSIZE; /* end of push-back buffer */
1.1 deraadt 79: stae mstack[STACKMAX+1]; /* stack of m4 machine */
80: char strspace[STRSPMAX+1]; /* string space for evaluation */
81: char *ep = strspace; /* first free char in strspace */
82: char *endest= strspace+STRSPMAX;/* end of string space */
83: int sp; /* current m4 stack pointer */
84: int fp; /* m4 call frame pointer */
85: FILE *infile[MAXINP]; /* input file stack (0=stdin) */
86: FILE *outfile[MAXOUT]; /* diversion array(0=bitbucket)*/
87: FILE *active; /* active output file pointer */
88: char *m4temp; /* filename for diversions */
89: int ilevel = 0; /* input file stack pointer */
90: int oindex = 0; /* diversion index.. */
91: char *null = ""; /* as it says.. just a null.. */
92: char *m4wraps = ""; /* m4wrap string default.. */
1.2 deraadt 93: char lquote[MAXCCHARS+1] = {LQUOTE}; /* left quote character (`) */
94: char rquote[MAXCCHARS+1] = {RQUOTE}; /* right quote character (') */
95: char scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */
96: char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */
1.1 deraadt 97:
98: struct keyblk keywrds[] = { /* m4 keywords to be installed */
1.8 millert 99: { "include", INCLTYPE },
100: { "sinclude", SINCTYPE },
101: { "define", DEFITYPE },
102: { "defn", DEFNTYPE },
103: { "divert", DIVRTYPE },
104: { "expr", EXPRTYPE },
105: { "eval", EXPRTYPE },
106: { "substr", SUBSTYPE },
107: { "ifelse", IFELTYPE },
108: { "ifdef", IFDFTYPE },
109: { "len", LENGTYPE },
110: { "incr", INCRTYPE },
111: { "decr", DECRTYPE },
112: { "dnl", DNLNTYPE },
113: { "changequote", CHNQTYPE },
114: { "changecom", CHNCTYPE },
115: { "index", INDXTYPE },
1.1 deraadt 116: #ifdef EXTENDED
1.8 millert 117: { "paste", PASTTYPE },
118: { "spaste", SPASTYPE },
1.1 deraadt 119: #endif
1.8 millert 120: { "popdef", POPDTYPE },
121: { "pushdef", PUSDTYPE },
122: { "dumpdef", DUMPTYPE },
123: { "shift", SHIFTYPE },
124: { "translit", TRNLTYPE },
125: { "undefine", UNDFTYPE },
126: { "undivert", UNDVTYPE },
127: { "divnum", DIVNTYPE },
128: { "maketemp", MKTMTYPE },
129: { "errprint", ERRPTYPE },
130: { "m4wrap", M4WRTYPE },
131: { "m4exit", EXITTYPE },
132: { "syscmd", SYSCTYPE },
133: { "sysval", SYSVTYPE },
1.1 deraadt 134:
1.4 deraadt 135: #if defined(unix) || defined(__NetBSD__) || defined(__OpenBSD__)
1.8 millert 136: { "unix", MACRTYPE },
1.1 deraadt 137: #else
138: #ifdef vms
1.8 millert 139: { "vms", MACRTYPE },
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:
149: void macro();
150: void initkwds();
151: extern int getopt();
152:
153: int
154: main(argc,argv)
155: int argc;
156: char *argv[];
157: {
158: register int c;
159: register int n;
160: char *p;
161: register FILE *ifp;
162:
163: if (signal(SIGINT, SIG_IGN) != SIG_IGN)
164: signal(SIGINT, onintr);
165:
166: initkwds();
167:
1.5 millert 168: while ((c = getopt(argc, argv, "tD:U:o:")) != -1)
1.1 deraadt 169: switch(c) {
170:
171: case 'D': /* define something..*/
172: for (p = optarg; *p; p++)
173: if (*p == '=')
174: break;
175: if (*p)
176: *p++ = EOS;
177: dodefine(optarg, p);
178: break;
179: case 'U': /* undefine... */
180: remhash(optarg, TOP);
181: break;
182: case 'o': /* specific output */
183: case '?':
184: usage();
185: }
186:
187: argc -= optind;
188: argv += optind;
189:
190: active = stdout; /* default active output */
191: /* filename for diversions */
192: m4temp = mktemp(xstrdup(_PATH_DIVNAME));
193:
194: bbase[0] = bufbase;
195: if (!argc) {
196: sp = -1; /* stack pointer initialized */
197: fp = 0; /* frame pointer initialized */
198: infile[0] = stdin; /* default input (naturally) */
199: macro();
200: } else
201: for (; argc--; ++argv) {
202: p = *argv;
203: if (p[0] == '-' && p[1] == '\0')
204: ifp = stdin;
205: else if ((ifp = fopen(p, "r")) == NULL)
1.11 ! espie 206: err(1, "%s", p);
1.1 deraadt 207: sp = -1;
208: fp = 0;
209: infile[0] = ifp;
210: macro();
211: if (ifp != stdin)
212: (void)fclose(ifp);
213: }
214:
215: if (*m4wraps) { /* anything for rundown ?? */
216: ilevel = 0; /* in case m4wrap includes.. */
217: bufbase = bp = buf; /* use the entire buffer */
218: putback(EOF); /* eof is a must !! */
219: pbstr(m4wraps); /* user-defined wrapup act */
220: macro(); /* last will and testament */
221: }
222:
223: if (active != stdout)
224: active = stdout; /* reset output just in case */
225: for (n = 1; n < MAXOUT; n++) /* default wrap-up: undivert */
226: if (outfile[n] != NULL)
227: getdiv(n);
228: /* remove bitbucket if used */
229: if (outfile[0] != NULL) {
230: (void) fclose(outfile[0]);
231: m4temp[UNIQUE] = '0';
232: #ifdef vms
233: (void) remove(m4temp);
234: #else
235: (void) unlink(m4temp);
236: #endif
237: }
238:
239: return 0;
240: }
241:
242: ndptr inspect();
243:
244: /*
1.2 deraadt 245: * Look ahead (at most MAXCCHARS characters) for `token'.
246: * (on input `t == token[0]')
247: * Used for comment and quoting delimiters.
248: * Returns 1 if `token' present; copied to output.
249: * 0 if `token' not found; all characters pushed back
250: */
251: int
252: do_look_ahead(t, token)
253: int t;
254: char *token;
255: {
256: int i;
257:
258: if (t != token[0])
1.11 ! espie 259: errx(1, "internal error");
1.2 deraadt 260:
261: for (i = 1; *++token; i++) {
262: t = gpbc();
263: if (t == EOF || t != *token) {
264: if (t != EOF)
265: putback(t);
266: while (--i)
267: putback(*--token);
268: return 0;
269: }
270: }
271: return 1;
272: }
273:
274: #define LOOK_AHEAD(t, token) ((t)==(token)[0] && do_look_ahead(t,token))
275:
276: /*
1.1 deraadt 277: * macro - the work horse..
278: */
279: void
280: macro() {
1.7 deraadt 281: char token[MAXTOK], chars[2];
1.1 deraadt 282: register char *s;
283: register int t, l;
284: register ndptr p;
285: register int nlpar;
286:
287: cycle {
1.2 deraadt 288: t = gpbc();
289: if (t == '_' || isalpha(t)) {
1.1 deraadt 290: putback(t);
1.10 deraadt 291: s = token;
1.9 mickey 292: if ((p = inspect(s)) == nil) {
1.1 deraadt 293: if (sp < 0)
294: while (*s)
295: putc(*s++, active);
296: else
297: while (*s)
298: chrsave(*s++);
299: }
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: putback(l = gpbc());
316: if (l != LPAREN) { /* add bracks */
317: putback(RPAREN);
318: putback(LPAREN);
319: }
320: }
321: }
322: else if (t == EOF) {
323: if (sp > -1)
1.11 ! espie 324: errx(1, "unexpected end of input");
1.1 deraadt 325: if (ilevel <= 0)
326: break; /* all done thanks.. */
327: --ilevel;
328: (void) fclose(infile[ilevel+1]);
329: bufbase = bbase[ilevel];
330: continue;
331: }
332: /*
1.7 deraadt 333: * non-alpha token possibly seen..
1.1 deraadt 334: * [the order of else if .. stmts is important.]
335: */
1.2 deraadt 336: else if (LOOK_AHEAD(t,lquote)) { /* strip quotes */
1.1 deraadt 337: nlpar = 1;
338: do {
1.7 deraadt 339:
1.2 deraadt 340: l = gpbc();
1.7 deraadt 341: if (LOOK_AHEAD(l,rquote)) {
1.1 deraadt 342: nlpar--;
1.7 deraadt 343: s = rquote;
344: } else if (LOOK_AHEAD(l,lquote)) {
1.1 deraadt 345: nlpar++;
1.7 deraadt 346: s = lquote;
347: } else if (l == EOF)
1.11 ! espie 348: errx(1, "missing right quote");
1.7 deraadt 349: else {
350: chars[0] = l;
351: chars[1] = '\0';
352: s = chars;
353: }
1.1 deraadt 354: if (nlpar > 0) {
355: if (sp < 0)
1.7 deraadt 356: while (*s)
357: putc(*s++, active);
1.1 deraadt 358: else
1.7 deraadt 359: while (*s)
360: chrsave(*s++);
1.1 deraadt 361: }
362: }
363: while (nlpar != 0);
364: }
365:
1.2 deraadt 366: else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
367: int i;
368: for (i = 0; i < MAXCCHARS && scommt[i]; i++)
369: putc(scommt[i], active);
370:
371: for(;;) {
372: t = gpbc();
373: if (LOOK_AHEAD(t, ecommt)) {
374: for (i = 0; i < MAXCCHARS && ecommt[i];
375: i++)
376: putc(ecommt[i], active);
377: break;
378: }
379: if (t == EOF)
380: break;
1.1 deraadt 381: putc(t, active);
382: }
1.2 deraadt 383: }
384:
385: else if (sp < 0) { /* not in a macro at all */
1.1 deraadt 386: putc(t, active); /* output directly.. */
387: }
388:
389: else switch(t) {
390:
391: case LPAREN:
392: if (PARLEV > 0)
393: chrsave(t);
394: while (isspace(l = gpbc()))
395: ; /* skip blank, tab, nl.. */
396: putback(l);
397: PARLEV++;
398: break;
399:
400: case RPAREN:
401: if (--PARLEV > 0)
402: chrsave(t);
403: else { /* end of argument list */
404: chrsave(EOS);
405:
406: if (sp == STACKMAX)
1.11 ! espie 407: errx(1, "internal stack overflow");
1.1 deraadt 408:
409: if (CALTYP == MACRTYPE)
410: expand((char **) mstack+fp+1, sp-fp);
411: else
412: eval((char **) mstack+fp+1, sp-fp, CALTYP);
413:
414: ep = PREVEP; /* flush strspace */
415: sp = PREVSP; /* previous sp.. */
416: fp = PREVFP; /* rewind stack...*/
417: }
418: break;
419:
420: case COMMA:
421: if (PARLEV == 1) {
422: chrsave(EOS); /* new argument */
423: while (isspace(l = gpbc()))
424: ;
425: putback(l);
426: pushs(ep);
427: } else
428: chrsave(t);
429: break;
430:
431: default:
432: chrsave(t); /* stack the char */
433: break;
434: }
435: }
436: }
437:
438: /*
439: * build an input token..
440: * consider only those starting with _ or A-Za-z. This is a
441: * combo with lookup to speed things up.
442: */
443: ndptr
444: inspect(tp)
445: register char *tp;
446: {
447: register char c;
448: register char *name = tp;
449: register char *etp = tp+MAXTOK;
450: register ndptr p;
451: register unsigned long h = 0;
452:
453: while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
454: h = (h << 5) + h + (*tp++ = c);
455: putback(c);
456: if (tp == etp)
1.11 ! espie 457: errx(1, "token too long");
1.1 deraadt 458:
459: *tp = EOS;
460:
461: for (p = hashtab[h%HASHSIZE]; p != nil; p = p->nxtptr)
462: if (STREQ(name, p->name))
463: break;
464: return p;
465: }
466:
467: /*
468: * initkwds - initialise m4 keywords as fast as possible.
469: * This very similar to install, but without certain overheads,
470: * such as calling lookup. Malloc is not used for storing the
471: * keyword strings, since we simply use the static pointers
472: * within keywrds block.
473: */
474: void
475: initkwds() {
476: register int i;
477: register int h;
478: register ndptr p;
479:
480: for (i = 0; i < MAXKEYS; i++) {
481: h = hash(keywrds[i].knam);
482: p = (ndptr) xalloc(sizeof(struct ndblock));
483: p->nxtptr = hashtab[h];
484: hashtab[h] = p;
485: p->name = keywrds[i].knam;
486: p->defn = null;
487: p->type = keywrds[i].ktyp | STATIC;
488: }
489: }