]> git.sur5r.net Git - cc65/blobdiff - src/cc65/input.c
Fixed several type conversion issues
[cc65] / src / cc65 / input.c
index e84919fe3944a6c5b2051d3b5976aba5dfa4114a..0f254f4921bb6034ad7ec79a62425aae5c46fae6 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 /* common */
 #include "check.h"
 #include "coll.h"
+#include "print.h"
 #include "xmalloc.h"
 
 /* cc65 */
 #include "asmcode.h"
+#include "codegen.h"
 #include "error.h"
-#include "global.h"
 #include "incpath.h"
+#include "lineinfo.h"
 #include "input.h"
 
 
@@ -69,20 +73,12 @@ char NextC = '\0';
 /* Maximum count of nested includes */
 #define MAX_INC_NESTING        16
 
-/* Struct that describes an input file */
-typedef struct IFile IFile;
-struct IFile {
-    unsigned   Index;          /* File index                           */
-    unsigned   Usage;          /* Usage counter                        */
-    char               Name[1];        /* Name of file (dynamically allocated) */
-};
-
 /* 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                    */
-    const char*        Name;           /* Points to corresponding IFile name   */
+    IFile*      Input;          /* Points to corresponding IFile        */
 };
 
 /* List of all input files */
@@ -94,7 +90,7 @@ static Collection AFiles = STATIC_COLLECTION_INITIALIZER;
 
 
 /*****************************************************************************/
-/*                              struct IFile                                */
+/*                              struct IFile                                */
 /*****************************************************************************/
 
 
@@ -111,6 +107,8 @@ static IFile* NewIFile (const char* Name)
     /* Initialize the fields */
     IF->Index = CollCount (&IFiles) + 1;
     IF->Usage = 0;
+    IF->Size  = 0;
+    IF->MTime = 0;
     memcpy (IF->Name, Name, Len+1);
 
     /* Insert the new structure into the IFile collection */
@@ -137,10 +135,26 @@ static AFile* NewAFile (IFile* IF, FILE* F)
     /* Initialize the fields */
     AF->Line  = 0;
     AF->F     = F;
-    AF->Name  = IF->Name;
+    AF->Input = IF;
+
+    /* Increment the usage counter of the corresponding IFile. If this
+     * is the first use, set the file data and output debug info if
+     * requested.
+     */
+    if (IF->Usage++ == 0) {
 
-    /* Increment the usage counter of the corresponding IFile */
-    ++IF->Usage;
+       /* Get file size and modification time */
+       struct stat Buf;
+       if (fstat (fileno (F), &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;
+
+       /* Set the debug data */
+       g_fileinfo (IF->Name, IF->Size, IF->MTime);
+    }
 
     /* Insert the new structure into the AFile collection */
     CollAppend (&AFiles, AF);
@@ -246,6 +260,9 @@ void OpenIncludeFile (const char* Name, unsigned DirSpec)
        return;
     }
 
+    /* Debugging output */
+    Print (stdout, 1, "Opened include file `%s'\n", IF->Name);
+
     /* Allocate a new AFile structure */
     (void) NewAFile (IF, F);
 }
@@ -370,24 +387,17 @@ int NextLine (void)
        /* We got a new line */
        ++Input->Line;
 
-       /* Remove the trailing newline if we have one */
+       /* Remove the trailing cr/lf if we have one. We will ignore both, cr
+        * and lf on all systems since this enables us to compile DOS/Windows
+        * stuff also on unix systems (where fgets does not remove the cr).
+        */
        Part = strlen (line + Len);
        Start = Len;
        Len += Part;
-       while (Len > 0 && line [Len-1] == '\n') {
+       while (Len > 0 && (line[Len-1] == '\n' || line[Len-1] == '\r')) {
            --Len;
        }
        line [Len] = '\0';
-           
-#if 0 
-       /* ######### */
-       /* Output the source line in the generated assembler file
-        * if requested.
-        */
-       if (AddSource && line[Start] != '\0') {
-           AddCodeLine ("; %s", line+Start);
-       }
-#endif
 
        /* Check if we have a line continuation character at the end. If not,
         * we're done.
@@ -402,6 +412,9 @@ int NextLine (void)
     /* Got a line. Initialize the current and next characters. */
     InitLine (line);
 
+    /* Create line information for this line */
+    UpdateLineInfo (Input->Input, Input->Line, line);
+
     /* Done */
     return 1;
 }
@@ -414,7 +427,7 @@ const char* GetCurrentFile (void)
     unsigned AFileCount = CollCount (&AFiles);
     if (AFileCount > 0) {
        const AFile* AF = (const AFile*) CollAt (&AFiles, AFileCount-1);
-       return AF->Name;
+       return AF->Input->Name;
     } else {
        /* No open file. Use the main file if we have one. */
        unsigned IFileCount = CollCount (&IFiles);