1 /*****************************************************************************/
5 /* Input file handling */
9 /* (C) 2000-2010, 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 /*****************************************************************************/
39 #include <sys/types.h>
60 /*****************************************************************************/
62 /*****************************************************************************/
66 /* An enum that describes different types of input files. The members are
67 * choosen so that it is possible to combine them to bitsets
70 IT_MAIN = 0x01, /* Main input file */
71 IT_SYSINC = 0x02, /* System include file (using <>) */
72 IT_USERINC = 0x04, /* User include file (using "") */
75 /* The current input line */
78 /* Current and next input character */
82 /* Maximum count of nested includes */
83 #define MAX_INC_NESTING 16
85 /* Struct that describes an input file */
86 typedef struct IFile IFile;
88 unsigned Index; /* File index */
89 unsigned Usage; /* Usage counter */
90 unsigned long Size; /* File size */
91 unsigned long MTime; /* Time of last modification */
92 InputType Type; /* Type of input file */
93 char Name[1]; /* Name of file (dynamically allocated) */
96 /* Struct that describes an active input file */
97 typedef struct AFile AFile;
99 unsigned Line; /* Line number for this file */
100 FILE* F; /* Input file stream */
101 IFile* Input; /* Points to corresponding IFile */
104 /* List of all input files */
105 static Collection IFiles = STATIC_COLLECTION_INITIALIZER;
107 /* List of all active files */
108 static Collection AFiles = STATIC_COLLECTION_INITIALIZER;
110 /* Input stack used when preprocessing. */
111 static Collection InputStack = STATIC_COLLECTION_INITIALIZER;
115 /*****************************************************************************/
117 /*****************************************************************************/
121 static IFile* NewIFile (const char* Name, InputType Type)
122 /* Create and return a new IFile */
124 /* Get the length of the name */
125 unsigned Len = strlen (Name);
127 /* Allocate a IFile structure */
128 IFile* IF = (IFile*) xmalloc (sizeof (IFile) + Len);
130 /* Initialize the fields */
131 IF->Index = CollCount (&IFiles) + 1;
136 memcpy (IF->Name, Name, Len+1);
138 /* Insert the new structure into the IFile collection */
139 CollAppend (&IFiles, IF);
141 /* Return the new struct */
147 /*****************************************************************************/
149 /*****************************************************************************/
153 static AFile* NewAFile (IFile* IF, FILE* F)
154 /* Create and return a new AFile */
156 /* Allocate a AFile structure */
157 AFile* AF = (AFile*) xmalloc (sizeof (AFile));
159 /* Initialize the fields */
164 /* Increment the usage counter of the corresponding IFile. If this
165 * is the first use, set the file data and output debug info if
168 if (IF->Usage++ == 0) {
170 /* Get file size and modification time. There a race condition here,
171 * since we cannot use fileno() (non standard identifier in standard
172 * header file), and therefore not fstat. When using stat with the
173 * file name, there's a risk that the file was deleted and recreated
174 * while it was open. Since mtime and size are only used to check
175 * if a file has changed in the debugger, we will ignore this problem
179 if (stat (IF->Name, &Buf) != 0) {
181 Fatal ("Cannot stat `%s': %s", IF->Name, strerror (errno));
183 IF->Size = (unsigned long) Buf.st_size;
184 IF->MTime = (unsigned long) Buf.st_mtime;
186 /* Set the debug data */
187 g_fileinfo (IF->Name, IF->Size, IF->MTime);
190 /* Insert the new structure into the AFile collection */
191 CollAppend (&AFiles, AF);
193 /* Return the new struct */
199 static void FreeAFile (AFile* AF)
200 /* Free an AFile structure */
207 /*****************************************************************************/
209 /*****************************************************************************/
213 static IFile* FindFile (const char* Name)
214 /* Find the file with the given name in the list of all files. Since the list
215 * is not large (usually less than 10), I don't care about using hashes or
216 * similar things and do a linear search.
220 for (I = 0; I < CollCount (&IFiles); ++I) {
221 /* Get the file struct */
222 IFile* IF = (IFile*) CollAt (&IFiles, I);
224 if (strcmp (Name, IF->Name) == 0) {
225 /* Found, return the struct */
236 void OpenMainFile (const char* Name)
237 /* Open the main file. Will call Fatal() in case of failures. */
242 /* Setup a new IFile structure for the main file */
243 IFile* IF = NewIFile (Name, IT_MAIN);
245 /* Open the file for reading */
246 FILE* F = fopen (Name, "r");
249 Fatal ("Cannot open input file `%s': %s", Name, strerror (errno));
252 /* Allocate a new AFile structure for the file */
253 MainFile = NewAFile (IF, F);
255 /* Allocate the input line buffer */
258 /* Update the line infos, so we have a valid line info even at start of
259 * the main file before the first line is read.
261 UpdateLineInfo (MainFile->Input, MainFile->Line, Line);
266 void OpenIncludeFile (const char* Name, unsigned DirSpec)
267 /* Open an include file and insert it into the tables. */
273 /* Check for the maximum include nesting */
274 if (CollCount (&AFiles) > MAX_INC_NESTING) {
275 PPError ("Include nesting too deep");
279 /* Search for the file */
280 N = FindInclude (Name, DirSpec);
282 PPError ("Include file `%s' not found", Name);
286 /* Search the list of all input files for this file. If we don't find
287 * it, create a new IFile object.
291 IF = NewIFile (N, (DirSpec == INC_SYS)? IT_SYSINC : IT_USERINC);
294 /* We don't need N any longer, since we may now use IF->Name */
298 F = fopen (IF->Name, "r");
300 /* Error opening the file */
301 PPError ("Cannot open include file `%s': %s", IF->Name, strerror (errno));
305 /* Debugging output */
306 Print (stdout, 1, "Opened include file `%s'\n", IF->Name);
308 /* Allocate a new AFile structure */
309 (void) NewAFile (IF, F);
314 static void CloseIncludeFile (void)
315 /* Close an include file and switch to the higher level file. Set Input to
316 * NULL if this was the main file.
321 /* Get the number of active input files */
322 unsigned AFileCount = CollCount (&AFiles);
324 /* Must have an input file when called */
325 PRECONDITION (AFileCount > 0);
327 /* Get the current active input file */
328 Input = (AFile*) CollLast (&AFiles);
330 /* Close the current input file (we're just reading so no error check) */
333 /* Delete the last active file from the active file collection */
334 CollDelete (&AFiles, AFileCount-1);
336 /* Delete the active file structure */
342 static void GetInputChar (void)
343 /* Read the next character from the input stream and make CurC and NextC
344 * valid. If end of line is reached, both are set to NUL, no more lines
345 * are read by this function.
348 /* Drop all pushed fragments that don't have data left */
349 while (SB_GetIndex (Line) >= SB_GetLen (Line)) {
350 /* Cannot read more from this line, check next line on stack if any */
351 if (CollCount (&InputStack) == 0) {
352 /* This is THE line */
356 Line = CollPop (&InputStack);
359 /* Now get the next characters from the line */
360 if (SB_GetIndex (Line) >= SB_GetLen (Line)) {
363 CurC = SB_AtUnchecked (Line, SB_GetIndex (Line));
364 if (SB_GetIndex (Line) + 1 < SB_GetLen (Line)) {
365 /* NextC comes from this fragment */
366 NextC = SB_AtUnchecked (Line, SB_GetIndex (Line) + 1);
368 /* NextC comes from next fragment */
369 if (CollCount (&InputStack) > 0) {
381 /* Skip the current input character and read the next one from the input
382 * stream. CurC and NextC are valid after the call. If end of line is
383 * reached, both are set to NUL, no more lines are read by this function.
386 /* Skip the last character read */
389 /* Read the next one */
395 void ClearLine (void)
396 /* Clear the current input line */
400 /* Remove all pushed fragments from the input stack */
401 for (I = 0; I < CollCount (&InputStack); ++I) {
402 FreeStrBuf (CollAtUnchecked (&InputStack, I));
404 CollDeleteAll (&InputStack);
406 /* Clear the contents of Line */
414 StrBuf* InitLine (StrBuf* Buf)
415 /* Initialize Line from Buf and read CurC and NextC from the new input line.
416 * The function returns the old input line.
419 StrBuf* OldLine = Line;
421 CurC = SB_LookAt (Buf, SB_GetIndex (Buf));
422 NextC = SB_LookAt (Buf, SB_GetIndex (Buf) + 1);
429 /* Get a line from the current input. Returns 0 on end of file. */
433 /* Clear the current line */
436 /* If there is no file open, bail out, otherwise get the current input file */
437 if (CollCount (&AFiles) == 0) {
440 Input = CollLast (&AFiles);
442 /* Read characters until we have one complete line */
445 /* Read the next character */
446 int C = fgetc (Input->F);
451 /* Accept files without a newline at the end */
452 if (SB_NotEmpty (Line)) {
457 /* Leave the current file */
460 /* If there is no file open, bail out, otherwise get the
461 * previous input file and start over.
463 if (CollCount (&AFiles) == 0) {
466 Input = CollLast (&AFiles);
470 /* Check for end of line */
473 /* We got a new line */
476 /* If the \n is preceeded by a \r, remove the \r, so we can read
477 * DOS/Windows files under *nix.
479 if (SB_LookAtLast (Line) == '\r') {
483 /* If we don't have a line continuation character at the end,
484 * we're done with this line. Otherwise replace the character
485 * by a newline and continue reading.
487 if (SB_LookAtLast (Line) == '\\') {
488 Line->Buf[Line->Len-1] = '\n';
493 } else if (C != '\0') { /* Ignore embedded NULs */
495 /* Just some character, add it to the line */
496 SB_AppendChar (Line, C);
501 /* Add a termination character to the string buffer */
504 /* Initialize the current and next characters. */
507 /* Create line information for this line */
508 UpdateLineInfo (Input->Input, Input->Line, Line);
516 const char* GetInputFile (const struct IFile* IF)
517 /* Return a filename from an IFile struct */
524 const char* GetCurrentFile (void)
525 /* Return the name of the current input file */
527 unsigned AFileCount = CollCount (&AFiles);
528 if (AFileCount > 0) {
529 const AFile* AF = (const AFile*) CollAt (&AFiles, AFileCount-1);
530 return AF->Input->Name;
532 /* No open file. Use the main file if we have one. */
533 unsigned IFileCount = CollCount (&IFiles);
534 if (IFileCount > 0) {
535 const IFile* IF = (const IFile*) CollAt (&IFiles, 0);
538 return "(outside file scope)";
545 unsigned GetCurrentLine (void)
546 /* Return the line number in the current input file */
548 unsigned AFileCount = CollCount (&AFiles);
549 if (AFileCount > 0) {
550 const AFile* AF = (const AFile*) CollAt (&AFiles, AFileCount-1);
560 static void WriteDep (FILE* F, InputType Types)
561 /* Helper function. Writes all file names that match Types to the output */
565 /* Loop over all files */
566 unsigned FileCount = CollCount (&IFiles);
567 for (I = 0; I < FileCount; ++I) {
569 /* Get the next input file */
570 const IFile* IF = (const IFile*) CollAt (&IFiles, I);
572 /* Ignore it if it is not of the correct type */
573 if ((IF->Type & Types) == 0) {
577 /* If this is not the first file, add a space */
582 /* Print the dependency */
589 static void CreateDepFile (const char* Name, InputType Types)
590 /* Create a dependency file with the given name and place dependencies for
591 * all files with the given types there.
597 FILE* F = fopen (Name, "w");
599 Fatal ("Cannot open dependency file `%s': %s", Name, strerror (errno));
602 /* If a dependency target was given, use it, otherwise use the output
603 * file name as target, followed by a tab character.
605 if (SB_IsEmpty (&DepTarget)) {
606 Target = OutputFilename;
608 Target = SB_GetConstBuf (&DepTarget);
610 fprintf (F, "%s:\t", Target);
612 /* Write out the dependencies for the output file */
616 /* Write out a phony dependency for the included files */
620 /* Close the file, check for errors */
621 if (fclose (F) != 0) {
623 Fatal ("Cannot write to dependeny file (disk full?)");
629 void CreateDependencies (void)
630 /* Create dependency files requested by the user */
632 if (SB_NotEmpty (&DepName)) {
633 CreateDepFile (SB_GetConstBuf (&DepName),
634 IT_MAIN | IT_USERINC);
636 if (SB_NotEmpty (&FullDepName)) {
637 CreateDepFile (SB_GetConstBuf (&FullDepName),
638 IT_MAIN | IT_SYSINC | IT_USERINC);