scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

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:
arg.h | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc1/arg.h | 65-----------------------------------------------------------------
cc1/main.c | 2+-
driver/posix/Makefile | 11+++++++++++
driver/posix/scc.c | 63+++++++++++++++++++++++++++------------------------------------
5 files changed, 104 insertions(+), 102 deletions(-)

diff --git a/arg.h b/arg.h @@ -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 @@ -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 @@ -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;