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 $	*/
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. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36/*
37 * main.c
38 * Facility: m4 macro processor
39 * by: oz
40 */
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: stable/11/usr.bin/m4/main.c 352281 2019-09-13 07:22:09Z bapt $");
43
44#include <assert.h>
45#include <signal.h>
46#include <err.h>
47#include <errno.h>
48#include <unistd.h>
49#include <stdio.h>
50#include <ctype.h>
51#include <string.h>
52#include <stddef.h>
53#include <stdint.h>
54#include <stdlib.h>
55#include <ohash.h>
56#include "mdef.h"
57#include "stdd.h"
58#include "extern.h"
59#include "pathnames.h"
60
61stae *mstack;			/* stack of m4 machine         */
62char *sstack;			/* shadow stack, for string space extension */
63static size_t STACKMAX;		/* current maximum size of stack */
64int sp;				/* current m4  stack pointer   */
65int fp;				/* m4 call frame pointer       */
66struct input_file infile[MAXINP];/* input file stack (0=stdin)  */
67FILE **outfile;			/* diversion array(0=bitbucket)*/
68int maxout;
69FILE *active;			/* active output file pointer  */
70int ilevel = 0;			/* input file stack pointer    */
71int oindex = 0;			/* diversion index..	       */
72const char *null = "";                /* as it says.. just a null..  */
73char **m4wraps = NULL;		/* m4wraps array.	       */
74int maxwraps = 0;		/* size of m4wraps array       */
75int wrapindex = 0;		/* current offset in m4wraps   */
76char lquote[MAXCCHARS+1] = {LQUOTE};	/* left quote character  (`)   */
77char rquote[MAXCCHARS+1] = {RQUOTE};	/* right quote character (')   */
78char scommt[MAXCCHARS+1] = {SCOMMT};	/* start character for comment */
79char ecommt[MAXCCHARS+1] = {ECOMMT};	/* end character for comment   */
80int  synch_lines = 0;		/* line synchronisation for C preprocessor */
81int  prefix_builtins = 0;	/* -P option to prefix builtin keywords */
82int  error_warns = 0;		/* -E option to make warnings exit_code = 1 */
83int  fatal_warns = 0;		/* -E -E option to make warnings fatal */
84
85struct keyblk {
86        const char    *knam;          /* keyword name */
87        int     ktyp;           /* keyword type */
88};
89
90static struct keyblk keywrds[] = {	/* m4 keywords to be installed */
91	{ "include",      INCLTYPE },
92	{ "sinclude",     SINCTYPE },
93	{ "define",       DEFITYPE },
94	{ "defn",         DEFNTYPE },
95	{ "divert",       DIVRTYPE | NOARGS },
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 | NOARGS },
105	{ "changequote",  CHNQTYPE | NOARGS },
106	{ "changecom",    CHNCTYPE | NOARGS },
107	{ "index",        INDXTYPE },
108#ifdef EXTENDED
109	{ "paste",        PASTTYPE },
110	{ "spaste",       SPASTYPE },
111	/* Newer extensions, needed to handle gnu-m4 scripts */
112	{ "indir",        INDIRTYPE},
113	{ "builtin",      BUILTINTYPE},
114	{ "patsubst",	  PATSTYPE},
115	{ "regexp",	  REGEXPTYPE},
116	{ "esyscmd",	  ESYSCMDTYPE},
117	{ "__file__",	  FILENAMETYPE | NOARGS},
118	{ "__line__",	  LINETYPE | NOARGS},
119#endif
120	{ "popdef",       POPDTYPE },
121	{ "pushdef",      PUSDTYPE },
122	{ "dumpdef",      DUMPTYPE | NOARGS },
123	{ "shift",        SHIFTYPE | NOARGS },
124	{ "translit",     TRNLTYPE },
125	{ "undefine",     UNDFTYPE },
126	{ "undivert",     UNDVTYPE | NOARGS },
127	{ "divnum",       DIVNTYPE | NOARGS },
128	{ "maketemp",     MKTMTYPE },
129	{ "mkstemp",      MKTMTYPE },
130	{ "errprint",     ERRPTYPE | NOARGS },
131	{ "m4wrap",       M4WRTYPE | NOARGS },
132	{ "m4exit",       EXITTYPE | NOARGS },
133	{ "syscmd",       SYSCTYPE },
134	{ "sysval",       SYSVTYPE | NOARGS },
135	{ "traceon",	  TRACEONTYPE | NOARGS },
136	{ "traceoff",	  TRACEOFFTYPE | NOARGS },
137
138	{ "unix",         SELFTYPE | NOARGS },
139};
140
141#define MAXKEYS	(sizeof(keywrds)/sizeof(struct keyblk))
142
143extern int optind;
144extern char *optarg;
145
146#define MAXRECORD 50
147static struct position {
148	char *name;
149	unsigned long line;
150} quotes[MAXRECORD], paren[MAXRECORD];
151
152static void record(struct position *, int);
153static void dump_stack(struct position *, int);
154
155static void macro(void);
156static void initkwds(void);
157static ndptr inspect(int, char *);
158static int do_look_ahead(int, const char *);
159static void reallyoutputstr(const char *);
160static void reallyputchar(int);
161
162static void enlarge_stack(void);
163
164int main(int, char *[]);
165
166int exit_code = 0;
167
168int
169main(int argc, char *argv[])
170{
171	int c;
172	int n;
173	char *p;
174
175	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
176		signal(SIGINT, onintr);
177
178	init_macros();
179	initspaces();
180	STACKMAX = INITSTACKMAX;
181
182	mstack = xreallocarray(NULL, STACKMAX, sizeof(stae), NULL);
183	sstack = xalloc(STACKMAX, NULL);
184
185	maxout = 0;
186	outfile = NULL;
187	resizedivs(MAXOUT);
188
189	while ((c = getopt(argc, argv, "gst:d:D:EU:o:I:P")) != -1)
190		switch(c) {
191
192		case 'D':               /* define something..*/
193			for (p = optarg; *p; p++)
194				if (*p == '=')
195					break;
196			if (*p)
197				*p++ = EOS;
198			dodefine(optarg, p);
199			break;
200		case 'E':               /* like GNU m4 1.4.9+ */
201			if (error_warns == 0)
202				error_warns = 1;
203			else
204				fatal_warns = 1;
205			break;
206		case 'I':
207			addtoincludepath(optarg);
208			break;
209		case 'P':
210			prefix_builtins = 1;
211			break;
212		case 'U':               /* undefine...       */
213			macro_popdef(optarg);
214			break;
215		case 'g':
216			mimic_gnu = 1;
217			break;
218		case 'd':
219			set_trace_flags(optarg);
220			break;
221		case 's':
222			synch_lines = 1;
223			break;
224		case 't':
225			mark_traced(optarg, 1);
226			break;
227		case 'o':
228			trace_file(optarg);
229                        break;
230		case '?':
231			usage();
232		}
233
234        argc -= optind;
235        argv += optind;
236
237	initkwds();
238	if (mimic_gnu)
239		setup_builtin("format", FORMATTYPE);
240
241	active = stdout;		/* default active output     */
242	bbase[0] = bufbase;
243        if (!argc) {
244		sp = -1;		/* stack pointer initialized */
245		fp = 0;			/* frame pointer initialized */
246		set_input(infile+0, stdin, "stdin");
247					/* default input (naturally) */
248		macro();
249	} else
250		for (; argc--; ++argv) {
251			p = *argv;
252			if (p[0] == '-' && p[1] == EOS)
253				set_input(infile, stdin, "stdin");
254			else if (fopen_trypath(infile, p) == NULL)
255				err(1, "%s", p);
256			sp = -1;
257			fp = 0;
258			macro();
259			release_input(infile);
260		}
261
262	if (wrapindex) {
263		int i;
264
265		ilevel = 0;		/* in case m4wrap includes.. */
266		bufbase = bp = buf;	/* use the entire buffer   */
267		if (mimic_gnu) {
268			while (wrapindex != 0) {
269				for (i = 0; i < wrapindex; i++)
270					pbstr(m4wraps[i]);
271				wrapindex =0;
272				macro();
273			}
274		} else {
275			for (i = 0; i < wrapindex; i++) {
276				pbstr(m4wraps[i]);
277				macro();
278			}
279		}
280	}
281
282	if (active != stdout)
283		active = stdout;	/* reset output just in case */
284	for (n = 1; n < maxout; n++)	/* default wrap-up: undivert */
285		if (outfile[n] != NULL)
286			getdiv(n);
287					/* remove bitbucket if used  */
288	if (outfile[0] != NULL) {
289		(void) fclose(outfile[0]);
290	}
291
292	return exit_code;
293}
294
295/*
296 * Look ahead for `token'.
297 * (on input `t == token[0]')
298 * Used for comment and quoting delimiters.
299 * Returns 1 if `token' present; copied to output.
300 *         0 if `token' not found; all characters pushed back
301 */
302static int
303do_look_ahead(int t, const char *token)
304{
305	int i;
306
307	assert((unsigned char)t == (unsigned char)token[0]);
308
309	for (i = 1; *++token; i++) {
310		t = gpbc();
311		if (t == EOF || (unsigned char)t != (unsigned char)*token) {
312			pushback(t);
313			while (--i)
314				pushback(*--token);
315			return 0;
316		}
317	}
318	return 1;
319}
320
321#define LOOK_AHEAD(t, token) (t != EOF &&		\
322    (unsigned char)(t)==(unsigned char)(token)[0] &&	\
323    do_look_ahead(t,token))
324
325/*
326 * macro - the work horse..
327 */
328static void
329macro(void)
330{
331	char token[MAXTOK+1];
332	int t, l;
333	ndptr p;
334	int  nlpar;
335
336	cycle {
337		t = gpbc();
338
339		if (LOOK_AHEAD(t,lquote)) {	/* strip quotes */
340			nlpar = 0;
341			record(quotes, nlpar++);
342			/*
343			 * Opening quote: scan forward until matching
344			 * closing quote has been found.
345			 */
346			do {
347
348				l = gpbc();
349				if (LOOK_AHEAD(l,rquote)) {
350					if (--nlpar > 0)
351						outputstr(rquote);
352				} else if (LOOK_AHEAD(l,lquote)) {
353					record(quotes, nlpar++);
354					outputstr(lquote);
355				} else if (l == EOF) {
356					if (nlpar == 1)
357						warnx("unclosed quote:");
358					else
359						warnx("%d unclosed quotes:", nlpar);
360					dump_stack(quotes, nlpar);
361					exit(1);
362				} else {
363					if (nlpar > 0) {
364						if (sp < 0)
365							reallyputchar(l);
366						else
367							CHRSAVE(l);
368					}
369				}
370			}
371			while (nlpar != 0);
372		} else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
373			reallyoutputstr(scommt);
374
375			for(;;) {
376				t = gpbc();
377				if (LOOK_AHEAD(t, ecommt)) {
378					reallyoutputstr(ecommt);
379					break;
380				}
381				if (t == EOF)
382					break;
383				reallyputchar(t);
384			}
385		} else if (t == '_' || isalpha(t)) {
386			p = inspect(t, token);
387			if (p != NULL)
388				pushback(l = gpbc());
389			if (p == NULL || (l != LPAREN &&
390			    (macro_getdef(p)->type & NEEDARGS) != 0))
391				outputstr(token);
392			else {
393		/*
394		 * real thing.. First build a call frame:
395		 */
396				pushf(fp);	/* previous call frm */
397				pushf(macro_getdef(p)->type); /* type of the call  */
398				pushf(is_traced(p));
399				pushf(0);	/* parenthesis level */
400				fp = sp;	/* new frame pointer */
401		/*
402		 * now push the string arguments:
403		 */
404				pushdef(p);			/* defn string */
405				pushs1((char *)macro_name(p));	/* macro name  */
406				pushs(ep);			/* start next..*/
407
408				if (l != LPAREN && PARLEV == 0) {
409				    /* no bracks  */
410					chrsave(EOS);
411
412					if (sp == (int)STACKMAX)
413						errx(1, "internal stack overflow");
414					eval((const char **) mstack+fp+1, 2,
415					    CALTYP, TRACESTATUS);
416
417					ep = PREVEP;	/* flush strspace */
418					sp = PREVSP;	/* previous sp..  */
419					fp = PREVFP;	/* rewind stack...*/
420				}
421			}
422		} else if (t == EOF) {
423			if (!mimic_gnu /* you can puke right there */
424			    && sp > -1 && ilevel <= 0) {
425				warnx( "unexpected end of input, unclosed parenthesis:");
426				dump_stack(paren, PARLEV);
427				exit(1);
428			}
429			if (ilevel <= 0)
430				break;			/* all done thanks.. */
431			release_input(infile+ilevel--);
432			emit_synchline();
433			bufbase = bbase[ilevel];
434			continue;
435		} else if (sp < 0) {		/* not in a macro at all */
436			reallyputchar(t);	/* output directly..	 */
437		}
438
439		else switch(t) {
440
441		case LPAREN:
442			if (PARLEV > 0)
443				chrsave(t);
444			while (isspace(l = gpbc())) /* skip blank, tab, nl.. */
445				if (PARLEV > 0)
446					chrsave(l);
447			pushback(l);
448			record(paren, PARLEV++);
449			break;
450
451		case RPAREN:
452			if (--PARLEV > 0)
453				chrsave(t);
454			else {			/* end of argument list */
455				chrsave(EOS);
456
457				if (sp == (int)STACKMAX)
458					errx(1, "internal stack overflow");
459
460				eval((const char **) mstack+fp+1, sp-fp,
461				    CALTYP, TRACESTATUS);
462
463				ep = PREVEP;	/* flush strspace */
464				sp = PREVSP;	/* previous sp..  */
465				fp = PREVFP;	/* rewind stack...*/
466			}
467			break;
468
469		case COMMA:
470			if (PARLEV == 1) {
471				chrsave(EOS);		/* new argument   */
472				while (isspace(l = gpbc()))
473					;
474				pushback(l);
475				pushs(ep);
476			} else
477				chrsave(t);
478			break;
479
480		default:
481			if (LOOK_AHEAD(t, scommt)) {
482				char *p;
483				for (p = scommt; *p; p++)
484					chrsave(*p);
485				for(;;) {
486					t = gpbc();
487					if (LOOK_AHEAD(t, ecommt)) {
488						for (p = ecommt; *p; p++)
489							chrsave(*p);
490						break;
491					}
492					if (t == EOF)
493					    break;
494					CHRSAVE(t);
495				}
496			} else
497				CHRSAVE(t);		/* stack the char */
498			break;
499		}
500	}
501}
502
503/*
504 * output string directly, without pushing it for reparses.
505 */
506void
507outputstr(const char *s)
508{
509	if (sp < 0)
510		reallyoutputstr(s);
511	else
512		while (*s)
513			CHRSAVE(*s++);
514}
515
516void
517reallyoutputstr(const char *s)
518{
519	if (synch_lines) {
520		while (*s) {
521			fputc(*s, active);
522			if (*s++ == '\n') {
523				infile[ilevel].synch_lineno++;
524				if (infile[ilevel].synch_lineno !=
525				    infile[ilevel].lineno)
526					do_emit_synchline();
527			}
528		}
529	} else
530		fputs(s, active);
531}
532
533void
534reallyputchar(int c)
535{
536	putc(c, active);
537	if (synch_lines && c == '\n') {
538		infile[ilevel].synch_lineno++;
539		if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
540			do_emit_synchline();
541	}
542}
543
544/*
545 * build an input token..
546 * consider only those starting with _ or A-Za-z.
547 */
548static ndptr
549inspect(int c, char *tp)
550{
551	char *name = tp;
552	char *etp = tp+MAXTOK;
553	ndptr p;
554
555	*tp++ = c;
556
557	while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
558		*tp++ = c;
559	if (c != EOF)
560		PUSHBACK(c);
561	*tp = EOS;
562	/* token is too long, it won't match anything, but it can still
563	 * be output. */
564	if (tp == ep) {
565		outputstr(name);
566		while (isalnum(c = gpbc()) || c == '_') {
567			if (sp < 0)
568				reallyputchar(c);
569			else
570				CHRSAVE(c);
571		}
572		*name = EOS;
573		return NULL;
574	}
575
576	p = ohash_find(&macros, ohash_qlookupi(&macros, name, (const char **)&tp));
577	if (p == NULL)
578		return NULL;
579	if (macro_getdef(p) == NULL)
580		return NULL;
581	return p;
582}
583
584/*
585 * initkwds - initialise m4 keywords as fast as possible.
586 * This very similar to install, but without certain overheads,
587 * such as calling lookup. Malloc is not used for storing the
588 * keyword strings, since we simply use the static pointers
589 * within keywrds block.
590 */
591static void
592initkwds(void)
593{
594	unsigned int type;
595	int i;
596
597	for (i = 0; i < (int)MAXKEYS; i++) {
598		type = keywrds[i].ktyp & TYPEMASK;
599		if ((keywrds[i].ktyp & NOARGS) == 0)
600			type |= NEEDARGS;
601		setup_builtin(keywrds[i].knam, type);
602	}
603}
604
605static void
606record(struct position *t, int lev)
607{
608	if (lev < MAXRECORD) {
609		t[lev].name = CURRENT_NAME;
610		t[lev].line = CURRENT_LINE;
611	}
612}
613
614static void
615dump_stack(struct position *t, int lev)
616{
617	int i;
618
619	for (i = 0; i < lev; i++) {
620		if (i == MAXRECORD) {
621			fprintf(stderr, "   ...\n");
622			break;
623		}
624		fprintf(stderr, "   %s at line %lu\n",
625			t[i].name, t[i].line);
626	}
627}
628
629
630static void
631enlarge_stack(void)
632{
633	STACKMAX += STACKMAX/2;
634	mstack = xreallocarray(mstack, STACKMAX, sizeof(stae),
635	    "Evaluation stack overflow (%lu)",
636	    (unsigned long)STACKMAX);
637	sstack = xrealloc(sstack, STACKMAX,
638	    "Evaluation stack overflow (%lu)",
639	    (unsigned long)STACKMAX);
640}
641