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