# HG changeset patch # User lost # Date 1232225698 0 # Node ID 14d835cf02d9bc3621607dde2cd578875d46699f # Parent 7a3d66e72a4ce94f3382821bbc24e9e5f108a21f Handle input files on command line and add some memory management utility functions diff -r 7a3d66e72a4c -r 14d835cf02d9 src/Makefile.am --- a/src/Makefile.am Sat Jan 17 20:54:20 2009 +0000 +++ b/src/Makefile.am Sat Jan 17 20:54:58 2009 +0000 @@ -1,3 +1,3 @@ bin_PROGRAMS = lwlink -lwlink_SOURCES = main.c lwlink.c -EXTRA_DIST = lwlink.h +lwlink_SOURCES = main.c lwlink.c util.c +EXTRA_DIST = lwlink.h util.h diff -r 7a3d66e72a4c -r 14d835cf02d9 src/lwlink.c --- a/src/lwlink.c Sat Jan 17 20:54:20 2009 +0000 +++ b/src/lwlink.c Sat Jan 17 20:54:58 2009 +0000 @@ -33,8 +33,18 @@ #include #include "lwlink.h" +#include "util.h" int debug_level = 0; int outformat = OUTPUT_DECB; char *outfile = NULL; +char **inputfiles = NULL; +int ninputfiles = 0; + +void add_input_file(char *fn) +{ + inputfiles = lw_realloc(inputfiles, sizeof(char *) * (ninputfiles + 1)); + inputfiles[ninputfiles++] = lw_strdup(fn); +} + diff -r 7a3d66e72a4c -r 14d835cf02d9 src/lwlink.h --- a/src/lwlink.h Sat Jan 17 20:54:20 2009 +0000 +++ b/src/lwlink.h Sat Jan 17 20:54:58 2009 +0000 @@ -32,7 +32,15 @@ extern int debug_level; extern int outformat; extern char *outfile; +extern int ninputfiles; +extern char **inputfiles; +#define __lwlink_E__ extern +#else +#define __lwlink_E__ #endif // __lwlink_c_seen__ +__lwlink_E__ void add_input_file(char *fn); + +#undef __lwlink_E__ #endif //__lwlink_h_seen__ diff -r 7a3d66e72a4c -r 14d835cf02d9 src/main.c --- a/src/main.c Sat Jan 17 20:54:20 2009 +0000 +++ b/src/main.c Sat Jan 17 20:54:58 2009 +0000 @@ -43,9 +43,6 @@ { case 'o': // output - if (outfile) - { - } outfile = arg; break; @@ -83,7 +80,7 @@ break; case ARGP_KEY_ARG: - // FIXME: input files! + add_input_file(arg); break; default: @@ -120,6 +117,11 @@ int main(int argc, char **argv) { argp_parse(&argp, argc, argv, 0, 0, NULL); - + if (ninputfiles == 0) + { + fprintf(stderr, "No input files\n"); + exit(1); + } + exit(0); } diff -r 7a3d66e72a4c -r 14d835cf02d9 src/util.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/util.c Sat Jan 17 20:54:58 2009 +0000 @@ -0,0 +1,84 @@ +/* +util.c +Copyright © 2008 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 . +*/ + +/* +Utility functions +*/ + +#define __util_c_seen__ + +#include +#include +#include +#include + +#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; + } + + ptr = realloc(optr, size); + if (!ptr) + { + fprintf(stderr, "lw_realloc(): memory allocation error\n"); + exit(1); + } +} + +void lw_free(void *ptr) +{ + if (ptr) + free(ptr); +} + +char *lw_strdup(const char *s) +{ + char *d; + + d = strdup(s); + if (!d) + { + fprintf(stderr, "lw_strdup(): memory allocation error\n"); + exit(1); + } + + return d; +} diff -r 7a3d66e72a4c -r 14d835cf02d9 src/util.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/util.h Sat Jan 17 20:54:58 2009 +0000 @@ -0,0 +1,44 @@ +/* +util.h +Copyright © 2008 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 . +*/ + +/* +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__