-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptparse.c
291 lines (268 loc) · 7.71 KB
/
optparse.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
#include <stdio.h>
#ifdef _MSC_VER
#include <stdlib.h> /* for malloc(), free() */
#endif
#include "optparse.h"
#define opterror(options, format, ...) \
snprintf(options->errmsg, sizeof(options->errmsg), format, __VA_ARGS__);
#define options_argv(i) \
((i) < options->argc ? options->argv[i] : NULL)
void optparse_init(struct optparse *options, int argc, char **argv)
{
options->argv = argv;
options->argc = argc;
options->permute = 1;
options->optind = 1;
options->subopt = 0;
options->optarg = NULL;
options->errmsg[0] = '\0';
}
static inline int
is_dashdash(const char *arg)
{
return arg != NULL && arg[0] == '-' && arg[1] == '-' && arg[2] == '\0';
}
static inline int
is_shortopt(const char *arg)
{
return arg != NULL && arg[0] == '-' && arg[1] != '-' && arg[1] != '\0';
}
static inline int
is_longopt(const char *arg)
{
return arg != NULL && arg[0] == '-' && arg[1] == '-' && arg[2] != '\0';
}
static void
permute(struct optparse *options, int index)
{
char *nonoption = options->argv[index];
int i=0;
for (i = index; i < options->optind - 1; i++)
options->argv[i] = options->argv[i + 1];
options->argv[options->optind - 1] = nonoption;
}
static enum optparse_argtype
argtype(const char *optstring, char c)
{
if (c == ':')
return -1;
for (; *optstring && c != *optstring; optstring++);
if (!*optstring)
return -1;
enum optparse_argtype count = OPTPARSE_NONE;
if (optstring[1] == ':')
count += optstring[2] == ':' ? 2 : 1;
return count;
}
int optparse(struct optparse *options, const char *optstring)
{
options->errmsg[0] = '\0';
options->optopt = 0;
options->optarg = NULL;
char *option = options_argv(options->optind);
if (option == NULL) {
return -1;
} else if (is_dashdash(option)) {
options->optind++; // consume "--"
return -1;
} else if (!is_shortopt(option)) {
if (options->permute) {
int index = options->optind;
options->optind++;
int r = optparse(options, optstring);
permute(options, index);
options->optind--;
return r;
} else {
return -1;
}
}
option += options->subopt + 1;
options->optopt = option[0];
int type = argtype(optstring, option[0]);
char *next = options_argv(options->optind + 1);
switch (type) {
case -1:
opterror(options, "invalid option -- '%c'", option[0]);
options->optind++;
return '?';
case OPTPARSE_NONE:
if (option[1]) {
options->subopt++;
} else {
options->subopt = 0;
options->optind++;
}
return option[0];
case OPTPARSE_REQUIRED:
options->subopt = 0;
options->optind++;
if (option[1]) {
options->optarg = option + 1;
} else if (next != NULL) {
options->optarg = next;
options->optind++;
} else {
opterror(options, "option requires an argument -- '%c'", option[0]);
options->optarg = NULL;
return '?';
}
return option[0];
case OPTPARSE_OPTIONAL:
options->subopt = 0;
options->optind++;
if (option[1])
options->optarg = option + 1;
else
options->optarg = NULL;
return option[0];
}
return 0;
}
char *optparse_arg(struct optparse *options)
{
options->subopt = 0;
char *option = options->argv[options->optind];
if (option != NULL)
options->optind++;
return option;
}
static inline int
longopts_end(const struct optparse_long *longopts, int i)
{
return !longopts[i].longname && !longopts[i].shortname;
}
static size_t
optstring_length(const struct optparse_long *longopts)
{
int length = 0;
int i = 0;
for (i = 0; !longopts_end(longopts, i); i++, length++)
length += longopts[i].argtype;
return length + 1;
}
static void
optstring_from_long(const struct optparse_long *longopts, char *optstring)
{
char *p = optstring;
int i = 0;
unsigned int a = 0;
for (i = 0; !longopts_end(longopts, i); i++) {
if (longopts[i].shortname) {
*p++ = longopts[i].shortname;
for (a = 0; a < longopts[i].argtype; a++)
*p++ = ':';
}
}
*p = '\0';
}
/* Unlike strcmp(), handles options containing "=". */
static int
longopts_match(const char *longname, const char *option)
{
if (longname == NULL)
return 0;
const char *a = option, *n = longname;
for (; *a && *n && *a != '='; a++, n++)
if (*a != *n)
return 0;
return *n == '\0' && (*a == '\0' || *a == '=');
}
/* Return the part after "=", or NULL. */
static const char *
longopts_arg(const char *option)
{
for (; *option && *option != '='; option++);
if (*option == '=')
return option + 1;
else
return NULL;
}
static int
long_fallback(struct optparse *options,
const struct optparse_long *longopts,
int *longindex)
{
#ifdef _MSC_VER
/* Variable length arrays are not currently supported in Visual Studio */
char *optstring = malloc(optstring_length(longopts));
#else
char optstring[optstring_length(longopts)];
#endif
optstring_from_long(longopts, optstring);
int result = optparse(options, optstring);
int i = 0;
if (longindex != NULL) {
*longindex = -1;
if (result != -1) {
for (i = 0; !longopts_end(longopts, i); i++) {
if (longopts[i].shortname == options->optopt){
*longindex = i;
}
}
}
}
#ifdef _MSC_VER
free(optstring);
#endif
return result;
}
int
optparse_long(struct optparse *options,
const struct optparse_long *longopts,
int *longindex)
{
// printf("%i < %i\n",options->optind,options->argc);
char *option = options_argv(options->optind);
int i = 0;
if (option == NULL) {
return -1;
} else if (is_dashdash(option)) {
options->optind++; // consume "--"
return -1;
} else if (is_shortopt(option)) {
return long_fallback(options, longopts, longindex);
} else if (!is_longopt(option)) {
if (options->permute) {
int index = options->optind;
options->optind++;
int r = optparse_long(options, longopts, longindex);
permute(options, index);
options->optind--;
return r;
} else {
return -1;
}
}
/* Parse as long option. */
options->errmsg[0] = '\0';
options->optopt = 0;
options->optarg = NULL;
option += 2; // skip "--"
options->optind++;
for (i = 0; !longopts_end(longopts, i); i++) {
const char *name = longopts[i].longname;
if (longopts_match(name, option)) {
if (longindex)
*longindex = i;
options->optopt = longopts[i].shortname;
const char *arg = longopts_arg(option);
if (longopts[i].argtype == OPTPARSE_NONE && arg != NULL) {
opterror(options, "option takes no arguments -- '%s'", name);
return '?';
} if (arg != NULL) {
options->optarg = arg;
} else if (longopts[i].argtype == OPTPARSE_REQUIRED) {
options->optarg = options_argv(options->optind);
options->optind++;
if (options->optarg == NULL) {
opterror(options, "option requires argument -- '%s'", name);
return '?';
}
}
return options->optopt;
}
}
opterror(options, "invalid option -- '%s'", option);
return '?';
}