parse-options.c 23 KB

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