scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit f5319b858eb3e6f18d2983745c90d9b024085b99
parent c64e4d4f07ef51d170230a44b1661b249475e162
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 21 Jun 2016 08:17:37 +0200

Merge remote-tracking branch 'quinq/master'

Diffstat:
cc1/main.c | 15++++++---------
driver/posix/scc.c | 37++++++++++++++++++++-----------------
inc/cc.h | 4++--
lib/newitem.c | 12++++++------
4 files changed, 34 insertions(+), 34 deletions(-)

diff --git a/cc1/main.c b/cc1/main.c @@ -15,7 +15,7 @@ char *argv0; int warnings; jmp_buf recover; -static char *base, *output; +static char *name, *output; static struct items uflags; int onlycpp; @@ -31,7 +31,7 @@ clean(void) static void usage(void) { - die(!strcmp(base, "cpp") ? + die(!strcmp(name, "cpp") ? "usage: cpp [-wd] [-D def[=val]]... [-U def]... [-I dir]... " "[input]" : "usage: cc1 [-Ewd] [-D def[=val]]... [-U def]... [-I dir]... " @@ -41,17 +41,14 @@ usage(void) int main(int argc, char *argv[]) { - char *base; + char *cp; int i; atexit(clean); icpp(); /* if run as cpp, only run the preprocessor */ - if ((base = strrchr(argv0, '/'))) - ++base; - else - base = argv0; + name = (cp = strrchr(*argv, '/')) ? cp + 1 : *argv; ARGBEGIN { case 'D': @@ -64,7 +61,7 @@ main(int argc, char *argv[]) incdir(EARGF(usage())); break; case 'U': - uflags.s = newitem(uflags.s, uflags.n++, EARGF(usage())); + newitem(&uflags, EARGF(usage())); break; case 'd': DBGON(); @@ -85,7 +82,7 @@ main(int argc, char *argv[]) if (output && !freopen(output, "w", stdout)) die("error opening output: %s", strerror(errno)); - if (!strcmp(base, "cpp")) + if (!strcmp(name, "cpp")) onlycpp = 1; for (i = 0; i < uflags.n; ++i) diff --git a/driver/posix/scc.c b/driver/posix/scc.c @@ -29,10 +29,11 @@ enum { static struct tool { char cmd[PATH_MAX]; - char **args; char bin[16]; char *outfile; - int nparams, nargs, in, out, init; + struct items args; + unsigned nparams; + int in, out, init; pid_t pid; } tools[] = { [CC1] = { .bin = "cc1", .cmd = PREFIX "/libexec/scc/", }, @@ -56,7 +57,7 @@ extern int failure; static void terminate(void) { - int i; + unsigned i; if (!kflag) { for (i = 0; i < objtmp.n; ++i) @@ -69,10 +70,10 @@ addarg(int tool, char *arg) { struct tool *t = &tools[tool]; - if (t->nargs < 1) - t->nargs = 1; + if (t->args.n < 1) + t->args.n = 1; - t->args = newitem(t->args, t->nargs++, arg); + newitem(&t->args, arg); } static void @@ -80,10 +81,10 @@ setargv0(int tool, char *arg) { struct tool *t = &tools[tool]; - if (t->nargs > 0) - t->args[0] = arg; + if (t->args.n > 0) + t->args.s[0] = arg; else - t->args = newitem(t->args, t->nargs++, arg); + newitem(&t->args, arg); } static int @@ -126,7 +127,7 @@ inittool(int tool) } setargv0(tool, t->bin); - t->nparams = t->nargs; + t->nparams = t->args.n; t->init = 1; return tool; @@ -165,7 +166,8 @@ static int settool(int tool, char *infile, int nexttool) { struct tool *t = &tools[tool]; - int i, fds[2]; + unsigned i; + int fds[2]; static int fdin = -1; switch (tool) { @@ -239,7 +241,7 @@ spawn(int tool) dup2(t->out, 1); if (t->in > -1) dup2(t->in, 0); - execvp(t->cmd, t->args); + execvp(t->cmd, t->args.s); fprintf(stderr, "scc: execvp %s: %s\n", t->cmd, strerror(errno)); _exit(1); @@ -277,7 +279,8 @@ static int validatetools(void) { struct tool *t; - int i, tool, st, failed = LAST_TOOL; + unsigned i; + int tool, st, failed = LAST_TOOL; for (tool = 0; tool < LAST_TOOL; ++tool) { t = &tools[tool]; @@ -289,9 +292,9 @@ validatetools(void) } if (tool >= failed && t->outfile) unlink(t->outfile); - for (i = t->nparams; i < t->nargs; ++i) - free(t->args[i]); - t->nargs = t->nparams; + for (i = t->nparams; i < t->args.n; ++i) + free(t->args.s[i]); + t->args.n = t->nparams; t->pid = 0; } } @@ -344,7 +347,7 @@ build(char *file) } if (validatetools()) - objs->s = newitem(objs->s, objs->n++, outfilename(file, "o")); + newitem(objs, outfilename(file, "o")); } static void diff --git a/inc/cc.h b/inc/cc.h @@ -16,12 +16,12 @@ extern int debug; struct items { char **s; - int n; + unsigned n; }; extern void die(const char *fmt, ...); extern void dbg(const char *fmt, ...); -extern char **newitem(char **array, int num, char *item); +extern void newitem(struct items *items, 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/newitem.c b/lib/newitem.c @@ -1,12 +1,12 @@ #include "../inc/cc.h" -char ** -newitem(char **array, int num, char *item) +void +newitem(struct items *items, char *item) { - char **ar = xrealloc(array, (num + 1) * sizeof(char **)); + if ((items->n + 1) < items->n) + die("newitem: overflow (%u + 1)", items->n); - ar[num] = item; - - return ar; + items->s = xrealloc(items->s, (items->n + 1) * sizeof(char **)); + items->s[items->n++] = item; }