parse-options.c 13 KB

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