parse-options.c 13 KB

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