--- /dev/null
+/*****************************************************************************/
+/* */
+/* 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 <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#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);
+}
+
+
+
--- /dev/null
+/*****************************************************************************/
+/* */
+/* 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
+
+
+
#include "error.h"
#include "expr.h"
#include "global.h"
+#include "incpath.h"
#include "instr.h"
#include "listing.h"
#include "macro.h"
{
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);
}
+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 */
{
+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 */
{
+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);
}
*/
if (Tok == TOK_EQ) {
/* Skip the '=' */
- NextTok ();
+ NextTok ();
/* Define the symbol with the expression following the
* '='
*/
HandleInstruction (IVal);
} else if (Tok == TOK_IDENT && IsMacro (SVal)) {
/* A macro expansion */
- MacExpandStart ();
+ MacExpandStart ();
}
}
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':
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':
/* 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 */
fname.o \
fragment.o \
global.o \
+ incpath.o \
instr.o \
listing.o \
macpack.o \
fname.obj \
fragment.obj \
global.obj \
+ incpath.obj \
instr.obj \
listing.obj \
macpack.obj \
FILE fname.obj
FILE fragment.obj
FILE global.obj
+FILE incpath.obj
FILE instr.obj
FILE listing.obj
FILE macpack.obj
#include "error.h"
#include "fname.h"
#include "global.h"
+#include "incpath.h"
#include "instr.h"
#include "listing.h"
#include "macro.h"
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;
}
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 ();
}
}