Added relative include path handling: The path of an input file is now pushed

onto the search path list, so include files will be searched relative to this
path first.


git-svn-id: svn://svn.cc65.org/cc65/trunk@4676 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz
2010-05-30 13:31:09 +00:00
parent b942fd0b56
commit 125ab37c09

View File

@@ -87,6 +87,8 @@ struct InputFile {
Token Tok; /* Last token */ Token Tok; /* Last token */
int C; /* Last character */ int C; /* Last character */
char Line[256]; /* The current input line */ char Line[256]; /* The current input line */
int IncSearchPath; /* True if we've added a search path */
int BinSearchPath; /* True if we've added a search path */
InputFile* Next; /* Linked list of input files */ InputFile* Next; /* Linked list of input files */
}; };
@@ -418,6 +420,14 @@ void IFDone (CharSource* S)
*/ */
CheckOpenIfs (); CheckOpenIfs ();
/* If we've added search paths for this file, remove them */
if (S->V.File.IncSearchPath) {
PopSearchPath (IncSearchPath);
}
if (S->V.File.BinSearchPath) {
PopSearchPath (BinSearchPath);
}
/* Close the input file and decrement the file count. We will ignore /* Close the input file and decrement the file count. We will ignore
* errors here, since we were just reading from the file. * errors here, since we were just reading from the file.
*/ */
@@ -443,16 +453,24 @@ int NewInputFile (const char* Name)
{ {
int RetCode = 0; /* Return code. Assume an error. */ int RetCode = 0; /* Return code. Assume an error. */
char* PathName = 0; char* PathName = 0;
FILE* F;
struct stat Buf;
StrBuf NameBuf; /* No need to initialize */
StrBuf Path = AUTO_STRBUF_INITIALIZER;
unsigned FileIdx;
CharSource* S;
/* First try to open the file */
FILE* F = fopen (Name, "r");
if (F == 0) {
/* Error (fatal error if this is the main file) */ /* If this is the main file, just try to open it. If it's an include file,
* search for it using the include path list.
*/
if (FCount == 0) { if (FCount == 0) {
/* Main file */
F = fopen (Name, "r");
if (F == 0) {
Fatal ("Cannot open input file `%s': %s", Name, strerror (errno)); Fatal ("Cannot open input file `%s': %s", Name, strerror (errno));
} }
} else {
/* We are on include level. Search for the file in the include /* We are on include level. Search for the file in the include
* directories. * directories.
*/ */
@@ -467,13 +485,6 @@ int NewInputFile (const char* Name)
Name = PathName; Name = PathName;
} }
/* check again if we do now have an open file */
if (F != 0) {
StrBuf NameBuf;
unsigned FileIdx;
CharSource* S;
/* Stat the file and remember the values. There a race condition here, /* Stat the file and remember the values. There a race condition here,
* since we cannot use fileno() (non standard identifier in standard * since we cannot use fileno() (non standard identifier in standard
* header file), and therefore not fstat. When using stat with the * header file), and therefore not fstat. When using stat with the
@@ -482,7 +493,6 @@ int NewInputFile (const char* Name)
* if a file has changed in the debugger, we will ignore this problem * if a file has changed in the debugger, we will ignore this problem
* here. * here.
*/ */
struct stat Buf;
if (stat (Name, &Buf) != 0) { if (stat (Name, &Buf) != 0) {
Fatal ("Cannot stat input file `%s': %s", Name, strerror (errno)); Fatal ("Cannot stat input file `%s': %s", Name, strerror (errno));
} }
@@ -501,12 +511,18 @@ int NewInputFile (const char* Name)
S->V.File.Pos.Name = FileIdx; S->V.File.Pos.Name = FileIdx;
S->V.File.Line[0] = '\0'; S->V.File.Line[0] = '\0';
/* Push the path for this file onto the include search lists */
SB_CopyBuf (&Path, Name, FindName (Name) - Name);
SB_Terminate (&Path);
S->V.File.IncSearchPath = PushSearchPath (IncSearchPath, SB_GetConstBuf (&Path));
S->V.File.BinSearchPath = PushSearchPath (BinSearchPath, SB_GetConstBuf (&Path));
SB_Done (&Path);
/* Count active input files */ /* Count active input files */
++FCount; ++FCount;
/* Use this input source */ /* Use this input source */
UseCharSource (S); UseCharSource (S);
}
/* File successfully opened */ /* File successfully opened */
RetCode = 1; RetCode = 1;