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