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