Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / tools / perf / util / help.c
1 #include "cache.h"
2 #include "../builtin.h"
3 #include "exec_cmd.h"
4 #include "levenshtein.h"
5 #include "help.h"
6
7 /* most GUI terminals set COLUMNS (although some don't export it) */
8 static int term_columns(void)
9 {
10         char *col_string = getenv("COLUMNS");
11         int n_cols;
12
13         if (col_string && (n_cols = atoi(col_string)) > 0)
14                 return n_cols;
15
16 #ifdef TIOCGWINSZ
17         {
18                 struct winsize ws;
19                 if (!ioctl(1, TIOCGWINSZ, &ws)) {
20                         if (ws.ws_col)
21                                 return ws.ws_col;
22                 }
23         }
24 #endif
25
26         return 80;
27 }
28
29 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
30 {
31         struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
32
33         ent->len = len;
34         memcpy(ent->name, name, len);
35         ent->name[len] = 0;
36
37         ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
38         cmds->names[cmds->cnt++] = ent;
39 }
40
41 static void clean_cmdnames(struct cmdnames *cmds)
42 {
43         int i;
44         for (i = 0; i < cmds->cnt; ++i)
45                 free(cmds->names[i]);
46         free(cmds->names);
47         cmds->cnt = 0;
48         cmds->alloc = 0;
49 }
50
51 static int cmdname_compare(const void *a_, const void *b_)
52 {
53         struct cmdname *a = *(struct cmdname **)a_;
54         struct cmdname *b = *(struct cmdname **)b_;
55         return strcmp(a->name, b->name);
56 }
57
58 static void uniq(struct cmdnames *cmds)
59 {
60         int i, j;
61
62         if (!cmds->cnt)
63                 return;
64
65         for (i = j = 1; i < cmds->cnt; i++)
66                 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
67                         cmds->names[j++] = cmds->names[i];
68
69         cmds->cnt = j;
70 }
71
72 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
73 {
74         int ci, cj, ei;
75         int cmp;
76
77         ci = cj = ei = 0;
78         while (ci < cmds->cnt && ei < excludes->cnt) {
79                 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
80                 if (cmp < 0)
81                         cmds->names[cj++] = cmds->names[ci++];
82                 else if (cmp == 0)
83                         ci++, ei++;
84                 else if (cmp > 0)
85                         ei++;
86         }
87
88         while (ci < cmds->cnt)
89                 cmds->names[cj++] = cmds->names[ci++];
90
91         cmds->cnt = cj;
92 }
93
94 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
95 {
96         int cols = 1, rows;
97         int space = longest + 1; /* min 1 SP between words */
98         int max_cols = term_columns() - 1; /* don't print *on* the edge */
99         int i, j;
100
101         if (space < max_cols)
102                 cols = max_cols / space;
103         rows = (cmds->cnt + cols - 1) / cols;
104
105         for (i = 0; i < rows; i++) {
106                 printf("  ");
107
108                 for (j = 0; j < cols; j++) {
109                         int n = j * rows + i;
110                         int size = space;
111                         if (n >= cmds->cnt)
112                                 break;
113                         if (j == cols-1 || n + rows >= cmds->cnt)
114                                 size = 1;
115                         printf("%-*s", size, cmds->names[n]->name);
116                 }
117                 putchar('\n');
118         }
119 }
120
121 static int is_executable(const char *name)
122 {
123         struct stat st;
124
125         if (stat(name, &st) || /* stat, not lstat */
126             !S_ISREG(st.st_mode))
127                 return 0;
128
129         return st.st_mode & S_IXUSR;
130 }
131
132 static void list_commands_in_dir(struct cmdnames *cmds,
133                                          const char *path,
134                                          const char *prefix)
135 {
136         int prefix_len;
137         DIR *dir = opendir(path);
138         struct dirent *de;
139         struct strbuf buf = STRBUF_INIT;
140         int len;
141
142         if (!dir)
143                 return;
144         if (!prefix)
145                 prefix = "perf-";
146         prefix_len = strlen(prefix);
147
148         strbuf_addf(&buf, "%s/", path);
149         len = buf.len;
150
151         while ((de = readdir(dir)) != NULL) {
152                 int entlen;
153
154                 if (prefixcmp(de->d_name, prefix))
155                         continue;
156
157                 strbuf_setlen(&buf, len);
158                 strbuf_addstr(&buf, de->d_name);
159                 if (!is_executable(buf.buf))
160                         continue;
161
162                 entlen = strlen(de->d_name) - prefix_len;
163                 if (has_extension(de->d_name, ".exe"))
164                         entlen -= 4;
165
166                 add_cmdname(cmds, de->d_name + prefix_len, entlen);
167         }
168         closedir(dir);
169         strbuf_release(&buf);
170 }
171
172 void load_command_list(const char *prefix,
173                 struct cmdnames *main_cmds,
174                 struct cmdnames *other_cmds)
175 {
176         const char *env_path = getenv("PATH");
177         const char *exec_path = perf_exec_path();
178
179         if (exec_path) {
180                 list_commands_in_dir(main_cmds, exec_path, prefix);
181                 qsort(main_cmds->names, main_cmds->cnt,
182                       sizeof(*main_cmds->names), cmdname_compare);
183                 uniq(main_cmds);
184         }
185
186         if (env_path) {
187                 char *paths, *path, *colon;
188                 path = paths = strdup(env_path);
189                 while (1) {
190                         if ((colon = strchr(path, PATH_SEP)))
191                                 *colon = 0;
192                         if (!exec_path || strcmp(path, exec_path))
193                                 list_commands_in_dir(other_cmds, path, prefix);
194
195                         if (!colon)
196                                 break;
197                         path = colon + 1;
198                 }
199                 free(paths);
200
201                 qsort(other_cmds->names, other_cmds->cnt,
202                       sizeof(*other_cmds->names), cmdname_compare);
203                 uniq(other_cmds);
204         }
205         exclude_cmds(other_cmds, main_cmds);
206 }
207
208 void list_commands(const char *title, struct cmdnames *main_cmds,
209                    struct cmdnames *other_cmds)
210 {
211         int i, longest = 0;
212
213         for (i = 0; i < main_cmds->cnt; i++)
214                 if (longest < main_cmds->names[i]->len)
215                         longest = main_cmds->names[i]->len;
216         for (i = 0; i < other_cmds->cnt; i++)
217                 if (longest < other_cmds->names[i]->len)
218                         longest = other_cmds->names[i]->len;
219
220         if (main_cmds->cnt) {
221                 const char *exec_path = perf_exec_path();
222                 printf("available %s in '%s'\n", title, exec_path);
223                 printf("----------------");
224                 mput_char('-', strlen(title) + strlen(exec_path));
225                 putchar('\n');
226                 pretty_print_string_list(main_cmds, longest);
227                 putchar('\n');
228         }
229
230         if (other_cmds->cnt) {
231                 printf("%s available from elsewhere on your $PATH\n", title);
232                 printf("---------------------------------------");
233                 mput_char('-', strlen(title));
234                 putchar('\n');
235                 pretty_print_string_list(other_cmds, longest);
236                 putchar('\n');
237         }
238 }
239
240 int is_in_cmdlist(struct cmdnames *c, const char *s)
241 {
242         int i;
243         for (i = 0; i < c->cnt; i++)
244                 if (!strcmp(s, c->names[i]->name))
245                         return 1;
246         return 0;
247 }
248
249 static int autocorrect;
250 static struct cmdnames aliases;
251
252 static int perf_unknown_cmd_config(const char *var, const char *value, void *cb)
253 {
254         if (!strcmp(var, "help.autocorrect"))
255                 autocorrect = perf_config_int(var,value);
256         /* Also use aliases for command lookup */
257         if (!prefixcmp(var, "alias."))
258                 add_cmdname(&aliases, var + 6, strlen(var + 6));
259
260         return perf_default_config(var, value, cb);
261 }
262
263 static int levenshtein_compare(const void *p1, const void *p2)
264 {
265         const struct cmdname *const *c1 = p1, *const *c2 = p2;
266         const char *s1 = (*c1)->name, *s2 = (*c2)->name;
267         int l1 = (*c1)->len;
268         int l2 = (*c2)->len;
269         return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
270 }
271
272 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
273 {
274         int i;
275         ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
276
277         for (i = 0; i < old->cnt; i++)
278                 cmds->names[cmds->cnt++] = old->names[i];
279         free(old->names);
280         old->cnt = 0;
281         old->names = NULL;
282 }
283
284 const char *help_unknown_cmd(const char *cmd)
285 {
286         int i, n = 0, best_similarity = 0;
287         struct cmdnames main_cmds, other_cmds;
288
289         memset(&main_cmds, 0, sizeof(main_cmds));
290         memset(&other_cmds, 0, sizeof(main_cmds));
291         memset(&aliases, 0, sizeof(aliases));
292
293         perf_config(perf_unknown_cmd_config, NULL);
294
295         load_command_list("perf-", &main_cmds, &other_cmds);
296
297         add_cmd_list(&main_cmds, &aliases);
298         add_cmd_list(&main_cmds, &other_cmds);
299         qsort(main_cmds.names, main_cmds.cnt,
300               sizeof(main_cmds.names), cmdname_compare);
301         uniq(&main_cmds);
302
303         if (main_cmds.cnt) {
304                 /* This reuses cmdname->len for similarity index */
305                 for (i = 0; i < main_cmds.cnt; ++i)
306                         main_cmds.names[i]->len =
307                                 levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
308
309                 qsort(main_cmds.names, main_cmds.cnt,
310                       sizeof(*main_cmds.names), levenshtein_compare);
311
312                 best_similarity = main_cmds.names[0]->len;
313                 n = 1;
314                 while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
315                         ++n;
316         }
317
318         if (autocorrect && n == 1) {
319                 const char *assumed = main_cmds.names[0]->name;
320
321                 main_cmds.names[0] = NULL;
322                 clean_cmdnames(&main_cmds);
323                 fprintf(stderr, "WARNING: You called a Git program named '%s', "
324                         "which does not exist.\n"
325                         "Continuing under the assumption that you meant '%s'\n",
326                         cmd, assumed);
327                 if (autocorrect > 0) {
328                         fprintf(stderr, "in %0.1f seconds automatically...\n",
329                                 (float)autocorrect/10.0);
330                         poll(NULL, 0, autocorrect * 100);
331                 }
332                 return assumed;
333         }
334
335         fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd);
336
337         if (main_cmds.cnt && best_similarity < 6) {
338                 fprintf(stderr, "\nDid you mean %s?\n",
339                         n < 2 ? "this": "one of these");
340
341                 for (i = 0; i < n; i++)
342                         fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
343         }
344
345         exit(1);
346 }
347
348 int cmd_version(int argc, const char **argv, const char *prefix)
349 {
350         printf("perf version %s\n", perf_version_string);
351         return 0;
352 }