From 05f72963695f843cca3fd3dac1be0175b470b179 Mon Sep 17 00:00:00 2001 From: uz Date: Sun, 9 May 2010 10:54:15 +0000 Subject: [PATCH] Restructured search path handling. git-svn-id: svn://svn.cc65.org/cc65/trunk@4662 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ca65/incpath.c | 42 +++++------- src/ca65/incpath.h | 17 ++--- src/ca65/main.c | 10 +-- src/ca65/pseudo.c | 4 +- src/ca65/scanner.c | 2 +- src/cc65/incpath.c | 42 ++++++------ src/cc65/incpath.h | 21 +++--- src/cc65/input.c | 21 ++---- src/cc65/input.h | 11 ++- src/cc65/main.c | 5 +- src/cc65/preproc.c | 8 +-- src/common/searchpath.c | 147 ++++++++++++++-------------------------- src/common/searchpath.h | 35 +++++----- src/ld65/filepath.c | 44 ++++++++---- src/ld65/filepath.h | 16 ++--- src/ld65/main.c | 14 ++-- src/sim65/chippath.c | 124 ++++----------------------------- src/sim65/chippath.h | 32 ++++++--- src/sim65/main.c | 3 + src/sim65/scanner.c | 2 +- 20 files changed, 236 insertions(+), 364 deletions(-) diff --git a/src/ca65/incpath.c b/src/ca65/incpath.c index def4102ce..59749d8d2 100644 --- a/src/ca65/incpath.c +++ b/src/ca65/incpath.c @@ -33,43 +33,33 @@ -/* common */ -#include "searchpath.h" - /* ca65 */ #include "incpath.h" /*****************************************************************************/ -/* Code */ +/* Data */ /*****************************************************************************/ -void AddIncludePath (const char* NewPath, unsigned Where) -/* Add a new include path to the existing one */ -{ - AddSearchPath (NewPath, Where); -} +SearchPath* IncSearchPath; /* Standard include path */ +SearchPath* BinSearchPath; /* Binary include path */ -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); -} +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ void ForgetAllIncludePaths (void) /* Remove all include search paths. */ { - ForgetAllSearchPaths (INC_STD | INC_BIN); + ForgetSearchPath (IncSearchPath); + ForgetSearchPath (BinSearchPath); } @@ -77,20 +67,24 @@ void ForgetAllIncludePaths (void) void InitIncludePaths (void) /* Initialize the include path search list */ { - /* Add some standard paths to the include search path */ - AddSearchPath ("", INC_STD); /* Current directory */ - AddSearchPath ("", INC_BIN); + /* Create the search path lists */ + IncSearchPath = NewSearchPath (); + 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 */ #ifdef CA65_INC - AddSearchPath (CA65_INC, INC_STD); + AddSearchPath (IncSearchPath, CA65_INC); #endif /* 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 */ - AddSubSearchPathFromEnv ("CC65_HOME", "asminc", INC_STD); + AddSubSearchPathFromEnv (IncSearchPath, "CC65_HOME", "asminc"); } diff --git a/src/ca65/incpath.h b/src/ca65/incpath.h index 82cd0d2e2..b2484cc96 100644 --- a/src/ca65/incpath.h +++ b/src/ca65/incpath.h @@ -38,14 +38,19 @@ +/* common */ +#include "searchpath.h" + + + /*****************************************************************************/ /* Data */ /*****************************************************************************/ -#define INC_STD 0x0001U /* Add to standard include path */ -#define INC_BIN 0x0002U /* Add to binary include path */ +extern SearchPath* IncSearchPath; /* Standard 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); /* Remove all include search paths. */ diff --git a/src/ca65/main.c b/src/ca65/main.c index aaf0bd802..b552e8363 100644 --- a/src/ca65/main.c +++ b/src/ca65/main.c @@ -360,8 +360,8 @@ static void OptAutoImport (const char* Opt attribute ((unused)), static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg) /* 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) /* Add an include search path */ { - AddIncludePath (Arg, INC_STD); + AddSearchPath (IncSearchPath, Arg); } @@ -1025,8 +1025,8 @@ int main (int argc, char* argv []) SegDump (); } - /* If we didn't have any errors, create the object, listing and - * dependency files + /* If we didn't have any errors, create the object, listing and + * dependency files */ if (ErrorCount == 0) { CreateObjFile (); diff --git a/src/ca65/pseudo.c b/src/ca65/pseudo.c index 07001b305..239075f1b 100644 --- a/src/ca65/pseudo.c +++ b/src/ca65/pseudo.c @@ -1122,7 +1122,7 @@ static void DoIncBin (void) if (F == 0) { /* 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) { /* Not found or cannot open, print an error and bail out */ 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 * if a file has changed in the debugger, we will ignore this problem * here. - */ + */ SB_Terminate (&Name); if (stat (SB_GetConstBuf (&Name), &StatBuf) != 0) { Fatal ("Cannot stat input file `%m%p': %s", &Name, strerror (errno)); diff --git a/src/ca65/scanner.c b/src/ca65/scanner.c index 90ee9fee7..102590abc 100644 --- a/src/ca65/scanner.c +++ b/src/ca65/scanner.c @@ -456,7 +456,7 @@ int NewInputFile (const char* Name) /* We are on include level. Search for the file in the include * directories. */ - PathName = FindInclude (Name, INC_STD); + PathName = SearchFile (IncSearchPath, Name); if (PathName == 0 || (F = fopen (PathName, "r")) == 0) { /* Not found or cannot open, print an error and bail out */ Error ("Cannot open include file `%s': %s", Name, strerror (errno)); diff --git a/src/cc65/incpath.c b/src/cc65/incpath.c index 937a0d9bf..fdbca2a07 100644 --- a/src/cc65/incpath.c +++ b/src/cc65/incpath.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2000-2009, Ullrich von Bassewitz */ +/* (C) 2000-2010, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -33,42 +33,33 @@ -/* common */ -#include "searchpath.h" - /* cc65 */ #include "incpath.h" /*****************************************************************************/ -/* Code */ +/* Data */ /*****************************************************************************/ -void AddIncludePath (const char* NewPath, unsigned Where) -/* Add a new include path to the existing one */ -{ - AddSearchPath (NewPath, Where); -} +SearchPath* SysIncSearchPath; /* System include path */ +SearchPath* UsrIncSearchPath; /* User include path */ -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. - */ -{ - return SearchFile (Name, Where); -} +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ void ForgetAllIncludePaths (void) /* Remove all include search paths. */ { - ForgetAllSearchPaths (INC_SYS | INC_USER); + ForgetSearchPath (SysIncSearchPath); + ForgetSearchPath (UsrIncSearchPath); } @@ -76,19 +67,24 @@ void ForgetAllIncludePaths (void) void InitIncludePaths (void) /* Initialize the include path search list */ { - /* Add some standard paths to the include search path */ - AddSearchPath ("", INC_USER); /* Current directory */ + /* Create the search path lists */ + 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 */ #ifdef CC65_INC - AddSearchPath (CC65_INC, INC_SYS); + AddSearchPath (SysIncSearchPath, CC65_INC); #endif /* 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 */ - AddSubSearchPathFromEnv ("CC65_HOME", "include", INC_SYS); + AddSubSearchPathFromEnv (SysIncSearchPath, "CC65_HOME", "include"); } diff --git a/src/cc65/incpath.h b/src/cc65/incpath.h index 26ab52f45..dba920f6d 100644 --- a/src/cc65/incpath.h +++ b/src/cc65/incpath.h @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2000-2009, Ullrich von Bassewitz */ +/* (C) 2000-2010, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -38,14 +38,19 @@ +/* common */ +#include "searchpath.h" + + + /*****************************************************************************/ -/* Data */ +/* Data */ /*****************************************************************************/ -#define INC_SYS 0x0001 /* Add to system include path */ -#define INC_USER 0x0002 /* Add to user include path */ +extern SearchPath* SysIncSearchPath; /* System 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); /* Remove all include search paths. */ diff --git a/src/cc65/input.c b/src/cc65/input.c index 7b6f808d6..46d380969 100644 --- a/src/cc65/input.c +++ b/src/cc65/input.c @@ -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 */ 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. */ { char* N; @@ -277,7 +268,7 @@ void OpenIncludeFile (const char* Name, unsigned DirSpec) } /* Search for the file */ - N = FindInclude (Name, DirSpec); + N = SearchFile ((IT == IT_SYSINC)? SysIncSearchPath : UsrIncSearchPath, Name); if (N == 0) { PPError ("Include file `%s' not found", Name); return; @@ -288,7 +279,7 @@ void OpenIncludeFile (const char* Name, unsigned DirSpec) */ IF = FindFile (N); 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 */ @@ -590,7 +581,7 @@ static void CreateDepFile (const char* Name, InputType Types) /* Create a dependency file with the given name and place dependencies for * all files with the given types there. */ -{ +{ const char* Target; /* Open the file */ @@ -631,11 +622,11 @@ void CreateDependencies (void) { if (SB_NotEmpty (&DepName)) { CreateDepFile (SB_GetConstBuf (&DepName), - IT_MAIN | IT_USERINC); + IT_MAIN | IT_USRINC); } if (SB_NotEmpty (&FullDepName)) { CreateDepFile (SB_GetConstBuf (&FullDepName), - IT_MAIN | IT_SYSINC | IT_USERINC); + IT_MAIN | IT_SYSINC | IT_USRINC); } } diff --git a/src/cc65/input.h b/src/cc65/input.h index 27ccce77a..dfa0ad03b 100644 --- a/src/cc65/input.h +++ b/src/cc65/input.h @@ -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 */ struct IFile; @@ -72,7 +81,7 @@ extern char NextC; void OpenMainFile (const char* Name); /* 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. */ void NextChar (void); diff --git a/src/cc65/main.c b/src/cc65/main.c index 63081855a..692b7fe7a 100644 --- a/src/cc65/main.c +++ b/src/cc65/main.c @@ -543,8 +543,9 @@ static void OptHelp (const char* Opt attribute ((unused)), static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg) /* Add an include search path */ -{ - AddIncludePath (Arg, INC_SYS | INC_USER); +{ + AddSearchPath (SysIncSearchPath, Arg); + AddSearchPath (UsrIncSearchPath, Arg); } diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 4fcdb40d2..8edeacdd7 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -1112,7 +1112,7 @@ static void DoInclude (void) /* Open an include file. */ { char RTerm; - unsigned DirSpec; + InputType IT; StrBuf Filename = STATIC_STRBUF_INITIALIZER; @@ -1129,12 +1129,12 @@ static void DoInclude (void) case '\"': RTerm = '\"'; - DirSpec = INC_USER; + IT = IT_USRINC; break; case '<': RTerm = '>'; - DirSpec = INC_SYS; + IT = IT_SYSINC; break; default: @@ -1153,7 +1153,7 @@ static void DoInclude (void) /* Check if we got a terminator */ if (CurC == RTerm) { /* Open the include file */ - OpenIncludeFile (SB_GetConstBuf (&Filename), DirSpec); + OpenIncludeFile (SB_GetConstBuf (&Filename), IT); } else if (CurC == '\0') { /* No terminator found */ PPError ("#include expects \"FILENAME\" or "); diff --git a/src/common/searchpath.c b/src/common/searchpath.c index d71326b8b..c44a9c9a8 100644 --- a/src/common/searchpath.c +++ b/src/common/searchpath.c @@ -2,7 +2,7 @@ /* */ /* 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 - * 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) +static void Add (SearchPath* P, const char* New) /* Cleanup a new search path and add it to the list */ { unsigned NewLen; @@ -101,73 +79,41 @@ static void Add (Collection* Paths, const char* New) NewPath [NewLen] = '\0'; /* Add the path to the collection */ - CollAppend (Paths, NewPath); + CollAppend (P, NewPath); } -static char* Find (const Collection* PathList, 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. - */ -{ - char* Name = 0; - StrBuf PathName = AUTO_STRBUF_INITIALIZER; - - /* Start the search */ - unsigned I; - for (I = 0; I < CollCount (PathList); ++I) { - - /* Copy the next path element into the buffer */ - SB_CopyStr (&PathName, CollConstAt (PathList, I)); - - /* Add a path separator and the filename */ - if (SB_NotEmpty (&PathName)) { - SB_AppendChar (&PathName, '/'); - } - SB_AppendStr (&PathName, File); - SB_Terminate (&PathName); - - /* Check if this file exists */ - if (access (SB_GetBuf (&PathName), 0) == 0) { - /* The file exists, we're done */ - Name = xstrdup (SB_GetBuf (&PathName)); - break; - } - } - - /* Cleanup and return the result of the search */ - SB_Done (&PathName); - return Name; +SearchPath* NewSearchPath (void) +/* Create a new, empty search path list */ +{ + return NewCollection (); } -void AddSearchPath (const char* NewPath, unsigned Where) -/* Add a new search path to the existing one */ +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) { - unsigned I; - for (I = 0; I < MAX_SEARCH_PATHS; ++I) { - if (Where & (0x01U << I)) { - Add (&SearchPaths[I], NewPath); - } - } + Add (P, NewPath); } } -void AddSearchPathFromEnv (const char* EnvVar, unsigned Where) -/* Add a search path from an environment variable */ +void AddSearchPathFromEnv (SearchPath* P, const char* EnvVar) +/* Add a search path from an environment variable to the end of an existing + * list. + */ { - AddSearchPath (getenv (EnvVar), Where); + AddSearchPath (P, getenv (EnvVar)); } -void AddSubSearchPathFromEnv (const char* EnvVar, const char* SubDir, unsigned Where) +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. */ @@ -190,14 +136,12 @@ void AddSubSearchPathFromEnv (const char* EnvVar, const char* SubDir, unsigned W } } - /* Add the subdirectory */ + /* Add the subdirectory and terminate the string */ SB_AppendStr (&Dir, SubDir); - - /* Terminate the string */ SB_Terminate (&Dir); /* Add the search path */ - AddSearchPath (SB_GetConstBuf (&Dir), Where); + AddSearchPath (P, SB_GetConstBuf (&Dir)); /* Free the temp buffer */ SB_Done (&Dir); @@ -205,40 +149,51 @@ void AddSubSearchPathFromEnv (const char* EnvVar, const char* SubDir, unsigned W -void ForgetAllSearchPaths (unsigned Where) -/* Forget all search paths in the given lists. */ +void ForgetSearchPath (SearchPath* P) +/* Forget all search paths in the given list */ { 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); - } + for (I = 0; I < CollCount (P); ++I) { + xfree (CollAt (P, I)); } + CollDeleteAll (P); } -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 * area that contains the complete path, if found, return 0 otherwise. */ { + char* Name = 0; + StrBuf PathName = AUTO_STRBUF_INITIALIZER; + + /* Start the search */ 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; - } - } + for (I = 0; I < CollCount (P); ++I) { + + /* Copy the next path element into the buffer */ + SB_CopyStr (&PathName, CollConstAt (P, I)); + + /* Add a path separator and the filename */ + if (SB_NotEmpty (&PathName)) { + SB_AppendChar (&PathName, '/'); + } + SB_AppendStr (&PathName, File); + SB_Terminate (&PathName); + + /* Check if this file exists */ + if (access (SB_GetBuf (&PathName), 0) == 0) { + /* The file exists, we're done */ + Name = xstrdup (SB_GetBuf (&PathName)); + break; + } } - return 0; + + /* Cleanup and return the result of the search */ + SB_Done (&PathName); + return Name; } diff --git a/src/common/searchpath.h b/src/common/searchpath.h index 181560f4c..fa833d139 100644 --- a/src/common/searchpath.h +++ b/src/common/searchpath.h @@ -2,11 +2,11 @@ /* */ /* 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 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -33,11 +33,7 @@ -/* Exports facilities to search files in a list of directories. 8 of these - * 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. - */ +/* Exports facilities to search files in a list of directories. */ @@ -52,8 +48,8 @@ -/* Maximum number of search paths */ -#define MAX_SEARCH_PATHS 8 +/* A search path is a pointer to the list */ +typedef struct Collection SearchPath; @@ -63,21 +59,26 @@ -void AddSearchPath (const char* NewPath, unsigned Where); -/* Add a new search path to the existing one */ +SearchPath* NewSearchPath (void); +/* Create a new, empty search path list */ + +void AddSearchPath (SearchPath* P, const char* NewPath); +/* Add a new search path to the end of an existing list */ -void AddSearchPathFromEnv (const char* EnvVar, unsigned Where); -/* Add a search path from an environment variable */ +void AddSearchPathFromEnv (SearchPath* P, const char* EnvVar); +/* Add a search path from an environment variable to the end of an existing + * list. + */ -void AddSubSearchPathFromEnv (const char* EnvVar, const char* SubDir, unsigned Where); +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. */ -void ForgetAllSearchPaths (unsigned Where); -/* Forget all search paths in the given lists. */ +void ForgetSearchPath (SearchPath* P); +/* 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 * area that contains the complete path, if found, return 0 otherwise. */ diff --git a/src/ld65/filepath.c b/src/ld65/filepath.c index 7cbc24ac1..aa7d547e2 100644 --- a/src/ld65/filepath.c +++ b/src/ld65/filepath.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2003-2009, Ullrich von Bassewitz */ +/* (C) 2003-2010, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -33,14 +33,23 @@ -/* common */ -#include "searchpath.h" - /* ld65 */ #include "filepath.h" +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +SearchPath* LibSearchPath; /* Library path */ +SearchPath* ObjSearchPath; /* Object file path */ +SearchPath* CfgSearchPath; /* Config file path */ + + + /*****************************************************************************/ /* Code */ /*****************************************************************************/ @@ -50,29 +59,36 @@ void InitSearchPaths (void) /* Initialize the path search list */ { + /* Create the search path lists */ + LibSearchPath = NewSearchPath (); + ObjSearchPath = NewSearchPath (); + CfgSearchPath = NewSearchPath (); + /* 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 */ #if defined(LD65_LIB) - AddSearchPath (LD65_LIB, SEARCH_LIB); + AddSearchPath (LibSearchPath, LD65_LIB); #endif #if defined(LD65_OBJ) - AddSearchPath (LD65_OBJ, SEARCH_OBJ); + AddSearchPath (ObjSearchPath, LD65_OBJ); #endif #if defined(LD65_CFG) - AddSearchPath (LD65_CFG, SEARCH_CFG); + AddSearchPath (CfgSearchPath, LD65_CFG); #endif /* Add specific paths from the environment */ - AddSearchPathFromEnv ("LD65_CFG", SEARCH_CFG); - AddSearchPathFromEnv ("LD65_LIB", SEARCH_LIB); - AddSearchPathFromEnv ("LD65_OBJ", SEARCH_OBJ); + AddSearchPathFromEnv (LibSearchPath, "LD65_LIB"); + AddSearchPathFromEnv (ObjSearchPath, "LD65_OBJ"); + AddSearchPathFromEnv (CfgSearchPath, "LD65_CFG"); /* Add paths relative to a main directory defined in an env var */ - AddSubSearchPathFromEnv ("CC65_HOME", "cfg", SEARCH_CFG); - AddSubSearchPathFromEnv ("CC65_HOME", "lib", SEARCH_LIB); - AddSubSearchPathFromEnv ("CC65_HOME", "obj", SEARCH_OBJ); + AddSubSearchPathFromEnv (LibSearchPath, "CC65_HOME", "lib"); + AddSubSearchPathFromEnv (ObjSearchPath, "CC65_HOME", "obj"); + AddSubSearchPathFromEnv (CfgSearchPath, "CC65_HOME", "cfg"); } diff --git a/src/ld65/filepath.h b/src/ld65/filepath.h index f1a501c1a..45d9a90c7 100644 --- a/src/ld65/filepath.h +++ b/src/ld65/filepath.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2003 Ullrich von Bassewitz */ -/* Römerstrasse 52 */ -/* D-70794 Filderstadt */ -/* EMail: uz@cc65.org */ +/* (C) 2003-2010, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -37,7 +37,7 @@ #define FILEPATH_H - + /* common */ #include "searchpath.h" @@ -49,9 +49,9 @@ -#define SEARCH_LIB 0x0001U /* Library path */ -#define SEARCH_OBJ 0x0002U /* Object file path */ -#define SEARCH_CFG 0x0004U /* Config file path */ +extern SearchPath* LibSearchPath; /* Library path */ +extern SearchPath* ObjSearchPath; /* Object file path */ +extern SearchPath* CfgSearchPath; /* Config file path */ diff --git a/src/ld65/main.c b/src/ld65/main.c index 4b7a5353f..10fff402e 100644 --- a/src/ld65/main.c +++ b/src/ld65/main.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 1998-2009, Ullrich von Bassewitz */ +/* (C) 1998-2010, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -177,11 +177,11 @@ static void LinkFile (const char* Name, FILETYPE Type) switch (Type) { case FILETYPE_LIB: - PathName = SearchFile (Name, SEARCH_LIB); + PathName = SearchFile (LibSearchPath, Name); break; case FILETYPE_OBJ: - PathName = SearchFile (Name, SEARCH_OBJ); + PathName = SearchFile (ObjSearchPath, Name); break; default: @@ -282,7 +282,7 @@ static void DefineSymbol (const char* Def) static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg) /* 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"); } /* Search for the file */ - PathName = SearchFile (Arg, SEARCH_CFG); + PathName = SearchFile (CfgSearchPath, Arg); if (PathName == 0) { Error ("Cannot find config file `%s'", Arg); } 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) /* 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) /* Specify an object file search path */ { - AddSearchPath (Arg, SEARCH_OBJ); + AddSearchPath (ObjSearchPath, Arg); } diff --git a/src/sim65/chippath.c b/src/sim65/chippath.c index f39431fcd..d3befeeea 100644 --- a/src/sim65/chippath.c +++ b/src/sim65/chippath.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2000-2002 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 2000-2010, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -43,137 +43,39 @@ # include #endif -/* common */ -#include "xmalloc.h" - /* sim65 */ #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) -/* Create a new path from Orig and New, delete Orig, return the result */ +void InitChipPaths (void) +/* Initialize the chip search path list */ { - unsigned OrigLen, NewLen; - char* NewPath; - - /* Get the length of the original string */ - OrigLen = Orig? strlen (Orig) : 0; - - /* 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); + /* Create the search path list */ + ChipSearchPath = NewSearchPath (); - /* Copy the strings */ - memcpy (NewPath, Orig, OrigLen); - memcpy (NewPath+OrigLen, New, NewLen); - NewPath [OrigLen+NewLen+0] = ';'; - NewPath [OrigLen+NewLen+1] = '\0'; + /* Add the current directory to the search path */ + AddSearchPath (ChipSearchPath, ""); - /* 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); -} - diff --git a/src/sim65/chippath.h b/src/sim65/chippath.h index 82560bb21..1bc864a5a 100644 --- a/src/sim65/chippath.h +++ b/src/sim65/chippath.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2000-2002 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 2000-2010, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -38,19 +38,29 @@ +/* common */ +#include "searchpath.h" + + + /*****************************************************************************/ -/* Code */ +/* Data */ /*****************************************************************************/ -void AddChipPath (const char* NewPath); -/* Add a search path for chips */ +extern SearchPath* ChipSearchPath; /* Search paths for chip libs */ + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + -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. - */ +void InitChipPaths (void); +/* Initialize the chip search path list */ diff --git a/src/sim65/main.c b/src/sim65/main.c index fe8fa2994..cddad7887 100644 --- a/src/sim65/main.c +++ b/src/sim65/main.c @@ -233,6 +233,9 @@ int main (int argc, char* argv[]) /* Initialize the cmdline module */ InitCmdLine (&argc, &argv, "sim65"); + /* Initialize the chip library search paths */ + InitChipPaths (); + /* Parse the command line */ I = 1; while (I < ArgCount) { diff --git a/src/sim65/scanner.c b/src/sim65/scanner.c index eb94e3a68..3e6ab739f 100644 --- a/src/sim65/scanner.c +++ b/src/sim65/scanner.c @@ -305,7 +305,7 @@ void CfgConsume (cfgtok_t T, const char* Msg) /* Skip a token, print an error message if not found */ { if (CfgTok != T) { - CfgError (Msg); + CfgError ("%s", Msg); } CfgNextTok (); } -- 2.39.5