help-unknown-cmd.c 2.9 KB

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