forked from wryun/es-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt.c
93 lines (83 loc) · 1.73 KB
/
opt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* opt.c -- option parsing ($Revision: 1.1.1.1 $) */
#include "es.h"
static const char *usage, *invoker;
static List *args;
static Term *termarg;
static int nextchar;
extern void esoptbegin(List *list, const char *caller, const char *usagemsg) {
static Boolean initialized = FALSE;
if (!initialized) {
initialized = TRUE;
globalroot(&usage);
globalroot(&invoker);
globalroot(&args);
globalroot(&termarg);
}
assert(usage == NULL);
usage = usagemsg;
invoker = caller;
args = list;
termarg = NULL;
nextchar = 0;
}
extern int esopt(const char *options) {
int c;
const char *arg, *opt;
assert(usage != NULL);
assert(termarg == NULL);
if (nextchar == 0) {
if (args == NULL)
return EOF;
assert(args->term != NULL);
arg = getstr(args->term);
if (*arg != '-')
return EOF;
if (arg[1] == '-' && arg[2] == '\0') {
args = args->next;
return EOF;
}
nextchar = 1;
} else {
assert(args != NULL && args->term != NULL);
arg = getstr(args->term);
}
c = arg[nextchar++];
opt = strchr(options, c);
if (opt == NULL) {
const char *msg = usage;
usage = NULL;
args = NULL;
nextchar = 0;
fail(invoker, "illegal option: -%c -- usage: %s", c, msg);
}
if (arg[nextchar] == '\0') {
nextchar = 0;
args = args->next;
}
if (opt[1] == ':') {
if (args == NULL) {
const char *msg = usage;
fail(invoker,
"option -%c expects an argument -- usage: %s",
c, msg);
}
termarg = (nextchar == 0)
? args->term
: mkstr(gcdup(arg + nextchar));
nextchar = 0;
args = args->next;
}
return c;
}
extern Term *esoptarg(void) {
Term *t = termarg;
assert(t != NULL);
termarg = NULL;
return t;
}
extern List *esoptend(void) {
List *result = args;
args = NULL;
usage = NULL;
return result;
}