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