shithub: scc

Download patch

ref: 5e7b55dcc252f28c5c21b347dadaa8209fd061bb
parent: cd19be49d9666dc294084fbe4dcb5ff613aa2d56
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Apr 21 09:44:28 EDT 2015

Add option parser for cc1

This was something should be done long time ago, but
I was too lazy. At this moment we only support -w to
enable warnings, and -o to indicate what is the file
where the intermediate code must be written.

--- a/cc1/main.c
+++ b/cc1/main.c
@@ -1,6 +1,9 @@
 
 #include <inttypes.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 
 #include "../inc/cc.h"
 #include "cc1.h"
@@ -10,14 +13,69 @@
 
 uint8_t npromote, warnings;
 
+static uint8_t noerror;
+static char *output;
+
+static void
+clean(void)
+{
+	if (noerror)
+		return;
+	if (output)
+		remove(output);
+}
+
+static void
+usage(void)
+{
+	fputs("usage: cc1 [-w] [-o output] [input]\n", stderr);
+	exit(1);
+}
+
 int
 main(int argc, char *argv[])
 {
+	char c, *input, *cp;
+
+	atexit(clean);
+repeat:
+	--argc, ++argv;
+	if (*argv && argv[0][0] == '-' && argv[0][1] != '-') {
+		for (cp = &argv[0][1]; (c = *cp); cp++) {
+			switch (c) {
+			case 'w':
+				warnings = 1;
+				break;
+			case 'o':
+				if (!*++argv || argv[0][0] == '-')
+					usage();
+				--argc;
+				output = *argv;
+				break;
+			default:
+				usage();
+			}
+		}
+		goto repeat;
+	}
+
+	if (output) {
+		if (!freopen(output, "w", stdout))
+			die("error opening output:%s", strerror(errno));
+	}
+	input = *argv;
+	if (argc > 1)
+		usage();
 	init_keywords();
 	init_expr();
-	open_file(NULL);
+	open_file(input);
 	for (next(); yytoken != EOFTOK; extdecl());
 		/* nothing */;
 
+	if (ferror(stdin) || ferror(stdout)) {
+		die("error reading/writing from input/output:%s",
+		    strerror(errno));
+	}
+	noerror = 1;
 	return 0;
 }