Mercurial > hg > index.cgi
annotate lwcc/cpp/file.c @ 294:048adfee2933 ccdev
Checkpoint on lwcc-cpp development
This checkpoint includes a tokenizer and basic implementation of #if,
#ifdef, #ifndef, #else, #endif, #elif, and #undef along with basic symbol
table management.
author | William Astle <lost@l-w.ca> |
---|---|
date | Tue, 10 Sep 2013 19:56:05 -0600 |
parents | c419b3b3d43f |
children |
rev | line source |
---|---|
292 | 1 /* |
2 lwcc/cpp/file.c | |
3 | |
4 Copyright © 2013 William Astle | |
5 | |
6 This file is part of LWTOOLS. | |
7 | |
8 LWTOOLS is free software: you can redistribute it and/or modify it under the | |
9 terms of the GNU General Public License as published by the Free Software | |
10 Foundation, either version 3 of the License, or (at your option) any later | |
11 version. | |
12 | |
13 This program is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
16 more details. | |
17 | |
18 You should have received a copy of the GNU General Public License along with | |
19 this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | |
21 */ | |
22 | |
23 #include <errno.h> | |
24 #include <stdio.h> | |
25 #include <string.h> | |
26 | |
27 #include <lw_alloc.h> | |
28 | |
29 #include "cpp.h" | |
30 | |
31 struct file_stack_e *file_stack = NULL; | |
32 | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
33 /* output a byte to the current output stream as long as we aren't in the |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
34 middle of a false conditional. CPP_EOL will be converted to '\n' |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
35 on output. */ |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
36 void outchr(int c) |
292 | 37 { |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
38 if (skip_level) |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
39 return; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
40 if (c == CPP_EOL) |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
41 c = '\n'; |
292 | 42 fputc(c, output_fp); |
43 } | |
44 | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
45 /* output a string to the current output stream as long as we aren't in the |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
46 middle of a false conditional */ |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
47 void outstr(char *s) |
292 | 48 { |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
49 if (skip_level) |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
50 return; |
292 | 51 while (*s) |
52 outchr(*s++); | |
53 } | |
54 | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
55 /* fetch a raw input byte from the current file. Will return CPP_EOF if |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
56 EOF is encountered and CPP_EOL if an end of line sequence is encountered. |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
57 End of line is defined as either CR, CRLF, LF, or LFCR. CPP_EOL is |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
58 returned on the first CR or LF encountered. The complementary CR or LF |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
59 is munched, if present, when the *next* character is read. This always |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
60 operates on file_stack. |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
61 |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
62 This function also accounts for line numbers in input files and also |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
63 character columns. |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
64 */ |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
65 int fetch_byte_ll(void) |
292 | 66 { |
67 int c; | |
68 | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
69 if (file_stack -> eolstate != 0) |
292 | 70 { |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
71 file_stack -> line++; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
72 file_stack -> col = 0; |
292 | 73 } |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
74 c = getc(file_stack -> fp); |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
75 file_stack -> col++; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
76 if (file_stack -> eolstate == 1) |
292 | 77 { |
78 // just saw CR, munch LF | |
79 if (c == 10) | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
80 c = getc(file_stack -> fp); |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
81 file_stack -> eolstate = 0; |
292 | 82 } |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
83 else if (file_stack -> eolstate == 2) |
292 | 84 { |
85 // just saw LF, much CR | |
86 if (c == 13) | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
87 c = getc(file_stack -> fp); |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
88 file_stack -> eolstate = 0; |
292 | 89 } |
90 | |
91 if (c == 10) | |
92 { | |
93 // we have LF - end of line, flag to munch CR | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
94 file_stack -> eolstate = 2; |
292 | 95 c = CPP_EOL; |
96 } | |
97 else if (c == 13) | |
98 { | |
99 // we have CR - end of line, flag to munch LF | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
100 file_stack -> eolstate = 1; |
292 | 101 c = CPP_EOL; |
102 } | |
103 else if (c == EOF) | |
104 { | |
105 c = CPP_EOF; | |
106 } | |
107 return c; | |
108 } | |
109 | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
110 /* This function takes a sequence of bytes from the _ll function above |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
111 and does trigraph interpretation on it, but only if the global |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
112 trigraphs is nonzero. */ |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
113 int fetch_byte_tg(void) |
292 | 114 { |
115 int c; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
116 |
292 | 117 if (!trigraphs) |
118 { | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
119 c = fetch_byte_ll(); |
292 | 120 } |
121 else | |
122 { | |
123 /* we have to do the trigraph shit here */ | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
124 if (file_stack -> ra != CPP_NOUNG) |
292 | 125 { |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
126 if (file_stack -> qseen > 0) |
292 | 127 { |
128 c = '?'; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
129 file_stack -> qseen -= 1; |
292 | 130 return c; |
131 } | |
132 else | |
133 { | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
134 c = file_stack -> ra; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
135 file_stack -> ra = CPP_NOUNG; |
292 | 136 return c; |
137 } | |
138 } | |
139 | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
140 c = fetch_byte_ll(); |
292 | 141 while (c == '?') |
142 { | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
143 file_stack -> qseen++; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
144 c = fetch_byte_ll(); |
292 | 145 } |
146 | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
147 if (file_stack -> qseen >= 2) |
292 | 148 { |
149 // we have a trigraph | |
150 switch (c) | |
151 { | |
152 case '=': | |
153 c = '#'; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
154 file_stack -> qseen -= 2; |
292 | 155 break; |
156 | |
157 case '/': | |
158 c = '\\'; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
159 file_stack -> qseen -= 2; |
292 | 160 break; |
161 | |
162 case '\'': | |
163 c = '^'; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
164 file_stack -> qseen -= 2; |
292 | 165 break; |
166 | |
167 case '(': | |
168 c = '['; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
169 file_stack -> qseen -= 2; |
292 | 170 break; |
171 | |
172 case ')': | |
173 c = ']'; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
174 file_stack -> qseen -= 2; |
292 | 175 break; |
176 | |
177 case '!': | |
178 c = '|'; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
179 file_stack -> qseen -= 2; |
292 | 180 break; |
181 | |
182 case '<': | |
183 c = '{'; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
184 file_stack -> qseen -= 2; |
292 | 185 break; |
186 | |
187 case '>': | |
188 c = '}'; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
189 file_stack -> qseen -= 2; |
292 | 190 break; |
191 | |
192 case '~': | |
193 c = '~'; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
194 file_stack -> qseen -= 2; |
292 | 195 break; |
196 } | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
197 if (file_stack -> qseen > 0) |
292 | 198 { |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
199 file_stack -> ra = c; |
292 | 200 c = '?'; |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
201 file_stack -> qseen--; |
292 | 202 } |
203 } | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
204 else if (file_stack -> qseen > 0) |
292 | 205 { |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
206 file_stack -> ra = c; |
292 | 207 c = '?'; |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
208 file_stack -> qseen--; |
292 | 209 } |
210 } | |
211 return c; | |
212 } | |
213 | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
214 /* This function puts a byte back onto the front of the input stream used |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
215 by fetch_byte(). Theoretically, an unlimited number of characters can |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
216 be unfetched. Line and column counting may be incorrect if unfetched |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
217 characters cross a token boundary. */ |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
218 void unfetch_byte(int c) |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
219 { |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
220 if (file_stack -> ungetbufl >= file_stack -> ungetbufs) |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
221 { |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
222 file_stack -> ungetbufs += 100; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
223 file_stack -> ungetbuf = lw_realloc(file_stack -> ungetbuf, file_stack -> ungetbufs); |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
224 } |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
225 file_stack -> ungetbuf[file_stack -> ungetbufl++] = c; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
226 } |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
227 |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
228 /* This function retrieves a byte from the input stream. It performs |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
229 backslash-newline splicing on the returned bytes. Any character |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
230 retrieved from the unfetch buffer is presumed to have already passed |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
231 the backslash-newline filter. */ |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
232 int fetch_byte(void) |
292 | 233 { |
234 int c; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
235 |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
236 if (file_stack -> ungetbufl > 0) |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
237 { |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
238 file_stack -> ungetbufl--; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
239 c = file_stack -> ungetbuf[file_stack -> ungetbufl]; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
240 if (file_stack -> ungetbufl == 0) |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
241 { |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
242 lw_free(file_stack -> ungetbuf); |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
243 file_stack -> ungetbuf = NULL; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
244 file_stack -> ungetbufs = 0; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
245 } |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
246 return c; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
247 } |
292 | 248 |
249 again: | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
250 if (file_stack -> unget != CPP_NOUNG) |
292 | 251 { |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
252 c = file_stack -> unget; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
253 file_stack -> unget = CPP_NOUNG; |
292 | 254 } |
255 else | |
256 { | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
257 c = fetch_byte_tg(); |
292 | 258 } |
259 if (c == '\\') | |
260 { | |
261 int c2; | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
262 c2 = fetch_byte_tg(); |
292 | 263 if (c2 == CPP_EOL) |
264 goto again; | |
265 else | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
266 file_stack -> unget = c2; |
292 | 267 } |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
268 file_stack -> curc = c; |
292 | 269 return c; |
270 } | |
271 | |
294
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
272 void skip_eol(void) |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
273 { |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
274 int c; |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
275 for (;;) |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
276 { |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
277 c = fetch_byte(); |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
278 if (c == CPP_EOF || c == CPP_EOL) |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
279 { |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
280 unfetch_byte(c); |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
281 return; |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
282 } |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
283 if (c == '/') |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
284 { |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
285 c = munch_comment(); |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
286 if (c > 0) |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
287 { |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
288 while (c--) |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
289 outchr(CPP_EOL); |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
290 } |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
291 } |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
292 } |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
293 } |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
294 |
048adfee2933
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
293
diff
changeset
|
295 |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
296 /* This function opens (if not stdin) the file f and pushes it onto the |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
297 top of the input file stack. It then proceeds to process the file |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
298 and return. Nonzero return means the file could not be opened. */ |
292 | 299 int process_file(const char *f) |
300 { | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
301 struct file_stack_e nf; |
292 | 302 FILE *fp; |
303 | |
304 fprintf(stderr, "Processing %s\n", f); | |
305 | |
306 if (strcmp(f, "-") == 0) | |
307 fp = stdin; | |
308 else | |
309 fp = fopen(f, "rb"); | |
310 if (fp == NULL) | |
311 { | |
312 do_warning("Cannot open %s: %s", f, strerror(errno)); | |
313 return -1; | |
314 } | |
315 | |
316 /* push the file onto the file stack */ | |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
317 nf.fn = f; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
318 nf.fp = fp; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
319 nf.next = file_stack; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
320 nf.line = 1; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
321 nf.col = 0; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
322 nf.qseen = 0; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
323 nf.ra = CPP_NOUNG; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
324 nf.unget = CPP_NOUNG; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
325 file_stack = &nf; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
326 nf.ungetbuf = NULL; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
327 nf.ungetbufs = 0; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
328 nf.ungetbufl = 0; |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
329 |
292 | 330 /* go preprocess the file */ |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
331 preprocess_file(); |
292 | 332 |
293
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
333 if (nf.fp != stdin) |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
334 fclose(nf.fp); |
c419b3b3d43f
Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
292
diff
changeset
|
335 file_stack = nf.next; |
292 | 336 return 0; |
337 } |