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