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"
51 /*****************************************************************************/
53 /*****************************************************************************/
57 /* Input line stuff */
58 static char LineBuf [LINESIZE];
62 /* Maximum count of nested includes */
63 #define MAX_INC_NESTING 16
65 /* Struct that describes an input file */
66 typedef struct IFile IFile;
68 IFile* Next; /* Next file in single linked list */
69 IFile* Active; /* Next file in list of active includes */
70 unsigned Index; /* File index */
71 unsigned Line; /* Line number for this file */
72 FILE* F; /* Input file stream */
73 char Name[1]; /* Name of file (dynamically allocated) */
76 /* List of input files */
77 static unsigned IFileTotal = 0; /* Total number of files */
78 static IFile* IFileList = 0; /* Single linked list of all files */
79 static unsigned IFileCount = 0; /* Number of active input files */
80 static IFile* Input = 0; /* Single linked list of active files */
84 /*****************************************************************************/
86 /*****************************************************************************/
90 static IFile* NewIFile (const char* Name, FILE* F)
91 /* Create and return a new IFile */
93 /* Get the length of the name */
94 unsigned Len = strlen (Name);
96 /* Allocate a IFile structure */
97 IFile* IF = xmalloc (sizeof (IFile) + Len);
99 /* Initialize the fields */
100 IF->Index = ++IFileTotal;
103 memcpy (IF->Name, Name, Len+1);
105 /* Insert the structure into both lists */
106 IF->Next = IFileList;
112 /* Return the new struct */
118 /*****************************************************************************/
120 /*****************************************************************************/
124 void OpenMainFile (const char* Name)
125 /* Open the main file. Will call Fatal() in case of failures. */
127 /* Open the file for reading */
128 FILE* F = fopen (Name, "r");
131 Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
134 /* Setup a new IFile structure */
140 void OpenIncludeFile (const char* Name, unsigned DirSpec)
141 /* Open an include file and insert it into the tables. */
146 /* Check for the maximum include nesting */
147 if (IFileCount > MAX_INC_NESTING) {
148 PPError (ERR_INCLUDE_NESTING);
152 /* Search for the file */
153 N = FindInclude (Name, DirSpec);
155 PPError (ERR_INCLUDE_NOT_FOUND, Name);
162 /* Error opening the file */
163 PPError (ERR_INCLUDE_OPEN_FAILURE, N, strerror (errno));
168 /* Allocate a new IFile structure */
171 /* We don't need the full name any longer */
177 static void CloseIncludeFile (void)
178 /* Close an include file and switch to the higher level file. Set Input to
179 * NULL if this was the main file.
182 /* Must have an input file when called */
183 PRECONDITION (Input != 0);
185 /* Close the current input file (we're just reading so no error check) */
188 /* Make this file inactive and the last one active again */
189 Input = Input->Active;
195 /* Get a line from the current input. Returns 0 on end of file. */
205 /* If there is no file open, bail out */
210 /* Read lines until we get one with real contents */
213 while (!Done && Len < LINESIZE) {
215 while (fgets (line + Len, LINESIZE - Len, Input->F) == 0) {
220 /* Leave the current file */
223 /* If this was the last file, bail out */
229 /* We got a new line */
232 /* Remove the trailing newline if we have one */
233 Part = strlen (line + Len);
236 while (Len > 0 && line [Len-1] == '\n') {
241 /* Output the source line in the generated assembler file
244 if (AddSource && line[Start] != '\0') {
245 AddCodeLine ("; %s", line+Start);
248 /* Check if we have a line continuation character at the end. If not,
251 if (Len > 0 && line[Len-1] == '\\') {
252 line[Len-1] = '\n'; /* Replace by newline */
264 void ClearLine (void)
265 /* Clear the current input line */
273 const char* GetCurrentFile (void)
274 /* Return the name of the current input file */
277 return "(outside file scope)";
285 unsigned GetCurrentLine (void)
286 /* Return the line number in the current input file */
288 return Input? Input->Line : 0;
294 /* Get the next char in input stream (the one behind the current one) */
299 return lptr[1] & 0xFF;
306 /* Get the current character in the input stream and advance line
307 * pointer (unless already at end of line).
313 return (*lptr++ & 0xFF);
320 /* Get the current character in the input stream and advance line
321 * pointer (no end of line check is performed).
324 return (*lptr++ & 0xFF);