1 /*****************************************************************************/
5 /* Input file handling */
9 /* (C) 2000-2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 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 /*****************************************************************************/
60 /*****************************************************************************/
62 /*****************************************************************************/
66 /* The current input line */
69 /* Current and next input character */
73 /* Maximum count of nested includes */
74 #define MAX_INC_NESTING 16
76 /* Struct that describes an input file */
77 typedef struct IFile IFile;
79 unsigned Index; /* File index */
80 unsigned Usage; /* Usage counter */
81 unsigned long Size; /* File size */
82 unsigned long MTime; /* Time of last modification */
83 InputType Type; /* Type of input file */
84 char Name[1]; /* Name of file (dynamically allocated) */
87 /* Struct that describes an active input file */
88 typedef struct AFile AFile;
90 unsigned Line; /* Line number for this file */
91 FILE* F; /* Input file stream */
92 IFile* Input; /* Points to corresponding IFile */
93 int SearchPath; /* True if we've added a path for this file */
96 /* List of all input files */
97 static Collection IFiles = STATIC_COLLECTION_INITIALIZER;
99 /* List of all active files */
100 static Collection AFiles = STATIC_COLLECTION_INITIALIZER;
102 /* Input stack used when preprocessing. */
103 static Collection InputStack = STATIC_COLLECTION_INITIALIZER;
107 /*****************************************************************************/
109 /*****************************************************************************/
113 static IFile* NewIFile (const char* Name, InputType Type)
114 /* Create and return a new IFile */
116 /* Get the length of the name */
117 unsigned Len = strlen (Name);
119 /* Allocate a IFile structure */
120 IFile* IF = (IFile*) xmalloc (sizeof (IFile) + Len);
122 /* Initialize the fields */
123 IF->Index = CollCount (&IFiles) + 1;
128 memcpy (IF->Name, Name, Len+1);
130 /* Insert the new structure into the IFile collection */
131 CollAppend (&IFiles, IF);
133 /* Return the new struct */
139 /*****************************************************************************/
141 /*****************************************************************************/
145 static AFile* NewAFile (IFile* IF, FILE* F)
146 /* Create a new AFile, push it onto the stack, add the path of the file to
147 ** the path search list, and finally return a pointer to the new AFile struct.
150 StrBuf Path = AUTO_STRBUF_INITIALIZER;
152 /* Allocate a AFile structure */
153 AFile* AF = (AFile*) xmalloc (sizeof (AFile));
155 /* Initialize the fields */
160 /* Increment the usage counter of the corresponding IFile. If this
161 ** is the first use, set the file data and output debug info if
164 if (IF->Usage++ == 0) {
166 /* Get file size and modification time. There a race condition here,
167 ** since we cannot use fileno() (non standard identifier in standard
168 ** header file), and therefore not fstat. When using stat with the
169 ** file name, there's a risk that the file was deleted and recreated
170 ** while it was open. Since mtime and size are only used to check
171 ** if a file has changed in the debugger, we will ignore this problem
175 if (FileStat (IF->Name, &Buf) != 0) {
177 Fatal ("Cannot stat '%s': %s", IF->Name, strerror (errno));
179 IF->Size = (unsigned long) Buf.st_size;
180 IF->MTime = (unsigned long) Buf.st_mtime;
182 /* Set the debug data */
183 g_fileinfo (IF->Name, IF->Size, IF->MTime);
186 /* Insert the new structure into the AFile collection */
187 CollAppend (&AFiles, AF);
189 /* Get the path of this file and add it as an extra search path.
190 ** To avoid file search overhead, we will add one path only once.
191 ** This is checked by the PushSearchPath function.
193 SB_CopyBuf (&Path, IF->Name, FindName (IF->Name) - IF->Name);
194 SB_Terminate (&Path);
195 AF->SearchPath = PushSearchPath (UsrIncSearchPath, SB_GetConstBuf (&Path));
198 /* Return the new struct */
204 static void FreeAFile (AFile* AF)
205 /* Free an AFile structure */
212 /*****************************************************************************/
214 /*****************************************************************************/
218 static IFile* FindFile (const char* Name)
219 /* Find the file with the given name in the list of all files. Since the list
220 ** is not large (usually less than 10), I don't care about using hashes or
221 ** similar things and do a linear search.
225 for (I = 0; I < CollCount (&IFiles); ++I) {
226 /* Get the file struct */
227 IFile* IF = (IFile*) CollAt (&IFiles, I);
229 if (strcmp (Name, IF->Name) == 0) {
230 /* Found, return the struct */
241 void OpenMainFile (const char* Name)
242 /* Open the main file. Will call Fatal() in case of failures. */
247 /* Setup a new IFile structure for the main file */
248 IFile* IF = NewIFile (Name, IT_MAIN);
250 /* Open the file for reading */
251 FILE* F = fopen (Name, "r");
254 Fatal ("Cannot open input file '%s': %s", Name, strerror (errno));
257 /* Allocate a new AFile structure for the file */
258 MainFile = NewAFile (IF, F);
260 /* Allocate the input line buffer */
263 /* Update the line infos, so we have a valid line info even at start of
264 ** the main file before the first line is read.
266 UpdateLineInfo (MainFile->Input, MainFile->Line, Line);
271 void OpenIncludeFile (const char* Name, InputType IT)
272 /* Open an include file and insert it into the tables. */
278 /* Check for the maximum include nesting */
279 if (CollCount (&AFiles) > MAX_INC_NESTING) {
280 PPError ("Include nesting too deep");
284 /* Search for the file */
285 N = SearchFile ((IT == IT_SYSINC)? SysIncSearchPath : UsrIncSearchPath, Name);
287 PPError ("Include file '%s' not found", Name);
291 /* Search the list of all input files for this file. If we don't find
292 ** it, create a new IFile object.
296 IF = NewIFile (N, IT);
299 /* We don't need N any longer, since we may now use IF->Name */
303 F = fopen (IF->Name, "r");
305 /* Error opening the file */
306 PPError ("Cannot open include file '%s': %s", IF->Name, strerror (errno));
310 /* Debugging output */
311 Print (stdout, 1, "Opened include file '%s'\n", IF->Name);
313 /* Allocate a new AFile structure */
314 (void) NewAFile (IF, F);
319 static void CloseIncludeFile (void)
320 /* Close an include file and switch to the higher level file. Set Input to
321 ** NULL if this was the main file.
326 /* Get the number of active input files */
327 unsigned AFileCount = CollCount (&AFiles);
329 /* Must have an input file when called */
330 PRECONDITION (AFileCount > 0);
332 /* Get the current active input file */
333 Input = (AFile*) CollLast (&AFiles);
335 /* Close the current input file (we're just reading so no error check) */
338 /* Delete the last active file from the active file collection */
339 CollDelete (&AFiles, AFileCount-1);
341 /* If we had added an extra search path for this AFile, remove it */
342 if (Input->SearchPath) {
343 PopSearchPath (UsrIncSearchPath);
346 /* Delete the active file structure */
352 static void GetInputChar (void)
353 /* Read the next character from the input stream and make CurC and NextC
354 ** valid. If end of line is reached, both are set to NUL, no more lines
355 ** are read by this function.
358 /* Drop all pushed fragments that don't have data left */
359 while (SB_GetIndex (Line) >= SB_GetLen (Line)) {
360 /* Cannot read more from this line, check next line on stack if any */
361 if (CollCount (&InputStack) == 0) {
362 /* This is THE line */
366 Line = CollPop (&InputStack);
369 /* Now get the next characters from the line */
370 if (SB_GetIndex (Line) >= SB_GetLen (Line)) {
373 CurC = SB_AtUnchecked (Line, SB_GetIndex (Line));
374 if (SB_GetIndex (Line) + 1 < SB_GetLen (Line)) {
375 /* NextC comes from this fragment */
376 NextC = SB_AtUnchecked (Line, SB_GetIndex (Line) + 1);
378 /* NextC comes from next fragment */
379 if (CollCount (&InputStack) > 0) {
391 /* Skip the current input character and read the next one from the input
392 ** stream. CurC and NextC are valid after the call. If end of line is
393 ** reached, both are set to NUL, no more lines are read by this function.
396 /* Skip the last character read */
399 /* Read the next one */
405 void ClearLine (void)
406 /* Clear the current input line */
410 /* Remove all pushed fragments from the input stack */
411 for (I = 0; I < CollCount (&InputStack); ++I) {
412 FreeStrBuf (CollAtUnchecked (&InputStack, I));
414 CollDeleteAll (&InputStack);
416 /* Clear the contents of Line */
424 StrBuf* InitLine (StrBuf* Buf)
425 /* Initialize Line from Buf and read CurC and NextC from the new input line.
426 ** The function returns the old input line.
429 StrBuf* OldLine = Line;
431 CurC = SB_LookAt (Buf, SB_GetIndex (Buf));
432 NextC = SB_LookAt (Buf, SB_GetIndex (Buf) + 1);
439 /* Get a line from the current input. Returns 0 on end of file. */
443 /* Clear the current line */
446 /* If there is no file open, bail out, otherwise get the current input file */
447 if (CollCount (&AFiles) == 0) {
450 Input = CollLast (&AFiles);
452 /* Read characters until we have one complete line */
455 /* Read the next character */
456 int C = fgetc (Input->F);
461 /* Accept files without a newline at the end */
462 if (SB_NotEmpty (Line)) {
467 /* Leave the current file */
470 /* If there is no file open, bail out, otherwise get the
471 ** previous input file and start over.
473 if (CollCount (&AFiles) == 0) {
476 Input = CollLast (&AFiles);
480 /* Check for end of line */
483 /* We got a new line */
486 /* If the \n is preceeded by a \r, remove the \r, so we can read
487 ** DOS/Windows files under *nix.
489 if (SB_LookAtLast (Line) == '\r') {
493 /* If we don't have a line continuation character at the end,
494 ** we're done with this line. Otherwise replace the character
495 ** by a newline and continue reading.
497 if (SB_LookAtLast (Line) == '\\') {
498 Line->Buf[Line->Len-1] = '\n';
503 } else if (C != '\0') { /* Ignore embedded NULs */
505 /* Just some character, add it to the line */
506 SB_AppendChar (Line, C);
511 /* Add a termination character to the string buffer */
514 /* Initialize the current and next characters. */
517 /* Create line information for this line */
518 UpdateLineInfo (Input->Input, Input->Line, Line);
526 const char* GetInputFile (const struct IFile* IF)
527 /* Return a filename from an IFile struct */
534 const char* GetCurrentFile (void)
535 /* Return the name of the current input file */
537 unsigned AFileCount = CollCount (&AFiles);
538 if (AFileCount > 0) {
539 const AFile* AF = (const AFile*) CollAt (&AFiles, AFileCount-1);
540 return AF->Input->Name;
542 /* No open file. Use the main file if we have one. */
543 unsigned IFileCount = CollCount (&IFiles);
544 if (IFileCount > 0) {
545 const IFile* IF = (const IFile*) CollAt (&IFiles, 0);
548 return "(outside file scope)";
555 unsigned GetCurrentLine (void)
556 /* Return the line number in the current input file */
558 unsigned AFileCount = CollCount (&AFiles);
559 if (AFileCount > 0) {
560 const AFile* AF = (const AFile*) CollAt (&AFiles, AFileCount-1);
570 static void WriteEscaped (FILE* F, const char* Name)
571 /* Write a file name to a dependency file escaping spaces */
585 static void WriteDep (FILE* F, InputType Types)
586 /* Helper function. Writes all file names that match Types to the output */
590 /* Loop over all files */
591 unsigned FileCount = CollCount (&IFiles);
592 for (I = 0; I < FileCount; ++I) {
594 /* Get the next input file */
595 const IFile* IF = (const IFile*) CollAt (&IFiles, I);
597 /* Ignore it if it is not of the correct type */
598 if ((IF->Type & Types) == 0) {
602 /* If this is not the first file, add a space */
607 /* Print the dependency escaping spaces */
608 WriteEscaped (F, IF->Name);
614 static void CreateDepFile (const char* Name, InputType Types)
615 /* Create a dependency file with the given name and place dependencies for
616 ** all files with the given types there.
620 FILE* F = fopen (Name, "w");
622 Fatal ("Cannot open dependency file '%s': %s", Name, strerror (errno));
625 /* If a dependency target was given, use it, otherwise use the output
626 ** file name as target, followed by a tab character.
628 if (SB_IsEmpty (&DepTarget)) {
629 WriteEscaped (F, OutputFilename);
631 WriteEscaped (F, SB_GetConstBuf (&DepTarget));
635 /* Write out the dependencies for the output file */
639 /* Write out a phony dependency for the included files */
643 /* Close the file, check for errors */
644 if (fclose (F) != 0) {
646 Fatal ("Cannot write to dependeny file (disk full?)");
652 void CreateDependencies (void)
653 /* Create dependency files requested by the user */
655 if (SB_NotEmpty (&DepName)) {
656 CreateDepFile (SB_GetConstBuf (&DepName),
657 IT_MAIN | IT_USRINC);
659 if (SB_NotEmpty (&FullDepName)) {
660 CreateDepFile (SB_GetConstBuf (&FullDepName),
661 IT_MAIN | IT_SYSINC | IT_USRINC);