parse-options.c 23 KB

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