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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include <u.h>
#include <libc.h>
// Flag hash.
typedef struct Flag Flag;
struct Flag
{
char *name;
int namelen;
char *desc;
int iscount;
void (*set)(char*, void*);
void (*set2)(char*, char*, void*);
void *arg;
Flag *next;
Flag *allnext;
};
static Flag *curflag;
static Flag *fhash[512];
static Flag *first, *last;
char *argv0;
/*
* Mac OS can't deal with files that only declare data.
* ARGBEGIN mentions this function so that this file gets pulled in.
*/
void __fixargv0(void) { }
// FNV-1 hash. http://isthe.com/chongo/tech/comp/fnv/
static uint32
fnv(char *p, int n)
{
uint32 h;
h = 2166136261U;
while(n-- > 0)
h = (h*16777619) ^ (uchar)*p++;
return h;
}
static Flag*
lookflag(char *name, int namelen, int creat)
{
uint32 h;
Flag *f;
h = fnv(name, namelen) & (nelem(fhash)-1);
for(f=fhash[h]; f; f=f->next) {
if(f->namelen == namelen && memcmp(f->name, name, (size_t)namelen) == 0) {
if(creat)
sysfatal("multiple definitions of flag -%s", name);
return f;
}
}
if(!creat)
return nil;
f = malloc(sizeof *f);
if(f == nil)
sysfatal("out of memory");
memset(f, 0, sizeof *f);
f->name = name;
f->namelen = namelen;
f->next = fhash[h];
if(first == nil)
first = f;
else
last->allnext = f;
last = f;
fhash[h] = f;
return f;
}
static void
count(char *arg, void *p)
{
int *ip;
ip = p;
if(arg != nil)
*ip = atoi(arg);
else
(*ip)++;
}
void
flagcount(char *name, char *desc, int *p)
{
Flag *f;
f = lookflag(name, (int)strlen(name), 1);
f->desc = desc;
f->iscount = 1;
f->set = count;
f->arg = p;
}
static void
atollwhex(char *s, void *p)
{
char *t;
*(int64*)p = strtoll(s, &t, 0);
if(*s == '\0' || *t != '\0')
sysfatal("invalid numeric argument -%s=%s", curflag->name, s);
}
void
flagint64(char *name, char *desc, int64 *p)
{
Flag *f;
f = lookflag(name, (int)strlen(name), 1);
f->desc = desc;
f->set = atollwhex;
f->arg = p;
}
static void
atolwhex(char *s, void *p)
{
char *t;
*(int32*)p = (int32)strtol(s, &t, 0);
if(*s == '\0' || *t != '\0')
sysfatal("invalid numeric argument -%s=%s", curflag->name, s);
}
void
flagint32(char *name, char *desc, int32 *p)
{
Flag *f;
f = lookflag(name, (int)strlen(name), 1);
f->desc = desc;
f->set = atolwhex;
f->arg = p;
}
static void
string(char *s, void *p)
{
*(char**)p = s;
}
void
flagstr(char *name, char *desc, char **p)
{
Flag *f;
f = lookflag(name, (int)strlen(name), 1);
f->desc = desc;
f->set = string;
f->arg = p;
}
static void
fn0(char *s, void *p)
{
USED(s);
((void(*)(void))p)();
}
void
flagfn0(char *name, char *desc, void (*fn)(void))
{
Flag *f;
f = lookflag(name, (int)strlen(name), 1);
f->desc = desc;
f->set = fn0;
f->arg = fn;
f->iscount = 1;
}
static void
fn1(char *s, void *p)
{
((void(*)(char*))p)(s);
}
void
flagfn1(char *name, char *desc, void (*fn)(char*))
{
Flag *f;
f = lookflag(name, (int)strlen(name), 1);
f->desc = desc;
f->set = fn1;
f->arg = fn;
}
static void
fn2(char *s, char *t, void *p)
{
((void(*)(char*, char*))p)(s, t);
}
void
flagfn2(char *name, char *desc, void (*fn)(char*, char*))
{
Flag *f;
f = lookflag(name, (int)strlen(name), 1);
f->desc = desc;
f->set2 = fn2;
f->arg = fn;
}
void
flagparse(int *argcp, char ***argvp, void (*usage)(void))
{
int argc;
char **argv, *p, *q;
char *name;
int namelen;
Flag *f;
argc = *argcp;
argv = *argvp;
argv0 = argv[0];
argc--;
argv++;
while(argc > 0) {
p = *argv;
// stop before non-flag or -
if(*p != '-' || p[1] == '\0')
break;
argc--;
argv++;
// stop after --
if(p[1] == '-' && p[2] == '\0') {
break;
}
// turn --foo into -foo
if(p[1] == '-' && p[2] != '-')
p++;
// allow -flag=arg if present
name = p+1;
q = strchr(name, '=');
if(q != nil)
namelen = (int)(q++ - name);
else
namelen = (int)strlen(name);
f = lookflag(name, namelen, 0);
if(f == nil) {
if(strcmp(p, "-h") == 0 || strcmp(p, "-help") == 0 || strcmp(p, "-?") == 0)
usage();
sysfatal("unknown flag %s", p);
}
curflag = f;
// otherwise consume next argument if non-boolean
if(!f->iscount && q == nil) {
if(argc-- == 0)
sysfatal("missing argument to flag %s", p);
q = *argv++;
}
// and another if we need two
if(f->set2 != nil) {
if(argc-- == 0)
sysfatal("missing second argument to flag %s", p);
f->set2(q, *argv++, f->arg);
continue;
}
f->set(q, f->arg);
}
*argcp = argc;
*argvp = argv;
}
void
flagprint(int fd)
{
Flag *f;
char *p, *q;
for(f=first; f; f=f->allnext) {
p = f->desc;
if(p == nil || *p == '\0') // undocumented flag
continue;
q = strstr(p, ": ");
if(q)
fprint(fd, " -%s %.*s\n \t%s\n", f->name, utfnlen(p, q-p), p, q+2);
else if(f->namelen > 1)
fprint(fd, " -%s\n \t%s\n", f->name, p);
else
fprint(fd, " -%s\t%s\n", f->name, p);
}
}
|