summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2010-03-08 17:27:42 +0000
committerDimitri Sokolyuk <demon@dim13.org>2010-03-08 17:27:42 +0000
commitdb76fe7805e0e3308eb217d1e3ea26dfd7a53e68 (patch)
treef09b9a6eee7537a08c72eb69352f2bbb2a8f1274
parentf65045fa62121f556dc90a51453350a9a78d0d89 (diff)
add getopt, usage
-rw-r--r--bf.c42
1 files changed, 38 insertions, 4 deletions
diff --git a/bf.c b/bf.c
index 6689c19..7f7e030 100644
--- a/bf.c
+++ b/bf.c
@@ -16,6 +16,7 @@
*/
#include <assert.h>
+#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -81,7 +82,8 @@ readprog(char *fname)
int ch;
fd = fopen(fname, "r");
- assert(fd);
+ if (!fd)
+ return NULL;
fseek(fd, 0L, SEEK_END);
len = ftell(fd);
@@ -100,17 +102,47 @@ readprog(char *fname)
return prog;
}
+void
+usage(void)
+{
+ extern char *__progname;
+
+ fprintf(stderr, "usage: %s [-d] <prog>\n", __progname);
+
+ exit(1);
+}
+
int
main(int argc, char **argv)
{
Cell *data;
char *prog, *p;
+ int dumpflag = 0;
int loop;
+ int c;
- if (argc != 2)
+ while ((c = getopt(argc, argv, "dh")) != -1)
+ switch (c) {
+ case 'd':
+ dumpflag = 1;
+ break;
+ case 'h':
+ case '?':
+ default:
+ usage();
+ /* NOTREACHED */
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (!argc)
return -1;
- prog = readprog(argv[1]);
+ prog = readprog(*argv);
+ if (!prog)
+ errx(1, "not found: %s", *argv);
+
data = alloccell();
for (p = prog; *p; p++)
@@ -164,7 +196,9 @@ main(int argc, char **argv)
break;
}
- dumpcells(data);
+ if (dumpflag)
+ dumpcells(data);
+
freecells(data);
free(prog);