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