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