changeset 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 211fc8038b8d
children 823560a8c251
files lwasm/input.c lwasm/input.h lwasm/main.c lwasm/pseudo.c lwasm/unicorns.c
diffstat 5 files changed, 63 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/lwasm/input.c	Sun Jul 15 20:14:51 2012 -0600
+++ b/lwasm/input.c	Sun Jul 15 20:50:18 2012 -0600
@@ -80,6 +80,28 @@
 
 #define IS	((struct input_stack *)(as -> input_data))
 
+struct ifl *ifl_head = NULL;
+
+/* this adds real filenames that were opened to a list */
+void input_add_to_resource_list(asmstate_t *as, const char *s)
+{
+	struct ifl *ifl;
+	
+	/* first see if the file is already referenced */
+	for (ifl = ifl_head; ifl; ifl = ifl -> next)
+	{
+		if (strcmp(s, ifl -> fn) == 0)
+			break;
+	}
+	if (!ifl)
+	{
+		ifl = lw_alloc(sizeof(struct ifl));
+		ifl -> next = ifl_head;
+		ifl_head = ifl;
+		ifl -> fn = lw_strdup(s);
+	}
+}
+
 void input_init(asmstate_t *as)
 {
 	struct input_stack *t;
@@ -200,6 +222,7 @@
 				as -> fileerr = 1;
 			}
 			input_pushpath(as, s);
+			input_add_to_resource_list(as, s);
 			return;
 		}
 		
@@ -211,6 +234,7 @@
 		if (IS -> data)
 		{
 			input_pushpath(as, p2);
+			input_add_to_resource_list(as, p2);
 			lw_free(p2);
 			return;
 		}
@@ -227,6 +251,7 @@
 			if (IS -> data)
 			{
 				input_pushpath(as, p2);
+				input_add_to_resource_list(as, p2);
 				lw_free(p2);
 				return;
 			}
@@ -252,18 +277,20 @@
 			lw_error("Cannot open file '%s': %s\n", s, strerror(errno));
 		}
 		input_pushpath(as, s);
+		input_add_to_resource_list(as, s);
 		return;
 	}
 
 	lw_error("Cannot figure out how to open '%s'.\n", t -> filespec);
 }
 
-FILE *input_open_standalone(asmstate_t *as, char *s)
+FILE *input_open_standalone(asmstate_t *as, char *s, char **rfn)
 {
 //	char *s2;
 	FILE *fp;
 	char *p, *p2;
 
+	debug_message(as, 2, "Open file (st) %s", s);
 	/* first check for absolute path and if so, skip path */
 	if (*s == '/')
 	{
@@ -276,6 +303,9 @@
 		{
 			return NULL;
 		}
+		if (rfn)
+			*rfn = lw_strdup(s);
+		input_add_to_resource_list(as, s);
 		return fp;
 	}
 
@@ -288,6 +318,9 @@
 	{
 		if (as -> flags & FLAG_DEPEND)
 			printf("%s\n", p2);
+		input_add_to_resource_list(as, p2);
+		if (rfn)
+			*rfn = lw_strdup(p2);
 		lw_free(p2);
 		return fp;
 	}
@@ -304,6 +337,9 @@
 		{
 			if (as -> flags & FLAG_DEPEND)
 				printf("%s\n", p2);
+			input_add_to_resource_list(as, p2);
+			if (rfn)
+				*rfn = lw_strdup(p2);
 			lw_free(p2);
 			return fp;
 		}
--- a/lwasm/input.h	Sun Jul 15 20:14:51 2012 -0600
+++ b/lwasm/input.h	Sun Jul 15 20:50:18 2012 -0600
@@ -37,6 +37,14 @@
 extern void input_open(asmstate_t *as, char *s);
 extern char *input_readline(asmstate_t *as);
 extern char *input_curspec(asmstate_t *as);
-extern FILE *input_open_standalone(asmstate_t *as, char *s);
+extern FILE *input_open_standalone(asmstate_t *as, char *s, char **rfn);
+
+struct ifl
+{
+	const char *fn;
+	struct ifl *next;
+};
+
+extern struct ifl *ifl_head;
 
 #endif
--- a/lwasm/main.c	Sun Jul 15 20:14:51 2012 -0600
+++ b/lwasm/main.c	Sun Jul 15 20:50:18 2012 -0600
@@ -58,7 +58,7 @@
 	{ "includedir",	'I',	"PATH",		0,							"Add entry to include path" },
 	{ "define", 'D', "SYM[=VAL]", 0, "Automatically define SYM to be VAL (or 1)"},
 	{ "preprocess",	'P',	0,			0,							"Preprocess macros and conditionals and output revised source to stdout" },
-	{ "unicorns",	0x4242,	0,			0,							"Add sooper sekrit sauce"},
+	{ "unicorns",	0x142,	0,			0,							"Add sooper sekrit sauce"},
 	{ 0 }
 };
 
@@ -141,7 +141,7 @@
 		as -> flags |= FLAG_DEPEND | FLAG_DEPENDNOERR;
 		break;
 	
-	case 0x4242:
+	case 0x142:
 		as -> flags |= FLAG_UNICORNS;
 		break;
 
@@ -299,16 +299,13 @@
 
 	if (asmstate.flags & FLAG_DEPEND)
 	{
-		if ((asmstate.flags & FLAG_UNICORNS) == 0)
-		{
-			// output dependencies (other than "includebin")
-			char *n;
+		// output dependencies (other than "includebin")
+		char *n;
 		
-			while ((n = lw_stack_pop(asmstate.includelist)))
-			{
-				fprintf(stdout, "%s\n", n);
-				lw_free(n);
-			}
+		while ((n = lw_stack_pop(asmstate.includelist)))
+		{
+			fprintf(stdout, "%s\n", n);
+			lw_free(n);
 		}
 	}	
 	else
@@ -321,6 +318,7 @@
 
 	if (asmstate.flags & FLAG_UNICORNS)
 	{	
+		debug_message(&asmstate, 50, "Invoking unicorns");
 		lwasm_do_unicorns(&asmstate);
 	}
 	else
--- a/lwasm/pseudo.c	Sun Jul 15 20:14:51 2012 -0600
+++ b/lwasm/pseudo.c	Sun Jul 15 20:50:18 2012 -0600
@@ -1316,6 +1316,7 @@
 	int delim = 0;
 	FILE *fp;
 	long flen;
+	char *rfn;
 	
 	if (!**p)
 	{
@@ -1341,7 +1342,7 @@
 	if (delim && **p)
 		(*p)++;
 	
-	fp = input_open_standalone(as, fn);
+	fp = input_open_standalone(as, fn, &rfn);
 	if (!fp)
 	{
 		lwasm_register_error(as, l, "Cannot open file");
@@ -1349,7 +1350,7 @@
 		return;
 	}
 	
-	l -> lstr = fn;
+	l -> lstr = rfn;
 	
 	fseek(fp, 0, SEEK_END);
 	flen = ftell(fp);
@@ -1363,7 +1364,7 @@
 	FILE *fp;
 	int c;
 	
-	fp = input_open_standalone(as, l -> lstr);
+	fp = fopen(l -> lstr, "r");
 	if (!fp)
 	{
 		lwasm_register_error(as, l, "Cannot open file (emit)!");
--- a/lwasm/unicorns.c	Sun Jul 15 20:14:51 2012 -0600
+++ b/lwasm/unicorns.c	Sun Jul 15 20:50:18 2012 -0600
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "input.h"
 #include "lwasm.h"
 #include "lw_alloc.h"
 
@@ -47,18 +48,17 @@
 
 void lwasm_do_unicorns(asmstate_t *as)
 {
-	char *n;
+	struct ifl *ifl;
 	macrotab_t *me;
 	structtab_t *se;
 	int i;
 			
 	/* output file list */	
-	while ((n = lw_stack_pop(as -> includelist)))
+	for (ifl = ifl_head; ifl; ifl = ifl -> next)
 	{
 		fputs("RESOURCE: type=file,filename=", stdout);
-		print_urlencoding(stdout, n);
+		print_urlencoding(stdout, ifl -> fn);
 		fputc('\n', stdout);
-		lw_free(n);
 	}
 	
 	/* output macro list */