parse-options.c 23 KB

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