commit f7e089f6ba9d2747d8b6f01e00c2a1d4b4e287e8
parent 8dea94d188b359ce62593231012246b7ab340662
Author: FRIGN <dev@frign.de>
Date: Wed, 25 May 2016 15:33:56 +0200
[driver] Add arg.h to and refactor the scc driver
This requires moving arg.h from /cc1 to /.
Also, replace all perror() calls to common functions using strerror().
Given we use die() in the driver it also required an update to the
driver Makefile.
Diffstat:
4 files changed, 39 insertions(+), 37 deletions(-)
diff --git a/cc1/arg.h b/arg.h
diff --git a/cc1/main.c b/cc1/main.c
@@ -5,9 +5,9 @@
#include <string.h>
#include <errno.h>
+#include "../arg.h"
#include "../inc/cc.h"
#include "arch.h"
-#include "arg.h"
#include "cc1.h"
char *argv0;
diff --git a/driver/posix/Makefile b/driver/posix/Makefile
@@ -3,7 +3,18 @@
include ../../config.mk
+OBJS = scc.o
+
all: scc
+$(OBJS): ../../inc/cc.h
+
+../../lib/libcc.a:
+ cd ../lib && $(MAKE) -e -$(MAKEFLAGS)
+
+scc: $(OBJS) ../../lib/libcc.a
+ $(CC) $(SCC_LDFLAGS) $(OBJS) ../../lib/libcc.a -o $@
+
clean:
+ rm -f $(OBJS)
rm -f scc
diff --git a/driver/posix/scc.c b/driver/posix/scc.c
@@ -4,14 +4,18 @@
#include <sys/wait.h>
#include <unistd.h>
+#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include "../../arg.h"
#include "../../inc/cc.h"
+char *argv0;
+
#define NARGS 64
static char cmd[FILENAME_MAX];
static char *argcc1[NARGS];
@@ -38,18 +42,17 @@ cc1(int fd)
switch (pid = fork()) {
case -1:
- perror("scc:cc1");
+ die("scc: cc1: %s", strerror(errno));
exit(1);
case 0:
dup2(fd, 1);
fmt = (arch) ? "%s/libexec/scc/cc1-%s" : "%s/libexec/scc/cc1";
r = snprintf(cmd, sizeof(cmd), fmt, PREFIX, arch);
if (r == sizeof(cmd)) {
- fputs("scc:incorrect prefix\n", stderr);
- exit(1);
+ die("scc: incorrect prefix\n");
}
execv(cmd, argcc1);
- perror("scc:execv cc1");
+ die("scc: execv cc1: %s", strerror(errno));
abort();
default:
pid_cc1 = pid;
@@ -67,18 +70,16 @@ cc2(int fd)
switch (pid = fork()) {
case -1:
- perror("scc:cc2");
- exit(1);
+ die("scc: cc2: %s", strerror(errno));
case 0:
dup2(fd, 0);
fmt = (arch) ? "%s/libexec/scc/cc2-%s" : "%s/libexec/scc/cc2";
r = snprintf(cmd, sizeof(cmd), fmt, PREFIX, arch);
if (r == sizeof(cmd)) {
- fputs("scc:incorrect prefix\n", stderr);
- exit(1);
+ die("scc: incorrect prefix");
}
execv(cmd, argcc2);
- perror("scc:execv cc2");
+ fprintf(stderr, "scc: execv cc2: %s\n", strerror(errno));
abort();
default:
pid_cc2 = pid;
@@ -90,8 +91,7 @@ cc2(int fd)
static void
usage(void)
{
- fputs("scc [-m arch] file.c\n", stderr);
- exit(1);
+ die("usage: %s [-m arch] input ...");
}
int
@@ -102,35 +102,26 @@ main(int argc, char *argv[])
pid_t pid;
atexit(terminate);
+
if (p = getenv("ARCH"))
arch = p;
- for (--argc; *++argv; --argc) {
- if (argv[0][0] != '-' || argv[0][1] == '-')
- break;
- for (p = &argv[0][1]; *p; ++p) {
- switch (*p) {
- case 'm':
- if ((arch = *++argv) == NULL)
- goto usage;
- --argc;
- break;
- default:
- usage:
- usage();
- break;
- }
- }
- }
+ ARGBEGIN {
+ case 'm':
+ arch = EARGF(usage());
+ break;
+ case '-':
+ printf("scc: ignored parameter --%s\n", EARGF(usage()));
+ break;
+ default:
+ usage();
+ } ARGEND
- if (argc == 0) {
- fputs("scc: fatal error: no input files\n", stderr);
- exit(1);
- }
- if (pipe(fds)) {
- perror("scc: pipe");
- exit(1);
- }
+ if (!argc)
+ die("scc: fatal error: no input files");
+
+ if (pipe(fds))
+ die("scc: pipe: %s", strerror(errno));
argcc1[0] = "cc1";
argcc1[1] = *argv;