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