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