summary refs log tree commit diff
path: root/lisc
diff options
context:
space:
mode:
authorQuentin Carbonneaux <quentin.carbonneaux@yale.edu>2016-02-28 18:10:16 -0500
committerQuentin Carbonneaux <quentin.carbonneaux@yale.edu>2016-02-28 18:12:43 -0500
commit09c45b936d8977ed0896905ad2fb9e5d1e1e9b65 (patch)
tree27760d18e3a9123751452bb5f44e7e06a2c93c04 /lisc
parent7185c41110ab8bc735f22c80092afbc6e62e5023 (diff)
downloadroux-09c45b936d8977ed0896905ad2fb9e5d1e1e9b65.tar.gz
support -o option
Diffstat (limited to 'lisc')
-rw-r--r--lisc/main.c73
1 files changed, 38 insertions, 35 deletions
diff --git a/lisc/main.c b/lisc/main.c
index 9595fe1..a0959ef 100644
--- a/lisc/main.c
+++ b/lisc/main.c
@@ -1,5 +1,6 @@
 #include "lisc.h"
 #include <ctype.h>
+#include <getopt.h>
 
 char debug['Z'+1] = {
 	['P'] = 0, /* parsing */
@@ -13,6 +14,7 @@ char debug['Z'+1] = {
 	['R'] = 0, /* reg. allocation */
 };
 
+static FILE *outf;
 static int dbg;
 
 static void
@@ -24,7 +26,7 @@ data(Dat *d)
 		puts("/* end data */\n");
 		freeall();
 	}
-	emitdat(d, stdout);
+	emitdat(d, outf);
 }
 
 static void
@@ -60,8 +62,8 @@ func(Fn *fn)
 		} else
 			fn->rpo[n]->link = fn->rpo[n+1];
 	if (!dbg) {
-		emitfn(fn, stdout);
-		printf("/* end function %s */\n\n", fn->name);
+		emitfn(fn, outf);
+		fprintf(outf, "/* end function %s */\n\n", fn->name);
 	} else
 		fprintf(stderr, "\n");
 	freeall();
@@ -70,44 +72,45 @@ func(Fn *fn)
 int
 main(int ac, char *av[])
 {
-	char *o, *f;
 	FILE *inf;
+	char *f;
+	int c;
 
-	if (ac > 1 && strncmp(av[1], "-d", 2) == 0) {
-		if (av[1][2] == 0) {
-			if (ac <= 2)
-				goto Usage;
-			o = av[2];
-			f = av[3];
-		} else {
-			o = &av[1][2];
-			f = av[2];
+	outf = stdout;
+	while ((c = getopt(ac, av, "d:o:")) != -1)
+		switch (c) {
+		case 'd':
+			for (; *optarg; optarg++)
+				if (isalpha(*optarg)) {
+					debug[toupper(*optarg)] = 1;
+					dbg = 1;
+				}
+			break;
+		case 'o':
+			if (strcmp(optarg, "-") != 0)
+				outf = fopen(optarg, "w");
+			break;
+		default:
+			fprintf(stderr, "usage: %s [-d <flags>] [-o out] {file.ssa, -}\n", av[0]);
+			exit(1);
 		}
-		for (; *o; o++)
-			if (isalpha(*o)) {
-				debug[toupper(*o)] = 1;
-				dbg = 1;
-			}
-	} else
-		f = av[1];
 
-	if (!f || strcmp(f, "-") == 0)
-		inf = stdin;
-	else {
-		inf = fopen(f, "r");
-		if (!inf) {
-			fprintf(stderr, "cannot open '%s'\n", f);
-			goto Usage;
+	do {
+		f = av[optind];
+		if (!f || strcmp(f, "-") == 0)
+			inf = stdin;
+		else {
+			inf = fopen(f, "r");
+			if (!inf) {
+				fprintf(stderr, "cannot open '%s'\n", f);
+				exit(1);
+			}
 		}
-	}
+		parse(inf, data, func);
+	} while (++optind < ac);
 
-	parse(inf, data, func);
 	if (!dbg)
-		emitfin(stdout);
-	fclose(inf);
-	exit(0);
+		emitfin(outf);
 
-Usage:
-	fprintf(stderr, "usage: %s [-d <flags>] {file.ssa, -}\n", av[0]);
-	exit(1);
+	exit(0);
 }