help-unknown-cmd.c 3.1 KB

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