parse-options.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. #include "util.h"
  2. #include "parse-options.h"
  3. #include "cache.h"
  4. #define OPT_SHORT 1
  5. #define OPT_UNSET 2
  6. static int opterror(const struct option *opt, const char *reason, int flags)
  7. {
  8. if (flags & OPT_SHORT)
  9. return error("switch `%c' %s", opt->short_name, reason);
  10. if (flags & OPT_UNSET)
  11. return error("option `no-%s' %s", opt->long_name, reason);
  12. return error("option `%s' %s", opt->long_name, reason);
  13. }
  14. static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
  15. int flags, const char **arg)
  16. {
  17. if (p->opt) {
  18. *arg = p->opt;
  19. p->opt = NULL;
  20. } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
  21. **(p->argv + 1) == '-')) {
  22. *arg = (const char *)opt->defval;
  23. } else if (p->argc > 1) {
  24. p->argc--;
  25. *arg = *++p->argv;
  26. } else
  27. return opterror(opt, "requires a value", flags);
  28. return 0;
  29. }
  30. static int get_value(struct parse_opt_ctx_t *p,
  31. const struct option *opt, int flags)
  32. {
  33. const char *s, *arg = NULL;
  34. const int unset = flags & OPT_UNSET;
  35. if (unset && p->opt)
  36. return opterror(opt, "takes no value", flags);
  37. if (unset && (opt->flags & PARSE_OPT_NONEG))
  38. return opterror(opt, "isn't available", flags);
  39. if (!(flags & OPT_SHORT) && p->opt) {
  40. switch (opt->type) {
  41. case OPTION_CALLBACK:
  42. if (!(opt->flags & PARSE_OPT_NOARG))
  43. break;
  44. /* FALLTHROUGH */
  45. case OPTION_BOOLEAN:
  46. case OPTION_INCR:
  47. case OPTION_BIT:
  48. case OPTION_SET_UINT:
  49. case OPTION_SET_PTR:
  50. return opterror(opt, "takes no value", flags);
  51. case OPTION_END:
  52. case OPTION_ARGUMENT:
  53. case OPTION_GROUP:
  54. case OPTION_STRING:
  55. case OPTION_INTEGER:
  56. case OPTION_UINTEGER:
  57. case OPTION_LONG:
  58. case OPTION_U64:
  59. default:
  60. break;
  61. }
  62. }
  63. switch (opt->type) {
  64. case OPTION_BIT:
  65. if (unset)
  66. *(int *)opt->value &= ~opt->defval;
  67. else
  68. *(int *)opt->value |= opt->defval;
  69. return 0;
  70. case OPTION_BOOLEAN:
  71. *(bool *)opt->value = unset ? false : true;
  72. return 0;
  73. case OPTION_INCR:
  74. *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
  75. return 0;
  76. case OPTION_SET_UINT:
  77. *(unsigned int *)opt->value = unset ? 0 : opt->defval;
  78. return 0;
  79. case OPTION_SET_PTR:
  80. *(void **)opt->value = unset ? NULL : (void *)opt->defval;
  81. return 0;
  82. case OPTION_STRING:
  83. if (unset)
  84. *(const char **)opt->value = NULL;
  85. else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
  86. *(const char **)opt->value = (const char *)opt->defval;
  87. else
  88. return get_arg(p, opt, flags, (const char **)opt->value);
  89. return 0;
  90. case OPTION_CALLBACK:
  91. if (unset)
  92. return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
  93. if (opt->flags & PARSE_OPT_NOARG)
  94. return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
  95. if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
  96. return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
  97. if (get_arg(p, opt, flags, &arg))
  98. return -1;
  99. return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
  100. case OPTION_INTEGER:
  101. if (unset) {
  102. *(int *)opt->value = 0;
  103. return 0;
  104. }
  105. if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
  106. *(int *)opt->value = opt->defval;
  107. return 0;
  108. }
  109. if (get_arg(p, opt, flags, &arg))
  110. return -1;
  111. *(int *)opt->value = strtol(arg, (char **)&s, 10);
  112. if (*s)
  113. return opterror(opt, "expects a numerical value", flags);
  114. return 0;
  115. case OPTION_UINTEGER:
  116. if (unset) {
  117. *(unsigned int *)opt->value = 0;
  118. return 0;
  119. }
  120. if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
  121. *(unsigned int *)opt->value = opt->defval;
  122. return 0;
  123. }
  124. if (get_arg(p, opt, flags, &arg))
  125. return -1;
  126. *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
  127. if (*s)
  128. return opterror(opt, "expects a numerical value", flags);
  129. return 0;
  130. case OPTION_LONG:
  131. if (unset) {
  132. *(long *)opt->value = 0;
  133. return 0;
  134. }
  135. if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
  136. *(long *)opt->value = opt->defval;
  137. return 0;
  138. }
  139. if (get_arg(p, opt, flags, &arg))
  140. return -1;
  141. *(long *)opt->value = strtol(arg, (char **)&s, 10);
  142. if (*s)
  143. return opterror(opt, "expects a numerical value", flags);
  144. return 0;
  145. case OPTION_U64:
  146. if (unset) {
  147. *(u64 *)opt->value = 0;
  148. return 0;
  149. }
  150. if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
  151. *(u64 *)opt->value = opt->defval;
  152. return 0;
  153. }
  154. if (get_arg(p, opt, flags, &arg))
  155. return -1;
  156. *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
  157. if (*s)
  158. return opterror(opt, "expects a numerical value", flags);
  159. return 0;
  160. case OPTION_END:
  161. case OPTION_ARGUMENT:
  162. case OPTION_GROUP:
  163. default:
  164. die("should not happen, someone must be hit on the forehead");
  165. }
  166. }
  167. static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
  168. {
  169. for (; options->type != OPTION_END; options++) {
  170. if (options->short_name == *p->opt) {
  171. p->opt = p->opt[1] ? p->opt + 1 : NULL;
  172. return get_value(p, options, OPT_SHORT);
  173. }
  174. }
  175. return -2;
  176. }
  177. static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
  178. const struct option *options)
  179. {
  180. const char *arg_end = strchr(arg, '=');
  181. const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
  182. int abbrev_flags = 0, ambiguous_flags = 0;
  183. if (!arg_end)
  184. arg_end = arg + strlen(arg);
  185. for (; options->type != OPTION_END; options++) {
  186. const char *rest;
  187. int flags = 0;
  188. if (!options->long_name)
  189. continue;
  190. rest = skip_prefix(arg, options->long_name);
  191. if (options->type == OPTION_ARGUMENT) {
  192. if (!rest)
  193. continue;
  194. if (*rest == '=')
  195. return opterror(options, "takes no value", flags);
  196. if (*rest)
  197. continue;
  198. p->out[p->cpidx++] = arg - 2;
  199. return 0;
  200. }
  201. if (!rest) {
  202. /* abbreviated? */
  203. if (!strncmp(options->long_name, arg, arg_end - arg)) {
  204. is_abbreviated:
  205. if (abbrev_option) {
  206. /*
  207. * If this is abbreviated, it is
  208. * ambiguous. So when there is no
  209. * exact match later, we need to
  210. * error out.
  211. */
  212. ambiguous_option = abbrev_option;
  213. ambiguous_flags = abbrev_flags;
  214. }
  215. if (!(flags & OPT_UNSET) && *arg_end)
  216. p->opt = arg_end + 1;
  217. abbrev_option = options;
  218. abbrev_flags = flags;
  219. continue;
  220. }
  221. /* negated and abbreviated very much? */
  222. if (!prefixcmp("no-", arg)) {
  223. flags |= OPT_UNSET;
  224. goto is_abbreviated;
  225. }
  226. /* negated? */
  227. if (strncmp(arg, "no-", 3))
  228. continue;
  229. flags |= OPT_UNSET;
  230. rest = skip_prefix(arg + 3, options->long_name);
  231. /* abbreviated and negated? */
  232. if (!rest && !prefixcmp(options->long_name, arg + 3))
  233. goto is_abbreviated;
  234. if (!rest)
  235. continue;
  236. }
  237. if (*rest) {
  238. if (*rest != '=')
  239. continue;
  240. p->opt = rest + 1;
  241. }
  242. return get_value(p, options, flags);
  243. }
  244. if (ambiguous_option)
  245. return error("Ambiguous option: %s "
  246. "(could be --%s%s or --%s%s)",
  247. arg,
  248. (ambiguous_flags & OPT_UNSET) ? "no-" : "",
  249. ambiguous_option->long_name,
  250. (abbrev_flags & OPT_UNSET) ? "no-" : "",
  251. abbrev_option->long_name);
  252. if (abbrev_option)
  253. return get_value(p, abbrev_option, abbrev_flags);
  254. return -2;
  255. }
  256. static void check_typos(const char *arg, const struct option *options)
  257. {
  258. if (strlen(arg) < 3)
  259. return;
  260. if (!prefixcmp(arg, "no-")) {
  261. error ("did you mean `--%s` (with two dashes ?)", arg);
  262. exit(129);
  263. }
  264. for (; options->type != OPTION_END; options++) {
  265. if (!options->long_name)
  266. continue;
  267. if (!prefixcmp(options->long_name, arg)) {
  268. error ("did you mean `--%s` (with two dashes ?)", arg);
  269. exit(129);
  270. }
  271. }
  272. }
  273. void parse_options_start(struct parse_opt_ctx_t *ctx,
  274. int argc, const char **argv, int flags)
  275. {
  276. memset(ctx, 0, sizeof(*ctx));
  277. ctx->argc = argc - 1;
  278. ctx->argv = argv + 1;
  279. ctx->out = argv;
  280. ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
  281. ctx->flags = flags;
  282. if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
  283. (flags & PARSE_OPT_STOP_AT_NON_OPTION))
  284. die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
  285. }
  286. static int usage_with_options_internal(const char * const *,
  287. const struct option *, int);
  288. int parse_options_step(struct parse_opt_ctx_t *ctx,
  289. const struct option *options,
  290. const char * const usagestr[])
  291. {
  292. int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
  293. /* we must reset ->opt, unknown short option leave it dangling */
  294. ctx->opt = NULL;
  295. for (; ctx->argc; ctx->argc--, ctx->argv++) {
  296. const char *arg = ctx->argv[0];
  297. if (*arg != '-' || !arg[1]) {
  298. if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
  299. break;
  300. ctx->out[ctx->cpidx++] = ctx->argv[0];
  301. continue;
  302. }
  303. if (arg[1] != '-') {
  304. ctx->opt = arg + 1;
  305. if (internal_help && *ctx->opt == 'h')
  306. return parse_options_usage(usagestr, options);
  307. switch (parse_short_opt(ctx, options)) {
  308. case -1:
  309. return parse_options_usage(usagestr, options);
  310. case -2:
  311. goto unknown;
  312. default:
  313. break;
  314. }
  315. if (ctx->opt)
  316. check_typos(arg + 1, options);
  317. while (ctx->opt) {
  318. if (internal_help && *ctx->opt == 'h')
  319. return parse_options_usage(usagestr, options);
  320. switch (parse_short_opt(ctx, options)) {
  321. case -1:
  322. return parse_options_usage(usagestr, options);
  323. case -2:
  324. /* fake a short option thing to hide the fact that we may have
  325. * started to parse aggregated stuff
  326. *
  327. * This is leaky, too bad.
  328. */
  329. ctx->argv[0] = strdup(ctx->opt - 1);
  330. *(char *)ctx->argv[0] = '-';
  331. goto unknown;
  332. default:
  333. break;
  334. }
  335. }
  336. continue;
  337. }
  338. if (!arg[2]) { /* "--" */
  339. if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
  340. ctx->argc--;
  341. ctx->argv++;
  342. }
  343. break;
  344. }
  345. if (internal_help && !strcmp(arg + 2, "help-all"))
  346. return usage_with_options_internal(usagestr, options, 1);
  347. if (internal_help && !strcmp(arg + 2, "help"))
  348. return parse_options_usage(usagestr, options);
  349. switch (parse_long_opt(ctx, arg + 2, options)) {
  350. case -1:
  351. return parse_options_usage(usagestr, options);
  352. case -2:
  353. goto unknown;
  354. default:
  355. break;
  356. }
  357. continue;
  358. unknown:
  359. if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
  360. return PARSE_OPT_UNKNOWN;
  361. ctx->out[ctx->cpidx++] = ctx->argv[0];
  362. ctx->opt = NULL;
  363. }
  364. return PARSE_OPT_DONE;
  365. }
  366. int parse_options_end(struct parse_opt_ctx_t *ctx)
  367. {
  368. memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
  369. ctx->out[ctx->cpidx + ctx->argc] = NULL;
  370. return ctx->cpidx + ctx->argc;
  371. }
  372. int parse_options(int argc, const char **argv, const struct option *options,
  373. const char * const usagestr[], int flags)
  374. {
  375. struct parse_opt_ctx_t ctx;
  376. parse_options_start(&ctx, argc, argv, flags);
  377. switch (parse_options_step(&ctx, options, usagestr)) {
  378. case PARSE_OPT_HELP:
  379. exit(129);
  380. case PARSE_OPT_DONE:
  381. break;
  382. default: /* PARSE_OPT_UNKNOWN */
  383. if (ctx.argv[0][1] == '-') {
  384. error("unknown option `%s'", ctx.argv[0] + 2);
  385. } else {
  386. error("unknown switch `%c'", *ctx.opt);
  387. }
  388. usage_with_options(usagestr, options);
  389. }
  390. return parse_options_end(&ctx);
  391. }
  392. #define USAGE_OPTS_WIDTH 24
  393. #define USAGE_GAP 2
  394. int usage_with_options_internal(const char * const *usagestr,
  395. const struct option *opts, int full)
  396. {
  397. if (!usagestr)
  398. return PARSE_OPT_HELP;
  399. fprintf(stderr, "\n usage: %s\n", *usagestr++);
  400. while (*usagestr && **usagestr)
  401. fprintf(stderr, " or: %s\n", *usagestr++);
  402. while (*usagestr) {
  403. fprintf(stderr, "%s%s\n",
  404. **usagestr ? " " : "",
  405. *usagestr);
  406. usagestr++;
  407. }
  408. if (opts->type != OPTION_GROUP)
  409. fputc('\n', stderr);
  410. for (; opts->type != OPTION_END; opts++) {
  411. size_t pos;
  412. int pad;
  413. if (opts->type == OPTION_GROUP) {
  414. fputc('\n', stderr);
  415. if (*opts->help)
  416. fprintf(stderr, "%s\n", opts->help);
  417. continue;
  418. }
  419. if (!full && (opts->flags & PARSE_OPT_HIDDEN))
  420. continue;
  421. pos = fprintf(stderr, " ");
  422. if (opts->short_name)
  423. pos += fprintf(stderr, "-%c", opts->short_name);
  424. else
  425. pos += fprintf(stderr, " ");
  426. if (opts->long_name && opts->short_name)
  427. pos += fprintf(stderr, ", ");
  428. if (opts->long_name)
  429. pos += fprintf(stderr, "--%s", opts->long_name);
  430. switch (opts->type) {
  431. case OPTION_ARGUMENT:
  432. break;
  433. case OPTION_LONG:
  434. case OPTION_U64:
  435. case OPTION_INTEGER:
  436. case OPTION_UINTEGER:
  437. if (opts->flags & PARSE_OPT_OPTARG)
  438. if (opts->long_name)
  439. pos += fprintf(stderr, "[=<n>]");
  440. else
  441. pos += fprintf(stderr, "[<n>]");
  442. else
  443. pos += fprintf(stderr, " <n>");
  444. break;
  445. case OPTION_CALLBACK:
  446. if (opts->flags & PARSE_OPT_NOARG)
  447. break;
  448. /* FALLTHROUGH */
  449. case OPTION_STRING:
  450. if (opts->argh) {
  451. if (opts->flags & PARSE_OPT_OPTARG)
  452. if (opts->long_name)
  453. pos += fprintf(stderr, "[=<%s>]", opts->argh);
  454. else
  455. pos += fprintf(stderr, "[<%s>]", opts->argh);
  456. else
  457. pos += fprintf(stderr, " <%s>", opts->argh);
  458. } else {
  459. if (opts->flags & PARSE_OPT_OPTARG)
  460. if (opts->long_name)
  461. pos += fprintf(stderr, "[=...]");
  462. else
  463. pos += fprintf(stderr, "[...]");
  464. else
  465. pos += fprintf(stderr, " ...");
  466. }
  467. break;
  468. default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
  469. case OPTION_END:
  470. case OPTION_GROUP:
  471. case OPTION_BIT:
  472. case OPTION_BOOLEAN:
  473. case OPTION_INCR:
  474. case OPTION_SET_UINT:
  475. case OPTION_SET_PTR:
  476. break;
  477. }
  478. if (pos <= USAGE_OPTS_WIDTH)
  479. pad = USAGE_OPTS_WIDTH - pos;
  480. else {
  481. fputc('\n', stderr);
  482. pad = USAGE_OPTS_WIDTH;
  483. }
  484. fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
  485. }
  486. fputc('\n', stderr);
  487. return PARSE_OPT_HELP;
  488. }
  489. void usage_with_options(const char * const *usagestr,
  490. const struct option *opts)
  491. {
  492. exit_browser(false);
  493. usage_with_options_internal(usagestr, opts, 0);
  494. exit(129);
  495. }
  496. int parse_options_usage(const char * const *usagestr,
  497. const struct option *opts)
  498. {
  499. return usage_with_options_internal(usagestr, opts, 0);
  500. }
  501. int parse_opt_verbosity_cb(const struct option *opt, const char *arg __used,
  502. int unset)
  503. {
  504. int *target = opt->value;
  505. if (unset)
  506. /* --no-quiet, --no-verbose */
  507. *target = 0;
  508. else if (opt->short_name == 'v') {
  509. if (*target >= 0)
  510. (*target)++;
  511. else
  512. *target = 1;
  513. } else {
  514. if (*target <= 0)
  515. (*target)--;
  516. else
  517. *target = -1;
  518. }
  519. return 0;
  520. }