-
Notifications
You must be signed in to change notification settings - Fork 23
/
rdopt.c
423 lines (329 loc) · 9.53 KB
/
rdopt.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* librd - Rapid Development C library
*
* Copyright (c) 2012-2013, Magnus Edenhill
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "rd.h"
#include "rdopt.h"
#include "rdbits.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdarg.h>
static char rd_opt_description[2048] = "";
void rd_opt_description_set (const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vsnprintf(rd_opt_description, sizeof(rd_opt_description), fmt, ap);
va_end(ap);
}
static const char *rd_opt_syntaxname (const rd_opt_t *ro) {
static char ret[128][4];
static int i = 0;
int retof = 0;
i = (i + 1) % 4;
*ret[i] = '\0';
if (ro->ro_short)
retof += snprintf(ret[i]+retof, sizeof(ret[i])-retof,
"-%c%s",
ro->ro_short,
ro->ro_long ? "/" : "");
if (ro->ro_long)
retof += snprintf(ret[i]+retof, sizeof(ret[i])-retof,
"--%s", ro->ro_long);
if (ro->ro_argcnt)
retof += snprintf(ret[i]+retof, sizeof(ret[i])-retof,
" <...>");
return ret[i];
}
const char *rd_opt_parse (const rd_opt_t *ros,
int argc, char **argv, int *argip) {
static char ret[1024];
int retof;
const rd_opt_t *ro = ros;
rd_bitvec_t opts_found;
int missed_reqs = 0;
int argi;
int roi;
const rd_opt_t *mutro[RD_OPT_MUT_NUM+1] = {};
rd_bitvec_init(&opts_found, RD_BITVEC_STATIC, 512);
/* Skip argv0 */
for (argi = 1; argi < argc ; argi++) {
const char *longopt = NULL;
const char *val = NULL;
int newi = argi;
const char *t;
rd_opt_parse_f(*parser);
if (!*argv[argi])
continue; /* Empty argument */
if (*argv[argi] != '-')
break; /* First non-option, we're done. */
if (!strcmp(argv[argi], "--help"))
return "(help)";
if (argv[argi][1] == '-')
longopt = &argv[argi][2];
else if (argv[argi][2] != '\0')
val = &argv[argi][2];
/* Match option */
for (ro = ros, roi = 0 ;
ro->ro_type != RD_OPT_END ; ro++, roi++) {
if (!longopt && ro->ro_short) {
if (ro->ro_short != argv[argi][1])
continue;
if (ro->ro_argcnt && !val && argi + 1 < argc)
val = argv[++newi];
} else if (longopt && ro->ro_long) {
if (strcmp(longopt, ro->ro_long))
continue;
if ((t = strchr(longopt, '=')))
val = t+1;
else if (ro->ro_argcnt) {
if (argi + 1 < argc)
val = argv[++newi];
}
} else
continue;
if (!ro->ro_argcnt && val) {
snprintf(ret, sizeof(ret),
"Option %s does"
"not expect any "
"arguments",
rd_opt_syntaxname(ro));
rd_bitvec_free(&opts_found);
return ret;
} else if (ro->ro_argcnt && !val) {
snprintf(ret, sizeof(ret),
"Missing arguments to option %s",
rd_opt_syntaxname(ro));
rd_bitvec_free(&opts_found);
return ret;
}
/* Check mutual exclusitivity. */
if (BIT_TEST(ro->ro_type, RD_OPT_MUT_MASK)) {
int mg = RD_OPT_MUT_GRP(ro->ro_type);
if (mutro[mg]) {
snprintf(ret, sizeof(ret),
"Option %s is mutually "
"exclusive with already set "
"option %s",
rd_opt_syntaxname(ro),
rd_opt_syntaxname(mutro[mg]));
rd_bitvec_free(&opts_found);
return ret;
}
mutro[mg] = ro;
}
/* Use default assignment parser if no parser
* is set but a specific pointer is. */
if (!(parser = ro->ro_parse) && ro->ro_opaque)
parser = rd_opt_parse_assign;
if (parser) {
if ((t = parser(ro, argv, argc,
&newi, ro->ro_opaque))) {
rd_bitvec_free(&opts_found);
return t;
}
}
rd_bitvec_set(&opts_found, roi);
argi = newi;
break;
}
if (ro->ro_type == RD_OPT_END) {
snprintf(ret, sizeof(ret),
"Unknown option: %s", argv[argi]);
rd_bitvec_free(&opts_found);
return ret;
}
}
retof = snprintf(ret, sizeof(ret), "Required option(s) missing:");
/* Check that all required options have been passed. */
for (ro = ros, roi = 0 ;
!BIT_TEST(ro->ro_type, RD_OPT_END) ;
ro++, roi++) {
if (!BIT_TEST(ro->ro_type, RD_OPT_REQ))
continue;
if (rd_bitvec_test(&opts_found, roi))
continue;
/* Required values in mutual exclusitivity groups only
* require one of the mutual excl. options to have been set. */
if (BIT_TEST(ro->ro_type, RD_OPT_MUT_MASK) &&
mutro[RD_OPT_MUT_GRP(ro->ro_type)])
continue;
/* Required option not passed, construct error string. */
retof += snprintf(ret+retof, sizeof(ret)-retof, "%s ",
missed_reqs ? "," : "");
missed_reqs++;
if (ro->ro_short)
retof += snprintf(ret+retof, sizeof(ret)-retof,
"-%c%s",
ro->ro_short,
ro->ro_long ? "/" : "");
if (ro->ro_long)
retof += snprintf(ret+retof, sizeof(ret)-retof,
"%s", ro->ro_long);
if (ro->ro_argcnt)
retof += snprintf(ret+retof, sizeof(ret)-retof,
" <...>");
}
rd_bitvec_free(&opts_found);
if (missed_reqs)
return ret;
if (argip)
*argip = argi;
return NULL;
}
rd_opt_parse_f(rd_opt_parse_assign) {
static char ret[512];
if (ro->ro_argcnt == 0) {
if (opaque && ro->ro_type & (RD_OPT_BOOL|RD_OPT_INT))
*(int *)opaque = 1;
return NULL;
}
/* Assuming just one argument. */
if (ro->ro_type & (RD_OPT_INT|RD_OPT_BOOL)) {
char *end;
int v = strtol(argv[*argip], &end, 0);
if (!v && end == argv[*argip]) {
snprintf(ret, sizeof(ret),
"Option %s requires integer argument",
rd_opt_syntaxname(ro));
return ret;
}
if (ro->ro_type & RD_OPT_BOOL)
v = !!v;
if (opaque)
*(int *)opaque = v;
return NULL;
} else if (ro->ro_type & RD_OPT_PATH) {
struct stat st;
if (stat(argv[*argip], &st) == -1) {
snprintf(ret, sizeof(ret),
"Option %s path %s: %s",
rd_opt_syntaxname(ro), argv[*argip],
strerror(errno));
return ret;
}
if (opaque)
*(const char **)opaque = argv[*argip];
return NULL;
} else if (ro->ro_type & RD_OPT_STR) {
/* Else treat is as a string */
if (!*argv[*argip]) {
snprintf(ret, sizeof(ret),
"Option %s requires an argument",
rd_opt_syntaxname(ro));
return ret;
}
if (opaque)
*(const char **)opaque = argv[*argip];
return NULL;
}
return NULL;
}
void rd_opt_usage (const rd_opt_t *ros, FILE *fp,
const char *argv0, const char *extra_args) {
const rd_opt_t *ro;
int confnamecnt = 0;
int mutcnt[RD_OPT_MUT_NUM+1] = {};
int mg;
if (!ros || BIT_TEST(ros->ro_type, RD_OPT_END)) {
/* No options */
fprintf(fp, "Usage: %s%s%s\n\n",
argv0, extra_args ? " " : "",
extra_args ? : "");
return;
}
fprintf(fp,
"Usage: %s [options]%s%s\n"
"%s\n"
"Options:\n",
argv0, extra_args ? " " : "",
extra_args ? : "",
rd_opt_description);
for (ro = ros ; !BIT_TEST(ro->ro_type, RD_OPT_END) ; ro++) {
char buf[40];
int of = 0;
char mgstr[2];
if (ro->ro_short && ro->ro_long)
of = snprintf(buf, sizeof(buf), "-%c | --%s",
ro->ro_short, ro->ro_long);
else if (ro->ro_short)
of = snprintf(buf, sizeof(buf), "-%c", ro->ro_short);
else if (ro->ro_long)
of = snprintf(buf, sizeof(buf), "--%s", ro->ro_long);
else
continue;
if (ro->ro_argcnt > 0) {
const char *typestr = "..";
if (ro->ro_type & RD_OPT_STR)
typestr = "string";
else if (ro->ro_type & RD_OPT_INT)
typestr = "integer";
else if (ro->ro_type & RD_OPT_PATH)
typestr = "path";
else if (ro->ro_type & RD_OPT_BOOL)
typestr = "bool:1|0";
of += snprintf(buf+of, sizeof(buf)-of,
" <%s>", typestr);
}
if ((mg = RD_OPT_MUT_GRP(ro->ro_type))) {
mutcnt[mg]++;
snprintf(mgstr, sizeof(mgstr), "%i", mg);
} else
strcpy(mgstr, " ");
fprintf(fp, " %s%s %-28s ",
ro->ro_type & RD_OPT_REQ ? "*" : " ", mgstr, buf);
if (ro->ro_confname) {
fprintf(fp, "{%s} ", ro->ro_confname);
confnamecnt++;
}
if (ro->ro_help)
fprintf(fp, "%s", ro->ro_help);
fprintf(fp, "\n");
}
fprintf(fp,
"\n"
" * = Required option\n");
if (confnamecnt)
fprintf(fp, " {..} = Configuration file token\n");
for (mg = 1 ; mg < RD_OPT_MUT_NUM+1 ; mg++)
if (mutcnt[mg])
fprintf(fp, " %i = Mutual exclusive group #%i\n",
mg, mg);
fprintf(fp, "\n");
}
int rd_opt_get (const rd_opt_t *ros,
int argc, char **argv, int *argip,
const char *extra_args) {
const char *errstr;
if ((errstr = rd_opt_parse(ros, argc, argv, argip))) {
if (strcmp(errstr, "(help)"))
fprintf(stderr, "%s: %s\n\n", argv[0], errstr);
rd_opt_usage(ros, stderr, argv[0], extra_args);
return 0;
}
return 1;
}