Pull every file parsing context into one structure instead of using a few

file global variables... adding an extra context variable is thus allmost
trivial now.
This commit is contained in:
reinoud 2002-02-21 22:21:34 +00:00
parent a9c374ed26
commit 502866c1fe

View File

@ -1,4 +1,4 @@
/* $NetBSD: parse.c,v 1.81 2002/02/17 23:53:46 pk Exp $ */ /* $NetBSD: parse.c,v 1.82 2002/02/21 22:21:34 reinoud Exp $ */
/* /*
* Copyright (c) 1988, 1989, 1990, 1993 * Copyright (c) 1988, 1989, 1990, 1993
@ -39,14 +39,14 @@
*/ */
#ifdef MAKE_BOOTSTRAP #ifdef MAKE_BOOTSTRAP
static char rcsid[] = "$NetBSD: parse.c,v 1.81 2002/02/17 23:53:46 pk Exp $"; static char rcsid[] = "$NetBSD: parse.c,v 1.82 2002/02/21 22:21:34 reinoud Exp $";
#else #else
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef lint #ifndef lint
#if 0 #if 0
static char sccsid[] = "@(#)parse.c 8.3 (Berkeley) 3/19/94"; static char sccsid[] = "@(#)parse.c 8.3 (Berkeley) 3/19/94";
#else #else
__RCSID("$NetBSD: parse.c,v 1.81 2002/02/17 23:53:46 pk Exp $"); __RCSID("$NetBSD: parse.c,v 1.82 2002/02/21 22:21:34 reinoud Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
#endif #endif
@ -126,27 +126,25 @@ typedef struct {
char *ptr; char *ptr;
} PTR; } PTR;
static char *fname; /* name of current file (for errors) */
static int lineno; /* line number in current file */
static FILE *curFILE = NULL; /* current makefile */
static PTR *curPTR = NULL; /* current makefile */
static int fatals = 0; static int fatals = 0;
static GNode *mainNode; /* The main target to create. This is the static GNode *mainNode; /* The main target to create. This is the
* first target on the first dependency * first target on the first dependency
* line in the first makefile */ * line in the first makefile */
/*
* Definitions for handling #include specifications
*/
typedef struct IFile { typedef struct IFile {
char *fname; /* name of previous file */ char *fname; /* name of previous file */
int lineno; /* saved line number */ int lineno; /* saved line number */
FILE * F; /* the open stream */ FILE * F; /* the open stream */
PTR * p; /* the char pointer */ PTR * P; /* the char pointer */
} IFile; } IFile;
static IFile curFile;
/*
* Definitions for handling #include specifications
*/
static Lst includes; /* stack of IFiles generated by static Lst includes; /* stack of IFiles generated by
* #includes */ * #includes */
Lst parseIncPath; /* list of directories for "..." includes */ Lst parseIncPath; /* list of directories for "..." includes */
@ -480,7 +478,7 @@ Parse_Error(va_alist)
type = va_arg(ap, int); type = va_arg(ap, int);
fmt = va_arg(ap, char *); fmt = va_arg(ap, char *);
#endif #endif
ParseVErrorInternal(fname, lineno, type, fmt, ap); ParseVErrorInternal(curFile.fname, curFile.lineno, type, fmt, ap);
va_end(ap); va_end(ap);
} }
@ -1816,7 +1814,7 @@ ParseDoInclude (line)
char *prefEnd, *Fname; char *prefEnd, *Fname;
/* Make a temporary copy of this, to be safe. */ /* Make a temporary copy of this, to be safe. */
Fname = estrdup(fname); Fname = estrdup(curFile.fname);
prefEnd = strrchr (Fname, '/'); prefEnd = strrchr (Fname, '/');
if (prefEnd != (char *)NULL) { if (prefEnd != (char *)NULL) {
@ -1876,11 +1874,8 @@ ParseDoInclude (line)
* a very nice stack to track how we got here... * a very nice stack to track how we got here...
*/ */
oldFile = (IFile *) emalloc (sizeof (IFile)); oldFile = (IFile *) emalloc (sizeof (IFile));
oldFile->fname = fname;
oldFile->F = curFILE; memcpy(oldFile, &curFile, sizeof (IFile));
oldFile->p = curPTR;
oldFile->lineno = lineno;
(void) Lst_AtFront (includes, (ClientData)oldFile); (void) Lst_AtFront (includes, (ClientData)oldFile);
@ -1890,14 +1885,15 @@ ParseDoInclude (line)
* name of the include file so error messages refer to the right * name of the include file so error messages refer to the right
* place. Naturally enough, we start reading at line number 0. * place. Naturally enough, we start reading at line number 0.
*/ */
fname = fullname; curFile.fname = fullname;
lineno = 0; curFile.lineno = 0;
ParseSetParseFile(fname); ParseSetParseFile(curFile.fname);
curFILE = fopen (fullname, "r"); curFile.F = fopen (fullname, "r");
curPTR = NULL; curFile.P = NULL;
if (curFILE == (FILE * ) NULL) {
if (curFile.F == (FILE * ) NULL) {
if (!silent) if (!silent)
Parse_Error (PARSE_FATAL, "Cannot open %s", fullname); Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
/* /*
@ -1964,18 +1960,15 @@ Parse_FromString(str)
(void) fprintf(stderr, "%s\n----\n", str); (void) fprintf(stderr, "%s\n----\n", str);
oldFile = (IFile *) emalloc (sizeof (IFile)); oldFile = (IFile *) emalloc (sizeof (IFile));
oldFile->lineno = lineno; memcpy(oldFile, &curFile, sizeof (IFile));
oldFile->fname = fname;
oldFile->F = curFILE;
oldFile->p = curPTR;
(void) Lst_AtFront (includes, (ClientData)oldFile); (void) Lst_AtFront (includes, (ClientData)oldFile);
curFILE = NULL; curFile.F = NULL;
curPTR = (PTR *) emalloc (sizeof (PTR)); curFile.P = (PTR *) emalloc (sizeof (PTR));
curPTR->str = curPTR->ptr = str; curFile.P->str = curFile.P->ptr = str;
lineno = 0; curFile.lineno = 0;
fname = estrdup(fname); curFile.fname = estrdup(curFile.fname);
} }
@ -2007,9 +2000,11 @@ ParseTraditionalInclude (line)
int done = 0; int done = 0;
int silent = (line[0] != 'i') ? 1 : 0; int silent = (line[0] != 'i') ? 1 : 0;
char *file = &line[silent + 7]; char *file = &line[silent + 7];
char *cfname = fname; char *cfname;
size_t clineno = lineno; size_t clineno;
cfname = curFile.fname;
clineno = curFile.lineno;
/* /*
* Skip over whitespace * Skip over whitespace
@ -2108,11 +2103,7 @@ ParseTraditionalInclude (line)
* a very nice stack to track how we got here... * a very nice stack to track how we got here...
*/ */
oldFile = (IFile *) emalloc(sizeof(IFile)); oldFile = (IFile *) emalloc(sizeof(IFile));
oldFile->fname = fname; memcpy(oldFile, &curFile, sizeof (IFile));
oldFile->F = curFILE;
oldFile->p = curPTR;
oldFile->lineno = lineno;
(void) Lst_AtFront(includes, (ClientData)oldFile); (void) Lst_AtFront(includes, (ClientData)oldFile);
@ -2122,12 +2113,13 @@ ParseTraditionalInclude (line)
* absolute name of the include file so error messages refer to the * absolute name of the include file so error messages refer to the
* right place. Naturally enough, we start reading at line number 0. * right place. Naturally enough, we start reading at line number 0.
*/ */
fname = fullname; curFile.fname = fullname;
lineno = 0; curFile.lineno = 0;
curFILE = fopen(fullname, "r"); curFile.F = fopen(fullname, "r");
curPTR = NULL; curFile.P = NULL;
if (curFILE == NULL) {
if (curFile.F == NULL) {
if (!silent) if (!silent)
ParseErrorInternal(cfname, clineno, PARSE_FATAL, ParseErrorInternal(cfname, clineno, PARSE_FATAL,
"Cannot open %s", fullname); "Cannot open %s", fullname);
@ -2168,21 +2160,22 @@ ParseEOF (opened)
} }
ifile = (IFile *) Lst_DeQueue (includes); ifile = (IFile *) Lst_DeQueue (includes);
free ((Address) fname);
fname = ifile->fname; /* XXX dispose of curFile info */
lineno = ifile->lineno; free ((Address) curFile.fname);
if (opened && curFILE) if (opened && curFile.F)
(void) fclose (curFILE); (void) fclose (curFile.F);
if (curPTR) { if (curFile.P) {
free((Address) curPTR->str); free((Address) curFile.P->str);
free((Address) curPTR); free((Address) curFile.P);
} }
curFILE = ifile->F;
curPTR = ifile->p; memcpy(&curFile, ifile, sizeof (IFile));
free ((Address)ifile); free ((Address)ifile);
/* pop the PARSEDIR/PARSEFILE variables */ /* pop the PARSEDIR/PARSEFILE variables */
ParseSetParseFile(fname); ParseSetParseFile(curFile.fname);
return (CONTINUE); return (CONTINUE);
} }
@ -2200,11 +2193,11 @@ ParseEOF (opened)
static __inline int static __inline int
ParseReadc() ParseReadc()
{ {
if (curFILE) if (curFile.F)
return fgetc(curFILE); return fgetc(curFile.F);
if (curPTR && *curPTR->ptr) if (curFile.P && *curFile.P->ptr)
return *curPTR->ptr++; return *curFile.P->ptr++;
return EOF; return EOF;
} }
@ -2224,12 +2217,12 @@ static void
ParseUnreadc(c) ParseUnreadc(c)
int c; int c;
{ {
if (curFILE) { if (curFile.F) {
ungetc(c, curFILE); ungetc(c, curFile.F);
return; return;
} }
if (curPTR) { if (curFile.P) {
*--(curPTR->ptr) = c; *--(curFile.P->ptr) = c;
return; return;
} }
} }
@ -2256,7 +2249,7 @@ ParseSkipLine(skip)
&& c != EOF) { && c != EOF) {
if (c == '\n') { if (c == '\n') {
Buf_ReplaceLastByte(buf, (Byte)' '); Buf_ReplaceLastByte(buf, (Byte)' ');
lineno++; curFile.lineno++;
while ((c = ParseReadc()) == ' ' || c == '\t'); while ((c = ParseReadc()) == ' ' || c == '\t');
@ -2274,7 +2267,7 @@ ParseSkipLine(skip)
return((char *)NULL); return((char *)NULL);
} }
lineno++; curFile.lineno++;
Buf_AddByte(buf, (Byte)'\0'); Buf_AddByte(buf, (Byte)'\0');
line = (char *)Buf_GetAll(buf, &lineLength); line = (char *)Buf_GetAll(buf, &lineLength);
} while (skip == 1 && line[0] != '.'); } while (skip == 1 && line[0] != '.');
@ -2334,7 +2327,7 @@ ParseReadLine ()
ignComment = ignDepOp = TRUE; ignComment = ignDepOp = TRUE;
break; break;
} else if (c == '\n') { } else if (c == '\n') {
lineno++; curFile.lineno++;
} else if (c == '#') { } else if (c == '#') {
ParseUnreadc(c); ParseUnreadc(c);
break; break;
@ -2364,7 +2357,7 @@ test_char:
* semi-colon and semiNL is TRUE, it will be recognized as a * semi-colon and semiNL is TRUE, it will be recognized as a
* newline in the code below this... * newline in the code below this...
*/ */
lineno++; curFile.lineno++;
lastc = ' '; lastc = ' ';
while ((c = ParseReadc ()) == ' ' || c == '\t') { while ((c = ParseReadc ()) == ' ' || c == '\t') {
continue; continue;
@ -2467,7 +2460,7 @@ test_char:
} }
line_read: line_read:
lineno++; curFile.lineno++;
if (lastc != '\0') { if (lastc != '\0') {
Buf_AddByte (buf, (Byte)lastc); Buf_AddByte (buf, (Byte)lastc);
@ -2596,12 +2589,13 @@ Parse_File(name, stream)
*line; /* the line we're working on */ *line; /* the line we're working on */
inLine = FALSE; inLine = FALSE;
fname = name;
curFILE = stream;
lineno = 0;
fatals = 0; fatals = 0;
ParseSetParseFile(fname); curFile.fname = name;
curFile.F = stream;
curFile.lineno = 0;
ParseSetParseFile(curFile.fname);
do { do {
while ((line = ParseReadLine ()) != NULL) { while ((line = ParseReadLine ()) != NULL) {
@ -2861,6 +2855,6 @@ static void
ParseMark(gn) ParseMark(gn)
GNode *gn; GNode *gn;
{ {
gn->fname = strdup(fname); gn->fname = strdup(curFile.fname);
gn->lineno = lineno; gn->lineno = curFile.lineno;
} }