parse-options.c 13 KB

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