]> git.sur5r.net Git - cc65/blob - src/cc65/input.c
ad31ebc30f23a3becd43ba066a524bd028aa354a
[cc65] / src / cc65 / input.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  input.c                                  */
4 /*                                                                           */
5 /*                            Input file handling                            */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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
40 #include "../common/xmalloc.h"
41
42 #include "asmcode.h"
43 #include "check.h"
44 #include "error.h"
45 #include "global.h"
46 #include "incpath.h"
47 #include "input.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Data                                    */
53 /*****************************************************************************/
54
55
56
57 /* Input line stuff */
58 static char LineBuf [LINESIZE];
59 char* line = LineBuf;
60 char* lptr = LineBuf;
61
62 /* Maximum count of nested includes */
63 #define MAX_INC_NESTING         20
64
65 /* Struct that describes an input file */
66 typedef struct IFile IFile;
67 struct IFile {
68     IFile*      Next;           /* Next file in single linked list      */
69     IFile*      Active;         /* Next file in list of active includes */
70     unsigned    Index;          /* File index                           */
71     unsigned    Line;           /* Line number for this file            */
72     FILE*       F;              /* Input file stream                    */
73     char        Name[1];        /* Name of file (dynamically allocated) */
74 };
75
76 /* List of input files */
77 static unsigned IFileTotal = 0; /* Total number of files                */
78 static IFile*   IFileList  = 0; /* Single linked list of all files      */
79 static unsigned IFileCount = 0; /* Number of active input files         */
80 static IFile*   Input      = 0; /* Single linked list of active files   */
81
82
83
84 /*****************************************************************************/
85 /*                               struct IFile                                */
86 /*****************************************************************************/
87
88
89
90 static IFile* NewIFile (const char* Name, FILE* F)
91 /* Create and return a new IFile */
92 {
93     /* Get the length of the name */
94     unsigned Len = strlen (Name);
95
96     /* Allocate a IFile structure */
97     IFile* IF = xmalloc (sizeof (IFile) + Len);
98
99     /* Initialize the fields */
100     IF->Index   = ++IFileTotal;
101     IF->Line    = 0;
102     IF->F       = F;
103     memcpy (IF->Name, Name, Len+1);
104
105     /* Insert the structure into both lists */
106     IF->Next    = IFileList;
107     IFileList   = IF;
108     IF->Active  = Input;
109     Input       = IF;
110     ++IFileCount;
111
112     /* Return the new struct */
113     return IF;
114 }
115
116
117
118 /*****************************************************************************/
119 /*                                   Code                                    */
120 /*****************************************************************************/
121
122
123
124 void OpenMainFile (const char* Name)
125 /* Open the main file. Will call Fatal() in case of failures. */
126 {
127     /* Open the file for reading */
128     FILE* F = fopen (Name, "r");
129     if (F == 0) {
130         /* Cannot open */
131         Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
132     }
133
134     /* Setup a new IFile structure */
135     NewIFile (Name, F);
136 }
137
138
139
140 void OpenIncludeFile (const char* Name, unsigned DirSpec)
141 /* Open an include file and insert it into the tables. */
142 {
143     char* N;
144     FILE* F;
145
146     /* Check for the maximum include nesting */
147     if (IFileCount > MAX_INC_NESTING) {
148         PPError (ERR_INCLUDE_NESTING);
149         return;
150     }
151
152     /* Search for the file */
153     N = FindInclude (Name, DirSpec);
154     if (N == 0) {
155         PPError (ERR_INCLUDE_NOT_FOUND, Name);
156         return;
157     }
158
159     /* Open the file */
160     F = fopen (N, "r");
161     if (F == 0) {
162         /* Error opening the file */
163         PPError (ERR_INCLUDE_OPEN_FAILURE, N, strerror (errno));
164         xfree (N);
165         return;
166     }
167
168     /* Allocate a new IFile structure */
169     NewIFile (N, F);
170
171     /* We don't need the full name any longer */
172     xfree (N);
173 }
174
175
176
177 static void CloseIncludeFile (void)
178 /* Close an include file and switch to the higher level file. Set Input to
179  * NULL if this was the main file.
180  */
181 {
182     /* Must have an input file when called */
183     PRECONDITION (Input != 0);
184
185     /* Close the current input file (we're just reading so no error check) */
186     fclose (Input->F);
187
188     /* Make this file inactive and the last one active again */
189     Input = Input->Active;
190 }
191
192
193
194 int NextLine (void)
195 /* Get a line from the current input. Returns 0 on end of file. */
196 {
197     unsigned    Len;
198     unsigned    Part;
199     unsigned    Start;
200     int         Done;
201
202     /* Setup the line */
203     ClearLine ();
204
205     /* If there is no file open, bail out */
206     if (Input == 0) {
207         return 0;
208     }
209
210     /* Read lines until we get one with real contents */
211     Len = 0;
212     Done = 0;
213     while (!Done && Len < LINESIZE) {
214
215         while (fgets (line + Len, LINESIZE - Len, Input->F) == 0) {
216
217             /* Assume EOF */
218             ClearLine ();
219
220             /* Leave the current file */
221             CloseIncludeFile ();
222
223             /* If this was the last file, bail out */
224             if (Input == 0) {
225                 return 0;
226             }
227         }
228
229         /* We got a new line */
230         ++Input->Line;
231
232         /* Remove the trailing newline if we have one */
233         Part = strlen (line + Len);
234         Start = Len;
235         Len += Part;
236         while (Len > 0 && line [Len-1] == '\n') {
237             --Len;
238         }
239         line [Len] = '\0';
240
241         /* Output the source line in the generated assembler file
242          * if requested.
243          */
244         if (AddSource && line[Start] != '\0') {
245             AddCodeLine ("; %s", line+Start);
246         }
247
248         /* Check if we have a line continuation character at the end. If not,
249          * we're done.
250          */
251         if (Len > 0 && line[Len-1] == '\\') {
252             line[Len-1] = '\n';         /* Replace by newline */
253         } else {
254             Done = 1;
255         }
256     }
257
258     /* Got a line */
259     return 1;
260 }
261
262
263
264 void ClearLine (void)
265 /* Clear the current input line */
266 {
267     line [0] = '\0';
268     lptr = line;
269 }
270
271
272
273 const char* GetCurrentFile (void)
274 /* Return the name of the current input file */
275 {
276     if (Input == 0) {
277         return "(outside file scope)";
278     } else {
279         return Input->Name;
280     }
281 }
282
283
284
285 unsigned GetCurrentLine (void)
286 /* Return the line number in the current input file */
287 {
288     return Input? Input->Line : 0;
289 }
290
291
292
293 int nch (void)
294 /* Get the next char in input stream (the one behind the current one) */
295 {
296     if (*lptr == '\0') {
297         return 0;
298     } else {
299         return lptr[1] & 0xFF;
300     }
301 }
302
303
304
305 int cgch (void)
306 /* Get the current character in the input stream and advance line
307  * pointer (unless already at end of line).
308  */
309 {
310     if (*lptr == '\0') {
311         return (0);
312     } else {
313         return (*lptr++ & 0xFF);
314     }
315 }
316
317
318
319 int gch (void)
320 /* Get the current character in the input stream and advance line
321  * pointer (no end of line check is performed).
322  */
323 {
324     return (*lptr++ & 0xFF);
325 }
326
327
328