diff lwasm/pseudo.c @ 225:058f18119025

Fixed filename parsing bug in include directive and added includebin directive
author lost
date Thu, 11 Jun 2009 23:29:15 +0000
parents 271c0ef9ea60
children 59a138df0401
line wrap: on
line diff
--- a/lwasm/pseudo.c	Thu Jun 11 23:12:31 2009 +0000
+++ b/lwasm/pseudo.c	Thu Jun 11 23:29:15 2009 +0000
@@ -21,6 +21,8 @@
 This file implements the various pseudo operations.
 */
 #include <config.h>
+#include <errno.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "lwasm.h"
@@ -117,6 +119,10 @@
 	memcpy(fn, *p, v1);
 	fn[v1] = '\0';
 
+	(*p) += v1;
+	if (**p == '"')
+		(*p)++;
+
 	// end local label context on include	
 	as -> context = lwasm_next_context(as);
 	if (lwasm_read_file(as, fn) < 0)
@@ -126,6 +132,75 @@
 	lwasm_free(fn);
 }
 
+/*
+The operand for includebin is a string optionally enclosed in "
+*/
+OPFUNC(pseudo_includebin)
+{
+	int v1;
+	char *fn;
+	FILE *f;
+	
+	// only include files on pass 1
+	while (**p && isspace(**p))
+		(*p)++;
+
+	if (!**p)
+	{
+		register_error(as, l, 1, "Bad file name");
+		return;
+	}
+
+	if (**p == '"')
+	{
+		// search for ending "
+		(*p)++;
+		for (v1 = 0; *((*p)+v1) && *((*p)+v1) != '"'; v1++)
+			/* do nothing */ ;
+		if (*((*p)+v1) != '"')
+		{
+			register_error(as, l, 1, "Bad file name");
+			return;
+		}
+	}
+	else
+	{
+		// search for a space type character
+		for (v1 = 0; *((*p)+v1) && !isspace(*((*p)+v1)); v1++)
+			;
+	}
+
+	fn = lwasm_alloc(v1 + 1);
+	memcpy(fn, *p, v1);
+	fn[v1] = '\0';
+
+	(*p) += v1;
+	if (**p == '"')
+		(*p)++;
+
+	// open the file
+	f = fopen(fn, "rb");
+	if (!f)
+	{
+		register_error(as, l, 1, "Cannot open file: %s", strerror(errno));
+		register_error(as, l, 2, "Cannot open file: %s", strerror(errno));
+	}
+	
+	// don't need fn any more
+	lwasm_free(fn);
+	
+	// read the contents of the file and "emit()" it
+	while (!feof(f) && !ferror(f))
+	{
+		v1 = fgetc(f);
+		if (v1 == EOF)
+			break;
+		lwasm_emit(as, l, v1);
+	}
+	// close file
+	fclose(f);
+}
+
 OPFUNC(pseudo_rmb)
 {
 	int r, v;