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