parse-options.c 23 KB

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