-
Notifications
You must be signed in to change notification settings - Fork 5
/
flag.c
295 lines (244 loc) · 5.63 KB
/
flag.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
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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "flag.h"
/*
* Singleton like a boss.
*/
static flagset_t *set = NULL;
/*
* Parsing error.
*/
#define ERROR_PARSING (self->error.flag = flag, FLAG_ERROR_PARSING)
/*
* Undefined flag error.
*/
#define ERROR_UNDEFINED (self->error.arg = arg, FLAG_ERROR_UNDEFINED_FLAG)
/*
* Require that nflags is within the bounds.
*/
#define CHECK_BOUNDS \
if (self->nflags >= FLAGS_MAX) { \
fprintf(stderr, "flag: exceeded max flags\n"); \
exit(1); \
}
/*
* Require a non-flag argument or return error.
*/
#define REQUIRE_ARG \
if (i == argc-1 || is_flag(args[i+1])) { \
self->error.flag = flag; \
return FLAG_ERROR_ARG_MISSING; \
}
/*
* Define flagset_<name> function for the given type,
* as well as the singleton flag_<name> variant.
*/
#define FLAG_TYPE(NAME, TYPE, CAP) \
void \
flagset_##NAME(flagset_t *self, TYPE *value, const char *name, const char *help) { \
CHECK_BOUNDS; \
self->flags[self->nflags++] = (flag_t) { \
.name = name, \
.help = help, \
.type = FLAG_TYPE_##CAP, \
.value = value \
}; \
} \
void \
flag_##NAME(TYPE *value, const char *name, const char *help) { \
if (set == NULL) set = flagset_new(); \
flagset_##NAME(set, value, name, help); \
}
/*
* Check for flag (--).
*/
static inline bool
is_flag(const char *s) {
return strlen(s) > 2 && s[0] == '-' && s[1] == '-';
}
/*
* Check for --no- prefix.
*/
static inline bool
is_negated(const char *s) {
return strncmp(s, "--no-", 5) == 0;
}
/*
* Largest flag name in the set.
*/
static int
largest_flag_name(flagset_t *self) {
int max = 0;
for (int i = 0; i < self->nflags; ++i) {
flag_t *flag = &self->flags[i];
size_t len = strlen(flag->name);
max = len > max ? len : max;
}
return max;
}
/*
* New flagset.
*/
flagset_t *
flagset_new() {
flagset_t *self = calloc(1, sizeof(flagset_t));
if (!self) return NULL;
self->usage = "[options] [arguments]";
return self;
}
/*
* Free the flagset.
*/
void
flagset_free(flagset_t *self) {
free(self);
}
/*
* Find flag matching `arg` --no-`arg`.
*/
static flag_t *
flagset_find(flagset_t *self, const char *arg) {
for (int i = 0; i < self->nflags; ++i) {
flag_t *flag = &self->flags[i];
if (strcmp(arg+2, flag->name) == 0) {
return flag;
}
if (is_negated(arg) && strcmp(arg+5, flag->name) == 0) {
return flag;
}
}
return NULL;
}
/*
* Parse and return error for delegation.
*/
flag_error
flagset_parse(flagset_t *self, int argc, const char **args) {
for (int i = 0; i < argc; ++i) {
const char *arg = args[i];
// Regular argument
if (!is_flag(arg)) {
if (self->argc >= FLAGS_MAX_ARGS) {
fprintf(stderr, "flag: exceeded max args\n");
exit(1);
}
self->argv[self->argc++] = arg;
continue;
}
// Flag lookup
flag_t *flag = flagset_find(self, arg);
if (flag == NULL) return ERROR_UNDEFINED;
// Error on non-bool negation
if (flag->type != FLAG_TYPE_BOOL && is_negated(arg)) {
return ERROR_UNDEFINED;
}
// Parse value
switch (flag->type) {
case FLAG_TYPE_INT: {
REQUIRE_ARG;
int rc = sscanf(args[++i], "%d", (int *) flag->value);
if (rc == 0) return ERROR_PARSING;
break;
}
case FLAG_TYPE_STRING:
REQUIRE_ARG;
*(const char **) flag->value = args[++i];
break;
case FLAG_TYPE_BOOL:
*(bool *) flag->value = is_negated(arg) ? false : true;
break;
}
}
return FLAG_OK;
}
/*
* Write usage information to `fp`.
*/
void
flagset_write_usage(flagset_t *self, FILE *fp, const char *name) {
fprintf(fp, "\n Usage: %s %s\n", name, self->usage);
fprintf(fp, "\n Options:\n");
int max = largest_flag_name(self);
for (int i = 0; i < self->nflags; ++i) {
flag_t *flag = &self->flags[i];
fprintf(fp, " --%-*s %s", max+1, flag->name, flag->help);
switch (flag->type) {
case FLAG_TYPE_STRING:
fprintf(fp, " (%s)", *(char **) flag->value);
break;
case FLAG_TYPE_INT:
fprintf(fp, " (%d)", *(int *) flag->value);
break;
case FLAG_TYPE_BOOL:
break;
}
fprintf(fp, "\n");
}
fprintf(fp, "\n");
}
/*
* Parse and output on error.
*/
void
flag_parse(int argc, const char **args, const char *version) {
int rc = 0;
const char *name = args[0];
bool showVersion = false;
bool showHelp = false;
flagset_bool(set, &showVersion, "version", "Output version");
flagset_bool(set, &showHelp, "help", "Output help");
flag_error err = flagset_parse(set, argc-1, args+1);
switch (err) {
case FLAG_ERROR_PARSING:
fprintf(stderr, "invalid value for --%s\n", set->error.flag->name);
rc = 1;
goto exit;
break;
case FLAG_ERROR_ARG_MISSING:
fprintf(stderr, "missing value for --%s\n", set->error.flag->name);
rc = 1;
goto exit;
break;
case FLAG_ERROR_UNDEFINED_FLAG:
fprintf(stderr, "undefined flag %s\n", set->error.arg);
rc = 1;
goto exit;
break;
case FLAG_OK:
break;
}
if (showHelp) {
flagset_write_usage(set, stdout, name);
goto exit;
}
if (showVersion) {
printf("%s\n", version);
goto exit;
}
goto ret;
exit:
if(set) {
free(set);
set = NULL;
}
exit(rc);
ret:
if(set) {
free(set);
set = NULL;
}
}
/*
* Supported flag types.
*/
FLAG_TYPE(int, int, INT);
FLAG_TYPE(bool, bool, BOOL);
FLAG_TYPE(string, const char *, STRING);
#undef CHECK_BOUNDS
#undef REQUIRE_ARG
#undef UNDEFINED_ERROR
#undef PARSE_ERROR
#undef FLAG_TYPE