1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-26 08:32:00 +00:00

Restructured search path handling.

git-svn-id: svn://svn.cc65.org/cc65/trunk@4662 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz 2010-05-09 10:54:15 +00:00
parent d95bb2e600
commit 05f7296369
20 changed files with 277 additions and 405 deletions

View File

@ -33,43 +33,33 @@
/* common */
#include "searchpath.h"
/* ca65 */ /* ca65 */
#include "incpath.h" #include "incpath.h"
/*****************************************************************************/
/* Data */
/*****************************************************************************/
SearchPath* IncSearchPath; /* Standard include path */
SearchPath* BinSearchPath; /* Binary include path */
/*****************************************************************************/ /*****************************************************************************/
/* Code */ /* Code */
/*****************************************************************************/ /*****************************************************************************/
void AddIncludePath (const char* NewPath, unsigned Where)
/* Add a new include path to the existing one */
{
AddSearchPath (NewPath, Where);
}
char* FindInclude (const char* Name, unsigned Where)
/* Find an include file. Return a pointer to a malloced area that contains
* the complete path, if found, return 0 otherwise.
*/
{
/* Search in the include directories */
return SearchFile (Name, Where);
}
void ForgetAllIncludePaths (void) void ForgetAllIncludePaths (void)
/* Remove all include search paths. */ /* Remove all include search paths. */
{ {
ForgetAllSearchPaths (INC_STD | INC_BIN); ForgetSearchPath (IncSearchPath);
ForgetSearchPath (BinSearchPath);
} }
@ -77,20 +67,24 @@ void ForgetAllIncludePaths (void)
void InitIncludePaths (void) void InitIncludePaths (void)
/* Initialize the include path search list */ /* Initialize the include path search list */
{ {
/* Add some standard paths to the include search path */ /* Create the search path lists */
AddSearchPath ("", INC_STD); /* Current directory */ IncSearchPath = NewSearchPath ();
AddSearchPath ("", INC_BIN); BinSearchPath = NewSearchPath ();
/* Add the current directory to the search paths */
AddSearchPath (IncSearchPath, "");
AddSearchPath (BinSearchPath, "");
/* Add some compiled in search paths if defined at compile time */ /* Add some compiled in search paths if defined at compile time */
#ifdef CA65_INC #ifdef CA65_INC
AddSearchPath (CA65_INC, INC_STD); AddSearchPath (IncSearchPath, CA65_INC);
#endif #endif
/* Add specific paths from the environment */ /* Add specific paths from the environment */
AddSearchPathFromEnv ("CA65_INC", INC_STD); AddSearchPathFromEnv (IncSearchPath, "CA65_INC");
/* Add paths relative to a main directory defined in an env var */ /* Add paths relative to a main directory defined in an env var */
AddSubSearchPathFromEnv ("CC65_HOME", "asminc", INC_STD); AddSubSearchPathFromEnv (IncSearchPath, "CC65_HOME", "asminc");
} }

View File

@ -38,14 +38,19 @@
/* common */
#include "searchpath.h"
/*****************************************************************************/ /*****************************************************************************/
/* Data */ /* Data */
/*****************************************************************************/ /*****************************************************************************/
#define INC_STD 0x0001U /* Add to standard include path */ extern SearchPath* IncSearchPath; /* Standard include path */
#define INC_BIN 0x0002U /* Add to binary include path */ extern SearchPath* BinSearchPath; /* Binary include path */
@ -55,14 +60,6 @@
void AddIncludePath (const char* NewPath, unsigned Where);
/* Add a new include path to the existing one */
char* FindInclude (const char* Name, unsigned Where);
/* Find an include file. Return a pointer to a malloced area that contains
* the complete path, if found, return 0 otherwise.
*/
void ForgetAllIncludePaths (void); void ForgetAllIncludePaths (void);
/* Remove all include search paths. */ /* Remove all include search paths. */

View File

@ -360,8 +360,8 @@ static void OptAutoImport (const char* Opt attribute ((unused)),
static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg) static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
/* Add an include search path for binaries */ /* Add an include search path for binaries */
{ {
AddIncludePath (Arg, INC_BIN); AddSearchPath (BinSearchPath, Arg);
} }
@ -450,7 +450,7 @@ static void OptIgnoreCase (const char* Opt attribute ((unused)),
static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg) static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
/* Add an include search path */ /* Add an include search path */
{ {
AddIncludePath (Arg, INC_STD); AddSearchPath (IncSearchPath, Arg);
} }
@ -1025,8 +1025,8 @@ int main (int argc, char* argv [])
SegDump (); SegDump ();
} }
/* If we didn't have any errors, create the object, listing and /* If we didn't have any errors, create the object, listing and
* dependency files * dependency files
*/ */
if (ErrorCount == 0) { if (ErrorCount == 0) {
CreateObjFile (); CreateObjFile ();

View File

@ -1122,7 +1122,7 @@ static void DoIncBin (void)
if (F == 0) { if (F == 0) {
/* Search for the file in the binary include directory */ /* Search for the file in the binary include directory */
char* PathName = FindInclude (SB_GetConstBuf (&Name), INC_BIN); char* PathName = SearchFile (BinSearchPath, SB_GetConstBuf (&Name));
if (PathName == 0 || (F = fopen (PathName, "rb")) == 0) { if (PathName == 0 || (F = fopen (PathName, "rb")) == 0) {
/* Not found or cannot open, print an error and bail out */ /* Not found or cannot open, print an error and bail out */
ErrorSkip ("Cannot open include file `%m%p': %s", &Name, strerror (errno)); ErrorSkip ("Cannot open include file `%m%p': %s", &Name, strerror (errno));
@ -1148,7 +1148,7 @@ static void DoIncBin (void)
* while it was open. Since mtime and size are only used to check * while it was open. Since mtime and size are only used to check
* 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.
*/ */
SB_Terminate (&Name); SB_Terminate (&Name);
if (stat (SB_GetConstBuf (&Name), &StatBuf) != 0) { if (stat (SB_GetConstBuf (&Name), &StatBuf) != 0) {
Fatal ("Cannot stat input file `%m%p': %s", &Name, strerror (errno)); Fatal ("Cannot stat input file `%m%p': %s", &Name, strerror (errno));

View File

@ -456,7 +456,7 @@ int NewInputFile (const char* Name)
/* 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.
*/ */
PathName = FindInclude (Name, INC_STD); PathName = SearchFile (IncSearchPath, Name);
if (PathName == 0 || (F = fopen (PathName, "r")) == 0) { if (PathName == 0 || (F = fopen (PathName, "r")) == 0) {
/* Not found or cannot open, print an error and bail out */ /* Not found or cannot open, print an error and bail out */
Error ("Cannot open include file `%s': %s", Name, strerror (errno)); Error ("Cannot open include file `%s': %s", Name, strerror (errno));

View File

@ -6,7 +6,7 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2000-2009, Ullrich von Bassewitz */ /* (C) 2000-2010, Ullrich von Bassewitz */
/* Roemerstrasse 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
@ -33,42 +33,33 @@
/* common */
#include "searchpath.h"
/* cc65 */ /* cc65 */
#include "incpath.h" #include "incpath.h"
/*****************************************************************************/ /*****************************************************************************/
/* Code */ /* Data */
/*****************************************************************************/ /*****************************************************************************/
void AddIncludePath (const char* NewPath, unsigned Where) SearchPath* SysIncSearchPath; /* System include path */
/* Add a new include path to the existing one */ SearchPath* UsrIncSearchPath; /* User include path */
{
AddSearchPath (NewPath, Where);
}
char* FindInclude (const char* Name, unsigned Where) /*****************************************************************************/
/* Find an include file. Return a pointer to a malloced area that contains /* Code */
* the complete path, if found, return 0 otherwise. /*****************************************************************************/
*/
{
return SearchFile (Name, Where);
}
void ForgetAllIncludePaths (void) void ForgetAllIncludePaths (void)
/* Remove all include search paths. */ /* Remove all include search paths. */
{ {
ForgetAllSearchPaths (INC_SYS | INC_USER); ForgetSearchPath (SysIncSearchPath);
ForgetSearchPath (UsrIncSearchPath);
} }
@ -76,19 +67,24 @@ void ForgetAllIncludePaths (void)
void InitIncludePaths (void) void InitIncludePaths (void)
/* Initialize the include path search list */ /* Initialize the include path search list */
{ {
/* Add some standard paths to the include search path */ /* Create the search path lists */
AddSearchPath ("", INC_USER); /* Current directory */ SysIncSearchPath = NewSearchPath ();
UsrIncSearchPath = NewSearchPath ();
/* Add the current path to the user search path list */
AddSearchPath (UsrIncSearchPath, "");
/* Add some compiled in search paths if defined at compile time */ /* Add some compiled in search paths if defined at compile time */
#ifdef CC65_INC #ifdef CC65_INC
AddSearchPath (CC65_INC, INC_SYS); AddSearchPath (SysIncSearchPath, CC65_INC);
#endif #endif
/* Add specific paths from the environment */ /* Add specific paths from the environment */
AddSearchPathFromEnv ("CC65_INC", INC_SYS | INC_USER); AddSearchPathFromEnv (SysIncSearchPath, "CC65_INC");
AddSearchPathFromEnv (UsrIncSearchPath, "CC65_INC");
/* Add paths relative to a main directory defined in an env var */ /* Add paths relative to a main directory defined in an env var */
AddSubSearchPathFromEnv ("CC65_HOME", "include", INC_SYS); AddSubSearchPathFromEnv (SysIncSearchPath, "CC65_HOME", "include");
} }

View File

@ -6,7 +6,7 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2000-2009, Ullrich von Bassewitz */ /* (C) 2000-2010, Ullrich von Bassewitz */
/* Roemerstrasse 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
@ -38,14 +38,19 @@
/* common */
#include "searchpath.h"
/*****************************************************************************/ /*****************************************************************************/
/* Data */ /* Data */
/*****************************************************************************/ /*****************************************************************************/
#define INC_SYS 0x0001 /* Add to system include path */ extern SearchPath* SysIncSearchPath; /* System include path */
#define INC_USER 0x0002 /* Add to user include path */ extern SearchPath* UsrIncSearchPath; /* User include path */
@ -55,14 +60,6 @@
void AddIncludePath (const char* NewPath, unsigned Where);
/* Add a new include path to the existing one */
char* FindInclude (const char* Name, unsigned Where);
/* Find an include file. Return a pointer to a malloced area that contains
* the complete path, if found, return 0 otherwise.
*/
void ForgetAllIncludePaths (void); void ForgetAllIncludePaths (void);
/* Remove all include search paths. */ /* Remove all include search paths. */

View File

@ -63,15 +63,6 @@
/* An enum that describes different types of input files. The members are
* choosen so that it is possible to combine them to bitsets
*/
typedef enum {
IT_MAIN = 0x01, /* Main input file */
IT_SYSINC = 0x02, /* System include file (using <>) */
IT_USERINC = 0x04, /* User include file (using "") */
} InputType;
/* The current input line */ /* The current input line */
StrBuf* Line; StrBuf* Line;
@ -263,7 +254,7 @@ void OpenMainFile (const char* Name)
void OpenIncludeFile (const char* Name, unsigned DirSpec) void OpenIncludeFile (const char* Name, InputType IT)
/* Open an include file and insert it into the tables. */ /* Open an include file and insert it into the tables. */
{ {
char* N; char* N;
@ -277,7 +268,7 @@ void OpenIncludeFile (const char* Name, unsigned DirSpec)
} }
/* Search for the file */ /* Search for the file */
N = FindInclude (Name, DirSpec); N = SearchFile ((IT == IT_SYSINC)? SysIncSearchPath : UsrIncSearchPath, Name);
if (N == 0) { if (N == 0) {
PPError ("Include file `%s' not found", Name); PPError ("Include file `%s' not found", Name);
return; return;
@ -288,7 +279,7 @@ void OpenIncludeFile (const char* Name, unsigned DirSpec)
*/ */
IF = FindFile (N); IF = FindFile (N);
if (IF == 0) { if (IF == 0) {
IF = NewIFile (N, (DirSpec == INC_SYS)? IT_SYSINC : IT_USERINC); IF = NewIFile (N, IT);
} }
/* We don't need N any longer, since we may now use IF->Name */ /* We don't need N any longer, since we may now use IF->Name */
@ -590,7 +581,7 @@ static void CreateDepFile (const char* Name, InputType Types)
/* Create a dependency file with the given name and place dependencies for /* Create a dependency file with the given name and place dependencies for
* all files with the given types there. * all files with the given types there.
*/ */
{ {
const char* Target; const char* Target;
/* Open the file */ /* Open the file */
@ -631,11 +622,11 @@ void CreateDependencies (void)
{ {
if (SB_NotEmpty (&DepName)) { if (SB_NotEmpty (&DepName)) {
CreateDepFile (SB_GetConstBuf (&DepName), CreateDepFile (SB_GetConstBuf (&DepName),
IT_MAIN | IT_USERINC); IT_MAIN | IT_USRINC);
} }
if (SB_NotEmpty (&FullDepName)) { if (SB_NotEmpty (&FullDepName)) {
CreateDepFile (SB_GetConstBuf (&FullDepName), CreateDepFile (SB_GetConstBuf (&FullDepName),
IT_MAIN | IT_SYSINC | IT_USERINC); IT_MAIN | IT_SYSINC | IT_USRINC);
} }
} }

View File

@ -51,6 +51,15 @@
/* An enum that describes different types of input files. The members are
* choosen so that it is possible to combine them to bitsets
*/
typedef enum {
IT_MAIN = 0x01, /* Main input file */
IT_SYSINC = 0x02, /* System include file (using <>) */
IT_USRINC = 0x04, /* User include file (using "") */
} InputType;
/* Forward for an IFile structure */ /* Forward for an IFile structure */
struct IFile; struct IFile;
@ -72,7 +81,7 @@ extern char NextC;
void OpenMainFile (const char* Name); void OpenMainFile (const char* Name);
/* Open the main file. Will call Fatal() in case of failures. */ /* Open the main file. Will call Fatal() in case of failures. */
void OpenIncludeFile (const char* Name, unsigned DirSpec); void OpenIncludeFile (const char* Name, InputType IT);
/* Open an include file and insert it into the tables. */ /* Open an include file and insert it into the tables. */
void NextChar (void); void NextChar (void);

View File

@ -543,8 +543,9 @@ static void OptHelp (const char* Opt attribute ((unused)),
static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg) static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
/* Add an include search path */ /* Add an include search path */
{ {
AddIncludePath (Arg, INC_SYS | INC_USER); AddSearchPath (SysIncSearchPath, Arg);
AddSearchPath (UsrIncSearchPath, Arg);
} }

View File

@ -1112,7 +1112,7 @@ static void DoInclude (void)
/* Open an include file. */ /* Open an include file. */
{ {
char RTerm; char RTerm;
unsigned DirSpec; InputType IT;
StrBuf Filename = STATIC_STRBUF_INITIALIZER; StrBuf Filename = STATIC_STRBUF_INITIALIZER;
@ -1129,12 +1129,12 @@ static void DoInclude (void)
case '\"': case '\"':
RTerm = '\"'; RTerm = '\"';
DirSpec = INC_USER; IT = IT_USRINC;
break; break;
case '<': case '<':
RTerm = '>'; RTerm = '>';
DirSpec = INC_SYS; IT = IT_SYSINC;
break; break;
default: default:
@ -1153,7 +1153,7 @@ static void DoInclude (void)
/* Check if we got a terminator */ /* Check if we got a terminator */
if (CurC == RTerm) { if (CurC == RTerm) {
/* Open the include file */ /* Open the include file */
OpenIncludeFile (SB_GetConstBuf (&Filename), DirSpec); OpenIncludeFile (SB_GetConstBuf (&Filename), IT);
} else if (CurC == '\0') { } else if (CurC == '\0') {
/* No terminator found */ /* No terminator found */
PPError ("#include expects \"FILENAME\" or <FILENAME>"); PPError ("#include expects \"FILENAME\" or <FILENAME>");

View File

@ -2,7 +2,7 @@
/* */ /* */
/* searchpath.h */ /* searchpath.h */
/* */ /* */
/* Search path path handling for ld65 */ /* Handling of search paths */
/* */ /* */
/* */ /* */
/* */ /* */
@ -52,34 +52,12 @@
/*****************************************************************************/ /*****************************************************************************/
/* Data */ /* Code */
/*****************************************************************************/ /*****************************************************************************/
/* A search path list is a collection containing path elements. We have static void Add (SearchPath* P, const char* New)
* several of those.
*/
static Collection SearchPaths[MAX_SEARCH_PATHS] = {
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
};
/*****************************************************************************/
/* Code */
/*****************************************************************************/
static void Add (Collection* Paths, const char* New)
/* Cleanup a new search path and add it to the list */ /* Cleanup a new search path and add it to the list */
{ {
unsigned NewLen; unsigned NewLen;
@ -101,25 +79,102 @@ static void Add (Collection* Paths, const char* New)
NewPath [NewLen] = '\0'; NewPath [NewLen] = '\0';
/* Add the path to the collection */ /* Add the path to the collection */
CollAppend (Paths, NewPath); CollAppend (P, NewPath);
} }
static char* Find (const Collection* PathList, const char* File) SearchPath* NewSearchPath (void)
/* Search for a file in a list of directories. If found, return the complete /* Create a new, empty search path list */
* name including the path in a malloced data area, if not found, return 0. {
return NewCollection ();
}
void AddSearchPath (SearchPath* P, const char* NewPath)
/* Add a new search path to the end of an existing list */
{
/* Allow a NULL path */
if (NewPath) {
Add (P, NewPath);
}
}
void AddSearchPathFromEnv (SearchPath* P, const char* EnvVar)
/* Add a search path from an environment variable to the end of an existing
* list.
*/ */
{ {
AddSearchPath (P, getenv (EnvVar));
}
void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* SubDir)
/* Add a search path from an environment variable, adding a subdirectory to
* the environment variable value.
*/
{
StrBuf Dir = AUTO_STRBUF_INITIALIZER;
const char* EnvVal = getenv (EnvVar);
if (EnvVal == 0) {
/* Not found */
return;
}
/* Copy the environment variable to the buffer */
SB_CopyStr (&Dir, EnvVal);
/* Add a path separator if necessary */
if (SB_NotEmpty (&Dir)) {
if (SB_LookAtLast (&Dir) != '\\' && SB_LookAtLast (&Dir) != '/') {
SB_AppendChar (&Dir, '/');
}
}
/* Add the subdirectory and terminate the string */
SB_AppendStr (&Dir, SubDir);
SB_Terminate (&Dir);
/* Add the search path */
AddSearchPath (P, SB_GetConstBuf (&Dir));
/* Free the temp buffer */
SB_Done (&Dir);
}
void ForgetSearchPath (SearchPath* P)
/* Forget all search paths in the given list */
{
unsigned I;
for (I = 0; I < CollCount (P); ++I) {
xfree (CollAt (P, I));
}
CollDeleteAll (P);
}
char* SearchFile (const SearchPath* P, const char* File)
/* Search for a file in a list of directories. Return a pointer to a malloced
* area that contains the complete path, if found, return 0 otherwise.
*/
{
char* Name = 0; char* Name = 0;
StrBuf PathName = AUTO_STRBUF_INITIALIZER; StrBuf PathName = AUTO_STRBUF_INITIALIZER;
/* Start the search */ /* Start the search */
unsigned I; unsigned I;
for (I = 0; I < CollCount (PathList); ++I) { for (I = 0; I < CollCount (P); ++I) {
/* Copy the next path element into the buffer */ /* Copy the next path element into the buffer */
SB_CopyStr (&PathName, CollConstAt (PathList, I)); SB_CopyStr (&PathName, CollConstAt (P, I));
/* Add a path separator and the filename */ /* Add a path separator and the filename */
if (SB_NotEmpty (&PathName)) { if (SB_NotEmpty (&PathName)) {
@ -143,103 +198,3 @@ static char* Find (const Collection* PathList, const char* File)
void AddSearchPath (const char* NewPath, unsigned Where)
/* Add a new search path to the existing one */
{
/* Allow a NULL path */
if (NewPath) {
unsigned I;
for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
if (Where & (0x01U << I)) {
Add (&SearchPaths[I], NewPath);
}
}
}
}
void AddSearchPathFromEnv (const char* EnvVar, unsigned Where)
/* Add a search path from an environment variable */
{
AddSearchPath (getenv (EnvVar), Where);
}
void AddSubSearchPathFromEnv (const char* EnvVar, const char* SubDir, unsigned Where)
/* Add a search path from an environment variable, adding a subdirectory to
* the environment variable value.
*/
{
StrBuf Dir = AUTO_STRBUF_INITIALIZER;
const char* EnvVal = getenv (EnvVar);
if (EnvVal == 0) {
/* Not found */
return;
}
/* Copy the environment variable to the buffer */
SB_CopyStr (&Dir, EnvVal);
/* Add a path separator if necessary */
if (SB_NotEmpty (&Dir)) {
if (SB_LookAtLast (&Dir) != '\\' && SB_LookAtLast (&Dir) != '/') {
SB_AppendChar (&Dir, '/');
}
}
/* Add the subdirectory */
SB_AppendStr (&Dir, SubDir);
/* Terminate the string */
SB_Terminate (&Dir);
/* Add the search path */
AddSearchPath (SB_GetConstBuf (&Dir), Where);
/* Free the temp buffer */
SB_Done (&Dir);
}
void ForgetAllSearchPaths (unsigned Where)
/* Forget all search paths in the given lists. */
{
unsigned I;
for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
if (Where & (0x01U << I)) {
unsigned J;
Collection* P = &SearchPaths[I];
for (J = 0; J < CollCount (P); ++J) {
xfree (CollAt (P, J));
}
CollDeleteAll (P);
}
}
}
char* SearchFile (const char* Name, unsigned Where)
/* Search for a file in a list of directories. Return a pointer to a malloced
* area that contains the complete path, if found, return 0 otherwise.
*/
{
unsigned I;
for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
if (Where & (0x01U << I)) {
char* Path = Find (&SearchPaths[I], Name);
if (Path) {
/* Found the file */
return Path;
}
}
}
return 0;
}

View File

@ -2,11 +2,11 @@
/* */ /* */
/* searchpath.h */ /* searchpath.h */
/* */ /* */
/* Search path path handling for ld65 */ /* Handling of search paths */
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2000-2009, Ullrich von Bassewitz */ /* (C) 2000-2010, Ullrich von Bassewitz */
/* Roemerstrasse 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
@ -33,11 +33,7 @@
/* Exports facilities to search files in a list of directories. 8 of these /* Exports facilities to search files in a list of directories. */
* lists are managed, and each list can contain an arbitrary number of
* directories. The "Where" argument is actually a bitset, specifying which
* of the search lists should be used when adding paths or searching files.
*/
@ -52,8 +48,8 @@
/* Maximum number of search paths */ /* A search path is a pointer to the list */
#define MAX_SEARCH_PATHS 8 typedef struct Collection SearchPath;
@ -63,21 +59,26 @@
void AddSearchPath (const char* NewPath, unsigned Where); SearchPath* NewSearchPath (void);
/* Add a new search path to the existing one */ /* Create a new, empty search path list */
void AddSearchPathFromEnv (const char* EnvVar, unsigned Where); void AddSearchPath (SearchPath* P, const char* NewPath);
/* Add a search path from an environment variable */ /* Add a new search path to the end of an existing list */
void AddSubSearchPathFromEnv (const char* EnvVar, const char* SubDir, unsigned Where); void AddSearchPathFromEnv (SearchPath* P, const char* EnvVar);
/* Add a search path from an environment variable to the end of an existing
* list.
*/
void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* SubDir);
/* Add a search path from an environment variable, adding a subdirectory to /* Add a search path from an environment variable, adding a subdirectory to
* the environment variable value. * the environment variable value.
*/ */
void ForgetAllSearchPaths (unsigned Where); void ForgetSearchPath (SearchPath* P);
/* Forget all search paths in the given lists. */ /* Forget all search paths in the given list */
char* SearchFile (const char* Name, unsigned Where); char* SearchFile (const SearchPath* P, const char* File);
/* Search for a file in a list of directories. Return a pointer to a malloced /* Search for a file in a list of directories. Return a pointer to a malloced
* area that contains the complete path, if found, return 0 otherwise. * area that contains the complete path, if found, return 0 otherwise.
*/ */

View File

@ -6,7 +6,7 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2003-2009, Ullrich von Bassewitz */ /* (C) 2003-2010, Ullrich von Bassewitz */
/* Roemerstrasse 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
@ -33,14 +33,23 @@
/* common */
#include "searchpath.h"
/* ld65 */ /* ld65 */
#include "filepath.h" #include "filepath.h"
/*****************************************************************************/
/* Data */
/*****************************************************************************/
SearchPath* LibSearchPath; /* Library path */
SearchPath* ObjSearchPath; /* Object file path */
SearchPath* CfgSearchPath; /* Config file path */
/*****************************************************************************/ /*****************************************************************************/
/* Code */ /* Code */
/*****************************************************************************/ /*****************************************************************************/
@ -50,29 +59,36 @@
void InitSearchPaths (void) void InitSearchPaths (void)
/* Initialize the path search list */ /* Initialize the path search list */
{ {
/* Create the search path lists */
LibSearchPath = NewSearchPath ();
ObjSearchPath = NewSearchPath ();
CfgSearchPath = NewSearchPath ();
/* Always search all stuff in the current directory */ /* Always search all stuff in the current directory */
AddSearchPath ("", SEARCH_LIB | SEARCH_OBJ | SEARCH_CFG); AddSearchPath (LibSearchPath, "");
AddSearchPath (ObjSearchPath, "");
AddSearchPath (CfgSearchPath, "");
/* Add some compiled in search paths if defined at compile time */ /* Add some compiled in search paths if defined at compile time */
#if defined(LD65_LIB) #if defined(LD65_LIB)
AddSearchPath (LD65_LIB, SEARCH_LIB); AddSearchPath (LibSearchPath, LD65_LIB);
#endif #endif
#if defined(LD65_OBJ) #if defined(LD65_OBJ)
AddSearchPath (LD65_OBJ, SEARCH_OBJ); AddSearchPath (ObjSearchPath, LD65_OBJ);
#endif #endif
#if defined(LD65_CFG) #if defined(LD65_CFG)
AddSearchPath (LD65_CFG, SEARCH_CFG); AddSearchPath (CfgSearchPath, LD65_CFG);
#endif #endif
/* Add specific paths from the environment */ /* Add specific paths from the environment */
AddSearchPathFromEnv ("LD65_CFG", SEARCH_CFG); AddSearchPathFromEnv (LibSearchPath, "LD65_LIB");
AddSearchPathFromEnv ("LD65_LIB", SEARCH_LIB); AddSearchPathFromEnv (ObjSearchPath, "LD65_OBJ");
AddSearchPathFromEnv ("LD65_OBJ", SEARCH_OBJ); AddSearchPathFromEnv (CfgSearchPath, "LD65_CFG");
/* Add paths relative to a main directory defined in an env var */ /* Add paths relative to a main directory defined in an env var */
AddSubSearchPathFromEnv ("CC65_HOME", "cfg", SEARCH_CFG); AddSubSearchPathFromEnv (LibSearchPath, "CC65_HOME", "lib");
AddSubSearchPathFromEnv ("CC65_HOME", "lib", SEARCH_LIB); AddSubSearchPathFromEnv (ObjSearchPath, "CC65_HOME", "obj");
AddSubSearchPathFromEnv ("CC65_HOME", "obj", SEARCH_OBJ); AddSubSearchPathFromEnv (CfgSearchPath, "CC65_HOME", "cfg");
} }

View File

@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2003 Ullrich von Bassewitz */ /* (C) 2003-2010, Ullrich von Bassewitz */
/* Römerstrasse 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@ -37,7 +37,7 @@
#define FILEPATH_H #define FILEPATH_H
/* common */ /* common */
#include "searchpath.h" #include "searchpath.h"
@ -49,9 +49,9 @@
#define SEARCH_LIB 0x0001U /* Library path */ extern SearchPath* LibSearchPath; /* Library path */
#define SEARCH_OBJ 0x0002U /* Object file path */ extern SearchPath* ObjSearchPath; /* Object file path */
#define SEARCH_CFG 0x0004U /* Config file path */ extern SearchPath* CfgSearchPath; /* Config file path */

View File

@ -6,7 +6,7 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998-2009, Ullrich von Bassewitz */ /* (C) 1998-2010, Ullrich von Bassewitz */
/* Roemerstrasse 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
@ -177,11 +177,11 @@ static void LinkFile (const char* Name, FILETYPE Type)
switch (Type) { switch (Type) {
case FILETYPE_LIB: case FILETYPE_LIB:
PathName = SearchFile (Name, SEARCH_LIB); PathName = SearchFile (LibSearchPath, Name);
break; break;
case FILETYPE_OBJ: case FILETYPE_OBJ:
PathName = SearchFile (Name, SEARCH_OBJ); PathName = SearchFile (ObjSearchPath, Name);
break; break;
default: default:
@ -282,7 +282,7 @@ static void DefineSymbol (const char* Def)
static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg) static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
/* Specify a config file search path */ /* Specify a config file search path */
{ {
AddSearchPath (Arg, SEARCH_CFG); AddSearchPath (CfgSearchPath, Arg);
} }
@ -296,7 +296,7 @@ static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
Error ("Cannot use -C/-t twice"); Error ("Cannot use -C/-t twice");
} }
/* Search for the file */ /* Search for the file */
PathName = SearchFile (Arg, SEARCH_CFG); PathName = SearchFile (CfgSearchPath, Arg);
if (PathName == 0) { if (PathName == 0) {
Error ("Cannot find config file `%s'", Arg); Error ("Cannot find config file `%s'", Arg);
} else { } else {
@ -405,7 +405,7 @@ static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg) static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
/* Specify a library file search path */ /* Specify a library file search path */
{ {
AddSearchPath (Arg, SEARCH_LIB); AddSearchPath (LibSearchPath, Arg);
} }
@ -441,7 +441,7 @@ static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg) static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
/* Specify an object file search path */ /* Specify an object file search path */
{ {
AddSearchPath (Arg, SEARCH_OBJ); AddSearchPath (ObjSearchPath, Arg);
} }

View File

@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2000-2002 Ullrich von Bassewitz */ /* (C) 2000-2010, Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Roemerstrasse 52 */
/* D-70597 Stuttgart */ /* D-70794 Filderstadt */
/* EMail: uz@musoftware.de */ /* EMail: uz@cc65.org */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@ -43,137 +43,39 @@
# include <unistd.h> # include <unistd.h>
#endif #endif
/* common */
#include "xmalloc.h"
/* sim65 */ /* sim65 */
#include "chippath.h" #include "chippath.h"
/*****************************************************************************/ /*****************************************************************************/
/* Data */ /* Data */
/*****************************************************************************/ /*****************************************************************************/
static char* ChipPath = 0; SearchPath* ChipSearchPath; /* Search paths for chip libs */
/*****************************************************************************/ /*****************************************************************************/
/* Code */ /* Code */
/*****************************************************************************/ /*****************************************************************************/
static char* Add (char* Orig, const char* New) void InitChipPaths (void)
/* Create a new path from Orig and New, delete Orig, return the result */ /* Initialize the chip search path list */
{ {
unsigned OrigLen, NewLen; /* Create the search path list */
char* NewPath; ChipSearchPath = NewSearchPath ();
/* Get the length of the original string */ /* Add the current directory to the search path */
OrigLen = Orig? strlen (Orig) : 0; AddSearchPath (ChipSearchPath, "");
/* Get the length of the new path */
NewLen = strlen (New);
/* Check for a trailing path separator and remove it */
if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) {
--NewLen;
}
/* Allocate memory for the new string */
NewPath = xmalloc (OrigLen + NewLen + 2);
/* Copy the strings */
memcpy (NewPath, Orig, OrigLen);
memcpy (NewPath+OrigLen, New, NewLen);
NewPath [OrigLen+NewLen+0] = ';';
NewPath [OrigLen+NewLen+1] = '\0';
/* Delete the original path */
xfree (Orig);
/* Return the new path */
return NewPath;
} }
static char* Find (const char* Path, const char* File)
/* Search for a file in a list of directories. If found, return the complete
* name including the path in a malloced data area, if not found, return 0.
*/
{
const char* P;
int Max;
char PathName [FILENAME_MAX];
/* Initialize variables */
Max = sizeof (PathName) - strlen (File) - 2;
if (Max < 0) {
return 0;
}
P = Path;
/* Handle a NULL pointer as replacement for an empty string */
if (P == 0) {
P = "";
}
/* Start the search */
while (*P) {
/* Copy the next path element into the buffer */
int Count = 0;
while (*P != '\0' && *P != ';' && Count < Max) {
PathName [Count++] = *P++;
}
/* Add a path separator and the filename */
if (Count) {
PathName [Count++] = '/';
}
strcpy (PathName + Count, File);
/* Check if this file exists */
if (access (PathName, 0) == 0) {
/* The file exists */
return xstrdup (PathName);
}
/* Skip a list separator if we have one */
if (*P == ';') {
++P;
}
}
/* Not found */
return 0;
}
void AddChipPath (const char* NewPath)
/* Add a search path for chips */
{
/* Allow a NULL path */
if (NewPath) {
ChipPath = Add (ChipPath, NewPath);
}
}
char* FindChipLib (const char* LibName)
/* Find a chip library. Return a pointer to a malloced area that contains
* the complete path, if found, return 0 otherwise.
*/
{
/* Search in the include directories */
return Find (ChipPath, LibName);
}

View File

@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2000-2002 Ullrich von Bassewitz */ /* (C) 2000-2010, Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Roemerstrasse 52 */
/* D-70597 Stuttgart */ /* D-70794 Filderstadt */
/* EMail: uz@musoftware.de */ /* EMail: uz@cc65.org */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@ -38,19 +38,29 @@
/* common */
#include "searchpath.h"
/*****************************************************************************/
/* Data */
/*****************************************************************************/
extern SearchPath* ChipSearchPath; /* Search paths for chip libs */
/*****************************************************************************/ /*****************************************************************************/
/* Code */ /* Code */
/*****************************************************************************/ /*****************************************************************************/
void AddChipPath (const char* NewPath); void InitChipPaths (void);
/* Add a search path for chips */ /* Initialize the chip search path list */
char* FindChipLib (const char* LibName);
/* Find a chip library. Return a pointer to a malloced area that contains
* the complete path, if found, return 0 otherwise.
*/

View File

@ -233,6 +233,9 @@ int main (int argc, char* argv[])
/* Initialize the cmdline module */ /* Initialize the cmdline module */
InitCmdLine (&argc, &argv, "sim65"); InitCmdLine (&argc, &argv, "sim65");
/* Initialize the chip library search paths */
InitChipPaths ();
/* Parse the command line */ /* Parse the command line */
I = 1; I = 1;
while (I < ArgCount) { while (I < ArgCount) {

View File

@ -305,7 +305,7 @@ void CfgConsume (cfgtok_t T, const char* Msg)
/* Skip a token, print an error message if not found */ /* Skip a token, print an error message if not found */
{ {
if (CfgTok != T) { if (CfgTok != T) {
CfgError (Msg); CfgError ("%s", Msg);
} }
CfgNextTok (); CfgNextTok ();
} }