[hackers] [scc] [driver] simplify tools handling readability || Quentin Rameau

From: <git_AT_suckless.org>
Date: Mon, 30 May 2016 18:37:30 +0200 (CEST)

commit 471425feb4a5703deb60456e359b216c0491e301
Author: Quentin Rameau <quinq_AT_fifth.space>
AuthorDate: Mon May 30 18:34:06 2016 +0200
Commit: Quentin Rameau <quinq_AT_fifth.space>
CommitDate: Mon May 30 18:37:09 2016 +0200

    [driver] simplify tools handling readability
    
    Add local pointers to the current tool in settool() and spawn().
    Add a macro for adding parameters to a tool.

diff --git a/driver/posix/scc.c b/driver/posix/scc.c
index eec985d..0ddae9d 100644
--- a/driver/posix/scc.c
+++ b/driver/posix/scc.c
_AT_@ -14,6 +14,7 @@
 #include "../../inc/arg.h"
 #include "../../inc/cc.h"
 
+#define ADDARG(t, p) ((tools[t].args[++tools[t].nargs]) = (p))
 #define NARGS 64
 
 enum {
_AT_@ -23,7 +24,7 @@ enum {
         NR_TOOLS,
 };
 
-static struct {
+static struct tool {
         char cmd[PATH_MAX];
         char *args[NARGS];
         int nargs;
_AT_@ -53,64 +54,67 @@ terminate(void)
 }
 
 int
-settool(int tool, int pipeout)
+settool(int t, int pipeout)
 {
+ struct tool *tool = &tools[t];
         int fds[2], n;
         static int fdin;
 
- switch (tool) {
+ switch (t) {
         case CC1:
         case CC2:
- n = snprintf(tools[tool].bin, sizeof(tools[tool].bin),
- arch ? "%s-%s" : "%s", tools[tool].name, arch);
- if (n < 0 || n >= sizeof(tools[tool].bin))
+ n = snprintf(tool->bin, sizeof(tool->bin),
+ arch ? "%s-%s" : "%s", tool->name, arch);
+ if (n < 0 || n >= sizeof(tool->bin))
                         die("scc: target tool name too long");
 
- n = snprintf(tools[tool].cmd, sizeof(tools[tool].cmd),
- "%s/libexec/scc/%s", PREFIX, tools[tool].bin);
- if (n < 0 || n >= sizeof(tools[tool].cmd))
+ n = snprintf(tool->cmd, sizeof(tool->cmd),
+ "%s/libexec/scc/%s", PREFIX, tool->bin);
+ if (n < 0 || n >= sizeof(tool->cmd))
                         die("scc: target tool path too long");
                 break;
         default:
                 break;
         }
 
- tools[tool].args[0] = tools[tool].bin;
+ tool->args[0] = tool->bin;
 
         if (fdin) {
- tools[tool].in = fdin;
+ tool->in = fdin;
                 fdin = 0;
         }
         if (pipeout) {
                 if (pipe(fds))
                         die("scc: pipe: %s", strerror(errno));
- tools[tool].out = fds[1];
+ tool->out = fds[1];
                 fdin = fds[0];
         }
 
- return tool;
+ return t;
 }
 
 void
-spawn(int tool)
+spawn(int t)
 {
- switch (tools[tool].pid = fork()) {
+ struct tool *tool = &tools[t];
+
+ switch (tool->pid = fork()) {
         case -1:
- die("scc: %s: %s", tools[tool].name, strerror(errno));
+ die("scc: %s: %s", tool->name, strerror(errno));
         case 0:
- if (tools[tool].out)
- dup2(tools[tool].out, 1);
- if (tools[tool].in)
- dup2(tools[tool].in, 0);
- execvp(tools[tool].cmd, tools[tool].args);
+ if (tool->out)
+ dup2(tool->out, 1);
+ if (tool->in)
+ dup2(tool->in, 0);
+ execvp(tool->cmd, tool->args);
                 fprintf(stderr, "scc: execv %s: %s\n",
- tools[tool].cmd, strerror(errno));
+ tool->cmd, strerror(errno));
                 _exit(1);
         default:
- if (tools[tool].in)
- close(tools[tool].in);
- if (tools[tool].out)
- close(tools[tool].out);
+ if (tool->in)
+ close(tool->in);
+ if (tool->out)
+ close(tool->out);
                 break;
         }
 }
_AT_@ -134,7 +138,7 @@ main(int argc, char *argv[])
         ARGBEGIN {
         case 'E':
                 Eflag = 1;
- tools[CC1].args[++tools[CC1].nargs] = "-E";
+ ADDARG(CC1, "-E");
                 break;
         case 'm':
                 arch = EARGF(usage());
_AT_@ -149,7 +153,7 @@ main(int argc, char *argv[])
         if (!argc)
                 die("scc: fatal error: no input files");
 
- tools[CC1].args[++tools[CC1].nargs] = *argv;
+ ADDARG(CC1, *argv);
 
         if (Eflag) {
                 spawn(settool(CC1, 0));
Received on Mon May 30 2016 - 18:37:30 CEST

This archive was generated by hypermail 2.3.0 : Mon May 30 2016 - 18:48:12 CEST