2002-05-01 21:37:29 +00:00
|
|
|
/* $OpenBSD: main.c,v 1.53 2002/04/26 16:15:16 espie Exp $ */
|
2002-04-19 17:26:21 +00:00
|
|
|
/* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
|
|
|
|
|
1994-05-27 12:33:43 +00: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.
|
|
|
|
*/
|
|
|
|
|
2002-05-03 14:48:21 +00:00
|
|
|
#ifndef lint
|
|
|
|
static char copyright[] =
|
|
|
|
"@(#) Copyright (c) 1989, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#else
|
|
|
|
#if 0
|
|
|
|
static char rcsid[] = "$OpenBSD: main.c,v 1.53 2002/04/26 16:15:16 espie Exp $";
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* main.c
|
|
|
|
* Facility: m4 macro processor
|
|
|
|
* by: oz
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2002-04-19 17:26:21 +00:00
|
|
|
#include <assert.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <signal.h>
|
2002-04-19 17:26:21 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
2002-04-19 17:26:21 +00:00
|
|
|
#include <ctype.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <string.h>
|
2002-04-19 17:26:21 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <err.h>
|
2004-07-12 02:48:40 +00:00
|
|
|
#include <locale.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include "mdef.h"
|
|
|
|
#include "stdd.h"
|
|
|
|
#include "extern.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
|
|
|
ndptr hashtab[HASHSIZE]; /* hash table for macros etc. */
|
2002-04-19 17:26:21 +00:00
|
|
|
stae *mstack; /* stack of m4 machine */
|
|
|
|
char *sstack; /* shadow stack, for string space extension */
|
|
|
|
static size_t STACKMAX; /* current maximum size of stack */
|
1994-05-27 12:33:43 +00:00
|
|
|
int sp; /* current m4 stack pointer */
|
|
|
|
int fp; /* m4 call frame pointer */
|
2002-04-19 17:26:21 +00:00
|
|
|
struct input_file infile[MAXINP];/* input file stack (0=stdin) */
|
|
|
|
char *inname[MAXINP]; /* names of these input files */
|
|
|
|
int inlineno[MAXINP]; /* current number in each input file */
|
|
|
|
FILE **outfile; /* diversion array(0=bitbucket)*/
|
|
|
|
int maxout;
|
1994-05-27 12:33:43 +00:00
|
|
|
FILE *active; /* active output file pointer */
|
|
|
|
int ilevel = 0; /* input file stack pointer */
|
|
|
|
int oindex = 0; /* diversion index.. */
|
Crank WARNS.
Cast sizeof() to (int), as it's being compared against an int, not a size_t.
If i is changed to a size_t, it means the logic must be slightly changed later
in the flow, where --i is checked to be >= 0. I am not sure I want to make a
logic change to account for clearing up a warning, when an aesthetic one will
keep from modifying the logic.
Other harmless casts, that I think I've made in the right directions.
Make gpbc() an inline function, rather than an obfuscated macro, make its
scratch space local, rather than global. The previous macro used a dirty
hack (logical AND in place of a conditional) which would lead GCC to throw
a fit (rightly so) as the logical check, as well as the incrementation of
a variable, were not used for anything.
const'ify a few places where gcc3 yells. xstrdup() some global consts in
places where we xstrdup() when not using consts, but tried to assign them
to non-consts before.
Don't use execv(2) if we don't have the kind of arguments it wants.
Reviewed by: asmodai obrien tjr
Submitted by: tjr (a gcc3 build log)
2002-04-20 01:49:10 +00:00
|
|
|
const char *null = ""; /* as it says.. just a null.. */
|
|
|
|
const char *m4wraps = ""; /* m4wrap string default.. */
|
2002-04-19 17:26:21 +00: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 */
|
2002-04-17 17:26:32 +00:00
|
|
|
int synccpp; /* Line synchronisation for C preprocessor */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
struct keyblk keywrds[] = { /* m4 keywords to be installed */
|
2002-04-19 17:26:21 +00:00
|
|
|
{ "include", INCLTYPE },
|
|
|
|
{ "sinclude", SINCTYPE },
|
|
|
|
{ "define", DEFITYPE },
|
|
|
|
{ "defn", DEFNTYPE },
|
|
|
|
{ "divert", DIVRTYPE | NOARGS },
|
|
|
|
{ "expr", EXPRTYPE },
|
|
|
|
{ "eval", EXPRTYPE },
|
|
|
|
{ "substr", SUBSTYPE },
|
|
|
|
{ "ifelse", IFELTYPE },
|
|
|
|
{ "ifdef", IFDFTYPE },
|
|
|
|
{ "len", LENGTYPE },
|
|
|
|
{ "incr", INCRTYPE },
|
|
|
|
{ "decr", DECRTYPE },
|
|
|
|
{ "dnl", DNLNTYPE | NOARGS },
|
|
|
|
{ "changequote", CHNQTYPE | NOARGS },
|
|
|
|
{ "changecom", CHNCTYPE | NOARGS },
|
|
|
|
{ "index", INDXTYPE },
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifdef EXTENDED
|
2002-04-19 17:26:21 +00:00
|
|
|
{ "paste", PASTTYPE },
|
|
|
|
{ "spaste", SPASTYPE },
|
|
|
|
/* Newer extensions, needed to handle gnu-m4 scripts */
|
|
|
|
{ "indir", INDIRTYPE},
|
|
|
|
{ "builtin", BUILTINTYPE},
|
|
|
|
{ "patsubst", PATSTYPE},
|
|
|
|
{ "regexp", REGEXPTYPE},
|
|
|
|
{ "esyscmd", ESYSCMDTYPE},
|
|
|
|
{ "__file__", FILENAMETYPE | NOARGS},
|
|
|
|
{ "__line__", LINETYPE | NOARGS},
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif
|
2002-04-19 17:26:21 +00:00
|
|
|
{ "popdef", POPDTYPE },
|
|
|
|
{ "pushdef", PUSDTYPE },
|
|
|
|
{ "dumpdef", DUMPTYPE | NOARGS },
|
|
|
|
{ "shift", SHIFTYPE | NOARGS },
|
|
|
|
{ "translit", TRNLTYPE },
|
|
|
|
{ "undefine", UNDFTYPE },
|
|
|
|
{ "undivert", UNDVTYPE | NOARGS },
|
|
|
|
{ "divnum", DIVNTYPE | NOARGS },
|
|
|
|
{ "maketemp", MKTMTYPE },
|
|
|
|
{ "errprint", ERRPTYPE | NOARGS },
|
|
|
|
{ "m4wrap", M4WRTYPE | NOARGS },
|
|
|
|
{ "m4exit", EXITTYPE | NOARGS },
|
|
|
|
{ "syscmd", SYSCTYPE },
|
|
|
|
{ "sysval", SYSVTYPE | NOARGS },
|
|
|
|
{ "traceon", TRACEONTYPE | NOARGS },
|
|
|
|
{ "traceoff", TRACEOFFTYPE | NOARGS },
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
#if defined(unix) || defined(__unix__)
|
2002-04-19 17:26:21 +00:00
|
|
|
{ "unix", SELFTYPE | NOARGS },
|
1994-05-27 12:33:43 +00:00
|
|
|
#else
|
|
|
|
#ifdef vms
|
2002-04-19 17:26:21 +00:00
|
|
|
{ "vms", SELFTYPE | NOARGS },
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk))
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
#define MAXRECORD 50
|
|
|
|
static struct position {
|
|
|
|
char *name;
|
|
|
|
unsigned long line;
|
|
|
|
} quotes[MAXRECORD], paren[MAXRECORD];
|
|
|
|
|
|
|
|
static void record(struct position *, int);
|
|
|
|
static void dump_stack(struct position *, int);
|
|
|
|
|
|
|
|
static void macro(void);
|
|
|
|
static void initkwds(void);
|
|
|
|
static ndptr inspect(int, char *);
|
|
|
|
static int do_look_ahead(int, const char *);
|
|
|
|
|
|
|
|
static void enlarge_stack(void);
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
int
|
2002-05-01 21:37:29 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-19 17:26:21 +00:00
|
|
|
int c;
|
|
|
|
int n;
|
2002-05-26 06:15:15 +00:00
|
|
|
int rval;
|
1994-05-27 12:33:43 +00:00
|
|
|
char *p;
|
2002-04-19 17:26:21 +00:00
|
|
|
|
2004-07-12 02:48:40 +00:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
traceout = stderr;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
|
|
|
|
signal(SIGINT, onintr);
|
|
|
|
|
|
|
|
initkwds();
|
2002-04-19 17:26:21 +00:00
|
|
|
initspaces();
|
|
|
|
STACKMAX = INITSTACKMAX;
|
|
|
|
|
|
|
|
mstack = (stae *)xalloc(sizeof(stae) * STACKMAX);
|
|
|
|
sstack = (char *)xalloc(STACKMAX);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
maxout = 0;
|
|
|
|
outfile = NULL;
|
|
|
|
resizedivs(MAXOUT);
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "gst:d:D:U:o:I:")) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch(c) {
|
|
|
|
case 'D': /* define something..*/
|
|
|
|
for (p = optarg; *p; p++)
|
|
|
|
if (*p == '=')
|
|
|
|
break;
|
2002-09-04 01:12:39 +00:00
|
|
|
if (p == optarg)
|
|
|
|
errx(1, "null variable cannot be defined");
|
1994-05-27 12:33:43 +00:00
|
|
|
if (*p)
|
|
|
|
*p++ = EOS;
|
|
|
|
dodefine(optarg, p);
|
|
|
|
break;
|
2002-04-19 17:26:21 +00:00
|
|
|
case 'I':
|
|
|
|
addtoincludepath(optarg);
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'U': /* undefine... */
|
|
|
|
remhash(optarg, TOP);
|
|
|
|
break;
|
2002-04-19 17:26:21 +00:00
|
|
|
case 'g':
|
|
|
|
mimic_gnu = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
set_trace_flags(optarg);
|
|
|
|
break;
|
2002-04-17 17:26:32 +00:00
|
|
|
case 's':
|
|
|
|
synccpp = 1;
|
|
|
|
break;
|
2002-04-19 17:26:21 +00:00
|
|
|
case 't':
|
|
|
|
mark_traced(optarg, 1);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
trace_file(optarg);
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case '?':
|
2002-07-14 02:34:37 +00:00
|
|
|
default:
|
1994-05-27 12:33:43 +00:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2002-05-26 06:15:15 +00:00
|
|
|
rval = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
active = stdout; /* default active output */
|
|
|
|
bbase[0] = bufbase;
|
|
|
|
if (!argc) {
|
|
|
|
sp = -1; /* stack pointer initialized */
|
|
|
|
fp = 0; /* frame pointer initialized */
|
2002-04-19 17:26:21 +00:00
|
|
|
set_input(infile+0, stdin, "stdin");
|
|
|
|
/* default input (naturally) */
|
2002-04-17 17:26:32 +00:00
|
|
|
if ((inname[0] = strdup("-")) == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
inlineno[0] = 1;
|
|
|
|
emitline();
|
1994-05-27 12:33:43 +00:00
|
|
|
macro();
|
|
|
|
} else
|
|
|
|
for (; argc--; ++argv) {
|
|
|
|
p = *argv;
|
2002-04-19 17:26:21 +00:00
|
|
|
if (p[0] == '-' && p[1] == EOS)
|
|
|
|
set_input(infile, stdin, "stdin");
|
2002-05-26 06:15:15 +00:00
|
|
|
else if (fopen_trypath(infile, p) == NULL) {
|
|
|
|
warn("%s", p);
|
|
|
|
rval = 1;
|
|
|
|
continue;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
sp = -1;
|
2002-07-15 02:15:12 +00:00
|
|
|
fp = 0;
|
2002-04-17 17:26:32 +00:00
|
|
|
if ((inname[0] = strdup(p)) == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
inlineno[0] = 1;
|
|
|
|
emitline();
|
1994-05-27 12:33:43 +00:00
|
|
|
macro();
|
2002-04-19 17:26:21 +00:00
|
|
|
release_input(infile);
|
1994-05-27 12:33:43 +00: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 */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active != stdout)
|
|
|
|
active = stdout; /* reset output just in case */
|
2002-04-19 17:26:21 +00:00
|
|
|
for (n = 1; n < maxout; n++) /* default wrap-up: undivert */
|
1994-05-27 12:33:43 +00:00
|
|
|
if (outfile[n] != NULL)
|
|
|
|
getdiv(n);
|
|
|
|
/* remove bitbucket if used */
|
2002-04-19 17:26:21 +00:00
|
|
|
if (outfile[0] != NULL) {
|
|
|
|
(void) fclose(outfile[0]);
|
|
|
|
}
|
|
|
|
|
2002-05-26 06:15:15 +00:00
|
|
|
exit(rval);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
/*
|
|
|
|
* Look ahead for `token'.
|
|
|
|
* (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
|
|
|
|
*/
|
|
|
|
static int
|
2002-05-01 21:37:29 +00:00
|
|
|
do_look_ahead(int t, const char *token)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
assert((unsigned char)t == (unsigned char)token[0]);
|
|
|
|
|
|
|
|
for (i = 1; *++token; i++) {
|
|
|
|
t = gpbc();
|
|
|
|
if (t == EOF || (unsigned char)t != (unsigned char)*token) {
|
|
|
|
putback(t);
|
|
|
|
while (--i)
|
|
|
|
putback(*--token);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOOK_AHEAD(t, token) (t != EOF && \
|
|
|
|
(unsigned char)(t)==(unsigned char)(token)[0] && \
|
|
|
|
do_look_ahead(t,token))
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* macro - the work horse..
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2002-07-14 02:03:23 +00:00
|
|
|
macro(void)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
|
|
|
char token[MAXTOK+1];
|
|
|
|
int t, l;
|
|
|
|
ndptr p;
|
|
|
|
int nlpar;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
cycle {
|
2002-04-19 17:26:21 +00:00
|
|
|
t = gpbc();
|
|
|
|
if (t == '_' || isalpha(t)) {
|
|
|
|
p = inspect(t, token);
|
|
|
|
if (p != nil)
|
|
|
|
putback(l = gpbc());
|
2002-07-15 02:15:12 +00:00
|
|
|
if (p == nil || (l != LPAREN &&
|
2002-04-19 17:26:21 +00:00
|
|
|
(p->type & NEEDARGS) != 0))
|
|
|
|
outputstr(token);
|
1994-05-27 12:33:43 +00: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:
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
pushs1(p->defn); /* defn string */
|
|
|
|
pushs1(p->name); /* macro name */
|
|
|
|
pushs(ep); /* start next..*/
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
if (l != LPAREN && PARLEV == 0) {
|
2002-04-19 17:26:21 +00:00
|
|
|
/* no bracks */
|
|
|
|
chrsave(EOS);
|
|
|
|
|
2002-06-20 11:14:54 +00:00
|
|
|
if ((uintptr_t)sp == STACKMAX)
|
2002-04-19 17:26:21 +00:00
|
|
|
errx(1, "internal stack overflow");
|
2002-07-15 02:15:12 +00:00
|
|
|
eval((const char **) mstack+fp+1, 2,
|
2002-04-19 17:26:21 +00:00
|
|
|
CALTYP);
|
|
|
|
|
|
|
|
ep = PREVEP; /* flush strspace */
|
|
|
|
sp = PREVSP; /* previous sp.. */
|
|
|
|
fp = PREVFP; /* rewind stack...*/
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
2002-04-19 17:26:21 +00:00
|
|
|
} else if (t == EOF) {
|
|
|
|
if (sp > -1) {
|
|
|
|
warnx( "unexpected end of input, unclosed parenthesis:");
|
|
|
|
dump_stack(paren, PARLEV);
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
if (ilevel <= 0)
|
|
|
|
break; /* all done thanks.. */
|
2002-04-19 17:26:21 +00:00
|
|
|
release_input(infile+ilevel--);
|
2002-04-17 17:26:32 +00:00
|
|
|
free(inname[ilevel+1]);
|
1994-05-27 12:33:43 +00:00
|
|
|
bufbase = bbase[ilevel];
|
2002-04-17 17:26:32 +00:00
|
|
|
emitline();
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
2002-04-19 17:26:21 +00:00
|
|
|
* non-alpha token possibly seen..
|
1994-05-27 12:33:43 +00:00
|
|
|
* [the order of else if .. stmts is important.]
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
else if (LOOK_AHEAD(t,lquote)) { /* strip quotes */
|
|
|
|
nlpar = 0;
|
|
|
|
record(quotes, nlpar++);
|
|
|
|
/*
|
|
|
|
* Opening quote: scan forward until matching
|
|
|
|
* closing quote has been found.
|
|
|
|
*/
|
1994-05-27 12:33:43 +00:00
|
|
|
do {
|
2002-04-19 17:26:21 +00:00
|
|
|
|
|
|
|
l = gpbc();
|
|
|
|
if (LOOK_AHEAD(l,rquote)) {
|
|
|
|
if (--nlpar > 0)
|
|
|
|
outputstr(rquote);
|
|
|
|
} else if (LOOK_AHEAD(l,lquote)) {
|
|
|
|
record(quotes, nlpar++);
|
|
|
|
outputstr(lquote);
|
|
|
|
} else if (l == EOF) {
|
|
|
|
if (nlpar == 1)
|
|
|
|
warnx("unclosed quote:");
|
1994-05-27 12:33:43 +00:00
|
|
|
else
|
2002-04-19 17:26:21 +00: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-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nlpar != 0);
|
|
|
|
}
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
|
|
|
|
fputs(scommt, active);
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
t = gpbc();
|
|
|
|
if (LOOK_AHEAD(t, ecommt)) {
|
|
|
|
fputs(ecommt, active);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (t == EOF)
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
putc(t, active);
|
|
|
|
}
|
2002-04-19 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (sp < 0) { /* not in a macro at all */
|
1994-05-27 12:33:43 +00:00
|
|
|
putc(t, active); /* output directly.. */
|
|
|
|
}
|
|
|
|
|
|
|
|
else switch(t) {
|
|
|
|
|
|
|
|
case LPAREN:
|
|
|
|
if (PARLEV > 0)
|
|
|
|
chrsave(t);
|
2002-04-19 17:26:21 +00:00
|
|
|
while (isspace(l = gpbc()))
|
1994-05-27 12:33:43 +00:00
|
|
|
; /* skip blank, tab, nl.. */
|
|
|
|
putback(l);
|
2002-04-19 17:26:21 +00:00
|
|
|
record(paren, PARLEV++);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RPAREN:
|
|
|
|
if (--PARLEV > 0)
|
|
|
|
chrsave(t);
|
|
|
|
else { /* end of argument list */
|
|
|
|
chrsave(EOS);
|
|
|
|
|
2002-06-20 11:14:54 +00:00
|
|
|
if ((uintptr_t)sp == STACKMAX)
|
1997-07-23 06:50:06 +00:00
|
|
|
errx(1, "internal stack overflow");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
eval((const char **) mstack+fp+1, sp-fp,
|
2002-04-19 17:26:21 +00:00
|
|
|
CALTYP);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
ep = PREVEP; /* flush strspace */
|
|
|
|
sp = PREVSP; /* previous sp.. */
|
|
|
|
fp = PREVFP; /* rewind stack...*/
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COMMA:
|
|
|
|
if (PARLEV == 1) {
|
|
|
|
chrsave(EOS); /* new argument */
|
2002-04-19 17:26:21 +00:00
|
|
|
while (isspace(l = gpbc()))
|
1994-05-27 12:33:43 +00:00
|
|
|
;
|
|
|
|
putback(l);
|
|
|
|
pushs(ep);
|
|
|
|
} else
|
|
|
|
chrsave(t);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2002-04-19 17:26:21 +00:00
|
|
|
if (LOOK_AHEAD(t, scommt)) {
|
Crank WARNS.
Cast sizeof() to (int), as it's being compared against an int, not a size_t.
If i is changed to a size_t, it means the logic must be slightly changed later
in the flow, where --i is checked to be >= 0. I am not sure I want to make a
logic change to account for clearing up a warning, when an aesthetic one will
keep from modifying the logic.
Other harmless casts, that I think I've made in the right directions.
Make gpbc() an inline function, rather than an obfuscated macro, make its
scratch space local, rather than global. The previous macro used a dirty
hack (logical AND in place of a conditional) which would lead GCC to throw
a fit (rightly so) as the logical check, as well as the incrementation of
a variable, were not used for anything.
const'ify a few places where gcc3 yells. xstrdup() some global consts in
places where we xstrdup() when not using consts, but tried to assign them
to non-consts before.
Don't use execv(2) if we don't have the kind of arguments it wants.
Reviewed by: asmodai obrien tjr
Submitted by: tjr (a gcc3 build log)
2002-04-20 01:49:10 +00:00
|
|
|
char *pc;
|
|
|
|
for (pc = scommt; *pc; pc++)
|
|
|
|
chrsave(*pc);
|
2002-04-19 17:26:21 +00:00
|
|
|
for(;;) {
|
|
|
|
t = gpbc();
|
|
|
|
if (LOOK_AHEAD(t, ecommt)) {
|
Crank WARNS.
Cast sizeof() to (int), as it's being compared against an int, not a size_t.
If i is changed to a size_t, it means the logic must be slightly changed later
in the flow, where --i is checked to be >= 0. I am not sure I want to make a
logic change to account for clearing up a warning, when an aesthetic one will
keep from modifying the logic.
Other harmless casts, that I think I've made in the right directions.
Make gpbc() an inline function, rather than an obfuscated macro, make its
scratch space local, rather than global. The previous macro used a dirty
hack (logical AND in place of a conditional) which would lead GCC to throw
a fit (rightly so) as the logical check, as well as the incrementation of
a variable, were not used for anything.
const'ify a few places where gcc3 yells. xstrdup() some global consts in
places where we xstrdup() when not using consts, but tried to assign them
to non-consts before.
Don't use execv(2) if we don't have the kind of arguments it wants.
Reviewed by: asmodai obrien tjr
Submitted by: tjr (a gcc3 build log)
2002-04-20 01:49:10 +00:00
|
|
|
for (pc = ecommt; *pc; pc++)
|
|
|
|
chrsave(*pc);
|
2002-04-19 17:26:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (t == EOF)
|
|
|
|
break;
|
|
|
|
CHRSAVE(t);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
CHRSAVE(t); /* stack the char */
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
/*
|
|
|
|
* output string directly, without pushing it for reparses.
|
2002-04-19 17:26:21 +00:00
|
|
|
*/
|
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
outputstr(const char *s)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
|
|
|
if (sp < 0)
|
|
|
|
while (*s)
|
|
|
|
putc(*s++, active);
|
|
|
|
else
|
|
|
|
while (*s)
|
|
|
|
CHRSAVE(*s++);
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* build an input token..
|
|
|
|
* consider only those starting with _ or A-Za-z. This is a
|
|
|
|
* combo with lookup to speed things up.
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static ndptr
|
2002-07-15 02:15:12 +00:00
|
|
|
inspect(int c, char *tp)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-19 17:26:21 +00:00
|
|
|
char *name = tp;
|
|
|
|
char *etp = tp+MAXTOK;
|
|
|
|
ndptr p;
|
|
|
|
unsigned int h;
|
2002-07-15 02:15:12 +00:00
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
h = *tp++ = c;
|
|
|
|
|
|
|
|
while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
|
1994-05-27 12:33:43 +00:00
|
|
|
h = (h << 5) + h + (*tp++ = c);
|
2002-04-19 17:26:21 +00:00
|
|
|
if (c != EOF)
|
|
|
|
PUTBACK(c);
|
1994-05-27 12:33:43 +00:00
|
|
|
*tp = EOS;
|
2002-04-19 17:26:21 +00: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;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
|
|
|
|
if (h == p->hv && STREQ(name, p->name))
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-07-15 02:15:12 +00:00
|
|
|
* initkwds - initialise m4 keywords as fast as possible.
|
1994-05-27 12:33:43 +00:00
|
|
|
* This very similar to install, but without certain overheads,
|
2002-07-15 02:15:12 +00:00
|
|
|
* such as calling lookup. Malloc is not used for storing the
|
2002-04-19 17:26:21 +00:00
|
|
|
* keyword strings, since we simply use the static pointers
|
1994-05-27 12:33:43 +00:00
|
|
|
* within keywrds block.
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2002-07-14 02:03:23 +00:00
|
|
|
initkwds(void)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
unsigned int h;
|
|
|
|
ndptr p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
for (i = 0; i < MAXKEYS; i++) {
|
|
|
|
h = hash(keywrds[i].knam);
|
2002-04-19 17:26:21 +00:00
|
|
|
p = (ndptr) xalloc(sizeof(struct ndblock));
|
|
|
|
p->nxtptr = hashtab[h % HASHSIZE];
|
|
|
|
hashtab[h % HASHSIZE] = p;
|
|
|
|
p->name = xstrdup(keywrds[i].knam);
|
Crank WARNS.
Cast sizeof() to (int), as it's being compared against an int, not a size_t.
If i is changed to a size_t, it means the logic must be slightly changed later
in the flow, where --i is checked to be >= 0. I am not sure I want to make a
logic change to account for clearing up a warning, when an aesthetic one will
keep from modifying the logic.
Other harmless casts, that I think I've made in the right directions.
Make gpbc() an inline function, rather than an obfuscated macro, make its
scratch space local, rather than global. The previous macro used a dirty
hack (logical AND in place of a conditional) which would lead GCC to throw
a fit (rightly so) as the logical check, as well as the incrementation of
a variable, were not used for anything.
const'ify a few places where gcc3 yells. xstrdup() some global consts in
places where we xstrdup() when not using consts, but tried to assign them
to non-consts before.
Don't use execv(2) if we don't have the kind of arguments it wants.
Reviewed by: asmodai obrien tjr
Submitted by: tjr (a gcc3 build log)
2002-04-20 01:49:10 +00:00
|
|
|
p->defn = xstrdup(null);
|
2002-04-19 17:26:21 +00: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 */
|
2002-07-15 02:15:12 +00:00
|
|
|
int
|
2002-05-01 21:37:29 +00:00
|
|
|
builtin_type(const char *key)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i != MAXKEYS; i++)
|
|
|
|
if (STREQ(keywrds[i].knam, key))
|
|
|
|
return keywrds[i].ktyp;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
Crank WARNS.
Cast sizeof() to (int), as it's being compared against an int, not a size_t.
If i is changed to a size_t, it means the logic must be slightly changed later
in the flow, where --i is checked to be >= 0. I am not sure I want to make a
logic change to account for clearing up a warning, when an aesthetic one will
keep from modifying the logic.
Other harmless casts, that I think I've made in the right directions.
Make gpbc() an inline function, rather than an obfuscated macro, make its
scratch space local, rather than global. The previous macro used a dirty
hack (logical AND in place of a conditional) which would lead GCC to throw
a fit (rightly so) as the logical check, as well as the incrementation of
a variable, were not used for anything.
const'ify a few places where gcc3 yells. xstrdup() some global consts in
places where we xstrdup() when not using consts, but tried to assign them
to non-consts before.
Don't use execv(2) if we don't have the kind of arguments it wants.
Reviewed by: asmodai obrien tjr
Submitted by: tjr (a gcc3 build log)
2002-04-20 01:49:10 +00:00
|
|
|
const char *
|
2002-05-01 21:37:29 +00:00
|
|
|
builtin_realname(int n)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i != MAXKEYS; i++)
|
|
|
|
if (((keywrds[i].ktyp ^ n) & TYPEMASK) == 0)
|
|
|
|
return keywrds[i].knam;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
record(struct position *t, int lev)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
|
|
|
if (lev < MAXRECORD) {
|
|
|
|
t[lev].name = CURRENT_NAME;
|
|
|
|
t[lev].line = CURRENT_LINE;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
2002-04-17 17:26:32 +00:00
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
dump_stack(struct position *t, int lev)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < lev; i++) {
|
|
|
|
if (i == MAXRECORD) {
|
|
|
|
fprintf(stderr, " ...\n");
|
|
|
|
break;
|
|
|
|
}
|
2002-07-15 02:15:12 +00:00
|
|
|
fprintf(stderr, " %s at line %lu\n",
|
2002-04-19 17:26:21 +00:00
|
|
|
t[i].name, t[i].line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
static void
|
2002-07-14 02:03:23 +00:00
|
|
|
enlarge_stack(void)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
|
|
|
STACKMAX *= 2;
|
|
|
|
mstack = realloc(mstack, sizeof(stae) * STACKMAX);
|
|
|
|
sstack = realloc(sstack, STACKMAX);
|
|
|
|
if (mstack == NULL || sstack == NULL)
|
2002-07-15 02:15:12 +00:00
|
|
|
errx(1, "Evaluation stack overflow (%lu)",
|
2002-04-19 17:26:21 +00:00
|
|
|
(unsigned long)STACKMAX);
|
|
|
|
}
|
|
|
|
|
2002-04-17 17:26:32 +00:00
|
|
|
/* Emit preprocessor #line directive if -s option used. */
|
|
|
|
void
|
|
|
|
emitline(void)
|
|
|
|
{
|
2002-04-19 17:26:21 +00:00
|
|
|
|
2002-04-17 17:26:32 +00:00
|
|
|
if (synccpp)
|
|
|
|
fprintf(active, "#line %d \"%s\"\n", inlineno[ilevel],
|
2002-04-19 17:26:21 +00:00
|
|
|
inname[ilevel]);
|
2002-04-17 17:26:32 +00:00
|
|
|
}
|