shithub: scc

Download patch

ref: 3791d53a684fb8df4bbd7df4f834c1dc07595627
parent: c466d3870558bbc5f37062e290c316c7095b0ed2
author: FRIGN <dev@frign.de>
date: Wed May 25 11:33:56 EDT 2016

[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.

--- /dev/null
+++ b/arg.h
@@ -1,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
--- a/cc1/arg.h
+++ /dev/null
@@ -1,65 +1,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
--- 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;
--- 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
--- a/driver/posix/scc.c
+++ b/driver/posix/scc.c
@@ -4,6 +4,7 @@
 #include <sys/wait.h>
 #include <unistd.h>
 
+#include <errno.h>
 #include <limits.h>
 #include <signal.h>
 #include <stdio.h>
@@ -10,8 +11,11 @@
 #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,7 +42,7 @@
 
 	switch (pid = fork()) {
 	case -1:
-		perror("scc:cc1");
+		die("scc: cc1: %s", strerror(errno));
 		exit(1);
 	case 0:
 		dup2(fd, 1);
@@ -45,11 +49,10 @@
 		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 @@
 
 	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 @@
 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 @@
 	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;