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