1#include "git-compat-util.h"
2#include "parse-options.h"
3
4#define OPT_SHORT 1
5#define OPT_UNSET 2
6
7struct optparse_t {
8 const char **argv;
9 int argc;
10 const char *opt;
11};
12
13static inline const char *get_arg(struct optparse_t *p)
14{
15 if (p->opt) {
16 const char *res = p->opt;
17 p->opt = NULL;
18 return res;
19 }
20 p->argc--;
21 return *++p->argv;
22}
23
24static inline const char *skip_prefix(const char *str, const char *prefix)
25{
26 size_t len = strlen(prefix);
27 return strncmp(str, prefix, len) ? NULL : str + len;
28}
29
30static int opterror(const struct option *opt, const char *reason, int flags)
31{
32 if (flags & OPT_SHORT)
33 return error("switch `%c' %s", opt->short_name, reason);
34 if (flags & OPT_UNSET)
35 return error("option `no-%s' %s", opt->long_name, reason);
36 return error("option `%s' %s", opt->long_name, reason);
37}
38
39static int get_value(struct optparse_t *p,
40 const struct option *opt, int flags)
41{
42 const char *s, *arg;
43 const int unset = flags & OPT_UNSET;
44
45 if (unset && p->opt)
46 return opterror(opt, "takes no value", flags);
47 if (unset && (opt->flags & PARSE_OPT_NONEG))
48 return opterror(opt, "isn't available", flags);
49
50 if (!(flags & OPT_SHORT) && p->opt) {
51 switch (opt->type) {
52 case OPTION_CALLBACK:
53 if (!(opt->flags & PARSE_OPT_NOARG))
54 break;
55 /* FALLTHROUGH */
56 case OPTION_BOOLEAN:
57 case OPTION_BIT:
58 case OPTION_SET_INT:
59 case OPTION_SET_PTR:
60 return opterror(opt, "takes no value", flags);
61 default:
62 break;
63 }
64 }
65
66 arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
67 switch (opt->type) {
68 case OPTION_BIT:
69 if (unset)
70 *(int *)opt->value &= ~opt->defval;
71 else
72 *(int *)opt->value |= opt->defval;
73 return 0;
74
75 case OPTION_BOOLEAN:
76 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
77 return 0;
78
79 case OPTION_SET_INT:
80 *(int *)opt->value = unset ? 0 : opt->defval;
81 return 0;
82
83 case OPTION_SET_PTR:
84 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
85 return 0;
86
87 case OPTION_STRING:
88 if (unset) {
89 *(const char **)opt->value = NULL;
90 return 0;
91 }
92 if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-')) {
93 *(const char **)opt->value = (const char *)opt->defval;
94 return 0;
95 }
96 if (!arg)
97 return opterror(opt, "requires a value", flags);
98 *(const char **)opt->value = get_arg(p);
99 return 0;
100
101 case OPTION_CALLBACK:
102 if (unset)
103 return (*opt->callback)(opt, NULL, 1);
104 if (opt->flags & PARSE_OPT_NOARG)
105 return (*opt->callback)(opt, NULL, 0);
106 if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-'))
107 return (*opt->callback)(opt, NULL, 0);
108 if (!arg)
109 return opterror(opt, "requires a value", flags);
110 return (*opt->callback)(opt, get_arg(p), 0);
111
112 case OPTION_INTEGER:
113 if (unset) {
114 *(int *)opt->value = 0;
115 return 0;
116 }
117 if (opt->flags & PARSE_OPT_OPTARG && (!arg || !isdigit(*arg))) {
118 *(int *)opt->value = opt->defval;
119 return 0;
120 }
121 if (!arg)
122 return opterror(opt, "requires a value", flags);
123 *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
124 if (*s)
125 return opterror(opt, "expects a numerical value", flags);
126 return 0;
127
128 default:
129 die("should not happen, someone must be hit on the forehead");
130 }
131}
132
133static int parse_short_opt(struct optparse_t *p, const struct option *options)
134{
135 for (; options->type != OPTION_END; options++) {
136 if (options->short_name == *p->opt) {
137 p->opt = p->opt[1] ? p->opt + 1 : NULL;
138 return get_value(p, options, OPT_SHORT);
139 }
140 }
141 return error("unknown switch `%c'", *p->opt);
142}
143
144static int parse_long_opt(struct optparse_t *p, const char *arg,
145 const struct option *options)
146{
147 const char *arg_end = strchr(arg, '=');
148 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
149 int abbrev_flags = 0, ambiguous_flags = 0;
150
151 if (!arg_end)
152 arg_end = arg + strlen(arg);
153
154 for (; options->type != OPTION_END; options++) {
155 const char *rest;
156 int flags = 0;
157
158 if (!options->long_name)
159 continue;
160
161 rest = skip_prefix(arg, options->long_name);
162 if (!rest) {
163 /* abbreviated? */
164 if (!strncmp(options->long_name, arg, arg_end - arg)) {
165is_abbreviated:
166 if (abbrev_option) {
167 /*
168 * If this is abbreviated, it is
169 * ambiguous. So when there is no
170 * exact match later, we need to
171 * error out.
172 */
173 ambiguous_option = abbrev_option;
174 ambiguous_flags = abbrev_flags;
175 }
176 if (!(flags & OPT_UNSET) && *arg_end)
177 p->opt = arg_end + 1;
178 abbrev_option = options;
179 abbrev_flags = flags;
180 continue;
181 }
182 /* negated and abbreviated very much? */
183 if (!prefixcmp("no-", arg)) {
184 flags |= OPT_UNSET;
185 goto is_abbreviated;
186 }
187 /* negated? */
188 if (strncmp(arg, "no-", 3))
189 continue;
190 flags |= OPT_UNSET;
191 rest = skip_prefix(arg + 3, options->long_name);
192 /* abbreviated and negated? */
193 if (!rest && !prefixcmp(options->long_name, arg + 3))
194 goto is_abbreviated;
195 if (!rest)
196 continue;
197 }
198 if (*rest) {
199 if (*rest != '=')
200 continue;
201 p->opt = rest + 1;
202 }
203 return get_value(p, options, flags);
204 }
205
206 if (ambiguous_option)
207 return error("Ambiguous option: %s "
208 "(could be --%s%s or --%s%s)",
209 arg,
210 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
211 ambiguous_option->long_name,
212 (abbrev_flags & OPT_UNSET) ? "no-" : "",
213 abbrev_option->long_name);
214 if (abbrev_option)
215 return get_value(p, abbrev_option, abbrev_flags);
216 return error("unknown option `%s'", arg);
217}
218
219int parse_options(int argc, const char **argv, const struct option *options,
220 const char * const usagestr[], int flags)
221{
222 struct optparse_t args = { argv + 1, argc - 1, NULL };
223 int j = 0;
224
225 for (; args.argc; args.argc--, args.argv++) {
226 const char *arg = args.argv[0];
227
228 if (*arg != '-' || !arg[1]) {
229 argv[j++] = args.argv[0];
230 continue;
231 }
232
233 if (arg[1] != '-') {
234 args.opt = arg + 1;
235 do {
236 if (*args.opt == 'h')
237 usage_with_options(usagestr, options);
238 if (parse_short_opt(&args, options) < 0)
239 usage_with_options(usagestr, options);
240 } while (args.opt);
241 continue;
242 }
243
244 if (!arg[2]) { /* "--" */
245 if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
246 args.argc--;
247 args.argv++;
248 }
249 break;
250 }
251
252 if (!strcmp(arg + 2, "help"))
253 usage_with_options(usagestr, options);
254 if (parse_long_opt(&args, arg + 2, options))
255 usage_with_options(usagestr, options);
256 }
257
258 memmove(argv + j, args.argv, args.argc * sizeof(*argv));
259 argv[j + args.argc] = NULL;
260 return j + args.argc;
261}
262
263#define USAGE_OPTS_WIDTH 24
264#define USAGE_GAP 2
265
266void usage_with_options(const char * const *usagestr,
267 const struct option *opts)
268{
269 fprintf(stderr, "usage: %s\n", *usagestr++);
270 while (*usagestr && **usagestr)
271 fprintf(stderr, " or: %s\n", *usagestr++);
272 while (*usagestr)
273 fprintf(stderr, " %s\n", *usagestr++);
274
275 if (opts->type != OPTION_GROUP)
276 fputc('\n', stderr);
277
278 for (; opts->type != OPTION_END; opts++) {
279 size_t pos;
280 int pad;
281
282 if (opts->type == OPTION_GROUP) {
283 fputc('\n', stderr);
284 if (*opts->help)
285 fprintf(stderr, "%s\n", opts->help);
286 continue;
287 }
288
289 pos = fprintf(stderr, " ");
290 if (opts->short_name)
291 pos += fprintf(stderr, "-%c", opts->short_name);
292 if (opts->long_name && opts->short_name)
293 pos += fprintf(stderr, ", ");
294 if (opts->long_name)
295 pos += fprintf(stderr, "--%s", opts->long_name);
296
297 switch (opts->type) {
298 case OPTION_INTEGER:
299 if (opts->flags & PARSE_OPT_OPTARG)
300 pos += fprintf(stderr, " [<n>]");
301 else
302 pos += fprintf(stderr, " <n>");
303 break;
304 case OPTION_CALLBACK:
305 if (opts->flags & PARSE_OPT_NOARG)
306 break;
307 /* FALLTHROUGH */
308 case OPTION_STRING:
309 if (opts->argh) {
310 if (opts->flags & PARSE_OPT_OPTARG)
311 pos += fprintf(stderr, " [<%s>]", opts->argh);
312 else
313 pos += fprintf(stderr, " <%s>", opts->argh);
314 } else {
315 if (opts->flags & PARSE_OPT_OPTARG)
316 pos += fprintf(stderr, " [...]");
317 else
318 pos += fprintf(stderr, " ...");
319 }
320 break;
321 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
322 break;
323 }
324
325 if (pos <= USAGE_OPTS_WIDTH)
326 pad = USAGE_OPTS_WIDTH - pos;
327 else {
328 fputc('\n', stderr);
329 pad = USAGE_OPTS_WIDTH;
330 }
331 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
332 }
333 fputc('\n', stderr);
334
335 exit(129);
336}
337
338/*----- some often used options -----*/
339#include "cache.h"
340
341int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
342{
343 int v;
344
345 if (!arg) {
346 v = unset ? 0 : DEFAULT_ABBREV;
347 } else {
348 v = strtol(arg, (char **)&arg, 10);
349 if (*arg)
350 return opterror(opt, "expects a numerical value", 0);
351 if (v && v < MINIMUM_ABBREV)
352 v = MINIMUM_ABBREV;
353 else if (v > 40)
354 v = 40;
355 }
356 *(int *)(opt->value) = v;
357 return 0;
358}