]> git.sur5r.net Git - cc65/blob - src/cc65/io.c
This commit was generated by cvs2svn to compensate for changes in r2,
[cc65] / src / cc65 / io.c
1
2 /* C I/O functions */
3
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <errno.h>
8
9 #include "asmcode.h"
10 #include "global.h"
11 #include "error.h"
12 #include "mem.h"
13 #include "codegen.h"
14 #include "optimize.h"
15 #include "io.h"
16
17
18
19 /*****************************************************************************/
20 /*                                   data                                    */
21 /*****************************************************************************/
22
23
24
25 /* Input line stuff */
26 char linebuf [LINESIZE];
27 char* line = linebuf;
28 char* lptr = 0;
29
30 /* Input file table and number of open input files */
31 struct filent filetab[MAXFILES];
32 int ifile = 0;
33
34 /* Current input file stream data */
35 FILE* inp = 0;
36 char* fin = 0;
37 unsigned ln = 0;
38
39
40
41 /*****************************************************************************/
42 /*                                   code                                    */
43 /*****************************************************************************/
44
45
46
47 int nch (void)
48 /* Get the next char in input stream (the one behind the current one) */
49 {
50     if (*lptr == '\0') {
51         return 0;
52     } else {
53         return lptr[1] & 0xFF;
54     }
55 }
56
57
58
59 int cgch (void)
60 /* Get the current character in the input stream and advance line
61  * pointer (unless already at end of line).
62  */
63 {
64     if (*lptr == '\0') {
65         return (0);
66     } else {
67         return (*lptr++ & 0xFF);
68     }
69 }
70
71
72
73 int gch (void)
74 /* Get the current character in the input stream and advance line
75  * pointer (no end of line check is performed).
76  */
77 {
78     return (*lptr++ & 0xFF);
79 }
80
81
82
83 void kill (void)
84 /* Reset input line pointer, clear input line */
85 {
86     lptr = line;
87     *lptr = '\0';
88 }
89
90
91
92 static void CloseInclude (void)
93 /* Close an include file and switch to the higher level file. Set inp to NULL
94  * if this was the main file.
95  */
96 {
97     struct filent* pftab;
98
99     /* Close the file */
100     fclose(inp);
101
102     /* Leave the include file */
103     if (ifile > 0) {
104         xfree (fin);
105         inp = (pftab = &filetab[--ifile])->f_iocb;
106         ln = pftab->f_ln;
107         fin = pftab->f_name;
108     } else {
109         inp = 0;
110     }
111 }
112
113
114
115 int readline (void)
116 /* Get a line from the current input.  Returns -1 on end of file. */
117 {
118     unsigned    Len;
119     unsigned    Part;
120     unsigned    Start;
121     int         Done;
122
123     /* Setup the line */
124     kill ();
125
126     /* If there is no file open, bail out */
127     if (inp == 0) {
128         return 0;
129     }
130
131     /* Read lines until we get one with real contents */
132     Len = 0;
133     Done = 0;
134     while (!Done && Len < LINESIZE) {
135
136         while (fgets (line + Len, LINESIZE - Len, inp) == 0) {
137
138             /* eof */
139             kill ();
140
141             /* Leave the current file */
142             CloseInclude ();
143
144             /* If this was the last file, bail out */
145             if (inp == 0) {
146                 return 0;
147             }
148         }
149
150         /* We got a new line */
151         ++ln;
152
153         /* Remove the trailing newline if we have one */
154         Part = strlen (line + Len);
155         Start = Len;
156         Len += Part;
157         while (Len > 0 && line [Len-1] == '\n') {
158             --Len;
159         }
160         line [Len] = '\0';
161
162         /* Output the source line in the generated assembler file
163          * if requested.
164          */
165         if (IncSource && line[Start] != '\0') {
166             AddCodeLine ("; %s", line+Start);
167         }
168
169         /* Check if we have a line continuation character at the end. If not,
170          * we're done.
171          */
172         if (Len > 0 && line[Len-1] == '\\') {
173             line[Len-1] = '\n';         /* Replace by newline */
174         } else {
175             Done = 1;
176         }
177     }
178
179     /* Got a line */
180     return 1;
181 }
182
183
184