1 /*****************************************************************************/
5 /* Input file handling */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
40 #include "../common/xmalloc.h"
52 /*****************************************************************************/
54 /*****************************************************************************/
58 /* Maximum count of nested includes */
59 #define MAX_INC_NESTING 16
61 /* Struct that describes an input file */
62 typedef struct IFile IFile;
64 IFile* Next; /* Next file in single linked list */
65 IFile* Active; /* Next file in list of active includes */
66 unsigned Index; /* File index */
67 unsigned Line; /* Line number for this file */
68 FILE* F; /* Input file stream */
69 char Name[1]; /* Name of file (dynamically allocated) */
72 /* List of input files */
73 static unsigned IFileTotal = 0; /* Total number of files */
74 static IFile* IFileList = 0; /* Single linked list of all files */
75 static unsigned IFileCount = 0; /* Number of active input files */
76 static IFile* Input = 0; /* Single linked list of active files */
80 /*****************************************************************************/
82 /*****************************************************************************/
86 static IFile* NewIFile (const char* Name, FILE* F)
87 /* Create and return a new IFile */
89 /* Get the length of the name */
90 unsigned Len = strlen (Name);
92 /* Allocate a IFile structure */
93 IFile* IF = xmalloc (sizeof (IFile) + Len);
95 /* Initialize the fields */
96 IF->Index = ++IFileTotal;
99 memcpy (IF->Name, Name, Len+1);
101 /* Insert the structure into both lists */
102 IF->Next = IFileList;
108 /* Return the new struct */
114 /*****************************************************************************/
116 /*****************************************************************************/
120 void OpenMainFile (const char* Name)
121 /* Open the main file. Will call Fatal() in case of failures. */
123 /* Open the file for reading */
124 FILE* F = fopen (Name, "r");
127 Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
130 /* Setup a new IFile structure */
136 void OpenIncludeFile (const char* Name, unsigned DirSpec)
137 /* Open an include file and insert it into the tables. */
142 /* Check for the maximum include nesting */
143 if (IFileCount > MAX_INC_NESTING) {
144 PPError (ERR_INCLUDE_NESTING);
148 /* Search for the file */
149 N = FindInclude (Name, DirSpec);
151 PPError (ERR_INCLUDE_NOT_FOUND, Name);
158 /* Error opening the file */
159 PPError (ERR_INCLUDE_OPEN_FAILURE, N);
164 /* Allocate a new IFile structure */
167 /* We don't need the full name any longer */
173 static void CloseIncludeFile (void)
174 /* Close an include file and switch to the higher level file. Set Input to
175 * NULL if this was the main file.
178 /* Must have an input file when called */
179 PRECONDITION (Input != 0);
181 /* Close the current input file (we're just reading so no error check) */
184 /* Make this file inactive and the last one active again */
185 Input = Input->Active;
191 /* Get a line from the current input. Returns 0 on end of file. */
201 /* If there is no file open, bail out */
206 /* Read lines until we get one with real contents */
209 while (!Done && Len < LINESIZE) {
211 while (fgets (line + Len, LINESIZE - Len, Input->F) == 0) {
216 /* Leave the current file */
219 /* If this was the last file, bail out */
225 /* We got a new line */
228 /* Remove the trailing newline if we have one */
229 Part = strlen (line + Len);
232 while (Len > 0 && line [Len-1] == '\n') {
237 /* Output the source line in the generated assembler file
240 if (AddSource && line[Start] != '\0') {
241 AddCodeLine ("; %s", line+Start);
244 /* Check if we have a line continuation character at the end. If not,
247 if (Len > 0 && line[Len-1] == '\\') {
248 line[Len-1] = '\n'; /* Replace by newline */
260 const char* GetCurrentFile (void)
261 /* Return the name of the current input file */
264 return "(outside file scope)";
272 unsigned GetCurrentLine (void)
273 /* Return the line number in the current input file */
275 return Input? Input->Line : 0;