1 /* $OpenBSD: main.c,v 1.87 2017/06/15 13:48:42 bcallah Exp $ */
2 /* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 1989, 1993
8 * The Regents of the University of California. All rights reserved.
10 * This code is derived from software contributed to Berkeley by
11 * Ozan Yigit at York University.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * Facility: m4 macro processor
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
62 #include "pathnames.h"
64 static const char *shortopts = "+D:d::EGgI:o:Pst:U:";
65 static const struct option longopts[] = {
66 { "define", required_argument, NULL, 'D' },
67 { "debug", optional_argument, NULL, 'd' },
68 { "fatal-warnings", no_argument, NULL, 'E' },
69 { "traditional", no_argument, NULL, 'G' },
70 { "gnu", no_argument, NULL, 'g' },
71 { "include", required_argument, NULL, 'I' },
72 { "error-output", required_argument, NULL, 'o' },
73 { "prefix-builtins", no_argument, NULL, 'P' },
74 { "synclines", no_argument, NULL, 's' },
75 { "trace", required_argument, NULL, 't' },
76 { "undefine", required_argument, NULL, 'U' },
80 stae *mstack; /* stack of m4 machine */
81 char *sstack; /* shadow stack, for string space extension */
82 static size_t STACKMAX; /* current maximum size of stack */
83 int sp; /* current m4 stack pointer */
84 int fp; /* m4 call frame pointer */
85 struct input_file infile[MAXINP];/* input file stack (0=stdin) */
86 FILE **outfile; /* diversion array(0=bitbucket)*/
88 FILE *active; /* active output file pointer */
89 int ilevel = 0; /* input file stack pointer */
90 int oindex = 0; /* diversion index.. */
91 const char *null = ""; /* as it says.. just a null.. */
92 char **m4wraps = NULL; /* m4wraps array. */
93 int maxwraps = 0; /* size of m4wraps array */
94 int wrapindex = 0; /* current offset in m4wraps */
95 char lquote[MAXCCHARS+1] = {LQUOTE}; /* left quote character (`) */
96 char rquote[MAXCCHARS+1] = {RQUOTE}; /* right quote character (') */
97 char scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */
98 char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */
99 int synch_lines = 0; /* line synchronisation for C preprocessor */
100 int prefix_builtins = 0; /* -P option to prefix builtin keywords */
101 int error_warns = 0; /* -E option to make warnings exit_code = 1 */
102 int fatal_warns = 0; /* -E -E option to make warnings fatal */
105 const char *knam; /* keyword name */
106 int ktyp; /* keyword type */
109 static struct keyblk keywrds[] = { /* m4 keywords to be installed */
110 { "include", INCLTYPE },
111 { "sinclude", SINCTYPE },
112 { "define", DEFITYPE },
113 { "defn", DEFNTYPE },
114 { "divert", DIVRTYPE | NOARGS },
115 { "expr", EXPRTYPE },
116 { "eval", EXPRTYPE },
117 { "substr", SUBSTYPE },
118 { "ifelse", IFELTYPE },
119 { "ifdef", IFDFTYPE },
121 { "incr", INCRTYPE },
122 { "decr", DECRTYPE },
123 { "dnl", DNLNTYPE | NOARGS },
124 { "changequote", CHNQTYPE | NOARGS },
125 { "changecom", CHNCTYPE | NOARGS },
126 { "index", INDXTYPE },
128 { "paste", PASTTYPE },
129 { "spaste", SPASTYPE },
130 /* Newer extensions, needed to handle gnu-m4 scripts */
131 { "indir", INDIRTYPE},
132 { "builtin", BUILTINTYPE},
133 { "patsubst", PATSTYPE},
134 { "regexp", REGEXPTYPE},
135 { "esyscmd", ESYSCMDTYPE},
136 { "__file__", FILENAMETYPE | NOARGS},
137 { "__line__", LINETYPE | NOARGS},
139 { "popdef", POPDTYPE },
140 { "pushdef", PUSDTYPE },
141 { "dumpdef", DUMPTYPE | NOARGS },
142 { "shift", SHIFTYPE | NOARGS },
143 { "translit", TRNLTYPE },
144 { "undefine", UNDFTYPE },
145 { "undivert", UNDVTYPE | NOARGS },
146 { "divnum", DIVNTYPE | NOARGS },
147 { "maketemp", MKTMTYPE },
148 { "mkstemp", MKTMTYPE },
149 { "errprint", ERRPTYPE | NOARGS },
150 { "m4wrap", M4WRTYPE | NOARGS },
151 { "m4exit", EXITTYPE | NOARGS },
152 { "syscmd", SYSCTYPE },
153 { "sysval", SYSVTYPE | NOARGS },
154 { "traceon", TRACEONTYPE | NOARGS },
155 { "traceoff", TRACEOFFTYPE | NOARGS },
157 { "unix", SELFTYPE | NOARGS },
160 #define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk))
166 static struct position {
169 } quotes[MAXRECORD], paren[MAXRECORD];
171 static void record(struct position *, int);
172 static void dump_stack(struct position *, int);
174 static void macro(void);
175 static void initkwds(void);
176 static ndptr inspect(int, char *);
177 static int do_look_ahead(int, const char *);
178 static void reallyoutputstr(const char *);
179 static void reallyputchar(int);
181 static void enlarge_stack(void);
183 int main(int, char *[]);
188 main(int argc, char *argv[])
194 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
195 signal(SIGINT, onintr);
199 STACKMAX = INITSTACKMAX;
201 mstack = xreallocarray(NULL, STACKMAX, sizeof(stae), NULL);
202 sstack = xalloc(STACKMAX, NULL);
208 while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1)
211 case 'D': /* define something..*/
212 for (p = optarg; *p; p++)
219 case 'E': /* like GNU m4 1.4.9+ */
220 if (error_warns == 0)
226 addtoincludepath(optarg);
231 case 'U': /* undefine... */
232 macro_popdef(optarg);
241 set_trace_flags(optarg ? optarg : "aeq");
247 mark_traced(optarg, 1);
261 setup_builtin("format", FORMATTYPE);
263 active = stdout; /* default active output */
266 sp = -1; /* stack pointer initialized */
267 fp = 0; /* frame pointer initialized */
268 set_input(infile+0, stdin, "stdin");
269 /* default input (naturally) */
272 for (; argc--; ++argv) {
274 if (p[0] == '-' && p[1] == EOS)
275 set_input(infile, stdin, "stdin");
276 else if (fopen_trypath(infile, p) == NULL)
281 release_input(infile);
287 ilevel = 0; /* in case m4wrap includes.. */
288 bufbase = bp = buf; /* use the entire buffer */
290 while (wrapindex != 0) {
291 for (i = 0; i < wrapindex; i++)
297 for (i = 0; i < wrapindex; i++) {
304 if (active != stdout)
305 active = stdout; /* reset output just in case */
306 for (n = 1; n < maxout; n++) /* default wrap-up: undivert */
307 if (outfile[n] != NULL)
309 /* remove bitbucket if used */
310 if (outfile[0] != NULL) {
311 (void) fclose(outfile[0]);
318 * Look ahead for `token'.
319 * (on input `t == token[0]')
320 * Used for comment and quoting delimiters.
321 * Returns 1 if `token' present; copied to output.
322 * 0 if `token' not found; all characters pushed back
325 do_look_ahead(int t, const char *token)
329 assert((unsigned char)t == (unsigned char)token[0]);
331 for (i = 1; *++token; i++) {
333 if (t == EOF || (unsigned char)t != (unsigned char)*token) {
343 #define LOOK_AHEAD(t, token) (t != EOF && \
344 (unsigned char)(t)==(unsigned char)(token)[0] && \
345 do_look_ahead(t,token))
348 * macro - the work horse..
353 char token[MAXTOK+1];
361 if (LOOK_AHEAD(t,lquote)) { /* strip quotes */
363 record(quotes, nlpar++);
365 * Opening quote: scan forward until matching
366 * closing quote has been found.
371 if (LOOK_AHEAD(l,rquote)) {
374 } else if (LOOK_AHEAD(l,lquote)) {
375 record(quotes, nlpar++);
377 } else if (l == EOF) {
379 warnx("unclosed quote:");
381 warnx("%d unclosed quotes:", nlpar);
382 dump_stack(quotes, nlpar);
394 } else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
395 reallyoutputstr(scommt);
399 if (LOOK_AHEAD(t, ecommt)) {
400 reallyoutputstr(ecommt);
407 } else if (t == '_' || isalpha(t)) {
408 p = inspect(t, token);
410 pushback(l = gpbc());
411 if (p == NULL || (l != LPAREN &&
412 (macro_getdef(p)->type & NEEDARGS) != 0))
416 * real thing.. First build a call frame:
418 pushf(fp); /* previous call frm */
419 pushf(macro_getdef(p)->type); /* type of the call */
421 pushf(0); /* parenthesis level */
422 fp = sp; /* new frame pointer */
424 * now push the string arguments:
426 pushdef(p); /* defn string */
427 pushs1((char *)macro_name(p)); /* macro name */
428 pushs(ep); /* start next..*/
430 if (l != LPAREN && PARLEV == 0) {
434 if (sp == (int)STACKMAX)
435 errx(1, "internal stack overflow");
436 eval((const char **) mstack+fp+1, 2,
437 CALTYP, TRACESTATUS);
439 ep = PREVEP; /* flush strspace */
440 sp = PREVSP; /* previous sp.. */
441 fp = PREVFP; /* rewind stack...*/
444 } else if (t == EOF) {
445 if (!mimic_gnu /* you can puke right there */
446 && sp > -1 && ilevel <= 0) {
447 warnx( "unexpected end of input, unclosed parenthesis:");
448 dump_stack(paren, PARLEV);
452 break; /* all done thanks.. */
453 release_input(infile+ilevel--);
455 bufbase = bbase[ilevel];
457 } else if (sp < 0) { /* not in a macro at all */
458 reallyputchar(t); /* output directly.. */
466 while (isspace(l = gpbc())) /* skip blank, tab, nl.. */
470 record(paren, PARLEV++);
476 else { /* end of argument list */
479 if (sp == (int)STACKMAX)
480 errx(1, "internal stack overflow");
482 eval((const char **) mstack+fp+1, sp-fp,
483 CALTYP, TRACESTATUS);
485 ep = PREVEP; /* flush strspace */
486 sp = PREVSP; /* previous sp.. */
487 fp = PREVFP; /* rewind stack...*/
493 chrsave(EOS); /* new argument */
494 while (isspace(l = gpbc()))
503 if (LOOK_AHEAD(t, scommt)) {
505 for (p = scommt; *p; p++)
509 if (LOOK_AHEAD(t, ecommt)) {
510 for (p = ecommt; *p; p++)
519 CHRSAVE(t); /* stack the char */
526 * output string directly, without pushing it for reparses.
529 outputstr(const char *s)
539 reallyoutputstr(const char *s)
545 infile[ilevel].synch_lineno++;
546 if (infile[ilevel].synch_lineno !=
547 infile[ilevel].lineno)
559 if (synch_lines && c == '\n') {
560 infile[ilevel].synch_lineno++;
561 if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
567 * build an input token..
568 * consider only those starting with _ or A-Za-z.
571 inspect(int c, char *tp)
574 char *etp = tp+MAXTOK;
579 while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
584 /* token is too long, it won't match anything, but it can still
588 while (isalnum(c = gpbc()) || c == '_') {
598 p = ohash_find(¯os, ohash_qlookupi(¯os, name, (const char **)&tp));
601 if (macro_getdef(p) == NULL)
607 * initkwds - initialise m4 keywords as fast as possible.
608 * This very similar to install, but without certain overheads,
609 * such as calling lookup. Malloc is not used for storing the
610 * keyword strings, since we simply use the static pointers
611 * within keywrds block.
619 for (i = 0; i < (int)MAXKEYS; i++) {
620 type = keywrds[i].ktyp & TYPEMASK;
621 if ((keywrds[i].ktyp & NOARGS) == 0)
623 setup_builtin(keywrds[i].knam, type);
628 record(struct position *t, int lev)
630 if (lev < MAXRECORD) {
631 t[lev].name = CURRENT_NAME;
632 t[lev].line = CURRENT_LINE;
637 dump_stack(struct position *t, int lev)
641 for (i = 0; i < lev; i++) {
642 if (i == MAXRECORD) {
643 fprintf(stderr, " ...\n");
646 fprintf(stderr, " %s at line %lu\n",
647 t[i].name, t[i].line);
655 STACKMAX += STACKMAX/2;
656 mstack = xreallocarray(mstack, STACKMAX, sizeof(stae),
657 "Evaluation stack overflow (%lu)",
658 (unsigned long)STACKMAX);
659 sstack = xrealloc(sstack, STACKMAX,
660 "Evaluation stack overflow (%lu)",
661 (unsigned long)STACKMAX);