-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathDopt.c
375 lines (335 loc) · 12.5 KB
/
Dopt.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
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
This software is licensed as OpenSource, under the Apache License, Version 2.0.
This license is available at: http://opensource.org/licenses/Apache-2.0. */
/*
* Command line argument processing support.
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include "Dopt.h"
/* Missing protypes */
extern int fprintf(FILE *stream, const char *format, ...);
extern int sscanf(const char *s, const char *format, ...);
#define opt_PRESENT (1 << 7) /* Flags if option was present */
char *opt_progname; /* Program name */
static struct
{
int nOpts; /* Number of options */
opt_Option *opts; /* Options definitions */
opt_Handler *handler; /* Error handler */
void *client; /* Client data pointer */
int error; /* Flags if error occured */
} global;
/* Compare two options */
static int cmpOptions(const void *a, const void *b) {
return strcmp(((opt_Option *)a)->name, ((opt_Option *)b)->name);
}
/* Scan option's value */
static int doScan(int argc, char *argv[], int argi, opt_Option *opt) {
opt->flags |= opt_PRESENT;
return (opt->scan == NULL) ? argi : opt->scan(argc, argv, argi, opt);
}
/* Match whole key to option */
static int matchWhole(const void *key, const void *value) {
return strcmp((char *)key, ((opt_Option *)value)->name);
}
/* Match part key to option */
static int matchPart(const void *key, const void *value) {
char *name = ((opt_Option *)value)->name;
return strncmp((char *)key, name, strlen(name));
}
/* Lookup option name using supplied match function */
static opt_Option *lookup(char *name,
int (*match)(const void *key, const void *value)) {
return (opt_Option *)bsearch(name, global.opts, global.nOpts,
sizeof(opt_Option), match);
}
static void message(char *fmt, char *arg) {
printf("%s [ERROR]: ", opt_progname);
printf(fmt, arg);
}
static void message2(char *fmt, char *optarg, char *arg) {
printf("%s [ERROR]: ", opt_progname);
printf(fmt, optarg, arg);
}
/* Default error handler */
static int defaultHandler(int error, opt_Option *opt, char *arg, void *client) {
switch (error) {
case opt_NoScanner:
message("no scanner (%s)\n", opt->name);
break;
case opt_Missing:
message("no value(s) (%s)\n", opt->name);
break;
case opt_Format:
message2("bad value <%s> (%s)\n", arg, opt->name);
break;
case opt_Range:
message2("value out of range <%s> (%s)\n", arg, opt->name);
break;
case opt_Required:
message("required option missing (%s)\n", opt->name);
break;
case opt_Exclusive:
message("mutually exclusive option conflict (%s)\n", opt->name);
break;
case opt_Unknown:
message("unknown option (%s)\n", arg);
break;
}
return 1;
}
/* Error handler wrapper */
void opt_Error(int error, opt_Option *opt, char *arg) {
global.error += global.handler(error, opt, arg, global.client);
}
bool is_known_option(char *arg) {
const char *known_options[] = {"-u", "-h", "-T", "-d", "-x", "-i", "-X"};
int num_opts = sizeof(known_options) / sizeof(known_options[0]);
int i;
for (i = 0; i < num_opts; i++)
if (strcmp(arg, known_options[i]) == 0)
return true;
return false;
}
/* Process argument list */
int opt_Scan(int argc, char *argv[],
int nOpts, opt_Option *opts, opt_Handler handler, void *client) {
int i;
int argi;
char *p = argv[0] + strlen(argv[0]);
/* Extract program name, scan for Unix, DOS, and Macintosh separators */
while (--p >= argv[0] && *p != '/' && *p != '\\' && *p != ':')
;
opt_progname = p + 1;
/* Initialize argument package */
global.nOpts = nOpts;
global.opts = opts;
if (handler != NULL) {
global.handler = handler;
global.client = client;
} else {
global.handler = defaultHandler;
global.client = NULL;
}
/* Sort options into alphabetical order */
qsort(opts, nOpts, sizeof(opt_Option), cmpOptions);
/* Set defaults */
for (i = 0; i < global.nOpts; i++) {
opt_Option *opt = &global.opts[i];
opt->flags &= ~opt_PRESENT;
if (opt->scan == NULL)
opt_Error(opt_NoScanner, opt, NULL);
else if (opt->scan != opt_Call)
opt->scan(1, &opt->dflt, 0, opt);
}
argi = 1;
while (argi < argc) {
char *arg = argv[argi];
if (arg[0] == '-' && !is_known_option(arg)) {
opt_Error(opt_Unknown, NULL, arg);
exit(1);
}
opt_Option *opt = lookup(arg, matchWhole);
if (opt != NULL)
/* Whole argument matched option */
argi = doScan(argc, argv, argi + 1, opt);
else {
opt = lookup(arg, matchPart);
if (opt != NULL) {
/* Initial part of argument matched option */
if (opt->flags & opt_COMBINED) {
/* Argument is a combination of options */
int plen = opt->length;
argi = doScan(argc, argv, argi + 1, opt);
for (;;) {
/* Trim last option name from argument and rematch */
strcpy(&arg[plen], &arg[strlen(opt->name)]);
if (arg[plen] == '\0')
break; /* No more options left */
opt = lookup(arg, matchPart);
if (opt == NULL) {
opt_Error(opt_Unknown, NULL, arg);
break;
}
argi = doScan(argc, argv, argi, opt);
}
} else {
/* Option and value combined together */
argv[argi] = arg + strlen(opt->name);
argi = doScan(argc, argv, argi, opt);
}
} else
break;
}
}
/* Test that required options were actually present */
for (i = 0; i < global.nOpts; i++) {
opt_Option *opt = &global.opts[i];
if (opt->flags & opt_REQUIRED && !(opt->flags & opt_PRESENT))
opt_Error(opt_Required, opt, NULL);
}
return (global.error != 0) ? 0 : argi;
}
/* If option was present return non-0 else return 0 */
int opt_Present(char *name) {
opt_Option *opt = lookup(name, matchWhole);
if (opt == NULL) {
message("unknown option (%s)\n", name);
return 0;
}
return opt->flags & opt_PRESENT;
}
/* If option exists and was present return its value else return NULL */
void *opt_Value(char *name) {
opt_Option *opt = lookup(name, matchWhole);
if (opt == NULL) {
message("unknown option (%s)\n", name);
return NULL;
}
return (opt->flags & opt_PRESENT) ? opt->value : (void *)NULL;
}
/* --- Standard value scanners --- */
/* Macro for declaring standard numeric scanners.
I found a bug when testing this code on a Sun3 using a gcc compiler.
Apparently, gcc normally makes string constants read-only. A consequence of
this is that functions that modify string constants passed to them will
cause a segmentation fault. On some systems, e.g. Sun3, the sscanf()
function modifies the source string and causes a segmentation fault when
called thus: sscanf("3.142", "%le", &dbl). Initialization of numeric options
will call sscanf() in a similar way so I defend against this by copying the
string to temporary read/write storage and passing this copy to sscanf().
*/
#define DECLARE(n, t, f) \
int opt_##n(int argc, char *argv[], int argi, opt_Option *opt) { \
if (argv[0] == NULL) \
return argi; \
if (argi == argc) \
opt_Error(opt_Missing, opt, NULL); \
else { \
t value; \
char s[64]; \
strncpy(s, argv[argi], 63); \
s[63] = '\0'; \
if (sscanf(s, f, &value) != 1) \
opt_Error(opt_Format, opt, argv[argi]); \
else if ((opt->min != 0 || opt->max != 0) && \
(value < opt->min || value > opt->max)) \
opt_Error(opt_Range, opt, argv[argi]); \
else \
*(t *)opt->value = value; \
argi++; \
} \
return argi; \
}
DECLARE(Short, short, "%hi")
DECLARE(Int, int, "%i")
DECLARE(Long, long, "%li")
DECLARE(UShort, unsigned short, "%hu")
DECLARE(UInt, unsigned int, "%u")
DECLARE(ULong, unsigned long, "%lu")
DECLARE(Double, double, "%lf")
/* (char) scanner */
int opt_Char(int argc, char *argv[], int argi, opt_Option *opt) {
if (argv[0] == NULL)
return argi;
if (argi == argc)
opt_Error(opt_Missing, opt, NULL);
else {
unsigned int value;
char *arg = argv[argi];
int len = strlen(arg);
int format = 0;
if (len == 1)
value = arg[0];
else {
if (arg[0] == '\\') {
switch (arg[1]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
/* Octal */
if (sscanf(&arg[1], "%o", &value) != 1)
format = 1;
break;
case 'a':
value = '\a';
break;
case 'b':
value = '\b';
break;
case 'f':
value = '\f';
break;
case 'n':
value = '\n';
break;
case 'r':
value = '\r';
break;
case 't':
value = '\t';
break;
case 'x':
/* Hexadecimal */
if (sscanf(&arg[2], "%x", &value) != 1)
format = 1;
break;
case 'v':
value = '\v';
break;
default:
if (len > 2)
format = 1;
else
value = arg[1]; /* Ignore '\' */
}
} else {
format = 1;
}
}
if (format)
opt_Error(opt_Format, opt, arg);
else if ((opt->min != 0 || opt->max != 0) &&
(value < opt->min || value > opt->max))
opt_Error(opt_Range, opt, arg);
else
*(char *)opt->value = value;
argi++;
}
return argi;
}
/* (char *) scanner */
int opt_String(int argc, char *argv[], int argi, opt_Option *opt) {
if (argv[0] == NULL)
return argi;
if (argi == argc)
opt_Error(opt_Missing, opt, NULL);
else {
char *arg = argv[argi];
int len = strlen(arg);
if ((opt->min != 0 || opt->max != 0) &&
(len < opt->min || len > opt->max))
opt_Error(opt_Range, opt, arg);
else
*(char **)opt->value = arg;
argi++;
}
return argi;
}
/* Simply call function */
int opt_Call(int argc, char *argv[], int argi, opt_Option *opt) {
((void (*)(void))opt->value)();
return argi;
}
/* Flag option scanner. Does nothing! */
int opt_Flag(int argc, char *argv[], int argi, opt_Option *opt) {
return argi;
}