1989-02-07 11:04:05 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
|
|
|
/* $Header$ */
|
|
|
|
/* PREPROCESSOR: CONTROLLINE INTERPRETER */
|
|
|
|
|
1990-01-10 17:33:35 +00:00
|
|
|
#include "debug.h"
|
1989-02-07 11:04:05 +00:00
|
|
|
#include "arith.h"
|
|
|
|
#include "LLlex.h"
|
|
|
|
#include "Lpars.h"
|
|
|
|
#include "idf.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "nopp.h"
|
|
|
|
|
|
|
|
#ifndef NOPP
|
1989-11-22 13:58:36 +00:00
|
|
|
#include "ifdepth.h"
|
|
|
|
#include "botch_free.h"
|
|
|
|
#include "nparams.h"
|
|
|
|
#include "parbufsize.h"
|
|
|
|
#include "textsize.h"
|
1989-02-07 11:04:05 +00:00
|
|
|
#include "idfsize.h"
|
|
|
|
#include "assert.h"
|
|
|
|
#include <alloc.h>
|
|
|
|
#include "class.h"
|
|
|
|
#include "macro.h"
|
1989-11-22 13:58:36 +00:00
|
|
|
#include "macbuf.h"
|
|
|
|
#include "replace.h"
|
1990-10-19 11:50:32 +00:00
|
|
|
#include "dbsymtab.h"
|
|
|
|
#ifdef DBSYMTAB
|
|
|
|
#include <stb.h>
|
|
|
|
int IncludeLevel = 0;
|
|
|
|
#endif
|
1989-02-07 11:04:05 +00:00
|
|
|
|
1989-10-27 13:33:10 +00:00
|
|
|
extern char options[];
|
1989-02-07 11:04:05 +00:00
|
|
|
extern char **inctable; /* list of include directories */
|
|
|
|
extern char *getwdir();
|
|
|
|
char ifstack[IFDEPTH]; /* if-stack: the content of an entry is */
|
|
|
|
/* 1 if a corresponding ELSE has been */
|
|
|
|
/* encountered. */
|
|
|
|
|
|
|
|
int nestlevel = -1;
|
|
|
|
|
|
|
|
struct idf *
|
1989-09-19 16:13:23 +00:00
|
|
|
GetIdentifier(skiponerr)
|
|
|
|
int skiponerr; /* skip the rest of the line on error */
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* returns a pointer to the descriptor of the identifier that is
|
1989-09-19 16:13:23 +00:00
|
|
|
read from the input stream. When the input doe not contain
|
1989-09-29 16:20:38 +00:00
|
|
|
an identifier, the rest of the line is skipped when
|
|
|
|
skiponerr is on, and a null-pointer is returned.
|
1989-02-07 11:04:05 +00:00
|
|
|
The substitution of macros is disabled.
|
|
|
|
*/
|
1989-09-19 16:13:23 +00:00
|
|
|
int tmp = UnknownIdIsZero;
|
1989-02-07 11:04:05 +00:00
|
|
|
int tok;
|
|
|
|
struct token tk;
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
UnknownIdIsZero = ReplaceMacros = 0;
|
1989-02-07 11:04:05 +00:00
|
|
|
tok = GetToken(&tk);
|
|
|
|
ReplaceMacros = 1;
|
1989-09-19 16:13:23 +00:00
|
|
|
UnknownIdIsZero = tmp;
|
|
|
|
if (tok != IDENTIFIER) {
|
1989-10-18 13:12:31 +00:00
|
|
|
if (skiponerr && tok != EOI) SkipToNewLine();
|
1989-09-19 16:13:23 +00:00
|
|
|
return (struct idf *)0;
|
|
|
|
}
|
|
|
|
return tk.tk_idf;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* domacro() is the control line interpreter. The '#' has already
|
|
|
|
been read by the lexical analyzer by which domacro() is called.
|
|
|
|
The token appearing directly after the '#' is obtained by calling
|
|
|
|
the basic lexical analyzing function GetToken() and is interpreted
|
|
|
|
to perform the action belonging to that token.
|
|
|
|
An error message is produced when the token is not recognized,
|
|
|
|
i.e. it is not one of "define" .. "undef" , integer or newline.
|
|
|
|
*/
|
|
|
|
domacro()
|
|
|
|
{
|
|
|
|
struct token tk; /* the token itself */
|
1989-09-19 16:13:23 +00:00
|
|
|
int toknum;
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
EoiForNewline = 1;
|
1989-09-19 16:13:23 +00:00
|
|
|
ReplaceMacros = 0;
|
|
|
|
toknum = GetToken(&tk);
|
|
|
|
ReplaceMacros = 1;
|
|
|
|
switch(toknum) { /* select control line action */
|
1989-02-07 11:04:05 +00:00
|
|
|
case IDENTIFIER: /* is it a macro keyword? */
|
|
|
|
switch (tk.tk_idf->id_resmac) {
|
|
|
|
case K_DEFINE: /* "define" */
|
|
|
|
do_define();
|
|
|
|
break;
|
|
|
|
case K_ELIF: /* "elif" */
|
|
|
|
do_elif();
|
|
|
|
break;
|
|
|
|
case K_ELSE: /* "else" */
|
|
|
|
do_else();
|
|
|
|
break;
|
|
|
|
case K_ENDIF: /* "endif" */
|
|
|
|
do_endif();
|
|
|
|
break;
|
|
|
|
case K_IF: /* "if" */
|
|
|
|
do_if();
|
|
|
|
break;
|
|
|
|
case K_IFDEF: /* "ifdef" */
|
|
|
|
do_ifdef(1);
|
|
|
|
break;
|
|
|
|
case K_IFNDEF: /* "ifndef" */
|
|
|
|
do_ifdef(0);
|
|
|
|
break;
|
|
|
|
case K_INCLUDE: /* "include" */
|
|
|
|
do_include();
|
|
|
|
break;
|
|
|
|
case K_LINE: /* "line" */
|
|
|
|
/* set LineNumber and FileName according to
|
|
|
|
the arguments.
|
|
|
|
*/
|
|
|
|
if (GetToken(&tk) != INTEGER) {
|
1991-03-01 13:51:37 +00:00
|
|
|
lexerror("bad #line syntax");
|
1989-10-18 13:12:31 +00:00
|
|
|
SkipToNewLine();
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
do_line((unsigned int)tk.tk_ival);
|
|
|
|
break;
|
|
|
|
case K_ERROR: /* "error" */
|
|
|
|
do_error();
|
|
|
|
break;
|
|
|
|
case K_PRAGMA: /* "pragma" */
|
|
|
|
do_pragma();
|
|
|
|
break;
|
|
|
|
case K_UNDEF: /* "undef" */
|
1990-04-18 16:40:17 +00:00
|
|
|
do_undef((struct idf *) 0);
|
1989-02-07 11:04:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* invalid word seen after the '#' */
|
|
|
|
lexerror("%s: unknown control", tk.tk_idf->id_text);
|
1989-10-18 13:12:31 +00:00
|
|
|
SkipToNewLine();
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case INTEGER: /* # <integer> [<filespecifier>]? */
|
|
|
|
do_line((unsigned int)tk.tk_ival);
|
|
|
|
break;
|
|
|
|
case EOI: /* only `#' on this line: do nothing, ignore */
|
|
|
|
break;
|
|
|
|
default: /* invalid token following '#' */
|
|
|
|
lexerror("illegal # line");
|
1989-10-18 13:12:31 +00:00
|
|
|
SkipToNewLine();
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
EoiForNewline = 0;
|
|
|
|
}
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
skip_block(to_endif)
|
|
|
|
int to_endif;
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* skip_block() skips the input from
|
|
|
|
1) a false #if, #ifdef, #ifndef or #elif until the
|
|
|
|
corresponding #elif (resulting in true), #else or
|
|
|
|
#endif is read.
|
|
|
|
2) a #else corresponding to a true #if, #ifdef,
|
|
|
|
#ifndef or #elif until the corresponding #endif is
|
|
|
|
seen.
|
|
|
|
*/
|
|
|
|
register int ch;
|
|
|
|
register int skiplevel = nestlevel; /* current nesting level */
|
|
|
|
struct token tk;
|
1989-09-19 16:13:23 +00:00
|
|
|
int toknum;
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
NoUnstack++;
|
|
|
|
for (;;) {
|
|
|
|
ch = GetChar(); /* read first character after newline */
|
|
|
|
while (class(ch) == STSKIP)
|
|
|
|
ch = GetChar();
|
|
|
|
if (ch != '#') {
|
|
|
|
if (ch == EOI) {
|
|
|
|
NoUnstack--;
|
|
|
|
return;
|
|
|
|
}
|
1989-11-22 13:58:36 +00:00
|
|
|
/* A possible '/' is not pushed back */
|
|
|
|
if (ch == '/') {
|
|
|
|
ch = GetChar();
|
|
|
|
if (ch != '*') UnGetChar();
|
|
|
|
else {
|
|
|
|
skipcomment();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else UnGetChar();
|
1989-10-18 13:12:31 +00:00
|
|
|
SkipToNewLine();
|
1989-02-07 11:04:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
1989-09-19 16:13:23 +00:00
|
|
|
ReplaceMacros = 0;
|
|
|
|
toknum = GetToken(&tk);
|
|
|
|
ReplaceMacros = 1;
|
|
|
|
if (toknum != IDENTIFIER) {
|
1990-12-10 11:17:34 +00:00
|
|
|
if (toknum != INTEGER) {
|
|
|
|
lexerror("illegal # line");
|
|
|
|
}
|
1989-10-18 13:12:31 +00:00
|
|
|
SkipToNewLine();
|
1989-02-07 11:04:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* an IDENTIFIER: look for #if, #ifdef and #ifndef
|
|
|
|
without interpreting them.
|
|
|
|
Interpret #else, #elif and #endif if they occur
|
|
|
|
on the same level.
|
|
|
|
*/
|
|
|
|
switch(tk.tk_idf->id_resmac) {
|
1989-09-19 16:13:23 +00:00
|
|
|
default:
|
1990-09-03 13:46:30 +00:00
|
|
|
case K_UNKNOWN:
|
|
|
|
/* invalid word seen after the '#' */
|
1990-12-10 11:17:34 +00:00
|
|
|
lexwarning("%s: unknown control", tk.tk_idf->id_text);
|
1990-09-03 13:46:30 +00:00
|
|
|
/* fallthrough */
|
|
|
|
case K_DEFINE:
|
|
|
|
case K_ERROR:
|
|
|
|
case K_INCLUDE:
|
|
|
|
case K_LINE:
|
|
|
|
case K_PRAGMA:
|
|
|
|
case K_UNDEF:
|
|
|
|
case K_FILE:
|
1989-10-18 13:12:31 +00:00
|
|
|
SkipToNewLine();
|
1989-09-19 16:13:23 +00:00
|
|
|
break;
|
1989-02-07 11:04:05 +00:00
|
|
|
case K_IF:
|
|
|
|
case K_IFDEF:
|
|
|
|
case K_IFNDEF:
|
|
|
|
push_if();
|
1989-10-18 13:12:31 +00:00
|
|
|
SkipToNewLine();
|
1989-02-07 11:04:05 +00:00
|
|
|
break;
|
|
|
|
case K_ELIF:
|
1989-09-19 16:13:23 +00:00
|
|
|
if (ifstack[nestlevel])
|
|
|
|
lexerror("#elif after #else");
|
|
|
|
if (!to_endif && nestlevel == skiplevel) {
|
1989-02-07 11:04:05 +00:00
|
|
|
nestlevel--;
|
|
|
|
push_if();
|
|
|
|
if (ifexpr()) {
|
|
|
|
NoUnstack--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1989-10-18 13:12:31 +00:00
|
|
|
else SkipToNewLine(); /* otherwise done in ifexpr() */
|
1989-02-07 11:04:05 +00:00
|
|
|
break;
|
|
|
|
case K_ELSE:
|
1989-09-19 16:13:23 +00:00
|
|
|
if (ifstack[nestlevel])
|
|
|
|
lexerror("#else after #else");
|
1989-02-07 11:04:05 +00:00
|
|
|
++(ifstack[nestlevel]);
|
1989-09-19 16:13:23 +00:00
|
|
|
if (!to_endif && nestlevel == skiplevel) {
|
1989-11-22 13:58:36 +00:00
|
|
|
if (SkipToNewLine()) {
|
1989-10-27 13:33:10 +00:00
|
|
|
if (!options['o'])
|
1989-11-08 16:52:34 +00:00
|
|
|
lexstrict("garbage following #else");
|
1989-11-22 13:58:36 +00:00
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
NoUnstack--;
|
|
|
|
return;
|
|
|
|
}
|
1989-10-18 13:12:31 +00:00
|
|
|
else SkipToNewLine();
|
1989-02-07 11:04:05 +00:00
|
|
|
break;
|
|
|
|
case K_ENDIF:
|
|
|
|
ASSERT(nestlevel > nestlow);
|
|
|
|
if (nestlevel == skiplevel) {
|
1989-11-22 13:58:36 +00:00
|
|
|
if (SkipToNewLine()) {
|
1989-10-27 13:33:10 +00:00
|
|
|
if (!options['o'])
|
1989-11-08 16:52:34 +00:00
|
|
|
lexstrict("garbage following #endif");
|
1989-11-22 13:58:36 +00:00
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
nestlevel--;
|
|
|
|
NoUnstack--;
|
|
|
|
return;
|
|
|
|
}
|
1989-10-18 13:12:31 +00:00
|
|
|
else SkipToNewLine();
|
1989-02-07 11:04:05 +00:00
|
|
|
nestlevel--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ifexpr()
|
|
|
|
{
|
|
|
|
/* ifexpr() returns whether the restricted constant
|
|
|
|
expression following #if or #elif evaluates to true. This
|
|
|
|
is done by calling the LLgen generated subparser for
|
|
|
|
constant expressions. The result of this expression will
|
|
|
|
be given in the extern long variable "ifval".
|
|
|
|
*/
|
|
|
|
extern arith ifval;
|
|
|
|
int errors = err_occurred;
|
|
|
|
|
|
|
|
ifval = (arith)0;
|
|
|
|
AccDefined = 1;
|
|
|
|
UnknownIdIsZero = 1;
|
|
|
|
PushLex(); /* NEW parser */
|
|
|
|
If_expr(); /* invoke constant expression parser */
|
|
|
|
PopLex(); /* OLD parser */
|
|
|
|
AccDefined = 0;
|
|
|
|
UnknownIdIsZero = 0;
|
|
|
|
return (errors == err_occurred) && (ifval != (arith)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
do_include()
|
|
|
|
{
|
|
|
|
/* do_include() performs the inclusion of a file.
|
|
|
|
*/
|
|
|
|
char *filenm;
|
|
|
|
char *result;
|
|
|
|
int tok;
|
|
|
|
struct token tk;
|
|
|
|
|
|
|
|
AccFileSpecifier = 1;
|
|
|
|
if (((tok = GetToken(&tk)) == FILESPECIFIER) || tok == STRING)
|
|
|
|
filenm = tk.tk_bts;
|
|
|
|
else {
|
1991-03-01 13:51:37 +00:00
|
|
|
lexerror("bad include syntax");
|
1989-02-07 11:04:05 +00:00
|
|
|
filenm = (char *)0;
|
|
|
|
}
|
|
|
|
AccFileSpecifier = 0;
|
1991-01-03 13:45:22 +00:00
|
|
|
if (SkipToNewLine()) {
|
1991-03-01 13:51:37 +00:00
|
|
|
lexerror("bad include syntax");
|
1991-01-03 13:45:22 +00:00
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
inctable[0] = WorkingDir;
|
|
|
|
if (filenm) {
|
|
|
|
if (!InsertFile(filenm, &inctable[tok==FILESPECIFIER],&result)){
|
1991-03-01 13:51:37 +00:00
|
|
|
lexerror("cannot open include file \"%s\"", filenm);
|
1990-06-08 10:03:47 +00:00
|
|
|
add_dependency(filenm);
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
else {
|
1990-06-08 10:03:47 +00:00
|
|
|
add_dependency(result);
|
1989-02-07 11:04:05 +00:00
|
|
|
WorkingDir = getwdir(result);
|
|
|
|
File_Inserted = 1;
|
|
|
|
FileName = result;
|
|
|
|
LineNumber = 0;
|
|
|
|
nestlow = nestlevel;
|
1990-10-19 11:50:32 +00:00
|
|
|
#ifdef DBSYMTAB
|
|
|
|
IncludeLevel++;
|
|
|
|
if (options['g']) {
|
|
|
|
C_ms_std(FileName, N_BINCL, 0);
|
|
|
|
}
|
|
|
|
#endif /* DBSYMTAB */
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do_define()
|
|
|
|
{
|
|
|
|
/* do_define() interprets a #define control line.
|
|
|
|
*/
|
|
|
|
struct idf *id; /* the #defined identifier's descriptor */
|
|
|
|
int nformals = -1; /* keep track of the number of formals */
|
|
|
|
char *formals[NPARAMS]; /* pointers to the names of the formals */
|
|
|
|
char parbuf[PARBUFSIZE]; /* names of formals */
|
|
|
|
char *repl_text; /* start of the replacement text */
|
|
|
|
int length; /* length of the replacement text */
|
|
|
|
register ch;
|
|
|
|
char *get_text();
|
|
|
|
|
|
|
|
/* read the #defined macro's name */
|
1989-09-19 16:13:23 +00:00
|
|
|
if (!(id = GetIdentifier(1))) {
|
|
|
|
lexerror("illegal #define line");
|
1989-02-07 11:04:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* there is a formal parameter list if the identifier is
|
1989-11-22 13:58:36 +00:00
|
|
|
followed immediately by a '('.
|
1989-02-07 11:04:05 +00:00
|
|
|
*/
|
|
|
|
ch = GetChar();
|
|
|
|
if (ch == '(') {
|
|
|
|
if ((nformals = getparams(formals, parbuf)) == -1) {
|
1989-10-18 13:12:31 +00:00
|
|
|
SkipToNewLine();
|
1989-02-07 11:04:05 +00:00
|
|
|
return; /* an error occurred */
|
|
|
|
}
|
|
|
|
ch = GetChar();
|
|
|
|
}
|
|
|
|
/* read the replacement text if there is any */
|
|
|
|
ch = skipspaces(ch,0); /* find first character of the text */
|
|
|
|
ASSERT(ch != EOI);
|
1990-01-05 09:28:54 +00:00
|
|
|
/* UngetChar() is not right when replacement starts with a '/' */
|
|
|
|
ChPushBack(ch);
|
1989-11-22 13:58:36 +00:00
|
|
|
repl_text = get_text((nformals > 0) ? formals : 0, &length);
|
1989-02-07 11:04:05 +00:00
|
|
|
macro_def(id, repl_text, nformals, length, NOFLAG);
|
|
|
|
LineNumber++;
|
|
|
|
}
|
|
|
|
|
|
|
|
push_if()
|
|
|
|
{
|
|
|
|
if (nestlevel >= IFDEPTH)
|
|
|
|
fatal("too many nested #if/#ifdef/#ifndef");
|
|
|
|
else
|
|
|
|
ifstack[++nestlevel] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_elif()
|
|
|
|
{
|
1989-09-19 16:13:23 +00:00
|
|
|
if (nestlevel <= nestlow) {
|
1989-02-07 11:04:05 +00:00
|
|
|
lexerror("#elif without corresponding #if");
|
1989-10-18 13:12:31 +00:00
|
|
|
SkipToNewLine();
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
1989-09-19 16:13:23 +00:00
|
|
|
else { /* restart at this level as if a #if is detected. */
|
|
|
|
if (ifstack[nestlevel]) {
|
|
|
|
lexerror("#elif after #else");
|
1989-10-18 13:12:31 +00:00
|
|
|
SkipToNewLine();
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
nestlevel--;
|
|
|
|
push_if();
|
1989-09-19 16:13:23 +00:00
|
|
|
skip_block(1);
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do_else()
|
|
|
|
{
|
1989-10-18 13:12:31 +00:00
|
|
|
if (SkipToNewLine())
|
1989-10-27 13:33:10 +00:00
|
|
|
if (!options['o'])
|
1989-11-08 16:52:34 +00:00
|
|
|
lexstrict("garbage following #else");
|
1989-09-19 16:13:23 +00:00
|
|
|
if (nestlevel <= nestlow)
|
1989-02-07 11:04:05 +00:00
|
|
|
lexerror("#else without corresponding #if");
|
|
|
|
else { /* mark this level as else-d */
|
1989-09-19 16:13:23 +00:00
|
|
|
if (ifstack[nestlevel]) {
|
|
|
|
lexerror("#else after #else");
|
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
++(ifstack[nestlevel]);
|
1989-09-19 16:13:23 +00:00
|
|
|
skip_block(1);
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do_endif()
|
|
|
|
{
|
1989-11-22 13:58:36 +00:00
|
|
|
if (SkipToNewLine()) {
|
1989-10-27 13:33:10 +00:00
|
|
|
if (!options['o'])
|
1989-11-08 16:52:34 +00:00
|
|
|
lexstrict("garbage following #endif");
|
1989-11-22 13:58:36 +00:00
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
if (nestlevel <= nestlow) {
|
|
|
|
lexerror("#endif without corresponding #if");
|
|
|
|
}
|
|
|
|
else nestlevel--;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_if()
|
|
|
|
{
|
|
|
|
push_if();
|
|
|
|
if (!ifexpr()) /* a false #if/#elif expression */
|
1989-09-19 16:13:23 +00:00
|
|
|
skip_block(0);
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do_ifdef(how)
|
|
|
|
{
|
|
|
|
register struct idf *id;
|
|
|
|
|
|
|
|
/* how == 1 : ifdef; how == 0 : ifndef
|
|
|
|
*/
|
|
|
|
push_if();
|
1989-09-19 16:13:23 +00:00
|
|
|
if (!(id = GetIdentifier(1)))
|
1989-02-07 11:04:05 +00:00
|
|
|
lexerror("illegal #ifdef construction");
|
1992-05-26 18:09:11 +00:00
|
|
|
else if (SkipToNewLine())
|
|
|
|
if (!options['o'])
|
|
|
|
lexstrict("garbage following #%s <identifier>",
|
|
|
|
how ? "ifdef" : "ifndef");
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
/* The next test is a shorthand for:
|
|
|
|
(how && !id->id_macro) || (!how && id->id_macro)
|
|
|
|
*/
|
|
|
|
if (how ^ (id && id->id_macro != 0))
|
1989-09-19 16:13:23 +00:00
|
|
|
skip_block(0);
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
|
1990-04-18 16:40:17 +00:00
|
|
|
/* argidf != NULL when the undef came from a -U option */
|
|
|
|
do_undef(argidf)
|
|
|
|
struct idf *argidf;
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
1990-04-18 16:40:17 +00:00
|
|
|
register struct idf *id = argidf;
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
/* Forget a macro definition. */
|
1990-04-18 16:40:17 +00:00
|
|
|
if (id || (id = GetIdentifier(1))) {
|
1989-02-07 11:04:05 +00:00
|
|
|
if (id->id_macro) { /* forget the macro */
|
1989-09-19 16:13:23 +00:00
|
|
|
if (id->id_macro->mc_flag & NOUNDEF) {
|
|
|
|
lexerror("it is not allowed to undef %s", id->id_text);
|
|
|
|
} else {
|
1989-09-29 16:20:38 +00:00
|
|
|
free(id->id_macro->mc_text);
|
1989-09-19 16:13:23 +00:00
|
|
|
free_macro(id->id_macro);
|
|
|
|
id->id_macro = (struct macro *) 0;
|
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
} /* else: don't complain */
|
1991-12-16 14:06:09 +00:00
|
|
|
if (!argidf) {
|
|
|
|
if (SkipToNewLine())
|
|
|
|
if (!options['o'])
|
|
|
|
lexstrict("garbage following #undef");
|
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
lexerror("illegal #undef construction");
|
|
|
|
}
|
|
|
|
|
|
|
|
do_error()
|
|
|
|
{
|
1991-04-11 10:52:52 +00:00
|
|
|
int len;
|
|
|
|
char *get_text();
|
|
|
|
char *bp = get_text((char **) 0, &len);
|
1989-02-07 11:04:05 +00:00
|
|
|
|
1991-04-11 10:52:52 +00:00
|
|
|
lexerror("user error: %s", bp);
|
|
|
|
free(bp);
|
|
|
|
LineNumber++;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
getparams(buf, parbuf)
|
|
|
|
char *buf[];
|
|
|
|
char parbuf[];
|
|
|
|
{
|
|
|
|
/* getparams() reads the formal parameter list of a macro
|
|
|
|
definition.
|
|
|
|
The number of parameters is returned.
|
|
|
|
As a formal parameter list is expected when calling this
|
|
|
|
routine, -1 is returned if an error is detected, for
|
|
|
|
example:
|
|
|
|
#define one(1), where 1 is not an identifier.
|
|
|
|
Note that the '(' has already been eaten.
|
|
|
|
The names of the formal parameters are stored into parbuf.
|
|
|
|
*/
|
|
|
|
register char **pbuf = &buf[0];
|
|
|
|
register int c;
|
|
|
|
register char *ptr = &parbuf[0];
|
|
|
|
register char **pbuf2;
|
|
|
|
|
|
|
|
c = GetChar();
|
|
|
|
c = skipspaces(c,0);
|
|
|
|
if (c == ')') { /* no parameters: #define name() */
|
|
|
|
*pbuf = (char *) 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (;;) { /* eat the formal parameter list */
|
|
|
|
if (class(c) != STIDF && class(c) != STELL) {
|
|
|
|
lexerror("#define: bad formal parameter");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*pbuf = ptr; /* name of the formal */
|
|
|
|
*ptr++ = c;
|
|
|
|
if (ptr >= &parbuf[PARBUFSIZE])
|
|
|
|
fatal("formal parameter buffer overflow");
|
|
|
|
do { /* eat the identifier name */
|
|
|
|
c = GetChar();
|
|
|
|
*ptr++ = c;
|
|
|
|
if (ptr >= &parbuf[PARBUFSIZE])
|
|
|
|
fatal("formal parameter buffer overflow");
|
|
|
|
} while (in_idf(c));
|
|
|
|
*(ptr - 1) = '\0'; /* mark end of the name */
|
|
|
|
|
|
|
|
/* Check if this formal parameter is already used.
|
|
|
|
Usually, macros do not have many parameters, so ...
|
|
|
|
*/
|
|
|
|
for (pbuf2 = pbuf - 1; pbuf2 >= &buf[0]; pbuf2--) {
|
|
|
|
if (!strcmp(*pbuf2, *pbuf)) {
|
1991-03-01 13:51:37 +00:00
|
|
|
lexerror("formal parameter \"%s\" already used",
|
1989-02-07 11:04:05 +00:00
|
|
|
*pbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pbuf++;
|
|
|
|
c = skipspaces(c,0);
|
|
|
|
if (c == ')') { /* end of the formal parameter list */
|
|
|
|
*pbuf = (char *) 0;
|
|
|
|
return pbuf - buf;
|
|
|
|
}
|
|
|
|
if (c != ',') {
|
|
|
|
lexerror("#define: bad formal parameter list");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
c = GetChar();
|
|
|
|
c = skipspaces(c,0);
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_def(id, text, nformals, length, flags)
|
|
|
|
register struct idf *id;
|
|
|
|
char *text;
|
|
|
|
{
|
|
|
|
register struct macro *newdef = id->id_macro;
|
|
|
|
|
|
|
|
/* macro_def() puts the contents and information of a macro
|
|
|
|
definition into a structure and stores it into the symbol
|
|
|
|
table entry belonging to the name of the macro.
|
1989-09-19 16:13:23 +00:00
|
|
|
An error is given if there was already a definition
|
1989-02-07 11:04:05 +00:00
|
|
|
*/
|
|
|
|
if (newdef) { /* is there a redefinition? */
|
1989-09-19 16:13:23 +00:00
|
|
|
if (newdef->mc_flag & NOUNDEF) {
|
|
|
|
lexerror("it is not allowed to redefine %s", id->id_text);
|
|
|
|
} else if (!macroeq(newdef->mc_text, text))
|
|
|
|
lexerror("illegal redefine of \"%s\"", id->id_text);
|
|
|
|
free(text);
|
|
|
|
return;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
1989-09-19 16:13:23 +00:00
|
|
|
id->id_macro = newdef = new_macro();
|
1989-02-07 11:04:05 +00:00
|
|
|
newdef->mc_text = text; /* replacement text */
|
|
|
|
newdef->mc_nps = nformals; /* nr of formals */
|
|
|
|
newdef->mc_length = length; /* length of repl. text */
|
|
|
|
newdef->mc_flag = flags; /* special flags */
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
find_name(nm, index)
|
|
|
|
char *nm, *index[];
|
|
|
|
{
|
|
|
|
/* find_name() returns the index of "nm" in the namelist
|
1989-11-22 13:58:36 +00:00
|
|
|
"index" if it can be found there. 0 is returned if it is
|
1989-02-07 11:04:05 +00:00
|
|
|
not there.
|
|
|
|
*/
|
|
|
|
register char **ip = &index[0];
|
|
|
|
|
|
|
|
while (*ip)
|
|
|
|
if (strcmp(nm, *ip++) == 0)
|
|
|
|
return ip - &index[0];
|
|
|
|
/* arrived here, nm is not in the name list. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1989-11-22 13:58:36 +00:00
|
|
|
#define BLANK(ch) ((ch == ' ') || (ch == '\t'))
|
|
|
|
|
1989-02-07 11:04:05 +00:00
|
|
|
char *
|
|
|
|
get_text(formals, length)
|
|
|
|
char *formals[];
|
|
|
|
int *length;
|
|
|
|
{
|
|
|
|
/* get_text() copies the replacement text of a macro
|
|
|
|
definition with zero, one or more parameters, thereby
|
|
|
|
substituting each formal parameter by a special character
|
|
|
|
(non-ascii: 0200 & (order-number in the formal parameter
|
|
|
|
list)) in order to substitute this character later by the
|
1989-11-22 13:58:36 +00:00
|
|
|
actual parameter. The replacement text is copied into
|
1989-02-07 11:04:05 +00:00
|
|
|
itself because the copied text will contain fewer or the
|
1989-11-22 13:58:36 +00:00
|
|
|
same amount of characters. The length of the replacement
|
1989-02-07 11:04:05 +00:00
|
|
|
text is returned.
|
|
|
|
|
|
|
|
Implementation:
|
1989-11-22 13:58:36 +00:00
|
|
|
finite automaton : we are interested in
|
|
|
|
1- white space, sequences must be mapped onto 1 single
|
|
|
|
blank.
|
|
|
|
2- identifiers, since they might be replaced by some
|
|
|
|
actual parameter.
|
|
|
|
3- strings and character constants, since replacing
|
|
|
|
variables within them is illegal, and white-space is
|
|
|
|
significant.
|
|
|
|
4- comment, same as for 1
|
|
|
|
Other tokens will not be seen as such.
|
1989-02-07 11:04:05 +00:00
|
|
|
*/
|
|
|
|
register int c;
|
1989-11-22 13:58:36 +00:00
|
|
|
struct repl repls;
|
|
|
|
register struct repl *repl = &repls;
|
|
|
|
int blank = 0;
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
c = GetChar();
|
|
|
|
|
1989-11-22 13:58:36 +00:00
|
|
|
repl->r_ptr = repl->r_text = Malloc(repl->r_size = ITEXTSIZE);
|
1989-11-27 11:37:11 +00:00
|
|
|
*repl->r_ptr = '\0';
|
1989-02-07 11:04:05 +00:00
|
|
|
while ((c != EOI) && (class(c) != STNL)) {
|
1989-11-22 13:58:36 +00:00
|
|
|
if (BLANK(c)) {
|
|
|
|
if (!blank++) add2repl(repl, ' ');
|
|
|
|
c = GetChar();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1989-02-07 11:04:05 +00:00
|
|
|
if (c == '\'' || c == '"') {
|
|
|
|
register int delim = c;
|
|
|
|
|
|
|
|
do {
|
1989-11-22 13:58:36 +00:00
|
|
|
add2repl(repl, c);
|
|
|
|
if (c == '\\') add2repl(repl, GetChar());
|
1989-09-19 16:13:23 +00:00
|
|
|
c = GetChar();
|
1989-02-07 11:04:05 +00:00
|
|
|
} while (c != delim && c != EOI && class(c) != STNL);
|
1991-04-11 10:52:52 +00:00
|
|
|
if (c == EOI || class(c) == STNL) {
|
|
|
|
lexstrict("unclosed opening %c", delim);
|
|
|
|
break;
|
|
|
|
}
|
1989-11-22 13:58:36 +00:00
|
|
|
add2repl(repl, c);
|
1989-02-07 11:04:05 +00:00
|
|
|
c = GetChar();
|
1989-11-22 13:58:36 +00:00
|
|
|
} else if (c == '/') {
|
1989-02-07 11:04:05 +00:00
|
|
|
c = GetChar();
|
|
|
|
if (c == '*') {
|
|
|
|
skipcomment();
|
1990-01-05 09:28:54 +00:00
|
|
|
if (!blank++) add2repl(repl, ' ');
|
1989-02-07 11:04:05 +00:00
|
|
|
c = GetChar();
|
1989-11-22 13:58:36 +00:00
|
|
|
continue;
|
1990-01-05 09:28:54 +00:00
|
|
|
} else add2repl(repl, '/');
|
1989-11-22 13:58:36 +00:00
|
|
|
} else if (formals
|
|
|
|
&& (class(c) == STIDF || class(c) == STELL)) {
|
1989-02-07 11:04:05 +00:00
|
|
|
char id_buf[IDFSIZE + 1];
|
1989-11-22 13:58:36 +00:00
|
|
|
register char *idp = id_buf;
|
|
|
|
int n;
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
/* read identifier: it may be a formal parameter */
|
1989-11-22 13:58:36 +00:00
|
|
|
*idp++ = c;
|
1989-02-07 11:04:05 +00:00
|
|
|
do {
|
|
|
|
c = GetChar();
|
1989-11-22 13:58:36 +00:00
|
|
|
if (idp <= &id_buf[IDFSIZE])
|
|
|
|
*idp++ = c;
|
1989-02-07 11:04:05 +00:00
|
|
|
} while (in_idf(c));
|
1989-11-22 13:58:36 +00:00
|
|
|
*--idp = '\0';
|
|
|
|
/* construct the formal parameter mark or identifier */
|
|
|
|
if (n = find_name(id_buf, formals))
|
|
|
|
add2repl(repl, FORMALP | (char) n);
|
1989-02-07 11:04:05 +00:00
|
|
|
else {
|
1989-11-22 13:58:36 +00:00
|
|
|
idp = id_buf;
|
|
|
|
while (*idp) add2repl(repl, *idp++);
|
|
|
|
}
|
|
|
|
} else if (class(c) == STNUM) {
|
|
|
|
add2repl(repl, c);
|
|
|
|
if (c == '.') {
|
|
|
|
c = GetChar();
|
|
|
|
if (class(c) != STNUM) {
|
|
|
|
blank = 0; continue;
|
|
|
|
}
|
|
|
|
add2repl(repl, c);
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
1989-11-22 13:58:36 +00:00
|
|
|
c = GetChar();
|
|
|
|
while(in_idf(c) || c == '.') {
|
|
|
|
add2repl(repl, c);
|
|
|
|
if((c = GetChar()) == 'e' || c == 'E') {
|
|
|
|
add2repl(repl, c);
|
|
|
|
c = GetChar();
|
|
|
|
if (c == '+' || c == '-') {
|
|
|
|
add2repl(repl, c);
|
|
|
|
c = GetChar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
add2repl(repl, c);
|
1989-02-07 11:04:05 +00:00
|
|
|
c = GetChar();
|
|
|
|
}
|
1989-11-22 13:58:36 +00:00
|
|
|
blank = 0;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
1989-11-22 13:58:36 +00:00
|
|
|
*length = repl->r_ptr - repl->r_text;
|
1990-04-25 15:09:51 +00:00
|
|
|
return Realloc(repl->r_text, (unsigned)(repl->r_ptr - repl->r_text +1));
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* macroeq() decides whether two macro replacement texts are
|
|
|
|
identical. This version compares the texts, which occur
|
|
|
|
as strings, without taking care of the leading and trailing
|
|
|
|
blanks (spaces and tabs).
|
|
|
|
*/
|
|
|
|
macroeq(s, t)
|
|
|
|
register char *s, *t;
|
|
|
|
{
|
1989-11-22 13:58:36 +00:00
|
|
|
|
1989-02-07 11:04:05 +00:00
|
|
|
/* skip leading spaces */
|
|
|
|
while (BLANK(*s)) s++;
|
|
|
|
while (BLANK(*t)) t++;
|
|
|
|
/* first non-blank encountered in both strings */
|
|
|
|
/* The actual comparison loop: */
|
|
|
|
while (*s && *s == *t)
|
|
|
|
s++, t++;
|
|
|
|
/* two cases are possible when arrived here: */
|
|
|
|
if (*s == '\0') { /* *s == '\0' */
|
|
|
|
while (BLANK(*t)) t++;
|
|
|
|
return *t == '\0';
|
|
|
|
}
|
|
|
|
else { /* *s != *t */
|
|
|
|
while (BLANK(*s)) s++;
|
|
|
|
while (BLANK(*t)) t++;
|
|
|
|
return (*s == '\0') && (*t == '\0');
|
|
|
|
}
|
|
|
|
}
|
1991-12-17 13:12:22 +00:00
|
|
|
#else /* NOPP */
|
1989-10-16 12:38:25 +00:00
|
|
|
|
|
|
|
struct idf *
|
|
|
|
GetIdentifier(skiponerr)
|
|
|
|
int skiponerr; /* skip the rest of the line on error */
|
|
|
|
{
|
|
|
|
/* returns a pointer to the descriptor of the identifier that is
|
1989-10-19 14:53:25 +00:00
|
|
|
read from the input stream. When the input does not contain
|
1989-10-16 12:38:25 +00:00
|
|
|
an identifier, the rest of the line is skipped when
|
|
|
|
skiponerr is on, and a null-pointer is returned.
|
|
|
|
The substitution of macros is disabled.
|
|
|
|
*/
|
|
|
|
int tok;
|
|
|
|
struct token tk;
|
|
|
|
|
|
|
|
tok = GetToken(&tk);
|
|
|
|
if (tok != IDENTIFIER) {
|
1989-10-18 13:12:31 +00:00
|
|
|
if (skiponerr && tok != EOI) SkipToNewLine();
|
1989-10-16 12:38:25 +00:00
|
|
|
return (struct idf *)0;
|
|
|
|
}
|
|
|
|
return tk.tk_idf;
|
|
|
|
}
|
|
|
|
|
1989-02-07 11:04:05 +00:00
|
|
|
domacro()
|
|
|
|
{
|
|
|
|
int tok;
|
|
|
|
struct token tk;
|
|
|
|
|
|
|
|
EoiForNewline = 1;
|
|
|
|
if ((tok = GetToken(&tk)) == IDENTIFIER) {
|
1989-10-24 15:02:02 +00:00
|
|
|
if (! strcmp(tk.tk_idf->id_text, "pragma")) {
|
1989-09-29 16:20:38 +00:00
|
|
|
do_pragma();
|
|
|
|
EoiForNewline = 0;
|
|
|
|
return;
|
|
|
|
}
|
1989-10-24 15:02:02 +00:00
|
|
|
} else if (tok == INTEGER) {
|
|
|
|
do_line((unsigned int) tk.tk_ival);
|
|
|
|
EoiForNewline = 0;
|
|
|
|
return;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
1991-03-01 13:51:37 +00:00
|
|
|
lexerror("illegal # line");
|
1989-02-07 11:04:05 +00:00
|
|
|
EoiForNewline = 0;
|
1989-10-19 14:53:25 +00:00
|
|
|
SkipToNewLine();
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* NOPP */
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
do_line(l)
|
|
|
|
unsigned int l;
|
|
|
|
{
|
|
|
|
struct token tk;
|
1989-11-22 13:58:36 +00:00
|
|
|
int t = GetToken(&tk);
|
1989-02-07 11:04:05 +00:00
|
|
|
|
1990-03-29 10:41:46 +00:00
|
|
|
if (t != EOI) SkipToNewLine();
|
1989-11-22 13:58:36 +00:00
|
|
|
LineNumber = l; /* the number of the next input line */
|
1990-01-16 15:21:15 +00:00
|
|
|
if (t == STRING) { /* is there a filespecifier? */
|
1990-10-19 11:50:32 +00:00
|
|
|
/*
|
|
|
|
* Do not attempt to free the old string, since it might
|
|
|
|
* be used in a def structure.
|
|
|
|
*/
|
1992-07-02 10:23:46 +00:00
|
|
|
#ifdef DBSYMTAB
|
|
|
|
if (options['g'] && strcmp(FileName, tk.tk_bts) != 0) {
|
|
|
|
C_ms_std(tk.tk_bts, N_SOL, 0);
|
|
|
|
}
|
|
|
|
#endif /* DBSYMTAB */
|
1989-11-22 13:58:36 +00:00
|
|
|
FileName = tk.tk_bts;
|
1990-01-16 15:21:15 +00:00
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|