]> git.sur5r.net Git - cc65/blob - src/cc65/input.c
Added a new option --dep-target to the compiler. This option allows to set the
[cc65] / src / cc65 / input.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  input.c                                  */
4 /*                                                                           */
5 /*                            Input file handling                            */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2010, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 /* common */
43 #include "check.h"
44 #include "coll.h"
45 #include "print.h"
46 #include "strbuf.h"
47 #include "xmalloc.h"
48
49 /* cc65 */
50 #include "codegen.h"
51 #include "error.h"
52 #include "global.h"
53 #include "incpath.h"
54 #include "input.h"
55 #include "lineinfo.h"
56 #include "output.h"
57
58
59
60 /*****************************************************************************/
61 /*                                   Data                                    */
62 /*****************************************************************************/
63
64
65
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
68  */
69 typedef enum {
70     IT_MAIN     = 0x01,         /* Main input file */
71     IT_SYSINC   = 0x02,         /* System include file (using <>) */
72     IT_USERINC  = 0x04,         /* User include file (using "") */
73 } InputType;
74
75 /* The current input line */
76 StrBuf* Line;
77
78 /* Current and next input character */
79 char CurC  = '\0';
80 char NextC = '\0';
81
82 /* Maximum count of nested includes */
83 #define MAX_INC_NESTING         16
84
85 /* Struct that describes an input file */
86 typedef struct IFile IFile;
87 struct 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) */
94 };
95
96 /* Struct that describes an active input file */
97 typedef struct AFile AFile;
98 struct AFile {
99     unsigned    Line;           /* Line number for this file            */
100     FILE*       F;              /* Input file stream                    */
101     IFile*      Input;          /* Points to corresponding IFile        */
102 };
103
104 /* List of all input files */
105 static Collection IFiles = STATIC_COLLECTION_INITIALIZER;
106
107 /* List of all active files */
108 static Collection AFiles = STATIC_COLLECTION_INITIALIZER;
109
110 /* Input stack used when preprocessing. */
111 static Collection InputStack = STATIC_COLLECTION_INITIALIZER;
112
113
114
115 /*****************************************************************************/
116 /*                               struct IFile                                */
117 /*****************************************************************************/
118
119
120
121 static IFile* NewIFile (const char* Name, InputType Type)
122 /* Create and return a new IFile */
123 {
124     /* Get the length of the name */
125     unsigned Len = strlen (Name);
126
127     /* Allocate a IFile structure */
128     IFile* IF = (IFile*) xmalloc (sizeof (IFile) + Len);
129
130     /* Initialize the fields */
131     IF->Index = CollCount (&IFiles) + 1;
132     IF->Usage = 0;
133     IF->Size  = 0;
134     IF->MTime = 0;
135     IF->Type  = Type;
136     memcpy (IF->Name, Name, Len+1);
137
138     /* Insert the new structure into the IFile collection */
139     CollAppend (&IFiles, IF);
140
141     /* Return the new struct */
142     return IF;
143 }
144
145
146
147 /*****************************************************************************/
148 /*                               struct AFile                                */
149 /*****************************************************************************/
150
151
152
153 static AFile* NewAFile (IFile* IF, FILE* F)
154 /* Create and return a new AFile */
155 {
156     /* Allocate a AFile structure */
157     AFile* AF = (AFile*) xmalloc (sizeof (AFile));
158
159     /* Initialize the fields */
160     AF->Line  = 0;
161     AF->F     = F;
162     AF->Input = IF;
163
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
166      * requested.
167      */
168     if (IF->Usage++ == 0) {
169
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
176          * here.
177          */
178         struct stat Buf;
179         if (stat (IF->Name, &Buf) != 0) {
180             /* Error */
181             Fatal ("Cannot stat `%s': %s", IF->Name, strerror (errno));
182         }
183         IF->Size  = (unsigned long) Buf.st_size;
184         IF->MTime = (unsigned long) Buf.st_mtime;
185
186         /* Set the debug data */
187         g_fileinfo (IF->Name, IF->Size, IF->MTime);
188     }
189
190     /* Insert the new structure into the AFile collection */
191     CollAppend (&AFiles, AF);
192
193     /* Return the new struct */
194     return AF;
195 }
196
197
198
199 static void FreeAFile (AFile* AF)
200 /* Free an AFile structure */
201 {
202     xfree (AF);
203 }
204
205
206
207 /*****************************************************************************/
208 /*                                   Code                                    */
209 /*****************************************************************************/
210
211
212
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.
217  */
218 {
219     unsigned I;
220     for (I = 0; I < CollCount (&IFiles); ++I) {
221         /* Get the file struct */
222         IFile* IF = (IFile*) CollAt (&IFiles, I);
223         /* Check the name */
224         if (strcmp (Name, IF->Name) == 0) {
225             /* Found, return the struct */
226             return IF;
227         }
228     }
229
230     /* Not found */
231     return 0;
232 }
233
234
235
236 void OpenMainFile (const char* Name)
237 /* Open the main file. Will call Fatal() in case of failures. */
238 {
239     AFile* MainFile;
240
241
242     /* Setup a new IFile structure for the main file */
243     IFile* IF = NewIFile (Name, IT_MAIN);
244
245     /* Open the file for reading */
246     FILE* F = fopen (Name, "r");
247     if (F == 0) {
248         /* Cannot open */
249         Fatal ("Cannot open input file `%s': %s", Name, strerror (errno));
250     }
251
252     /* Allocate a new AFile structure for the file */
253     MainFile = NewAFile (IF, F);
254
255     /* Allocate the input line buffer */
256     Line = NewStrBuf ();
257
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.
260      */
261     UpdateLineInfo (MainFile->Input, MainFile->Line, Line);
262 }
263
264
265
266 void OpenIncludeFile (const char* Name, unsigned DirSpec)
267 /* Open an include file and insert it into the tables. */
268 {
269     char*  N;
270     FILE*  F;
271     IFile* IF;
272
273     /* Check for the maximum include nesting */
274     if (CollCount (&AFiles) > MAX_INC_NESTING) {
275         PPError ("Include nesting too deep");
276         return;
277     }
278
279     /* Search for the file */
280     N = FindInclude (Name, DirSpec);
281     if (N == 0) {
282         PPError ("Include file `%s' not found", Name);
283         return;
284     }
285
286     /* Search the list of all input files for this file. If we don't find
287      * it, create a new IFile object.
288      */
289     IF = FindFile (N);
290     if (IF == 0) {
291         IF = NewIFile (N, (DirSpec == INC_SYS)? IT_SYSINC : IT_USERINC);
292     }
293
294     /* We don't need N any longer, since we may now use IF->Name */
295     xfree (N);
296
297     /* Open the file */
298     F = fopen (IF->Name, "r");
299     if (F == 0) {
300         /* Error opening the file */
301         PPError ("Cannot open include file `%s': %s", IF->Name, strerror (errno));
302         return;
303     }
304
305     /* Debugging output */
306     Print (stdout, 1, "Opened include file `%s'\n", IF->Name);
307
308     /* Allocate a new AFile structure */
309     (void) NewAFile (IF, F);
310 }
311
312
313
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.
317  */
318 {
319     AFile* Input;
320
321     /* Get the number of active input files */
322     unsigned AFileCount = CollCount (&AFiles);
323
324     /* Must have an input file when called */
325     PRECONDITION (AFileCount > 0);
326
327     /* Get the current active input file */
328     Input = (AFile*) CollLast (&AFiles);
329
330     /* Close the current input file (we're just reading so no error check) */
331     fclose (Input->F);
332
333     /* Delete the last active file from the active file collection */
334     CollDelete (&AFiles, AFileCount-1);
335
336     /* Delete the active file structure */
337     FreeAFile (Input);
338 }
339
340
341
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.
346  */
347 {
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 */
353             break;
354         }
355         FreeStrBuf (Line);
356         Line = CollPop (&InputStack);
357     }
358
359     /* Now get the next characters from the line */
360     if (SB_GetIndex (Line) >= SB_GetLen (Line)) {
361         CurC = NextC = '\0';
362     } else {
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);
367         } else {
368             /* NextC comes from next fragment */
369             if (CollCount (&InputStack) > 0) {
370                 NextC = ' ';
371             } else {
372                 NextC = '\0';
373             }
374         }
375     }
376 }
377
378
379
380 void NextChar (void)
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.
384  */
385 {
386     /* Skip the last character read */
387     SB_Skip (Line);
388
389     /* Read the next one */
390     GetInputChar ();
391 }
392
393
394
395 void ClearLine (void)
396 /* Clear the current input line */
397 {
398     unsigned I;
399
400     /* Remove all pushed fragments from the input stack */
401     for (I = 0; I < CollCount (&InputStack); ++I) {
402         FreeStrBuf (CollAtUnchecked (&InputStack, I));
403     }
404     CollDeleteAll (&InputStack);
405
406     /* Clear the contents of Line */
407     SB_Clear (Line);
408     CurC    = '\0';
409     NextC   = '\0';
410 }
411
412
413
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.
417  */
418 {
419     StrBuf* OldLine = Line;
420     Line  = Buf;
421     CurC  = SB_LookAt (Buf, SB_GetIndex (Buf));
422     NextC = SB_LookAt (Buf, SB_GetIndex (Buf) + 1);
423     return OldLine;
424 }
425
426
427
428 int NextLine (void)
429 /* Get a line from the current input. Returns 0 on end of file. */
430 {
431     AFile*      Input;
432
433     /* Clear the current line */
434     ClearLine ();
435
436     /* If there is no file open, bail out, otherwise get the current input file */
437     if (CollCount (&AFiles) == 0) {
438         return 0;
439     }
440     Input = CollLast (&AFiles);
441
442     /* Read characters until we have one complete line */
443     while (1) {
444
445         /* Read the next character */
446         int C = fgetc (Input->F);
447
448         /* Check for EOF */
449         if (C == EOF) {
450
451             /* Accept files without a newline at the end */
452             if (SB_NotEmpty (Line)) {
453                 ++Input->Line;
454                 break;
455             }
456
457             /* Leave the current file */
458             CloseIncludeFile ();
459
460             /* If there is no file open, bail out, otherwise get the
461              * previous input file and start over.
462              */
463             if (CollCount (&AFiles) == 0) {
464                 return 0;
465             }
466             Input = CollLast (&AFiles);
467             continue;
468         }
469
470         /* Check for end of line */
471         if (C == '\n') {
472
473             /* We got a new line */
474             ++Input->Line;
475
476             /* If the \n is preceeded by a \r, remove the \r, so we can read
477              * DOS/Windows files under *nix.
478              */
479             if (SB_LookAtLast (Line) == '\r') {
480                 SB_Drop (Line, 1);
481             }
482
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.
486              */
487             if (SB_LookAtLast (Line) == '\\') {
488                 Line->Buf[Line->Len-1] = '\n';
489             } else {
490                 break;
491             }
492
493         } else if (C != '\0') {         /* Ignore embedded NULs */
494
495             /* Just some character, add it to the line */
496             SB_AppendChar (Line, C);
497
498         }
499     }
500
501     /* Add a termination character to the string buffer */
502     SB_Terminate (Line);
503
504     /* Initialize the current and next characters. */
505     InitLine (Line);
506
507     /* Create line information for this line */
508     UpdateLineInfo (Input->Input, Input->Line, Line);
509
510     /* Done */
511     return 1;
512 }
513
514
515
516 const char* GetInputFile (const struct IFile* IF)
517 /* Return a filename from an IFile struct */
518 {
519     return IF->Name;
520 }
521
522
523
524 const char* GetCurrentFile (void)
525 /* Return the name of the current input file */
526 {
527     unsigned AFileCount = CollCount (&AFiles);
528     if (AFileCount > 0) {
529         const AFile* AF = (const AFile*) CollAt (&AFiles, AFileCount-1);
530         return AF->Input->Name;
531     } else {
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);
536             return IF->Name;
537         } else {
538             return "(outside file scope)";
539         }
540     }
541 }
542
543
544
545 unsigned GetCurrentLine (void)
546 /* Return the line number in the current input file */
547 {
548     unsigned AFileCount = CollCount (&AFiles);
549     if (AFileCount > 0) {
550         const AFile* AF = (const AFile*) CollAt (&AFiles, AFileCount-1);
551         return AF->Line;
552     } else {
553         /* No open file */
554         return 0;
555     }
556 }
557
558
559
560 static void WriteDep (FILE* F, InputType Types)
561 /* Helper function. Writes all file names that match Types to the output */
562 {
563     unsigned I;
564
565     /* Loop over all files */
566     unsigned FileCount = CollCount (&IFiles);
567     for (I = 0; I < FileCount; ++I) {
568
569         /* Get the next input file */
570         const IFile* IF = (const IFile*) CollAt (&IFiles, I);
571
572         /* Ignore it if it is not of the correct type */
573         if ((IF->Type & Types) == 0) {
574             continue;
575         }
576
577         /* If this is not the first file, add a space */
578         if (I > 0) {
579             fputc (' ', F);
580         }
581
582         /* Print the dependency */
583         fputs (IF->Name, F);
584     }
585 }
586
587
588
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.
592  */
593 {      
594     const char* Target;
595
596     /* Open the file */
597     FILE* F = fopen (Name, "w");
598     if (F == 0) {
599         Fatal ("Cannot open dependency file `%s': %s", Name, strerror (errno));
600     }
601
602     /* If a dependency target was given, use it, otherwise use the output
603      * file name as target, followed by a tab character.
604      */
605     if (SB_IsEmpty (&DepTarget)) {
606         Target = OutputFilename;
607     } else {
608         Target = SB_GetConstBuf (&DepTarget);
609     }
610     fprintf (F, "%s:\t", Target);
611
612     /* Write out the dependencies for the output file */
613     WriteDep (F, Types);
614     fputs ("\n\n", F);
615
616     /* Write out a phony dependency for the included files */
617     WriteDep (F, Types);
618     fputs (":\n\n", F);
619
620     /* Close the file, check for errors */
621     if (fclose (F) != 0) {
622         remove (Name);
623         Fatal ("Cannot write to dependeny file (disk full?)");
624     }
625 }
626
627
628
629 void CreateDependencies (void)
630 /* Create dependency files requested by the user */
631 {
632     if (SB_NotEmpty (&DepName)) {
633         CreateDepFile (SB_GetConstBuf (&DepName),
634                        IT_MAIN | IT_USERINC);
635     }
636     if (SB_NotEmpty (&FullDepName)) {
637         CreateDepFile (SB_GetConstBuf (&FullDepName),
638                        IT_MAIN | IT_SYSINC | IT_USERINC);
639     }
640 }
641
642