commit d27f833a8a22815fd2b916a711bfa59f56d93b21
parent 3be6d85c774f18872bc2482221862c6211e82948
Author: Ed van Bruggen <edvb@uw.edu>
Date: Mon, 4 Feb 2019 23:47:45 -0800
Remove arg.h dependency in main
Diffstat:
extern/arg.h | | | 63 | --------------------------------------------------------------- |
main.c | | | 29 | +++++++++++------------------ |
2 files changed, 11 insertions(+), 81 deletions(-)
diff --git a/extern/arg.h b/extern/arg.h
@@ -1,63 +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 = basename(*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 -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, atoi(argv[0]))
-
-#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])))
-
-#endif
diff --git a/main.c b/main.c
@@ -5,7 +5,6 @@
#include <stdlib.h>
#include <string.h>
-#include "extern/arg.h"
#include "extern/linenoise.h"
#include "config.h"
@@ -16,8 +15,6 @@
# include "tibs/io.h"
#endif
-char *argv0;
-
void
die(int eval, const char *fmt, ...)
{
@@ -58,23 +55,19 @@ read_val(Env env, Str cmd)
return ret;
}
-static void
-usage(const int eval)
-{
- die(eval, "usage: %s [-hv] [FILENAME]", argv0);
-}
-
int
main(int argc, char *argv[])
{
- ARGBEGIN {
- case 'h':
- usage(0);
- case 'v':
- die(0, "%s v%s (c) 2017-2019 Ed van Bruggen", argv0, VERSION);
- default:
- usage(1);
- } ARGEND;
+ int i;
+
+ for (i = 1; i < argc; i++)
+ if (!strcmp(argv[i], "-v")) {
+ fprintf(stderr, "tisp v%s (c) 2017-2019 Ed van Bruggen\n", VERSION);
+ exit(0);
+ } else if (argv[i][0] == '-') {
+ fputs("usage: tisp [-hv] [FILE ...]\n", stderr);
+ exit(argv[i][1] == 'h' ? 0 : 1);
+ }
size_t nread;
char buf[BUFSIZ];
@@ -87,7 +80,7 @@ main(int argc, char *argv[])
tib_env_io(env);
#endif
- if (argc > 0) {
+ if (argc > 1) {
if (!(fp = fopen(*argv, "r")))
die(1, "%s: %s:", argv[0], *argv);
while ((nread = fread(buf, 1, sizeof(buf), fp)) > 0) ;