comparison lwasm/input.c @ 224:3864d96ee8c7

Make unicorns notice referenced files better Fix bug related to includebin and also make unicorns notice all files opened through the input subsystem when generating the resource list.
author William Astle <lost@l-w.ca>
date Sun, 15 Jul 2012 20:50:18 -0600
parents afd50d6b4113
children 4b36eaaf80c3
comparison
equal deleted inserted replaced
223:211fc8038b8d 224:3864d96ee8c7
77 sprintf(r, "%s/%s", p, f); 77 sprintf(r, "%s/%s", p, f);
78 return r; 78 return r;
79 } 79 }
80 80
81 #define IS ((struct input_stack *)(as -> input_data)) 81 #define IS ((struct input_stack *)(as -> input_data))
82
83 struct ifl *ifl_head = NULL;
84
85 /* this adds real filenames that were opened to a list */
86 void input_add_to_resource_list(asmstate_t *as, const char *s)
87 {
88 struct ifl *ifl;
89
90 /* first see if the file is already referenced */
91 for (ifl = ifl_head; ifl; ifl = ifl -> next)
92 {
93 if (strcmp(s, ifl -> fn) == 0)
94 break;
95 }
96 if (!ifl)
97 {
98 ifl = lw_alloc(sizeof(struct ifl));
99 ifl -> next = ifl_head;
100 ifl_head = ifl;
101 ifl -> fn = lw_strdup(s);
102 }
103 }
82 104
83 void input_init(asmstate_t *as) 105 void input_init(asmstate_t *as)
84 { 106 {
85 struct input_stack *t; 107 struct input_stack *t;
86 108
198 else 220 else
199 { 221 {
200 as -> fileerr = 1; 222 as -> fileerr = 1;
201 } 223 }
202 input_pushpath(as, s); 224 input_pushpath(as, s);
225 input_add_to_resource_list(as, s);
203 return; 226 return;
204 } 227 }
205 228
206 /* relative path, check relative to "current file" directory */ 229 /* relative path, check relative to "current file" directory */
207 p = lw_stack_top(as -> file_dir); 230 p = lw_stack_top(as -> file_dir);
209 debug_message(as, 1, "Open: (cd) %s\n", p2); 232 debug_message(as, 1, "Open: (cd) %s\n", p2);
210 IS -> data = fopen(p2, "rb"); 233 IS -> data = fopen(p2, "rb");
211 if (IS -> data) 234 if (IS -> data)
212 { 235 {
213 input_pushpath(as, p2); 236 input_pushpath(as, p2);
237 input_add_to_resource_list(as, p2);
214 lw_free(p2); 238 lw_free(p2);
215 return; 239 return;
216 } 240 }
217 debug_message(as, 2, "Failed to open: (cd) %s (%s)\n", p2, strerror(errno)); 241 debug_message(as, 2, "Failed to open: (cd) %s (%s)\n", p2, strerror(errno));
218 lw_free(p2); 242 lw_free(p2);
225 debug_message(as, 1, "Open (sp): %s\n", p2); 249 debug_message(as, 1, "Open (sp): %s\n", p2);
226 IS -> data = fopen(p2, "rb"); 250 IS -> data = fopen(p2, "rb");
227 if (IS -> data) 251 if (IS -> data)
228 { 252 {
229 input_pushpath(as, p2); 253 input_pushpath(as, p2);
254 input_add_to_resource_list(as, p2);
230 lw_free(p2); 255 lw_free(p2);
231 return; 256 return;
232 } 257 }
233 debug_message(as, 2, "Failed to open: (sp) %s (%s)\n", p2, strerror(errno)); 258 debug_message(as, 2, "Failed to open: (sp) %s (%s)\n", p2, strerror(errno));
234 lw_free(p2); 259 lw_free(p2);
250 if (!IS -> data) 275 if (!IS -> data)
251 { 276 {
252 lw_error("Cannot open file '%s': %s\n", s, strerror(errno)); 277 lw_error("Cannot open file '%s': %s\n", s, strerror(errno));
253 } 278 }
254 input_pushpath(as, s); 279 input_pushpath(as, s);
280 input_add_to_resource_list(as, s);
255 return; 281 return;
256 } 282 }
257 283
258 lw_error("Cannot figure out how to open '%s'.\n", t -> filespec); 284 lw_error("Cannot figure out how to open '%s'.\n", t -> filespec);
259 } 285 }
260 286
261 FILE *input_open_standalone(asmstate_t *as, char *s) 287 FILE *input_open_standalone(asmstate_t *as, char *s, char **rfn)
262 { 288 {
263 // char *s2; 289 // char *s2;
264 FILE *fp; 290 FILE *fp;
265 char *p, *p2; 291 char *p, *p2;
266 292
293 debug_message(as, 2, "Open file (st) %s", s);
267 /* first check for absolute path and if so, skip path */ 294 /* first check for absolute path and if so, skip path */
268 if (*s == '/') 295 if (*s == '/')
269 { 296 {
270 /* absolute path */ 297 /* absolute path */
271 debug_message(as, 2, "Open file (st abs) %s", s); 298 debug_message(as, 2, "Open file (st abs) %s", s);
274 fp = fopen(s, "rb"); 301 fp = fopen(s, "rb");
275 if (!fp) 302 if (!fp)
276 { 303 {
277 return NULL; 304 return NULL;
278 } 305 }
306 if (rfn)
307 *rfn = lw_strdup(s);
308 input_add_to_resource_list(as, s);
279 return fp; 309 return fp;
280 } 310 }
281 311
282 /* relative path, check relative to "current file" directory */ 312 /* relative path, check relative to "current file" directory */
283 p = lw_stack_top(as -> file_dir); 313 p = lw_stack_top(as -> file_dir);
286 fp = fopen(p2, "rb"); 316 fp = fopen(p2, "rb");
287 if (fp) 317 if (fp)
288 { 318 {
289 if (as -> flags & FLAG_DEPEND) 319 if (as -> flags & FLAG_DEPEND)
290 printf("%s\n", p2); 320 printf("%s\n", p2);
321 input_add_to_resource_list(as, p2);
322 if (rfn)
323 *rfn = lw_strdup(p2);
291 lw_free(p2); 324 lw_free(p2);
292 return fp; 325 return fp;
293 } 326 }
294 lw_free(p2); 327 lw_free(p2);
295 328
302 fp = fopen(p2, "rb"); 335 fp = fopen(p2, "rb");
303 if (fp) 336 if (fp)
304 { 337 {
305 if (as -> flags & FLAG_DEPEND) 338 if (as -> flags & FLAG_DEPEND)
306 printf("%s\n", p2); 339 printf("%s\n", p2);
340 input_add_to_resource_list(as, p2);
341 if (rfn)
342 *rfn = lw_strdup(p2);
307 lw_free(p2); 343 lw_free(p2);
308 return fp; 344 return fp;
309 } 345 }
310 lw_free(p2); 346 lw_free(p2);
311 lw_stringlist_next(as -> include_list); 347 lw_stringlist_next(as -> include_list);