parse-options.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. for (; options->type != OPTION_END; options++) {
  281. if (options->short_name == *p->opt) {
  282. p->opt = p->opt[1] ? p->opt + 1 : NULL;
  283. return get_value(p, options, OPT_SHORT);
  284. }
  285. }
  286. return -2;
  287. }
  288. static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
  289. const struct option *options)
  290. {
  291. const char *arg_end = strchr(arg, '=');
  292. const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
  293. int abbrev_flags = 0, ambiguous_flags = 0;
  294. if (!arg_end)
  295. arg_end = arg + strlen(arg);
  296. for (; options->type != OPTION_END; options++) {
  297. const char *rest;
  298. int flags = 0;
  299. if (!options->long_name)
  300. continue;
  301. rest = skip_prefix(arg, options->long_name);
  302. if (options->type == OPTION_ARGUMENT) {
  303. if (!rest)
  304. continue;
  305. if (*rest == '=')
  306. return opterror(options, "takes no value", flags);
  307. if (*rest)
  308. continue;
  309. p->out[p->cpidx++] = arg - 2;
  310. return 0;
  311. }
  312. if (!rest) {
  313. if (!prefixcmp(options->long_name, "no-")) {
  314. /*
  315. * The long name itself starts with "no-", so
  316. * accept the option without "no-" so that users
  317. * do not have to enter "no-no-" to get the
  318. * negation.
  319. */
  320. rest = skip_prefix(arg, options->long_name + 3);
  321. if (rest) {
  322. flags |= OPT_UNSET;
  323. goto match;
  324. }
  325. /* Abbreviated case */
  326. if (!prefixcmp(options->long_name + 3, arg)) {
  327. flags |= OPT_UNSET;
  328. goto is_abbreviated;
  329. }
  330. }
  331. /* abbreviated? */
  332. if (!strncmp(options->long_name, arg, arg_end - arg)) {
  333. is_abbreviated:
  334. if (abbrev_option) {
  335. /*
  336. * If this is abbreviated, it is
  337. * ambiguous. So when there is no
  338. * exact match later, we need to
  339. * error out.
  340. */
  341. ambiguous_option = abbrev_option;
  342. ambiguous_flags = abbrev_flags;
  343. }
  344. if (!(flags & OPT_UNSET) && *arg_end)
  345. p->opt = arg_end + 1;
  346. abbrev_option = options;
  347. abbrev_flags = flags;
  348. continue;
  349. }
  350. /* negated and abbreviated very much? */
  351. if (!prefixcmp("no-", arg)) {
  352. flags |= OPT_UNSET;
  353. goto is_abbreviated;
  354. }
  355. /* negated? */
  356. if (strncmp(arg, "no-", 3))
  357. continue;
  358. flags |= OPT_UNSET;
  359. rest = skip_prefix(arg + 3, options->long_name);
  360. /* abbreviated and negated? */
  361. if (!rest && !prefixcmp(options->long_name, arg + 3))
  362. goto is_abbreviated;
  363. if (!rest)
  364. continue;
  365. }
  366. match:
  367. if (*rest) {
  368. if (*rest != '=')
  369. continue;
  370. p->opt = rest + 1;
  371. }
  372. return get_value(p, options, flags);
  373. }
  374. if (ambiguous_option) {
  375. fprintf(stderr,
  376. " Error: Ambiguous option: %s (could be --%s%s or --%s%s)",
  377. arg,
  378. (ambiguous_flags & OPT_UNSET) ? "no-" : "",
  379. ambiguous_option->long_name,
  380. (abbrev_flags & OPT_UNSET) ? "no-" : "",
  381. abbrev_option->long_name);
  382. return -1;
  383. }
  384. if (abbrev_option)
  385. return get_value(p, abbrev_option, abbrev_flags);
  386. return -2;
  387. }
  388. static void check_typos(const char *arg, const struct option *options)
  389. {
  390. if (strlen(arg) < 3)
  391. return;
  392. if (!prefixcmp(arg, "no-")) {
  393. fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg);
  394. exit(129);
  395. }
  396. for (; options->type != OPTION_END; options++) {
  397. if (!options->long_name)
  398. continue;
  399. if (!prefixcmp(options->long_name, arg)) {
  400. fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg);
  401. exit(129);
  402. }
  403. }
  404. }
  405. static void parse_options_start(struct parse_opt_ctx_t *ctx,
  406. int argc, const char **argv, int flags)
  407. {
  408. memset(ctx, 0, sizeof(*ctx));
  409. ctx->argc = argc - 1;
  410. ctx->argv = argv + 1;
  411. ctx->out = argv;
  412. ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
  413. ctx->flags = flags;
  414. if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
  415. (flags & PARSE_OPT_STOP_AT_NON_OPTION))
  416. die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
  417. }
  418. static int usage_with_options_internal(const char * const *,
  419. const struct option *, int,
  420. struct parse_opt_ctx_t *);
  421. static int parse_options_step(struct parse_opt_ctx_t *ctx,
  422. const struct option *options,
  423. const char * const usagestr[])
  424. {
  425. int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
  426. int excl_short_opt = 1;
  427. const char *arg;
  428. /* we must reset ->opt, unknown short option leave it dangling */
  429. ctx->opt = NULL;
  430. for (; ctx->argc; ctx->argc--, ctx->argv++) {
  431. arg = ctx->argv[0];
  432. if (*arg != '-' || !arg[1]) {
  433. if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
  434. break;
  435. ctx->out[ctx->cpidx++] = ctx->argv[0];
  436. continue;
  437. }
  438. if (arg[1] != '-') {
  439. ctx->opt = ++arg;
  440. if (internal_help && *ctx->opt == 'h') {
  441. return usage_with_options_internal(usagestr, options, 0, ctx);
  442. }
  443. switch (parse_short_opt(ctx, options)) {
  444. case -1:
  445. return parse_options_usage(usagestr, options, arg, 1);
  446. case -2:
  447. goto unknown;
  448. case -3:
  449. goto exclusive;
  450. default:
  451. break;
  452. }
  453. if (ctx->opt)
  454. check_typos(arg, options);
  455. while (ctx->opt) {
  456. if (internal_help && *ctx->opt == 'h')
  457. return usage_with_options_internal(usagestr, options, 0, ctx);
  458. arg = ctx->opt;
  459. switch (parse_short_opt(ctx, options)) {
  460. case -1:
  461. return parse_options_usage(usagestr, options, arg, 1);
  462. case -2:
  463. /* fake a short option thing to hide the fact that we may have
  464. * started to parse aggregated stuff
  465. *
  466. * This is leaky, too bad.
  467. */
  468. ctx->argv[0] = strdup(ctx->opt - 1);
  469. *(char *)ctx->argv[0] = '-';
  470. goto unknown;
  471. case -3:
  472. goto exclusive;
  473. default:
  474. break;
  475. }
  476. }
  477. continue;
  478. }
  479. if (!arg[2]) { /* "--" */
  480. if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
  481. ctx->argc--;
  482. ctx->argv++;
  483. }
  484. break;
  485. }
  486. arg += 2;
  487. if (internal_help && !strcmp(arg, "help-all"))
  488. return usage_with_options_internal(usagestr, options, 1, ctx);
  489. if (internal_help && !strcmp(arg, "help"))
  490. return usage_with_options_internal(usagestr, options, 0, ctx);
  491. if (!strcmp(arg, "list-opts"))
  492. return PARSE_OPT_LIST_OPTS;
  493. if (!strcmp(arg, "list-cmds"))
  494. return PARSE_OPT_LIST_SUBCMDS;
  495. switch (parse_long_opt(ctx, arg, options)) {
  496. case -1:
  497. return parse_options_usage(usagestr, options, arg, 0);
  498. case -2:
  499. goto unknown;
  500. case -3:
  501. excl_short_opt = 0;
  502. goto exclusive;
  503. default:
  504. break;
  505. }
  506. continue;
  507. unknown:
  508. if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
  509. return PARSE_OPT_UNKNOWN;
  510. ctx->out[ctx->cpidx++] = ctx->argv[0];
  511. ctx->opt = NULL;
  512. }
  513. return PARSE_OPT_DONE;
  514. exclusive:
  515. parse_options_usage(usagestr, options, arg, excl_short_opt);
  516. if ((excl_short_opt && ctx->excl_opt->short_name) ||
  517. ctx->excl_opt->long_name == NULL) {
  518. char opt = ctx->excl_opt->short_name;
  519. parse_options_usage(NULL, options, &opt, 1);
  520. } else {
  521. parse_options_usage(NULL, options, ctx->excl_opt->long_name, 0);
  522. }
  523. return PARSE_OPT_HELP;
  524. }
  525. static int parse_options_end(struct parse_opt_ctx_t *ctx)
  526. {
  527. memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
  528. ctx->out[ctx->cpidx + ctx->argc] = NULL;
  529. return ctx->cpidx + ctx->argc;
  530. }
  531. int parse_options_subcommand(int argc, const char **argv, const struct option *options,
  532. const char *const subcommands[], const char *usagestr[], int flags)
  533. {
  534. struct parse_opt_ctx_t ctx;
  535. /* build usage string if it's not provided */
  536. if (subcommands && !usagestr[0]) {
  537. char *buf = NULL;
  538. astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
  539. for (int i = 0; subcommands[i]; i++) {
  540. if (i)
  541. astrcat(&buf, "|");
  542. astrcat(&buf, subcommands[i]);
  543. }
  544. astrcat(&buf, "}");
  545. usagestr[0] = buf;
  546. }
  547. parse_options_start(&ctx, argc, argv, flags);
  548. switch (parse_options_step(&ctx, options, usagestr)) {
  549. case PARSE_OPT_HELP:
  550. exit(129);
  551. case PARSE_OPT_DONE:
  552. break;
  553. case PARSE_OPT_LIST_OPTS:
  554. while (options->type != OPTION_END) {
  555. if (options->long_name)
  556. printf("--%s ", options->long_name);
  557. options++;
  558. }
  559. putchar('\n');
  560. exit(130);
  561. case PARSE_OPT_LIST_SUBCMDS:
  562. if (subcommands) {
  563. for (int i = 0; subcommands[i]; i++)
  564. printf("%s ", subcommands[i]);
  565. }
  566. putchar('\n');
  567. exit(130);
  568. default: /* PARSE_OPT_UNKNOWN */
  569. if (ctx.argv[0][1] == '-')
  570. astrcatf(&error_buf, "unknown option `%s'",
  571. ctx.argv[0] + 2);
  572. else
  573. astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
  574. usage_with_options(usagestr, options);
  575. }
  576. return parse_options_end(&ctx);
  577. }
  578. int parse_options(int argc, const char **argv, const struct option *options,
  579. const char * const usagestr[], int flags)
  580. {
  581. return parse_options_subcommand(argc, argv, options, NULL,
  582. (const char **) usagestr, flags);
  583. }
  584. #define USAGE_OPTS_WIDTH 24
  585. #define USAGE_GAP 2
  586. static void print_option_help(const struct option *opts, int full)
  587. {
  588. size_t pos;
  589. int pad;
  590. if (opts->type == OPTION_GROUP) {
  591. fputc('\n', stderr);
  592. if (*opts->help)
  593. fprintf(stderr, "%s\n", opts->help);
  594. return;
  595. }
  596. if (!full && (opts->flags & PARSE_OPT_HIDDEN))
  597. return;
  598. if (opts->flags & PARSE_OPT_DISABLED)
  599. return;
  600. pos = fprintf(stderr, " ");
  601. if (opts->short_name)
  602. pos += fprintf(stderr, "-%c", opts->short_name);
  603. else
  604. pos += fprintf(stderr, " ");
  605. if (opts->long_name && opts->short_name)
  606. pos += fprintf(stderr, ", ");
  607. if (opts->long_name)
  608. pos += fprintf(stderr, "--%s", opts->long_name);
  609. switch (opts->type) {
  610. case OPTION_ARGUMENT:
  611. break;
  612. case OPTION_LONG:
  613. case OPTION_U64:
  614. case OPTION_INTEGER:
  615. case OPTION_UINTEGER:
  616. if (opts->flags & PARSE_OPT_OPTARG)
  617. if (opts->long_name)
  618. pos += fprintf(stderr, "[=<n>]");
  619. else
  620. pos += fprintf(stderr, "[<n>]");
  621. else
  622. pos += fprintf(stderr, " <n>");
  623. break;
  624. case OPTION_CALLBACK:
  625. if (opts->flags & PARSE_OPT_NOARG)
  626. break;
  627. /* FALLTHROUGH */
  628. case OPTION_STRING:
  629. if (opts->argh) {
  630. if (opts->flags & PARSE_OPT_OPTARG)
  631. if (opts->long_name)
  632. pos += fprintf(stderr, "[=<%s>]", opts->argh);
  633. else
  634. pos += fprintf(stderr, "[<%s>]", opts->argh);
  635. else
  636. pos += fprintf(stderr, " <%s>", opts->argh);
  637. } else {
  638. if (opts->flags & PARSE_OPT_OPTARG)
  639. if (opts->long_name)
  640. pos += fprintf(stderr, "[=...]");
  641. else
  642. pos += fprintf(stderr, "[...]");
  643. else
  644. pos += fprintf(stderr, " ...");
  645. }
  646. break;
  647. default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
  648. case OPTION_END:
  649. case OPTION_GROUP:
  650. case OPTION_BIT:
  651. case OPTION_BOOLEAN:
  652. case OPTION_INCR:
  653. case OPTION_SET_UINT:
  654. case OPTION_SET_PTR:
  655. break;
  656. }
  657. if (pos <= USAGE_OPTS_WIDTH)
  658. pad = USAGE_OPTS_WIDTH - pos;
  659. else {
  660. fputc('\n', stderr);
  661. pad = USAGE_OPTS_WIDTH;
  662. }
  663. fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
  664. if (opts->flags & PARSE_OPT_NOBUILD)
  665. fprintf(stderr, "%*s(not built-in because %s)\n",
  666. USAGE_OPTS_WIDTH + USAGE_GAP, "",
  667. opts->build_opt);
  668. }
  669. static int option__cmp(const void *va, const void *vb)
  670. {
  671. const struct option *a = va, *b = vb;
  672. int sa = tolower(a->short_name), sb = tolower(b->short_name), ret;
  673. if (sa == 0)
  674. sa = 'z' + 1;
  675. if (sb == 0)
  676. sb = 'z' + 1;
  677. ret = sa - sb;
  678. if (ret == 0) {
  679. const char *la = a->long_name ?: "",
  680. *lb = b->long_name ?: "";
  681. ret = strcmp(la, lb);
  682. }
  683. return ret;
  684. }
  685. static struct option *options__order(const struct option *opts)
  686. {
  687. int nr_opts = 0, len;
  688. const struct option *o = opts;
  689. struct option *ordered;
  690. for (o = opts; o->type != OPTION_END; o++)
  691. ++nr_opts;
  692. len = sizeof(*o) * (nr_opts + 1);
  693. ordered = malloc(len);
  694. if (!ordered)
  695. goto out;
  696. memcpy(ordered, opts, len);
  697. qsort(ordered, nr_opts, sizeof(*o), option__cmp);
  698. out:
  699. return ordered;
  700. }
  701. static bool option__in_argv(const struct option *opt, const struct parse_opt_ctx_t *ctx)
  702. {
  703. int i;
  704. for (i = 1; i < ctx->argc; ++i) {
  705. const char *arg = ctx->argv[i];
  706. if (arg[0] != '-') {
  707. if (arg[1] == '\0') {
  708. if (arg[0] == opt->short_name)
  709. return true;
  710. continue;
  711. }
  712. if (opt->long_name && strcmp(opt->long_name, arg) == 0)
  713. return true;
  714. if (opt->help && strcasestr(opt->help, arg) != NULL)
  715. return true;
  716. continue;
  717. }
  718. if (arg[1] == opt->short_name ||
  719. (arg[1] == '-' && opt->long_name && strcmp(opt->long_name, arg + 2) == 0))
  720. return true;
  721. }
  722. return false;
  723. }
  724. static int usage_with_options_internal(const char * const *usagestr,
  725. const struct option *opts, int full,
  726. struct parse_opt_ctx_t *ctx)
  727. {
  728. struct option *ordered;
  729. if (!usagestr)
  730. return PARSE_OPT_HELP;
  731. setup_pager();
  732. if (error_buf) {
  733. fprintf(stderr, " Error: %s\n", error_buf);
  734. zfree(&error_buf);
  735. }
  736. fprintf(stderr, "\n Usage: %s\n", *usagestr++);
  737. while (*usagestr && **usagestr)
  738. fprintf(stderr, " or: %s\n", *usagestr++);
  739. while (*usagestr) {
  740. fprintf(stderr, "%s%s\n",
  741. **usagestr ? " " : "",
  742. *usagestr);
  743. usagestr++;
  744. }
  745. if (opts->type != OPTION_GROUP)
  746. fputc('\n', stderr);
  747. ordered = options__order(opts);
  748. if (ordered)
  749. opts = ordered;
  750. for ( ; opts->type != OPTION_END; opts++) {
  751. if (ctx && ctx->argc > 1 && !option__in_argv(opts, ctx))
  752. continue;
  753. print_option_help(opts, full);
  754. }
  755. fputc('\n', stderr);
  756. free(ordered);
  757. return PARSE_OPT_HELP;
  758. }
  759. void usage_with_options(const char * const *usagestr,
  760. const struct option *opts)
  761. {
  762. usage_with_options_internal(usagestr, opts, 0, NULL);
  763. exit(129);
  764. }
  765. void usage_with_options_msg(const char * const *usagestr,
  766. const struct option *opts, const char *fmt, ...)
  767. {
  768. va_list ap;
  769. char *tmp = error_buf;
  770. va_start(ap, fmt);
  771. if (vasprintf(&error_buf, fmt, ap) == -1)
  772. die("vasprintf failed");
  773. va_end(ap);
  774. free(tmp);
  775. usage_with_options_internal(usagestr, opts, 0, NULL);
  776. exit(129);
  777. }
  778. int parse_options_usage(const char * const *usagestr,
  779. const struct option *opts,
  780. const char *optstr, bool short_opt)
  781. {
  782. if (!usagestr)
  783. goto opt;
  784. fprintf(stderr, "\n Usage: %s\n", *usagestr++);
  785. while (*usagestr && **usagestr)
  786. fprintf(stderr, " or: %s\n", *usagestr++);
  787. while (*usagestr) {
  788. fprintf(stderr, "%s%s\n",
  789. **usagestr ? " " : "",
  790. *usagestr);
  791. usagestr++;
  792. }
  793. fputc('\n', stderr);
  794. opt:
  795. for ( ; opts->type != OPTION_END; opts++) {
  796. if (short_opt) {
  797. if (opts->short_name == *optstr) {
  798. print_option_help(opts, 0);
  799. break;
  800. }
  801. continue;
  802. }
  803. if (opts->long_name == NULL)
  804. continue;
  805. if (!prefixcmp(opts->long_name, optstr))
  806. print_option_help(opts, 0);
  807. if (!prefixcmp("no-", optstr) &&
  808. !prefixcmp(opts->long_name, optstr + 3))
  809. print_option_help(opts, 0);
  810. }
  811. return PARSE_OPT_HELP;
  812. }
  813. int parse_opt_verbosity_cb(const struct option *opt,
  814. const char *arg __maybe_unused,
  815. int unset)
  816. {
  817. int *target = opt->value;
  818. if (unset)
  819. /* --no-quiet, --no-verbose */
  820. *target = 0;
  821. else if (opt->short_name == 'v') {
  822. if (*target >= 0)
  823. (*target)++;
  824. else
  825. *target = 1;
  826. } else {
  827. if (*target <= 0)
  828. (*target)--;
  829. else
  830. *target = -1;
  831. }
  832. return 0;
  833. }
  834. static struct option *
  835. find_option(struct option *opts, int shortopt, const char *longopt)
  836. {
  837. for (; opts->type != OPTION_END; opts++) {
  838. if ((shortopt && opts->short_name == shortopt) ||
  839. (opts->long_name && longopt &&
  840. !strcmp(opts->long_name, longopt)))
  841. return opts;
  842. }
  843. return NULL;
  844. }
  845. void set_option_flag(struct option *opts, int shortopt, const char *longopt,
  846. int flag)
  847. {
  848. struct option *opt = find_option(opts, shortopt, longopt);
  849. if (opt)
  850. opt->flags |= flag;
  851. return;
  852. }
  853. void set_option_nobuild(struct option *opts, int shortopt,
  854. const char *longopt,
  855. const char *build_opt,
  856. bool can_skip)
  857. {
  858. struct option *opt = find_option(opts, shortopt, longopt);
  859. if (!opt)
  860. return;
  861. opt->flags |= PARSE_OPT_NOBUILD;
  862. opt->flags |= can_skip ? PARSE_OPT_CANSKIP : 0;
  863. opt->build_opt = build_opt;
  864. }