changeset 8:fdc11ef4115b

Switched lwlink to lw_cmdline from argp and also brought in lw_alloc and lw_string to replace util.c
author lost@l-w.ca
date Sat, 22 Jan 2011 09:58:24 -0700
parents 917b608b8c66
children 6eed14cccac9
files Makefile lwlib/lw_cmdline.c lwlink/expr.c lwlink/link.c lwlink/lwlink.c lwlink/main.c lwlink/map.c lwlink/objdump.c lwlink/readfiles.c lwlink/rules.make lwlink/script.c lwlink/util.c lwlink/util.h
diffstat 13 files changed, 60 insertions(+), 179 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sat Jan 22 09:40:52 2011 -0700
+++ b/Makefile	Sat Jan 22 09:58:24 2011 -0700
@@ -32,24 +32,26 @@
 lwasm/lwasm: $(lwasm_objs) lwlib lwasm/rules.make
 	$(CC) -o $@ $(lwasm_objs) $(LDFLAGS)
 
-lwlink/lwlink: $(lwlink_objs) lwlink/rules.make
-	$(CC) -o $@ $(lwlink_objs)
+lwlink/lwlink: $(lwlink_objs) lwlib lwlink/rules.make
+	$(CC) -o $@ $(lwlink_objs) $(LDFLAGS)
 
-lwlink/lwobjdump: $(lwobjdump_objs) lwlink/rules.make
-	$(CC) -o $@ $(lwobjdump_objs)
+lwlink/lwobjdump: $(lwobjdump_objs) lwlib lwlink/rules.make
+	$(CC) -o $@ $(lwobjdump_objs) $(LDFLAGS)
 
 lwar/lwar: $(lwar_objs) lwar/rules.make
 	$(CC) -o $@ $(lwar_objs)
 
+test: test.c lwlib
+	$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ test.c $(LDFLAGS)
 
 .PHONY: lwlib
 lwlib: lwlib/liblw.a
 
 lwlib/liblw.a: $(lwlib_objs) lwlib/rules.make
-	$(AR) rc $@ $^
+	$(AR) rc $@ $(lwlib_objs)
 
 %.d: %.c
-	@echo "Building dependencies for $@"
+#	@echo "Building dependencies for $@"
 	@$(CC) -MM $(CPPFLAGS) -o $*.d $<
 	@mv -f $*.d $*.d.tmp
 	@sed -e 's|.*:|$*.o $*.d:|' < $*.d.tmp > $*.d
--- a/lwlib/lw_cmdline.c	Sat Jan 22 09:40:52 2011 -0700
+++ b/lwlib/lw_cmdline.c	Sat Jan 22 09:58:24 2011 -0700
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 
 #include "lw_alloc.h"
 
@@ -207,7 +208,6 @@
 	struct lw_cmdline_options **llist;
 	int nopt;
 	int i;
-	int t;
 	char *tstr;
 	int col = 0;
 	
--- a/lwlink/expr.c	Sat Jan 22 09:40:52 2011 -0700
+++ b/lwlink/expr.c	Sat Jan 22 09:58:24 2011 -0700
@@ -28,14 +28,16 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <lw_alloc.h>
+#include <lw_string.h>
+
 #include "expr.h"
-#include "util.h"
 
 lw_expr_stack_t *lw_expr_stack_create(void)
 {
 	lw_expr_stack_t *s;
 	
-	s = lw_malloc(sizeof(lw_expr_stack_t));
+	s = lw_alloc(sizeof(lw_expr_stack_t));
 	s -> head = NULL;
 	s -> tail = NULL;
 	return s;
@@ -80,7 +82,7 @@
 {
 	lw_expr_term_t *t;
 
-	t = lw_malloc(sizeof(lw_expr_term_t));
+	t = lw_alloc(sizeof(lw_expr_term_t));
 	t -> term_type = LW_TERM_OPER;
 	t -> value = oper;
 	return t;
@@ -90,7 +92,7 @@
 {
 	lw_expr_term_t *t;
 	
-	t = lw_malloc(sizeof(lw_expr_term_t));
+	t = lw_alloc(sizeof(lw_expr_term_t));
 	t -> term_type = LW_TERM_INT;
 	t -> value = val;
 	return t;
@@ -100,7 +102,7 @@
 {
 	lw_expr_term_t *t;
 	
-	t = lw_malloc(sizeof(lw_expr_term_t));
+	t = lw_alloc(sizeof(lw_expr_term_t));
 	t -> term_type = LW_TERM_SYM;
 	t -> symbol = lw_strdup(sym);
 	t -> value = symtype;
@@ -135,7 +137,7 @@
 		exit(1);
 	}
 	
-	n = lw_malloc(sizeof(lw_expr_stack_node_t));
+	n = lw_alloc(sizeof(lw_expr_stack_node_t));
 	n -> next = NULL;
 	n -> prev = s -> tail;
 	n -> term = lw_expr_term_dup(t);
--- a/lwlink/link.c	Sat Jan 22 09:40:52 2011 -0700
+++ b/lwlink/link.c	Sat Jan 22 09:58:24 2011 -0700
@@ -25,9 +25,10 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <lw_alloc.h>
+
 #include "expr.h"
 #include "lwlink.h"
-#include "util.h"
 
 struct section_list *sectlist = NULL;
 int nsects = 0;
--- a/lwlink/lwlink.c	Sat Jan 22 09:40:52 2011 -0700
+++ b/lwlink/lwlink.c	Sat Jan 22 09:58:24 2011 -0700
@@ -29,8 +29,10 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <lw_alloc.h>
+#include <lw_string.h>
+
 #include "lwlink.h"
-#include "util.h"
 
 int debug_level = 0;
 int outformat = OUTPUT_DECB;
@@ -51,7 +53,7 @@
 void add_input_file(char *fn)
 {
 	inputfiles = lw_realloc(inputfiles, sizeof(fileinfo_t *) * (ninputfiles + 1));
-	inputfiles[ninputfiles] = lw_malloc(sizeof(fileinfo_t));
+	inputfiles[ninputfiles] = lw_alloc(sizeof(fileinfo_t));
 	memset(inputfiles[ninputfiles], 0, sizeof(fileinfo_t));
 	inputfiles[ninputfiles] -> forced = 1;
 	inputfiles[ninputfiles++] -> filename = lw_strdup(fn);
@@ -60,7 +62,7 @@
 void add_input_library(char *libname)
 {
 	inputfiles = lw_realloc(inputfiles, sizeof(fileinfo_t *) * (ninputfiles + 1));
-	inputfiles[ninputfiles] = lw_malloc(sizeof(fileinfo_t));
+	inputfiles[ninputfiles] = lw_alloc(sizeof(fileinfo_t));
 	memset(inputfiles[ninputfiles], 0, sizeof(fileinfo_t));
 	inputfiles[ninputfiles] -> islib = 1;
 	inputfiles[ninputfiles] -> forced = 0;
@@ -95,7 +97,7 @@
 	}
 	baseaddr = baseaddr & 0xffff;
 	
-	t = lw_malloc(l + 25);
+	t = lw_alloc(l + 25);
 	sprintf(t, "section %s load %04X", sectspec, baseaddr);
 	if (base)
 		*base = '=';
--- a/lwlink/main.c	Sat Jan 22 09:40:52 2011 -0700
+++ b/lwlink/main.c	Sat Jan 22 09:58:24 2011 -0700
@@ -22,22 +22,22 @@
 
 */
 
-#include <argp.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 
+#include <lw_cmdline.h>
+
 #include "lwlink.h"
 
 char *program_name;
 
 // command line option handling
-const char *argp_program_version = "LWLINK from " PACKAGE_STRING;
-const char *argp_program_bug_address = PACKAGE_BUGREPORT;
+#define PROGVER "lwlink from " PACKAGE_STRING
 
-static error_t parse_opts(int key, char *arg, struct argp_state *state)
+static int parse_opts(int key, char *arg, void *state)
 {
 	switch (key)
 	{
@@ -80,7 +80,7 @@
 			exit(1);
 		}
 		break;
-	case ARGP_KEY_END:
+	case lw_cmdline_key_end:
 		// done; sanity check
 		if (!outfile)
 			outfile = "a.out";
@@ -102,17 +102,17 @@
 		map_file = arg;
 		break;
 	
-	case ARGP_KEY_ARG:
+	case lw_cmdline_key_arg:
 		add_input_file(arg);
 		break;
 		
 	default:
-		return ARGP_ERR_UNKNOWN;
+		return lw_cmdline_err_unknown;
 	}
 	return 0;
 }
 
-static struct argp_option options[] =
+static struct lw_cmdline_options options[] =
 {
 	{ "output",		'o',	"FILE",	0,
 				"Output to FILE"},
@@ -137,12 +137,13 @@
 	{ 0 }
 };
 
-static struct argp argp =
+static struct lw_cmdline_parser cmdline_parser =
 {
 	options,
 	parse_opts,
-	"<input file> ...",
-	"LWLINK, a HD6309 and MC6809 cross-linker"
+	"INPUTFILE ...",
+	"lwlink, a HD6309 and MC6809 cross-linker",
+	PROGVER
 };
 
 extern void read_files(void);
@@ -159,7 +160,7 @@
 {
 	program_name = argv[0];
 
-	argp_parse(&argp, argc, argv, 0, 0, NULL);
+	lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, NULL);
 	if (ninputfiles == 0)
 	{
 		fprintf(stderr, "No input files\n");
--- a/lwlink/map.c	Sat Jan 22 09:40:52 2011 -0700
+++ b/lwlink/map.c	Sat Jan 22 09:58:24 2011 -0700
@@ -26,8 +26,9 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <lw_alloc.h>
+
 #include "lwlink.h"
-#include "util.h"
 
 struct symliste
 {
@@ -91,7 +92,7 @@
 					break;
 				pe = ce;
 			}
-			ne = lw_malloc(sizeof(struct symliste));
+			ne = lw_alloc(sizeof(struct symliste));
 			ne -> ext = 0;
 			ne -> addr = sym -> offset + sectlist[sn].ptr -> loadaddress;
 			ne -> next = ce;
--- a/lwlink/objdump.c	Sat Jan 22 09:40:52 2011 -0700
+++ b/lwlink/objdump.c	Sat Jan 22 09:58:24 2011 -0700
@@ -25,7 +25,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "util.h"
+#include <lw_alloc.h>
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -100,7 +100,7 @@
 	size = ftell(f);
 	rewind(f);
 		
-	filedata = lw_malloc(size);
+	filedata = lw_alloc(size);
 		
 	bread = fread(filedata, 1, size, f);
 	if (bread < size)
--- a/lwlink/readfiles.c	Sat Jan 22 09:40:52 2011 -0700
+++ b/lwlink/readfiles.c	Sat Jan 22 09:58:24 2011 -0700
@@ -22,14 +22,15 @@
 
 */
 
-#include <argp.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+#include <lw_alloc.h>
+#include <lw_string.h>
+
 #include "lwlink.h"
-#include "util.h"
 
 void read_lwobj16v0(fileinfo_t *fn);
 void read_lwar1v(fileinfo_t *fn);
@@ -77,7 +78,7 @@
 			for (j = 0; j < nlibdirs; j++)
 			{
 				s = strlen(libdirs[j]) + 7 + strlen(inputfiles[i] -> filename);
-				tf = lw_malloc(s + 1);
+				tf = lw_alloc(s + 1);
 				sprintf(tf, "%s/lib%s.a", libdirs[j], inputfiles[i] -> filename);
 				f = fopen(tf, "rb");
 				if (!f)
@@ -108,7 +109,7 @@
 		size = ftell(f);
 		rewind(f);
 		
-		inputfiles[i] -> filedata = lw_malloc(size);
+		inputfiles[i] -> filedata = lw_alloc(size);
 		inputfiles[i] -> filesize = size;
 		
 		bread = fread(inputfiles[i] -> filedata, 1, size, f);
@@ -219,7 +220,7 @@
 			// val is now the symbol value
 			
 			// create symbol table entry
-			se = lw_malloc(sizeof(symtab_t));
+			se = lw_alloc(sizeof(symtab_t));
 			se -> next = s -> localsyms;
 			s -> localsyms = se;
 			se -> sym = fp;
@@ -241,7 +242,7 @@
 			// val is now the symbol value
 			
 			// create symbol table entry
-			se = lw_malloc(sizeof(symtab_t));
+			se = lw_alloc(sizeof(symtab_t));
 			se -> next = s -> exportedsyms;
 			s -> exportedsyms = se;
 			se -> sym = fp;
@@ -258,7 +259,7 @@
 			lw_expr_term_t *term;
 			
 			// we have a reference
-			rp = lw_malloc(sizeof(reloc_t));
+			rp = lw_alloc(sizeof(reloc_t));
 			rp -> next = s -> incompletes;
 			s -> incompletes = rp;
 			rp -> offset = 0;
@@ -409,7 +410,7 @@
 		
 		// add the "sub" input file
 		fn -> subs = lw_realloc(fn -> subs, sizeof(fileinfo_t *) * (fn -> nsubs + 1));
-		fn -> subs[fn -> nsubs] = lw_malloc(sizeof(fileinfo_t));
+		fn -> subs[fn -> nsubs] = lw_alloc(sizeof(fileinfo_t));
 		memset(fn -> subs[fn -> nsubs], 0, sizeof(fileinfo_t));
 		fn -> subs[fn -> nsubs] -> filedata = fn -> filedata + cc;
 		fn -> subs[fn -> nsubs] -> filesize = flen;
--- a/lwlink/rules.make	Sat Jan 22 09:40:52 2011 -0700
+++ b/lwlink/rules.make	Sat Jan 22 09:58:24 2011 -0700
@@ -1,8 +1,8 @@
 dirname := $(dir $(lastword $(MAKEFILE_LIST)))
 
-lwlink_srcs_local := main.c lwlink.c util.c readfiles.c expr.c script.c link.c output.c map.c
+lwlink_srcs_local := main.c lwlink.c readfiles.c expr.c script.c link.c output.c map.c
 
-lwobjdump_srcs_l := objdump.c util.c
+lwobjdump_srcs_l := objdump.c
 
 
 lwlink_srcs := $(lwlink_srcs) $(addprefix $(dirname),$(lwlink_srcs_local))
--- a/lwlink/script.c	Sat Jan 22 09:40:52 2011 -0700
+++ b/lwlink/script.c	Sat Jan 22 09:58:24 2011 -0700
@@ -27,8 +27,10 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <lw_alloc.h>
+#include <lw_string.h>
+
 #include "lwlink.h"
-#include "util.h"
 
 // the built-in DECB target linker script
 static char *decb_script =
@@ -87,7 +89,7 @@
 		size = ftell(f);
 		rewind(f);
 		
-		script = lw_malloc(size + 2);
+		script = lw_alloc(size + 2);
 		
 		bread = fread(script, 1, size, f);
 		if (bread < size)
@@ -132,7 +134,7 @@
 			for (i = 0; i < nscriptls; i++)
 				size += strlen(scriptls[i]) + 1;
 			
-			rscript = lw_malloc(size + 1);
+			rscript = lw_alloc(size + 1);
 			oscript = rscript;
 			for (i = 0; i < nscriptls; i++)
 			{
@@ -155,7 +157,7 @@
 		for (ptr = script; *ptr && *ptr != '\n' && *ptr != '\r'; ptr++)
 			/* do nothing */ ;
 		
-		line = lw_malloc(ptr - script + 1);
+		line = lw_alloc(ptr - script + 1);
 		memcpy(line, script, ptr - script);
 		line[ptr - script] = '\0';
 
--- a/lwlink/util.c	Sat Jan 22 09:40:52 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,87 +0,0 @@
-/*
-util.c
-Copyright © 2009 William Astle
-
-This file is part of LWLINK.
-
-LWLINK 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/>.
-*/
-
-/*
-Utility functions
-*/
-
-#define __util_c_seen__
-#include <malloc.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "util.h"
-
-void *lw_malloc(int size)
-{
-	void *ptr;
-	
-	ptr = malloc(size);
-	if (!ptr)
-	{
-		// bail out; memory allocation error
-		fprintf(stderr, "lw_malloc(): Memory allocation error\n");
-		exit(1);
-	}
-	return ptr;
-}
-
-void *lw_realloc(void *optr, int size)
-{
-	void *ptr;
-	
-	if (size == 0)
-	{
-		lw_free(optr);
-		return NULL;
-	}
-	
-	ptr = realloc(optr, size);
-	if (!ptr)
-	{
-		fprintf(stderr, "lw_realloc(): memory allocation error\n");
-		exit(1);
-	}
-	return ptr;
-}
-
-void lw_free(void *ptr)
-{
-	if (ptr)
-		free(ptr);
-}
-
-char *lw_strdup(const char *s)
-{
-	char *d;
-	
-	if (!s)
-		return NULL;
-
-	d = strdup(s);
-	if (!d)
-	{
-		fprintf(stderr, "lw_strdup(): memory allocation error\n");
-		exit(1);
-	}
-	
-	return d;
-}
--- a/lwlink/util.h	Sat Jan 22 09:40:52 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,44 +0,0 @@
-/*
-util.h
-Copyright © 2009 William Astle
-
-This file is part of LWLINK.
-
-LWLINK 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/>.
-*/
-
-/*
-Utility functions
-*/
-
-#ifndef __util_h_seen__
-#define __util_h_seen__
-
-#ifndef __util_c_seen__
-#define __util_E__ extern
-#else
-#define __util_E__
-#endif
-
-// allocate memory
-__util_E__ void *lw_malloc(int size);
-__util_E__ void lw_free(void *ptr);
-__util_E__ void *lw_realloc(void *optr, int size);
-
-// string stuff
-__util_E__ char *lw_strdup(const char *s);
-
-#undef __util_E__
-
-#endif // __util_h_seen__