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);
|
384
|
70 as -> includelist = lw_stack_create(lw_free);
|
329
|
71 lw_stringlist_reset(as -> input_files);
|
330
|
72 while (IS)
|
329
|
73 {
|
330
|
74 t = IS;
|
|
75 as -> input_data = IS -> next;
|
329
|
76 lw_free(t);
|
|
77 }
|
|
78 }
|
328
|
79
|
329
|
80 void input_pushpath(asmstate_t *as, char *fn)
|
328
|
81 {
|
329
|
82 /* take apart fn into path and filename then push the path */
|
|
83 /* onto the current file path stack */
|
384
|
84
|
|
85 /* also add it to the list of files included */
|
329
|
86 char *dn, *dp;
|
|
87 int o;
|
384
|
88
|
|
89 dn = lw_strdup(fn);
|
|
90 lw_stack_push(as -> includelist, dn);
|
|
91
|
329
|
92 dn = lw_strdup(fn);
|
332
|
93 dp = dn + strlen(dn);
|
329
|
94
|
|
95 while (--dp != dn)
|
|
96 {
|
|
97 if (*dp == '/')
|
|
98 break;
|
|
99 }
|
|
100 if (*dp == '/')
|
|
101 *dp = '\0';
|
328
|
102
|
329
|
103 if (dp == dn)
|
|
104 {
|
|
105 lw_free(dn);
|
|
106 dn = lw_strdup(".");
|
|
107 lw_stack_push(as -> file_dir, dn);
|
|
108 return;
|
|
109 }
|
|
110 dp = lw_strdup(dn);
|
|
111 lw_free(dn);
|
|
112 lw_stack_push(as -> file_dir, dp);
|
|
113 }
|
|
114
|
|
115 void input_openstring(asmstate_t *as, char *s, char *str)
|
|
116 {
|
|
117 struct input_stack *t;
|
|
118
|
|
119 t = lw_alloc(sizeof(struct input_stack));
|
|
120 t -> filespec = lw_strdup(s);
|
|
121
|
|
122 t -> type = input_type_string;
|
|
123 t -> data = lw_strdup(str);
|
|
124 t -> data2 = 0;
|
330
|
125 t -> next = IS;
|
|
126 as -> input_data = t;
|
329
|
127 t -> filespec = lw_strdup(s);
|
328
|
128 }
|
|
129
|
329
|
130 void input_open(asmstate_t *as, char *s)
|
328
|
131 {
|
329
|
132 struct input_stack *t;
|
|
133 char *s2;
|
|
134 char *p, *p2;
|
|
135
|
|
136 t = lw_alloc(sizeof(struct input_stack));
|
|
137 t -> filespec = lw_strdup(s);
|
|
138
|
332
|
139 for (s2 = s; *s2 && (*s2 != ':'); s2++)
|
329
|
140 /* do nothing */ ;
|
332
|
141 if (!*s2)
|
329
|
142 {
|
|
143 t -> type = input_type_file;
|
|
144 }
|
|
145 else
|
328
|
146 {
|
329
|
147 char *ts;
|
|
148
|
|
149 ts = lw_strndup(s, s2 - s);
|
|
150 s = s2 + 1;
|
|
151 if (!strcmp(ts, "include"))
|
|
152 t -> type = input_type_include;
|
|
153 else if (!strcmp(ts, "file"))
|
|
154 t -> type = input_type_file;
|
|
155 else
|
|
156 t -> type = input_type_error;
|
332
|
157
|
|
158 lw_free(ts);
|
329
|
159 }
|
332
|
160 t -> next = as -> input_data;
|
330
|
161 as -> input_data = t;
|
329
|
162
|
330
|
163 switch (IS -> type)
|
329
|
164 {
|
|
165 case input_type_include:
|
|
166 /* first check for absolute path and if so, skip path */
|
|
167 if (*s == '/')
|
328
|
168 {
|
329
|
169 /* absolute path */
|
330
|
170 IS -> data = fopen(s, "rb");
|
386
|
171 debug_message(as, 1, "Opening (abs) %s", s);
|
330
|
172 if (!IS -> data)
|
329
|
173 {
|
|
174 lw_error("Cannot open file '%s': %s", s, strerror(errno));
|
|
175 }
|
|
176 input_pushpath(as, s);
|
|
177 break;
|
328
|
178 }
|
|
179
|
329
|
180 /* relative path, check relative to "current file" directory */
|
|
181 p = lw_stack_top(as -> file_dir);
|
|
182 0 == asprintf(&p2, "%s/%s", p, s);
|
386
|
183 debug_message(as, 1, "Open: (cd) %s\n", p2);
|
330
|
184 IS -> data = fopen(p2, "rb");
|
|
185 if (IS -> data)
|
329
|
186 {
|
|
187 input_pushpath(as, p2);
|
|
188 lw_free(p2);
|
|
189 break;
|
|
190 }
|
|
191 lw_free(p2);
|
|
192
|
|
193 /* now check relative to entries in the search path */
|
|
194 lw_stringlist_reset(as -> include_list);
|
|
195 while (p = lw_stringlist_current(as -> include_list))
|
|
196 {
|
|
197 0 == asprintf(&p2, "%s/%s", p, s);
|
386
|
198 debug_message(as, 1, "Open (sp): %s\n", p2);
|
330
|
199 IS -> data = fopen(p2, "rb");
|
|
200 if (IS -> data)
|
329
|
201 {
|
|
202 input_pushpath(as, p2);
|
|
203 lw_free(p2);
|
|
204 return;
|
|
205 }
|
|
206 lw_free(p2);
|
|
207 lw_stringlist_next(as -> include_list);
|
|
208 }
|
|
209 lw_error("Cannot open include file '%s': %s", s, strerror(errno));
|
332
|
210
|
329
|
211 case input_type_file:
|
386
|
212 debug_message(as, 1, "Opening (reg): %s\n", s);
|
330
|
213 IS -> data = fopen(s, "rb");
|
329
|
214
|
330
|
215 if (!IS -> data)
|
329
|
216 {
|
|
217 lw_error("Cannot open file '%s': %s", s, strerror(errno));
|
|
218 }
|
|
219 input_pushpath(as, s);
|
|
220 return;
|
|
221 }
|
|
222
|
332
|
223 lw_error("Cannot figure out how to open '%s'.", t -> filespec);
|
329
|
224 }
|
|
225
|
356
|
226 FILE *input_open_standalone(asmstate_t *as, char *s)
|
|
227 {
|
|
228 char *s2;
|
|
229 FILE *fp;
|
|
230 char *p, *p2;
|
|
231
|
|
232 /* first check for absolute path and if so, skip path */
|
|
233 if (*s == '/')
|
|
234 {
|
|
235 /* absolute path */
|
386
|
236 debug_message(as, 2, "Open file (st abs) %s", s);
|
356
|
237 fp = fopen(s, "rb");
|
|
238 if (!fp)
|
|
239 {
|
|
240 return NULL;
|
|
241 }
|
|
242 return fp;
|
|
243 }
|
|
244
|
|
245 /* relative path, check relative to "current file" directory */
|
|
246 p = lw_stack_top(as -> file_dir);
|
|
247 0 == asprintf(&p2, "%s/%s", p, s);
|
386
|
248 debug_message(as, 2, "Open file (st cd) %s", p2);
|
356
|
249 fp = fopen(p2, "rb");
|
|
250 if (fp)
|
|
251 {
|
|
252 lw_free(p2);
|
|
253 return fp;
|
|
254 }
|
|
255 lw_free(p2);
|
|
256
|
|
257 /* now check relative to entries in the search path */
|
|
258 lw_stringlist_reset(as -> include_list);
|
|
259 while (p = lw_stringlist_current(as -> include_list))
|
|
260 {
|
|
261 0 == asprintf(&p2, "%s/%s", p, s);
|
386
|
262 debug_message(as, 2, "Open file (st ip) %s", p2);
|
356
|
263 fp = fopen(p2, "rb");
|
|
264 if (fp)
|
|
265 {
|
|
266 lw_free(p2);
|
|
267 return fp;
|
|
268 }
|
|
269 lw_free(p2);
|
|
270 lw_stringlist_next(as -> include_list);
|
|
271 }
|
|
272
|
|
273 return NULL;
|
|
274 }
|
|
275
|
329
|
276 char *input_readline(asmstate_t *as)
|
|
277 {
|
|
278 char *s;
|
|
279 char linebuff[2049];
|
|
280 int lbloc;
|
|
281 int eol = 0;
|
|
282
|
|
283 /* if no file is open, open one */
|
|
284 nextfile:
|
330
|
285 if (!IS) {
|
329
|
286 s = lw_stringlist_current(as -> input_files);
|
|
287 if (!s)
|
|
288 return NULL;
|
|
289 lw_stringlist_next(as -> input_files);
|
|
290 input_open(as, s);
|
328
|
291 }
|
|
292
|
330
|
293 switch (IS -> type)
|
329
|
294 {
|
|
295 case input_type_file:
|
|
296 case input_type_include:
|
|
297 /* read from a file */
|
332
|
298 lbloc = 0;
|
329
|
299 for (;;)
|
|
300 {
|
|
301 int c, c2;
|
330
|
302 c = fgetc(IS -> data);
|
329
|
303 if (c == EOF)
|
|
304 {
|
|
305 if (lbloc == 0)
|
|
306 {
|
|
307 struct input_stack *t;
|
330
|
308 fclose(IS -> data);
|
329
|
309 lw_free(lw_stack_pop(as -> file_dir));
|
330
|
310 lw_free(IS -> filespec);
|
|
311 t = IS -> next;
|
|
312 lw_free(IS);
|
|
313 as -> input_data = t;
|
329
|
314 goto nextfile;
|
|
315 }
|
|
316 linebuff[lbloc] = '\0';
|
|
317 eol = 1;
|
|
318 }
|
|
319 else if (c == '\r')
|
|
320 {
|
|
321 linebuff[lbloc] = '\0';
|
|
322 eol = 1;
|
330
|
323 c2 = fgetc(IS -> data);
|
329
|
324 if (c2 == EOF)
|
|
325 c = EOF;
|
|
326 else if (c2 != '\n')
|
330
|
327 ungetc(c2, IS -> data);
|
329
|
328 }
|
|
329 else if (c == '\n')
|
|
330 {
|
|
331 linebuff[lbloc] = '\0';
|
|
332 eol = 1;
|
330
|
333 c2 = fgetc(IS -> data);
|
329
|
334 if (c2 == EOF)
|
|
335 c = EOF;
|
|
336 else if (c2 != '\r')
|
330
|
337 ungetc(c2, IS -> data);
|
329
|
338 }
|
|
339 else
|
|
340 {
|
|
341 if (lbloc < 2048)
|
|
342 linebuff[lbloc++] = c;
|
|
343 }
|
|
344 if (eol)
|
|
345 {
|
|
346 s = lw_strdup(linebuff);
|
|
347 return s;
|
|
348 }
|
|
349 }
|
|
350
|
|
351 case input_type_string:
|
|
352 /* read from a string */
|
330
|
353 if (((char *)(IS -> data))[IS -> data2] == '\0')
|
329
|
354 {
|
|
355 struct input_stack *t;
|
330
|
356 lw_free(IS -> data);
|
|
357 lw_free(IS -> filespec);
|
|
358 t = IS -> next;
|
|
359 lw_free(IS);
|
|
360 as -> input_data = t;
|
329
|
361 goto nextfile;
|
|
362 }
|
330
|
363 s = (char *)(IS -> data);
|
329
|
364 lbloc = 0;
|
|
365 for (;;)
|
|
366 {
|
|
367 int c;
|
330
|
368 c = s[IS -> data2];
|
329
|
369 if (c)
|
330
|
370 IS -> data2++;
|
329
|
371 if (c == '\0')
|
|
372 {
|
|
373 linebuff[lbloc] = '\0';
|
|
374 eol = 1;
|
|
375 }
|
|
376 else if (c == '\r')
|
|
377 {
|
|
378 linebuff[lbloc] = '\0';
|
|
379 eol = 1;
|
330
|
380 if (s[IS -> data2] == '\n')
|
|
381 IS -> data2++;
|
329
|
382 }
|
|
383 else if (c == '\n')
|
|
384 {
|
|
385 linebuff[lbloc] = '\0';
|
|
386 eol = 1;
|
330
|
387 if (s[IS -> data2] == '\r')
|
|
388 IS -> data2++;
|
329
|
389 }
|
|
390 else
|
|
391 {
|
|
392 if (lbloc < 2048)
|
|
393 linebuff[lbloc++] = c;
|
|
394 }
|
|
395 if (eol)
|
|
396 {
|
|
397 s = lw_strdup(linebuff);
|
|
398 return s;
|
|
399 }
|
|
400 }
|
328
|
401
|
329
|
402 default:
|
|
403 lw_error("Problem reading from unknown input type");
|
|
404 }
|
328
|
405 }
|
330
|
406
|
|
407 char *input_curspec(asmstate_t *as)
|
|
408 {
|
|
409 if (IS)
|
|
410 return IS -> filespec;
|
|
411 return NULL;
|
|
412 }
|