help-unknown-cmd.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "cache.h"
  2. #include <subcmd/help.h>
  3. #include "../builtin.h"
  4. #include "levenshtein.h"
  5. static int autocorrect;
  6. static struct cmdnames aliases;
  7. static int perf_unknown_cmd_config(const char *var, const char *value, void *cb)
  8. {
  9. if (!strcmp(var, "help.autocorrect"))
  10. autocorrect = perf_config_int(var,value);
  11. /* Also use aliases for command lookup */
  12. if (!prefixcmp(var, "alias."))
  13. add_cmdname(&aliases, var + 6, strlen(var + 6));
  14. return perf_default_config(var, value, cb);
  15. }
  16. static int levenshtein_compare(const void *p1, const void *p2)
  17. {
  18. const struct cmdname *const *c1 = p1, *const *c2 = p2;
  19. const char *s1 = (*c1)->name, *s2 = (*c2)->name;
  20. int l1 = (*c1)->len;
  21. int l2 = (*c2)->len;
  22. return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
  23. }
  24. static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
  25. {
  26. unsigned int i;
  27. ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
  28. for (i = 0; i < old->cnt; i++)
  29. cmds->names[cmds->cnt++] = old->names[i];
  30. zfree(&old->names);
  31. old->cnt = 0;
  32. }
  33. const char *help_unknown_cmd(const char *cmd)
  34. {
  35. unsigned int i, n = 0, best_similarity = 0;
  36. struct cmdnames main_cmds, other_cmds;
  37. memset(&main_cmds, 0, sizeof(main_cmds));
  38. memset(&other_cmds, 0, sizeof(main_cmds));
  39. memset(&aliases, 0, sizeof(aliases));
  40. perf_config(perf_unknown_cmd_config, NULL);
  41. load_command_list("perf-", &main_cmds, &other_cmds);
  42. add_cmd_list(&main_cmds, &aliases);
  43. add_cmd_list(&main_cmds, &other_cmds);
  44. qsort(main_cmds.names, main_cmds.cnt,
  45. sizeof(main_cmds.names), cmdname_compare);
  46. uniq(&main_cmds);
  47. if (main_cmds.cnt) {
  48. /* This reuses cmdname->len for similarity index */
  49. for (i = 0; i < main_cmds.cnt; ++i)
  50. main_cmds.names[i]->len =
  51. levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
  52. qsort(main_cmds.names, main_cmds.cnt,
  53. sizeof(*main_cmds.names), levenshtein_compare);
  54. best_similarity = main_cmds.names[0]->len;
  55. n = 1;
  56. while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
  57. ++n;
  58. }
  59. if (autocorrect && n == 1) {
  60. const char *assumed = main_cmds.names[0]->name;
  61. main_cmds.names[0] = NULL;
  62. clean_cmdnames(&main_cmds);
  63. fprintf(stderr, "WARNING: You called a perf program named '%s', "
  64. "which does not exist.\n"
  65. "Continuing under the assumption that you meant '%s'\n",
  66. cmd, assumed);
  67. if (autocorrect > 0) {
  68. fprintf(stderr, "in %0.1f seconds automatically...\n",
  69. (float)autocorrect/10.0);
  70. poll(NULL, 0, autocorrect * 100);
  71. }
  72. return assumed;
  73. }
  74. fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd);
  75. if (main_cmds.cnt && best_similarity < 6) {
  76. fprintf(stderr, "\nDid you mean %s?\n",
  77. n < 2 ? "this": "one of these");
  78. for (i = 0; i < n; i++)
  79. fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
  80. }
  81. exit(1);
  82. }