Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 2 | #include <linux/compiler.h> |
Arnaldo Carvalho de Melo | 96395cb | 2017-04-26 15:49:21 -0300 | [diff] [blame] | 3 | #include <linux/string.h> |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 4 | #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 Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 10 | #include "subcmd-util.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 11 | #include "parse-options.h" |
Josh Poimboeuf | 096d355 | 2015-12-15 09:39:35 -0600 | [diff] [blame] | 12 | #include "subcmd-config.h" |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 13 | #include "pager.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 14 | |
| 15 | #define OPT_SHORT 1 |
| 16 | #define OPT_UNSET 2 |
| 17 | |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 18 | char *error_buf; |
Namhyung Kim | 01b1945 | 2015-10-25 00:49:26 +0900 | [diff] [blame] | 19 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 20 | static int opterror(const struct option *opt, const char *reason, int flags) |
| 21 | { |
| 22 | if (flags & OPT_SHORT) |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 23 | 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 | |
| 32 | static 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 Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 38 | static void optwarning(const struct option *opt, const char *reason, int flags) |
| 39 | { |
| 40 | if (flags & OPT_SHORT) |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 41 | fprintf(stderr, " Warning: switch `%c' %s", opt->short_name, reason); |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 42 | else if (flags & OPT_UNSET) |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 43 | fprintf(stderr, " Warning: option `no-%s' %s", opt->long_name, reason); |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 44 | else |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 45 | fprintf(stderr, " Warning: option `%s' %s", opt->long_name, reason); |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 48 | static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt, |
| 49 | int flags, const char **arg) |
| 50 | { |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 51 | const char *res; |
| 52 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 53 | if (p->opt) { |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 54 | res = p->opt; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 55 | p->opt = NULL; |
Frederic Weisbecker | 5a4b181 | 2009-07-02 17:58:20 +0200 | [diff] [blame] | 56 | } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 || |
| 57 | **(p->argv + 1) == '-')) { |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 58 | res = (const char *)opt->defval; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 59 | } else if (p->argc > 1) { |
| 60 | p->argc--; |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 61 | res = *++p->argv; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 62 | } else |
| 63 | return opterror(opt, "requires a value", flags); |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 64 | if (arg) |
| 65 | *arg = res; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int get_value(struct parse_opt_ctx_t *p, |
| 70 | const struct option *opt, int flags) |
| 71 | { |
Ingo Molnar | 233f0b9 | 2009-06-03 21:48:40 +0200 | [diff] [blame] | 72 | const char *s, *arg = NULL; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 73 | const int unset = flags & OPT_UNSET; |
Wang Nan | 0c8c207 | 2015-03-13 12:51:54 +0000 | [diff] [blame] | 74 | int err; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 75 | |
| 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 Kim | d152d1b | 2014-10-23 00:15:45 +0900 | [diff] [blame] | 80 | if (opt->flags & PARSE_OPT_DISABLED) |
| 81 | return opterror(opt, "is not usable", flags); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 82 | |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 83 | if (opt->flags & PARSE_OPT_EXCLUSIVE) { |
Namhyung Kim | 5594b55 | 2015-01-10 19:33:45 +0900 | [diff] [blame] | 84 | if (p->excl_opt && p->excl_opt != opt) { |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 85 | char msg[128]; |
| 86 | |
| 87 | if (((flags & OPT_SHORT) && p->excl_opt->short_name) || |
| 88 | p->excl_opt->long_name == NULL) { |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 89 | snprintf(msg, sizeof(msg), "cannot be used with switch `%c'", |
| 90 | p->excl_opt->short_name); |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 91 | } else { |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 92 | snprintf(msg, sizeof(msg), "cannot be used with %s", |
| 93 | p->excl_opt->long_name); |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 94 | } |
| 95 | opterror(opt, msg, flags); |
| 96 | return -3; |
| 97 | } |
| 98 | p->excl_opt = opt; |
| 99 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 100 | 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 Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 107 | case OPTION_INCR: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 108 | case OPTION_BIT: |
Arnaldo Carvalho de Melo | edb7c60 | 2010-05-17 16:22:41 -0300 | [diff] [blame] | 109 | case OPTION_SET_UINT: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 110 | case OPTION_SET_PTR: |
| 111 | return opterror(opt, "takes no value", flags); |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 112 | case OPTION_END: |
| 113 | case OPTION_ARGUMENT: |
| 114 | case OPTION_GROUP: |
| 115 | case OPTION_STRING: |
| 116 | case OPTION_INTEGER: |
Arnaldo Carvalho de Melo | c100edb | 2010-05-17 15:30:00 -0300 | [diff] [blame] | 117 | case OPTION_UINTEGER: |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 118 | case OPTION_LONG: |
Arnaldo Carvalho de Melo | 4ba8b3e | 2018-10-18 15:24:07 -0300 | [diff] [blame] | 119 | case OPTION_ULONG: |
Arnaldo Carvalho de Melo | 6ba85ce | 2010-05-17 12:16:48 -0300 | [diff] [blame] | 120 | case OPTION_U64: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 121 | default: |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 126 | 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 Melo | 4ba8b3e | 2018-10-18 15:24:07 -0300 | [diff] [blame] | 170 | case OPTION_ULONG: |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 171 | 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 Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 185 | 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 Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 194 | *(bool *)opt->value = unset ? false : true; |
Adrian Hunter | 167faf3 | 2013-11-18 11:55:56 +0200 | [diff] [blame] | 195 | if (opt->set) |
| 196 | *(bool *)opt->set = true; |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 197 | return 0; |
| 198 | |
| 199 | case OPTION_INCR: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 200 | *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1; |
| 201 | return 0; |
| 202 | |
Arnaldo Carvalho de Melo | edb7c60 | 2010-05-17 16:22:41 -0300 | [diff] [blame] | 203 | case OPTION_SET_UINT: |
| 204 | *(unsigned int *)opt->value = unset ? 0 : opt->defval; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 205 | 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 Nan | 0c8c207 | 2015-03-13 12:51:54 +0000 | [diff] [blame] | 212 | err = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 213 | 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 Nan | 0c8c207 | 2015-03-13 12:51:54 +0000 | [diff] [blame] | 218 | err = get_arg(p, opt, flags, (const char **)opt->value); |
| 219 | |
Jiri Olsa | b66fb1d | 2017-01-03 09:19:54 +0100 | [diff] [blame] | 220 | if (opt->set) |
| 221 | *(bool *)opt->set = true; |
| 222 | |
Wang Nan | 0c8c207 | 2015-03-13 12:51:54 +0000 | [diff] [blame] | 223 | /* 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 Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 238 | |
| 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 Melo | c100edb | 2010-05-17 15:30:00 -0300 | [diff] [blame] | 266 | 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 Melo | b988971 | 2017-02-14 13:55:40 -0300 | [diff] [blame] | 277 | if (arg[0] == '-') |
| 278 | return opterror(opt, "expects an unsigned numerical value", flags); |
Arnaldo Carvalho de Melo | c100edb | 2010-05-17 15:30:00 -0300 | [diff] [blame] | 279 | *(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 Zijlstra | e61078a | 2009-06-03 11:24:33 +0200 | [diff] [blame] | 284 | 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 Melo | 4ba8b3e | 2018-10-18 15:24:07 -0300 | [diff] [blame] | 300 | 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 Melo | 6ba85ce | 2010-05-17 12:16:48 -0300 | [diff] [blame] | 316 | 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 Melo | b988971 | 2017-02-14 13:55:40 -0300 | [diff] [blame] | 327 | if (arg[0] == '-') |
| 328 | return opterror(opt, "expects an unsigned numerical value", flags); |
Arnaldo Carvalho de Melo | 6ba85ce | 2010-05-17 12:16:48 -0300 | [diff] [blame] | 329 | *(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 Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 334 | case OPTION_END: |
| 335 | case OPTION_ARGUMENT: |
| 336 | case OPTION_GROUP: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 337 | default: |
| 338 | die("should not happen, someone must be hit on the forehead"); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options) |
| 343 | { |
Namhyung Kim | 369a247 | 2016-10-24 12:00:02 +0900 | [diff] [blame] | 344 | retry: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 345 | 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 Kim | 369a247 | 2016-10-24 12:00:02 +0900 | [diff] [blame] | 351 | |
| 352 | if (options->parent) { |
| 353 | options = options->parent; |
| 354 | goto retry; |
| 355 | } |
| 356 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 357 | return -2; |
| 358 | } |
| 359 | |
| 360 | static 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 Kim | 369a247 | 2016-10-24 12:00:02 +0900 | [diff] [blame] | 370 | retry: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 371 | 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 Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 390 | if (strstarts(options->long_name, "no-")) { |
Adrian Hunter | 4bc4379 | 2013-11-18 11:55:55 +0200 | [diff] [blame] | 391 | /* |
| 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 Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 403 | if (strstarts(options->long_name + 3, arg)) { |
Adrian Hunter | 4bc4379 | 2013-11-18 11:55:55 +0200 | [diff] [blame] | 404 | flags |= OPT_UNSET; |
| 405 | goto is_abbreviated; |
| 406 | } |
| 407 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 408 | /* abbreviated? */ |
| 409 | if (!strncmp(options->long_name, arg, arg_end - arg)) { |
| 410 | is_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 Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 428 | if (strstarts("no-", arg)) { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 429 | 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 Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 438 | if (!rest && strstarts(options->long_name, arg + 3)) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 439 | goto is_abbreviated; |
| 440 | if (!rest) |
| 441 | continue; |
| 442 | } |
Adrian Hunter | 4bc4379 | 2013-11-18 11:55:55 +0200 | [diff] [blame] | 443 | match: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 444 | if (*rest) { |
| 445 | if (*rest != '=') |
| 446 | continue; |
| 447 | p->opt = rest + 1; |
| 448 | } |
| 449 | return get_value(p, options, flags); |
| 450 | } |
| 451 | |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 452 | if (ambiguous_option) { |
| 453 | fprintf(stderr, |
Ravi Bangoria | 66f5a07 | 2018-04-17 09:43:44 +0530 | [diff] [blame] | 454 | " Error: Ambiguous option: %s (could be --%s%s or --%s%s)\n", |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 455 | 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 Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 462 | if (abbrev_option) |
| 463 | return get_value(p, abbrev_option, abbrev_flags); |
Namhyung Kim | 369a247 | 2016-10-24 12:00:02 +0900 | [diff] [blame] | 464 | |
| 465 | if (options->parent) { |
| 466 | options = options->parent; |
| 467 | goto retry; |
| 468 | } |
| 469 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 470 | return -2; |
| 471 | } |
| 472 | |
| 473 | static void check_typos(const char *arg, const struct option *options) |
| 474 | { |
| 475 | if (strlen(arg) < 3) |
| 476 | return; |
| 477 | |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 478 | if (strstarts(arg, "no-")) { |
Ravi Bangoria | 66f5a07 | 2018-04-17 09:43:44 +0530 | [diff] [blame] | 479 | fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)\n", arg); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 480 | exit(129); |
| 481 | } |
| 482 | |
| 483 | for (; options->type != OPTION_END; options++) { |
| 484 | if (!options->long_name) |
| 485 | continue; |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 486 | if (strstarts(options->long_name, arg)) { |
Ravi Bangoria | 66f5a07 | 2018-04-17 09:43:44 +0530 | [diff] [blame] | 487 | fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)\n", arg); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 488 | exit(129); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
Josh Poimboeuf | 408cf34 | 2015-12-13 22:18:12 -0600 | [diff] [blame] | 493 | static void parse_options_start(struct parse_opt_ctx_t *ctx, |
| 494 | int argc, const char **argv, int flags) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 495 | { |
| 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 | |
| 507 | static int usage_with_options_internal(const char * const *, |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 508 | const struct option *, int, |
| 509 | struct parse_opt_ctx_t *); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 510 | |
Josh Poimboeuf | 408cf34 | 2015-12-13 22:18:12 -0600 | [diff] [blame] | 511 | static int parse_options_step(struct parse_opt_ctx_t *ctx, |
| 512 | const struct option *options, |
| 513 | const char * const usagestr[]) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 514 | { |
| 515 | int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP); |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 516 | int excl_short_opt = 1; |
| 517 | const char *arg; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 518 | |
| 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 Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 523 | arg = ctx->argv[0]; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 524 | 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 Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 532 | ctx->opt = ++arg; |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 533 | if (internal_help && *ctx->opt == 'h') { |
| 534 | return usage_with_options_internal(usagestr, options, 0, ctx); |
| 535 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 536 | switch (parse_short_opt(ctx, options)) { |
| 537 | case -1: |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 538 | return parse_options_usage(usagestr, options, arg, 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 539 | case -2: |
| 540 | goto unknown; |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 541 | case -3: |
| 542 | goto exclusive; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 543 | default: |
| 544 | break; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 545 | } |
| 546 | if (ctx->opt) |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 547 | check_typos(arg, options); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 548 | while (ctx->opt) { |
| 549 | if (internal_help && *ctx->opt == 'h') |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 550 | return usage_with_options_internal(usagestr, options, 0, ctx); |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 551 | arg = ctx->opt; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 552 | switch (parse_short_opt(ctx, options)) { |
| 553 | case -1: |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 554 | return parse_options_usage(usagestr, options, arg, 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 555 | 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 Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 564 | case -3: |
| 565 | goto exclusive; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 566 | default: |
| 567 | break; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 568 | } |
| 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 Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 581 | arg += 2; |
| 582 | if (internal_help && !strcmp(arg, "help-all")) |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 583 | return usage_with_options_internal(usagestr, options, 1, ctx); |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 584 | if (internal_help && !strcmp(arg, "help")) |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 585 | return usage_with_options_internal(usagestr, options, 0, ctx); |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 586 | if (!strcmp(arg, "list-opts")) |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 587 | return PARSE_OPT_LIST_OPTS; |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 588 | if (!strcmp(arg, "list-cmds")) |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 589 | return PARSE_OPT_LIST_SUBCMDS; |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 590 | switch (parse_long_opt(ctx, arg, options)) { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 591 | case -1: |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 592 | return parse_options_usage(usagestr, options, arg, 0); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 593 | case -2: |
| 594 | goto unknown; |
Namhyung Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 595 | case -3: |
| 596 | excl_short_opt = 0; |
| 597 | goto exclusive; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 598 | default: |
| 599 | break; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 600 | } |
| 601 | continue; |
| 602 | unknown: |
| 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 Kim | 42bd71d | 2014-10-23 00:15:48 +0900 | [diff] [blame] | 609 | |
| 610 | exclusive: |
| 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 Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Josh Poimboeuf | 408cf34 | 2015-12-13 22:18:12 -0600 | [diff] [blame] | 622 | static int parse_options_end(struct parse_opt_ctx_t *ctx) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 623 | { |
| 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 Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 629 | int parse_options_subcommand(int argc, const char **argv, const struct option *options, |
| 630 | const char *const subcommands[], const char *usagestr[], int flags) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 631 | { |
| 632 | struct parse_opt_ctx_t ctx; |
| 633 | |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 634 | /* build usage string if it's not provided */ |
| 635 | if (subcommands && !usagestr[0]) { |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 636 | char *buf = NULL; |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 637 | |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 638 | astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]); |
| 639 | |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 640 | for (int i = 0; subcommands[i]; i++) { |
| 641 | if (i) |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 642 | astrcat(&buf, "|"); |
| 643 | astrcat(&buf, subcommands[i]); |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 644 | } |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 645 | astrcat(&buf, "}"); |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 646 | |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 647 | usagestr[0] = buf; |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 648 | } |
| 649 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 650 | 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 Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 656 | case PARSE_OPT_LIST_OPTS: |
Namhyung Kim | 4d8061f | 2012-10-03 00:21:34 +0900 | [diff] [blame] | 657 | while (options->type != OPTION_END) { |
Yunlong Song | 3ef1e65 | 2015-02-27 18:21:30 +0800 | [diff] [blame] | 658 | if (options->long_name) |
| 659 | printf("--%s ", options->long_name); |
Namhyung Kim | 4d8061f | 2012-10-03 00:21:34 +0900 | [diff] [blame] | 660 | options++; |
| 661 | } |
Yunlong Song | ed45752 | 2015-02-27 18:21:29 +0800 | [diff] [blame] | 662 | putchar('\n'); |
Namhyung Kim | 4d8061f | 2012-10-03 00:21:34 +0900 | [diff] [blame] | 663 | exit(130); |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 664 | case PARSE_OPT_LIST_SUBCMDS: |
Yunlong Song | 3a03005 | 2015-02-13 21:11:52 +0800 | [diff] [blame] | 665 | if (subcommands) { |
| 666 | for (int i = 0; subcommands[i]; i++) |
| 667 | printf("%s ", subcommands[i]); |
| 668 | } |
Yunlong Song | ed45752 | 2015-02-27 18:21:29 +0800 | [diff] [blame] | 669 | putchar('\n'); |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 670 | exit(130); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 671 | default: /* PARSE_OPT_UNKNOWN */ |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 672 | 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 Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 677 | usage_with_options(usagestr, options); |
| 678 | } |
| 679 | |
| 680 | return parse_options_end(&ctx); |
| 681 | } |
| 682 | |
Ramkumar Ramachandra | 09a71b9 | 2014-03-03 20:26:36 -0500 | [diff] [blame] | 683 | int 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 Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 690 | #define USAGE_OPTS_WIDTH 24 |
| 691 | #define USAGE_GAP 2 |
| 692 | |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 693 | static 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 Kim | d152d1b | 2014-10-23 00:15:45 +0900 | [diff] [blame] | 706 | if (opts->flags & PARSE_OPT_DISABLED) |
| 707 | return; |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 708 | |
| 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 Melo | 4ba8b3e | 2018-10-18 15:24:07 -0300 | [diff] [blame] | 724 | case OPTION_ULONG: |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 725 | 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 Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 777 | 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 Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 781 | } |
| 782 | |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 783 | static 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 | |
| 804 | static struct option *options__order(const struct option *opts) |
| 805 | { |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 806 | int nr_opts = 0, len; |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 807 | const struct option *o = opts; |
| 808 | struct option *ordered; |
| 809 | |
| 810 | for (o = opts; o->type != OPTION_END; o++) |
| 811 | ++nr_opts; |
| 812 | |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 813 | len = sizeof(*o) * (nr_opts + 1); |
| 814 | ordered = malloc(len); |
| 815 | if (!ordered) |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 816 | goto out; |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 817 | memcpy(ordered, opts, len); |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 818 | |
| 819 | qsort(ordered, nr_opts, sizeof(*o), option__cmp); |
| 820 | out: |
| 821 | return ordered; |
| 822 | } |
| 823 | |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 824 | static 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 Melo | f4efcce | 2015-10-27 17:35:58 -0300 | [diff] [blame] | 831 | 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 Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 844 | continue; |
Arnaldo Carvalho de Melo | f4efcce | 2015-10-27 17:35:58 -0300 | [diff] [blame] | 845 | } |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 846 | |
| 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 Poimboeuf | 408cf34 | 2015-12-13 22:18:12 -0600 | [diff] [blame] | 855 | static int usage_with_options_internal(const char * const *usagestr, |
| 856 | const struct option *opts, int full, |
| 857 | struct parse_opt_ctx_t *ctx) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 858 | { |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 859 | struct option *ordered; |
| 860 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 861 | if (!usagestr) |
| 862 | return PARSE_OPT_HELP; |
| 863 | |
Namhyung Kim | 01b1945 | 2015-10-25 00:49:26 +0900 | [diff] [blame] | 864 | setup_pager(); |
| 865 | |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 866 | if (error_buf) { |
| 867 | fprintf(stderr, " Error: %s\n", error_buf); |
| 868 | zfree(&error_buf); |
Namhyung Kim | 01b1945 | 2015-10-25 00:49:26 +0900 | [diff] [blame] | 869 | } |
| 870 | |
Yunlong Song | 3a134ae | 2015-10-15 15:39:51 +0800 | [diff] [blame] | 871 | fprintf(stderr, "\n Usage: %s\n", *usagestr++); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 872 | while (*usagestr && **usagestr) |
Ingo Molnar | 6e6b754 | 2008-04-15 22:39:31 +0200 | [diff] [blame] | 873 | fprintf(stderr, " or: %s\n", *usagestr++); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 874 | 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 Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 884 | ordered = options__order(opts); |
| 885 | if (ordered) |
| 886 | opts = ordered; |
| 887 | |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 888 | for ( ; opts->type != OPTION_END; opts++) { |
| 889 | if (ctx && ctx->argc > 1 && !option__in_argv(opts, ctx)) |
| 890 | continue; |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 891 | print_option_help(opts, full); |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 892 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 893 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 894 | fputc('\n', stderr); |
| 895 | |
Arnaldo Carvalho de Melo | 869c55b | 2015-10-23 11:23:28 -0300 | [diff] [blame] | 896 | free(ordered); |
| 897 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 898 | return PARSE_OPT_HELP; |
| 899 | } |
| 900 | |
| 901 | void usage_with_options(const char * const *usagestr, |
| 902 | const struct option *opts) |
| 903 | { |
Arnaldo Carvalho de Melo | 161d904 | 2015-10-23 13:27:39 -0300 | [diff] [blame] | 904 | usage_with_options_internal(usagestr, opts, 0, NULL); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 905 | exit(129); |
| 906 | } |
| 907 | |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 908 | void usage_with_options_msg(const char * const *usagestr, |
| 909 | const struct option *opts, const char *fmt, ...) |
| 910 | { |
| 911 | va_list ap; |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 912 | char *tmp = error_buf; |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 913 | |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 914 | va_start(ap, fmt); |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 915 | if (vasprintf(&error_buf, fmt, ap) == -1) |
| 916 | die("vasprintf failed"); |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 917 | va_end(ap); |
| 918 | |
Josh Poimboeuf | 901421a | 2015-12-15 09:39:36 -0600 | [diff] [blame] | 919 | free(tmp); |
| 920 | |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 921 | usage_with_options_internal(usagestr, opts, 0, NULL); |
| 922 | exit(129); |
| 923 | } |
| 924 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 925 | int parse_options_usage(const char * const *usagestr, |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 926 | const struct option *opts, |
| 927 | const char *optstr, bool short_opt) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 928 | { |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 929 | if (!usagestr) |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 930 | goto opt; |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 931 | |
Yunlong Song | 3a134ae | 2015-10-15 15:39:51 +0800 | [diff] [blame] | 932 | fprintf(stderr, "\n Usage: %s\n", *usagestr++); |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 933 | 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 Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 943 | opt: |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 944 | for ( ; opts->type != OPTION_END; opts++) { |
| 945 | if (short_opt) { |
Namhyung Kim | a5f4a69 | 2015-10-25 00:49:24 +0900 | [diff] [blame] | 946 | if (opts->short_name == *optstr) { |
| 947 | print_option_help(opts, 0); |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 948 | break; |
Namhyung Kim | a5f4a69 | 2015-10-25 00:49:24 +0900 | [diff] [blame] | 949 | } |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 950 | continue; |
| 951 | } |
| 952 | |
| 953 | if (opts->long_name == NULL) |
| 954 | continue; |
| 955 | |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 956 | if (strstarts(opts->long_name, optstr)) |
Namhyung Kim | a5f4a69 | 2015-10-25 00:49:24 +0900 | [diff] [blame] | 957 | print_option_help(opts, 0); |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 958 | if (strstarts("no-", optstr) && |
| 959 | strstarts(opts->long_name, optstr + 3)) |
Namhyung Kim | a5f4a69 | 2015-10-25 00:49:24 +0900 | [diff] [blame] | 960 | print_option_help(opts, 0); |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 961 | } |
| 962 | |
Namhyung Kim | ac69762 | 2013-11-01 16:33:11 +0900 | [diff] [blame] | 963 | return PARSE_OPT_HELP; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 967 | int parse_opt_verbosity_cb(const struct option *opt, |
| 968 | const char *arg __maybe_unused, |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 969 | 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 Kim | d152d1b | 2014-10-23 00:15:45 +0900 | [diff] [blame] | 989 | |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 990 | static struct option * |
| 991 | find_option(struct option *opts, int shortopt, const char *longopt) |
Namhyung Kim | d152d1b | 2014-10-23 00:15:45 +0900 | [diff] [blame] | 992 | { |
| 993 | for (; opts->type != OPTION_END; opts++) { |
| 994 | if ((shortopt && opts->short_name == shortopt) || |
| 995 | (opts->long_name && longopt && |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 996 | !strcmp(opts->long_name, longopt))) |
| 997 | return opts; |
Namhyung Kim | d152d1b | 2014-10-23 00:15:45 +0900 | [diff] [blame] | 998 | } |
Wang Nan | 48e1cab | 2015-12-14 10:39:22 +0000 | [diff] [blame] | 999 | return NULL; |
| 1000 | } |
| 1001 | |
| 1002 | void 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 | |
| 1012 | void 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 Kim | d152d1b | 2014-10-23 00:15:45 +0900 | [diff] [blame] | 1025 | } |