shithub: scc

Download patch

ref: b85da7f9b81831a35f3d881fb9f048a4ac94eb6f
parent: 628bdafd3f9bc1cf19d595dc97e40bee68a54215
author: Quentin Rameau <[email protected]>
date: Wed Jun 1 14:39:23 EDT 2016

[driver] separate tool initialization

Set tool files paths in a separate function as this will only be needed
once.
Try to simplify the tool struct too.

--- a/driver/posix/scc.c
+++ b/driver/posix/scc.c
@@ -25,17 +25,15 @@
 };
 
 static struct tool {
-	char cmd[PATH_MAX];
+	char  cmd[PATH_MAX];
 	char *args[NARGS];
-	int nargs;
-	char bin[16];
-	char name[8];
-	int in, out;
+	char  bin[16];
+	int   nargs, in, out;
 	pid_t pid;
 } tools[NR_TOOLS] = {
-	[CC1] = { .name = "cc1", },
-	[CC2] = { .name = "cc2", },
-	[QBE] = { .name = "qbe", .bin = "qbe", .cmd = "qbe", },
+	[CC1] = { .bin = "cc1", .cmd = PREFIX "/libexec/scc/", },
+	[CC2] = { .bin = "cc2", .cmd = PREFIX "/libexec/scc/", },
+	[QBE] = { .bin = "qbe", .bin = "qbe", .cmd = "qbe", },
 };
 
 char *argv0;
@@ -54,6 +52,41 @@
 }
 
 int
+inittool(int tool)
+{
+	struct tool *t = &tools[tool];
+	size_t binln;
+	int n;
+
+	if (!t->args[0]) {
+		switch (tool) {
+		case CC1:
+		case CC2:
+			binln = strlen(t->bin);
+			if (arch) {
+				n = snprintf(t->bin + binln,
+					     sizeof(t->bin) - binln,
+					     "-%s", arch);
+				if (n < 0 || n >= sizeof(t->bin))
+					die("scc: target tool bin too long");
+				binln = strlen(t->bin);
+			}
+
+			if (strlen(t->cmd) + binln + 1 > sizeof(t->cmd))
+				die("scc: target tool path too long");
+			strcat(t->cmd, t->bin);
+			break;
+		default:
+			break;
+		}
+
+		t->args[0] = t->bin;
+	}
+
+	return tool;
+}
+
+int
 settool(int t, int pipeout)
 {
 	struct tool *tool = &tools[t];
@@ -60,25 +93,8 @@
 	int fds[2], n;
 	static int fdin;
 
-	switch (t) {
-	case CC1:
-	case CC2:
-		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");
+	inittool(t);
 
-		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;
-	}
-
-	tool->args[0] = tool->bin;
-
 	if (fdin) {
 		tool->in = fdin;
 		fdin = 0;
@@ -100,7 +116,7 @@
 
 	switch (tool->pid = fork()) {
 	case -1:
-		die("scc: %s: %s", tool->name, strerror(errno));
+		die("scc: %s: %s", tool->bin, strerror(errno));
 	case 0:
 		if (tool->out)
 			dup2(tool->out, 1);