parse-options.c 12 KB

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