changeset 330:81c005b82775

More tinkering with input subsystem
author lost
date Sun, 28 Feb 2010 05:35:50 +0000
parents c15cca3ae6a2
children ed3553296580
files lwasm/Makefile.am lwasm/input.c lwasm/input.h lwasm/lwasm.h lwasm/main.c
diffstat 5 files changed, 91 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/lwasm/Makefile.am	Sun Feb 28 05:01:31 2010 +0000
+++ b/lwasm/Makefile.am	Sun Feb 28 05:35:50 2010 +0000
@@ -2,4 +2,4 @@
 bin_PROGRAMS = lwasm
 lwasm_SOURCES = main.c pragma.c input.c
 lwasm_LDADD = -L$(top_builddir)/lib -L$(top_srcdir)/lib -L$(top_builddir)/lwlib -L$(top_srcdir)/lwlib -lgnu -llw
-EXTRA_DIST =  lwasm.h
+EXTRA_DIST =  lwasm.h input.h
--- a/lwasm/input.c	Sun Feb 28 05:01:31 2010 +0000
+++ b/lwasm/input.c	Sun Feb 28 05:35:50 2010 +0000
@@ -58,17 +58,20 @@
 	char *filespec;
 };
 
-static struct input_stack *is = NULL;
+#define IS	((struct input_stack *)(as -> input_data))
 
 void input_init(asmstate_t *as)
 {
 	struct input_stack *t;
-	
+
+	if (as -> file_dir)
+		lw_stack_destroy(as -> file_dir);
+	as -> file_dir = lw_stack_create(lw_free);
 	lw_stringlist_reset(as -> input_files);
-	while (is)
+	while (IS)
 	{
-		t = is;
-		is = is -> next;
+		t = IS;
+		as -> input_data = IS -> next;
 		lw_free(t);
 	}
 }
@@ -113,8 +116,8 @@
 	t -> type = input_type_string;
 	t -> data = lw_strdup(str);
 	t -> data2 = 0;
-	t -> next = is;
-	is = t;
+	t -> next = IS;
+	as -> input_data = t;
 	t -> filespec = lw_strdup(s);
 }
 
@@ -148,18 +151,18 @@
 			t -> type = input_type_error;
 	}
 		
-	t -> next = is;
-	is = t;
+	t -> next = IS;
+	as -> input_data = t;
 	
-	switch (is -> type)
+	switch (IS -> type)
 	{
 	case input_type_include:
 		/* first check for absolute path and if so, skip path */
 		if (*s == '/')
 		{
 			/* absolute path */
-			is -> data = fopen(s, "rb");
-			if (!is -> data)
+			IS -> data = fopen(s, "rb");
+			if (!IS -> data)
 			{
 				lw_error("Cannot open file '%s': %s", s, strerror(errno));
 			}
@@ -170,8 +173,8 @@
 		/* relative path, check relative to "current file" directory */
 		p = lw_stack_top(as -> file_dir);
 		0 == asprintf(&p2, "%s/%s", p, s);
-		is -> data = fopen(p2, "rb");
-		if (is -> data)
+		IS -> data = fopen(p2, "rb");
+		if (IS -> data)
 		{
 			input_pushpath(as, p2);
 			lw_free(p2);
@@ -184,8 +187,8 @@
 		while (p = lw_stringlist_current(as -> include_list))
 		{
 			0 == asprintf(&p2, "%s/%s", p, s);
-			is -> data = fopen(p2, "rb");
-			if (is -> data)
+			IS -> data = fopen(p2, "rb");
+			if (IS -> data)
 			{
 				input_pushpath(as, p2);
 				lw_free(p2);
@@ -197,9 +200,9 @@
 		lw_error("Cannot open include file '%s': %s", s, strerror(errno));
 
 	case input_type_file:
-		is -> data = fopen(s, "rb");
+		IS -> data = fopen(s, "rb");
 
-		if (!is -> data)
+		if (!IS -> data)
 		{
 			lw_error("Cannot open file '%s': %s", s, strerror(errno));
 		}
@@ -219,7 +222,7 @@
 	
 	/* if no file is open, open one */
 nextfile:
-	if (!is) {
+	if (!IS) {
 		s = lw_stringlist_current(as -> input_files);
 		if (!s)
 			return NULL;
@@ -227,7 +230,7 @@
 		input_open(as, s);
 	}
 	
-	switch (is -> type)
+	switch (IS -> type)
 	{
 	case input_type_file:
 	case input_type_include:
@@ -235,19 +238,19 @@
 		for (;;)
 		{
 			int c, c2;
-			c = fgetc(is -> data);
+			c = fgetc(IS -> data);
 			lbloc = 0;
 			if (c == EOF)
 			{
 				if (lbloc == 0)
 				{
 					struct input_stack *t;
-					fclose(is -> data);
+					fclose(IS -> data);
 					lw_free(lw_stack_pop(as -> file_dir));
-					lw_free(is -> filespec);
-					t = is -> next;
-					lw_free(is);
-					is = t;
+					lw_free(IS -> filespec);
+					t = IS -> next;
+					lw_free(IS);
+					as -> input_data = t;
 					goto nextfile;
 				}
 				linebuff[lbloc] = '\0';
@@ -257,21 +260,21 @@
 			{
 				linebuff[lbloc] = '\0';
 				eol = 1;
-				c2 = fgetc(is -> data);
+				c2 = fgetc(IS -> data);
 				if (c2 == EOF)
 					c = EOF;  
 				else if (c2 != '\n')
-					ungetc(c2, is -> data);  
+					ungetc(c2, IS -> data);  
 			}
 			else if (c == '\n')
 			{
 				linebuff[lbloc] = '\0';
 				eol = 1;
-				c2 = fgetc(is -> data);
+				c2 = fgetc(IS -> data);
 				if (c2 == EOF)
 					c = EOF;  
 				else if (c2 != '\r')
-					ungetc(c2, is -> data);  
+					ungetc(c2, IS -> data);  
 			}
 			else
 			{
@@ -287,24 +290,24 @@
 
 	case input_type_string:
 		/* read from a string */
-		if (((char *)(is -> data))[is -> data2] == '\0')
+		if (((char *)(IS -> data))[IS -> data2] == '\0')
 		{
 			struct input_stack *t;
-			lw_free(is -> data);
-			lw_free(is -> filespec);
-			t = is -> next;
-			lw_free(is);
-			is = t;
+			lw_free(IS -> data);
+			lw_free(IS -> filespec);
+			t = IS -> next;
+			lw_free(IS);
+			as -> input_data = t;
 			goto nextfile;
 		}
-		s = (char *)(is -> data);
+		s = (char *)(IS -> data);
 		lbloc = 0;
 		for (;;)
 		{
 			int c;
-			c = s[is -> data2];
+			c = s[IS -> data2];
 			if (c)
-				is -> data2++;
+				IS -> data2++;
 			if (c == '\0')
 			{
 				linebuff[lbloc] = '\0';
@@ -314,15 +317,15 @@
 			{
 				linebuff[lbloc] = '\0';
 				eol = 1;
-				if (s[is -> data2] == '\n')
-					is -> data2++;
+				if (s[IS -> data2] == '\n')
+					IS -> data2++;
 			}
 			else if (c == '\n')
 			{
 				linebuff[lbloc] = '\0';
 				eol = 1;
-				if (s[is -> data2] == '\r')
-					is -> data2++;
+				if (s[IS -> data2] == '\r')
+					IS -> data2++;
 			}
 			else
 			{
@@ -340,3 +343,10 @@
 		lw_error("Problem reading from unknown input type");
 	}
 }
+
+char *input_curspec(asmstate_t *as)
+{
+	if (IS)
+		return IS -> filespec;
+	return NULL;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwasm/input.h	Sun Feb 28 05:35:50 2010 +0000
@@ -0,0 +1,33 @@
+/*
+input.h
+
+Copyright © 2010 William Astle
+
+This file is part of LWTOOLS.
+
+LWTOOLS is free software: you can redistribute it and/or modify it under the
+terms of the GNU General Public License as published by the Free Software
+Foundation, either version 3 of the License, or (at your option) any later
+version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+more details.
+
+You should have received a copy of the GNU General Public License along with
+this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef ___input_h_seen___
+#define ___input_h_seen___
+
+#include "lwasm.h"
+
+extern void input_init(asmstate_t *as);
+extern void input_openstring(asmstate_t *as, char *s, char *str);
+extern void input_open(asmstate_t *as, char *s);
+extern char *input_readline(asmstate_t *as);
+extern char *input_curspec(asmstate_t *as);
+
+#endif
--- a/lwasm/lwasm.h	Sun Feb 28 05:01:31 2010 +0000
+++ b/lwasm/lwasm.h	Sun Feb 28 05:35:50 2010 +0000
@@ -68,6 +68,7 @@
 	char *list_file;					// name of file to list to
 	char *output_file;					// output file name	
 	lw_stringlist_t input_files;		// files to assemble
+	void *input_data;					// opaque data used by the input system
 	lw_stringlist_t include_list;		// include paths
 	lw_stack_t file_dir;				// stack of the "current file" dir
 } asmstate_t;
--- a/lwasm/main.c	Sun Feb 28 05:01:31 2010 +0000
+++ b/lwasm/main.c	Sun Feb 28 05:35:50 2010 +0000
@@ -30,6 +30,7 @@
 #include <lw_stringlist.h>
 
 #include "lwasm.h"
+#include "input.h"
 
 extern int parse_pragma_string(asmstate_t *as, char *str);
 
@@ -171,8 +172,6 @@
 	/* initialize assembler state */
 	asmstate.include_list = lw_stringlist_create();
 	asmstate.input_files = lw_stringlist_create();
-	asmstate.file_dir = lw_stack_create(lw_free);
-	input_init();
 
 	/* parse command line arguments */	
 	argp_parse(&argp, argc, argv, 0, 0, &asmstate);
@@ -182,5 +181,7 @@
 		asmstate.output_file = lw_strdup("a.out");
 	}
 
+	input_init(&asmstate);
+
 	exit(0);
 }