main.c revision 331722
1/*	$OpenBSD: main.c,v 1.86 2015/11/03 16:21:47 deraadt 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 331722 2018-03-29 02:50:57Z eadler $");
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 */
82
83struct keyblk {
84        const char    *knam;          /* keyword name */
85        int     ktyp;           /* keyword type */
86};
87
88static struct keyblk keywrds[] = {	/* m4 keywords to be installed */
89	{ "include",      INCLTYPE },
90	{ "sinclude",     SINCTYPE },
91	{ "define",       DEFITYPE },
92	{ "defn",         DEFNTYPE },
93	{ "divert",       DIVRTYPE | NOARGS },
94	{ "expr",         EXPRTYPE },
95	{ "eval",         EXPRTYPE },
96	{ "substr",       SUBSTYPE },
97	{ "ifelse",       IFELTYPE },
98	{ "ifdef",        IFDFTYPE },
99	{ "len",          LENGTYPE },
100	{ "incr",         INCRTYPE },
101	{ "decr",         DECRTYPE },
102	{ "dnl",          DNLNTYPE | NOARGS },
103	{ "changequote",  CHNQTYPE | NOARGS },
104	{ "changecom",    CHNCTYPE | NOARGS },
105	{ "index",        INDXTYPE },
106#ifdef EXTENDED
107	{ "paste",        PASTTYPE },
108	{ "spaste",       SPASTYPE },
109	/* Newer extensions, needed to handle gnu-m4 scripts */
110	{ "indir",        INDIRTYPE},
111	{ "builtin",      BUILTINTYPE},
112	{ "patsubst",	  PATSTYPE},
113	{ "regexp",	  REGEXPTYPE},
114	{ "esyscmd",	  ESYSCMDTYPE},
115	{ "__file__",	  FILENAMETYPE | NOARGS},
116	{ "__line__",	  LINETYPE | NOARGS},
117#endif
118	{ "popdef",       POPDTYPE },
119	{ "pushdef",      PUSDTYPE },
120	{ "dumpdef",      DUMPTYPE | NOARGS },
121	{ "shift",        SHIFTYPE | NOARGS },
122	{ "translit",     TRNLTYPE },
123	{ "undefine",     UNDFTYPE },
124	{ "undivert",     UNDVTYPE | NOARGS },
125	{ "divnum",       DIVNTYPE | NOARGS },
126	{ "maketemp",     MKTMTYPE },
127	{ "mkstemp",      MKTMTYPE },
128	{ "errprint",     ERRPTYPE | NOARGS },
129	{ "m4wrap",       M4WRTYPE | NOARGS },
130	{ "m4exit",       EXITTYPE | NOARGS },
131	{ "syscmd",       SYSCTYPE },
132	{ "sysval",       SYSVTYPE | NOARGS },
133	{ "traceon",	  TRACEONTYPE | NOARGS },
134	{ "traceoff",	  TRACEOFFTYPE | NOARGS },
135
136	{ "unix",         SELFTYPE | NOARGS },
137};
138
139#define MAXKEYS	(sizeof(keywrds)/sizeof(struct keyblk))
140
141extern int optind;
142extern char *optarg;
143
144#define MAXRECORD 50
145static struct position {
146	char *name;
147	unsigned long line;
148} quotes[MAXRECORD], paren[MAXRECORD];
149
150static void record(struct position *, int);
151static void dump_stack(struct position *, int);
152
153static void macro(void);
154static void initkwds(void);
155static ndptr inspect(int, char *);
156static int do_look_ahead(int, const char *);
157static void reallyoutputstr(const char *);
158static void reallyputchar(int);
159
160static void enlarge_stack(void);
161
162int main(int, char *[]);
163
164int exit_code = 0;
165
166int
167main(int argc, char *argv[])
168{
169	int c;
170	int n;
171	char *p;
172
173	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
174		signal(SIGINT, onintr);
175
176	init_macros();
177	initspaces();
178	STACKMAX = INITSTACKMAX;
179
180	mstack = xreallocarray(NULL, STACKMAX, sizeof(stae), NULL);
181	sstack = xalloc(STACKMAX, NULL);
182
183	maxout = 0;
184	outfile = NULL;
185	resizedivs(MAXOUT);
186
187	while ((c = getopt(argc, argv, "gst:d:D:U:o:I:P")) != -1)
188		switch(c) {
189
190		case 'D':               /* define something..*/
191			for (p = optarg; *p; p++)
192				if (*p == '=')
193					break;
194			if (*p)
195				*p++ = EOS;
196			dodefine(optarg, p);
197			break;
198		case 'I':
199			addtoincludepath(optarg);
200			break;
201		case 'P':
202			prefix_builtins = 1;
203			break;
204		case 'U':               /* undefine...       */
205			macro_popdef(optarg);
206			break;
207		case 'g':
208			mimic_gnu = 1;
209			break;
210		case 'd':
211			set_trace_flags(optarg);
212			break;
213		case 's':
214			synch_lines = 1;
215			break;
216		case 't':
217			mark_traced(optarg, 1);
218			break;
219		case 'o':
220			trace_file(optarg);
221                        break;
222		case '?':
223			usage();
224		}
225
226        argc -= optind;
227        argv += optind;
228
229	initkwds();
230	if (mimic_gnu)
231		setup_builtin("format", FORMATTYPE);
232
233	active = stdout;		/* default active output     */
234	bbase[0] = bufbase;
235        if (!argc) {
236		sp = -1;		/* stack pointer initialized */
237		fp = 0;			/* frame pointer initialized */
238		set_input(infile+0, stdin, "stdin");
239					/* default input (naturally) */
240		macro();
241	} else
242		for (; argc--; ++argv) {
243			p = *argv;
244			if (p[0] == '-' && p[1] == EOS)
245				set_input(infile, stdin, "stdin");
246			else if (fopen_trypath(infile, p) == NULL)
247				err(1, "%s", p);
248			sp = -1;
249			fp = 0;
250			macro();
251			release_input(infile);
252		}
253
254	if (wrapindex) {
255		int i;
256
257		ilevel = 0;		/* in case m4wrap includes.. */
258		bufbase = bp = buf;	/* use the entire buffer   */
259		if (mimic_gnu) {
260			while (wrapindex != 0) {
261				for (i = 0; i < wrapindex; i++)
262					pbstr(m4wraps[i]);
263				wrapindex =0;
264				macro();
265			}
266		} else {
267			for (i = 0; i < wrapindex; i++) {
268				pbstr(m4wraps[i]);
269				macro();
270			}
271		}
272	}
273
274	if (active != stdout)
275		active = stdout;	/* reset output just in case */
276	for (n = 1; n < maxout; n++)	/* default wrap-up: undivert */
277		if (outfile[n] != NULL)
278			getdiv(n);
279					/* remove bitbucket if used  */
280	if (outfile[0] != NULL) {
281		(void) fclose(outfile[0]);
282	}
283
284	return exit_code;
285}
286
287/*
288 * Look ahead for `token'.
289 * (on input `t == token[0]')
290 * Used for comment and quoting delimiters.
291 * Returns 1 if `token' present; copied to output.
292 *         0 if `token' not found; all characters pushed back
293 */
294static int
295do_look_ahead(int t, const char *token)
296{
297	int i;
298
299	assert((unsigned char)t == (unsigned char)token[0]);
300
301	for (i = 1; *++token; i++) {
302		t = gpbc();
303		if (t == EOF || (unsigned char)t != (unsigned char)*token) {
304			pushback(t);
305			while (--i)
306				pushback(*--token);
307			return 0;
308		}
309	}
310	return 1;
311}
312
313#define LOOK_AHEAD(t, token) (t != EOF &&		\
314    (unsigned char)(t)==(unsigned char)(token)[0] &&	\
315    do_look_ahead(t,token))
316
317/*
318 * macro - the work horse..
319 */
320static void
321macro(void)
322{
323	char token[MAXTOK+1];
324	int t, l;
325	ndptr p;
326	int  nlpar;
327
328	cycle {
329		t = gpbc();
330
331		if (LOOK_AHEAD(t,lquote)) {	/* strip quotes */
332			nlpar = 0;
333			record(quotes, nlpar++);
334			/*
335			 * Opening quote: scan forward until matching
336			 * closing quote has been found.
337			 */
338			do {
339
340				l = gpbc();
341				if (LOOK_AHEAD(l,rquote)) {
342					if (--nlpar > 0)
343						outputstr(rquote);
344				} else if (LOOK_AHEAD(l,lquote)) {
345					record(quotes, nlpar++);
346					outputstr(lquote);
347				} else if (l == EOF) {
348					if (nlpar == 1)
349						warnx("unclosed quote:");
350					else
351						warnx("%d unclosed quotes:", nlpar);
352					dump_stack(quotes, nlpar);
353					exit(1);
354				} else {
355					if (nlpar > 0) {
356						if (sp < 0)
357							reallyputchar(l);
358						else
359							CHRSAVE(l);
360					}
361				}
362			}
363			while (nlpar != 0);
364		} else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
365			reallyoutputstr(scommt);
366
367			for(;;) {
368				t = gpbc();
369				if (LOOK_AHEAD(t, ecommt)) {
370					reallyoutputstr(ecommt);
371					break;
372				}
373				if (t == EOF)
374					break;
375				reallyputchar(t);
376			}
377		} else if (t == '_' || isalpha(t)) {
378			p = inspect(t, token);
379			if (p != NULL)
380				pushback(l = gpbc());
381			if (p == NULL || (l != LPAREN &&
382			    (macro_getdef(p)->type & NEEDARGS) != 0))
383				outputstr(token);
384			else {
385		/*
386		 * real thing.. First build a call frame:
387		 */
388				pushf(fp);	/* previous call frm */
389				pushf(macro_getdef(p)->type); /* type of the call  */
390				pushf(is_traced(p));
391				pushf(0);	/* parenthesis level */
392				fp = sp;	/* new frame pointer */
393		/*
394		 * now push the string arguments:
395		 */
396				pushdef(p);			/* defn string */
397				pushs1((char *)macro_name(p));	/* macro name  */
398				pushs(ep);			/* start next..*/
399
400				if (l != LPAREN && PARLEV == 0) {
401				    /* no bracks  */
402					chrsave(EOS);
403
404					if (sp == (int)STACKMAX)
405						errx(1, "internal stack overflow");
406					eval((const char **) mstack+fp+1, 2,
407					    CALTYP, TRACESTATUS);
408
409					ep = PREVEP;	/* flush strspace */
410					sp = PREVSP;	/* previous sp..  */
411					fp = PREVFP;	/* rewind stack...*/
412				}
413			}
414		} else if (t == EOF) {
415			if (!mimic_gnu /* you can puke right there */
416			    && sp > -1 && ilevel <= 0) {
417				warnx( "unexpected end of input, unclosed parenthesis:");
418				dump_stack(paren, PARLEV);
419				exit(1);
420			}
421			if (ilevel <= 0)
422				break;			/* all done thanks.. */
423			release_input(infile+ilevel--);
424			emit_synchline();
425			bufbase = bbase[ilevel];
426			continue;
427		} else if (sp < 0) {		/* not in a macro at all */
428			reallyputchar(t);	/* output directly..	 */
429		}
430
431		else switch(t) {
432
433		case LPAREN:
434			if (PARLEV > 0)
435				chrsave(t);
436			while (isspace(l = gpbc())) /* skip blank, tab, nl.. */
437				if (PARLEV > 0)
438					chrsave(l);
439			pushback(l);
440			record(paren, PARLEV++);
441			break;
442
443		case RPAREN:
444			if (--PARLEV > 0)
445				chrsave(t);
446			else {			/* end of argument list */
447				chrsave(EOS);
448
449				if (sp == (int)STACKMAX)
450					errx(1, "internal stack overflow");
451
452				eval((const char **) mstack+fp+1, sp-fp,
453				    CALTYP, TRACESTATUS);
454
455				ep = PREVEP;	/* flush strspace */
456				sp = PREVSP;	/* previous sp..  */
457				fp = PREVFP;	/* rewind stack...*/
458			}
459			break;
460
461		case COMMA:
462			if (PARLEV == 1) {
463				chrsave(EOS);		/* new argument   */
464				while (isspace(l = gpbc()))
465					;
466				pushback(l);
467				pushs(ep);
468			} else
469				chrsave(t);
470			break;
471
472		default:
473			if (LOOK_AHEAD(t, scommt)) {
474				char *p;
475				for (p = scommt; *p; p++)
476					chrsave(*p);
477				for(;;) {
478					t = gpbc();
479					if (LOOK_AHEAD(t, ecommt)) {
480						for (p = ecommt; *p; p++)
481							chrsave(*p);
482						break;
483					}
484					if (t == EOF)
485					    break;
486					CHRSAVE(t);
487				}
488			} else
489				CHRSAVE(t);		/* stack the char */
490			break;
491		}
492	}
493}
494
495/*
496 * output string directly, without pushing it for reparses.
497 */
498void
499outputstr(const char *s)
500{
501	if (sp < 0)
502		reallyoutputstr(s);
503	else
504		while (*s)
505			CHRSAVE(*s++);
506}
507
508void
509reallyoutputstr(const char *s)
510{
511	if (synch_lines) {
512		while (*s) {
513			fputc(*s, active);
514			if (*s++ == '\n') {
515				infile[ilevel].synch_lineno++;
516				if (infile[ilevel].synch_lineno !=
517				    infile[ilevel].lineno)
518					do_emit_synchline();
519			}
520		}
521	} else
522		fputs(s, active);
523}
524
525void
526reallyputchar(int c)
527{
528	putc(c, active);
529	if (synch_lines && c == '\n') {
530		infile[ilevel].synch_lineno++;
531		if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
532			do_emit_synchline();
533	}
534}
535
536/*
537 * build an input token..
538 * consider only those starting with _ or A-Za-z.
539 */
540static ndptr
541inspect(int c, char *tp)
542{
543	char *name = tp;
544	char *etp = tp+MAXTOK;
545	ndptr p;
546
547	*tp++ = c;
548
549	while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
550		*tp++ = c;
551	if (c != EOF)
552		PUSHBACK(c);
553	*tp = EOS;
554	/* token is too long, it won't match anything, but it can still
555	 * be output. */
556	if (tp == ep) {
557		outputstr(name);
558		while (isalnum(c = gpbc()) || c == '_') {
559			if (sp < 0)
560				reallyputchar(c);
561			else
562				CHRSAVE(c);
563		}
564		*name = EOS;
565		return NULL;
566	}
567
568	p = ohash_find(&macros, ohash_qlookupi(&macros, name, (const char **)&tp));
569	if (p == NULL)
570		return NULL;
571	if (macro_getdef(p) == NULL)
572		return NULL;
573	return p;
574}
575
576/*
577 * initkwds - initialise m4 keywords as fast as possible.
578 * This very similar to install, but without certain overheads,
579 * such as calling lookup. Malloc is not used for storing the
580 * keyword strings, since we simply use the static pointers
581 * within keywrds block.
582 */
583static void
584initkwds(void)
585{
586	unsigned int type;
587	int i;
588
589	for (i = 0; i < (int)MAXKEYS; i++) {
590		type = keywrds[i].ktyp & TYPEMASK;
591		if ((keywrds[i].ktyp & NOARGS) == 0)
592			type |= NEEDARGS;
593		setup_builtin(keywrds[i].knam, type);
594	}
595}
596
597static void
598record(struct position *t, int lev)
599{
600	if (lev < MAXRECORD) {
601		t[lev].name = CURRENT_NAME;
602		t[lev].line = CURRENT_LINE;
603	}
604}
605
606static void
607dump_stack(struct position *t, int lev)
608{
609	int i;
610
611	for (i = 0; i < lev; i++) {
612		if (i == MAXRECORD) {
613			fprintf(stderr, "   ...\n");
614			break;
615		}
616		fprintf(stderr, "   %s at line %lu\n",
617			t[i].name, t[i].line);
618	}
619}
620
621
622static void
623enlarge_stack(void)
624{
625	STACKMAX += STACKMAX/2;
626	mstack = xreallocarray(mstack, STACKMAX, sizeof(stae),
627	    "Evaluation stack overflow (%lu)",
628	    (unsigned long)STACKMAX);
629	sstack = xrealloc(sstack, STACKMAX,
630	    "Evaluation stack overflow (%lu)",
631	    (unsigned long)STACKMAX);
632}
633