[hackers] [scc] [driver] Add arg.h to and refactor the scc driver || FRIGN

From: <git_AT_suckless.org>
Date: Wed, 25 May 2016 15:35:06 +0200 (CEST)

commit f7e089f6ba9d2747d8b6f01e00c2a1d4b4e287e8
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Wed May 25 15:33:56 2016 +0200
Commit: FRIGN <dev_AT_frign.de>
CommitDate: Wed May 25 15:33:56 2016 +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.

diff --git a/arg.h b/arg.h
new file mode 100644
index 0000000..0b23c53
--- /dev/null
+++ b/arg.h
_AT_@ -0,0 +1,65 @@
+/*
+ * Copy me if you can.
+ * by 20h
+ */
+
+#ifndef ARG_H__
+#define ARG_H__
+
+extern char *argv0;
+
+/* use main(int argc, char *argv[]) */
+#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
+ argv[0] && argv[0][0] == '-'\
+ && argv[0][1];\
+ argc--, argv++) {\
+ char argc_;\
+ char **argv_;\
+ int brk_;\
+ if (argv[0][1] == '-' && argv[0][2] == '\0') {\
+ argv++;\
+ argc--;\
+ break;\
+ }\
+ for (brk_ = 0, argv[0]++, argv_ = argv;\
+ argv[0][0] && !brk_;\
+ argv[0]++) {\
+ if (argv_ != argv)\
+ break;\
+ argc_ = argv[0][0];\
+ switch (argc_)
+
+/* Handles obsolete -NUM syntax */
+#define ARGNUM case '0':\
+ case '1':\
+ case '2':\
+ case '3':\
+ case '4':\
+ case '5':\
+ case '6':\
+ case '7':\
+ case '8':\
+ case '9'
+
+#define ARGEND }\
+ }
+
+#define ARGC() argc_
+
+#define ARGNUMF() (brk_ = 1, estrtonum(argv[0], 0, INT_MAX))
+
+#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\
+ ((x), abort(), (char *)0) :\
+ (brk_ = 1, (argv[0][1] != '\0')?\
+ (&argv[0][1]) :\
+ (argc--, argv++, argv[0])))
+
+#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\
+ (char *)0 :\
+ (brk_ = 1, (argv[0][1] != '\0')?\
+ (&argv[0][1]) :\
+ (argc--, argv++, argv[0])))
+
+#define LNGARG() &argv[0][0]
+
+#endif
diff --git a/cc1/arg.h b/cc1/arg.h
deleted file mode 100644
index 0b23c53..0000000
--- a/cc1/arg.h
+++ /dev/null
_AT_@ -1,65 +0,0 @@
-/*
- * Copy me if you can.
- * by 20h
- */
-
-#ifndef ARG_H__
-#define ARG_H__
-
-extern char *argv0;
-
-/* use main(int argc, char *argv[]) */
-#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
- argv[0] && argv[0][0] == '-'\
- && argv[0][1];\
- argc--, argv++) {\
- char argc_;\
- char **argv_;\
- int brk_;\
- if (argv[0][1] == '-' && argv[0][2] == '\0') {\
- argv++;\
- argc--;\
- break;\
- }\
- for (brk_ = 0, argv[0]++, argv_ = argv;\
- argv[0][0] && !brk_;\
- argv[0]++) {\
- if (argv_ != argv)\
- break;\
- argc_ = argv[0][0];\
- switch (argc_)
-
-/* Handles obsolete -NUM syntax */
-#define ARGNUM case '0':\
- case '1':\
- case '2':\
- case '3':\
- case '4':\
- case '5':\
- case '6':\
- case '7':\
- case '8':\
- case '9'
-
-#define ARGEND }\
- }
-
-#define ARGC() argc_
-
-#define ARGNUMF() (brk_ = 1, estrtonum(argv[0], 0, INT_MAX))
-
-#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\
- ((x), abort(), (char *)0) :\
- (brk_ = 1, (argv[0][1] != '\0')?\
- (&argv[0][1]) :\
- (argc--, argv++, argv[0])))
-
-#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\
- (char *)0 :\
- (brk_ = 1, (argv[0][1] != '\0')?\
- (&argv[0][1]) :\
- (argc--, argv++, argv[0])))
-
-#define LNGARG() &argv[0][0]
-
-#endif
diff --git a/cc1/main.c b/cc1/main.c
index 86ee922..116d77f 100644
--- a/cc1/main.c
+++ b/cc1/main.c
_AT_@ -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
index d17adbf..316f6a4 100644
--- a/driver/posix/Makefile
+++ b/driver/posix/Makefile
_AT_@ -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 $_AT_
+
 clean:
+ rm -f $(OBJS)
         rm -f scc
diff --git a/driver/posix/scc.c b/driver/posix/scc.c
index 6fc22f1..ca7f97d 100644
--- a/driver/posix/scc.c
+++ b/driver/posix/scc.c
_AT_@ -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];
_AT_@ -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;
_AT_@ -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;
_AT_@ -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
_AT_@ -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;
Received on Wed May 25 2016 - 15:35:06 CEST

This archive was generated by hypermail 2.3.0 : Wed May 25 2016 - 15:36:14 CEST