Run files through clang-format before editing.

This commit is contained in:
David Given 2018-09-02 11:36:15 +02:00
parent fa1ba55ad2
commit dd00b81080
2 changed files with 928 additions and 757 deletions

View file

@ -38,9 +38,7 @@ int nestlevel = -1;
void macro_def();
void do_define();
struct idf *
GetIdentifier(skiponerr)
int skiponerr; /* skip the rest of the line on error */
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
read from the input stream. When the input doe not contain
@ -56,8 +54,10 @@ GetIdentifier(skiponerr)
tok = GetToken(&tk);
ReplaceMacros = 1;
UnknownIdIsZero = tmp;
if (tok != IDENTIFIER) {
if (skiponerr && tok != EOI) SkipToNewLine();
if (tok != IDENTIFIER)
{
if (skiponerr && tok != EOI)
SkipToNewLine();
return (struct idf*)0;
}
return tk.tk_idf;
@ -80,9 +80,11 @@ domacro()
ReplaceMacros = 0;
toknum = GetToken(&tk);
ReplaceMacros = 1;
switch(toknum) { /* select control line action */
switch (toknum)
{ /* select control line action */
case IDENTIFIER: /* is it a macro keyword? */
switch (tk.tk_idf->id_resmac) {
switch (tk.tk_idf->id_resmac)
{
case K_DEFINE: /* "define" */
do_define();
break;
@ -111,7 +113,8 @@ domacro()
/* set LineNumber and FileName according to
the arguments.
*/
if (GetToken(&tk) != INTEGER) {
if (GetToken(&tk) != INTEGER)
{
lexerror("bad #line syntax");
SkipToNewLine();
}
@ -149,9 +152,7 @@ domacro()
int lint_skip_comment;
#endif
void
skip_block(to_endif)
int to_endif;
void skip_block(to_endif) int to_endif;
{
/* skip_block() skips the input from
1) a false #if, #ifdef, #ifndef or #elif until the
@ -170,12 +171,15 @@ int to_endif;
lint_skip_comment++;
#endif
NoUnstack++;
for (;;) {
for (;;)
{
ch = GetChar(); /* read first character after newline */
while (class(ch) == STSKIP)
ch = GetChar();
if (ch != '#') {
if (ch == EOI) {
if (ch != '#')
{
if (ch == EOI)
{
NoUnstack--;
#ifdef LINT
lint_skip_comment--;
@ -183,22 +187,29 @@ int to_endif;
return;
}
/* A possible '/' is not pushed back */
if (ch == '/') {
if (ch == '/')
{
ch = GetChar();
if (ch != '*') UnGetChar();
else {
if (ch != '*')
UnGetChar();
else
{
skipcomment();
continue;
}
} else UnGetChar();
}
else
UnGetChar();
SkipToNewLine();
continue;
}
ReplaceMacros = 0;
toknum = GetToken(&tk);
ReplaceMacros = 1;
if (toknum != IDENTIFIER) {
if (toknum != INTEGER) {
if (toknum != IDENTIFIER)
{
if (toknum != INTEGER)
{
lexerror("illegal # line");
}
SkipToNewLine();
@ -209,7 +220,8 @@ int to_endif;
Interpret #else, #elif and #endif if they occur
on the same level.
*/
switch(tk.tk_idf->id_resmac) {
switch (tk.tk_idf->id_resmac)
{
default:
case K_UNKNOWN:
/* invalid word seen after the '#' */
@ -233,10 +245,12 @@ int to_endif;
case K_ELIF:
if (ifstack[nestlevel])
lexerror("#elif after #else");
if (!to_endif && nestlevel == skiplevel) {
if (!to_endif && nestlevel == skiplevel)
{
nestlevel--;
push_if();
if (ifexpr()) {
if (ifexpr())
{
NoUnstack--;
#ifdef LINT
lint_skip_comment--;
@ -244,14 +258,17 @@ int to_endif;
return;
}
}
else SkipToNewLine(); /* otherwise done in ifexpr() */
else
SkipToNewLine(); /* otherwise done in ifexpr() */
break;
case K_ELSE:
if (ifstack[nestlevel])
lexerror("#else after #else");
++(ifstack[nestlevel]);
if (!to_endif && nestlevel == skiplevel) {
if (SkipToNewLine()) {
if (!to_endif && nestlevel == skiplevel)
{
if (SkipToNewLine())
{
if (!options['o'])
lexstrict("garbage following #else");
}
@ -261,12 +278,15 @@ int to_endif;
#endif
return;
}
else SkipToNewLine();
else
SkipToNewLine();
break;
case K_ENDIF:
assert(nestlevel > nestlow);
if (nestlevel == skiplevel) {
if (SkipToNewLine()) {
if (nestlevel == skiplevel)
{
if (SkipToNewLine())
{
if (!options['o'])
lexstrict("garbage following #endif");
}
@ -277,14 +297,14 @@ int to_endif;
#endif
return;
}
else SkipToNewLine();
else
SkipToNewLine();
nestlevel--;
break;
}
}
}
ifexpr()
{
/* ifexpr() returns whether the restricted constant
@ -319,22 +339,27 @@ do_include()
AccFileSpecifier = 1;
if (((tok = GetToken(&tk)) == FILESPECIFIER) || tok == STRING)
filenm = tk.tk_bts;
else {
else
{
lexerror("bad include syntax");
filenm = (char*)0;
}
AccFileSpecifier = 0;
if (SkipToNewLine()) {
if (SkipToNewLine())
{
lexerror("bad include syntax");
}
inctable[0] = WorkingDir;
if (filenm) {
if (!InsertFile(filenm, &inctable[tok==FILESPECIFIER],&result)){
if (filenm)
{
if (!InsertFile(filenm, &inctable[tok == FILESPECIFIER], &result))
{
lexerror("cannot open include file \"%s\"", filenm);
add_dependency(filenm);
free(filenm);
}
else {
else
{
add_dependency(result);
WorkingDir = getwdir(result);
File_Inserted = 1;
@ -343,17 +368,18 @@ do_include()
nestlow = nestlevel;
#ifdef DBSYMTAB
IncludeLevel++;
if (options['g']) {
if (options['g'])
{
C_ms_stb_cst(FileName, N_BINCL, 0, (arith)0);
}
#endif /* DBSYMTAB */
if (result != filenm) free(filenm);
if (result != filenm)
free(filenm);
}
}
}
void
do_define()
void do_define()
{
/* do_define() interprets a #define control line.
*/
@ -367,7 +393,8 @@ do_define()
char* get_text();
/* read the #defined macro's name */
if (!(id = GetIdentifier(1))) {
if (!(id = GetIdentifier(1)))
{
lexerror("illegal #define line");
return;
}
@ -375,8 +402,10 @@ do_define()
followed immediately by a '('.
*/
ch = GetChar();
if (ch == '(') {
if ((nformals = getparams(formals, parbuf)) == -1) {
if (ch == '(')
{
if ((nformals = getparams(formals, parbuf)) == -1)
{
SkipToNewLine();
return; /* an error occurred */
}
@ -402,12 +431,15 @@ push_if()
do_elif()
{
if (nestlevel <= nestlow) {
if (nestlevel <= nestlow)
{
lexerror("#elif without corresponding #if");
SkipToNewLine();
}
else { /* restart at this level as if a #if is detected. */
if (ifstack[nestlevel]) {
else
{ /* restart at this level as if a #if is detected. */
if (ifstack[nestlevel])
{
lexerror("#elif after #else");
SkipToNewLine();
}
@ -424,8 +456,10 @@ do_else()
lexstrict("garbage following #else");
if (nestlevel <= nestlow)
lexerror("#else without corresponding #if");
else { /* mark this level as else-d */
if (ifstack[nestlevel]) {
else
{ /* mark this level as else-d */
if (ifstack[nestlevel])
{
lexerror("#else after #else");
}
++(ifstack[nestlevel]);
@ -435,14 +469,17 @@ do_else()
do_endif()
{
if (SkipToNewLine()) {
if (SkipToNewLine())
{
if (!options['o'])
lexstrict("garbage following #endif");
}
if (nestlevel <= nestlow) {
if (nestlevel <= nestlow)
{
lexerror("#endif without corresponding #if");
}
else nestlevel--;
else
nestlevel--;
}
do_if()
@ -463,8 +500,7 @@ do_ifdef(how)
lexerror("illegal #ifdef construction");
else if (SkipToNewLine())
if (!options['o'])
lexstrict("garbage following #%s <identifier>",
how ? "ifdef" : "ifndef");
lexstrict("garbage following #%s <identifier>", how ? "ifdef" : "ifndef");
/* The next test is a shorthand for:
(how && !id->id_macro) || (!how && id->id_macro)
@ -474,23 +510,28 @@ do_ifdef(how)
}
/* argidf != NULL when the undef came from a -U option */
do_undef(argidf)
struct idf *argidf;
do_undef(argidf) struct idf* argidf;
{
register struct idf* id = argidf;
/* Forget a macro definition. */
if (id || (id = GetIdentifier(1))) {
if (id->id_macro) { /* forget the macro */
if (id->id_macro->mc_flag & NOUNDEF) {
if (id || (id = GetIdentifier(1)))
{
if (id->id_macro)
{ /* forget the macro */
if (id->id_macro->mc_flag & NOUNDEF)
{
lexerror("it is not allowed to undef %s", id->id_text);
} else {
}
else
{
free(id->id_macro->mc_text);
free_macro(id->id_macro);
id->id_macro = (struct macro*)0;
}
} /* else: don't complain */
if (!argidf) {
if (!argidf)
{
if (SkipToNewLine())
if (!options['o'])
lexstrict("garbage following #undef");
@ -511,9 +552,7 @@ do_error()
LineNumber++;
}
int
getparams(buf, parbuf)
char *buf[];
int getparams(buf, parbuf) char* buf[];
char parbuf[];
{
/* getparams() reads the formal parameter list of a macro
@ -533,12 +572,15 @@ getparams(buf, parbuf)
c = GetChar();
c = skipspaces(c, 0);
if (c == ')') { /* no parameters: #define name() */
if (c == ')')
{ /* no parameters: #define name() */
*pbuf = (char*)0;
return 0;
}
for (;;) { /* eat the formal parameter list */
if (class(c) != STIDF && class(c) != STELL) {
for (;;)
{ /* eat the formal parameter list */
if (class(c) != STIDF && class(c) != STELL)
{
lexerror("#define: bad formal parameter");
return -1;
}
@ -546,7 +588,8 @@ getparams(buf, parbuf)
*ptr++ = c;
if (ptr >= &parbuf[PARBUFSIZE])
fatal("formal parameter buffer overflow");
do { /* eat the identifier name */
do
{ /* eat the identifier name */
c = GetChar();
*ptr++ = c;
if (ptr >= &parbuf[PARBUFSIZE])
@ -557,20 +600,23 @@ getparams(buf, parbuf)
/* 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)) {
lexerror("formal parameter \"%s\" already used",
*pbuf);
for (pbuf2 = pbuf - 1; pbuf2 >= &buf[0]; pbuf2--)
{
if (!strcmp(*pbuf2, *pbuf))
{
lexerror("formal parameter \"%s\" already used", *pbuf);
}
}
pbuf++;
c = skipspaces(c, 0);
if (c == ')') { /* end of the formal parameter list */
if (c == ')')
{ /* end of the formal parameter list */
*pbuf = (char*)0;
return pbuf - buf;
}
if (c != ',') {
if (c != ',')
{
lexerror("#define: bad formal parameter list");
return -1;
}
@ -580,9 +626,7 @@ getparams(buf, parbuf)
/*NOTREACHED*/
}
void
macro_def(id, text, nformals, length, flags)
register struct idf *id;
void macro_def(id, text, nformals, length, flags) register struct idf* id;
char* text;
{
register struct macro* newdef = id->id_macro;
@ -592,10 +636,13 @@ macro_def(id, text, nformals, length, flags)
table entry belonging to the name of the macro.
An error is given if there was already a definition
*/
if (newdef) { /* is there a redefinition? */
if (newdef->mc_flag & NOUNDEF) {
if (newdef)
{ /* is there a redefinition? */
if (newdef->mc_flag & NOUNDEF)
{
lexerror("it is not allowed to redefine %s", id->id_text);
} else if (!macroeq(newdef->mc_text, text))
}
else if (!macroeq(newdef->mc_text, text))
lexerror("illegal redefine of \"%s\"", id->id_text);
free(text);
return;
@ -607,9 +654,7 @@ macro_def(id, text, nformals, length, flags)
newdef->mc_flag = flags; /* special flags */
}
int
find_name(nm, index)
char *nm, *index[];
int find_name(nm, index) char *nm, *index[];
{
/* find_name() returns the index of "nm" in the namelist
"index" if it can be found there. 0 is returned if it is
@ -626,9 +671,7 @@ find_name(nm, index)
#define BLANK(ch) ((ch == ' ') || (ch == '\t'))
char *
get_text(formals, length)
char *formals[];
char* get_text(formals, length) char* formals[];
int* length;
{
/* get_text() copies the replacement text of a macro
@ -662,97 +705,124 @@ get_text(formals, length)
repl->r_ptr = repl->r_text = Malloc(repl->r_size = ITEXTSIZE);
*repl->r_ptr = '\0';
while ((c != EOI) && (class(c) != STNL)) {
if (BLANK(c)) {
while ((c != EOI) && (class(c) != STNL))
{
if (BLANK(c))
{
blank++;
c = GetChar();
continue;
}
if (c == '\'' || c == '"') {
if (c == '\'' || c == '"')
{
register int delim = c;
if (blank) {
if (blank)
{
blank = 0;
add2repl(repl, ' ');
}
do {
do
{
add2repl(repl, c);
if (c == '\\') add2repl(repl, GetChar());
if (c == '\\')
add2repl(repl, GetChar());
c = GetChar();
} while (c != delim && c != EOI && class(c) != STNL);
if (c == EOI || class(c) == STNL) {
if (c == EOI || class(c) == STNL)
{
lexstrict("unclosed opening %c", delim);
break;
}
add2repl(repl, c);
c = GetChar();
} else if (c == '/') {
}
else if (c == '/')
{
c = GetChar();
if (c == '*') {
if (c == '*')
{
skipcomment();
blank++;
c = GetChar();
continue;
}
if (blank) {
if (blank)
{
blank = 0;
add2repl(repl, ' ');
}
add2repl(repl, '/');
} else if (formals
&& (class(c) == STIDF || class(c) == STELL)) {
}
else if (formals && (class(c) == STIDF || class(c) == STELL))
{
char id_buf[IDFSIZE + 1];
register char* idp = id_buf;
int n;
/* read identifier: it may be a formal parameter */
*idp++ = c;
do {
do
{
c = GetChar();
if (idp <= &id_buf[IDFSIZE])
*idp++ = c;
} while (in_idf(c));
*--idp = '\0';
if (blank) {
if (blank)
{
blank = 0;
add2repl(repl, ' ');
}
/* construct the formal parameter mark or identifier */
if (n = find_name(id_buf, formals))
add2repl(repl, FORMALP | (char)n);
else {
else
{
idp = id_buf;
while (*idp) add2repl(repl, *idp++);
while (*idp)
add2repl(repl, *idp++);
}
} else if (class(c) == STNUM) {
if (blank) {
}
else if (class(c) == STNUM)
{
if (blank)
{
blank = 0;
add2repl(repl, ' ');
}
add2repl(repl, c);
if (c == '.') {
if (c == '.')
{
c = GetChar();
if (class(c) != STNUM) {
if (class(c) != STNUM)
{
continue;
}
add2repl(repl, c);
}
c = GetChar();
while(in_idf(c) || c == '.') {
while (in_idf(c) || c == '.')
{
add2repl(repl, c);
if((c = GetChar()) == 'e' || c == 'E') {
if ((c = GetChar()) == 'e' || c == 'E')
{
add2repl(repl, c);
c = GetChar();
if (c == '+' || c == '-') {
if (c == '+' || c == '-')
{
add2repl(repl, c);
c = GetChar();
}
}
}
} else {
if (blank) {
}
else
{
if (blank)
{
blank = 0;
add2repl(repl, ' ');
}
@ -769,33 +839,37 @@ get_text(formals, length)
as strings, without taking care of the leading and trailing
blanks (spaces and tabs).
*/
macroeq(s, t)
register char *s, *t;
macroeq(s, t) register char* s, *t;
{
/* skip leading spaces */
while (BLANK(*s)) s++;
while (BLANK(*t)) t++;
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++;
if (*s == '\0')
{ /* *s == '\0' */
while (BLANK(*t))
t++;
return *t == '\0';
}
else { /* *s != *t */
while (BLANK(*s)) s++;
while (BLANK(*t)) t++;
else
{ /* *s != *t */
while (BLANK(*s))
s++;
while (BLANK(*t))
t++;
return (*s == '\0') && (*t == '\0');
}
}
#else /* NOPP */
struct idf *
GetIdentifier(skiponerr)
int skiponerr; /* skip the rest of the line on error */
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
read from the input stream. When the input does not contain
@ -807,8 +881,10 @@ GetIdentifier(skiponerr)
struct token tk;
tok = GetToken(&tk);
if (tok != IDENTIFIER) {
if (skiponerr && tok != EOI) SkipToNewLine();
if (tok != IDENTIFIER)
{
if (skiponerr && tok != EOI)
SkipToNewLine();
return (struct idf*)0;
}
return tk.tk_idf;
@ -820,13 +896,17 @@ domacro()
struct token tk;
EoiForNewline = 1;
if ((tok = GetToken(&tk)) == IDENTIFIER) {
if (! strcmp(tk.tk_idf->id_text, "pragma")) {
if ((tok = GetToken(&tk)) == IDENTIFIER)
{
if (!strcmp(tk.tk_idf->id_text, "pragma"))
{
do_pragma();
EoiForNewline = 0;
return;
}
} else if (tok == INTEGER) {
}
else if (tok == INTEGER)
{
do_line((unsigned int)tk.tk_ival);
EoiForNewline = 0;
return;
@ -837,22 +917,23 @@ domacro()
}
#endif /* NOPP */
do_line(l)
unsigned int l;
do_line(l) unsigned int l;
{
struct token tk;
int t = GetToken(&tk);
if (t != EOI) SkipToNewLine();
if (t != EOI)
SkipToNewLine();
LineNumber = l; /* the number of the next input line */
if (t == STRING) { /* is there a filespecifier? */
if (t == STRING)
{ /* is there a filespecifier? */
/*
* Do not attempt to free the old string, since it might
* be used in a def structure.
*/
#ifdef DBSYMTAB
if (options['g'] && strcmp(FileName, tk.tk_bts) != 0) {
if (options['g'] && strcmp(FileName, tk.tk_bts) != 0)
{
C_ms_std(tk.tk_bts, N_SOL, 0);
}
#endif /* DBSYMTAB */

View file

@ -36,9 +36,7 @@ extern int do_preprocess;
void macro_def();
void do_define();
char *
GetIdentifier(skiponerr)
int skiponerr; /* skip the rest of the line on error */
char* GetIdentifier(skiponerr) int skiponerr; /* skip the rest of the line on error */
{
/* Returns a pointer to the identifier that is read from the
input stream. When the input does not contain an
@ -55,8 +53,10 @@ GetIdentifier(skiponerr)
tok = GetToken(&tk);
ReplaceMacros = 1;
UnknownIdIsZero = tmp;
if (tok != IDENTIFIER) {
if (skiponerr && tok != EOF) SkipToNewLine();
if (tok != IDENTIFIER)
{
if (skiponerr && tok != EOF)
SkipToNewLine();
return (char*)0;
}
return tk.tk_str;
@ -80,17 +80,20 @@ domacro()
ReplaceMacros = 0;
toknum = GetToken(&tk);
ReplaceMacros = 1;
switch(toknum) { /* select control line action */
switch (toknum)
{ /* select control line action */
case IDENTIFIER: /* is it a macro keyword? */
id = findidf(tk.tk_str);
if (!id) {
if (!id)
{
error("%s: unknown control", tk.tk_str);
SkipToNewLine();
free(tk.tk_str);
break;
}
free(tk.tk_str);
switch (id->id_resmac) {
switch (id->id_resmac)
{
case K_DEFINE: /* "define" */
do_define();
break;
@ -119,7 +122,8 @@ domacro()
/* set LineNumber and FileName according to
the arguments.
*/
if (GetToken(&tk) != INTEGER) {
if (GetToken(&tk) != INTEGER)
{
error("bad #line syntax");
SkipToNewLine();
}
@ -152,9 +156,7 @@ domacro()
}
}
void
skip_block(to_endif)
int to_endif;
void skip_block(to_endif) int to_endif;
{
/* skip_block() skips the input from
1) a false #if, #ifdef, #ifndef or #elif until the
@ -171,30 +173,40 @@ int to_endif;
struct idf* id;
NoUnstack++;
for (;;) {
for (;;)
{
ch = GetChar(); /* read first character after newline */
while (class(ch) == STSKIP)
ch = GetChar();
if (ch != '#') {
if (ch == EOI) {
if (ch != '#')
{
if (ch == EOI)
{
NoUnstack--;
return;
}
if (ch == '/') {
if (ch != '*') UnGetChar();
else {
if (ch == '/')
{
if (ch != '*')
UnGetChar();
else
{
skipcomment();
continue;
}
} else UnGetChar();
}
else
UnGetChar();
SkipToNewLine();
continue;
}
ReplaceMacros = 0;
toknum = GetToken(&tk);
ReplaceMacros = 1;
if (toknum != IDENTIFIER) {
if (toknum != INTEGER) {
if (toknum != IDENTIFIER)
{
if (toknum != INTEGER)
{
error("illegal # line");
}
SkipToNewLine();
@ -206,13 +218,16 @@ int to_endif;
on the same level.
*/
id = findidf(tk.tk_str);
if (id == (struct idf *)0) {
if (id == (struct idf*)0)
{
/* invalid word seen after the '#' */
warning("%s: unknown control", tk.tk_str);
}
free(tk.tk_str);
if (id == (struct idf *)0) continue;
switch(id->id_resmac) {
if (id == (struct idf*)0)
continue;
switch (id->id_resmac)
{
case K_DEFINE:
case K_ERROR:
case K_INCLUDE:
@ -231,34 +246,42 @@ int to_endif;
case K_ELIF:
if (ifstack[nestlevel])
error("#elif after #else");
if (!to_endif && nestlevel == skiplevel) {
if (!to_endif && nestlevel == skiplevel)
{
nestlevel--;
push_if();
if (ifexpr()) {
if (ifexpr())
{
NoUnstack--;
return;
}
}
else SkipToNewLine(); /* otherwise done in ifexpr() */
else
SkipToNewLine(); /* otherwise done in ifexpr() */
break;
case K_ELSE:
if (ifstack[nestlevel])
error("#else after #else");
++(ifstack[nestlevel]);
if (!to_endif && nestlevel == skiplevel) {
if (SkipToNewLine()) {
if (!to_endif && nestlevel == skiplevel)
{
if (SkipToNewLine())
{
if (!options['o'])
strict("garbage following #else");
}
NoUnstack--;
return;
}
else SkipToNewLine();
else
SkipToNewLine();
break;
case K_ENDIF:
assert(nestlevel > svnestlevel[nestcount]);
if (nestlevel == skiplevel) {
if (SkipToNewLine()) {
if (nestlevel == skiplevel)
{
if (SkipToNewLine())
{
if (!options['o'])
strict("garbage following #endif");
}
@ -266,14 +289,14 @@ int to_endif;
NoUnstack--;
return;
}
else SkipToNewLine();
else
SkipToNewLine();
nestlevel--;
break;
}
}
}
ifexpr()
{
/* ifexpr() returns whether the restricted constant
@ -307,22 +330,29 @@ do_include()
AccFileSpecifier = 1;
if (((tok = GetToken(&tk)) == FILESPECIFIER) || tok == STRING)
filenm = tk.tk_str;
else {
else
{
error("bad include syntax");
filenm = (char*)0;
}
AccFileSpecifier = 0;
if (SkipToNewLine()) {
if (SkipToNewLine())
{
error("bad include syntax");
}
inctable[0] = WorkingDir;
if (filenm) {
if (!InsertFile(filenm, &inctable[tok==FILESPECIFIER],&result)){
if (do_preprocess) error("cannot open include file \"%s\"", filenm);
else warning("cannot open include file \"%s\"", filenm);
if (filenm)
{
if (!InsertFile(filenm, &inctable[tok == FILESPECIFIER], &result))
{
if (do_preprocess)
error("cannot open include file \"%s\"", filenm);
else
warning("cannot open include file \"%s\"", filenm);
add_dependency(filenm);
}
else {
else
{
add_dependency(result);
WorkingDir = getwdir(result);
svnestlevel[++nestcount] = nestlevel;
@ -332,8 +362,7 @@ do_include()
}
}
void
do_define()
void do_define()
{
/* do_define() interprets a #define control line.
*/
@ -347,7 +376,8 @@ do_define()
char* get_text();
/* read the #defined macro's name */
if (!(str = GetIdentifier(1))) {
if (!(str = GetIdentifier(1)))
{
error("#define: illegal macro name");
return;
}
@ -355,8 +385,10 @@ do_define()
followed immediately by a '('.
*/
ch = GetChar();
if (ch == '(') {
if ((nformals = getparams(formals, parbuf)) == -1) {
if (ch == '(')
{
if ((nformals = getparams(formals, parbuf)) == -1)
{
SkipToNewLine();
free(str);
return; /* an error occurred */
@ -383,12 +415,15 @@ push_if()
do_elif()
{
if (nestlevel <= svnestlevel[nestcount]) {
if (nestlevel <= svnestlevel[nestcount])
{
error("#elif without corresponding #if");
SkipToNewLine();
}
else { /* restart at this level as if a #if is detected. */
if (ifstack[nestlevel]) {
else
{ /* restart at this level as if a #if is detected. */
if (ifstack[nestlevel])
{
error("#elif after #else");
SkipToNewLine();
}
@ -400,14 +435,17 @@ do_elif()
do_else()
{
if (SkipToNewLine()) {
if (SkipToNewLine())
{
if (!options['o'])
strict("garbage following #else");
}
if (nestlevel <= svnestlevel[nestcount])
error("#else without corresponding #if");
else { /* mark this level as else-d */
if (ifstack[nestlevel]) {
else
{ /* mark this level as else-d */
if (ifstack[nestlevel])
{
error("#else after #else");
}
++(ifstack[nestlevel]);
@ -417,14 +455,17 @@ do_else()
do_endif()
{
if (SkipToNewLine()) {
if (SkipToNewLine())
{
if (!options['o'])
strict("garbage following #endif");
}
if (nestlevel <= svnestlevel[nestcount]) {
if (nestlevel <= svnestlevel[nestcount])
{
error("#endif without corresponding #if");
}
else nestlevel--;
else
nestlevel--;
}
do_if()
@ -442,17 +483,20 @@ do_ifdef(how)
/* how == 1 : ifdef; how == 0 : ifndef
*/
push_if();
if (!(str = GetIdentifier(1))) {
if (!(str = GetIdentifier(1)))
{
error("illegal #ifdef construction");
id = (struct idf*)0;
} else {
}
else
{
id = findidf(str);
free(str);
}
if (SkipToNewLine()) {
if (SkipToNewLine())
{
if (str && !options['o'])
strict("garbage following #%s <identifier>",
how ? "ifdef" : "ifndef");
strict("garbage following #%s <identifier>", how ? "ifdef" : "ifndef");
}
/* The next test is a shorthand for:
@ -463,26 +507,32 @@ do_ifdef(how)
}
/* argstr != NULL when the undef came from a -U option */
do_undef(argstr)
char *argstr;
do_undef(argstr) char* argstr;
{
register struct idf* id;
register char* str = argstr;
/* Forget a macro definition. */
if (str || (str = GetIdentifier(1))) {
if ((id = findidf(str)) && id->id_macro) {
if (id->id_macro->mc_flag & NOUNDEF) {
if (str || (str = GetIdentifier(1)))
{
if ((id = findidf(str)) && id->id_macro)
{
if (id->id_macro->mc_flag & NOUNDEF)
{
error("it is not allowed to #undef %s", str);
} else {
}
else
{
free(id->id_macro->mc_text);
free_macro(id->id_macro);
id->id_macro = (struct macro*)0;
}
} /* else: don't complain */
if (!argstr){
if (!argstr)
{
free(str);
if (SkipToNewLine()) {
if (SkipToNewLine())
{
if (!options['o'])
strict("garbage following #undef");
}
@ -503,9 +553,7 @@ do_error()
LineNumber++;
}
int
getparams(buf, parbuf)
char *buf[];
int getparams(buf, parbuf) char* buf[];
char parbuf[];
{
/* getparams() reads the formal parameter list of a macro
@ -525,12 +573,15 @@ getparams(buf, parbuf)
c = GetChar();
c = skipspaces(c, 0);
if (c == ')') { /* no parameters: #define name() */
if (c == ')')
{ /* no parameters: #define name() */
*pbuf = (char*)0;
return 0;
}
for (;;) { /* eat the formal parameter list */
if (class(c) != STIDF && class(c) != STELL) {
for (;;)
{ /* eat the formal parameter list */
if (class(c) != STIDF && class(c) != STELL)
{
error("#define: bad formal parameter");
return -1;
}
@ -538,7 +589,8 @@ getparams(buf, parbuf)
*ptr++ = c;
if (ptr >= &parbuf[PARBUFSIZE])
fatal("formal parameter buffer overflow");
do { /* eat the identifier name */
do
{ /* eat the identifier name */
c = GetChar();
*ptr++ = c;
if (ptr >= &parbuf[PARBUFSIZE])
@ -549,20 +601,23 @@ getparams(buf, parbuf)
/* 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)) {
warning("formal parameter \"%s\" already used",
*pbuf);
for (pbuf2 = pbuf - 1; pbuf2 >= &buf[0]; pbuf2--)
{
if (!strcmp(*pbuf2, *pbuf))
{
warning("formal parameter \"%s\" already used", *pbuf);
}
}
pbuf++;
c = skipspaces(c, 0);
if (c == ')') { /* end of the formal parameter list */
if (c == ')')
{ /* end of the formal parameter list */
*pbuf = (char*)0;
return pbuf - buf;
}
if (c != ',') {
if (c != ',')
{
error("#define: bad formal parameter list");
return -1;
}
@ -572,9 +627,7 @@ getparams(buf, parbuf)
/*NOTREACHED*/
}
void
macro_def(id, text, nformals, length, flags)
register struct idf *id;
void macro_def(id, text, nformals, length, flags) register struct idf* id;
char* text;
{
register struct macro* newdef = id->id_macro;
@ -584,17 +637,25 @@ macro_def(id, text, nformals, length, flags)
table entry belonging to the name of the macro.
An error is given if there was already a definition
*/
if (newdef) { /* is there a redefinition? */
if (newdef->mc_flag & NOUNDEF) {
if (newdef)
{ /* is there a redefinition? */
if (newdef->mc_flag & NOUNDEF)
{
error("it is not allowed to redefine %s", id->id_text);
} else if (!macroeq(newdef->mc_text, text))
}
else if (!macroeq(newdef->mc_text, text))
error("illegal redefine of \"%s\"", id->id_text);
free(text);
return;
} else {
}
else
{
#ifdef DOBITS
register char* p = id->id_text;
#define setbit(bx) if (!*p) goto go_on; bits[*p++] |= (bx)
#define setbit(bx) \
if (!*p) \
goto go_on; \
bits[*p++] |= (bx)
setbit(bit0);
setbit(bit1);
setbit(bit2);
@ -614,9 +675,7 @@ macro_def(id, text, nformals, length, flags)
newdef->mc_flag = flags; /* special flags */
}
int
find_name(nm, index)
char *nm, *index[];
int find_name(nm, index) char *nm, *index[];
{
/* find_name() returns the index of "nm" in the namelist
"index" if it can be found there. 0 is returned if it is
@ -633,9 +692,7 @@ find_name(nm, index)
#define BLANK(ch) ((ch == ' ') || (ch == '\t'))
char *
get_text(formals, length)
char *formals[];
char* get_text(formals, length) char* formals[];
int* length;
{
/* get_text() copies the replacement text of a macro
@ -669,97 +726,124 @@ get_text(formals, length)
repl->r_ptr = repl->r_text = Malloc((unsigned)(repl->r_size = ITEXTSIZE));
*repl->r_ptr = '\0';
while ((c != EOI) && (class(c) != STNL)) {
if (BLANK(c)) {
while ((c != EOI) && (class(c) != STNL))
{
if (BLANK(c))
{
blank++;
c = GetChar();
continue;
}
if (c == '\'' || c == '"') {
if (c == '\'' || c == '"')
{
register int delim = c;
if (blank) {
if (blank)
{
blank = 0;
add2repl(repl, ' ');
}
do {
do
{
add2repl(repl, c);
if (c == '\\') add2repl(repl, GetChar());
if (c == '\\')
add2repl(repl, GetChar());
c = GetChar();
} while (c != delim && c != EOI && class(c) != STNL);
if (c != delim) {
if (c != delim)
{
strict("unclosed opening %c", delim);
break;
}
add2repl(repl, c);
c = GetChar();
} else if (c == '/') {
}
else if (c == '/')
{
c = GetChar();
if (c == '*') {
if (c == '*')
{
skipcomment();
blank++;
c = GetChar();
continue;
}
if (blank) {
if (blank)
{
blank = 0;
add2repl(repl, ' ');
}
add2repl(repl, '/');
} else if (formals
&& (class(c) == STIDF || class(c) == STELL)) {
}
else if (formals && (class(c) == STIDF || class(c) == STELL))
{
char id_buf[IDFSIZE + 1];
register char* idp = id_buf;
int n;
/* read identifier: it may be a formal parameter */
*idp++ = c;
do {
do
{
c = GetChar();
if (idp <= &id_buf[IDFSIZE])
*idp++ = c;
} while (in_idf(c));
*--idp = '\0';
if (blank) {
if (blank)
{
blank = 0;
add2repl(repl, ' ');
}
/* construct the formal parameter mark or identifier */
if (n = find_name(id_buf, formals))
add2repl(repl, FORMALP | (char)n);
else {
else
{
idp = id_buf;
while (*idp) add2repl(repl, *idp++);
while (*idp)
add2repl(repl, *idp++);
}
} else if (class(c) == STNUM) {
if (blank) {
}
else if (class(c) == STNUM)
{
if (blank)
{
blank = 0;
add2repl(repl, ' ');
}
add2repl(repl, c);
if (c == '.') {
if (c == '.')
{
c = GetChar();
if (class(c) != STNUM) {
if (class(c) != STNUM)
{
continue;
}
add2repl(repl, c);
}
c = GetChar();
while(in_idf(c) || c == '.') {
while (in_idf(c) || c == '.')
{
add2repl(repl, c);
if((c = GetChar()) == 'e' || c == 'E') {
if ((c = GetChar()) == 'e' || c == 'E')
{
add2repl(repl, c);
c = GetChar();
if (c == '+' || c == '-') {
if (c == '+' || c == '-')
{
add2repl(repl, c);
c = GetChar();
}
}
}
} else {
if (blank) {
}
else
{
if (blank)
{
blank = 0;
add2repl(repl, ' ');
}
@ -776,36 +860,42 @@ get_text(formals, length)
as strings, without taking care of the leading and trailing
blanks (spaces and tabs).
*/
macroeq(s, t)
register char *s, *t;
macroeq(s, t) register char* s, *t;
{
/* skip leading spaces */
while (BLANK(*s)) s++;
while (BLANK(*t)) t++;
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++;
if (*s == '\0')
{ /* *s == '\0' */
while (BLANK(*t))
t++;
return *t == '\0';
}
else { /* *s != *t */
while (BLANK(*s)) s++;
while (BLANK(*t)) t++;
else
{ /* *s != *t */
while (BLANK(*s))
s++;
while (BLANK(*t))
t++;
return (*s == '\0') && (*t == '\0');
}
}
do_line(l)
unsigned int l;
do_line(l) unsigned int l;
{
struct token tk;
int t = GetToken(&tk);
if (t != EOF) SkipToNewLine();
if (t != EOF)
SkipToNewLine();
LineNumber = l; /* the number of the next input line */
if (t == STRING) /* is there a filespecifier? */
FileName = tk.tk_str;