1 /* $OpenBSD: main.c,v 1.53 2002/04/26 16:15:16 espie Exp $ */
2 /* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Ozan Yigit at York University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
41 static char copyright[] =
42 "@(#) Copyright (c) 1989, 1993\n\
43 The Regents of the University of California. All rights reserved.\n";
48 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
51 static char rcsid[] = "$OpenBSD: main.c,v 1.53 2002/04/26 16:15:16 espie Exp $";
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD: src/usr.bin/m4/main.c,v 1.6.2.6 2002/07/15 02:06:15 jmallett Exp $");
61 * Facility: m4 macro processor
65 #include <sys/types.h>
79 #include "pathnames.h"
81 ndptr hashtab[HASHSIZE]; /* hash table for macros etc. */
82 stae *mstack; /* stack of m4 machine */
83 char *sstack; /* shadow stack, for string space extension */
84 static size_t STACKMAX; /* current maximum size of stack */
85 int sp; /* current m4 stack pointer */
86 int fp; /* m4 call frame pointer */
87 struct input_file infile[MAXINP];/* input file stack (0=stdin) */
88 char *inname[MAXINP]; /* names of these input files */
89 int inlineno[MAXINP]; /* current number in each input file */
90 FILE **outfile; /* diversion array(0=bitbucket)*/
92 FILE *active; /* active output file pointer */
93 int ilevel = 0; /* input file stack pointer */
94 int oindex = 0; /* diversion index.. */
95 const char *null = ""; /* as it says.. just a null.. */
96 const char *m4wraps = ""; /* m4wrap string default.. */
97 char lquote[MAXCCHARS+1] = {LQUOTE}; /* left quote character (`) */
98 char rquote[MAXCCHARS+1] = {RQUOTE}; /* right quote character (') */
99 char scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */
100 char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */
101 int synccpp; /* Line synchronisation for C preprocessor */
103 struct keyblk keywrds[] = { /* m4 keywords to be installed */
104 { "include", INCLTYPE },
105 { "sinclude", SINCTYPE },
106 { "define", DEFITYPE },
107 { "defn", DEFNTYPE },
108 { "divert", DIVRTYPE | NOARGS },
109 { "expr", EXPRTYPE },
110 { "eval", EXPRTYPE },
111 { "substr", SUBSTYPE },
112 { "ifelse", IFELTYPE },
113 { "ifdef", IFDFTYPE },
115 { "incr", INCRTYPE },
116 { "decr", DECRTYPE },
117 { "dnl", DNLNTYPE | NOARGS },
118 { "changequote", CHNQTYPE | NOARGS },
119 { "changecom", CHNCTYPE | NOARGS },
120 { "index", INDXTYPE },
122 { "paste", PASTTYPE },
123 { "spaste", SPASTYPE },
124 /* Newer extensions, needed to handle gnu-m4 scripts */
125 { "indir", INDIRTYPE},
126 { "builtin", BUILTINTYPE},
127 { "patsubst", PATSTYPE},
128 { "regexp", REGEXPTYPE},
129 { "esyscmd", ESYSCMDTYPE},
130 { "__file__", FILENAMETYPE | NOARGS},
131 { "__line__", LINETYPE | NOARGS},
133 { "popdef", POPDTYPE },
134 { "pushdef", PUSDTYPE },
135 { "dumpdef", DUMPTYPE | NOARGS },
136 { "shift", SHIFTYPE | NOARGS },
137 { "translit", TRNLTYPE },
138 { "undefine", UNDFTYPE },
139 { "undivert", UNDVTYPE | NOARGS },
140 { "divnum", DIVNTYPE | NOARGS },
141 { "maketemp", MKTMTYPE },
142 { "errprint", ERRPTYPE | NOARGS },
143 { "m4wrap", M4WRTYPE | NOARGS },
144 { "m4exit", EXITTYPE | NOARGS },
145 { "syscmd", SYSCTYPE },
146 { "sysval", SYSVTYPE | NOARGS },
147 { "traceon", TRACEONTYPE | NOARGS },
148 { "traceoff", TRACEOFFTYPE | NOARGS },
150 #if defined(unix) || defined(__unix__)
151 { "unix", SELFTYPE | NOARGS },
154 { "vms", SELFTYPE | NOARGS },
159 #define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk))
162 static struct position {
165 } quotes[MAXRECORD], paren[MAXRECORD];
167 static void record(struct position *, int);
168 static void dump_stack(struct position *, int);
170 static void macro(void);
171 static void initkwds(void);
172 static ndptr inspect(int, char *);
173 static int do_look_ahead(int, const char *);
175 static void enlarge_stack(void);
178 main(int argc, char *argv[])
187 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
188 signal(SIGINT, onintr);
192 STACKMAX = INITSTACKMAX;
194 mstack = (stae *)xalloc(sizeof(stae) * STACKMAX);
195 sstack = (char *)xalloc(STACKMAX);
201 while ((c = getopt(argc, argv, "gst:d:D:U:o:I:")) != -1)
203 case 'D': /* define something..*/
204 for (p = optarg; *p; p++)
212 addtoincludepath(optarg);
214 case 'U': /* undefine... */
215 remhash(optarg, TOP);
221 set_trace_flags(optarg);
227 mark_traced(optarg, 1);
241 active = stdout; /* default active output */
244 sp = -1; /* stack pointer initialized */
245 fp = 0; /* frame pointer initialized */
246 set_input(infile+0, stdin, "stdin");
247 /* default input (naturally) */
248 if ((inname[0] = strdup("-")) == NULL)
254 for (; argc--; ++argv) {
256 if (p[0] == '-' && p[1] == EOS)
257 set_input(infile, stdin, "stdin");
258 else if (fopen_trypath(infile, p) == NULL) {
265 if ((inname[0] = strdup(p)) == NULL)
270 release_input(infile);
273 if (*m4wraps) { /* anything for rundown ?? */
274 ilevel = 0; /* in case m4wrap includes.. */
275 bufbase = bp = buf; /* use the entire buffer */
276 pbstr(m4wraps); /* user-defined wrapup act */
277 macro(); /* last will and testament */
280 if (active != stdout)
281 active = stdout; /* reset output just in case */
282 for (n = 1; n < maxout; n++) /* default wrap-up: undivert */
283 if (outfile[n] != NULL)
285 /* remove bitbucket if used */
286 if (outfile[0] != NULL) {
287 (void) fclose(outfile[0]);
294 * Look ahead for `token'.
295 * (on input `t == token[0]')
296 * Used for comment and quoting delimiters.
297 * Returns 1 if `token' present; copied to output.
298 * 0 if `token' not found; all characters pushed back
301 do_look_ahead(int t, const char *token)
305 assert((unsigned char)t == (unsigned char)token[0]);
307 for (i = 1; *++token; i++) {
309 if (t == EOF || (unsigned char)t != (unsigned char)*token) {
319 #define LOOK_AHEAD(t, token) (t != EOF && \
320 (unsigned char)(t)==(unsigned char)(token)[0] && \
321 do_look_ahead(t,token))
324 * macro - the work horse..
329 char token[MAXTOK+1];
336 if (t == '_' || isalpha(t)) {
337 p = inspect(t, token);
340 if (p == nil || (l != LPAREN &&
341 (p->type & NEEDARGS) != 0))
345 * real thing.. First build a call frame:
347 pushf(fp); /* previous call frm */
348 pushf(p->type); /* type of the call */
349 pushf(0); /* parenthesis level */
350 fp = sp; /* new frame pointer */
352 * now push the string arguments:
354 pushs1(p->defn); /* defn string */
355 pushs1(p->name); /* macro name */
356 pushs(ep); /* start next..*/
358 if (l != LPAREN && PARLEV == 0) {
362 if ((uintptr_t)sp == STACKMAX)
363 errx(1, "internal stack overflow");
364 eval((const char **) mstack+fp+1, 2,
367 ep = PREVEP; /* flush strspace */
368 sp = PREVSP; /* previous sp.. */
369 fp = PREVFP; /* rewind stack...*/
372 } else if (t == EOF) {
374 warnx( "unexpected end of input, unclosed parenthesis:");
375 dump_stack(paren, PARLEV);
379 break; /* all done thanks.. */
380 release_input(infile+ilevel--);
381 free(inname[ilevel+1]);
382 bufbase = bbase[ilevel];
387 * non-alpha token possibly seen..
388 * [the order of else if .. stmts is important.]
390 else if (LOOK_AHEAD(t,lquote)) { /* strip quotes */
392 record(quotes, nlpar++);
394 * Opening quote: scan forward until matching
395 * closing quote has been found.
400 if (LOOK_AHEAD(l,rquote)) {
403 } else if (LOOK_AHEAD(l,lquote)) {
404 record(quotes, nlpar++);
406 } else if (l == EOF) {
408 warnx("unclosed quote:");
410 warnx("%d unclosed quotes:", nlpar);
411 dump_stack(quotes, nlpar);
425 else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
426 fputs(scommt, active);
430 if (LOOK_AHEAD(t, ecommt)) {
431 fputs(ecommt, active);
440 else if (sp < 0) { /* not in a macro at all */
441 putc(t, active); /* output directly.. */
449 while (isspace(l = gpbc()))
450 ; /* skip blank, tab, nl.. */
452 record(paren, PARLEV++);
458 else { /* end of argument list */
461 if ((uintptr_t)sp == STACKMAX)
462 errx(1, "internal stack overflow");
464 eval((const char **) mstack+fp+1, sp-fp,
467 ep = PREVEP; /* flush strspace */
468 sp = PREVSP; /* previous sp.. */
469 fp = PREVFP; /* rewind stack...*/
475 chrsave(EOS); /* new argument */
476 while (isspace(l = gpbc()))
485 if (LOOK_AHEAD(t, scommt)) {
487 for (pc = scommt; *pc; pc++)
491 if (LOOK_AHEAD(t, ecommt)) {
492 for (pc = ecommt; *pc; pc++)
501 CHRSAVE(t); /* stack the char */
508 * output string directly, without pushing it for reparses.
511 outputstr(const char *s)
522 * build an input token..
523 * consider only those starting with _ or A-Za-z. This is a
524 * combo with lookup to speed things up.
527 inspect(int c, char *tp)
530 char *etp = tp+MAXTOK;
536 while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
537 h = (h << 5) + h + (*tp++ = c);
541 /* token is too long, it won't match anything, but it can still
545 while (isalnum(c = gpbc()) || c == '_') {
555 for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
556 if (h == p->hv && STREQ(name, p->name))
562 * initkwds - initialise m4 keywords as fast as possible.
563 * This very similar to install, but without certain overheads,
564 * such as calling lookup. Malloc is not used for storing the
565 * keyword strings, since we simply use the static pointers
566 * within keywrds block.
575 for (i = 0; i < MAXKEYS; i++) {
576 h = hash(keywrds[i].knam);
577 p = (ndptr) xalloc(sizeof(struct ndblock));
578 p->nxtptr = hashtab[h % HASHSIZE];
579 hashtab[h % HASHSIZE] = p;
580 p->name = xstrdup(keywrds[i].knam);
581 p->defn = xstrdup(null);
583 p->type = keywrds[i].ktyp & TYPEMASK;
584 if ((keywrds[i].ktyp & NOARGS) == 0)
589 /* Look up a builtin type, even if overridden by the user */
591 builtin_type(const char *key)
595 for (i = 0; i != MAXKEYS; i++)
596 if (STREQ(keywrds[i].knam, key))
597 return keywrds[i].ktyp;
602 builtin_realname(int n)
606 for (i = 0; i != MAXKEYS; i++)
607 if (((keywrds[i].ktyp ^ n) & TYPEMASK) == 0)
608 return keywrds[i].knam;
613 record(struct position *t, int lev)
615 if (lev < MAXRECORD) {
616 t[lev].name = CURRENT_NAME;
617 t[lev].line = CURRENT_LINE;
622 dump_stack(struct position *t, int lev)
626 for (i = 0; i < lev; i++) {
627 if (i == MAXRECORD) {
628 fprintf(stderr, " ...\n");
631 fprintf(stderr, " %s at line %lu\n",
632 t[i].name, t[i].line);
641 mstack = realloc(mstack, sizeof(stae) * STACKMAX);
642 sstack = realloc(sstack, STACKMAX);
643 if (mstack == NULL || sstack == NULL)
644 errx(1, "Evaluation stack overflow (%lu)",
645 (unsigned long)STACKMAX);
648 /* Emit preprocessor #line directive if -s option used. */
654 fprintf(active, "#line %d \"%s\"\n", inlineno[ilevel],