blob: 6f2975a0035856a6ea1f7d1b02b3ad47a0d5ef45 [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001#include "cache.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +02002#include "../builtin.h"
Ingo Molnar07800602009-04-20 15:00:56 +02003#include "exec_cmd.h"
4#include "levenshtein.h"
5#include "help.h"
6
Ingo Molnarf37a2912009-07-01 12:37:06 +02007void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
Ingo Molnar07800602009-04-20 15:00:56 +02008{
9 struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
10
11 ent->len = len;
12 memcpy(ent->name, name, len);
13 ent->name[len] = 0;
14
15 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
16 cmds->names[cmds->cnt++] = ent;
17}
18
19static void clean_cmdnames(struct cmdnames *cmds)
20{
Ingo Molnarf37a2912009-07-01 12:37:06 +020021 unsigned int i;
22
Ingo Molnar07800602009-04-20 15:00:56 +020023 for (i = 0; i < cmds->cnt; ++i)
24 free(cmds->names[i]);
25 free(cmds->names);
26 cmds->cnt = 0;
27 cmds->alloc = 0;
28}
29
30static int cmdname_compare(const void *a_, const void *b_)
31{
32 struct cmdname *a = *(struct cmdname **)a_;
33 struct cmdname *b = *(struct cmdname **)b_;
34 return strcmp(a->name, b->name);
35}
36
37static void uniq(struct cmdnames *cmds)
38{
Ingo Molnarf37a2912009-07-01 12:37:06 +020039 unsigned int i, j;
Ingo Molnar07800602009-04-20 15:00:56 +020040
41 if (!cmds->cnt)
42 return;
43
44 for (i = j = 1; i < cmds->cnt; i++)
45 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
46 cmds->names[j++] = cmds->names[i];
47
48 cmds->cnt = j;
49}
50
51void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
52{
Ingo Molnarf37a2912009-07-01 12:37:06 +020053 size_t ci, cj, ei;
Ingo Molnar07800602009-04-20 15:00:56 +020054 int cmp;
55
56 ci = cj = ei = 0;
57 while (ci < cmds->cnt && ei < excludes->cnt) {
58 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
59 if (cmp < 0)
60 cmds->names[cj++] = cmds->names[ci++];
61 else if (cmp == 0)
62 ci++, ei++;
63 else if (cmp > 0)
64 ei++;
65 }
66
67 while (ci < cmds->cnt)
68 cmds->names[cj++] = cmds->names[ci++];
69
70 cmds->cnt = cj;
71}
72
73static void pretty_print_string_list(struct cmdnames *cmds, int longest)
74{
75 int cols = 1, rows;
76 int space = longest + 1; /* min 1 SP between words */
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -030077 struct winsize win;
78 int max_cols;
Ingo Molnar07800602009-04-20 15:00:56 +020079 int i, j;
80
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -030081 get_term_dimensions(&win);
82 max_cols = win.ws_col - 1; /* don't print *on* the edge */
83
Ingo Molnar07800602009-04-20 15:00:56 +020084 if (space < max_cols)
85 cols = max_cols / space;
86 rows = (cmds->cnt + cols - 1) / cols;
87
88 for (i = 0; i < rows; i++) {
89 printf(" ");
90
91 for (j = 0; j < cols; j++) {
Ingo Molnarf37a2912009-07-01 12:37:06 +020092 unsigned int n = j * rows + i;
93 unsigned int size = space;
94
Ingo Molnar07800602009-04-20 15:00:56 +020095 if (n >= cmds->cnt)
96 break;
97 if (j == cols-1 || n + rows >= cmds->cnt)
98 size = 1;
99 printf("%-*s", size, cmds->names[n]->name);
100 }
101 putchar('\n');
102 }
103}
104
105static int is_executable(const char *name)
106{
107 struct stat st;
108
109 if (stat(name, &st) || /* stat, not lstat */
110 !S_ISREG(st.st_mode))
111 return 0;
112
Ingo Molnar07800602009-04-20 15:00:56 +0200113 return st.st_mode & S_IXUSR;
114}
115
116static void list_commands_in_dir(struct cmdnames *cmds,
117 const char *path,
118 const char *prefix)
119{
120 int prefix_len;
121 DIR *dir = opendir(path);
122 struct dirent *de;
123 struct strbuf buf = STRBUF_INIT;
124 int len;
125
126 if (!dir)
127 return;
128 if (!prefix)
129 prefix = "perf-";
130 prefix_len = strlen(prefix);
131
132 strbuf_addf(&buf, "%s/", path);
133 len = buf.len;
134
135 while ((de = readdir(dir)) != NULL) {
136 int entlen;
137
138 if (prefixcmp(de->d_name, prefix))
139 continue;
140
141 strbuf_setlen(&buf, len);
142 strbuf_addstr(&buf, de->d_name);
143 if (!is_executable(buf.buf))
144 continue;
145
146 entlen = strlen(de->d_name) - prefix_len;
147 if (has_extension(de->d_name, ".exe"))
148 entlen -= 4;
149
150 add_cmdname(cmds, de->d_name + prefix_len, entlen);
151 }
152 closedir(dir);
153 strbuf_release(&buf);
154}
155
156void load_command_list(const char *prefix,
157 struct cmdnames *main_cmds,
158 struct cmdnames *other_cmds)
159{
160 const char *env_path = getenv("PATH");
161 const char *exec_path = perf_exec_path();
162
163 if (exec_path) {
164 list_commands_in_dir(main_cmds, exec_path, prefix);
165 qsort(main_cmds->names, main_cmds->cnt,
166 sizeof(*main_cmds->names), cmdname_compare);
167 uniq(main_cmds);
168 }
169
170 if (env_path) {
171 char *paths, *path, *colon;
172 path = paths = strdup(env_path);
173 while (1) {
174 if ((colon = strchr(path, PATH_SEP)))
175 *colon = 0;
176 if (!exec_path || strcmp(path, exec_path))
177 list_commands_in_dir(other_cmds, path, prefix);
178
179 if (!colon)
180 break;
181 path = colon + 1;
182 }
183 free(paths);
184
185 qsort(other_cmds->names, other_cmds->cnt,
186 sizeof(*other_cmds->names), cmdname_compare);
187 uniq(other_cmds);
188 }
189 exclude_cmds(other_cmds, main_cmds);
190}
191
192void list_commands(const char *title, struct cmdnames *main_cmds,
193 struct cmdnames *other_cmds)
194{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200195 unsigned int i, longest = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200196
197 for (i = 0; i < main_cmds->cnt; i++)
198 if (longest < main_cmds->names[i]->len)
199 longest = main_cmds->names[i]->len;
200 for (i = 0; i < other_cmds->cnt; i++)
201 if (longest < other_cmds->names[i]->len)
202 longest = other_cmds->names[i]->len;
203
204 if (main_cmds->cnt) {
205 const char *exec_path = perf_exec_path();
206 printf("available %s in '%s'\n", title, exec_path);
207 printf("----------------");
208 mput_char('-', strlen(title) + strlen(exec_path));
209 putchar('\n');
210 pretty_print_string_list(main_cmds, longest);
211 putchar('\n');
212 }
213
214 if (other_cmds->cnt) {
215 printf("%s available from elsewhere on your $PATH\n", title);
216 printf("---------------------------------------");
217 mput_char('-', strlen(title));
218 putchar('\n');
219 pretty_print_string_list(other_cmds, longest);
220 putchar('\n');
221 }
222}
223
224int is_in_cmdlist(struct cmdnames *c, const char *s)
225{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200226 unsigned int i;
227
Ingo Molnar07800602009-04-20 15:00:56 +0200228 for (i = 0; i < c->cnt; i++)
229 if (!strcmp(s, c->names[i]->name))
230 return 1;
231 return 0;
232}
233
234static int autocorrect;
235static struct cmdnames aliases;
236
237static int perf_unknown_cmd_config(const char *var, const char *value, void *cb)
238{
239 if (!strcmp(var, "help.autocorrect"))
240 autocorrect = perf_config_int(var,value);
241 /* Also use aliases for command lookup */
242 if (!prefixcmp(var, "alias."))
243 add_cmdname(&aliases, var + 6, strlen(var + 6));
244
245 return perf_default_config(var, value, cb);
246}
247
248static int levenshtein_compare(const void *p1, const void *p2)
249{
250 const struct cmdname *const *c1 = p1, *const *c2 = p2;
251 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
252 int l1 = (*c1)->len;
253 int l2 = (*c2)->len;
254 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
255}
256
257static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
258{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200259 unsigned int i;
260
Ingo Molnar07800602009-04-20 15:00:56 +0200261 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
262
263 for (i = 0; i < old->cnt; i++)
264 cmds->names[cmds->cnt++] = old->names[i];
265 free(old->names);
266 old->cnt = 0;
267 old->names = NULL;
268}
269
270const char *help_unknown_cmd(const char *cmd)
271{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200272 unsigned int i, n = 0, best_similarity = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200273 struct cmdnames main_cmds, other_cmds;
274
275 memset(&main_cmds, 0, sizeof(main_cmds));
276 memset(&other_cmds, 0, sizeof(main_cmds));
277 memset(&aliases, 0, sizeof(aliases));
278
279 perf_config(perf_unknown_cmd_config, NULL);
280
281 load_command_list("perf-", &main_cmds, &other_cmds);
282
283 add_cmd_list(&main_cmds, &aliases);
284 add_cmd_list(&main_cmds, &other_cmds);
285 qsort(main_cmds.names, main_cmds.cnt,
286 sizeof(main_cmds.names), cmdname_compare);
287 uniq(&main_cmds);
288
Ingo Molnar051cdc32009-06-03 20:09:11 +0200289 if (main_cmds.cnt) {
290 /* This reuses cmdname->len for similarity index */
291 for (i = 0; i < main_cmds.cnt; ++i)
292 main_cmds.names[i]->len =
293 levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
Ingo Molnar07800602009-04-20 15:00:56 +0200294
Ingo Molnar051cdc32009-06-03 20:09:11 +0200295 qsort(main_cmds.names, main_cmds.cnt,
296 sizeof(*main_cmds.names), levenshtein_compare);
Ingo Molnar07800602009-04-20 15:00:56 +0200297
Ingo Molnar051cdc32009-06-03 20:09:11 +0200298 best_similarity = main_cmds.names[0]->len;
299 n = 1;
300 while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
301 ++n;
302 }
303
Ingo Molnar07800602009-04-20 15:00:56 +0200304 if (autocorrect && n == 1) {
305 const char *assumed = main_cmds.names[0]->name;
Ingo Molnar051cdc32009-06-03 20:09:11 +0200306
Ingo Molnar07800602009-04-20 15:00:56 +0200307 main_cmds.names[0] = NULL;
308 clean_cmdnames(&main_cmds);
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -0300309 fprintf(stderr, "WARNING: You called a perf program named '%s', "
Ingo Molnar07800602009-04-20 15:00:56 +0200310 "which does not exist.\n"
311 "Continuing under the assumption that you meant '%s'\n",
312 cmd, assumed);
313 if (autocorrect > 0) {
314 fprintf(stderr, "in %0.1f seconds automatically...\n",
315 (float)autocorrect/10.0);
316 poll(NULL, 0, autocorrect * 100);
317 }
318 return assumed;
319 }
320
321 fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd);
322
Ingo Molnar051cdc32009-06-03 20:09:11 +0200323 if (main_cmds.cnt && best_similarity < 6) {
Ingo Molnar07800602009-04-20 15:00:56 +0200324 fprintf(stderr, "\nDid you mean %s?\n",
325 n < 2 ? "this": "one of these");
326
327 for (i = 0; i < n; i++)
328 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
329 }
330
331 exit(1);
332}
333
Ingo Molnarf37a2912009-07-01 12:37:06 +0200334int cmd_version(int argc __used, const char **argv __used, const char *prefix __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200335{
336 printf("perf version %s\n", perf_version_string);
337 return 0;
338}