parse-options.c 23 KB

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