main.c revision 95918
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 95918 2002-05-02 05:25:23Z 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
149#define MAXRECORD 50
150static struct position {
151	char *name;
152	unsigned long line;
153} quotes[MAXRECORD], paren[MAXRECORD];
154
155static void record(struct position *, int);
156static void dump_stack(struct position *, int);
157
158static void macro(void);
159static void initkwds(void);
160static ndptr inspect(int, char *);
161static int do_look_ahead(int, const char *);
162
163static void enlarge_stack(void);
164
165int main(int, char *[]);
166
167int
168main(int argc, char *argv[])
169{
170	int c;
171	int n;
172	char *p;
173
174	traceout = stderr;
175
176	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
177		signal(SIGINT, onintr);
178
179	initkwds();
180	initspaces();
181	STACKMAX = INITSTACKMAX;
182
183	mstack = (stae *)xalloc(sizeof(stae) * STACKMAX);
184	sstack = (char *)xalloc(STACKMAX);
185
186	maxout = 0;
187	outfile = NULL;
188	resizedivs(MAXOUT);
189
190	while ((c = getopt(argc, argv, "gst:d:D:U:o:I:")) != -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 'U':               /* undefine...       */
205			remhash(optarg, TOP);
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			synccpp = 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	active = stdout;		/* default active output     */
230	bbase[0] = bufbase;
231        if (!argc) {
232 		sp = -1;		/* stack pointer initialized */
233		fp = 0; 		/* frame pointer initialized */
234		set_input(infile+0, stdin, "stdin");
235					/* default input (naturally) */
236		if ((inname[0] = strdup("-")) == NULL)
237			err(1, NULL);
238		inlineno[0] = 1;
239		emitline();
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			if ((inname[0] = strdup(p)) == NULL)
251				err(1, NULL);
252			inlineno[0] = 1;
253			emitline();
254			macro();
255		    	release_input(infile);
256		}
257
258	if (*m4wraps) { 		/* anything for rundown ??   */
259		ilevel = 0;		/* in case m4wrap includes.. */
260		bufbase = bp = buf;	/* use the entire buffer   */
261		pbstr(m4wraps); 	/* user-defined wrapup act   */
262		macro();		/* last will and testament   */
263	}
264
265	if (active != stdout)
266		active = stdout;	/* reset output just in case */
267	for (n = 1; n < maxout; n++)	/* default wrap-up: undivert */
268		if (outfile[n] != NULL)
269			getdiv(n);
270					/* remove bitbucket if used  */
271	if (outfile[0] != NULL) {
272		(void) fclose(outfile[0]);
273	}
274
275	return 0;
276}
277
278/*
279 * Look ahead for `token'.
280 * (on input `t == token[0]')
281 * Used for comment and quoting delimiters.
282 * Returns 1 if `token' present; copied to output.
283 *         0 if `token' not found; all characters pushed back
284 */
285static int
286do_look_ahead(int t, const char *token)
287{
288	int i;
289
290	assert((unsigned char)t == (unsigned char)token[0]);
291
292	for (i = 1; *++token; i++) {
293		t = gpbc();
294		if (t == EOF || (unsigned char)t != (unsigned char)*token) {
295			putback(t);
296			while (--i)
297				putback(*--token);
298			return 0;
299		}
300	}
301	return 1;
302}
303
304#define LOOK_AHEAD(t, token) (t != EOF && 		\
305    (unsigned char)(t)==(unsigned char)(token)[0] && 	\
306    do_look_ahead(t,token))
307
308/*
309 * macro - the work horse..
310 */
311static void
312macro()
313{
314	char token[MAXTOK+1];
315	int t, l;
316	ndptr p;
317	int  nlpar;
318
319	cycle {
320		t = gpbc();
321		if (t == '_' || isalpha(t)) {
322			p = inspect(t, token);
323			if (p != nil)
324				putback(l = gpbc());
325			if (p == nil || (l != LPAREN &&
326			    (p->type & NEEDARGS) != 0))
327				outputstr(token);
328			else {
329		/*
330		 * real thing.. First build a call frame:
331		 */
332				pushf(fp);	/* previous call frm */
333				pushf(p->type); /* type of the call  */
334				pushf(0);	/* parenthesis level */
335				fp = sp;	/* new frame pointer */
336		/*
337		 * now push the string arguments:
338		 */
339				pushs1(p->defn);	/* defn string */
340				pushs1(p->name);	/* macro name  */
341				pushs(ep);	      	/* start next..*/
342
343				if (l != LPAREN && PARLEV == 0)  {
344				    /* no bracks  */
345					chrsave(EOS);
346
347					if (sp == STACKMAX)
348						errx(1, "internal stack overflow");
349					eval((const char **) mstack+fp+1, 2,
350					    CALTYP);
351
352					ep = PREVEP;	/* flush strspace */
353					sp = PREVSP;	/* previous sp..  */
354					fp = PREVFP;	/* rewind stack...*/
355				}
356			}
357		} else if (t == EOF) {
358			if (sp > -1) {
359				warnx( "unexpected end of input, unclosed parenthesis:");
360				dump_stack(paren, PARLEV);
361				exit(1);
362			}
363			if (ilevel <= 0)
364				break;			/* all done thanks.. */
365			release_input(infile+ilevel--);
366			free(inname[ilevel+1]);
367			bufbase = bbase[ilevel];
368			emitline();
369			continue;
370		}
371	/*
372	 * non-alpha token possibly seen..
373	 * [the order of else if .. stmts is important.]
374	 */
375		else if (LOOK_AHEAD(t,lquote)) {	/* strip quotes */
376			nlpar = 0;
377			record(quotes, nlpar++);
378			/*
379			 * Opening quote: scan forward until matching
380			 * closing quote has been found.
381			 */
382			do {
383
384				l = gpbc();
385				if (LOOK_AHEAD(l,rquote)) {
386					if (--nlpar > 0)
387						outputstr(rquote);
388				} else if (LOOK_AHEAD(l,lquote)) {
389					record(quotes, nlpar++);
390					outputstr(lquote);
391				} else if (l == EOF) {
392					if (nlpar == 1)
393						warnx("unclosed quote:");
394					else
395						warnx("%d unclosed quotes:", nlpar);
396					dump_stack(quotes, nlpar);
397					exit(1);
398				} else {
399					if (nlpar > 0) {
400						if (sp < 0)
401							putc(l, active);
402						else
403							CHRSAVE(l);
404					}
405				}
406			}
407			while (nlpar != 0);
408		}
409
410		else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
411			fputs(scommt, active);
412
413			for(;;) {
414				t = gpbc();
415				if (LOOK_AHEAD(t, ecommt)) {
416					fputs(ecommt, active);
417					break;
418				}
419				if (t == EOF)
420					break;
421				putc(t, active);
422			}
423		}
424
425		else if (sp < 0) {		/* not in a macro at all */
426			putc(t, active);	/* output directly..	 */
427		}
428
429		else switch(t) {
430
431		case LPAREN:
432			if (PARLEV > 0)
433				chrsave(t);
434			while (isspace(l = gpbc()))
435				;		/* skip blank, tab, nl.. */
436			putback(l);
437			record(paren, PARLEV++);
438			break;
439
440		case RPAREN:
441			if (--PARLEV > 0)
442				chrsave(t);
443			else {			/* end of argument list */
444				chrsave(EOS);
445
446				if (sp == STACKMAX)
447					errx(1, "internal stack overflow");
448
449				eval((const char **) mstack+fp+1, sp-fp,
450				    CALTYP);
451
452				ep = PREVEP;	/* flush strspace */
453				sp = PREVSP;	/* previous sp..  */
454				fp = PREVFP;	/* rewind stack...*/
455			}
456			break;
457
458		case COMMA:
459			if (PARLEV == 1) {
460				chrsave(EOS);		/* new argument   */
461				while (isspace(l = gpbc()))
462					;
463				putback(l);
464				pushs(ep);
465			} else
466				chrsave(t);
467			break;
468
469		default:
470			if (LOOK_AHEAD(t, scommt)) {
471				char *pc;
472				for (pc = scommt; *pc; pc++)
473					chrsave(*pc);
474				for(;;) {
475					t = gpbc();
476					if (LOOK_AHEAD(t, ecommt)) {
477						for (pc = ecommt; *pc; pc++)
478							chrsave(*pc);
479						break;
480					}
481					if (t == EOF)
482					    break;
483					CHRSAVE(t);
484				}
485			} else
486				CHRSAVE(t);		/* stack the char */
487			break;
488		}
489	}
490}
491
492/*
493 * output string directly, without pushing it for reparses.
494 */
495void
496outputstr(const char *s)
497{
498	if (sp < 0)
499		while (*s)
500			putc(*s++, active);
501	else
502		while (*s)
503			CHRSAVE(*s++);
504}
505
506/*
507 * build an input token..
508 * consider only those starting with _ or A-Za-z. This is a
509 * combo with lookup to speed things up.
510 */
511static ndptr
512inspect(int c, char *tp)
513{
514	char *name = tp;
515	char *etp = tp+MAXTOK;
516	ndptr p;
517	unsigned int h;
518
519	h = *tp++ = c;
520
521	while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
522		h = (h << 5) + h + (*tp++ = c);
523	if (c != EOF)
524		PUTBACK(c);
525	*tp = EOS;
526	/* token is too long, it won't match anything, but it can still
527	 * be output. */
528	if (tp == ep) {
529		outputstr(name);
530		while (isalnum(c = gpbc()) || c == '_') {
531			if (sp < 0)
532				putc(c, active);
533			else
534				CHRSAVE(c);
535		}
536		*name = EOS;
537		return nil;
538	}
539
540	for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
541		if (h == p->hv && STREQ(name, p->name))
542			break;
543	return p;
544}
545
546/*
547 * initkwds - initialise m4 keywords as fast as possible.
548 * This very similar to install, but without certain overheads,
549 * such as calling lookup. Malloc is not used for storing the
550 * keyword strings, since we simply use the static pointers
551 * within keywrds block.
552 */
553static void
554initkwds()
555{
556	size_t i;
557	unsigned int h;
558	ndptr p;
559
560	for (i = 0; i < MAXKEYS; i++) {
561		h = hash(keywrds[i].knam);
562		p = (ndptr) xalloc(sizeof(struct ndblock));
563		p->nxtptr = hashtab[h % HASHSIZE];
564		hashtab[h % HASHSIZE] = p;
565		p->name = xstrdup(keywrds[i].knam);
566		p->defn = xstrdup(null);
567		p->hv = h;
568		p->type = keywrds[i].ktyp & TYPEMASK;
569		if ((keywrds[i].ktyp & NOARGS) == 0)
570			p->type |= NEEDARGS;
571	}
572}
573
574/* Look up a builtin type, even if overridden by the user */
575int
576builtin_type(const char *key)
577{
578	int i;
579
580	for (i = 0; i != MAXKEYS; i++)
581		if (STREQ(keywrds[i].knam, key))
582			return keywrds[i].ktyp;
583	return -1;
584}
585
586const char *
587builtin_realname(int n)
588{
589	int i;
590
591	for (i = 0; i != MAXKEYS; i++)
592		if (((keywrds[i].ktyp ^ n) & TYPEMASK) == 0)
593			return keywrds[i].knam;
594	return NULL;
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()
624{
625	STACKMAX *= 2;
626	mstack = realloc(mstack, sizeof(stae) * STACKMAX);
627	sstack = realloc(sstack, STACKMAX);
628	if (mstack == NULL || sstack == NULL)
629		errx(1, "Evaluation stack overflow (%lu)",
630		    (unsigned long)STACKMAX);
631}
632
633/* Emit preprocessor #line directive if -s option used. */
634void
635emitline(void)
636{
637
638	if (synccpp)
639		fprintf(active, "#line %d \"%s\"\n", inlineno[ilevel],
640			inname[ilevel]);
641}
642