blob: dbb9efbf718a065d4d6e9998fa4d147b1cc691fc [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -06002#include <linux/compiler.h>
Arnaldo Carvalho de Melo96395cb2017-04-26 15:49:21 -03003#include <linux/string.h>
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -06004#include <linux/types.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <stdint.h>
8#include <string.h>
9#include <ctype.h>
Josh Poimboeuf901421a2015-12-15 09:39:36 -060010#include "subcmd-util.h"
Ingo Molnar07800602009-04-20 15:00:56 +020011#include "parse-options.h"
Josh Poimboeuf096d3552015-12-15 09:39:35 -060012#include "subcmd-config.h"
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -060013#include "pager.h"
Ingo Molnar07800602009-04-20 15:00:56 +020014
15#define OPT_SHORT 1
16#define OPT_UNSET 2
17
Josh Poimboeuf901421a2015-12-15 09:39:36 -060018char *error_buf;
Namhyung Kim01b19452015-10-25 00:49:26 +090019
Ingo Molnar07800602009-04-20 15:00:56 +020020static int opterror(const struct option *opt, const char *reason, int flags)
21{
22 if (flags & OPT_SHORT)
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -060023 fprintf(stderr, " Error: switch `%c' %s", opt->short_name, reason);
24 else if (flags & OPT_UNSET)
25 fprintf(stderr, " Error: option `no-%s' %s", opt->long_name, reason);
26 else
27 fprintf(stderr, " Error: option `%s' %s", opt->long_name, reason);
28
29 return -1;
30}
31
32static const char *skip_prefix(const char *str, const char *prefix)
33{
34 size_t len = strlen(prefix);
35 return strncmp(str, prefix, len) ? NULL : str + len;
Ingo Molnar07800602009-04-20 15:00:56 +020036}
37
Wang Nan48e1cab2015-12-14 10:39:22 +000038static void optwarning(const struct option *opt, const char *reason, int flags)
39{
40 if (flags & OPT_SHORT)
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -060041 fprintf(stderr, " Warning: switch `%c' %s", opt->short_name, reason);
Wang Nan48e1cab2015-12-14 10:39:22 +000042 else if (flags & OPT_UNSET)
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -060043 fprintf(stderr, " Warning: option `no-%s' %s", opt->long_name, reason);
Wang Nan48e1cab2015-12-14 10:39:22 +000044 else
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -060045 fprintf(stderr, " Warning: option `%s' %s", opt->long_name, reason);
Wang Nan48e1cab2015-12-14 10:39:22 +000046}
47
Ingo Molnar07800602009-04-20 15:00:56 +020048static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
49 int flags, const char **arg)
50{
Wang Nan48e1cab2015-12-14 10:39:22 +000051 const char *res;
52
Ingo Molnar07800602009-04-20 15:00:56 +020053 if (p->opt) {
Wang Nan48e1cab2015-12-14 10:39:22 +000054 res = p->opt;
Ingo Molnar07800602009-04-20 15:00:56 +020055 p->opt = NULL;
Frederic Weisbecker5a4b1812009-07-02 17:58:20 +020056 } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
57 **(p->argv + 1) == '-')) {
Wang Nan48e1cab2015-12-14 10:39:22 +000058 res = (const char *)opt->defval;
Ingo Molnar07800602009-04-20 15:00:56 +020059 } else if (p->argc > 1) {
60 p->argc--;
Wang Nan48e1cab2015-12-14 10:39:22 +000061 res = *++p->argv;
Ingo Molnar07800602009-04-20 15:00:56 +020062 } else
63 return opterror(opt, "requires a value", flags);
Wang Nan48e1cab2015-12-14 10:39:22 +000064 if (arg)
65 *arg = res;
Ingo Molnar07800602009-04-20 15:00:56 +020066 return 0;
67}
68
69static int get_value(struct parse_opt_ctx_t *p,
70 const struct option *opt, int flags)
71{
Ingo Molnar233f0b92009-06-03 21:48:40 +020072 const char *s, *arg = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +020073 const int unset = flags & OPT_UNSET;
Wang Nan0c8c2072015-03-13 12:51:54 +000074 int err;
Ingo Molnar07800602009-04-20 15:00:56 +020075
76 if (unset && p->opt)
77 return opterror(opt, "takes no value", flags);
78 if (unset && (opt->flags & PARSE_OPT_NONEG))
79 return opterror(opt, "isn't available", flags);
Namhyung Kimd152d1b2014-10-23 00:15:45 +090080 if (opt->flags & PARSE_OPT_DISABLED)
81 return opterror(opt, "is not usable", flags);
Ingo Molnar07800602009-04-20 15:00:56 +020082
Namhyung Kim42bd71d2014-10-23 00:15:48 +090083 if (opt->flags & PARSE_OPT_EXCLUSIVE) {
Namhyung Kim5594b552015-01-10 19:33:45 +090084 if (p->excl_opt && p->excl_opt != opt) {
Namhyung Kim42bd71d2014-10-23 00:15:48 +090085 char msg[128];
86
87 if (((flags & OPT_SHORT) && p->excl_opt->short_name) ||
88 p->excl_opt->long_name == NULL) {
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -060089 snprintf(msg, sizeof(msg), "cannot be used with switch `%c'",
90 p->excl_opt->short_name);
Namhyung Kim42bd71d2014-10-23 00:15:48 +090091 } else {
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -060092 snprintf(msg, sizeof(msg), "cannot be used with %s",
93 p->excl_opt->long_name);
Namhyung Kim42bd71d2014-10-23 00:15:48 +090094 }
95 opterror(opt, msg, flags);
96 return -3;
97 }
98 p->excl_opt = opt;
99 }
Ingo Molnar07800602009-04-20 15:00:56 +0200100 if (!(flags & OPT_SHORT) && p->opt) {
101 switch (opt->type) {
102 case OPTION_CALLBACK:
103 if (!(opt->flags & PARSE_OPT_NOARG))
104 break;
105 /* FALLTHROUGH */
106 case OPTION_BOOLEAN:
Ian Munsiec0555642010-04-13 18:37:33 +1000107 case OPTION_INCR:
Ingo Molnar07800602009-04-20 15:00:56 +0200108 case OPTION_BIT:
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300109 case OPTION_SET_UINT:
Ingo Molnar07800602009-04-20 15:00:56 +0200110 case OPTION_SET_PTR:
111 return opterror(opt, "takes no value", flags);
Ingo Molnar83a09442009-08-15 12:26:57 +0200112 case OPTION_END:
113 case OPTION_ARGUMENT:
114 case OPTION_GROUP:
115 case OPTION_STRING:
116 case OPTION_INTEGER:
Arnaldo Carvalho de Meloc100edb2010-05-17 15:30:00 -0300117 case OPTION_UINTEGER:
Ingo Molnar83a09442009-08-15 12:26:57 +0200118 case OPTION_LONG:
Arnaldo Carvalho de Melo4ba8b3e2018-10-18 15:24:07 -0300119 case OPTION_ULONG:
Arnaldo Carvalho de Melo6ba85ce2010-05-17 12:16:48 -0300120 case OPTION_U64:
Ingo Molnar07800602009-04-20 15:00:56 +0200121 default:
122 break;
123 }
124 }
125
Wang Nan48e1cab2015-12-14 10:39:22 +0000126 if (opt->flags & PARSE_OPT_NOBUILD) {
127 char reason[128];
128 bool noarg = false;
129
130 err = snprintf(reason, sizeof(reason),
131 opt->flags & PARSE_OPT_CANSKIP ?
132 "is being ignored because %s " :
133 "is not available because %s",
134 opt->build_opt);
135 reason[sizeof(reason) - 1] = '\0';
136
137 if (err < 0)
138 strncpy(reason, opt->flags & PARSE_OPT_CANSKIP ?
139 "is being ignored" :
140 "is not available",
141 sizeof(reason));
142
143 if (!(opt->flags & PARSE_OPT_CANSKIP))
144 return opterror(opt, reason, flags);
145
146 err = 0;
147 if (unset)
148 noarg = true;
149 if (opt->flags & PARSE_OPT_NOARG)
150 noarg = true;
151 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
152 noarg = true;
153
154 switch (opt->type) {
155 case OPTION_BOOLEAN:
156 case OPTION_INCR:
157 case OPTION_BIT:
158 case OPTION_SET_UINT:
159 case OPTION_SET_PTR:
160 case OPTION_END:
161 case OPTION_ARGUMENT:
162 case OPTION_GROUP:
163 noarg = true;
164 break;
165 case OPTION_CALLBACK:
166 case OPTION_STRING:
167 case OPTION_INTEGER:
168 case OPTION_UINTEGER:
169 case OPTION_LONG:
Arnaldo Carvalho de Melo4ba8b3e2018-10-18 15:24:07 -0300170 case OPTION_ULONG:
Wang Nan48e1cab2015-12-14 10:39:22 +0000171 case OPTION_U64:
172 default:
173 break;
174 }
175
176 if (!noarg)
177 err = get_arg(p, opt, flags, NULL);
178 if (err)
179 return err;
180
181 optwarning(opt, reason, flags);
182 return 0;
183 }
184
Ingo Molnar07800602009-04-20 15:00:56 +0200185 switch (opt->type) {
186 case OPTION_BIT:
187 if (unset)
188 *(int *)opt->value &= ~opt->defval;
189 else
190 *(int *)opt->value |= opt->defval;
191 return 0;
192
193 case OPTION_BOOLEAN:
Ian Munsiec0555642010-04-13 18:37:33 +1000194 *(bool *)opt->value = unset ? false : true;
Adrian Hunter167faf32013-11-18 11:55:56 +0200195 if (opt->set)
196 *(bool *)opt->set = true;
Ian Munsiec0555642010-04-13 18:37:33 +1000197 return 0;
198
199 case OPTION_INCR:
Ingo Molnar07800602009-04-20 15:00:56 +0200200 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
201 return 0;
202
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300203 case OPTION_SET_UINT:
204 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
Ingo Molnar07800602009-04-20 15:00:56 +0200205 return 0;
206
207 case OPTION_SET_PTR:
208 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
209 return 0;
210
211 case OPTION_STRING:
Wang Nan0c8c2072015-03-13 12:51:54 +0000212 err = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200213 if (unset)
214 *(const char **)opt->value = NULL;
215 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
216 *(const char **)opt->value = (const char *)opt->defval;
217 else
Wang Nan0c8c2072015-03-13 12:51:54 +0000218 err = get_arg(p, opt, flags, (const char **)opt->value);
219
Jiri Olsab66fb1d2017-01-03 09:19:54 +0100220 if (opt->set)
221 *(bool *)opt->set = true;
222
Wang Nan0c8c2072015-03-13 12:51:54 +0000223 /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
224 if (opt->flags & PARSE_OPT_NOEMPTY) {
225 const char *val = *(const char **)opt->value;
226
227 if (!val)
228 return err;
229
230 /* Similar to unset if we are given an empty string. */
231 if (val[0] == '\0') {
232 *(const char **)opt->value = NULL;
233 return 0;
234 }
235 }
236
237 return err;
Ingo Molnar07800602009-04-20 15:00:56 +0200238
239 case OPTION_CALLBACK:
240 if (unset)
241 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
242 if (opt->flags & PARSE_OPT_NOARG)
243 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
244 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
245 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
246 if (get_arg(p, opt, flags, &arg))
247 return -1;
248 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
249
250 case OPTION_INTEGER:
251 if (unset) {
252 *(int *)opt->value = 0;
253 return 0;
254 }
255 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
256 *(int *)opt->value = opt->defval;
257 return 0;
258 }
259 if (get_arg(p, opt, flags, &arg))
260 return -1;
261 *(int *)opt->value = strtol(arg, (char **)&s, 10);
262 if (*s)
263 return opterror(opt, "expects a numerical value", flags);
264 return 0;
265
Arnaldo Carvalho de Meloc100edb2010-05-17 15:30:00 -0300266 case OPTION_UINTEGER:
267 if (unset) {
268 *(unsigned int *)opt->value = 0;
269 return 0;
270 }
271 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
272 *(unsigned int *)opt->value = opt->defval;
273 return 0;
274 }
275 if (get_arg(p, opt, flags, &arg))
276 return -1;
Arnaldo Carvalho de Melob9889712017-02-14 13:55:40 -0300277 if (arg[0] == '-')
278 return opterror(opt, "expects an unsigned numerical value", flags);
Arnaldo Carvalho de Meloc100edb2010-05-17 15:30:00 -0300279 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
280 if (*s)
281 return opterror(opt, "expects a numerical value", flags);
282 return 0;
283
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200284 case OPTION_LONG:
285 if (unset) {
286 *(long *)opt->value = 0;
287 return 0;
288 }
289 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
290 *(long *)opt->value = opt->defval;
291 return 0;
292 }
293 if (get_arg(p, opt, flags, &arg))
294 return -1;
295 *(long *)opt->value = strtol(arg, (char **)&s, 10);
296 if (*s)
297 return opterror(opt, "expects a numerical value", flags);
298 return 0;
299
Arnaldo Carvalho de Melo4ba8b3e2018-10-18 15:24:07 -0300300 case OPTION_ULONG:
301 if (unset) {
302 *(unsigned long *)opt->value = 0;
303 return 0;
304 }
305 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
306 *(unsigned long *)opt->value = opt->defval;
307 return 0;
308 }
309 if (get_arg(p, opt, flags, &arg))
310 return -1;
311 *(unsigned long *)opt->value = strtoul(arg, (char **)&s, 10);
312 if (*s)
313 return opterror(opt, "expects a numerical value", flags);
314 return 0;
315
Arnaldo Carvalho de Melo6ba85ce2010-05-17 12:16:48 -0300316 case OPTION_U64:
317 if (unset) {
318 *(u64 *)opt->value = 0;
319 return 0;
320 }
321 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
322 *(u64 *)opt->value = opt->defval;
323 return 0;
324 }
325 if (get_arg(p, opt, flags, &arg))
326 return -1;
Arnaldo Carvalho de Melob9889712017-02-14 13:55:40 -0300327 if (arg[0] == '-')
328 return opterror(opt, "expects an unsigned numerical value", flags);
Arnaldo Carvalho de Melo6ba85ce2010-05-17 12:16:48 -0300329 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
330 if (*s)
331 return opterror(opt, "expects a numerical value", flags);
332 return 0;
333
Ingo Molnar83a09442009-08-15 12:26:57 +0200334 case OPTION_END:
335 case OPTION_ARGUMENT:
336 case OPTION_GROUP:
Ingo Molnar07800602009-04-20 15:00:56 +0200337 default:
338 die("should not happen, someone must be hit on the forehead");
339 }
340}
341
342static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
343{
Namhyung Kim369a2472016-10-24 12:00:02 +0900344retry:
Ingo Molnar07800602009-04-20 15:00:56 +0200345 for (; options->type != OPTION_END; options++) {
346 if (options->short_name == *p->opt) {
347 p->opt = p->opt[1] ? p->opt + 1 : NULL;
348 return get_value(p, options, OPT_SHORT);
349 }
350 }
Namhyung Kim369a2472016-10-24 12:00:02 +0900351
352 if (options->parent) {
353 options = options->parent;
354 goto retry;
355 }
356
Ingo Molnar07800602009-04-20 15:00:56 +0200357 return -2;
358}
359
360static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
361 const struct option *options)
362{
363 const char *arg_end = strchr(arg, '=');
364 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
365 int abbrev_flags = 0, ambiguous_flags = 0;
366
367 if (!arg_end)
368 arg_end = arg + strlen(arg);
369
Namhyung Kim369a2472016-10-24 12:00:02 +0900370retry:
Ingo Molnar07800602009-04-20 15:00:56 +0200371 for (; options->type != OPTION_END; options++) {
372 const char *rest;
373 int flags = 0;
374
375 if (!options->long_name)
376 continue;
377
378 rest = skip_prefix(arg, options->long_name);
379 if (options->type == OPTION_ARGUMENT) {
380 if (!rest)
381 continue;
382 if (*rest == '=')
383 return opterror(options, "takes no value", flags);
384 if (*rest)
385 continue;
386 p->out[p->cpidx++] = arg - 2;
387 return 0;
388 }
389 if (!rest) {
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300390 if (strstarts(options->long_name, "no-")) {
Adrian Hunter4bc43792013-11-18 11:55:55 +0200391 /*
392 * The long name itself starts with "no-", so
393 * accept the option without "no-" so that users
394 * do not have to enter "no-no-" to get the
395 * negation.
396 */
397 rest = skip_prefix(arg, options->long_name + 3);
398 if (rest) {
399 flags |= OPT_UNSET;
400 goto match;
401 }
402 /* Abbreviated case */
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300403 if (strstarts(options->long_name + 3, arg)) {
Adrian Hunter4bc43792013-11-18 11:55:55 +0200404 flags |= OPT_UNSET;
405 goto is_abbreviated;
406 }
407 }
Ingo Molnar07800602009-04-20 15:00:56 +0200408 /* abbreviated? */
409 if (!strncmp(options->long_name, arg, arg_end - arg)) {
410is_abbreviated:
411 if (abbrev_option) {
412 /*
413 * If this is abbreviated, it is
414 * ambiguous. So when there is no
415 * exact match later, we need to
416 * error out.
417 */
418 ambiguous_option = abbrev_option;
419 ambiguous_flags = abbrev_flags;
420 }
421 if (!(flags & OPT_UNSET) && *arg_end)
422 p->opt = arg_end + 1;
423 abbrev_option = options;
424 abbrev_flags = flags;
425 continue;
426 }
427 /* negated and abbreviated very much? */
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300428 if (strstarts("no-", arg)) {
Ingo Molnar07800602009-04-20 15:00:56 +0200429 flags |= OPT_UNSET;
430 goto is_abbreviated;
431 }
432 /* negated? */
433 if (strncmp(arg, "no-", 3))
434 continue;
435 flags |= OPT_UNSET;
436 rest = skip_prefix(arg + 3, options->long_name);
437 /* abbreviated and negated? */
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300438 if (!rest && strstarts(options->long_name, arg + 3))
Ingo Molnar07800602009-04-20 15:00:56 +0200439 goto is_abbreviated;
440 if (!rest)
441 continue;
442 }
Adrian Hunter4bc43792013-11-18 11:55:55 +0200443match:
Ingo Molnar07800602009-04-20 15:00:56 +0200444 if (*rest) {
445 if (*rest != '=')
446 continue;
447 p->opt = rest + 1;
448 }
449 return get_value(p, options, flags);
450 }
451
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -0600452 if (ambiguous_option) {
453 fprintf(stderr,
Ravi Bangoria66f5a072018-04-17 09:43:44 +0530454 " Error: Ambiguous option: %s (could be --%s%s or --%s%s)\n",
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -0600455 arg,
456 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
457 ambiguous_option->long_name,
458 (abbrev_flags & OPT_UNSET) ? "no-" : "",
459 abbrev_option->long_name);
460 return -1;
461 }
Ingo Molnar07800602009-04-20 15:00:56 +0200462 if (abbrev_option)
463 return get_value(p, abbrev_option, abbrev_flags);
Namhyung Kim369a2472016-10-24 12:00:02 +0900464
465 if (options->parent) {
466 options = options->parent;
467 goto retry;
468 }
469
Ingo Molnar07800602009-04-20 15:00:56 +0200470 return -2;
471}
472
473static void check_typos(const char *arg, const struct option *options)
474{
475 if (strlen(arg) < 3)
476 return;
477
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300478 if (strstarts(arg, "no-")) {
Ravi Bangoria66f5a072018-04-17 09:43:44 +0530479 fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)\n", arg);
Ingo Molnar07800602009-04-20 15:00:56 +0200480 exit(129);
481 }
482
483 for (; options->type != OPTION_END; options++) {
484 if (!options->long_name)
485 continue;
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300486 if (strstarts(options->long_name, arg)) {
Ravi Bangoria66f5a072018-04-17 09:43:44 +0530487 fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)\n", arg);
Ingo Molnar07800602009-04-20 15:00:56 +0200488 exit(129);
489 }
490 }
491}
492
Josh Poimboeuf408cf342015-12-13 22:18:12 -0600493static void parse_options_start(struct parse_opt_ctx_t *ctx,
494 int argc, const char **argv, int flags)
Ingo Molnar07800602009-04-20 15:00:56 +0200495{
496 memset(ctx, 0, sizeof(*ctx));
497 ctx->argc = argc - 1;
498 ctx->argv = argv + 1;
499 ctx->out = argv;
500 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
501 ctx->flags = flags;
502 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
503 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
504 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
505}
506
507static int usage_with_options_internal(const char * const *,
Arnaldo Carvalho de Melo161d9042015-10-23 13:27:39 -0300508 const struct option *, int,
509 struct parse_opt_ctx_t *);
Ingo Molnar07800602009-04-20 15:00:56 +0200510
Josh Poimboeuf408cf342015-12-13 22:18:12 -0600511static int parse_options_step(struct parse_opt_ctx_t *ctx,
512 const struct option *options,
513 const char * const usagestr[])
Ingo Molnar07800602009-04-20 15:00:56 +0200514{
515 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900516 int excl_short_opt = 1;
517 const char *arg;
Ingo Molnar07800602009-04-20 15:00:56 +0200518
519 /* we must reset ->opt, unknown short option leave it dangling */
520 ctx->opt = NULL;
521
522 for (; ctx->argc; ctx->argc--, ctx->argv++) {
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900523 arg = ctx->argv[0];
Ingo Molnar07800602009-04-20 15:00:56 +0200524 if (*arg != '-' || !arg[1]) {
525 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
526 break;
527 ctx->out[ctx->cpidx++] = ctx->argv[0];
528 continue;
529 }
530
531 if (arg[1] != '-') {
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900532 ctx->opt = ++arg;
Arnaldo Carvalho de Melo161d9042015-10-23 13:27:39 -0300533 if (internal_help && *ctx->opt == 'h') {
534 return usage_with_options_internal(usagestr, options, 0, ctx);
535 }
Ingo Molnar07800602009-04-20 15:00:56 +0200536 switch (parse_short_opt(ctx, options)) {
537 case -1:
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900538 return parse_options_usage(usagestr, options, arg, 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200539 case -2:
540 goto unknown;
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900541 case -3:
542 goto exclusive;
Ingo Molnar83a09442009-08-15 12:26:57 +0200543 default:
544 break;
Ingo Molnar07800602009-04-20 15:00:56 +0200545 }
546 if (ctx->opt)
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900547 check_typos(arg, options);
Ingo Molnar07800602009-04-20 15:00:56 +0200548 while (ctx->opt) {
549 if (internal_help && *ctx->opt == 'h')
Arnaldo Carvalho de Melo161d9042015-10-23 13:27:39 -0300550 return usage_with_options_internal(usagestr, options, 0, ctx);
Namhyung Kimac697622013-11-01 16:33:11 +0900551 arg = ctx->opt;
Ingo Molnar07800602009-04-20 15:00:56 +0200552 switch (parse_short_opt(ctx, options)) {
553 case -1:
Namhyung Kimac697622013-11-01 16:33:11 +0900554 return parse_options_usage(usagestr, options, arg, 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200555 case -2:
556 /* fake a short option thing to hide the fact that we may have
557 * started to parse aggregated stuff
558 *
559 * This is leaky, too bad.
560 */
561 ctx->argv[0] = strdup(ctx->opt - 1);
562 *(char *)ctx->argv[0] = '-';
563 goto unknown;
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900564 case -3:
565 goto exclusive;
Ingo Molnar83a09442009-08-15 12:26:57 +0200566 default:
567 break;
Ingo Molnar07800602009-04-20 15:00:56 +0200568 }
569 }
570 continue;
571 }
572
573 if (!arg[2]) { /* "--" */
574 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
575 ctx->argc--;
576 ctx->argv++;
577 }
578 break;
579 }
580
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900581 arg += 2;
582 if (internal_help && !strcmp(arg, "help-all"))
Arnaldo Carvalho de Melo161d9042015-10-23 13:27:39 -0300583 return usage_with_options_internal(usagestr, options, 1, ctx);
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900584 if (internal_help && !strcmp(arg, "help"))
Arnaldo Carvalho de Melo161d9042015-10-23 13:27:39 -0300585 return usage_with_options_internal(usagestr, options, 0, ctx);
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900586 if (!strcmp(arg, "list-opts"))
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500587 return PARSE_OPT_LIST_OPTS;
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900588 if (!strcmp(arg, "list-cmds"))
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500589 return PARSE_OPT_LIST_SUBCMDS;
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900590 switch (parse_long_opt(ctx, arg, options)) {
Ingo Molnar07800602009-04-20 15:00:56 +0200591 case -1:
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900592 return parse_options_usage(usagestr, options, arg, 0);
Ingo Molnar07800602009-04-20 15:00:56 +0200593 case -2:
594 goto unknown;
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900595 case -3:
596 excl_short_opt = 0;
597 goto exclusive;
Ingo Molnar83a09442009-08-15 12:26:57 +0200598 default:
599 break;
Ingo Molnar07800602009-04-20 15:00:56 +0200600 }
601 continue;
602unknown:
603 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
604 return PARSE_OPT_UNKNOWN;
605 ctx->out[ctx->cpidx++] = ctx->argv[0];
606 ctx->opt = NULL;
607 }
608 return PARSE_OPT_DONE;
Namhyung Kim42bd71d2014-10-23 00:15:48 +0900609
610exclusive:
611 parse_options_usage(usagestr, options, arg, excl_short_opt);
612 if ((excl_short_opt && ctx->excl_opt->short_name) ||
613 ctx->excl_opt->long_name == NULL) {
614 char opt = ctx->excl_opt->short_name;
615 parse_options_usage(NULL, options, &opt, 1);
616 } else {
617 parse_options_usage(NULL, options, ctx->excl_opt->long_name, 0);
618 }
619 return PARSE_OPT_HELP;
Ingo Molnar07800602009-04-20 15:00:56 +0200620}
621
Josh Poimboeuf408cf342015-12-13 22:18:12 -0600622static int parse_options_end(struct parse_opt_ctx_t *ctx)
Ingo Molnar07800602009-04-20 15:00:56 +0200623{
624 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
625 ctx->out[ctx->cpidx + ctx->argc] = NULL;
626 return ctx->cpidx + ctx->argc;
627}
628
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500629int parse_options_subcommand(int argc, const char **argv, const struct option *options,
630 const char *const subcommands[], const char *usagestr[], int flags)
Ingo Molnar07800602009-04-20 15:00:56 +0200631{
632 struct parse_opt_ctx_t ctx;
633
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500634 /* build usage string if it's not provided */
635 if (subcommands && !usagestr[0]) {
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600636 char *buf = NULL;
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500637
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600638 astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
639
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500640 for (int i = 0; subcommands[i]; i++) {
641 if (i)
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600642 astrcat(&buf, "|");
643 astrcat(&buf, subcommands[i]);
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500644 }
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600645 astrcat(&buf, "}");
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500646
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600647 usagestr[0] = buf;
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500648 }
649
Ingo Molnar07800602009-04-20 15:00:56 +0200650 parse_options_start(&ctx, argc, argv, flags);
651 switch (parse_options_step(&ctx, options, usagestr)) {
652 case PARSE_OPT_HELP:
653 exit(129);
654 case PARSE_OPT_DONE:
655 break;
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500656 case PARSE_OPT_LIST_OPTS:
Namhyung Kim4d8061f2012-10-03 00:21:34 +0900657 while (options->type != OPTION_END) {
Yunlong Song3ef1e652015-02-27 18:21:30 +0800658 if (options->long_name)
659 printf("--%s ", options->long_name);
Namhyung Kim4d8061f2012-10-03 00:21:34 +0900660 options++;
661 }
Yunlong Songed457522015-02-27 18:21:29 +0800662 putchar('\n');
Namhyung Kim4d8061f2012-10-03 00:21:34 +0900663 exit(130);
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500664 case PARSE_OPT_LIST_SUBCMDS:
Yunlong Song3a030052015-02-13 21:11:52 +0800665 if (subcommands) {
666 for (int i = 0; subcommands[i]; i++)
667 printf("%s ", subcommands[i]);
668 }
Yunlong Songed457522015-02-27 18:21:29 +0800669 putchar('\n');
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500670 exit(130);
Ingo Molnar07800602009-04-20 15:00:56 +0200671 default: /* PARSE_OPT_UNKNOWN */
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600672 if (ctx.argv[0][1] == '-')
673 astrcatf(&error_buf, "unknown option `%s'",
674 ctx.argv[0] + 2);
675 else
676 astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
Ingo Molnar07800602009-04-20 15:00:56 +0200677 usage_with_options(usagestr, options);
678 }
679
680 return parse_options_end(&ctx);
681}
682
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -0500683int parse_options(int argc, const char **argv, const struct option *options,
684 const char * const usagestr[], int flags)
685{
686 return parse_options_subcommand(argc, argv, options, NULL,
687 (const char **) usagestr, flags);
688}
689
Ingo Molnar07800602009-04-20 15:00:56 +0200690#define USAGE_OPTS_WIDTH 24
691#define USAGE_GAP 2
692
Namhyung Kimac697622013-11-01 16:33:11 +0900693static void print_option_help(const struct option *opts, int full)
694{
695 size_t pos;
696 int pad;
697
698 if (opts->type == OPTION_GROUP) {
699 fputc('\n', stderr);
700 if (*opts->help)
701 fprintf(stderr, "%s\n", opts->help);
702 return;
703 }
704 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
705 return;
Namhyung Kimd152d1b2014-10-23 00:15:45 +0900706 if (opts->flags & PARSE_OPT_DISABLED)
707 return;
Namhyung Kimac697622013-11-01 16:33:11 +0900708
709 pos = fprintf(stderr, " ");
710 if (opts->short_name)
711 pos += fprintf(stderr, "-%c", opts->short_name);
712 else
713 pos += fprintf(stderr, " ");
714
715 if (opts->long_name && opts->short_name)
716 pos += fprintf(stderr, ", ");
717 if (opts->long_name)
718 pos += fprintf(stderr, "--%s", opts->long_name);
719
720 switch (opts->type) {
721 case OPTION_ARGUMENT:
722 break;
723 case OPTION_LONG:
Arnaldo Carvalho de Melo4ba8b3e2018-10-18 15:24:07 -0300724 case OPTION_ULONG:
Namhyung Kimac697622013-11-01 16:33:11 +0900725 case OPTION_U64:
726 case OPTION_INTEGER:
727 case OPTION_UINTEGER:
728 if (opts->flags & PARSE_OPT_OPTARG)
729 if (opts->long_name)
730 pos += fprintf(stderr, "[=<n>]");
731 else
732 pos += fprintf(stderr, "[<n>]");
733 else
734 pos += fprintf(stderr, " <n>");
735 break;
736 case OPTION_CALLBACK:
737 if (opts->flags & PARSE_OPT_NOARG)
738 break;
739 /* FALLTHROUGH */
740 case OPTION_STRING:
741 if (opts->argh) {
742 if (opts->flags & PARSE_OPT_OPTARG)
743 if (opts->long_name)
744 pos += fprintf(stderr, "[=<%s>]", opts->argh);
745 else
746 pos += fprintf(stderr, "[<%s>]", opts->argh);
747 else
748 pos += fprintf(stderr, " <%s>", opts->argh);
749 } else {
750 if (opts->flags & PARSE_OPT_OPTARG)
751 if (opts->long_name)
752 pos += fprintf(stderr, "[=...]");
753 else
754 pos += fprintf(stderr, "[...]");
755 else
756 pos += fprintf(stderr, " ...");
757 }
758 break;
759 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
760 case OPTION_END:
761 case OPTION_GROUP:
762 case OPTION_BIT:
763 case OPTION_BOOLEAN:
764 case OPTION_INCR:
765 case OPTION_SET_UINT:
766 case OPTION_SET_PTR:
767 break;
768 }
769
770 if (pos <= USAGE_OPTS_WIDTH)
771 pad = USAGE_OPTS_WIDTH - pos;
772 else {
773 fputc('\n', stderr);
774 pad = USAGE_OPTS_WIDTH;
775 }
776 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
Wang Nan48e1cab2015-12-14 10:39:22 +0000777 if (opts->flags & PARSE_OPT_NOBUILD)
778 fprintf(stderr, "%*s(not built-in because %s)\n",
779 USAGE_OPTS_WIDTH + USAGE_GAP, "",
780 opts->build_opt);
Namhyung Kimac697622013-11-01 16:33:11 +0900781}
782
Arnaldo Carvalho de Melo869c55b2015-10-23 11:23:28 -0300783static int option__cmp(const void *va, const void *vb)
784{
785 const struct option *a = va, *b = vb;
786 int sa = tolower(a->short_name), sb = tolower(b->short_name), ret;
787
788 if (sa == 0)
789 sa = 'z' + 1;
790 if (sb == 0)
791 sb = 'z' + 1;
792
793 ret = sa - sb;
794
795 if (ret == 0) {
796 const char *la = a->long_name ?: "",
797 *lb = b->long_name ?: "";
798 ret = strcmp(la, lb);
799 }
800
801 return ret;
802}
803
804static struct option *options__order(const struct option *opts)
805{
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -0600806 int nr_opts = 0, len;
Arnaldo Carvalho de Melo869c55b2015-10-23 11:23:28 -0300807 const struct option *o = opts;
808 struct option *ordered;
809
810 for (o = opts; o->type != OPTION_END; o++)
811 ++nr_opts;
812
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -0600813 len = sizeof(*o) * (nr_opts + 1);
814 ordered = malloc(len);
815 if (!ordered)
Arnaldo Carvalho de Melo869c55b2015-10-23 11:23:28 -0300816 goto out;
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -0600817 memcpy(ordered, opts, len);
Arnaldo Carvalho de Melo869c55b2015-10-23 11:23:28 -0300818
819 qsort(ordered, nr_opts, sizeof(*o), option__cmp);
820out:
821 return ordered;
822}
823
Arnaldo Carvalho de Melo161d9042015-10-23 13:27:39 -0300824static bool option__in_argv(const struct option *opt, const struct parse_opt_ctx_t *ctx)
825{
826 int i;
827
828 for (i = 1; i < ctx->argc; ++i) {
829 const char *arg = ctx->argv[i];
830
Arnaldo Carvalho de Melof4efcce2015-10-27 17:35:58 -0300831 if (arg[0] != '-') {
832 if (arg[1] == '\0') {
833 if (arg[0] == opt->short_name)
834 return true;
835 continue;
836 }
837
838 if (opt->long_name && strcmp(opt->long_name, arg) == 0)
839 return true;
840
841 if (opt->help && strcasestr(opt->help, arg) != NULL)
842 return true;
843
Arnaldo Carvalho de Melo161d9042015-10-23 13:27:39 -0300844 continue;
Arnaldo Carvalho de Melof4efcce2015-10-27 17:35:58 -0300845 }
Arnaldo Carvalho de Melo161d9042015-10-23 13:27:39 -0300846
847 if (arg[1] == opt->short_name ||
848 (arg[1] == '-' && opt->long_name && strcmp(opt->long_name, arg + 2) == 0))
849 return true;
850 }
851
852 return false;
853}
854
Josh Poimboeuf408cf342015-12-13 22:18:12 -0600855static int usage_with_options_internal(const char * const *usagestr,
856 const struct option *opts, int full,
857 struct parse_opt_ctx_t *ctx)
Ingo Molnar07800602009-04-20 15:00:56 +0200858{
Arnaldo Carvalho de Melo869c55b2015-10-23 11:23:28 -0300859 struct option *ordered;
860
Ingo Molnar07800602009-04-20 15:00:56 +0200861 if (!usagestr)
862 return PARSE_OPT_HELP;
863
Namhyung Kim01b19452015-10-25 00:49:26 +0900864 setup_pager();
865
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600866 if (error_buf) {
867 fprintf(stderr, " Error: %s\n", error_buf);
868 zfree(&error_buf);
Namhyung Kim01b19452015-10-25 00:49:26 +0900869 }
870
Yunlong Song3a134ae2015-10-15 15:39:51 +0800871 fprintf(stderr, "\n Usage: %s\n", *usagestr++);
Ingo Molnar07800602009-04-20 15:00:56 +0200872 while (*usagestr && **usagestr)
Ingo Molnar6e6b7542008-04-15 22:39:31 +0200873 fprintf(stderr, " or: %s\n", *usagestr++);
Ingo Molnar07800602009-04-20 15:00:56 +0200874 while (*usagestr) {
875 fprintf(stderr, "%s%s\n",
876 **usagestr ? " " : "",
877 *usagestr);
878 usagestr++;
879 }
880
881 if (opts->type != OPTION_GROUP)
882 fputc('\n', stderr);
883
Arnaldo Carvalho de Melo869c55b2015-10-23 11:23:28 -0300884 ordered = options__order(opts);
885 if (ordered)
886 opts = ordered;
887
Arnaldo Carvalho de Melo161d9042015-10-23 13:27:39 -0300888 for ( ; opts->type != OPTION_END; opts++) {
889 if (ctx && ctx->argc > 1 && !option__in_argv(opts, ctx))
890 continue;
Namhyung Kimac697622013-11-01 16:33:11 +0900891 print_option_help(opts, full);
Arnaldo Carvalho de Melo161d9042015-10-23 13:27:39 -0300892 }
Ingo Molnar07800602009-04-20 15:00:56 +0200893
Ingo Molnar07800602009-04-20 15:00:56 +0200894 fputc('\n', stderr);
895
Arnaldo Carvalho de Melo869c55b2015-10-23 11:23:28 -0300896 free(ordered);
897
Ingo Molnar07800602009-04-20 15:00:56 +0200898 return PARSE_OPT_HELP;
899}
900
901void usage_with_options(const char * const *usagestr,
902 const struct option *opts)
903{
Arnaldo Carvalho de Melo161d9042015-10-23 13:27:39 -0300904 usage_with_options_internal(usagestr, opts, 0, NULL);
Ingo Molnar07800602009-04-20 15:00:56 +0200905 exit(129);
906}
907
Namhyung Kimc7118362015-10-25 00:49:27 +0900908void usage_with_options_msg(const char * const *usagestr,
909 const struct option *opts, const char *fmt, ...)
910{
911 va_list ap;
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600912 char *tmp = error_buf;
Namhyung Kimc7118362015-10-25 00:49:27 +0900913
Namhyung Kimc7118362015-10-25 00:49:27 +0900914 va_start(ap, fmt);
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600915 if (vasprintf(&error_buf, fmt, ap) == -1)
916 die("vasprintf failed");
Namhyung Kimc7118362015-10-25 00:49:27 +0900917 va_end(ap);
918
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600919 free(tmp);
920
Namhyung Kimc7118362015-10-25 00:49:27 +0900921 usage_with_options_internal(usagestr, opts, 0, NULL);
922 exit(129);
923}
924
Ingo Molnar07800602009-04-20 15:00:56 +0200925int parse_options_usage(const char * const *usagestr,
Namhyung Kimac697622013-11-01 16:33:11 +0900926 const struct option *opts,
927 const char *optstr, bool short_opt)
Ingo Molnar07800602009-04-20 15:00:56 +0200928{
Namhyung Kimac697622013-11-01 16:33:11 +0900929 if (!usagestr)
Namhyung Kimcc03c542013-11-01 16:33:15 +0900930 goto opt;
Namhyung Kimac697622013-11-01 16:33:11 +0900931
Yunlong Song3a134ae2015-10-15 15:39:51 +0800932 fprintf(stderr, "\n Usage: %s\n", *usagestr++);
Namhyung Kimac697622013-11-01 16:33:11 +0900933 while (*usagestr && **usagestr)
934 fprintf(stderr, " or: %s\n", *usagestr++);
935 while (*usagestr) {
936 fprintf(stderr, "%s%s\n",
937 **usagestr ? " " : "",
938 *usagestr);
939 usagestr++;
940 }
941 fputc('\n', stderr);
942
Namhyung Kimcc03c542013-11-01 16:33:15 +0900943opt:
Namhyung Kimac697622013-11-01 16:33:11 +0900944 for ( ; opts->type != OPTION_END; opts++) {
945 if (short_opt) {
Namhyung Kima5f4a692015-10-25 00:49:24 +0900946 if (opts->short_name == *optstr) {
947 print_option_help(opts, 0);
Namhyung Kimac697622013-11-01 16:33:11 +0900948 break;
Namhyung Kima5f4a692015-10-25 00:49:24 +0900949 }
Namhyung Kimac697622013-11-01 16:33:11 +0900950 continue;
951 }
952
953 if (opts->long_name == NULL)
954 continue;
955
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300956 if (strstarts(opts->long_name, optstr))
Namhyung Kima5f4a692015-10-25 00:49:24 +0900957 print_option_help(opts, 0);
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300958 if (strstarts("no-", optstr) &&
959 strstarts(opts->long_name, optstr + 3))
Namhyung Kima5f4a692015-10-25 00:49:24 +0900960 print_option_help(opts, 0);
Namhyung Kimac697622013-11-01 16:33:11 +0900961 }
962
Namhyung Kimac697622013-11-01 16:33:11 +0900963 return PARSE_OPT_HELP;
Ingo Molnar07800602009-04-20 15:00:56 +0200964}
965
966
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300967int parse_opt_verbosity_cb(const struct option *opt,
968 const char *arg __maybe_unused,
Ingo Molnar07800602009-04-20 15:00:56 +0200969 int unset)
970{
971 int *target = opt->value;
972
973 if (unset)
974 /* --no-quiet, --no-verbose */
975 *target = 0;
976 else if (opt->short_name == 'v') {
977 if (*target >= 0)
978 (*target)++;
979 else
980 *target = 1;
981 } else {
982 if (*target <= 0)
983 (*target)--;
984 else
985 *target = -1;
986 }
987 return 0;
988}
Namhyung Kimd152d1b2014-10-23 00:15:45 +0900989
Wang Nan48e1cab2015-12-14 10:39:22 +0000990static struct option *
991find_option(struct option *opts, int shortopt, const char *longopt)
Namhyung Kimd152d1b2014-10-23 00:15:45 +0900992{
993 for (; opts->type != OPTION_END; opts++) {
994 if ((shortopt && opts->short_name == shortopt) ||
995 (opts->long_name && longopt &&
Wang Nan48e1cab2015-12-14 10:39:22 +0000996 !strcmp(opts->long_name, longopt)))
997 return opts;
Namhyung Kimd152d1b2014-10-23 00:15:45 +0900998 }
Wang Nan48e1cab2015-12-14 10:39:22 +0000999 return NULL;
1000}
1001
1002void set_option_flag(struct option *opts, int shortopt, const char *longopt,
1003 int flag)
1004{
1005 struct option *opt = find_option(opts, shortopt, longopt);
1006
1007 if (opt)
1008 opt->flags |= flag;
1009 return;
1010}
1011
1012void set_option_nobuild(struct option *opts, int shortopt,
1013 const char *longopt,
1014 const char *build_opt,
1015 bool can_skip)
1016{
1017 struct option *opt = find_option(opts, shortopt, longopt);
1018
1019 if (!opt)
1020 return;
1021
1022 opt->flags |= PARSE_OPT_NOBUILD;
1023 opt->flags |= can_skip ? PARSE_OPT_CANSKIP : 0;
1024 opt->build_opt = build_opt;
Namhyung Kimd152d1b2014-10-23 00:15:45 +09001025}