From 78057df08bda588aa312c4e5ae0c4788929a38dc Mon Sep 17 00:00:00 2001 From: cuz Date: Mon, 29 May 2000 18:40:56 +0000 Subject: [PATCH] Added functions to remember a set of include search paths and to search for includes in these directories. Extended the scanner to use the new functions. Added several command line switches, including -I (--include-dir) to use the new include search feature. git-svn-id: svn://svn.cc65.org/cc65/trunk@12 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ca65/incpath.c | 171 +++++++++++++++++++++++++++++++++++++++ src/ca65/incpath.h | 62 ++++++++++++++ src/ca65/main.c | 149 +++++++++++++++++++++++++++------- src/ca65/make/gcc.mak | 1 + src/ca65/make/watcom.mak | 2 + src/ca65/scanner.c | 49 +++++++---- 6 files changed, 387 insertions(+), 47 deletions(-) create mode 100644 src/ca65/incpath.c create mode 100644 src/ca65/incpath.h diff --git a/src/ca65/incpath.c b/src/ca65/incpath.c new file mode 100644 index 000000000..ed1f79386 --- /dev/null +++ b/src/ca65/incpath.c @@ -0,0 +1,171 @@ +/*****************************************************************************/ +/* */ +/* incpath.c */ +/* */ +/* Include path handling for the ca65 macro assembler */ +/* */ +/* */ +/* */ +/* (C) 2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#include +#include +#include + +#include "mem.h" +#include "incpath.h" + + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +static char* IncludePath = 0; + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +static char* Add (char* Orig, const char* New) +/* Create a new path from Orig and New, delete Orig, return the result */ +{ + 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); + + /* 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; + unsigned Count; + 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 */ + 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, R_OK) == 0) { + /* The file exists */ + return StrDup (PathName); + } + + /* Skip a list separator if we have one */ + if (*P == ';') { + ++P; + } + } + + /* Not found */ + return 0; +} + + + +void AddIncludePath (const char* NewPath) +/* Add a new include path to the existing one */ +{ + /* Allow a NULL path */ + if (NewPath) { + IncludePath = Add (IncludePath, NewPath); + } +} + + + +char* FindInclude (const char* Name) +/* 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 Find (IncludePath, Name); +} + + + diff --git a/src/ca65/incpath.h b/src/ca65/incpath.h new file mode 100644 index 000000000..7b04e1bfe --- /dev/null +++ b/src/ca65/incpath.h @@ -0,0 +1,62 @@ +/*****************************************************************************/ +/* */ +/* incpath.h */ +/* */ +/* Include path handling for the ca65 macro assembler */ +/* */ +/* */ +/* */ +/* (C) 2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#ifndef INCPATH_H +#define INCPATH_H + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +void AddIncludePath (const char* NewPath); +/* Add a new include path to the existing one */ + +char* FindInclude (const char* Name); +/* Find an include file. Return a pointer to a malloced area that contains + * the complete path, if found, return 0 otherwise. + */ + + + +/* End of incpath.h */ + +#endif + + + diff --git a/src/ca65/main.c b/src/ca65/main.c index 1c65f09d6..ec5dac13d 100644 --- a/src/ca65/main.c +++ b/src/ca65/main.c @@ -44,6 +44,7 @@ #include "error.h" #include "expr.h" #include "global.h" +#include "incpath.h" #include "instr.h" #include "listing.h" #include "macro.h" @@ -69,20 +70,30 @@ static void Usage (void) { fprintf (stderr, "Usage: %s [options] file\n" - "Options:\n" - "\t-g\t\tAdd debug info to object file\n" - "\t-i\t\tIgnore case of symbols\n" - "\t-l\t\tCreate a listing if assembly was ok\n" - "\t-o name\t\tName the output file\n" - "\t-s\t\tEnable smart mode\n" - "\t-v\t\tIncrease verbosity\n" - "\t-D name[=value]\tDefine a symbol\n" - "\t-U\t\tMark unresolved symbols as import\n" - "\t-V\t\tPrint the assembler version\n" - "\t-W n\t\tSet warning level n\n" - "\t--cpu type\tSet cpu type\n" - "\t--pagelength n\tSet the page length for the listing\n" - "\t--smart\t\tEnable smart mode\n", + "Short options:\n" + " -g\t\t\tAdd debug info to object file\n" + " -i\t\t\tIgnore case of symbols\n" + " -l\t\t\tCreate a listing if assembly was ok\n" + " -o name\t\tName the output file\n" + " -s\t\t\tEnable smart mode\n" + " -v\t\t\tIncrease verbosity\n" + " -D name[=value]\tDefine a symbol\n" + " -I dir\t\tSet an include directory search path\n" + " -U\t\t\tMark unresolved symbols as import\n" + " -V\t\t\tPrint the assembler version\n" + " -W n\t\t\tSet warning level n\n" + "\n" + "Long options:\n" + " --auto-import\t\tMark unresolved symbols as import\n" + " --cpu type\t\tSet cpu type\n" + " --debug-info\t\tAdd debug info to object file\n" + " --ignore-case\t\tIgnore case of symbols\n" + " --include-dir dir\tSet an include directory search path\n" + " --listing\t\tCreate a listing if assembly was ok\n" + " --pagelength n\tSet the page length for the listing\n" + " --smart\t\tEnable smart mode\n" + " --verbose\t\tIncrease verbosity\n" + " --version\t\tPrint the assembler version\n", ProgName); exit (EXIT_FAILURE); } @@ -209,6 +220,14 @@ static void DefineSymbol (const char* Def) +static void OptAutoImport (const char* Opt) +/* Mark unresolved symbols as imported */ +{ + AutoImport = 1; +} + + + static void OptCPU (const char* Opt, const char* Arg) /* Handle the --cpu option */ { @@ -233,6 +252,41 @@ static void OptCPU (const char* Opt, const char* Arg) +static void OptDebugInfo (const char* Opt) +/* Add debug info to the object file */ +{ + DbgSyms = 1; +} + + + +static void OptIgnoreCase (const char* Opt) +/* Ignore case on symbols */ +{ + IgnoreCase = 1; +} + + + +static void OptIncludeDir (const char* Opt, const char* Arg) +/* Add an include search path */ +{ + if (Arg == 0) { + NeedArg (Opt); + } + AddIncludePath (Arg); +} + + + +static void OptListing (const char* Opt) +/* Create a listing file */ +{ + Listing = 1; +} + + + static void OptPageLength (const char* Opt, const char* Arg) /* Handle the --pagelength option */ { @@ -258,20 +312,53 @@ static void OptSmart (const char* Opt) +static void OptVerbose (const char* Opt) +/* Increase verbosity */ +{ + ++Verbose; +} + + + +static void OptVersion (const char* Opt) +/* Print the assembler version */ +{ + fprintf (stderr, + "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n", + VER_MAJOR, VER_MINOR, VER_PATCH); +} + + + static void LongOption (int* ArgNum, char* argv []) /* Handle a long command line option */ { const char* Opt = argv [*ArgNum]; const char* Arg = argv [*ArgNum+1]; - if (strcmp (Opt, "--cpu") == 0) { + if (strcmp (Opt, "--auto-import") == 0) { + OptAutoImport (Opt); + } else if (strcmp (Opt, "--cpu") == 0) { OptCPU (Opt, Arg); ++(*ArgNum); + } else if (strcmp (Opt, "--debug-info") == 0) { + OptIgnoreCase (Opt); + } else if (strcmp (Opt, "--ignore-case") == 0) { + OptIgnoreCase (Opt); + } else if (strcmp (Opt, "--include-dir") == 0) { + OptIncludeDir (Opt, Arg); + ++(*ArgNum); + } else if (strcmp (Opt, "--listing") == 0) { + OptListing (Opt); } else if (strcmp (Opt, "--pagelength") == 0) { OptPageLength (Opt, Arg); ++(*ArgNum); } else if (strcmp (Opt, "--smart") == 0) { OptSmart (Opt); + } else if (strcmp (Opt, "--verbose") == 0) { + OptVerbose (Opt); + } else if (strcmp (Opt, "--version") == 0) { + OptVersion (Opt); } else { UnknownOption (Opt); } @@ -316,7 +403,7 @@ static void OneLine (void) */ if (Tok == TOK_EQ) { /* Skip the '=' */ - NextTok (); + NextTok (); /* Define the symbol with the expression following the * '=' */ @@ -356,7 +443,7 @@ static void OneLine (void) HandleInstruction (IVal); } else if (Tok == TOK_IDENT && IsMacro (SVal)) { /* A macro expansion */ - MacExpandStart (); + MacExpandStart (); } } @@ -449,15 +536,15 @@ int main (int argc, char* argv []) break; case 'g': - DbgSyms = 1; + OptDebugInfo (Arg); break; case 'i': - IgnoreCase = 1; + OptIgnoreCase (Arg); break; - case 'l': - Listing = 1; + case 'l': + OptListing (Arg); break; case 'o': @@ -469,21 +556,23 @@ int main (int argc, char* argv []) break; case 'v': - ++Verbose; + OptVerbose (Arg); break; - case 'D': - DefineSymbol (GetArg (&I, argv, 2)); - break; + case 'D': + DefineSymbol (GetArg (&I, argv, 2)); + break; + + case 'I': + OptIncludeDir (Arg, GetArg (&I, argv, 2)); + break; case 'U': - AutoImport = 1; + OptAutoImport (Arg); break; case 'V': - fprintf (stderr, - "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n", - VER_MAJOR, VER_MINOR, VER_PATCH); + OptVersion (Arg); break; case 'W': @@ -526,7 +615,7 @@ int main (int argc, char* argv []) /* If we didn't have any errors, check the unnamed labels */ if (ErrorCount == 0) { - ULabCheck (); + ULabCheck (); } /* If we didn't have any errors, check the symbol table */ diff --git a/src/ca65/make/gcc.mak b/src/ca65/make/gcc.mak index 8d5c69413..97d58b76a 100644 --- a/src/ca65/make/gcc.mak +++ b/src/ca65/make/gcc.mak @@ -13,6 +13,7 @@ OBJS = condasm.o \ fname.o \ fragment.o \ global.o \ + incpath.o \ instr.o \ listing.o \ macpack.o \ diff --git a/src/ca65/make/watcom.mak b/src/ca65/make/watcom.mak index 7ba0b5e0c..0e3c28734 100644 --- a/src/ca65/make/watcom.mak +++ b/src/ca65/make/watcom.mak @@ -70,6 +70,7 @@ OBJS = condasm.obj \ fname.obj \ fragment.obj \ global.obj \ + incpath.obj \ instr.obj \ listing.obj \ macpack.obj \ @@ -113,6 +114,7 @@ FILE expr.obj FILE fname.obj FILE fragment.obj FILE global.obj +FILE incpath.obj FILE instr.obj FILE listing.obj FILE macpack.obj diff --git a/src/ca65/scanner.c b/src/ca65/scanner.c index 8e5261f9d..8a2a376ad 100644 --- a/src/ca65/scanner.c +++ b/src/ca65/scanner.c @@ -44,6 +44,7 @@ #include "error.h" #include "fname.h" #include "global.h" +#include "incpath.h" #include "instr.h" #include "listing.h" #include "macro.h" @@ -323,25 +324,38 @@ void NewInputFile (const char* Name) InputFile* I; FILE* F; - /* Insert a copy of the filename into the list */ + /* Check for nested include overflow */ if (FileCount >= MAX_INPUT_FILES) { Fatal (FAT_MAX_INPUT_FILES); } - Files [FileCount].Name = StrDup (Name); /* First try to open the file */ F = fopen (Name, "r"); if (F == 0) { + char* PathName; + /* Error (fatal error if this is the main file) */ if (ICount == 0) { Fatal (FAT_CANNOT_OPEN_INPUT, Name, strerror (errno)); - } else { + } + + /* We are on include level. Search for the file in the include + * directories. + */ + PathName = FindInclude (Name); + if (PathName == 0 || (F = fopen (PathName, "r")) == 0) { + /* Not found or cannot open, print an error and bail out */ Error (ERR_CANNOT_OPEN_INCLUDE, Name, strerror (errno)); - Xfree (Files [FileCount].Name); } - } else { + /* Free the allocated memory */ + Xfree (PathName); + + } + + /* check again if we do now have an open file */ + if (F != 0) { /* Stat the file and remember the values */ struct stat Buf; @@ -350,25 +364,26 @@ void NewInputFile (const char* Name) } Files [FileCount].MTime = Buf.st_mtime; Files [FileCount].Size = Buf.st_size; + Files [FileCount].Name = StrDup (Name); ++FileCount; /* Create a new state variable and initialize it */ - I = Xmalloc (sizeof (*I)); - I->F = F; - I->Pos.Line = 0; + I = Xmalloc (sizeof (*I)); + I->F = F; + I->Pos.Line = 0; I->Pos.Col = 0; - I->Pos.Name = FileCount; - I->Tok = Tok; + I->Pos.Name = FileCount; + I->Tok = Tok; I->C = C; - I->Line[0] = '\0'; + I->Line[0] = '\0'; - /* Use the new file */ - I->Next = IFile; - IFile = I; - ++ICount; + /* Use the new file */ + I->Next = IFile; + IFile = I; + ++ICount; - /* Prime the pump */ - NextChar (); + /* Prime the pump */ + NextChar (); } }