help-unknown-cmd.c 3.1 KB

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