scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit e06d5bda89394fa97fb77af7816129685b27899f
parent cbd198b38bee47a2b58541ff413f847d78db4b67
Author: Quentin Rameau <quinq@fifth.space>
Date:   Thu, 16 Jun 2016 16:17:37 +0200

[lib][driver] move newitem() + related struct to lib

Diffstat:
Mdriver/posix/scc.c | 29+++++++----------------------
Minc/cc.h | 8++++++++
Mlib/Makefile | 2+-
Alib/newitem.c | 12++++++++++++
4 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/driver/posix/scc.c b/driver/posix/scc.c @@ -46,14 +46,9 @@ static struct tool { [STRIP] = { .bin = "strip", .cmd = "strip", }, }; -struct objects { - char **f; - int n; -}; - char *argv0; static char *arch, *outfile; -static struct objects objtmp, objout; +static struct items objtmp, objout; static int Eflag, Sflag, cflag, kflag, sflag; static void @@ -75,20 +70,10 @@ terminate(void) if (!kflag) { for (i = 0; i < objtmp.n; ++i) - unlink(objtmp.f[i]); + unlink(objtmp.s[i]); } } -static char ** -newitem(char **array, int num, char *item) -{ - char **ar = xrealloc(array, (num + 1) * sizeof(char **)); - - ar[num] = item; - - return ar; -} - static void addarg(int tool, char *arg) { @@ -212,14 +197,14 @@ settool(int tool, char *infile, int nexttool) break; case LD: for (i = 0; i < objtmp.n; ++i) - addarg(tool, xstrdup(objtmp.f[i])); + addarg(tool, xstrdup(objtmp.s[i])); for (i = 0; i < objout.n; ++i) - addarg(tool, xstrdup(objout.f[i])); + addarg(tool, xstrdup(objout.s[i])); break; case STRIP: if (cflag || kflag) { for (i = 0; i < objout.n; ++i) - addarg(tool, xstrdup(objout.f[i])); + addarg(tool, xstrdup(objout.s[i])); } if (!cflag && tools[LD].outfile) addarg(tool, tools[LD].outfile); @@ -322,7 +307,7 @@ static void build(char *file) { int tool = toolfor(file), nexttool; - struct objects *objs = (tool == LD || cflag || kflag) ? + struct items *objs = (tool == LD || cflag || kflag) ? &objout : &objtmp; for (; tool < LAST_TOOL; tool = nexttool) { @@ -364,7 +349,7 @@ build(char *file) validatetools(); - objs->f = newitem(objs->f, objs->n++, outfilename(file, "o")); + objs->s = newitem(objs->s, objs->n++, outfilename(file, "o")); } static void diff --git a/inc/cc.h b/inc/cc.h @@ -1,4 +1,6 @@ /* See LICENSE file for copyright and license details. */ +#include <sys/types.h> + #ifndef NDEBUG extern int debug; #define DBG(fmt, ...) dbg(fmt, __VA_ARGS__) @@ -12,8 +14,14 @@ extern int debug; #define PREFIX "/usr/local/" #endif +struct items { + char **s; + int n; +}; + extern void die(const char *fmt, ...); extern void dbg(const char *fmt, ...); +extern char **newitem(char **array, int num, char *item); extern void *xmalloc(size_t size); extern void *xcalloc(size_t nmemb, size_t size); extern char *xstrdup(const char *s); diff --git a/lib/Makefile b/lib/Makefile @@ -2,7 +2,7 @@ .POSIX: include ../config.mk -OBJS = die.o xcalloc.o xmalloc.o xrealloc.o xstrdup.o debug.o +OBJS = debug.o die.o newitem.o xcalloc.o xmalloc.o xrealloc.o xstrdup.o all: libcc.a diff --git a/lib/newitem.c b/lib/newitem.c @@ -0,0 +1,12 @@ +#include "../inc/cc.h" + +char ** +newitem(char **array, int num, char *item) +{ + char **ar = xrealloc(array, (num + 1) * sizeof(char **)); + + ar[num] = item; + + return ar; +} +