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