Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 1 | #include <linux/compiler.h> |
| 2 | #include <linux/types.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdint.h> |
| 6 | #include <string.h> |
| 7 | #include <ctype.h> |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 8 | #include "subcmd-util.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 9 | #include "parse-options.h" |
Josh Poimboeuf | 096d355 | 2015-12-15 09:39:35 -0600 | [diff] [blame] | 10 | #include "subcmd-config.h" |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 11 | #include "pager.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 12 | |
| 13 | #define OPT_SHORT 1 |
| 14 | #define OPT_UNSET 2 |
| 15 | |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 16 | char *error_buf; |
Namhyung Kim | 01b1945 | 2015-10-25 00:49:26 +0900 | [diff] [blame] | 17 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 18 | static int opterror(const struct option *opt, const char *reason, int flags) |
| 19 | { |
| 20 | if (flags & OPT_SHORT) |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 21 | fprintf(stderr, " Error: switch `%c' %s", opt->short_name, reason); |
| 22 | else if (flags & OPT_UNSET) |
| 23 | fprintf(stderr, " Error: option `no-%s' %s", opt->long_name, reason); |
| 24 | else |
| 25 | fprintf(stderr, " Error: option `%s' %s", opt->long_name, reason); |
| 26 | |
| 27 | return -1; |
| 28 | } |
| 29 | |
| 30 | static const char *skip_prefix(const char *str, const char *prefix) |
| 31 | { |
| 32 | size_t len = strlen(prefix); |
| 33 | return strncmp(str, prefix, len) ? NULL : str + len; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 34 | } |
| 35 | |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 36 | static void optwarning(const struct option *opt, const char *reason, int flags) |
| 37 | { |
| 38 | if (flags & OPT_SHORT) |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 39 | fprintf(stderr, " Warning: switch `%c' %s", opt->short_name, reason); |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 40 | else if (flags & OPT_UNSET) |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 41 | fprintf(stderr, " Warning: option `no-%s' %s", opt->long_name, reason); |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 42 | else |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 43 | fprintf(stderr, " Warning: option `%s' %s", opt->long_name, reason); |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 46 | static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt, |
| 47 | int flags, const char **arg) |
| 48 | { |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 49 | const char *res; |
| 50 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 51 | if (p->opt) { |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 52 | res = p->opt; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 53 | p->opt = NULL; |
Frederic Weisbecker | 5a4b181 | 2009-07-02 17:58:20 +0200 | [diff] [blame] | 54 | } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 || |
| 55 | **(p->argv + 1) == '-')) { |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 56 | res = (const char *)opt->defval; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 57 | } else if (p->argc > 1) { |
| 58 | p->argc--; |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 59 | res = *++p->argv; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 60 | } else |
| 61 | return opterror(opt, "requires a value", flags); |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 62 | if (arg) |
| 63 | *arg = res; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int get_value(struct parse_opt_ctx_t *p, |
| 68 | const struct option *opt, int flags) |
| 69 | { |
Ingo Molnar | 233f0b9 | 2009-06-03 21:48:40 +0200 | [diff] [blame] | 70 | const char *s, *arg = NULL; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 71 | const int unset = flags & OPT_UNSET; |
Wang Nan | 0c8c207 | 2015-03-13 12:51:54 +0000 | [diff] [blame] | 72 | int err; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 73 | |
| 74 | if (unset && p->opt) |
| 75 | return opterror(opt, "takes no value", flags); |
| 76 | if (unset && (opt->flags & PARSE_OPT_NONEG)) |
| 77 | return opterror(opt, "isn't available", flags); |
Namhyung Kim | d152d1b | 2014-10-23 00:15:45 +0900 | [diff] [blame] | 78 | if (opt->flags & PARSE_OPT_DISABLED) |
| 79 | return opterror(opt, "is not usable", flags); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 80 | |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 81 | if (opt->flags & PARSE_OPT_EXCLUSIVE) { |
Namhyung Kim | 5594b55 | 2015-01-10 19:33:45 +0900 | [diff] [blame] | 82 | if (p->excl_opt && p->excl_opt != opt) { |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 83 | char msg[128]; |
| 84 | |
| 85 | if (((flags & OPT_SHORT) && p->excl_opt->short_name) || |
| 86 | p->excl_opt->long_name == NULL) { |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 87 | snprintf(msg, sizeof(msg), "cannot be used with switch `%c'", |
| 88 | p->excl_opt->short_name); |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 89 | } else { |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 90 | snprintf(msg, sizeof(msg), "cannot be used with %s", |
| 91 | p->excl_opt->long_name); |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 92 | } |
| 93 | opterror(opt, msg, flags); |
| 94 | return -3; |
| 95 | } |
| 96 | p->excl_opt = opt; |
| 97 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 98 | if (!(flags & OPT_SHORT) && p->opt) { |
| 99 | switch (opt->type) { |
| 100 | case OPTION_CALLBACK: |
| 101 | if (!(opt->flags & PARSE_OPT_NOARG)) |
| 102 | break; |
| 103 | /* FALLTHROUGH */ |
| 104 | case OPTION_BOOLEAN: |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 105 | case OPTION_INCR: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 106 | case OPTION_BIT: |
Arnaldo Carvalho de Melo | edb7c60 | 2010-05-17 16:22:41 -0300 | [diff] [blame] | 107 | case OPTION_SET_UINT: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 108 | case OPTION_SET_PTR: |
| 109 | return opterror(opt, "takes no value", flags); |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 110 | case OPTION_END: |
| 111 | case OPTION_ARGUMENT: |
| 112 | case OPTION_GROUP: |
| 113 | case OPTION_STRING: |
| 114 | case OPTION_INTEGER: |
Arnaldo Carvalho de Melo | c100edb | 2010-05-17 15:30:00 -0300 | [diff] [blame] | 115 | case OPTION_UINTEGER: |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 116 | case OPTION_LONG: |
Arnaldo Carvalho de Melo | 6ba85ce | 2010-05-17 12:16:48 -0300 | [diff] [blame] | 117 | case OPTION_U64: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 123 | if (opt->flags & PARSE_OPT_NOBUILD) { |
| 124 | char reason[128]; |
| 125 | bool noarg = false; |
| 126 | |
| 127 | err = snprintf(reason, sizeof(reason), |
| 128 | opt->flags & PARSE_OPT_CANSKIP ? |
| 129 | "is being ignored because %s " : |
| 130 | "is not available because %s", |
| 131 | opt->build_opt); |
| 132 | reason[sizeof(reason) - 1] = '\0'; |
| 133 | |
| 134 | if (err < 0) |
| 135 | strncpy(reason, opt->flags & PARSE_OPT_CANSKIP ? |
| 136 | "is being ignored" : |
| 137 | "is not available", |
| 138 | sizeof(reason)); |
| 139 | |
| 140 | if (!(opt->flags & PARSE_OPT_CANSKIP)) |
| 141 | return opterror(opt, reason, flags); |
| 142 | |
| 143 | err = 0; |
| 144 | if (unset) |
| 145 | noarg = true; |
| 146 | if (opt->flags & PARSE_OPT_NOARG) |
| 147 | noarg = true; |
| 148 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
| 149 | noarg = true; |
| 150 | |
| 151 | switch (opt->type) { |
| 152 | case OPTION_BOOLEAN: |
| 153 | case OPTION_INCR: |
| 154 | case OPTION_BIT: |
| 155 | case OPTION_SET_UINT: |
| 156 | case OPTION_SET_PTR: |
| 157 | case OPTION_END: |
| 158 | case OPTION_ARGUMENT: |
| 159 | case OPTION_GROUP: |
| 160 | noarg = true; |
| 161 | break; |
| 162 | case OPTION_CALLBACK: |
| 163 | case OPTION_STRING: |
| 164 | case OPTION_INTEGER: |
| 165 | case OPTION_UINTEGER: |
| 166 | case OPTION_LONG: |
| 167 | case OPTION_U64: |
| 168 | default: |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | if (!noarg) |
| 173 | err = get_arg(p, opt, flags, NULL); |
| 174 | if (err) |
| 175 | return err; |
| 176 | |
| 177 | optwarning(opt, reason, flags); |
| 178 | return 0; |
| 179 | } |
| 180 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 181 | switch (opt->type) { |
| 182 | case OPTION_BIT: |
| 183 | if (unset) |
| 184 | *(int *)opt->value &= ~opt->defval; |
| 185 | else |
| 186 | *(int *)opt->value |= opt->defval; |
| 187 | return 0; |
| 188 | |
| 189 | case OPTION_BOOLEAN: |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 190 | *(bool *)opt->value = unset ? false : true; |
Adrian Hunter | 167faf3 | 2013-11-18 11:55:56 +0200 | [diff] [blame] | 191 | if (opt->set) |
| 192 | *(bool *)opt->set = true; |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 193 | return 0; |
| 194 | |
| 195 | case OPTION_INCR: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 196 | *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1; |
| 197 | return 0; |
| 198 | |
Arnaldo Carvalho de Melo | edb7c60 | 2010-05-17 16:22:41 -0300 | [diff] [blame] | 199 | case OPTION_SET_UINT: |
| 200 | *(unsigned int *)opt->value = unset ? 0 : opt->defval; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 201 | return 0; |
| 202 | |
| 203 | case OPTION_SET_PTR: |
| 204 | *(void **)opt->value = unset ? NULL : (void *)opt->defval; |
| 205 | return 0; |
| 206 | |
| 207 | case OPTION_STRING: |
Wang Nan | 0c8c207 | 2015-03-13 12:51:54 +0000 | [diff] [blame] | 208 | err = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 209 | if (unset) |
| 210 | *(const char **)opt->value = NULL; |
| 211 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
| 212 | *(const char **)opt->value = (const char *)opt->defval; |
| 213 | else |
Wang Nan | 0c8c207 | 2015-03-13 12:51:54 +0000 | [diff] [blame] | 214 | err = get_arg(p, opt, flags, (const char **)opt->value); |
| 215 | |
| 216 | /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */ |
| 217 | if (opt->flags & PARSE_OPT_NOEMPTY) { |
| 218 | const char *val = *(const char **)opt->value; |
| 219 | |
| 220 | if (!val) |
| 221 | return err; |
| 222 | |
| 223 | /* Similar to unset if we are given an empty string. */ |
| 224 | if (val[0] == '\0') { |
| 225 | *(const char **)opt->value = NULL; |
| 226 | return 0; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return err; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 231 | |
| 232 | case OPTION_CALLBACK: |
| 233 | if (unset) |
| 234 | return (*opt->callback)(opt, NULL, 1) ? (-1) : 0; |
| 235 | if (opt->flags & PARSE_OPT_NOARG) |
| 236 | return (*opt->callback)(opt, NULL, 0) ? (-1) : 0; |
| 237 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
| 238 | return (*opt->callback)(opt, NULL, 0) ? (-1) : 0; |
| 239 | if (get_arg(p, opt, flags, &arg)) |
| 240 | return -1; |
| 241 | return (*opt->callback)(opt, arg, 0) ? (-1) : 0; |
| 242 | |
| 243 | case OPTION_INTEGER: |
| 244 | if (unset) { |
| 245 | *(int *)opt->value = 0; |
| 246 | return 0; |
| 247 | } |
| 248 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
| 249 | *(int *)opt->value = opt->defval; |
| 250 | return 0; |
| 251 | } |
| 252 | if (get_arg(p, opt, flags, &arg)) |
| 253 | return -1; |
| 254 | *(int *)opt->value = strtol(arg, (char **)&s, 10); |
| 255 | if (*s) |
| 256 | return opterror(opt, "expects a numerical value", flags); |
| 257 | return 0; |
| 258 | |
Arnaldo Carvalho de Melo | c100edb | 2010-05-17 15:30:00 -0300 | [diff] [blame] | 259 | case OPTION_UINTEGER: |
| 260 | if (unset) { |
| 261 | *(unsigned int *)opt->value = 0; |
| 262 | return 0; |
| 263 | } |
| 264 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
| 265 | *(unsigned int *)opt->value = opt->defval; |
| 266 | return 0; |
| 267 | } |
| 268 | if (get_arg(p, opt, flags, &arg)) |
| 269 | return -1; |
| 270 | *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10); |
| 271 | if (*s) |
| 272 | return opterror(opt, "expects a numerical value", flags); |
| 273 | return 0; |
| 274 | |
Peter Zijlstra | e61078a | 2009-06-03 11:24:33 +0200 | [diff] [blame] | 275 | case OPTION_LONG: |
| 276 | if (unset) { |
| 277 | *(long *)opt->value = 0; |
| 278 | return 0; |
| 279 | } |
| 280 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
| 281 | *(long *)opt->value = opt->defval; |
| 282 | return 0; |
| 283 | } |
| 284 | if (get_arg(p, opt, flags, &arg)) |
| 285 | return -1; |
| 286 | *(long *)opt->value = strtol(arg, (char **)&s, 10); |
| 287 | if (*s) |
| 288 | return opterror(opt, "expects a numerical value", flags); |
| 289 | return 0; |
| 290 | |
Arnaldo Carvalho de Melo | 6ba85ce | 2010-05-17 12:16:48 -0300 | [diff] [blame] | 291 | case OPTION_U64: |
| 292 | if (unset) { |
| 293 | *(u64 *)opt->value = 0; |
| 294 | return 0; |
| 295 | } |
| 296 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
| 297 | *(u64 *)opt->value = opt->defval; |
| 298 | return 0; |
| 299 | } |
| 300 | if (get_arg(p, opt, flags, &arg)) |
| 301 | return -1; |
| 302 | *(u64 *)opt->value = strtoull(arg, (char **)&s, 10); |
| 303 | if (*s) |
| 304 | return opterror(opt, "expects a numerical value", flags); |
| 305 | return 0; |
| 306 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 307 | case OPTION_END: |
| 308 | case OPTION_ARGUMENT: |
| 309 | case OPTION_GROUP: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 310 | default: |
| 311 | die("should not happen, someone must be hit on the forehead"); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options) |
| 316 | { |
| 317 | for (; options->type != OPTION_END; options++) { |
| 318 | if (options->short_name == *p->opt) { |
| 319 | p->opt = p->opt[1] ? p->opt + 1 : NULL; |
| 320 | return get_value(p, options, OPT_SHORT); |
| 321 | } |
| 322 | } |
| 323 | return -2; |
| 324 | } |
| 325 | |
| 326 | static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg, |
| 327 | const struct option *options) |
| 328 | { |
| 329 | const char *arg_end = strchr(arg, '='); |
| 330 | const struct option *abbrev_option = NULL, *ambiguous_option = NULL; |
| 331 | int abbrev_flags = 0, ambiguous_flags = 0; |
| 332 | |
| 333 | if (!arg_end) |
| 334 | arg_end = arg + strlen(arg); |
| 335 | |
| 336 | for (; options->type != OPTION_END; options++) { |
| 337 | const char *rest; |
| 338 | int flags = 0; |
| 339 | |
| 340 | if (!options->long_name) |
| 341 | continue; |
| 342 | |
| 343 | rest = skip_prefix(arg, options->long_name); |
| 344 | if (options->type == OPTION_ARGUMENT) { |
| 345 | if (!rest) |
| 346 | continue; |
| 347 | if (*rest == '=') |
| 348 | return opterror(options, "takes no value", flags); |
| 349 | if (*rest) |
| 350 | continue; |
| 351 | p->out[p->cpidx++] = arg - 2; |
| 352 | return 0; |
| 353 | } |
| 354 | if (!rest) { |
Adrian Hunter | 4bc4379 | 2013-11-18 11:55:55 +0200 | [diff] [blame] | 355 | if (!prefixcmp(options->long_name, "no-")) { |
| 356 | /* |
| 357 | * The long name itself starts with "no-", so |
| 358 | * accept the option without "no-" so that users |
| 359 | * do not have to enter "no-no-" to get the |
| 360 | * negation. |
| 361 | */ |
| 362 | rest = skip_prefix(arg, options->long_name + 3); |
| 363 | if (rest) { |
| 364 | flags |= OPT_UNSET; |
| 365 | goto match; |
| 366 | } |
| 367 | /* Abbreviated case */ |
| 368 | if (!prefixcmp(options->long_name + 3, arg)) { |
| 369 | flags |= OPT_UNSET; |
| 370 | goto is_abbreviated; |
| 371 | } |
| 372 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 373 | /* abbreviated? */ |
| 374 | if (!strncmp(options->long_name, arg, arg_end - arg)) { |
| 375 | is_abbreviated: |
| 376 | if (abbrev_option) { |
| 377 | /* |
| 378 | * If this is abbreviated, it is |
| 379 | * ambiguous. So when there is no |
| 380 | * exact match later, we need to |
| 381 | * error out. |
| 382 | */ |
| 383 | ambiguous_option = abbrev_option; |
| 384 | ambiguous_flags = abbrev_flags; |
| 385 | } |
| 386 | if (!(flags & OPT_UNSET) && *arg_end) |
| 387 | p->opt = arg_end + 1; |
| 388 | abbrev_option = options; |
| 389 | abbrev_flags = flags; |
| 390 | continue; |
| 391 | } |
| 392 | /* negated and abbreviated very much? */ |
| 393 | if (!prefixcmp("no-", arg)) { |
| 394 | flags |= OPT_UNSET; |
| 395 | goto is_abbreviated; |
| 396 | } |
| 397 | /* negated? */ |
| 398 | if (strncmp(arg, "no-", 3)) |
| 399 | continue; |
| 400 | flags |= OPT_UNSET; |
| 401 | rest = skip_prefix(arg + 3, options->long_name); |
| 402 | /* abbreviated and negated? */ |
| 403 | if (!rest && !prefixcmp(options->long_name, arg + 3)) |
| 404 | goto is_abbreviated; |
| 405 | if (!rest) |
| 406 | continue; |
| 407 | } |
Adrian Hunter | 4bc4379 | 2013-11-18 11:55:55 +0200 | [diff] [blame] | 408 | match: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 409 | if (*rest) { |
| 410 | if (*rest != '=') |
| 411 | continue; |
| 412 | p->opt = rest + 1; |
| 413 | } |
| 414 | return get_value(p, options, flags); |
| 415 | } |
| 416 | |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 417 | if (ambiguous_option) { |
| 418 | fprintf(stderr, |
| 419 | " Error: Ambiguous option: %s (could be --%s%s or --%s%s)", |
| 420 | arg, |
| 421 | (ambiguous_flags & OPT_UNSET) ? "no-" : "", |
| 422 | ambiguous_option->long_name, |
| 423 | (abbrev_flags & OPT_UNSET) ? "no-" : "", |
| 424 | abbrev_option->long_name); |
| 425 | return -1; |
| 426 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 427 | if (abbrev_option) |
| 428 | return get_value(p, abbrev_option, abbrev_flags); |
| 429 | return -2; |
| 430 | } |
| 431 | |
| 432 | static void check_typos(const char *arg, const struct option *options) |
| 433 | { |
| 434 | if (strlen(arg) < 3) |
| 435 | return; |
| 436 | |
| 437 | if (!prefixcmp(arg, "no-")) { |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 438 | fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 439 | exit(129); |
| 440 | } |
| 441 | |
| 442 | for (; options->type != OPTION_END; options++) { |
| 443 | if (!options->long_name) |
| 444 | continue; |
| 445 | if (!prefixcmp(options->long_name, arg)) { |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 446 | fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 447 | exit(129); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
Josh Poimboeuf | 408cf34 | 2015-12-13 22:18:12 -0600 | [diff] [blame] | 452 | static void parse_options_start(struct parse_opt_ctx_t *ctx, |
| 453 | int argc, const char **argv, int flags) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 454 | { |
| 455 | memset(ctx, 0, sizeof(*ctx)); |
| 456 | ctx->argc = argc - 1; |
| 457 | ctx->argv = argv + 1; |
| 458 | ctx->out = argv; |
| 459 | ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0); |
| 460 | ctx->flags = flags; |
| 461 | if ((flags & PARSE_OPT_KEEP_UNKNOWN) && |
| 462 | (flags & PARSE_OPT_STOP_AT_NON_OPTION)) |
| 463 | die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together"); |
| 464 | } |
| 465 | |
| 466 | static int usage_with_options_internal(const char * const *, |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 467 | const struct option *, int, |
| 468 | struct parse_opt_ctx_t *); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 469 | |
Josh Poimboeuf | 408cf34 | 2015-12-13 22:18:12 -0600 | [diff] [blame] | 470 | static int parse_options_step(struct parse_opt_ctx_t *ctx, |
| 471 | const struct option *options, |
| 472 | const char * const usagestr[]) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 473 | { |
| 474 | int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP); |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 475 | int excl_short_opt = 1; |
| 476 | const char *arg; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 477 | |
| 478 | /* we must reset ->opt, unknown short option leave it dangling */ |
| 479 | ctx->opt = NULL; |
| 480 | |
| 481 | for (; ctx->argc; ctx->argc--, ctx->argv++) { |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 482 | arg = ctx->argv[0]; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 483 | if (*arg != '-' || !arg[1]) { |
| 484 | if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION) |
| 485 | break; |
| 486 | ctx->out[ctx->cpidx++] = ctx->argv[0]; |
| 487 | continue; |
| 488 | } |
| 489 | |
| 490 | if (arg[1] != '-') { |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 491 | ctx->opt = ++arg; |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 492 | if (internal_help && *ctx->opt == 'h') { |
| 493 | return usage_with_options_internal(usagestr, options, 0, ctx); |
| 494 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 495 | switch (parse_short_opt(ctx, options)) { |
| 496 | case -1: |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 497 | return parse_options_usage(usagestr, options, arg, 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 498 | case -2: |
| 499 | goto unknown; |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 500 | case -3: |
| 501 | goto exclusive; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 502 | default: |
| 503 | break; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 504 | } |
| 505 | if (ctx->opt) |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 506 | check_typos(arg, options); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 507 | while (ctx->opt) { |
| 508 | if (internal_help && *ctx->opt == 'h') |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 509 | return usage_with_options_internal(usagestr, options, 0, ctx); |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 510 | arg = ctx->opt; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 511 | switch (parse_short_opt(ctx, options)) { |
| 512 | case -1: |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 513 | return parse_options_usage(usagestr, options, arg, 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 514 | case -2: |
| 515 | /* fake a short option thing to hide the fact that we may have |
| 516 | * started to parse aggregated stuff |
| 517 | * |
| 518 | * This is leaky, too bad. |
| 519 | */ |
| 520 | ctx->argv[0] = strdup(ctx->opt - 1); |
| 521 | *(char *)ctx->argv[0] = '-'; |
| 522 | goto unknown; |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 523 | case -3: |
| 524 | goto exclusive; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 525 | default: |
| 526 | break; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | continue; |
| 530 | } |
| 531 | |
| 532 | if (!arg[2]) { /* "--" */ |
| 533 | if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) { |
| 534 | ctx->argc--; |
| 535 | ctx->argv++; |
| 536 | } |
| 537 | break; |
| 538 | } |
| 539 | |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 540 | arg += 2; |
| 541 | if (internal_help && !strcmp(arg, "help-all")) |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 542 | return usage_with_options_internal(usagestr, options, 1, ctx); |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 543 | if (internal_help && !strcmp(arg, "help")) |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 544 | return usage_with_options_internal(usagestr, options, 0, ctx); |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 545 | if (!strcmp(arg, "list-opts")) |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 546 | return PARSE_OPT_LIST_OPTS; |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 547 | if (!strcmp(arg, "list-cmds")) |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 548 | return PARSE_OPT_LIST_SUBCMDS; |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 549 | switch (parse_long_opt(ctx, arg, options)) { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 550 | case -1: |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 551 | return parse_options_usage(usagestr, options, arg, 0); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 552 | case -2: |
| 553 | goto unknown; |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 554 | case -3: |
| 555 | excl_short_opt = 0; |
| 556 | goto exclusive; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 557 | default: |
| 558 | break; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 559 | } |
| 560 | continue; |
| 561 | unknown: |
| 562 | if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN)) |
| 563 | return PARSE_OPT_UNKNOWN; |
| 564 | ctx->out[ctx->cpidx++] = ctx->argv[0]; |
| 565 | ctx->opt = NULL; |
| 566 | } |
| 567 | return PARSE_OPT_DONE; |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 568 | |
| 569 | exclusive: |
| 570 | parse_options_usage(usagestr, options, arg, excl_short_opt); |
| 571 | if ((excl_short_opt && ctx->excl_opt->short_name) || |
| 572 | ctx->excl_opt->long_name == NULL) { |
| 573 | char opt = ctx->excl_opt->short_name; |
| 574 | parse_options_usage(NULL, options, &opt, 1); |
| 575 | } else { |
| 576 | parse_options_usage(NULL, options, ctx->excl_opt->long_name, 0); |
| 577 | } |
| 578 | return PARSE_OPT_HELP; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Josh Poimboeuf | 408cf34 | 2015-12-13 22:18:12 -0600 | [diff] [blame] | 581 | static int parse_options_end(struct parse_opt_ctx_t *ctx) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 582 | { |
| 583 | memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out)); |
| 584 | ctx->out[ctx->cpidx + ctx->argc] = NULL; |
| 585 | return ctx->cpidx + ctx->argc; |
| 586 | } |
| 587 | |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 588 | int parse_options_subcommand(int argc, const char **argv, const struct option *options, |
| 589 | const char *const subcommands[], const char *usagestr[], int flags) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 590 | { |
| 591 | struct parse_opt_ctx_t ctx; |
| 592 | |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 593 | /* build usage string if it's not provided */ |
| 594 | if (subcommands && !usagestr[0]) { |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 595 | char *buf = NULL; |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 596 | |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 597 | astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]); |
| 598 | |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 599 | for (int i = 0; subcommands[i]; i++) { |
| 600 | if (i) |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 601 | astrcat(&buf, "|"); |
| 602 | astrcat(&buf, subcommands[i]); |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 603 | } |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 604 | astrcat(&buf, "}"); |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 605 | |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 606 | usagestr[0] = buf; |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 607 | } |
| 608 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 609 | parse_options_start(&ctx, argc, argv, flags); |
| 610 | switch (parse_options_step(&ctx, options, usagestr)) { |
| 611 | case PARSE_OPT_HELP: |
| 612 | exit(129); |
| 613 | case PARSE_OPT_DONE: |
| 614 | break; |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 615 | case PARSE_OPT_LIST_OPTS: |
Namhyung Kim | 4d8061f | 2012-10-03 00:21:34 +0900 | [diff] [blame] | 616 | while (options->type != OPTION_END) { |
Yunlong Song | 3ef1e65 | 2015-02-27 18:21:30 +0800 | [diff] [blame] | 617 | if (options->long_name) |
| 618 | printf("--%s ", options->long_name); |
Namhyung Kim | 4d8061f | 2012-10-03 00:21:34 +0900 | [diff] [blame] | 619 | options++; |
| 620 | } |
Yunlong Song | ed45752 | 2015-02-27 18:21:29 +0800 | [diff] [blame] | 621 | putchar('\n'); |
Namhyung Kim | 4d8061f | 2012-10-03 00:21:34 +0900 | [diff] [blame] | 622 | exit(130); |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 623 | case PARSE_OPT_LIST_SUBCMDS: |
Yunlong Song | 3a03005 | 2015-02-13 21:11:52 +0800 | [diff] [blame] | 624 | if (subcommands) { |
| 625 | for (int i = 0; subcommands[i]; i++) |
| 626 | printf("%s ", subcommands[i]); |
| 627 | } |
Yunlong Song | ed45752 | 2015-02-27 18:21:29 +0800 | [diff] [blame] | 628 | putchar('\n'); |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 629 | exit(130); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 630 | default: /* PARSE_OPT_UNKNOWN */ |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 631 | if (ctx.argv[0][1] == '-') |
| 632 | astrcatf(&error_buf, "unknown option `%s'", |
| 633 | ctx.argv[0] + 2); |
| 634 | else |
| 635 | astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 636 | usage_with_options(usagestr, options); |
| 637 | } |
| 638 | |
| 639 | return parse_options_end(&ctx); |
| 640 | } |
| 641 | |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 642 | int parse_options(int argc, const char **argv, const struct option *options, |
| 643 | const char * const usagestr[], int flags) |
| 644 | { |
| 645 | return parse_options_subcommand(argc, argv, options, NULL, |
| 646 | (const char **) usagestr, flags); |
| 647 | } |
| 648 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 649 | #define USAGE_OPTS_WIDTH 24 |
| 650 | #define USAGE_GAP 2 |
| 651 | |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 652 | static void print_option_help(const struct option *opts, int full) |
| 653 | { |
| 654 | size_t pos; |
| 655 | int pad; |
| 656 | |
| 657 | if (opts->type == OPTION_GROUP) { |
| 658 | fputc('\n', stderr); |
| 659 | if (*opts->help) |
| 660 | fprintf(stderr, "%s\n", opts->help); |
| 661 | return; |
| 662 | } |
| 663 | if (!full && (opts->flags & PARSE_OPT_HIDDEN)) |
| 664 | return; |
Namhyung Kim | d152d1b | 2014-10-23 00:15:45 +0900 | [diff] [blame] | 665 | if (opts->flags & PARSE_OPT_DISABLED) |
| 666 | return; |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 667 | |
| 668 | pos = fprintf(stderr, " "); |
| 669 | if (opts->short_name) |
| 670 | pos += fprintf(stderr, "-%c", opts->short_name); |
| 671 | else |
| 672 | pos += fprintf(stderr, " "); |
| 673 | |
| 674 | if (opts->long_name && opts->short_name) |
| 675 | pos += fprintf(stderr, ", "); |
| 676 | if (opts->long_name) |
| 677 | pos += fprintf(stderr, "--%s", opts->long_name); |
| 678 | |
| 679 | switch (opts->type) { |
| 680 | case OPTION_ARGUMENT: |
| 681 | break; |
| 682 | case OPTION_LONG: |
| 683 | case OPTION_U64: |
| 684 | case OPTION_INTEGER: |
| 685 | case OPTION_UINTEGER: |
| 686 | if (opts->flags & PARSE_OPT_OPTARG) |
| 687 | if (opts->long_name) |
| 688 | pos += fprintf(stderr, "[=<n>]"); |
| 689 | else |
| 690 | pos += fprintf(stderr, "[<n>]"); |
| 691 | else |
| 692 | pos += fprintf(stderr, " <n>"); |
| 693 | break; |
| 694 | case OPTION_CALLBACK: |
| 695 | if (opts->flags & PARSE_OPT_NOARG) |
| 696 | break; |
| 697 | /* FALLTHROUGH */ |
| 698 | case OPTION_STRING: |
| 699 | if (opts->argh) { |
| 700 | if (opts->flags & PARSE_OPT_OPTARG) |
| 701 | if (opts->long_name) |
| 702 | pos += fprintf(stderr, "[=<%s>]", opts->argh); |
| 703 | else |
| 704 | pos += fprintf(stderr, "[<%s>]", opts->argh); |
| 705 | else |
| 706 | pos += fprintf(stderr, " <%s>", opts->argh); |
| 707 | } else { |
| 708 | if (opts->flags & PARSE_OPT_OPTARG) |
| 709 | if (opts->long_name) |
| 710 | pos += fprintf(stderr, "[=...]"); |
| 711 | else |
| 712 | pos += fprintf(stderr, "[...]"); |
| 713 | else |
| 714 | pos += fprintf(stderr, " ..."); |
| 715 | } |
| 716 | break; |
| 717 | default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */ |
| 718 | case OPTION_END: |
| 719 | case OPTION_GROUP: |
| 720 | case OPTION_BIT: |
| 721 | case OPTION_BOOLEAN: |
| 722 | case OPTION_INCR: |
| 723 | case OPTION_SET_UINT: |
| 724 | case OPTION_SET_PTR: |
| 725 | break; |
| 726 | } |
| 727 | |
| 728 | if (pos <= USAGE_OPTS_WIDTH) |
| 729 | pad = USAGE_OPTS_WIDTH - pos; |
| 730 | else { |
| 731 | fputc('\n', stderr); |
| 732 | pad = USAGE_OPTS_WIDTH; |
| 733 | } |
| 734 | fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help); |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 735 | if (opts->flags & PARSE_OPT_NOBUILD) |
| 736 | fprintf(stderr, "%*s(not built-in because %s)\n", |
| 737 | USAGE_OPTS_WIDTH + USAGE_GAP, "", |
| 738 | opts->build_opt); |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 739 | } |
| 740 | |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 741 | static int option__cmp(const void *va, const void *vb) |
| 742 | { |
| 743 | const struct option *a = va, *b = vb; |
| 744 | int sa = tolower(a->short_name), sb = tolower(b->short_name), ret; |
| 745 | |
| 746 | if (sa == 0) |
| 747 | sa = 'z' + 1; |
| 748 | if (sb == 0) |
| 749 | sb = 'z' + 1; |
| 750 | |
| 751 | ret = sa - sb; |
| 752 | |
| 753 | if (ret == 0) { |
| 754 | const char *la = a->long_name ?: "", |
| 755 | *lb = b->long_name ?: ""; |
| 756 | ret = strcmp(la, lb); |
| 757 | } |
| 758 | |
| 759 | return ret; |
| 760 | } |
| 761 | |
| 762 | static struct option *options__order(const struct option *opts) |
| 763 | { |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 764 | int nr_opts = 0, len; |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 765 | const struct option *o = opts; |
| 766 | struct option *ordered; |
| 767 | |
| 768 | for (o = opts; o->type != OPTION_END; o++) |
| 769 | ++nr_opts; |
| 770 | |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 771 | len = sizeof(*o) * (nr_opts + 1); |
| 772 | ordered = malloc(len); |
| 773 | if (!ordered) |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 774 | goto out; |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 775 | memcpy(ordered, opts, len); |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 776 | |
| 777 | qsort(ordered, nr_opts, sizeof(*o), option__cmp); |
| 778 | out: |
| 779 | return ordered; |
| 780 | } |
| 781 | |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 782 | static bool option__in_argv(const struct option *opt, const struct parse_opt_ctx_t *ctx) |
| 783 | { |
| 784 | int i; |
| 785 | |
| 786 | for (i = 1; i < ctx->argc; ++i) { |
| 787 | const char *arg = ctx->argv[i]; |
| 788 | |
Arnaldo Carvalho de Melo | f4efcce | 2015-10-27 17:35:58 -0300 | [diff] [blame] | 789 | if (arg[0] != '-') { |
| 790 | if (arg[1] == '\0') { |
| 791 | if (arg[0] == opt->short_name) |
| 792 | return true; |
| 793 | continue; |
| 794 | } |
| 795 | |
| 796 | if (opt->long_name && strcmp(opt->long_name, arg) == 0) |
| 797 | return true; |
| 798 | |
| 799 | if (opt->help && strcasestr(opt->help, arg) != NULL) |
| 800 | return true; |
| 801 | |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 802 | continue; |
Arnaldo Carvalho de Melo | f4efcce | 2015-10-27 17:35:58 -0300 | [diff] [blame] | 803 | } |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 804 | |
| 805 | if (arg[1] == opt->short_name || |
| 806 | (arg[1] == '-' && opt->long_name && strcmp(opt->long_name, arg + 2) == 0)) |
| 807 | return true; |
| 808 | } |
| 809 | |
| 810 | return false; |
| 811 | } |
| 812 | |
Josh Poimboeuf | 408cf34 | 2015-12-13 22:18:12 -0600 | [diff] [blame] | 813 | static int usage_with_options_internal(const char * const *usagestr, |
| 814 | const struct option *opts, int full, |
| 815 | struct parse_opt_ctx_t *ctx) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 816 | { |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 817 | struct option *ordered; |
| 818 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 819 | if (!usagestr) |
| 820 | return PARSE_OPT_HELP; |
| 821 | |
Namhyung Kim | 01b1945 | 2015-10-25 00:49:26 +0900 | [diff] [blame] | 822 | setup_pager(); |
| 823 | |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 824 | if (error_buf) { |
| 825 | fprintf(stderr, " Error: %s\n", error_buf); |
| 826 | zfree(&error_buf); |
Namhyung Kim | 01b1945 | 2015-10-25 00:49:26 +0900 | [diff] [blame] | 827 | } |
| 828 | |
Yunlong Song | 3a134ae | 2015-10-15 15:39:51 +0800 | [diff] [blame] | 829 | fprintf(stderr, "\n Usage: %s\n", *usagestr++); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 830 | while (*usagestr && **usagestr) |
Ingo Molnar | 6e6b754 | 2008-04-15 22:39:31 +0200 | [diff] [blame] | 831 | fprintf(stderr, " or: %s\n", *usagestr++); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 832 | while (*usagestr) { |
| 833 | fprintf(stderr, "%s%s\n", |
| 834 | **usagestr ? " " : "", |
| 835 | *usagestr); |
| 836 | usagestr++; |
| 837 | } |
| 838 | |
| 839 | if (opts->type != OPTION_GROUP) |
| 840 | fputc('\n', stderr); |
| 841 | |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 842 | ordered = options__order(opts); |
| 843 | if (ordered) |
| 844 | opts = ordered; |
| 845 | |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 846 | for ( ; opts->type != OPTION_END; opts++) { |
| 847 | if (ctx && ctx->argc > 1 && !option__in_argv(opts, ctx)) |
| 848 | continue; |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 849 | print_option_help(opts, full); |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 850 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 851 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 852 | fputc('\n', stderr); |
| 853 | |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 854 | free(ordered); |
| 855 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 856 | return PARSE_OPT_HELP; |
| 857 | } |
| 858 | |
| 859 | void usage_with_options(const char * const *usagestr, |
| 860 | const struct option *opts) |
| 861 | { |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 862 | usage_with_options_internal(usagestr, opts, 0, NULL); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 863 | exit(129); |
| 864 | } |
| 865 | |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 866 | void usage_with_options_msg(const char * const *usagestr, |
| 867 | const struct option *opts, const char *fmt, ...) |
| 868 | { |
| 869 | va_list ap; |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 870 | char *tmp = error_buf; |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 871 | |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 872 | va_start(ap, fmt); |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 873 | if (vasprintf(&error_buf, fmt, ap) == -1) |
| 874 | die("vasprintf failed"); |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 875 | va_end(ap); |
| 876 | |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 877 | free(tmp); |
| 878 | |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 879 | usage_with_options_internal(usagestr, opts, 0, NULL); |
| 880 | exit(129); |
| 881 | } |
| 882 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 883 | int parse_options_usage(const char * const *usagestr, |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 884 | const struct option *opts, |
| 885 | const char *optstr, bool short_opt) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 886 | { |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 887 | if (!usagestr) |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 888 | goto opt; |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 889 | |
Yunlong Song | 3a134ae | 2015-10-15 15:39:51 +0800 | [diff] [blame] | 890 | fprintf(stderr, "\n Usage: %s\n", *usagestr++); |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 891 | while (*usagestr && **usagestr) |
| 892 | fprintf(stderr, " or: %s\n", *usagestr++); |
| 893 | while (*usagestr) { |
| 894 | fprintf(stderr, "%s%s\n", |
| 895 | **usagestr ? " " : "", |
| 896 | *usagestr); |
| 897 | usagestr++; |
| 898 | } |
| 899 | fputc('\n', stderr); |
| 900 | |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 901 | opt: |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 902 | for ( ; opts->type != OPTION_END; opts++) { |
| 903 | if (short_opt) { |
Namhyung Kim | a5f4a69 | 2015-10-25 00:49:24 +0900 | [diff] [blame] | 904 | if (opts->short_name == *optstr) { |
| 905 | print_option_help(opts, 0); |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 906 | break; |
Namhyung Kim | a5f4a69 | 2015-10-25 00:49:24 +0900 | [diff] [blame] | 907 | } |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 908 | continue; |
| 909 | } |
| 910 | |
| 911 | if (opts->long_name == NULL) |
| 912 | continue; |
| 913 | |
Namhyung Kim | a5f4a69 | 2015-10-25 00:49:24 +0900 | [diff] [blame] | 914 | if (!prefixcmp(opts->long_name, optstr)) |
| 915 | print_option_help(opts, 0); |
| 916 | if (!prefixcmp("no-", optstr) && |
| 917 | !prefixcmp(opts->long_name, optstr + 3)) |
| 918 | print_option_help(opts, 0); |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 919 | } |
| 920 | |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 921 | return PARSE_OPT_HELP; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 925 | int parse_opt_verbosity_cb(const struct option *opt, |
| 926 | const char *arg __maybe_unused, |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 927 | int unset) |
| 928 | { |
| 929 | int *target = opt->value; |
| 930 | |
| 931 | if (unset) |
| 932 | /* --no-quiet, --no-verbose */ |
| 933 | *target = 0; |
| 934 | else if (opt->short_name == 'v') { |
| 935 | if (*target >= 0) |
| 936 | (*target)++; |
| 937 | else |
| 938 | *target = 1; |
| 939 | } else { |
| 940 | if (*target <= 0) |
| 941 | (*target)--; |
| 942 | else |
| 943 | *target = -1; |
| 944 | } |
| 945 | return 0; |
| 946 | } |
Namhyung Kim | d152d1b | 2014-10-23 00:15:45 +0900 | [diff] [blame] | 947 | |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 948 | static struct option * |
| 949 | find_option(struct option *opts, int shortopt, const char *longopt) |
Namhyung Kim | d152d1b | 2014-10-23 00:15:45 +0900 | [diff] [blame] | 950 | { |
| 951 | for (; opts->type != OPTION_END; opts++) { |
| 952 | if ((shortopt && opts->short_name == shortopt) || |
| 953 | (opts->long_name && longopt && |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 954 | !strcmp(opts->long_name, longopt))) |
| 955 | return opts; |
Namhyung Kim | d152d1b | 2014-10-23 00:15:45 +0900 | [diff] [blame] | 956 | } |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 957 | return NULL; |
| 958 | } |
| 959 | |
| 960 | void set_option_flag(struct option *opts, int shortopt, const char *longopt, |
| 961 | int flag) |
| 962 | { |
| 963 | struct option *opt = find_option(opts, shortopt, longopt); |
| 964 | |
| 965 | if (opt) |
| 966 | opt->flags |= flag; |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | void set_option_nobuild(struct option *opts, int shortopt, |
| 971 | const char *longopt, |
| 972 | const char *build_opt, |
| 973 | bool can_skip) |
| 974 | { |
| 975 | struct option *opt = find_option(opts, shortopt, longopt); |
| 976 | |
| 977 | if (!opt) |
| 978 | return; |
| 979 | |
| 980 | opt->flags |= PARSE_OPT_NOBUILD; |
| 981 | opt->flags |= can_skip ? PARSE_OPT_CANSKIP : 0; |
| 982 | opt->build_opt = build_opt; |
Namhyung Kim | d152d1b | 2014-10-23 00:15:45 +0900 | [diff] [blame] | 983 | } |