1 /*****************************************************************************/
5 /* Input file handling */
9 /* (C) 2000-2004 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 /*****************************************************************************/
39 #include <sys/types.h>
57 /*****************************************************************************/
59 /*****************************************************************************/
63 /* The current input line */
66 /* Current and next input character */
70 /* Maximum count of nested includes */
71 #define MAX_INC_NESTING 16
73 /* Struct that describes an active input file */
74 typedef struct AFile AFile;
76 unsigned Line; /* Line number for this file */
77 FILE* F; /* Input file stream */
78 IFile* Input; /* Points to corresponding IFile */
81 /* List of all input files */
82 static Collection IFiles = STATIC_COLLECTION_INITIALIZER;
84 /* List of all active files */
85 static Collection AFiles = STATIC_COLLECTION_INITIALIZER;
87 /* Input stack used when preprocessing. */
88 static Collection InputStack = STATIC_COLLECTION_INITIALIZER;
92 /*****************************************************************************/
94 /*****************************************************************************/
98 static IFile* NewIFile (const char* Name)
99 /* Create and return a new IFile */
101 /* Get the length of the name */
102 unsigned Len = strlen (Name);
104 /* Allocate a IFile structure */
105 IFile* IF = (IFile*) xmalloc (sizeof (IFile) + Len);
107 /* Initialize the fields */
108 IF->Index = CollCount (&IFiles) + 1;
112 memcpy (IF->Name, Name, Len+1);
114 /* Insert the new structure into the IFile collection */
115 CollAppend (&IFiles, IF);
117 /* Return the new struct */
123 /*****************************************************************************/
125 /*****************************************************************************/
129 static AFile* NewAFile (IFile* IF, FILE* F)
130 /* Create and return a new AFile */
132 /* Allocate a AFile structure */
133 AFile* AF = (AFile*) xmalloc (sizeof (AFile));
135 /* Initialize the fields */
140 /* Increment the usage counter of the corresponding IFile. If this
141 * is the first use, set the file data and output debug info if
144 if (IF->Usage++ == 0) {
146 /* Get file size and modification time. There a race condition here,
147 * since we cannot use fileno() (non standard identifier in standard
148 * header file), and therefore not fstat. When using stat with the
149 * file name, there's a risk that the file was deleted and recreated
150 * while it was open. Since mtime and size are only used to check
151 * if a file has changed in the debugger, we will ignore this problem
155 if (stat (IF->Name, &Buf) != 0) {
157 Fatal ("Cannot stat `%s': %s", IF->Name, strerror (errno));
159 IF->Size = (unsigned long) Buf.st_size;
160 IF->MTime = (unsigned long) Buf.st_mtime;
162 /* Set the debug data */
163 g_fileinfo (IF->Name, IF->Size, IF->MTime);
166 /* Insert the new structure into the AFile collection */
167 CollAppend (&AFiles, AF);
169 /* Return the new struct */
175 static void FreeAFile (AFile* AF)
176 /* Free an AFile structure */
183 /*****************************************************************************/
185 /*****************************************************************************/
189 static IFile* FindFile (const char* Name)
190 /* Find the file with the given name in the list of all files. Since the list
191 * is not large (usually less than 10), I don't care about using hashes or
192 * similar things and do a linear search.
196 for (I = 0; I < CollCount (&IFiles); ++I) {
197 /* Get the file struct */
198 IFile* IF = (IFile*) CollAt (&IFiles, I);
200 if (strcmp (Name, IF->Name) == 0) {
201 /* Found, return the struct */
212 void OpenMainFile (const char* Name)
213 /* Open the main file. Will call Fatal() in case of failures. */
218 /* Setup a new IFile structure for the main file */
219 IFile* IF = NewIFile (Name);
221 /* Open the file for reading */
222 FILE* F = fopen (Name, "r");
225 Fatal ("Cannot open input file `%s': %s", Name, strerror (errno));
228 /* Allocate a new AFile structure for the file */
229 MainFile = NewAFile (IF, F);
231 /* Allocate the input line buffer */
234 /* Update the line infos, so we have a valid line info even at start of
235 * the main file before the first line is read.
237 UpdateLineInfo (MainFile->Input, MainFile->Line, Line);
242 void OpenIncludeFile (const char* Name, unsigned DirSpec)
243 /* Open an include file and insert it into the tables. */
249 /* Check for the maximum include nesting */
250 if (CollCount (&AFiles) > MAX_INC_NESTING) {
251 PPError ("Include nesting too deep");
255 /* Search for the file */
256 N = FindInclude (Name, DirSpec);
258 PPError ("Include file `%s' not found", Name);
262 /* Search the list of all input files for this file. If we don't find
263 * it, create a new IFile object.
270 /* We don't need N any longer, since we may now use IF->Name */
274 F = fopen (IF->Name, "r");
276 /* Error opening the file */
277 PPError ("Cannot open include file `%s': %s", IF->Name, strerror (errno));
281 /* Debugging output */
282 Print (stdout, 1, "Opened include file `%s'\n", IF->Name);
284 /* Allocate a new AFile structure */
285 (void) NewAFile (IF, F);
290 static void CloseIncludeFile (void)
291 /* Close an include file and switch to the higher level file. Set Input to
292 * NULL if this was the main file.
297 /* Get the number of active input files */
298 unsigned AFileCount = CollCount (&AFiles);
300 /* Must have an input file when called */
301 PRECONDITION (AFileCount > 0);
303 /* Get the current active input file */
304 Input = (AFile*) CollLast (&AFiles);
306 /* Close the current input file (we're just reading so no error check) */
309 /* Delete the last active file from the active file collection */
310 CollDelete (&AFiles, AFileCount-1);
312 /* Delete the active file structure */
318 static void GetInputChar (void)
319 /* Read the next character from the input stream and make CurC and NextC
320 * valid. If end of line is reached, both are set to NUL, no more lines
321 * are read by this function.
324 /* Drop all pushed fragments that don't have data left */
325 while (SB_GetIndex (Line) >= SB_GetLen (Line)) {
326 /* Cannot read more from this line, check next line on stack if any */
327 if (CollCount (&InputStack) == 0) {
328 /* This is THE line */
332 Line = CollPop (&InputStack);
335 /* Now get the next characters from the line */
336 if (SB_GetIndex (Line) >= SB_GetLen (Line)) {
339 CurC = SB_AtUnchecked (Line, SB_GetIndex (Line));
340 if (SB_GetIndex (Line) + 1 < SB_GetLen (Line)) {
341 /* NextC comes from this fragment */
342 NextC = SB_AtUnchecked (Line, SB_GetIndex (Line) + 1);
344 /* NextC comes from next fragment */
345 if (CollCount (&InputStack) > 0) {
357 /* Skip the current input character and read the next one from the input
358 * stream. CurC and NextC are valid after the call. If end of line is
359 * reached, both are set to NUL, no more lines are read by this function.
362 /* Skip the last character read */
365 /* Read the next one */
371 void ClearLine (void)
372 /* Clear the current input line */
376 /* Remove all pushed fragments from the input stack */
377 for (I = 0; I < CollCount (&InputStack); ++I) {
378 FreeStrBuf (CollAtUnchecked (&InputStack, I));
380 CollDeleteAll (&InputStack);
382 /* Clear the contents of Line */
390 StrBuf* InitLine (StrBuf* Buf)
391 /* Initialize Line from Buf and read CurC and NextC from the new input line.
392 * The function returns the old input line.
395 StrBuf* OldLine = Line;
397 CurC = SB_LookAt (Buf, SB_GetIndex (Buf));
398 NextC = SB_LookAt (Buf, SB_GetIndex (Buf) + 1);
404 void PushLine (const StrBuf* Buf)
405 /* Push a copy of Buf onto the input stack */
407 CollAppend (&InputStack, Line);
411 /* Make CurC and NextC valid */
418 /* Get a line from the current input. Returns 0 on end of file. */
422 /* Clear the current line */
425 /* If there is no file open, bail out, otherwise get the current input file */
426 if (CollCount (&AFiles) == 0) {
429 Input = CollLast (&AFiles);
431 /* Read characters until we have one complete line */
434 /* Read the next character */
435 int C = fgetc (Input->F);
440 /* Accept files without a newline at the end */
441 if (SB_NotEmpty (Line)) {
446 /* Leave the current file */
449 /* If there is no file open, bail out, otherwise get the
450 * previous input file and start over.
452 if (CollCount (&AFiles) == 0) {
455 Input = CollLast (&AFiles);
459 /* Check for end of line */
462 /* We got a new line */
465 /* If the \n is preceeded by a \r, remove the \r, so we can read
466 * DOS/Windows files under *nix.
468 if (SB_LookAtLast (Line) == '\r') {
472 /* If we don't have a line continuation character at the end,
473 * we're done with this line. Otherwise replace the character
474 * by a newline and continue reading.
476 if (SB_LookAtLast (Line) == '\\') {
477 Line->Buf[Line->Len-1] = '\n';
482 } else if (C != '\0') { /* Ignore embedded NULs */
484 /* Just some character, add it to the line */
485 SB_AppendChar (Line, C);
490 /* Add a termination character to the string buffer */
493 /* Initialize the current and next characters. */
496 /* Create line information for this line */
497 UpdateLineInfo (Input->Input, Input->Line, Line);
505 const char* GetCurrentFile (void)
506 /* Return the name of the current input file */
508 unsigned AFileCount = CollCount (&AFiles);
509 if (AFileCount > 0) {
510 const AFile* AF = (const AFile*) CollAt (&AFiles, AFileCount-1);
511 return AF->Input->Name;
513 /* No open file. Use the main file if we have one. */
514 unsigned IFileCount = CollCount (&IFiles);
515 if (IFileCount > 0) {
516 const IFile* IF = (const IFile*) CollAt (&IFiles, 0);
519 return "(outside file scope)";
526 unsigned GetCurrentLine (void)
527 /* Return the line number in the current input file */
529 unsigned AFileCount = CollCount (&AFiles);
530 if (AFileCount > 0) {
531 const AFile* AF = (const AFile*) CollAt (&AFiles, AFileCount-1);
541 void WriteDependencies (FILE* F, const char* OutputFile)
542 /* Write a makefile dependency list to the given file */
546 /* Get the number of input files */
547 unsigned IFileCount = CollCount (&IFiles);
549 /* Print the output file followed by a tab char */
550 fprintf (F, "%s:\t", OutputFile);
552 /* Loop over all files */
553 for (I = 0; I < IFileCount; ++I) {
554 /* Get the next input file */
555 const IFile* IF = (const IFile*) CollAt (&IFiles, I);
556 /* If this is not the first file, add a space */
557 const char* Format = (I == 0)? "%s" : " %s";
558 /* Print the dependency */
559 fprintf (F, Format, IF->Name);