]> git.sur5r.net Git - cc65/blobdiff - src/cc65/input.c
Fixed two compiler warnings.
[cc65] / src / cc65 / input.c
index 80a8ace07d075a64c3a0f79d5041698ce04047d9..2a2b8d5b51e7d9aeca9324bf5c6552c0678fc9f1 100644 (file)
@@ -42,6 +42,7 @@
 /* common */
 #include "check.h"
 #include "coll.h"
+#include "fname.h"
 #include "print.h"
 #include "strbuf.h"
 #include "xmalloc.h"
 
 
 
-/* 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;
 
@@ -96,9 +88,10 @@ struct IFile {
 /* Struct that describes an active input file */
 typedef struct AFile AFile;
 struct AFile {
-    unsigned   Line;           /* Line number for this file            */
-    FILE*      F;              /* Input file stream                    */
-    IFile*      Input;          /* Points to corresponding IFile        */
+    unsigned   Line;           /* Line number for this file */
+    FILE*      F;              /* Input file stream */
+    IFile*      Input;          /* Points to corresponding IFile */
+    int         SearchPath;     /* True if we've added a path for this file */
 };
 
 /* List of all input files */
@@ -151,8 +144,12 @@ static IFile* NewIFile (const char* Name, InputType Type)
 
 
 static AFile* NewAFile (IFile* IF, FILE* F)
-/* Create and return a new AFile */
+/* Create a new AFile, push it onto the stack, add the path of the file to
+ * the path search list, and finally return a pointer to the new AFile struct.
+ */
 {
+    StrBuf Path = AUTO_STRBUF_INITIALIZER;
+
     /* Allocate a AFile structure */
     AFile* AF = (AFile*) xmalloc (sizeof (AFile));
 
@@ -167,7 +164,7 @@ static AFile* NewAFile (IFile* IF, FILE* F)
      */
     if (IF->Usage++ == 0) {
 
-       /* Get file size and modification time. There a race condition here,
+       /* Get file size and modification time. There a race condition here,
          * since we cannot use fileno() (non standard identifier in standard
          * header file), and therefore not fstat. When using stat with the
          * file name, there's a risk that the file was deleted and recreated
@@ -175,21 +172,30 @@ static AFile* NewAFile (IFile* IF, FILE* F)
          * if a file has changed in the debugger, we will ignore this problem
          * here.
          */
-       struct stat Buf;
-       if (stat (IF->Name, &Buf) != 0) {
-           /* Error */
-           Fatal ("Cannot stat `%s': %s", IF->Name, strerror (errno));
-       }
+       struct stat Buf;
+       if (stat (IF->Name, &Buf) != 0) {
+           /* Error */
+           Fatal ("Cannot stat `%s': %s", IF->Name, strerror (errno));
+       }
                IF->Size  = (unsigned long) Buf.st_size;
-       IF->MTime = (unsigned long) Buf.st_mtime;
+       IF->MTime = (unsigned long) Buf.st_mtime;
 
-       /* Set the debug data */
-       g_fileinfo (IF->Name, IF->Size, IF->MTime);
+       /* Set the debug data */
+       g_fileinfo (IF->Name, IF->Size, IF->MTime);
     }
 
     /* Insert the new structure into the AFile collection */
     CollAppend (&AFiles, AF);
 
+    /* Get the path of this file and add it as an extra search path.
+     * To avoid file search overhead, we will add one path only once.
+     * This is checked by the PushSearchPath function.
+     */
+    SB_CopyBuf (&Path, IF->Name, FindName (IF->Name) - IF->Name);
+    SB_Terminate (&Path);
+    AF->SearchPath = PushSearchPath (UsrIncSearchPath, SB_GetConstBuf (&Path));
+    SB_Done (&Path);
+
     /* Return the new struct */
     return AF;
 }
@@ -263,7 +269,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 +283,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 +294,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 */
@@ -333,6 +339,11 @@ static void CloseIncludeFile (void)
     /* Delete the last active file from the active file collection */
     CollDelete (&AFiles, AFileCount-1);
 
+    /* If we had added an extra search path for this AFile, remove it */
+    if (Input->SearchPath) {
+        PopSearchPath (UsrIncSearchPath);
+    }
+
     /* Delete the active file structure */
     FreeAFile (Input);
 }
@@ -557,6 +568,21 @@ unsigned GetCurrentLine (void)
 
 
 
+static void WriteEscaped (FILE* F, const char* Name)
+/* Write a file name to a dependency file escaping spaces */
+{
+    while (*Name) {
+        if (*Name == ' ') {
+            /* Escape spaces */
+            fputc ('\\', F);
+        }
+        fputc (*Name, F);
+        ++Name;
+    }
+}
+
+
+
 static void WriteDep (FILE* F, InputType Types)
 /* Helper function. Writes all file names that match Types to the output */
 {
@@ -579,8 +605,8 @@ static void WriteDep (FILE* F, InputType Types)
             fputc (' ', F);
         }
 
-       /* Print the dependency */
-        fputs (IF->Name, F);
+       /* Print the dependency escaping spaces */
+        WriteEscaped (F, IF->Name);
     }
 }
 
@@ -594,11 +620,18 @@ static void CreateDepFile (const char* Name, InputType Types)
     /* Open the file */
     FILE* F = fopen (Name, "w");
     if (F == 0) {
-       Fatal ("Cannot open dependency file `%s': %s", Name, strerror (errno));
+               Fatal ("Cannot open dependency file `%s': %s", Name, strerror (errno));
     }
 
-    /* Print the output file followed by a tab char */
-    fprintf (F, "%s:\t", OutputFilename);
+    /* If a dependency target was given, use it, otherwise use the output
+     * file name as target, followed by a tab character.
+     */
+    if (SB_IsEmpty (&DepTarget)) {
+        WriteEscaped (F, OutputFilename);
+    } else {
+        WriteEscaped (F, SB_GetConstBuf (&DepTarget));
+    }
+    fputs (":\t", F);
 
     /* Write out the dependencies for the output file */
     WriteDep (F, Types);
@@ -622,11 +655,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);
     }
 }