7 /* user defined includes */
8 #include <linux/types.h>
10 #include <kvm/parse-options.h>
11 #include <kvm/strbuf.h>
16 static int opterror(const struct option
*opt
, const char *reason
, int flags
)
18 if (flags
& OPT_SHORT
)
19 return pr_error("switch `%c' %s", opt
->short_name
, reason
);
20 if (flags
& OPT_UNSET
)
21 return pr_error("option `no-%s' %s", opt
->long_name
, reason
);
22 return pr_error("option `%s' %s", opt
->long_name
, reason
);
25 static int get_arg(struct parse_opt_ctx_t
*p
, const struct option
*opt
,
26 int flags
, const char **arg
)
31 } else if ((opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
) && (p
->argc
== 1 ||
32 **(p
->argv
+ 1) == '-')) {
33 *arg
= (const char *)opt
->defval
;
34 } else if (p
->argc
> 1) {
38 return opterror(opt
, "requires a value", flags
);
43 ((c) >= 'a' ? (c) - 'a' + 10 : \
44 (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
46 static u64
readhex(const char *str
, bool *error
)
48 char *pos
= strchr(str
, 'x') + 1;
52 unsigned int v
= numvalue(*pos
);
66 static int readnum(const struct option
*opt
, int flags
,
67 const char *str
, char **end
)
69 if (strchr(str
, 'x')) {
73 value
= readhex(str
, &error
);
79 *(int *)opt
->value
= value
;
82 *(unsigned int *)opt
->value
= value
;
85 *(long *)opt
->value
= value
;
88 *(u64
*)opt
->value
= value
;
96 *(int *)opt
->value
= strtol(str
, end
, 10);
99 *(unsigned int *)opt
->value
= strtol(str
, end
, 10);
102 *(long *)opt
->value
= strtol(str
, end
, 10);
105 *(u64
*)opt
->value
= strtoull(str
, end
, 10);
115 return opterror(opt
, "expects a numerical value", flags
);
117 return opterror(opt
, "invalid numeric conversion", flags
);
120 static int get_value(struct parse_opt_ctx_t
*p
,
121 const struct option
*opt
, int flags
)
123 const char *s
, *arg
= NULL
;
124 const int unset
= flags
& OPT_UNSET
;
127 return opterror(opt
, "takes no value", flags
);
128 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
129 return opterror(opt
, "isn't available", flags
);
131 if (!(flags
& OPT_SHORT
) && p
->opt
) {
133 case OPTION_CALLBACK
:
134 if (!(opt
->flags
& PARSE_OPT_NOARG
))
140 case OPTION_SET_UINT
:
142 return opterror(opt
, "takes no value", flags
);
144 case OPTION_ARGUMENT
:
148 case OPTION_UINTEGER
:
159 *(int *)opt
->value
&= ~opt
->defval
;
161 *(int *)opt
->value
|= opt
->defval
;
165 *(bool *)opt
->value
= unset
? false : true;
169 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
172 case OPTION_SET_UINT
:
173 *(unsigned int *)opt
->value
= unset
? 0 : opt
->defval
;
177 *(void **)opt
->value
= unset
? NULL
: (void *)opt
->defval
;
182 *(const char **)opt
->value
= NULL
;
183 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
184 *(const char **)opt
->value
= (const char *)opt
->defval
;
186 return get_arg(p
, opt
, flags
,
187 (const char **)opt
->value
);
190 case OPTION_CALLBACK
:
192 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
193 if (opt
->flags
& PARSE_OPT_NOARG
)
194 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
195 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
196 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
197 if (get_arg(p
, opt
, flags
, &arg
))
199 return (*opt
->callback
)(opt
, arg
, 0) ? (-1) : 0;
203 *(int *)opt
->value
= 0;
206 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
207 *(int *)opt
->value
= opt
->defval
;
210 if (get_arg(p
, opt
, flags
, &arg
))
212 return readnum(opt
, flags
, arg
, (char **)&s
);
214 case OPTION_UINTEGER
:
216 *(unsigned int *)opt
->value
= 0;
219 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
220 *(unsigned int *)opt
->value
= opt
->defval
;
223 if (get_arg(p
, opt
, flags
, &arg
))
225 return readnum(opt
, flags
, arg
, (char **)&s
);
229 *(long *)opt
->value
= 0;
232 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
233 *(long *)opt
->value
= opt
->defval
;
236 if (get_arg(p
, opt
, flags
, &arg
))
238 return readnum(opt
, flags
, arg
, (char **)&s
);
242 *(u64
*)opt
->value
= 0;
245 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
246 *(u64
*)opt
->value
= opt
->defval
;
249 if (get_arg(p
, opt
, flags
, &arg
))
251 return readnum(opt
, flags
, arg
, (char **)&s
);
254 case OPTION_ARGUMENT
:
257 die("should not happen, someone must be hit on the forehead");
261 #define USAGE_OPTS_WIDTH 24
264 static int usage_with_options_internal(const char * const *usagestr
,
265 const struct option
*opts
, int full
)
268 return PARSE_OPT_HELP
;
270 fprintf(stderr
, "\n usage: %s\n", *usagestr
++);
271 while (*usagestr
&& **usagestr
)
272 fprintf(stderr
, " or: %s\n", *usagestr
++);
274 fprintf(stderr
, "%s%s\n",
275 **usagestr
? " " : "",
280 if (opts
->type
!= OPTION_GROUP
)
283 for (; opts
->type
!= OPTION_END
; opts
++) {
287 if (opts
->type
== OPTION_GROUP
) {
290 fprintf(stderr
, "%s\n", opts
->help
);
293 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
296 pos
= fprintf(stderr
, " ");
297 if (opts
->short_name
)
298 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
300 pos
+= fprintf(stderr
, " ");
302 if (opts
->long_name
&& opts
->short_name
)
303 pos
+= fprintf(stderr
, ", ");
305 pos
+= fprintf(stderr
, "--%s", opts
->long_name
);
307 switch (opts
->type
) {
308 case OPTION_ARGUMENT
:
313 case OPTION_UINTEGER
:
314 if (opts
->flags
& PARSE_OPT_OPTARG
)
316 pos
+= fprintf(stderr
, "[=<n>]");
318 pos
+= fprintf(stderr
, "[<n>]");
320 pos
+= fprintf(stderr
, " <n>");
322 case OPTION_CALLBACK
:
323 if (opts
->flags
& PARSE_OPT_NOARG
)
328 if (opts
->flags
& PARSE_OPT_OPTARG
)
330 pos
+= fprintf(stderr
, "[=<%s>]", opts
->argh
);
332 pos
+= fprintf(stderr
, "[<%s>]", opts
->argh
);
334 pos
+= fprintf(stderr
, " <%s>", opts
->argh
);
336 if (opts
->flags
& PARSE_OPT_OPTARG
)
338 pos
+= fprintf(stderr
, "[=...]");
340 pos
+= fprintf(stderr
, "[...]");
342 pos
+= fprintf(stderr
, " ...");
345 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
351 case OPTION_SET_UINT
:
355 if (pos
<= USAGE_OPTS_WIDTH
)
356 pad
= USAGE_OPTS_WIDTH
- pos
;
359 pad
= USAGE_OPTS_WIDTH
;
361 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
365 return PARSE_OPT_HELP
;
368 void usage_with_options(const char * const *usagestr
,
369 const struct option
*opts
)
371 usage_with_options_internal(usagestr
, opts
, 0);
375 static void check_typos(const char *arg
, const struct option
*options
)
380 if (!prefixcmp(arg
, "no-")) {
381 pr_error ("did you mean `--%s` (with two dashes ?)", arg
);
385 for (; options
->type
!= OPTION_END
; options
++) {
386 if (!options
->long_name
)
388 if (!prefixcmp(options
->long_name
, arg
)) {
389 pr_error ("did you mean `--%s` (with two dashes ?)", arg
);
395 static int parse_options_usage(const char * const *usagestr
,
396 const struct option
*opts
)
398 return usage_with_options_internal(usagestr
, opts
, 0);
401 static int parse_short_opt(struct parse_opt_ctx_t
*p
,
402 const struct option
*options
)
404 for (; options
->type
!= OPTION_END
; options
++) {
405 if (options
->short_name
== *p
->opt
) {
406 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
407 return get_value(p
, options
, OPT_SHORT
);
413 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
414 const struct option
*options
)
416 const char *arg_end
= strchr(arg
, '=');
417 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
418 int abbrev_flags
= 0, ambiguous_flags
= 0;
421 arg_end
= arg
+ strlen(arg
);
423 for (; options
->type
!= OPTION_END
; options
++) {
427 if (!options
->long_name
)
430 rest
= skip_prefix(arg
, options
->long_name
);
431 if (options
->type
== OPTION_ARGUMENT
) {
435 return opterror(options
, "takes no value",
439 p
->out
[p
->cpidx
++] = arg
- 2;
444 if (!strncmp(options
->long_name
, arg
, arg_end
- arg
)) {
448 * If this is abbreviated, it is
449 * ambiguous. So when there is no
450 * exact match later, we need to
453 ambiguous_option
= abbrev_option
;
454 ambiguous_flags
= abbrev_flags
;
456 if (!(flags
& OPT_UNSET
) && *arg_end
)
457 p
->opt
= arg_end
+ 1;
458 abbrev_option
= options
;
459 abbrev_flags
= flags
;
462 /* negated and abbreviated very much? */
463 if (!prefixcmp("no-", arg
)) {
468 if (strncmp(arg
, "no-", 3))
471 rest
= skip_prefix(arg
+ 3, options
->long_name
);
472 /* abbreviated and negated? */
473 if (!rest
&& !prefixcmp(options
->long_name
, arg
+ 3))
483 return get_value(p
, options
, flags
);
486 if (ambiguous_option
)
487 return pr_error("Ambiguous option: %s "
488 "(could be --%s%s or --%s%s)",
490 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
491 ambiguous_option
->long_name
,
492 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
493 abbrev_option
->long_name
);
495 return get_value(p
, abbrev_option
, abbrev_flags
);
500 static void parse_options_start(struct parse_opt_ctx_t
*ctx
, int argc
,
501 const char **argv
, int flags
)
503 memset(ctx
, 0, sizeof(*ctx
));
507 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
509 if ((flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
510 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
))
511 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
514 static int parse_options_end(struct parse_opt_ctx_t
*ctx
)
516 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
517 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
518 return ctx
->cpidx
+ ctx
->argc
;
522 static int parse_options_step(struct parse_opt_ctx_t
*ctx
,
523 const struct option
*options
, const char * const usagestr
[])
525 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
527 /* we must reset ->opt, unknown short option leave it dangling */
530 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
531 const char *arg
= ctx
->argv
[0];
533 if (*arg
!= '-' || !arg
[1]) {
534 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
536 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
542 if (internal_help
&& *ctx
->opt
== 'h')
543 return parse_options_usage(usagestr
, options
);
544 switch (parse_short_opt(ctx
, options
)) {
546 return parse_options_usage(usagestr
, options
);
553 check_typos(arg
+ 1, options
);
555 if (internal_help
&& *ctx
->opt
== 'h')
556 return parse_options_usage(usagestr
,
558 switch (parse_short_opt(ctx
, options
)) {
560 return parse_options_usage(usagestr
,
563 /* fake a short option thing to hide
564 * the fact that we may have
565 * started to parse aggregated stuff
567 * This is leaky, too bad.
569 ctx
->argv
[0] = strdup(ctx
->opt
- 1);
570 *(char *)ctx
->argv
[0] = '-';
579 if (!arg
[2]) { /* "--" */
580 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
587 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
588 return usage_with_options_internal(usagestr
, options
,
590 if (internal_help
&& !strcmp(arg
+ 2, "help"))
591 return parse_options_usage(usagestr
, options
);
592 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
594 return parse_options_usage(usagestr
, options
);
602 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
603 return PARSE_OPT_UNKNOWN
;
604 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
607 return PARSE_OPT_DONE
;
610 int parse_options(int argc
, const char **argv
, const struct option
*options
,
611 const char * const usagestr
[], int flags
)
613 struct parse_opt_ctx_t ctx
;
615 parse_options_start(&ctx
, argc
, argv
, flags
);
616 switch (parse_options_step(&ctx
, options
, usagestr
)) {
621 default: /* PARSE_OPT_UNKNOWN */
622 if (ctx
.argv
[0][1] == '-') {
623 pr_error("unknown option `%s'", ctx
.argv
[0] + 2);
625 pr_error("unknown switch `%c'", *ctx
.opt
);
627 usage_with_options(usagestr
, options
);
630 return parse_options_end(&ctx
);