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 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
59 /* Input line stuff */
60 static char LineBuf [LINESIZE];
62 const char* lptr = LineBuf;
64 /* Current and next input character */
68 /* Maximum count of nested includes */
69 #define MAX_INC_NESTING 16
71 /* Struct that describes an input file */
72 typedef struct IFile IFile;
74 IFile* Next; /* Next file in single linked list */
75 IFile* Active; /* Next file in list of active includes */
76 unsigned Index; /* File index */
77 unsigned Line; /* Line number for this file */
78 FILE* F; /* Input file stream */
79 char Name[1]; /* Name of file (dynamically allocated) */
82 /* Main file input data */
83 static const IFile* MainFile = 0;
85 /* List of input files */
86 static unsigned IFileTotal = 0; /* Total number of files */
87 static IFile* IFileList = 0; /* Single linked list of all files */
88 static unsigned IFileCount = 0; /* Number of active input files */
89 static IFile* Input = 0; /* Single linked list of active files */
93 /*****************************************************************************/
95 /*****************************************************************************/
99 static IFile* NewIFile (const char* Name, FILE* F)
100 /* Create and return a new IFile */
102 /* Get the length of the name */
103 unsigned Len = strlen (Name);
105 /* Allocate a IFile structure */
106 IFile* IF = xmalloc (sizeof (IFile) + Len);
108 /* Initialize the fields */
109 IF->Index = ++IFileTotal;
112 memcpy (IF->Name, Name, Len+1);
114 /* Insert the structure into both lists */
115 IF->Next = IFileList;
122 /* Return the new struct */
128 /*****************************************************************************/
130 /*****************************************************************************/
134 void OpenMainFile (const char* Name)
135 /* Open the main file. Will call Fatal() in case of failures. */
137 /* Open the file for reading */
138 FILE* F = fopen (Name, "r");
141 Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
144 /* Setup a new IFile structure */
145 MainFile = NewIFile (Name, F);
150 void OpenIncludeFile (const char* Name, unsigned DirSpec)
151 /* Open an include file and insert it into the tables. */
156 /* Check for the maximum include nesting */
157 if (IFileCount > MAX_INC_NESTING) {
158 PPError (ERR_INCLUDE_NESTING);
162 /* Search for the file */
163 N = FindInclude (Name, DirSpec);
165 PPError (ERR_INCLUDE_NOT_FOUND, Name);
172 /* Error opening the file */
173 PPError (ERR_INCLUDE_OPEN_FAILURE, N, strerror (errno));
178 /* Allocate a new IFile structure */
181 /* We don't need the full name any longer */
187 static void CloseIncludeFile (void)
188 /* Close an include file and switch to the higher level file. Set Input to
189 * NULL if this was the main file.
192 /* Must have an input file when called */
193 PRECONDITION (Input != 0);
195 /* Close the current input file (we're just reading so no error check) */
198 /* Make this file inactive and the last one active again */
199 Input = Input->Active;
201 /* Adjust the counter */
207 void ClearLine (void)
208 /* Clear the current input line */
218 void InitLine (const char* Buf)
219 /* Initialize lptr from Buf and read CurC and NextC from the new input line */
233 /* Read the next character from the input stream and make CurC and NextC
234 * valid. If end of line is reached, both are set to NUL, no more lines
235 * are read by this function.
238 if (lptr[0] != '\0') {
254 /* Get a line from the current input. Returns 0 on end of file. */
264 /* If there is no file open, bail out */
269 /* Read lines until we get one with real contents */
272 while (!Done && Len < LINESIZE) {
274 while (fgets (line + Len, LINESIZE - Len, Input->F) == 0) {
279 /* Leave the current file */
282 /* If this was the last file, bail out */
288 /* We got a new line */
291 /* Remove the trailing newline if we have one */
292 Part = strlen (line + Len);
295 while (Len > 0 && line [Len-1] == '\n') {
300 /* Output the source line in the generated assembler file
303 if (AddSource && line[Start] != '\0') {
304 AddCodeLine ("; %s", line+Start);
307 /* Check if we have a line continuation character at the end. If not,
310 if (Len > 0 && line[Len-1] == '\\') {
311 line[Len-1] = '\n'; /* Replace by newline */
317 /* Got a line. Initialize the current and next characters. */
326 const char* GetCurrentFile (void)
327 /* Return the name of the current input file */
331 return MainFile->Name;
333 return "(outside file scope)";
342 unsigned GetCurrentLine (void)
343 /* Return the line number in the current input file */
345 return Input? Input->Line : 0;