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];
60 const char* lptr = LineBuf;
62 /* Current and next input character */
66 /* Maximum count of nested includes */
67 #define MAX_INC_NESTING 16
69 /* Struct that describes an input file */
70 typedef struct IFile IFile;
72 IFile* Next; /* Next file in single linked list */
73 IFile* Active; /* Next file in list of active includes */
74 unsigned Index; /* File index */
75 unsigned Line; /* Line number for this file */
76 FILE* F; /* Input file stream */
77 char Name[1]; /* Name of file (dynamically allocated) */
80 /* Main file input data */
81 static const IFile* MainFile = 0;
83 /* List of input files */
84 static unsigned IFileTotal = 0; /* Total number of files */
85 static IFile* IFileList = 0; /* Single linked list of all files */
86 static unsigned IFileCount = 0; /* Number of active input files */
87 static IFile* Input = 0; /* Single linked list of active files */
91 /*****************************************************************************/
93 /*****************************************************************************/
97 static IFile* NewIFile (const char* Name, FILE* F)
98 /* Create and return a new IFile */
100 /* Get the length of the name */
101 unsigned Len = strlen (Name);
103 /* Allocate a IFile structure */
104 IFile* IF = xmalloc (sizeof (IFile) + Len);
106 /* Initialize the fields */
107 IF->Index = ++IFileTotal;
110 memcpy (IF->Name, Name, Len+1);
112 /* Insert the structure into both lists */
113 IF->Next = IFileList;
120 /* Return the new struct */
126 /*****************************************************************************/
128 /*****************************************************************************/
132 void OpenMainFile (const char* Name)
133 /* Open the main file. Will call Fatal() in case of failures. */
135 /* Open the file for reading */
136 FILE* F = fopen (Name, "r");
139 Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
142 /* Setup a new IFile structure */
143 MainFile = NewIFile (Name, F);
148 void OpenIncludeFile (const char* Name, unsigned DirSpec)
149 /* Open an include file and insert it into the tables. */
154 /* Check for the maximum include nesting */
155 if (IFileCount > MAX_INC_NESTING) {
156 PPError (ERR_INCLUDE_NESTING);
160 /* Search for the file */
161 N = FindInclude (Name, DirSpec);
163 PPError (ERR_INCLUDE_NOT_FOUND, Name);
170 /* Error opening the file */
171 PPError (ERR_INCLUDE_OPEN_FAILURE, N, strerror (errno));
176 /* Allocate a new IFile structure */
179 /* We don't need the full name any longer */
185 static void CloseIncludeFile (void)
186 /* Close an include file and switch to the higher level file. Set Input to
187 * NULL if this was the main file.
190 /* Must have an input file when called */
191 PRECONDITION (Input != 0);
193 /* Close the current input file (we're just reading so no error check) */
196 /* Make this file inactive and the last one active again */
197 Input = Input->Active;
199 /* Adjust the counter */
205 void ClearLine (void)
206 /* Clear the current input line */
216 void InitLine (const char* Buf)
217 /* Initialize lptr from Buf and read CurC and NextC from the new input line */
231 /* Read the next character from the input stream and make CurC and NextC
232 * valid. If end of line is reached, both are set to NUL, no more lines
233 * are read by this function.
236 if (lptr[0] != '\0') {
252 /* Get a line from the current input. Returns 0 on end of file. */
262 /* If there is no file open, bail out */
267 /* Read lines until we get one with real contents */
270 while (!Done && Len < LINESIZE) {
272 while (fgets (line + Len, LINESIZE - Len, Input->F) == 0) {
277 /* Leave the current file */
280 /* If this was the last file, bail out */
286 /* We got a new line */
289 /* Remove the trailing newline if we have one */
290 Part = strlen (line + Len);
293 while (Len > 0 && line [Len-1] == '\n') {
298 /* Output the source line in the generated assembler file
301 if (AddSource && line[Start] != '\0') {
302 AddCodeLine ("; %s", line+Start);
305 /* Check if we have a line continuation character at the end. If not,
308 if (Len > 0 && line[Len-1] == '\\') {
309 line[Len-1] = '\n'; /* Replace by newline */
315 /* Got a line. Initialize the current and next characters. */
324 const char* GetCurrentFile (void)
325 /* Return the name of the current input file */
329 return MainFile->Name;
331 return "(outside file scope)";
340 unsigned GetCurrentLine (void)
341 /* Return the line number in the current input file */
343 return Input? Input->Line : 0;