parse-options.c 12 KB

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