2001-11-14 17:57:04 +03:00
|
|
|
/* $NetBSD: main.c,v 1.28 2001/11/14 14:57:04 tv Exp $ */
|
2001-11-14 09:16:07 +03:00
|
|
|
/* $OpenBSD: main.c,v 1.51 2001/10/06 10:52:25 espie Exp $ */
|
1995-09-28 08:37:28 +03:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Ozan Yigit at York University.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 08:39:51 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-06-19 07:58:22 +04:00
|
|
|
#ifndef lint
|
1997-10-19 08:39:51 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1994-06-19 07:58:22 +04:00
|
|
|
#endif /* not lint */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
#ifndef lint
|
1995-09-28 08:37:28 +03:00
|
|
|
#if 0
|
1994-06-19 07:58:22 +04:00
|
|
|
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
|
1995-09-28 08:37:28 +03:00
|
|
|
#else
|
2001-11-14 17:57:04 +03:00
|
|
|
__RCSID("$NetBSD: main.c,v 1.28 2001/11/14 14:57:04 tv Exp $");
|
1995-09-28 08:37:28 +03:00
|
|
|
#endif
|
1994-06-19 07:58:22 +04:00
|
|
|
#endif /* not lint */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
1994-06-19 07:58:22 +04:00
|
|
|
* main.c
|
|
|
|
* Facility: m4 macro processor
|
|
|
|
* by: oz
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
#include <sys/types.h>
|
2001-11-14 09:16:07 +03:00
|
|
|
#include <assert.h>
|
1997-10-19 08:39:51 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
1994-06-19 07:58:22 +04:00
|
|
|
#include <errno.h>
|
1997-10-19 08:39:51 +04:00
|
|
|
#include <signal.h>
|
2001-11-14 09:16:07 +03:00
|
|
|
#include <stddef.h>
|
1994-06-19 07:58:22 +04:00
|
|
|
#include <stdio.h>
|
1998-06-01 17:54:49 +04:00
|
|
|
#include <stdlib.h>
|
1994-06-19 07:58:22 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include "mdef.h"
|
|
|
|
#include "stdd.h"
|
|
|
|
#include "extern.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
|
|
|
ndptr hashtab[HASHSIZE]; /* hash table for macros etc. */
|
2001-11-14 09:16:07 +03:00
|
|
|
stae *mstack; /* stack of m4 machine */
|
|
|
|
char *sstack; /* shadow stack, for string space extension */
|
|
|
|
static size_t STACKMAX; /* current maximum size of stack */
|
1993-03-21 12:45:37 +03:00
|
|
|
int sp; /* current m4 stack pointer */
|
|
|
|
int fp; /* m4 call frame pointer */
|
2001-11-14 09:16:07 +03:00
|
|
|
struct input_file infile[MAXINP];/* input file stack (0=stdin) */
|
|
|
|
FILE **outfile; /* diversion array(0=bitbucket)*/
|
|
|
|
int maxout;
|
1993-03-21 12:45:37 +03:00
|
|
|
FILE *active; /* active output file pointer */
|
|
|
|
int ilevel = 0; /* input file stack pointer */
|
|
|
|
int oindex = 0; /* diversion index.. */
|
|
|
|
char *null = ""; /* as it says.. just a null.. */
|
|
|
|
char *m4wraps = ""; /* m4wrap string default.. */
|
1999-06-23 23:09:35 +04:00
|
|
|
int m4prefix = 0; /* prefix keywords with m4_ */
|
1996-01-14 02:25:23 +03:00
|
|
|
char lquote[MAXCCHARS+1] = {LQUOTE}; /* left quote character (`) */
|
|
|
|
char rquote[MAXCCHARS+1] = {RQUOTE}; /* right quote character (') */
|
|
|
|
char scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */
|
|
|
|
char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
struct keyblk keywrds[] = { /* m4 keywords to be installed */
|
1997-10-19 08:39:51 +04:00
|
|
|
{ "include", INCLTYPE },
|
|
|
|
{ "sinclude", SINCTYPE },
|
|
|
|
{ "define", DEFITYPE },
|
|
|
|
{ "defn", DEFNTYPE },
|
2001-11-14 09:16:07 +03:00
|
|
|
{ "divert", DIVRTYPE | NOARGS },
|
1997-10-19 08:39:51 +04:00
|
|
|
{ "expr", EXPRTYPE },
|
|
|
|
{ "eval", EXPRTYPE },
|
|
|
|
{ "substr", SUBSTYPE },
|
|
|
|
{ "ifelse", IFELTYPE },
|
|
|
|
{ "ifdef", IFDFTYPE },
|
|
|
|
{ "len", LENGTYPE },
|
|
|
|
{ "incr", INCRTYPE },
|
|
|
|
{ "decr", DECRTYPE },
|
2001-11-14 09:16:07 +03:00
|
|
|
{ "dnl", DNLNTYPE | NOARGS },
|
|
|
|
{ "changequote", CHNQTYPE | NOARGS },
|
|
|
|
{ "changecom", CHNCTYPE | NOARGS },
|
1997-10-19 08:39:51 +04:00
|
|
|
{ "index", INDXTYPE },
|
1993-06-19 01:50:26 +04:00
|
|
|
#ifdef EXTENDED
|
1997-10-19 08:39:51 +04:00
|
|
|
{ "paste", PASTTYPE },
|
|
|
|
{ "spaste", SPASTYPE },
|
2001-11-14 09:16:07 +03:00
|
|
|
/* Newer extensions, needed to handle gnu-m4 scripts */
|
|
|
|
{ "indir", INDIRTYPE},
|
|
|
|
{ "builtin", BUILTINTYPE},
|
|
|
|
{ "patsubst", PATSTYPE},
|
|
|
|
{ "regexp", REGEXPTYPE},
|
|
|
|
{ "esyscmd", ESYSCMDTYPE},
|
|
|
|
{ "__file__", FILENAMETYPE | NOARGS},
|
|
|
|
{ "__line__", LINETYPE | NOARGS},
|
1993-06-19 01:50:26 +04:00
|
|
|
#endif
|
1997-10-19 08:39:51 +04:00
|
|
|
{ "popdef", POPDTYPE },
|
|
|
|
{ "pushdef", PUSDTYPE },
|
2001-11-14 09:16:07 +03:00
|
|
|
{ "dumpdef", DUMPTYPE | NOARGS },
|
|
|
|
{ "shift", SHIFTYPE | NOARGS },
|
1997-10-19 08:39:51 +04:00
|
|
|
{ "translit", TRNLTYPE },
|
|
|
|
{ "undefine", UNDFTYPE },
|
2001-11-14 09:16:07 +03:00
|
|
|
{ "undivert", UNDVTYPE | NOARGS },
|
|
|
|
{ "divnum", DIVNTYPE | NOARGS },
|
1997-10-19 08:39:51 +04:00
|
|
|
{ "maketemp", MKTMTYPE },
|
2001-11-14 09:16:07 +03:00
|
|
|
{ "errprint", ERRPTYPE | NOARGS },
|
|
|
|
{ "m4wrap", M4WRTYPE | NOARGS },
|
|
|
|
{ "m4exit", EXITTYPE | NOARGS },
|
1997-10-19 08:39:51 +04:00
|
|
|
{ "syscmd", SYSCTYPE },
|
2001-11-14 09:16:07 +03:00
|
|
|
{ "sysval", SYSVTYPE | NOARGS },
|
|
|
|
{ "traceon", TRACEONTYPE | NOARGS },
|
|
|
|
{ "traceoff", TRACEOFFTYPE | NOARGS },
|
1994-06-19 07:58:22 +04:00
|
|
|
|
2001-11-14 09:16:07 +03:00
|
|
|
#if defined(unix) || defined(__unix__)
|
|
|
|
{ "unix", SELFTYPE | NOARGS },
|
1993-06-19 01:50:26 +04:00
|
|
|
#else
|
1994-06-19 07:58:22 +04:00
|
|
|
#ifdef vms
|
2001-11-14 09:16:07 +03:00
|
|
|
{ "vms", SELFTYPE | NOARGS },
|
1993-06-19 01:50:26 +04:00
|
|
|
#endif
|
|
|
|
#endif
|
1994-06-19 07:58:22 +04:00
|
|
|
};
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
#define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk))
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-11-14 09:16:07 +03:00
|
|
|
extern int optind;
|
|
|
|
extern char *optarg;
|
|
|
|
|
|
|
|
#define MAXRECORD 50
|
|
|
|
static struct position {
|
|
|
|
char *name;
|
|
|
|
unsigned long line;
|
|
|
|
} quotes[MAXRECORD], paren[MAXRECORD];
|
|
|
|
|
|
|
|
static void record __P((struct position *, int));
|
|
|
|
static void dump_stack __P((struct position *, int));
|
|
|
|
|
|
|
|
static void macro __P((void));
|
|
|
|
static void initkwds __P((void));
|
|
|
|
static ndptr inspect __P((int, char *));
|
|
|
|
static int do_look_ahead __P((int, const char *));
|
|
|
|
|
|
|
|
static void enlarge_stack __P((void));
|
|
|
|
|
|
|
|
int main __P((int, char *[]));
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
int
|
|
|
|
main(argc,argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
1997-10-19 08:39:51 +04:00
|
|
|
int c;
|
|
|
|
int n;
|
1994-06-19 07:58:22 +04:00
|
|
|
char *p;
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
|
|
|
|
signal(SIGINT, onintr);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1999-06-23 23:09:35 +04:00
|
|
|
/*
|
|
|
|
* We need to know if -P is there before checking -D and -U.
|
|
|
|
*/
|
2001-11-14 17:57:04 +03:00
|
|
|
while ((c = getopt(argc, argv, "D:I:PU:d:go:t:")) != -1)
|
1999-06-23 23:09:35 +04:00
|
|
|
if (c == 'P')
|
|
|
|
m4prefix = 1;
|
|
|
|
optind = 1;
|
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
initkwds();
|
2001-11-14 09:16:07 +03:00
|
|
|
initspaces();
|
|
|
|
STACKMAX = INITSTACKMAX;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-11-14 09:16:07 +03:00
|
|
|
mstack = (stae *)xalloc(sizeof(stae) * STACKMAX);
|
|
|
|
sstack = (char *)xalloc(STACKMAX);
|
|
|
|
|
|
|
|
maxout = 0;
|
|
|
|
outfile = NULL;
|
|
|
|
resizedivs(MAXOUT);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-11-14 09:16:07 +03:00
|
|
|
while ((c = getopt(argc, argv, "D:I:PU:d:go:t:")) != -1)
|
|
|
|
switch (c) {
|
1994-06-19 07:58:22 +04:00
|
|
|
case 'D': /* define something..*/
|
|
|
|
for (p = optarg; *p; p++)
|
|
|
|
if (*p == '=')
|
|
|
|
break;
|
|
|
|
if (*p)
|
|
|
|
*p++ = EOS;
|
|
|
|
dodefine(optarg, p);
|
|
|
|
break;
|
2001-11-14 09:16:07 +03:00
|
|
|
case 'I':
|
|
|
|
addtoincludepath(optarg);
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
break;
|
1994-06-19 07:58:22 +04:00
|
|
|
case 'U': /* undefine... */
|
|
|
|
remhash(optarg, TOP);
|
|
|
|
break;
|
2001-11-14 09:16:07 +03:00
|
|
|
case 'd':
|
|
|
|
set_trace_flags(optarg);
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
mimic_gnu = 1;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
trace_file(optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
mark_traced(optarg, 1);
|
1999-06-23 23:09:35 +04:00
|
|
|
break;
|
1994-06-19 07:58:22 +04:00
|
|
|
case '?':
|
1999-06-23 23:09:35 +04:00
|
|
|
default:
|
2001-11-14 17:57:04 +03:00
|
|
|
usage(argv[0]);
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
active = stdout; /* default active output */
|
|
|
|
bbase[0] = bufbase;
|
|
|
|
if (!argc) {
|
|
|
|
sp = -1; /* stack pointer initialized */
|
|
|
|
fp = 0; /* frame pointer initialized */
|
2001-11-14 09:16:07 +03:00
|
|
|
set_input(infile+0, stdin, "stdin");
|
|
|
|
/* default input (naturally) */
|
1994-06-19 07:58:22 +04:00
|
|
|
macro();
|
|
|
|
} else
|
|
|
|
for (; argc--; ++argv) {
|
|
|
|
p = *argv;
|
2001-11-14 09:16:07 +03:00
|
|
|
if (p[0] == '-' && p[1] == EOS)
|
|
|
|
set_input(infile, stdin, "stdin");
|
|
|
|
else if (fopen_trypath(infile, p) == NULL)
|
1997-10-19 08:39:51 +04:00
|
|
|
err(1, "%s", p);
|
1994-06-19 07:58:22 +04:00
|
|
|
sp = -1;
|
|
|
|
fp = 0;
|
|
|
|
macro();
|
2001-11-14 09:16:07 +03:00
|
|
|
release_input(infile);
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
if (*m4wraps) { /* anything for rundown ?? */
|
|
|
|
ilevel = 0; /* in case m4wrap includes.. */
|
|
|
|
bufbase = bp = buf; /* use the entire buffer */
|
|
|
|
pbstr(m4wraps); /* user-defined wrapup act */
|
|
|
|
macro(); /* last will and testament */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
if (active != stdout)
|
|
|
|
active = stdout; /* reset output just in case */
|
2001-11-14 09:16:07 +03:00
|
|
|
for (n = 1; n < maxout; n++) /* default wrap-up: undivert */
|
1994-06-19 07:58:22 +04:00
|
|
|
if (outfile[n] != NULL)
|
|
|
|
getdiv(n);
|
2001-11-14 09:16:07 +03:00
|
|
|
/* remove bitbucket if used */
|
|
|
|
if (outfile[0] != NULL) {
|
1994-06-19 07:58:22 +04:00
|
|
|
(void) fclose(outfile[0]);
|
2001-11-14 09:16:07 +03:00
|
|
|
}
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
return 0;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1996-01-14 02:25:23 +03:00
|
|
|
/*
|
2001-11-14 09:16:07 +03:00
|
|
|
* Look ahead for `token'.
|
1996-01-14 02:25:23 +03:00
|
|
|
* (on input `t == token[0]')
|
|
|
|
* Used for comment and quoting delimiters.
|
|
|
|
* Returns 1 if `token' present; copied to output.
|
|
|
|
* 0 if `token' not found; all characters pushed back
|
|
|
|
*/
|
2001-11-14 09:16:07 +03:00
|
|
|
static int
|
1996-01-14 02:25:23 +03:00
|
|
|
do_look_ahead(t, token)
|
|
|
|
int t;
|
2001-11-14 09:16:07 +03:00
|
|
|
const char *token;
|
1996-01-14 02:25:23 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2001-11-14 09:16:07 +03:00
|
|
|
assert((unsigned char)t == (unsigned char)token[0]);
|
1996-01-14 02:25:23 +03:00
|
|
|
|
|
|
|
for (i = 1; *++token; i++) {
|
|
|
|
t = gpbc();
|
2001-11-14 09:16:07 +03:00
|
|
|
if (t == EOF || (unsigned char)t != (unsigned char)*token) {
|
|
|
|
putback(t);
|
1996-01-14 02:25:23 +03:00
|
|
|
while (--i)
|
|
|
|
putback(*--token);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-11-14 09:16:07 +03:00
|
|
|
#define LOOK_AHEAD(t, token) (t != EOF && \
|
|
|
|
(unsigned char)(t)==(unsigned char)(token)[0] && \
|
|
|
|
do_look_ahead(t,token))
|
1996-01-14 02:25:23 +03:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
/*
|
|
|
|
* macro - the work horse..
|
|
|
|
*/
|
2001-11-14 09:16:07 +03:00
|
|
|
static void
|
1997-10-19 08:39:51 +04:00
|
|
|
macro()
|
|
|
|
{
|
2001-11-14 09:16:07 +03:00
|
|
|
char token[MAXTOK+1];
|
1997-10-19 08:39:51 +04:00
|
|
|
int t, l;
|
|
|
|
ndptr p;
|
|
|
|
int nlpar;
|
1994-06-19 07:58:22 +04:00
|
|
|
|
|
|
|
cycle {
|
1996-01-14 02:25:23 +03:00
|
|
|
t = gpbc();
|
|
|
|
if (t == '_' || isalpha(t)) {
|
2001-11-14 09:16:07 +03:00
|
|
|
p = inspect(t, token);
|
|
|
|
if (p != nil)
|
|
|
|
putback(l = gpbc());
|
|
|
|
if (p == nil || (l != LPAREN &&
|
|
|
|
(p->type & NEEDARGS) != 0))
|
|
|
|
outputstr(token);
|
1994-06-19 07:58:22 +04:00
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* real thing.. First build a call frame:
|
|
|
|
*/
|
|
|
|
pushf(fp); /* previous call frm */
|
|
|
|
pushf(p->type); /* type of the call */
|
|
|
|
pushf(0); /* parenthesis level */
|
|
|
|
fp = sp; /* new frame pointer */
|
|
|
|
/*
|
|
|
|
* now push the string arguments:
|
|
|
|
*/
|
2001-11-14 09:16:07 +03:00
|
|
|
pushs1(p->defn); /* defn string */
|
|
|
|
pushs1(p->name); /* macro name */
|
|
|
|
pushs(ep); /* start next..*/
|
|
|
|
|
|
|
|
if (l != LPAREN && PARLEV == 0) {
|
|
|
|
/* no bracks */
|
|
|
|
chrsave(EOS);
|
|
|
|
|
|
|
|
if (sp == STACKMAX)
|
|
|
|
errx(1, "internal stack overflow");
|
|
|
|
eval((const char **) mstack+fp+1, 2,
|
|
|
|
CALTYP);
|
|
|
|
|
|
|
|
ep = PREVEP; /* flush strspace */
|
|
|
|
sp = PREVSP; /* previous sp.. */
|
|
|
|
fp = PREVFP; /* rewind stack...*/
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
|
|
|
}
|
2001-11-14 09:16:07 +03:00
|
|
|
} else if (t == EOF) {
|
|
|
|
if (sp > -1) {
|
|
|
|
warnx( "unexpected end of input, unclosed parenthesis:");
|
|
|
|
dump_stack(paren, PARLEV);
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-06-19 07:58:22 +04:00
|
|
|
if (ilevel <= 0)
|
|
|
|
break; /* all done thanks.. */
|
2001-11-14 09:16:07 +03:00
|
|
|
release_input(infile+ilevel--);
|
1994-06-19 07:58:22 +04:00
|
|
|
bufbase = bbase[ilevel];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
1997-02-09 02:54:49 +03:00
|
|
|
* non-alpha token possibly seen..
|
1994-06-19 07:58:22 +04:00
|
|
|
* [the order of else if .. stmts is important.]
|
|
|
|
*/
|
1996-01-14 02:25:23 +03:00
|
|
|
else if (LOOK_AHEAD(t,lquote)) { /* strip quotes */
|
2001-11-14 09:16:07 +03:00
|
|
|
nlpar = 0;
|
|
|
|
record(quotes, nlpar++);
|
|
|
|
/*
|
|
|
|
* Opening quote: scan forward until matching
|
|
|
|
* closing quote has been found.
|
|
|
|
*/
|
1994-06-19 07:58:22 +04:00
|
|
|
do {
|
1997-02-09 02:54:49 +03:00
|
|
|
|
1996-01-14 02:25:23 +03:00
|
|
|
l = gpbc();
|
1997-02-09 02:54:49 +03:00
|
|
|
if (LOOK_AHEAD(l,rquote)) {
|
2001-11-14 09:16:07 +03:00
|
|
|
if (--nlpar > 0)
|
|
|
|
outputstr(rquote);
|
1997-02-09 02:54:49 +03:00
|
|
|
} else if (LOOK_AHEAD(l,lquote)) {
|
2001-11-14 09:16:07 +03:00
|
|
|
record(quotes, nlpar++);
|
|
|
|
outputstr(lquote);
|
|
|
|
} else if (l == EOF) {
|
|
|
|
if (nlpar == 1)
|
|
|
|
warnx("unclosed quote:");
|
1994-06-19 07:58:22 +04:00
|
|
|
else
|
2001-11-14 09:16:07 +03:00
|
|
|
warnx("%d unclosed quotes:", nlpar);
|
|
|
|
dump_stack(quotes, nlpar);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
if (nlpar > 0) {
|
|
|
|
if (sp < 0)
|
|
|
|
putc(l, active);
|
|
|
|
else
|
|
|
|
CHRSAVE(l);
|
|
|
|
}
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nlpar != 0);
|
|
|
|
}
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1996-01-14 02:25:23 +03:00
|
|
|
else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
|
2001-11-14 09:16:07 +03:00
|
|
|
fputs(scommt, active);
|
1996-01-14 02:25:23 +03:00
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
t = gpbc();
|
|
|
|
if (LOOK_AHEAD(t, ecommt)) {
|
2001-11-14 09:16:07 +03:00
|
|
|
fputs(ecommt, active);
|
1996-01-14 02:25:23 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (t == EOF)
|
|
|
|
break;
|
1994-06-19 07:58:22 +04:00
|
|
|
putc(t, active);
|
|
|
|
}
|
1996-01-14 02:25:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (sp < 0) { /* not in a macro at all */
|
1994-06-19 07:58:22 +04:00
|
|
|
putc(t, active); /* output directly.. */
|
|
|
|
}
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
else switch(t) {
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
case LPAREN:
|
|
|
|
if (PARLEV > 0)
|
|
|
|
chrsave(t);
|
|
|
|
while (isspace(l = gpbc()))
|
|
|
|
; /* skip blank, tab, nl.. */
|
|
|
|
putback(l);
|
2001-11-14 09:16:07 +03:00
|
|
|
record(paren, PARLEV++);
|
1994-06-19 07:58:22 +04:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
case RPAREN:
|
|
|
|
if (--PARLEV > 0)
|
|
|
|
chrsave(t);
|
|
|
|
else { /* end of argument list */
|
|
|
|
chrsave(EOS);
|
|
|
|
|
|
|
|
if (sp == STACKMAX)
|
1997-10-19 08:39:51 +04:00
|
|
|
errx(1, "internal stack overflow");
|
1994-06-19 07:58:22 +04:00
|
|
|
|
2001-11-14 09:16:07 +03:00
|
|
|
eval((const char **) mstack+fp+1, sp-fp,
|
|
|
|
CALTYP);
|
1994-06-19 07:58:22 +04:00
|
|
|
|
|
|
|
ep = PREVEP; /* flush strspace */
|
|
|
|
sp = PREVSP; /* previous sp.. */
|
|
|
|
fp = PREVFP; /* rewind stack...*/
|
|
|
|
}
|
|
|
|
break;
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
case COMMA:
|
|
|
|
if (PARLEV == 1) {
|
|
|
|
chrsave(EOS); /* new argument */
|
|
|
|
while (isspace(l = gpbc()))
|
|
|
|
;
|
|
|
|
putback(l);
|
|
|
|
pushs(ep);
|
|
|
|
} else
|
|
|
|
chrsave(t);
|
|
|
|
break;
|
1993-06-19 01:50:26 +04:00
|
|
|
|
|
|
|
default:
|
2001-11-14 09:16:07 +03:00
|
|
|
if (LOOK_AHEAD(t, scommt)) {
|
|
|
|
char *p;
|
|
|
|
for (p = scommt; *p; p++)
|
|
|
|
chrsave(*p);
|
|
|
|
for(;;) {
|
|
|
|
t = gpbc();
|
|
|
|
if (LOOK_AHEAD(t, ecommt)) {
|
|
|
|
for (p = ecommt; *p; p++)
|
|
|
|
chrsave(*p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (t == EOF)
|
|
|
|
break;
|
|
|
|
CHRSAVE(t);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
CHRSAVE(t); /* stack the char */
|
1994-06-19 07:58:22 +04:00
|
|
|
break;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
1993-06-19 01:50:26 +04:00
|
|
|
|
2001-11-14 09:16:07 +03:00
|
|
|
/*
|
|
|
|
* output string directly, without pushing it for reparses.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
outputstr(s)
|
|
|
|
const char *s;
|
|
|
|
{
|
|
|
|
if (sp < 0)
|
|
|
|
while (*s)
|
|
|
|
putc(*s++, active);
|
|
|
|
else
|
|
|
|
while (*s)
|
|
|
|
CHRSAVE(*s++);
|
|
|
|
}
|
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
/*
|
|
|
|
* build an input token..
|
|
|
|
* consider only those starting with _ or A-Za-z. This is a
|
|
|
|
* combo with lookup to speed things up.
|
|
|
|
*/
|
2001-11-14 09:16:07 +03:00
|
|
|
static ndptr
|
|
|
|
inspect(c, tp)
|
|
|
|
int c;
|
1997-10-19 08:39:51 +04:00
|
|
|
char *tp;
|
1994-06-19 07:58:22 +04:00
|
|
|
{
|
1997-10-19 08:39:51 +04:00
|
|
|
char *name = tp;
|
|
|
|
char *etp = tp+MAXTOK;
|
|
|
|
ndptr p;
|
2001-11-14 09:16:07 +03:00
|
|
|
unsigned int h;
|
|
|
|
|
|
|
|
h = *tp++ = c;
|
1993-06-19 01:50:26 +04:00
|
|
|
|
2001-11-14 09:16:07 +03:00
|
|
|
while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
|
1994-06-19 07:58:22 +04:00
|
|
|
h = (h << 5) + h + (*tp++ = c);
|
2001-11-14 09:16:07 +03:00
|
|
|
if (c != EOF)
|
|
|
|
PUTBACK(c);
|
1994-06-19 07:58:22 +04:00
|
|
|
*tp = EOS;
|
2001-11-14 09:16:07 +03:00
|
|
|
/* token is too long, it won't match anything, but it can still
|
|
|
|
* be output. */
|
|
|
|
if (tp == ep) {
|
|
|
|
outputstr(name);
|
|
|
|
while (isalnum(c = gpbc()) || c == '_') {
|
|
|
|
if (sp < 0)
|
|
|
|
putc(c, active);
|
|
|
|
else
|
|
|
|
CHRSAVE(c);
|
|
|
|
}
|
|
|
|
*name = EOS;
|
|
|
|
return nil;
|
|
|
|
}
|
1993-06-19 01:50:26 +04:00
|
|
|
|
2001-11-14 09:16:07 +03:00
|
|
|
for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
|
|
|
|
if (h == p->hv && STREQ(name, p->name))
|
1994-06-19 07:58:22 +04:00
|
|
|
break;
|
|
|
|
return p;
|
|
|
|
}
|
1993-11-03 08:16:21 +03:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
/*
|
2000-10-18 21:23:18 +04:00
|
|
|
* initkwds - initialise m4 keywords as fast as possible.
|
1994-06-19 07:58:22 +04:00
|
|
|
* This very similar to install, but without certain overheads,
|
2000-10-18 21:23:18 +04:00
|
|
|
* such as calling lookup. Malloc is not used for storing the
|
1997-10-19 08:39:51 +04:00
|
|
|
* keyword strings, since we simply use the static pointers
|
1994-06-19 07:58:22 +04:00
|
|
|
* within keywrds block.
|
|
|
|
*/
|
2001-11-14 09:16:07 +03:00
|
|
|
static void
|
1997-10-19 08:39:51 +04:00
|
|
|
initkwds()
|
|
|
|
{
|
2001-11-14 09:16:07 +03:00
|
|
|
size_t i;
|
|
|
|
unsigned int h;
|
1997-10-19 08:39:51 +04:00
|
|
|
ndptr p;
|
1999-06-23 23:09:35 +04:00
|
|
|
char *k;
|
1993-11-03 08:16:21 +03:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
for (i = 0; i < MAXKEYS; i++) {
|
2001-03-05 23:26:17 +03:00
|
|
|
k = (char *)keywrds[i].knam;
|
2001-11-14 09:16:07 +03:00
|
|
|
if (m4prefix) {
|
|
|
|
if (asprintf(&k, "m4_%s", k) == -1)
|
|
|
|
err(1, "asprintf");
|
|
|
|
keywrds[i].knam = k;
|
|
|
|
}
|
1999-06-23 23:09:35 +04:00
|
|
|
h = hash(k);
|
1994-06-19 07:58:22 +04:00
|
|
|
p = (ndptr) xalloc(sizeof(struct ndblock));
|
2001-11-14 09:16:07 +03:00
|
|
|
p->nxtptr = hashtab[h % HASHSIZE];
|
|
|
|
hashtab[h % HASHSIZE] = p;
|
|
|
|
p->name = xstrdup(keywrds[i].knam);
|
1994-06-19 07:58:22 +04:00
|
|
|
p->defn = null;
|
2001-11-14 09:16:07 +03:00
|
|
|
p->hv = h;
|
|
|
|
p->type = keywrds[i].ktyp & TYPEMASK;
|
|
|
|
if ((keywrds[i].ktyp & NOARGS) == 0)
|
|
|
|
p->type |= NEEDARGS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up a builtin type, even if overridden by the user */
|
|
|
|
int
|
|
|
|
builtin_type(key)
|
|
|
|
const char *key;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i != MAXKEYS; i++)
|
|
|
|
if (STREQ(keywrds[i].knam, key))
|
|
|
|
return keywrds[i].ktyp;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
builtin_realname(n)
|
|
|
|
int n;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i != MAXKEYS; i++)
|
|
|
|
if (((keywrds[i].ktyp ^ n) & TYPEMASK) == 0)
|
|
|
|
return keywrds[i].knam;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
record(t, lev)
|
|
|
|
struct position *t;
|
|
|
|
int lev;
|
|
|
|
{
|
|
|
|
if (lev < MAXRECORD) {
|
|
|
|
t[lev].name = CURRENT_NAME;
|
|
|
|
t[lev].line = CURRENT_LINE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_stack(t, lev)
|
|
|
|
struct position *t;
|
|
|
|
int lev;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < lev; i++) {
|
|
|
|
if (i == MAXRECORD) {
|
|
|
|
fprintf(stderr, " ...\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fprintf(stderr, " %s at line %lu\n",
|
|
|
|
t[i].name, t[i].line);
|
1993-06-19 01:50:26 +04:00
|
|
|
}
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
2001-11-14 09:16:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
enlarge_stack()
|
|
|
|
{
|
|
|
|
STACKMAX *= 2;
|
|
|
|
mstack = realloc(mstack, sizeof(stae) * STACKMAX);
|
|
|
|
sstack = realloc(sstack, STACKMAX);
|
|
|
|
if (mstack == NULL || sstack == NULL)
|
|
|
|
errx(1, "Evaluation stack overflow (%lu)",
|
|
|
|
(unsigned long)STACKMAX);
|
|
|
|
}
|