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);
|
392
|
177 return;
|
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);
|
392
|
189 return;
|
329
|
190 }
|
392
|
191 debug_message(as, 2, "Failed to open: (cd) %s (%s)\n", p2, strerror(errno));
|
329
|
192 lw_free(p2);
|
|
193
|
|
194 /* now check relative to entries in the search path */
|
|
195 lw_stringlist_reset(as -> include_list);
|
|
196 while (p = lw_stringlist_current(as -> include_list))
|
|
197 {
|
|
198 0 == asprintf(&p2, "%s/%s", p, s);
|
386
|
199 debug_message(as, 1, "Open (sp): %s\n", p2);
|
330
|
200 IS -> data = fopen(p2, "rb");
|
|
201 if (IS -> data)
|
329
|
202 {
|
|
203 input_pushpath(as, p2);
|
|
204 lw_free(p2);
|
|
205 return;
|
|
206 }
|
392
|
207 debug_message(as, 2, "Failed to open: (sp) %s (%s)\n", p2, strerror(errno));
|
329
|
208 lw_free(p2);
|
|
209 lw_stringlist_next(as -> include_list);
|
|
210 }
|
|
211 lw_error("Cannot open include file '%s': %s", s, strerror(errno));
|
392
|
212 break;
|
332
|
213
|
329
|
214 case input_type_file:
|
386
|
215 debug_message(as, 1, "Opening (reg): %s\n", s);
|
330
|
216 IS -> data = fopen(s, "rb");
|
329
|
217
|
330
|
218 if (!IS -> data)
|
329
|
219 {
|
|
220 lw_error("Cannot open file '%s': %s", s, strerror(errno));
|
|
221 }
|
|
222 input_pushpath(as, s);
|
|
223 return;
|
|
224 }
|
|
225
|
332
|
226 lw_error("Cannot figure out how to open '%s'.", t -> filespec);
|
329
|
227 }
|
|
228
|
356
|
229 FILE *input_open_standalone(asmstate_t *as, char *s)
|
|
230 {
|
|
231 char *s2;
|
|
232 FILE *fp;
|
|
233 char *p, *p2;
|
|
234
|
|
235 /* first check for absolute path and if so, skip path */
|
|
236 if (*s == '/')
|
|
237 {
|
|
238 /* absolute path */
|
386
|
239 debug_message(as, 2, "Open file (st abs) %s", s);
|
356
|
240 fp = fopen(s, "rb");
|
|
241 if (!fp)
|
|
242 {
|
|
243 return NULL;
|
|
244 }
|
|
245 return fp;
|
|
246 }
|
|
247
|
|
248 /* relative path, check relative to "current file" directory */
|
|
249 p = lw_stack_top(as -> file_dir);
|
|
250 0 == asprintf(&p2, "%s/%s", p, s);
|
386
|
251 debug_message(as, 2, "Open file (st cd) %s", p2);
|
356
|
252 fp = fopen(p2, "rb");
|
|
253 if (fp)
|
|
254 {
|
|
255 lw_free(p2);
|
|
256 return fp;
|
|
257 }
|
|
258 lw_free(p2);
|
|
259
|
|
260 /* now check relative to entries in the search path */
|
|
261 lw_stringlist_reset(as -> include_list);
|
|
262 while (p = lw_stringlist_current(as -> include_list))
|
|
263 {
|
|
264 0 == asprintf(&p2, "%s/%s", p, s);
|
386
|
265 debug_message(as, 2, "Open file (st ip) %s", p2);
|
356
|
266 fp = fopen(p2, "rb");
|
|
267 if (fp)
|
|
268 {
|
|
269 lw_free(p2);
|
|
270 return fp;
|
|
271 }
|
|
272 lw_free(p2);
|
|
273 lw_stringlist_next(as -> include_list);
|
|
274 }
|
|
275
|
|
276 return NULL;
|
|
277 }
|
|
278
|
329
|
279 char *input_readline(asmstate_t *as)
|
|
280 {
|
|
281 char *s;
|
|
282 char linebuff[2049];
|
|
283 int lbloc;
|
|
284 int eol = 0;
|
|
285
|
|
286 /* if no file is open, open one */
|
|
287 nextfile:
|
330
|
288 if (!IS) {
|
329
|
289 s = lw_stringlist_current(as -> input_files);
|
|
290 if (!s)
|
|
291 return NULL;
|
|
292 lw_stringlist_next(as -> input_files);
|
|
293 input_open(as, s);
|
328
|
294 }
|
|
295
|
330
|
296 switch (IS -> type)
|
329
|
297 {
|
|
298 case input_type_file:
|
|
299 case input_type_include:
|
|
300 /* read from a file */
|
332
|
301 lbloc = 0;
|
329
|
302 for (;;)
|
|
303 {
|
|
304 int c, c2;
|
330
|
305 c = fgetc(IS -> data);
|
329
|
306 if (c == EOF)
|
|
307 {
|
|
308 if (lbloc == 0)
|
|
309 {
|
|
310 struct input_stack *t;
|
330
|
311 fclose(IS -> data);
|
329
|
312 lw_free(lw_stack_pop(as -> file_dir));
|
330
|
313 lw_free(IS -> filespec);
|
|
314 t = IS -> next;
|
|
315 lw_free(IS);
|
|
316 as -> input_data = t;
|
329
|
317 goto nextfile;
|
|
318 }
|
|
319 linebuff[lbloc] = '\0';
|
|
320 eol = 1;
|
|
321 }
|
|
322 else if (c == '\r')
|
|
323 {
|
|
324 linebuff[lbloc] = '\0';
|
|
325 eol = 1;
|
330
|
326 c2 = fgetc(IS -> data);
|
329
|
327 if (c2 == EOF)
|
|
328 c = EOF;
|
|
329 else if (c2 != '\n')
|
330
|
330 ungetc(c2, IS -> data);
|
329
|
331 }
|
|
332 else if (c == '\n')
|
|
333 {
|
|
334 linebuff[lbloc] = '\0';
|
|
335 eol = 1;
|
330
|
336 c2 = fgetc(IS -> data);
|
329
|
337 if (c2 == EOF)
|
|
338 c = EOF;
|
|
339 else if (c2 != '\r')
|
330
|
340 ungetc(c2, IS -> data);
|
329
|
341 }
|
|
342 else
|
|
343 {
|
|
344 if (lbloc < 2048)
|
|
345 linebuff[lbloc++] = c;
|
|
346 }
|
|
347 if (eol)
|
|
348 {
|
|
349 s = lw_strdup(linebuff);
|
|
350 return s;
|
|
351 }
|
|
352 }
|
|
353
|
|
354 case input_type_string:
|
|
355 /* read from a string */
|
330
|
356 if (((char *)(IS -> data))[IS -> data2] == '\0')
|
329
|
357 {
|
|
358 struct input_stack *t;
|
330
|
359 lw_free(IS -> data);
|
|
360 lw_free(IS -> filespec);
|
|
361 t = IS -> next;
|
|
362 lw_free(IS);
|
|
363 as -> input_data = t;
|
329
|
364 goto nextfile;
|
|
365 }
|
330
|
366 s = (char *)(IS -> data);
|
329
|
367 lbloc = 0;
|
|
368 for (;;)
|
|
369 {
|
|
370 int c;
|
330
|
371 c = s[IS -> data2];
|
329
|
372 if (c)
|
330
|
373 IS -> data2++;
|
329
|
374 if (c == '\0')
|
|
375 {
|
|
376 linebuff[lbloc] = '\0';
|
|
377 eol = 1;
|
|
378 }
|
|
379 else if (c == '\r')
|
|
380 {
|
|
381 linebuff[lbloc] = '\0';
|
|
382 eol = 1;
|
330
|
383 if (s[IS -> data2] == '\n')
|
|
384 IS -> data2++;
|
329
|
385 }
|
|
386 else if (c == '\n')
|
|
387 {
|
|
388 linebuff[lbloc] = '\0';
|
|
389 eol = 1;
|
330
|
390 if (s[IS -> data2] == '\r')
|
|
391 IS -> data2++;
|
329
|
392 }
|
|
393 else
|
|
394 {
|
|
395 if (lbloc < 2048)
|
|
396 linebuff[lbloc++] = c;
|
|
397 }
|
|
398 if (eol)
|
|
399 {
|
|
400 s = lw_strdup(linebuff);
|
|
401 return s;
|
|
402 }
|
|
403 }
|
328
|
404
|
329
|
405 default:
|
|
406 lw_error("Problem reading from unknown input type");
|
|
407 }
|
328
|
408 }
|
330
|
409
|
|
410 char *input_curspec(asmstate_t *as)
|
|
411 {
|
|
412 if (IS)
|
|
413 return IS -> filespec;
|
|
414 return NULL;
|
|
415 }
|