328
|
1 /*
|
|
2 input.c
|
|
3
|
|
4 Copyright © 2010 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 #include <config.h>
|
|
23
|
|
24 /*
|
|
25 This file is used to handle reading input files. It serves to encapsulate
|
|
26 the entire input system to make porting to different media and systems
|
|
27 less difficult.
|
|
28 */
|
|
29
|
329
|
30 #include <errno.h>
|
|
31 #include <stdio.h>
|
|
32 #include <string.h>
|
|
33
|
328
|
34 #include <lw_alloc.h>
|
|
35 #include <lw_stringlist.h>
|
329
|
36 #include <lw_string.h>
|
|
37 #include "lwasm.h"
|
328
|
38
|
329
|
39 /*
|
|
40 Data type for storing input buffers
|
|
41 */
|
|
42
|
|
43 enum input_types_e
|
328
|
44 {
|
329
|
45 input_type_file, // regular file, no search path
|
|
46 input_type_include, // include path, start from "local"
|
|
47 input_type_string, // input from a string
|
|
48
|
|
49 input_type_error // invalid input type
|
|
50 };
|
|
51
|
|
52 struct input_stack
|
|
53 {
|
|
54 struct input_stack *next;
|
|
55 int type;
|
|
56 void *data;
|
|
57 int data2;
|
|
58 char *filespec;
|
328
|
59 };
|
|
60
|
330
|
61 #define IS ((struct input_stack *)(as -> input_data))
|
328
|
62
|
329
|
63 void input_init(asmstate_t *as)
|
|
64 {
|
|
65 struct input_stack *t;
|
330
|
66
|
|
67 if (as -> file_dir)
|
|
68 lw_stack_destroy(as -> file_dir);
|
|
69 as -> file_dir = lw_stack_create(lw_free);
|
329
|
70 lw_stringlist_reset(as -> input_files);
|
330
|
71 while (IS)
|
329
|
72 {
|
330
|
73 t = IS;
|
|
74 as -> input_data = IS -> next;
|
329
|
75 lw_free(t);
|
|
76 }
|
|
77 }
|
328
|
78
|
329
|
79 void input_pushpath(asmstate_t *as, char *fn)
|
328
|
80 {
|
329
|
81 /* take apart fn into path and filename then push the path */
|
|
82 /* onto the current file path stack */
|
|
83 char *dn, *dp;
|
|
84 int o;
|
|
85
|
|
86 dn = lw_strdup(fn);
|
332
|
87 dp = dn + strlen(dn);
|
329
|
88
|
|
89 while (--dp != dn)
|
|
90 {
|
|
91 if (*dp == '/')
|
|
92 break;
|
|
93 }
|
|
94 if (*dp == '/')
|
|
95 *dp = '\0';
|
328
|
96
|
329
|
97 if (dp == dn)
|
|
98 {
|
|
99 lw_free(dn);
|
|
100 dn = lw_strdup(".");
|
|
101 lw_stack_push(as -> file_dir, dn);
|
|
102 return;
|
|
103 }
|
|
104 dp = lw_strdup(dn);
|
|
105 lw_free(dn);
|
|
106 lw_stack_push(as -> file_dir, dp);
|
|
107 }
|
|
108
|
|
109 void input_openstring(asmstate_t *as, char *s, char *str)
|
|
110 {
|
|
111 struct input_stack *t;
|
|
112
|
|
113 t = lw_alloc(sizeof(struct input_stack));
|
|
114 t -> filespec = lw_strdup(s);
|
|
115
|
|
116 t -> type = input_type_string;
|
|
117 t -> data = lw_strdup(str);
|
|
118 t -> data2 = 0;
|
330
|
119 t -> next = IS;
|
|
120 as -> input_data = t;
|
329
|
121 t -> filespec = lw_strdup(s);
|
328
|
122 }
|
|
123
|
329
|
124 void input_open(asmstate_t *as, char *s)
|
328
|
125 {
|
329
|
126 struct input_stack *t;
|
|
127 char *s2;
|
|
128 char *p, *p2;
|
|
129
|
|
130 t = lw_alloc(sizeof(struct input_stack));
|
|
131 t -> filespec = lw_strdup(s);
|
|
132
|
332
|
133 for (s2 = s; *s2 && (*s2 != ':'); s2++)
|
329
|
134 /* do nothing */ ;
|
332
|
135 if (!*s2)
|
329
|
136 {
|
|
137 t -> type = input_type_file;
|
|
138 }
|
|
139 else
|
328
|
140 {
|
329
|
141 char *ts;
|
|
142
|
|
143 ts = lw_strndup(s, s2 - s);
|
|
144 s = s2 + 1;
|
|
145 if (!strcmp(ts, "include"))
|
|
146 t -> type = input_type_include;
|
|
147 else if (!strcmp(ts, "file"))
|
|
148 t -> type = input_type_file;
|
|
149 else
|
|
150 t -> type = input_type_error;
|
332
|
151
|
|
152 lw_free(ts);
|
329
|
153 }
|
332
|
154 t -> next = as -> input_data;
|
330
|
155 as -> input_data = t;
|
329
|
156
|
330
|
157 switch (IS -> type)
|
329
|
158 {
|
|
159 case input_type_include:
|
|
160 /* first check for absolute path and if so, skip path */
|
|
161 if (*s == '/')
|
328
|
162 {
|
329
|
163 /* absolute path */
|
330
|
164 IS -> data = fopen(s, "rb");
|
|
165 if (!IS -> data)
|
329
|
166 {
|
|
167 lw_error("Cannot open file '%s': %s", s, strerror(errno));
|
|
168 }
|
|
169 input_pushpath(as, s);
|
|
170 break;
|
328
|
171 }
|
|
172
|
329
|
173 /* relative path, check relative to "current file" directory */
|
|
174 p = lw_stack_top(as -> file_dir);
|
|
175 0 == asprintf(&p2, "%s/%s", p, s);
|
330
|
176 IS -> data = fopen(p2, "rb");
|
|
177 if (IS -> data)
|
329
|
178 {
|
|
179 input_pushpath(as, p2);
|
|
180 lw_free(p2);
|
|
181 break;
|
|
182 }
|
|
183 lw_free(p2);
|
|
184
|
|
185 /* now check relative to entries in the search path */
|
|
186 lw_stringlist_reset(as -> include_list);
|
|
187 while (p = lw_stringlist_current(as -> include_list))
|
|
188 {
|
|
189 0 == asprintf(&p2, "%s/%s", p, s);
|
330
|
190 IS -> data = fopen(p2, "rb");
|
|
191 if (IS -> data)
|
329
|
192 {
|
|
193 input_pushpath(as, p2);
|
|
194 lw_free(p2);
|
|
195 return;
|
|
196 }
|
|
197 lw_free(p2);
|
|
198 lw_stringlist_next(as -> include_list);
|
|
199 }
|
|
200 lw_error("Cannot open include file '%s': %s", s, strerror(errno));
|
332
|
201
|
329
|
202 case input_type_file:
|
330
|
203 IS -> data = fopen(s, "rb");
|
329
|
204
|
330
|
205 if (!IS -> data)
|
329
|
206 {
|
|
207 lw_error("Cannot open file '%s': %s", s, strerror(errno));
|
|
208 }
|
|
209 input_pushpath(as, s);
|
|
210 return;
|
|
211 }
|
|
212
|
332
|
213 lw_error("Cannot figure out how to open '%s'.", t -> filespec);
|
329
|
214 }
|
|
215
|
|
216 char *input_readline(asmstate_t *as)
|
|
217 {
|
|
218 char *s;
|
|
219 char linebuff[2049];
|
|
220 int lbloc;
|
|
221 int eol = 0;
|
|
222
|
|
223 /* if no file is open, open one */
|
|
224 nextfile:
|
330
|
225 if (!IS) {
|
329
|
226 s = lw_stringlist_current(as -> input_files);
|
|
227 if (!s)
|
|
228 return NULL;
|
|
229 lw_stringlist_next(as -> input_files);
|
|
230 input_open(as, s);
|
328
|
231 }
|
|
232
|
330
|
233 switch (IS -> type)
|
329
|
234 {
|
|
235 case input_type_file:
|
|
236 case input_type_include:
|
|
237 /* read from a file */
|
332
|
238 lbloc = 0;
|
329
|
239 for (;;)
|
|
240 {
|
|
241 int c, c2;
|
330
|
242 c = fgetc(IS -> data);
|
329
|
243 if (c == EOF)
|
|
244 {
|
|
245 if (lbloc == 0)
|
|
246 {
|
|
247 struct input_stack *t;
|
330
|
248 fclose(IS -> data);
|
329
|
249 lw_free(lw_stack_pop(as -> file_dir));
|
330
|
250 lw_free(IS -> filespec);
|
|
251 t = IS -> next;
|
|
252 lw_free(IS);
|
|
253 as -> input_data = t;
|
329
|
254 goto nextfile;
|
|
255 }
|
|
256 linebuff[lbloc] = '\0';
|
|
257 eol = 1;
|
|
258 }
|
|
259 else if (c == '\r')
|
|
260 {
|
|
261 linebuff[lbloc] = '\0';
|
|
262 eol = 1;
|
330
|
263 c2 = fgetc(IS -> data);
|
329
|
264 if (c2 == EOF)
|
|
265 c = EOF;
|
|
266 else if (c2 != '\n')
|
330
|
267 ungetc(c2, IS -> data);
|
329
|
268 }
|
|
269 else if (c == '\n')
|
|
270 {
|
|
271 linebuff[lbloc] = '\0';
|
|
272 eol = 1;
|
330
|
273 c2 = fgetc(IS -> data);
|
329
|
274 if (c2 == EOF)
|
|
275 c = EOF;
|
|
276 else if (c2 != '\r')
|
330
|
277 ungetc(c2, IS -> data);
|
329
|
278 }
|
|
279 else
|
|
280 {
|
|
281 if (lbloc < 2048)
|
|
282 linebuff[lbloc++] = c;
|
|
283 }
|
|
284 if (eol)
|
|
285 {
|
|
286 s = lw_strdup(linebuff);
|
|
287 return s;
|
|
288 }
|
|
289 }
|
|
290
|
|
291 case input_type_string:
|
|
292 /* read from a string */
|
330
|
293 if (((char *)(IS -> data))[IS -> data2] == '\0')
|
329
|
294 {
|
|
295 struct input_stack *t;
|
330
|
296 lw_free(IS -> data);
|
|
297 lw_free(IS -> filespec);
|
|
298 t = IS -> next;
|
|
299 lw_free(IS);
|
|
300 as -> input_data = t;
|
329
|
301 goto nextfile;
|
|
302 }
|
330
|
303 s = (char *)(IS -> data);
|
329
|
304 lbloc = 0;
|
|
305 for (;;)
|
|
306 {
|
|
307 int c;
|
330
|
308 c = s[IS -> data2];
|
329
|
309 if (c)
|
330
|
310 IS -> data2++;
|
329
|
311 if (c == '\0')
|
|
312 {
|
|
313 linebuff[lbloc] = '\0';
|
|
314 eol = 1;
|
|
315 }
|
|
316 else if (c == '\r')
|
|
317 {
|
|
318 linebuff[lbloc] = '\0';
|
|
319 eol = 1;
|
330
|
320 if (s[IS -> data2] == '\n')
|
|
321 IS -> data2++;
|
329
|
322 }
|
|
323 else if (c == '\n')
|
|
324 {
|
|
325 linebuff[lbloc] = '\0';
|
|
326 eol = 1;
|
330
|
327 if (s[IS -> data2] == '\r')
|
|
328 IS -> data2++;
|
329
|
329 }
|
|
330 else
|
|
331 {
|
|
332 if (lbloc < 2048)
|
|
333 linebuff[lbloc++] = c;
|
|
334 }
|
|
335 if (eol)
|
|
336 {
|
|
337 s = lw_strdup(linebuff);
|
|
338 return s;
|
|
339 }
|
|
340 }
|
328
|
341
|
329
|
342 default:
|
|
343 lw_error("Problem reading from unknown input type");
|
|
344 }
|
328
|
345 }
|
330
|
346
|
|
347 char *input_curspec(asmstate_t *as)
|
|
348 {
|
|
349 if (IS)
|
|
350 return IS -> filespec;
|
|
351 return NULL;
|
|
352 }
|