Home | History | Annotate | Line # | Download | only in m4
main.c revision 1.42
      1  1.38  christos /*	$OpenBSD: main.c,v 1.77 2009/10/14 17:19:47 sthen Exp $	*/
      2  1.42  christos /*	$NetBSD: main.c,v 1.42 2012/04/25 18:23:58 christos Exp $	*/
      3   1.9       tls 
      4   1.8     glass /*-
      5   1.8     glass  * Copyright (c) 1989, 1993
      6   1.8     glass  *	The Regents of the University of California.  All rights reserved.
      7   1.8     glass  *
      8   1.8     glass  * This code is derived from software contributed to Berkeley by
      9   1.8     glass  * Ozan Yigit at York University.
     10   1.8     glass  *
     11   1.8     glass  * Redistribution and use in source and binary forms, with or without
     12   1.8     glass  * modification, are permitted provided that the following conditions
     13   1.8     glass  * are met:
     14   1.8     glass  * 1. Redistributions of source code must retain the above copyright
     15   1.8     glass  *    notice, this list of conditions and the following disclaimer.
     16   1.8     glass  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.8     glass  *    notice, this list of conditions and the following disclaimer in the
     18   1.8     glass  *    documentation and/or other materials provided with the distribution.
     19  1.34       agc  * 3. Neither the name of the University nor the names of its contributors
     20   1.8     glass  *    may be used to endorse or promote products derived from this software
     21   1.8     glass  *    without specific prior written permission.
     22   1.8     glass  *
     23   1.8     glass  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.8     glass  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.8     glass  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.8     glass  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.8     glass  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.8     glass  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.8     glass  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.8     glass  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.8     glass  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.8     glass  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.8     glass  * SUCH DAMAGE.
     34   1.8     glass  */
     35   1.1       cgd 
     36   1.1       cgd /*
     37   1.8     glass  * main.c
     38   1.8     glass  * Facility: m4 macro processor
     39   1.8     glass  * by: oz
     40   1.1       cgd  */
     41  1.38  christos #if HAVE_NBTOOL_CONFIG_H
     42  1.38  christos #include "nbtool_config.h"
     43  1.38  christos #endif
     44  1.38  christos #include <sys/cdefs.h>
     45  1.42  christos __RCSID("$NetBSD: main.c,v 1.42 2012/04/25 18:23:58 christos Exp $");
     46  1.27        tv #include <assert.h>
     47  1.38  christos #include <signal.h>
     48  1.38  christos #include <err.h>
     49  1.38  christos #include <errno.h>
     50  1.38  christos #include <unistd.h>
     51  1.38  christos #include <stdio.h>
     52  1.13     lukem #include <ctype.h>
     53  1.38  christos #include <string.h>
     54  1.27        tv #include <stddef.h>
     55  1.38  christos #include <stdint.h>
     56  1.16    kleink #include <stdlib.h>
     57  1.38  christos #include <ohash.h>
     58   1.8     glass #include "mdef.h"
     59   1.8     glass #include "stdd.h"
     60   1.8     glass #include "extern.h"
     61   1.8     glass #include "pathnames.h"
     62   1.8     glass 
     63   1.8     glass ndptr hashtab[HASHSIZE];	/* hash table for macros etc.  */
     64  1.27        tv stae *mstack;		 	/* stack of m4 machine         */
     65  1.27        tv char *sstack;		 	/* shadow stack, for string space extension */
     66  1.27        tv static size_t STACKMAX;		/* current maximum size of stack */
     67   1.1       cgd int sp; 			/* current m4  stack pointer   */
     68   1.1       cgd int fp; 			/* m4 call frame pointer       */
     69  1.27        tv struct input_file infile[MAXINP];/* input file stack (0=stdin)  */
     70  1.27        tv FILE **outfile;			/* diversion array(0=bitbucket)*/
     71  1.27        tv int maxout;
     72   1.1       cgd FILE *active;			/* active output file pointer  */
     73   1.1       cgd int ilevel = 0; 		/* input file stack pointer    */
     74   1.1       cgd int oindex = 0; 		/* diversion index..	       */
     75  1.38  christos const char *null = "";          /* as it says.. just a null..  */
     76  1.38  christos char **m4wraps = NULL;		/* m4wraps array.     	       */
     77  1.38  christos int maxwraps = 0;		/* size of m4wraps array       */
     78  1.38  christos int wrapindex = 0;		/* current offset in m4wraps   */
     79  1.11        pk char lquote[MAXCCHARS+1] = {LQUOTE};	/* left quote character  (`)   */
     80  1.11        pk char rquote[MAXCCHARS+1] = {RQUOTE};	/* right quote character (')   */
     81  1.11        pk char scommt[MAXCCHARS+1] = {SCOMMT};	/* start character for comment */
     82  1.11        pk char ecommt[MAXCCHARS+1] = {ECOMMT};	/* end character for comment   */
     83  1.38  christos int  synch_lines = 0;		/* line synchronisation for C preprocessor */
     84  1.38  christos int  prefix_builtins = 0;	/* -P option to prefix builtin keywords */
     85  1.38  christos 
     86  1.38  christos struct keyblk {
     87  1.38  christos         const char *knam;	/* keyword name */
     88  1.38  christos         int	ktyp;           /* keyword type */
     89  1.38  christos };
     90   1.2     glass 
     91   1.8     glass struct keyblk keywrds[] = {	/* m4 keywords to be installed */
     92  1.13     lukem 	{ "include",      INCLTYPE },
     93  1.13     lukem 	{ "sinclude",     SINCTYPE },
     94  1.13     lukem 	{ "define",       DEFITYPE },
     95  1.13     lukem 	{ "defn",         DEFNTYPE },
     96  1.27        tv 	{ "divert",       DIVRTYPE | NOARGS },
     97  1.13     lukem 	{ "expr",         EXPRTYPE },
     98  1.13     lukem 	{ "eval",         EXPRTYPE },
     99  1.13     lukem 	{ "substr",       SUBSTYPE },
    100  1.13     lukem 	{ "ifelse",       IFELTYPE },
    101  1.13     lukem 	{ "ifdef",        IFDFTYPE },
    102  1.13     lukem 	{ "len",          LENGTYPE },
    103  1.13     lukem 	{ "incr",         INCRTYPE },
    104  1.13     lukem 	{ "decr",         DECRTYPE },
    105  1.27        tv 	{ "dnl",          DNLNTYPE | NOARGS },
    106  1.27        tv 	{ "changequote",  CHNQTYPE | NOARGS },
    107  1.27        tv 	{ "changecom",    CHNCTYPE | NOARGS },
    108  1.13     lukem 	{ "index",        INDXTYPE },
    109   1.8     glass #ifdef EXTENDED
    110  1.13     lukem 	{ "paste",        PASTTYPE },
    111  1.13     lukem 	{ "spaste",       SPASTYPE },
    112  1.27        tv     	/* Newer extensions, needed to handle gnu-m4 scripts */
    113  1.27        tv 	{ "indir",        INDIRTYPE},
    114  1.27        tv 	{ "builtin",      BUILTINTYPE},
    115  1.27        tv 	{ "patsubst",	  PATSTYPE},
    116  1.27        tv 	{ "regexp",	  REGEXPTYPE},
    117  1.27        tv 	{ "esyscmd",	  ESYSCMDTYPE},
    118  1.27        tv 	{ "__file__",	  FILENAMETYPE | NOARGS},
    119  1.27        tv 	{ "__line__",	  LINETYPE | NOARGS},
    120   1.8     glass #endif
    121  1.13     lukem 	{ "popdef",       POPDTYPE },
    122  1.13     lukem 	{ "pushdef",      PUSDTYPE },
    123  1.27        tv 	{ "dumpdef",      DUMPTYPE | NOARGS },
    124  1.27        tv 	{ "shift",        SHIFTYPE | NOARGS },
    125  1.13     lukem 	{ "translit",     TRNLTYPE },
    126  1.13     lukem 	{ "undefine",     UNDFTYPE },
    127  1.27        tv 	{ "undivert",     UNDVTYPE | NOARGS },
    128  1.27        tv 	{ "divnum",       DIVNTYPE | NOARGS },
    129  1.13     lukem 	{ "maketemp",     MKTMTYPE },
    130  1.27        tv 	{ "errprint",     ERRPTYPE | NOARGS },
    131  1.27        tv 	{ "m4wrap",       M4WRTYPE | NOARGS },
    132  1.27        tv 	{ "m4exit",       EXITTYPE | NOARGS },
    133  1.13     lukem 	{ "syscmd",       SYSCTYPE },
    134  1.27        tv 	{ "sysval",       SYSVTYPE | NOARGS },
    135  1.27        tv 	{ "traceon",	  TRACEONTYPE | NOARGS },
    136  1.27        tv 	{ "traceoff",	  TRACEOFFTYPE | NOARGS },
    137   1.2     glass 
    138  1.27        tv #if defined(unix) || defined(__unix__)
    139  1.27        tv 	{ "unix",         SELFTYPE | NOARGS },
    140   1.2     glass #else
    141   1.8     glass #ifdef vms
    142  1.27        tv 	{ "vms",          SELFTYPE | NOARGS },
    143   1.1       cgd #endif
    144   1.1       cgd #endif
    145   1.8     glass };
    146   1.8     glass 
    147   1.8     glass #define MAXKEYS	(sizeof(keywrds)/sizeof(struct keyblk))
    148   1.1       cgd 
    149  1.27        tv extern int optind;
    150  1.27        tv extern char *optarg;
    151  1.27        tv 
    152  1.27        tv #define MAXRECORD 50
    153  1.27        tv static struct position {
    154  1.27        tv 	char *name;
    155  1.27        tv 	unsigned long line;
    156  1.27        tv } quotes[MAXRECORD], paren[MAXRECORD];
    157  1.27        tv 
    158  1.38  christos static void record(struct position *, int);
    159  1.38  christos static void dump_stack(struct position *, int);
    160  1.27        tv 
    161  1.38  christos static void macro(void);
    162  1.38  christos static void initkwds(void);
    163  1.38  christos static ndptr inspect(int, char *);
    164  1.38  christos static int do_look_ahead(int, const char *);
    165  1.38  christos static void reallyoutputstr(const char *);
    166  1.38  christos static void reallyputchar(int);
    167  1.27        tv 
    168  1.38  christos static void enlarge_stack(void);
    169  1.27        tv 
    170  1.41     joerg __dead static void
    171  1.41     joerg usage(void)
    172  1.41     joerg {
    173  1.41     joerg 	fprintf(stderr, "usage: %s [-gPs] [-Dname[=value]] [-d flags] "
    174  1.41     joerg 			"[-I dirname] [-o filename]\n"
    175  1.41     joerg 			"\t[-t macro] [-Uname] [file ...]\n", getprogname());
    176  1.41     joerg 	exit(1);
    177  1.41     joerg }
    178  1.41     joerg 
    179  1.41     joerg __dead static void
    180  1.41     joerg onintr(int signo)
    181  1.41     joerg {
    182  1.41     joerg 	char intrmessage[] = "m4: interrupted.\n";
    183  1.41     joerg 	write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
    184  1.41     joerg 	_exit(1);
    185  1.41     joerg }
    186   1.1       cgd 
    187   1.8     glass int
    188  1.38  christos main(int argc, char *argv[])
    189   1.8     glass {
    190  1.13     lukem 	int c;
    191  1.13     lukem 	int n;
    192   1.8     glass 	char *p;
    193   1.8     glass 
    194  1.39     joerg 	setprogname(argv[0]);
    195  1.39     joerg 
    196   1.8     glass 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    197   1.8     glass 		signal(SIGINT, onintr);
    198   1.1       cgd 
    199  1.38  christos 	init_macros();
    200  1.27        tv 	initspaces();
    201  1.27        tv 	STACKMAX = INITSTACKMAX;
    202  1.27        tv 
    203  1.38  christos 	mstack = (stae *)xalloc(sizeof(stae) * STACKMAX, NULL);
    204  1.38  christos 	sstack = (char *)xalloc(STACKMAX, NULL);
    205   1.1       cgd 
    206  1.27        tv 	maxout = 0;
    207  1.27        tv 	outfile = NULL;
    208  1.27        tv 	resizedivs(MAXOUT);
    209   1.1       cgd 
    210  1.38  christos 	while ((c = getopt(argc, argv, "gst:d:D:U:o:I:P")) != -1)
    211  1.38  christos 		switch(c) {
    212  1.38  christos 
    213   1.8     glass 		case 'D':               /* define something..*/
    214   1.8     glass 			for (p = optarg; *p; p++)
    215   1.8     glass 				if (*p == '=')
    216   1.8     glass 					break;
    217   1.8     glass 			if (*p)
    218   1.8     glass 				*p++ = EOS;
    219   1.8     glass 			dodefine(optarg, p);
    220   1.8     glass 			break;
    221  1.27        tv 		case 'I':
    222  1.27        tv 			addtoincludepath(optarg);
    223  1.27        tv 			break;
    224  1.27        tv 		case 'P':
    225  1.38  christos 			prefix_builtins = 1;
    226  1.27        tv 			break;
    227   1.8     glass 		case 'U':               /* undefine...       */
    228  1.38  christos 			macro_popdef(optarg);
    229  1.38  christos 			break;
    230  1.38  christos 		case 'g':
    231  1.38  christos 			mimic_gnu = 1;
    232   1.8     glass 			break;
    233  1.27        tv 		case 'd':
    234  1.27        tv 			set_trace_flags(optarg);
    235  1.27        tv 			break;
    236  1.38  christos 		case 's':
    237  1.38  christos 			synch_lines = 1;
    238  1.38  christos 			break;
    239  1.38  christos 		case 't':
    240  1.38  christos 			mark_traced(optarg, 1);
    241  1.27        tv 			break;
    242  1.27        tv 		case 'o':
    243  1.27        tv 			trace_file(optarg);
    244  1.27        tv                         break;
    245   1.8     glass 		case '?':
    246  1.38  christos 			usage();
    247   1.2     glass 		}
    248   1.1       cgd 
    249   1.8     glass         argc -= optind;
    250   1.8     glass         argv += optind;
    251   1.2     glass 
    252  1.38  christos 	initkwds();
    253  1.38  christos 	if (mimic_gnu)
    254  1.38  christos 		setup_builtin("format", FORMATTYPE);
    255  1.38  christos 
    256   1.8     glass 	active = stdout;		/* default active output     */
    257   1.8     glass 	bbase[0] = bufbase;
    258   1.8     glass         if (!argc) {
    259   1.8     glass  		sp = -1;		/* stack pointer initialized */
    260   1.8     glass 		fp = 0; 		/* frame pointer initialized */
    261  1.27        tv 		set_input(infile+0, stdin, "stdin");
    262  1.27        tv 					/* default input (naturally) */
    263   1.8     glass 		macro();
    264   1.8     glass 	} else
    265   1.8     glass 		for (; argc--; ++argv) {
    266   1.8     glass 			p = *argv;
    267  1.27        tv 			if (p[0] == '-' && p[1] == EOS)
    268  1.27        tv 				set_input(infile, stdin, "stdin");
    269  1.27        tv 			else if (fopen_trypath(infile, p) == NULL)
    270  1.13     lukem 				err(1, "%s", p);
    271   1.8     glass 			sp = -1;
    272   1.8     glass 			fp = 0;
    273   1.8     glass 			macro();
    274  1.27        tv 		    	release_input(infile);
    275   1.8     glass 		}
    276   1.2     glass 
    277  1.38  christos 	if (wrapindex) {
    278  1.38  christos 		int i;
    279  1.38  christos 
    280   1.8     glass 		ilevel = 0;		/* in case m4wrap includes.. */
    281   1.8     glass 		bufbase = bp = buf;	/* use the entire buffer   */
    282  1.38  christos 		if (mimic_gnu) {
    283  1.38  christos 			while (wrapindex != 0) {
    284  1.38  christos 				for (i = 0; i < wrapindex; i++)
    285  1.38  christos 					pbstr(m4wraps[i]);
    286  1.38  christos 				wrapindex =0;
    287  1.38  christos 				macro();
    288  1.38  christos 			}
    289  1.38  christos 		} else {
    290  1.38  christos 			for (i = 0; i < wrapindex; i++) {
    291  1.38  christos 				pbstr(m4wraps[i]);
    292  1.38  christos 				macro();
    293  1.38  christos 		    	}
    294  1.38  christos 		}
    295   1.8     glass 	}
    296   1.2     glass 
    297   1.8     glass 	if (active != stdout)
    298   1.8     glass 		active = stdout;	/* reset output just in case */
    299  1.27        tv 	for (n = 1; n < maxout; n++)	/* default wrap-up: undivert */
    300   1.8     glass 		if (outfile[n] != NULL)
    301   1.8     glass 			getdiv(n);
    302  1.27        tv 					/* remove bitbucket if used  */
    303  1.27        tv 	if (outfile[0] != NULL) {
    304   1.8     glass 		(void) fclose(outfile[0]);
    305  1.27        tv 	}
    306   1.8     glass 
    307   1.8     glass 	return 0;
    308   1.8     glass }
    309   1.2     glass 
    310   1.8     glass /*
    311  1.27        tv  * Look ahead for `token'.
    312  1.11        pk  * (on input `t == token[0]')
    313  1.11        pk  * Used for comment and quoting delimiters.
    314  1.11        pk  * Returns 1 if `token' present; copied to output.
    315  1.11        pk  *         0 if `token' not found; all characters pushed back
    316  1.11        pk  */
    317  1.27        tv static int
    318  1.38  christos do_look_ahead(int t, const char *token)
    319  1.11        pk {
    320  1.11        pk 	int i;
    321  1.11        pk 
    322  1.27        tv 	assert((unsigned char)t == (unsigned char)token[0]);
    323  1.11        pk 
    324  1.11        pk 	for (i = 1; *++token; i++) {
    325  1.11        pk 		t = gpbc();
    326  1.27        tv 		if (t == EOF || (unsigned char)t != (unsigned char)*token) {
    327  1.38  christos 			pushback(t);
    328  1.11        pk 			while (--i)
    329  1.38  christos 				pushback(*--token);
    330  1.11        pk 			return 0;
    331  1.11        pk 		}
    332  1.11        pk 	}
    333  1.11        pk 	return 1;
    334  1.11        pk }
    335  1.11        pk 
    336  1.27        tv #define LOOK_AHEAD(t, token) (t != EOF && 		\
    337  1.27        tv     (unsigned char)(t)==(unsigned char)(token)[0] && 	\
    338  1.27        tv     do_look_ahead(t,token))
    339  1.11        pk 
    340  1.11        pk /*
    341   1.8     glass  * macro - the work horse..
    342   1.8     glass  */
    343  1.27        tv static void
    344  1.38  christos macro(void)
    345  1.13     lukem {
    346  1.27        tv 	char token[MAXTOK+1];
    347  1.13     lukem 	int t, l;
    348  1.13     lukem 	ndptr p;
    349  1.13     lukem 	int  nlpar;
    350   1.1       cgd 
    351   1.8     glass 	cycle {
    352  1.11        pk 		t = gpbc();
    353  1.27        tv 
    354  1.38  christos 		if (LOOK_AHEAD(t,lquote)) {	/* strip quotes */
    355  1.27        tv 			nlpar = 0;
    356  1.27        tv 			record(quotes, nlpar++);
    357  1.27        tv 			/*
    358  1.27        tv 			 * Opening quote: scan forward until matching
    359  1.27        tv 			 * closing quote has been found.
    360  1.27        tv 			 */
    361   1.8     glass 			do {
    362  1.12       cgd 
    363  1.11        pk 				l = gpbc();
    364  1.12       cgd 				if (LOOK_AHEAD(l,rquote)) {
    365  1.27        tv 					if (--nlpar > 0)
    366  1.27        tv 						outputstr(rquote);
    367  1.12       cgd 				} else if (LOOK_AHEAD(l,lquote)) {
    368  1.27        tv 					record(quotes, nlpar++);
    369  1.27        tv 					outputstr(lquote);
    370  1.27        tv 				} else if (l == EOF) {
    371  1.27        tv 					if (nlpar == 1)
    372  1.27        tv 						warnx("unclosed quote:");
    373   1.8     glass 					else
    374  1.27        tv 						warnx("%d unclosed quotes:", nlpar);
    375  1.27        tv 					dump_stack(quotes, nlpar);
    376  1.27        tv 					exit(1);
    377  1.27        tv 				} else {
    378  1.27        tv 					if (nlpar > 0) {
    379  1.27        tv 						if (sp < 0)
    380  1.38  christos 							reallyputchar(l);
    381  1.27        tv 						else
    382  1.27        tv 							CHRSAVE(l);
    383  1.27        tv 					}
    384   1.2     glass 				}
    385   1.8     glass 			}
    386   1.8     glass 			while (nlpar != 0);
    387  1.38  christos 		} else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
    388  1.38  christos 			reallyoutputstr(scommt);
    389  1.11        pk 
    390  1.11        pk 			for(;;) {
    391  1.11        pk 				t = gpbc();
    392  1.11        pk 				if (LOOK_AHEAD(t, ecommt)) {
    393  1.38  christos 					reallyoutputstr(ecommt);
    394  1.11        pk 					break;
    395  1.11        pk 				}
    396  1.11        pk 				if (t == EOF)
    397  1.11        pk 					break;
    398  1.38  christos 				reallyputchar(t);
    399   1.1       cgd 			}
    400  1.38  christos 		} else if (t == '_' || isalpha(t)) {
    401  1.38  christos 			p = inspect(t, token);
    402  1.38  christos 			if (p != NULL)
    403  1.38  christos 				pushback(l = gpbc());
    404  1.38  christos 			if (p == NULL || (l != LPAREN &&
    405  1.38  christos 			    (macro_getdef(p)->type & NEEDARGS) != 0))
    406  1.38  christos 				outputstr(token);
    407  1.38  christos 			else {
    408  1.38  christos 		/*
    409  1.38  christos 		 * real thing.. First build a call frame:
    410  1.38  christos 		 */
    411  1.38  christos 				pushf(fp);	/* previous call frm */
    412  1.38  christos 				pushf(macro_getdef(p)->type); /* type of the call  */
    413  1.38  christos 				pushf(is_traced(p));
    414  1.38  christos 				pushf(0);	/* parenthesis level */
    415  1.38  christos 				fp = sp;	/* new frame pointer */
    416  1.38  christos 		/*
    417  1.38  christos 		 * now push the string arguments:
    418  1.38  christos 		 */
    419  1.38  christos 				pushs1(macro_getdef(p)->defn);	/* defn string */
    420  1.38  christos 				pushs1((char *)macro_name(p));	/* macro name  */
    421  1.38  christos 				pushs(ep);	      	/* start next..*/
    422  1.38  christos 
    423  1.38  christos 				if (l != LPAREN && PARLEV == 0)  {
    424  1.38  christos 				    /* no bracks  */
    425  1.38  christos 					chrsave(EOS);
    426  1.38  christos 
    427  1.38  christos 					if ((size_t)sp == STACKMAX)
    428  1.38  christos 						errx(1, "internal stack overflow");
    429  1.38  christos 					eval((const char **) mstack+fp+1, 2,
    430  1.38  christos 					    CALTYP, TRACESTATUS);
    431  1.11        pk 
    432  1.38  christos 					ep = PREVEP;	/* flush strspace */
    433  1.38  christos 					sp = PREVSP;	/* previous sp..  */
    434  1.38  christos 					fp = PREVFP;	/* rewind stack...*/
    435  1.38  christos 				}
    436  1.38  christos 			}
    437  1.38  christos 		} else if (t == EOF) {
    438  1.38  christos 			if (sp > -1 && ilevel <= 0) {
    439  1.38  christos 				warnx( "unexpected end of input, unclosed parenthesis:");
    440  1.38  christos 				dump_stack(paren, PARLEV);
    441  1.38  christos 				exit(1);
    442  1.38  christos 			}
    443  1.38  christos 			if (ilevel <= 0)
    444  1.38  christos 				break;			/* all done thanks.. */
    445  1.38  christos 			release_input(infile+ilevel--);
    446  1.38  christos 			emit_synchline();
    447  1.38  christos 			bufbase = bbase[ilevel];
    448  1.38  christos 			continue;
    449  1.38  christos 		} else if (sp < 0) {		/* not in a macro at all */
    450  1.38  christos 			reallyputchar(t);	/* output directly..	 */
    451   1.8     glass 		}
    452   1.1       cgd 
    453   1.8     glass 		else switch(t) {
    454   1.2     glass 
    455   1.8     glass 		case LPAREN:
    456   1.8     glass 			if (PARLEV > 0)
    457   1.8     glass 				chrsave(t);
    458  1.38  christos 			while (isspace(l = gpbc())) /* skip blank, tab, nl.. */
    459  1.38  christos 				if (PARLEV > 0)
    460  1.38  christos 					chrsave(l);
    461  1.38  christos 			pushback(l);
    462  1.27        tv 			record(paren, PARLEV++);
    463   1.8     glass 			break;
    464   1.1       cgd 
    465   1.8     glass 		case RPAREN:
    466   1.8     glass 			if (--PARLEV > 0)
    467   1.8     glass 				chrsave(t);
    468   1.8     glass 			else {			/* end of argument list */
    469   1.8     glass 				chrsave(EOS);
    470   1.8     glass 
    471  1.38  christos 				if ((size_t)sp == STACKMAX)
    472  1.13     lukem 					errx(1, "internal stack overflow");
    473   1.8     glass 
    474  1.27        tv 				eval((const char **) mstack+fp+1, sp-fp,
    475  1.38  christos 				    CALTYP, TRACESTATUS);
    476   1.8     glass 
    477   1.8     glass 				ep = PREVEP;	/* flush strspace */
    478   1.8     glass 				sp = PREVSP;	/* previous sp..  */
    479   1.8     glass 				fp = PREVFP;	/* rewind stack...*/
    480   1.8     glass 			}
    481   1.8     glass 			break;
    482   1.1       cgd 
    483   1.8     glass 		case COMMA:
    484   1.8     glass 			if (PARLEV == 1) {
    485   1.8     glass 				chrsave(EOS);		/* new argument   */
    486   1.8     glass 				while (isspace(l = gpbc()))
    487   1.8     glass 					;
    488  1.38  christos 				pushback(l);
    489   1.8     glass 				pushs(ep);
    490   1.8     glass 			} else
    491   1.8     glass 				chrsave(t);
    492   1.8     glass 			break;
    493   1.2     glass 
    494   1.2     glass 		default:
    495  1.27        tv 			if (LOOK_AHEAD(t, scommt)) {
    496  1.38  christos 				char *q;
    497  1.42  christos 				for (q = scommt; *q; q++)
    498  1.38  christos 					chrsave(*q);
    499  1.27        tv 				for(;;) {
    500  1.27        tv 					t = gpbc();
    501  1.27        tv 					if (LOOK_AHEAD(t, ecommt)) {
    502  1.38  christos 						for (q = ecommt; *q; q++)
    503  1.38  christos 							chrsave(*q);
    504  1.27        tv 						break;
    505  1.27        tv 					}
    506  1.27        tv 					if (t == EOF)
    507  1.27        tv 					    break;
    508  1.27        tv 					CHRSAVE(t);
    509  1.27        tv 				}
    510  1.27        tv 			} else
    511  1.27        tv 				CHRSAVE(t);		/* stack the char */
    512   1.8     glass 			break;
    513   1.8     glass 		}
    514   1.2     glass 	}
    515   1.8     glass }
    516   1.2     glass 
    517  1.27        tv /*
    518  1.27        tv  * output string directly, without pushing it for reparses.
    519  1.27        tv  */
    520  1.27        tv void
    521  1.38  christos outputstr(const char *s)
    522  1.27        tv {
    523  1.27        tv 	if (sp < 0)
    524  1.38  christos 		reallyoutputstr(s);
    525  1.27        tv 	else
    526  1.27        tv 		while (*s)
    527  1.27        tv 			CHRSAVE(*s++);
    528  1.27        tv }
    529  1.27        tv 
    530  1.38  christos void
    531  1.38  christos reallyoutputstr(const char *s)
    532  1.38  christos {
    533  1.38  christos 	if (synch_lines) {
    534  1.38  christos 		while (*s) {
    535  1.38  christos 			fputc(*s, active);
    536  1.38  christos 			if (*s++ == '\n') {
    537  1.38  christos 				infile[ilevel].synch_lineno++;
    538  1.38  christos 				if (infile[ilevel].synch_lineno !=
    539  1.38  christos 				    infile[ilevel].lineno)
    540  1.38  christos 					do_emit_synchline();
    541  1.38  christos 			}
    542  1.38  christos 		}
    543  1.38  christos 	} else
    544  1.38  christos 		fputs(s, active);
    545  1.38  christos }
    546  1.38  christos 
    547  1.38  christos void
    548  1.38  christos reallyputchar(int c)
    549  1.38  christos {
    550  1.38  christos 	putc(c, active);
    551  1.38  christos 	if (synch_lines && c == '\n') {
    552  1.38  christos 		infile[ilevel].synch_lineno++;
    553  1.38  christos 		if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
    554  1.38  christos 			do_emit_synchline();
    555  1.38  christos 	}
    556  1.38  christos }
    557  1.38  christos 
    558   1.8     glass /*
    559   1.8     glass  * build an input token..
    560  1.38  christos  * consider only those starting with _ or A-Za-z.
    561   1.8     glass  */
    562  1.27        tv static ndptr
    563  1.38  christos inspect(int c, char *tp)
    564   1.8     glass {
    565  1.13     lukem 	char *name = tp;
    566  1.13     lukem 	char *etp = tp+MAXTOK;
    567  1.13     lukem 	ndptr p;
    568  1.27        tv 
    569  1.38  christos 	*tp++ = c;
    570   1.2     glass 
    571  1.27        tv 	while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
    572  1.38  christos 		*tp++ = c;
    573  1.27        tv 	if (c != EOF)
    574  1.38  christos 		PUSHBACK(c);
    575   1.8     glass 	*tp = EOS;
    576  1.27        tv 	/* token is too long, it won't match anything, but it can still
    577  1.27        tv 	 * be output. */
    578  1.27        tv 	if (tp == ep) {
    579  1.27        tv 		outputstr(name);
    580  1.27        tv 		while (isalnum(c = gpbc()) || c == '_') {
    581  1.27        tv 			if (sp < 0)
    582  1.38  christos 				reallyputchar(c);
    583  1.27        tv 			else
    584  1.27        tv 				CHRSAVE(c);
    585  1.27        tv 		}
    586  1.27        tv 		*name = EOS;
    587  1.38  christos 		return NULL;
    588  1.27        tv 	}
    589   1.1       cgd 
    590  1.40  christos 	p = ohash_find(&macros, ohash_qlookupi(&macros, name, (void *)&tp));
    591  1.38  christos 	if (p == NULL)
    592  1.38  christos 		return NULL;
    593  1.38  christos 	if (macro_getdef(p) == NULL)
    594  1.38  christos 		return NULL;
    595   1.8     glass 	return p;
    596   1.8     glass }
    597   1.7       cgd 
    598   1.8     glass /*
    599  1.38  christos  * initkwds - initialise m4 keywords as fast as possible.
    600   1.8     glass  * This very similar to install, but without certain overheads,
    601  1.38  christos  * such as calling lookup. Malloc is not used for storing the
    602  1.13     lukem  * keyword strings, since we simply use the static pointers
    603   1.8     glass  * within keywrds block.
    604   1.8     glass  */
    605  1.27        tv static void
    606  1.38  christos initkwds(void)
    607  1.13     lukem {
    608  1.38  christos 	unsigned int type;
    609  1.32        tv 	size_t i;
    610   1.1       cgd 
    611   1.8     glass 	for (i = 0; i < MAXKEYS; i++) {
    612  1.38  christos 		type = keywrds[i].ktyp & TYPEMASK;
    613  1.27        tv 		if ((keywrds[i].ktyp & NOARGS) == 0)
    614  1.38  christos 			type |= NEEDARGS;
    615  1.38  christos 		setup_builtin(keywrds[i].knam, type);
    616   1.1       cgd 	}
    617  1.27        tv }
    618  1.27        tv 
    619  1.27        tv static void
    620  1.38  christos record(struct position *t, int lev)
    621  1.27        tv {
    622  1.27        tv 	if (lev < MAXRECORD) {
    623  1.27        tv 		t[lev].name = CURRENT_NAME;
    624  1.27        tv 		t[lev].line = CURRENT_LINE;
    625  1.27        tv 	}
    626  1.27        tv }
    627  1.27        tv 
    628  1.27        tv static void
    629  1.38  christos dump_stack(struct position *t, int lev)
    630  1.27        tv {
    631  1.27        tv 	int i;
    632  1.27        tv 
    633  1.27        tv 	for (i = 0; i < lev; i++) {
    634  1.27        tv 		if (i == MAXRECORD) {
    635  1.27        tv 			fprintf(stderr, "   ...\n");
    636  1.27        tv 			break;
    637  1.27        tv 		}
    638  1.27        tv 		fprintf(stderr, "   %s at line %lu\n",
    639  1.27        tv 			t[i].name, t[i].line);
    640  1.27        tv 	}
    641  1.27        tv }
    642  1.27        tv 
    643  1.27        tv 
    644  1.27        tv static void
    645  1.38  christos enlarge_stack(void)
    646  1.27        tv {
    647  1.38  christos 	STACKMAX += STACKMAX/2;
    648  1.38  christos 	mstack = xrealloc(mstack, sizeof(stae) * STACKMAX,
    649  1.38  christos 	    "Evaluation stack overflow (%lu)",
    650  1.38  christos 	    (unsigned long)STACKMAX);
    651  1.38  christos 	sstack = xrealloc(sstack, STACKMAX,
    652  1.38  christos 	    "Evaluation stack overflow (%lu)",
    653  1.38  christos 	    (unsigned long)STACKMAX);
    654   1.8     glass }
    655