main.c revision 95887
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 $	*/
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. 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.
26 *
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
37 * SUCH DAMAGE.
38 */
39
40#include <sys/cdefs.h>
41__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
42	The Regents of the University of California.  All rights reserved.\n");
43__SCCSID("@(#)main.c	8.1 (Berkeley) 6/6/93");
44__RCSID_SOURCE("$OpenBSD: main.c,v 1.53 2002/04/26 16:15:16 espie Exp $");
45__FBSDID("$FreeBSD: head/usr.bin/m4/main.c 95887 2002-05-01 21:37:29Z jmallett $");
46
47/*
48 * main.c
49 * Facility: m4 macro processor
50 * by: oz
51 */
52
53#include <sys/types.h>
54#include <assert.h>
55#include <signal.h>
56#include <errno.h>
57#include <unistd.h>
58#include <stdio.h>
59#include <ctype.h>
60#include <string.h>
61#include <stddef.h>
62#include <stdlib.h>
63#include <err.h>
64#include "mdef.h"
65#include "stdd.h"
66#include "extern.h"
67#include "pathnames.h"
68
69ndptr hashtab[HASHSIZE];	/* hash table for macros etc.  */
70stae *mstack;		 	/* stack of m4 machine         */
71char *sstack;		 	/* shadow stack, for string space extension */
72static size_t STACKMAX;		/* current maximum size of stack */
73int sp; 			/* current m4  stack pointer   */
74int fp; 			/* m4 call frame pointer       */
75struct input_file infile[MAXINP];/* input file stack (0=stdin)  */
76char *inname[MAXINP];		/* names of these input files */
77int inlineno[MAXINP];		/* current number in each input file */
78FILE **outfile;			/* diversion array(0=bitbucket)*/
79int maxout;
80FILE *active;			/* active output file pointer  */
81int ilevel = 0; 		/* input file stack pointer    */
82int oindex = 0; 		/* diversion index..	       */
83const char *null = "";          /* as it says.. just a null..  */
84const char *m4wraps = "";       /* m4wrap string default..     */
85char lquote[MAXCCHARS+1] = {LQUOTE};	/* left quote character  (`)   */
86char rquote[MAXCCHARS+1] = {RQUOTE};	/* right quote character (')   */
87char scommt[MAXCCHARS+1] = {SCOMMT};	/* start character for comment */
88char ecommt[MAXCCHARS+1] = {ECOMMT};	/* end character for comment   */
89int synccpp;			/* Line synchronisation for C preprocessor */
90
91struct keyblk keywrds[] = {	/* m4 keywords to be installed */
92	{ "include",      INCLTYPE },
93	{ "sinclude",     SINCTYPE },
94	{ "define",       DEFITYPE },
95	{ "defn",         DEFNTYPE },
96	{ "divert",       DIVRTYPE | NOARGS },
97	{ "expr",         EXPRTYPE },
98	{ "eval",         EXPRTYPE },
99	{ "substr",       SUBSTYPE },
100	{ "ifelse",       IFELTYPE },
101	{ "ifdef",        IFDFTYPE },
102	{ "len",          LENGTYPE },
103	{ "incr",         INCRTYPE },
104	{ "decr",         DECRTYPE },
105	{ "dnl",          DNLNTYPE | NOARGS },
106	{ "changequote",  CHNQTYPE | NOARGS },
107	{ "changecom",    CHNCTYPE | NOARGS },
108	{ "index",        INDXTYPE },
109#ifdef EXTENDED
110	{ "paste",        PASTTYPE },
111	{ "spaste",       SPASTYPE },
112    	/* Newer extensions, needed to handle gnu-m4 scripts */
113	{ "indir",        INDIRTYPE},
114	{ "builtin",      BUILTINTYPE},
115	{ "patsubst",	  PATSTYPE},
116	{ "regexp",	  REGEXPTYPE},
117	{ "esyscmd",	  ESYSCMDTYPE},
118	{ "__file__",	  FILENAMETYPE | NOARGS},
119	{ "__line__",	  LINETYPE | NOARGS},
120#endif
121	{ "popdef",       POPDTYPE },
122	{ "pushdef",      PUSDTYPE },
123	{ "dumpdef",      DUMPTYPE | NOARGS },
124	{ "shift",        SHIFTYPE | NOARGS },
125	{ "translit",     TRNLTYPE },
126	{ "undefine",     UNDFTYPE },
127	{ "undivert",     UNDVTYPE | NOARGS },
128	{ "divnum",       DIVNTYPE | NOARGS },
129	{ "maketemp",     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#if defined(unix) || defined(__unix__)
139	{ "unix",         SELFTYPE | NOARGS },
140#else
141#ifdef vms
142	{ "vms",          SELFTYPE | NOARGS },
143#endif
144#endif
145};
146
147#define MAXKEYS	(sizeof(keywrds)/sizeof(struct keyblk))
148
149extern int optind;
150extern char *optarg;
151
152#define MAXRECORD 50
153static struct position {
154	char *name;
155	unsigned long line;
156} quotes[MAXRECORD], paren[MAXRECORD];
157
158static void record(struct position *, int);
159static void dump_stack(struct position *, int);
160
161static void macro(void);
162static void initkwds(void);
163static ndptr inspect(int, char *);
164static int do_look_ahead(int, const char *);
165
166static void enlarge_stack(void);
167
168int main(int, char *[]);
169
170int
171main(int argc, char *argv[])
172{
173	int c;
174	int n;
175	char *p;
176
177	traceout = stderr;
178
179	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
180		signal(SIGINT, onintr);
181
182	initkwds();
183	initspaces();
184	STACKMAX = INITSTACKMAX;
185
186	mstack = (stae *)xalloc(sizeof(stae) * STACKMAX);
187	sstack = (char *)xalloc(STACKMAX);
188
189	maxout = 0;
190	outfile = NULL;
191	resizedivs(MAXOUT);
192
193	while ((c = getopt(argc, argv, "gst:d:D:U:o:I:")) != -1)
194		switch(c) {
195
196		case 'D':               /* define something..*/
197			for (p = optarg; *p; p++)
198				if (*p == '=')
199					break;
200			if (*p)
201				*p++ = EOS;
202			dodefine(optarg, p);
203			break;
204		case 'I':
205			addtoincludepath(optarg);
206			break;
207		case 'U':               /* undefine...       */
208			remhash(optarg, TOP);
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			synccpp = 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	active = stdout;		/* default active output     */
233	bbase[0] = bufbase;
234        if (!argc) {
235 		sp = -1;		/* stack pointer initialized */
236		fp = 0; 		/* frame pointer initialized */
237		set_input(infile+0, stdin, "stdin");
238					/* default input (naturally) */
239		if ((inname[0] = strdup("-")) == NULL)
240			err(1, NULL);
241		inlineno[0] = 1;
242		emitline();
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			if ((inname[0] = strdup(p)) == NULL)
254				err(1, NULL);
255			inlineno[0] = 1;
256			emitline();
257			macro();
258		    	release_input(infile);
259		}
260
261	if (*m4wraps) { 		/* anything for rundown ??   */
262		ilevel = 0;		/* in case m4wrap includes.. */
263		bufbase = bp = buf;	/* use the entire buffer   */
264		pbstr(m4wraps); 	/* user-defined wrapup act   */
265		macro();		/* last will and testament   */
266	}
267
268	if (active != stdout)
269		active = stdout;	/* reset output just in case */
270	for (n = 1; n < maxout; n++)	/* default wrap-up: undivert */
271		if (outfile[n] != NULL)
272			getdiv(n);
273					/* remove bitbucket if used  */
274	if (outfile[0] != NULL) {
275		(void) fclose(outfile[0]);
276	}
277
278	return 0;
279}
280
281/*
282 * Look ahead for `token'.
283 * (on input `t == token[0]')
284 * Used for comment and quoting delimiters.
285 * Returns 1 if `token' present; copied to output.
286 *         0 if `token' not found; all characters pushed back
287 */
288static int
289do_look_ahead(int t, const char *token)
290{
291	int i;
292
293	assert((unsigned char)t == (unsigned char)token[0]);
294
295	for (i = 1; *++token; i++) {
296		t = gpbc();
297		if (t == EOF || (unsigned char)t != (unsigned char)*token) {
298			putback(t);
299			while (--i)
300				putback(*--token);
301			return 0;
302		}
303	}
304	return 1;
305}
306
307#define LOOK_AHEAD(t, token) (t != EOF && 		\
308    (unsigned char)(t)==(unsigned char)(token)[0] && 	\
309    do_look_ahead(t,token))
310
311/*
312 * macro - the work horse..
313 */
314static void
315macro()
316{
317	char token[MAXTOK+1];
318	int t, l;
319	ndptr p;
320	int  nlpar;
321
322	cycle {
323		t = gpbc();
324		if (t == '_' || isalpha(t)) {
325			p = inspect(t, token);
326			if (p != nil)
327				putback(l = gpbc());
328			if (p == nil || (l != LPAREN &&
329			    (p->type & NEEDARGS) != 0))
330				outputstr(token);
331			else {
332		/*
333		 * real thing.. First build a call frame:
334		 */
335				pushf(fp);	/* previous call frm */
336				pushf(p->type); /* type of the call  */
337				pushf(0);	/* parenthesis level */
338				fp = sp;	/* new frame pointer */
339		/*
340		 * now push the string arguments:
341		 */
342				pushs1(p->defn);	/* defn string */
343				pushs1(p->name);	/* macro name  */
344				pushs(ep);	      	/* start next..*/
345
346				if (l != LPAREN && PARLEV == 0)  {
347				    /* no bracks  */
348					chrsave(EOS);
349
350					if (sp == STACKMAX)
351						errx(1, "internal stack overflow");
352					eval((const char **) mstack+fp+1, 2,
353					    CALTYP);
354
355					ep = PREVEP;	/* flush strspace */
356					sp = PREVSP;	/* previous sp..  */
357					fp = PREVFP;	/* rewind stack...*/
358				}
359			}
360		} else if (t == EOF) {
361			if (sp > -1) {
362				warnx( "unexpected end of input, unclosed parenthesis:");
363				dump_stack(paren, PARLEV);
364				exit(1);
365			}
366			if (ilevel <= 0)
367				break;			/* all done thanks.. */
368			release_input(infile+ilevel--);
369			free(inname[ilevel+1]);
370			bufbase = bbase[ilevel];
371			emitline();
372			continue;
373		}
374	/*
375	 * non-alpha token possibly seen..
376	 * [the order of else if .. stmts is important.]
377	 */
378		else if (LOOK_AHEAD(t,lquote)) {	/* strip quotes */
379			nlpar = 0;
380			record(quotes, nlpar++);
381			/*
382			 * Opening quote: scan forward until matching
383			 * closing quote has been found.
384			 */
385			do {
386
387				l = gpbc();
388				if (LOOK_AHEAD(l,rquote)) {
389					if (--nlpar > 0)
390						outputstr(rquote);
391				} else if (LOOK_AHEAD(l,lquote)) {
392					record(quotes, nlpar++);
393					outputstr(lquote);
394				} else if (l == EOF) {
395					if (nlpar == 1)
396						warnx("unclosed quote:");
397					else
398						warnx("%d unclosed quotes:", nlpar);
399					dump_stack(quotes, nlpar);
400					exit(1);
401				} else {
402					if (nlpar > 0) {
403						if (sp < 0)
404							putc(l, active);
405						else
406							CHRSAVE(l);
407					}
408				}
409			}
410			while (nlpar != 0);
411		}
412
413		else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
414			fputs(scommt, active);
415
416			for(;;) {
417				t = gpbc();
418				if (LOOK_AHEAD(t, ecommt)) {
419					fputs(ecommt, active);
420					break;
421				}
422				if (t == EOF)
423					break;
424				putc(t, active);
425			}
426		}
427
428		else if (sp < 0) {		/* not in a macro at all */
429			putc(t, active);	/* output directly..	 */
430		}
431
432		else switch(t) {
433
434		case LPAREN:
435			if (PARLEV > 0)
436				chrsave(t);
437			while (isspace(l = gpbc()))
438				;		/* skip blank, tab, nl.. */
439			putback(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 == STACKMAX)
450					errx(1, "internal stack overflow");
451
452				eval((const char **) mstack+fp+1, sp-fp,
453				    CALTYP);
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				putback(l);
467				pushs(ep);
468			} else
469				chrsave(t);
470			break;
471
472		default:
473			if (LOOK_AHEAD(t, scommt)) {
474				char *pc;
475				for (pc = scommt; *pc; pc++)
476					chrsave(*pc);
477				for(;;) {
478					t = gpbc();
479					if (LOOK_AHEAD(t, ecommt)) {
480						for (pc = ecommt; *pc; pc++)
481							chrsave(*pc);
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		while (*s)
503			putc(*s++, active);
504	else
505		while (*s)
506			CHRSAVE(*s++);
507}
508
509/*
510 * build an input token..
511 * consider only those starting with _ or A-Za-z. This is a
512 * combo with lookup to speed things up.
513 */
514static ndptr
515inspect(int c, char *tp)
516{
517	char *name = tp;
518	char *etp = tp+MAXTOK;
519	ndptr p;
520	unsigned int h;
521
522	h = *tp++ = c;
523
524	while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
525		h = (h << 5) + h + (*tp++ = c);
526	if (c != EOF)
527		PUTBACK(c);
528	*tp = EOS;
529	/* token is too long, it won't match anything, but it can still
530	 * be output. */
531	if (tp == ep) {
532		outputstr(name);
533		while (isalnum(c = gpbc()) || c == '_') {
534			if (sp < 0)
535				putc(c, active);
536			else
537				CHRSAVE(c);
538		}
539		*name = EOS;
540		return nil;
541	}
542
543	for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
544		if (h == p->hv && STREQ(name, p->name))
545			break;
546	return p;
547}
548
549/*
550 * initkwds - initialise m4 keywords as fast as possible.
551 * This very similar to install, but without certain overheads,
552 * such as calling lookup. Malloc is not used for storing the
553 * keyword strings, since we simply use the static pointers
554 * within keywrds block.
555 */
556static void
557initkwds()
558{
559	size_t i;
560	unsigned int h;
561	ndptr p;
562
563	for (i = 0; i < MAXKEYS; i++) {
564		h = hash(keywrds[i].knam);
565		p = (ndptr) xalloc(sizeof(struct ndblock));
566		p->nxtptr = hashtab[h % HASHSIZE];
567		hashtab[h % HASHSIZE] = p;
568		p->name = xstrdup(keywrds[i].knam);
569		p->defn = xstrdup(null);
570		p->hv = h;
571		p->type = keywrds[i].ktyp & TYPEMASK;
572		if ((keywrds[i].ktyp & NOARGS) == 0)
573			p->type |= NEEDARGS;
574	}
575}
576
577/* Look up a builtin type, even if overridden by the user */
578int
579builtin_type(const char *key)
580{
581	int i;
582
583	for (i = 0; i != MAXKEYS; i++)
584		if (STREQ(keywrds[i].knam, key))
585			return keywrds[i].ktyp;
586	return -1;
587}
588
589const char *
590builtin_realname(int n)
591{
592	int i;
593
594	for (i = 0; i != MAXKEYS; i++)
595		if (((keywrds[i].ktyp ^ n) & TYPEMASK) == 0)
596			return keywrds[i].knam;
597	return NULL;
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()
627{
628	STACKMAX *= 2;
629	mstack = realloc(mstack, sizeof(stae) * STACKMAX);
630	sstack = realloc(sstack, STACKMAX);
631	if (mstack == NULL || sstack == NULL)
632		errx(1, "Evaluation stack overflow (%lu)",
633		    (unsigned long)STACKMAX);
634}
635
636/* Emit preprocessor #line directive if -s option used. */
637void
638emitline(void)
639{
640
641	if (synccpp)
642		fprintf(active, "#line %d \"%s\"\n", inlineno[ilevel],
643			inname[ilevel]);
644}
645