parse-options.c 14 KB

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