ipv6: drop unused "dev" arg of icmpv6_send()
[linux-2.6/next.git] / tools / perf / util / help.c
blobfbb00978b2e2988045f4f970964b7c5826c1cba4
1 #include "cache.h"
2 #include "../builtin.h"
3 #include "exec_cmd.h"
4 #include "levenshtein.h"
5 #include "help.h"
7 /* most GUI terminals set COLUMNS (although some don't export it) */
8 static int term_columns(void)
10 char *col_string = getenv("COLUMNS");
11 int n_cols;
13 if (col_string && (n_cols = atoi(col_string)) > 0)
14 return n_cols;
16 #ifdef TIOCGWINSZ
18 struct winsize ws;
19 if (!ioctl(1, TIOCGWINSZ, &ws)) {
20 if (ws.ws_col)
21 return ws.ws_col;
24 #endif
26 return 80;
29 void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
31 struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
33 ent->len = len;
34 memcpy(ent->name, name, len);
35 ent->name[len] = 0;
37 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
38 cmds->names[cmds->cnt++] = ent;
41 static void clean_cmdnames(struct cmdnames *cmds)
43 unsigned int i;
45 for (i = 0; i < cmds->cnt; ++i)
46 free(cmds->names[i]);
47 free(cmds->names);
48 cmds->cnt = 0;
49 cmds->alloc = 0;
52 static int cmdname_compare(const void *a_, const void *b_)
54 struct cmdname *a = *(struct cmdname **)a_;
55 struct cmdname *b = *(struct cmdname **)b_;
56 return strcmp(a->name, b->name);
59 static void uniq(struct cmdnames *cmds)
61 unsigned int i, j;
63 if (!cmds->cnt)
64 return;
66 for (i = j = 1; i < cmds->cnt; i++)
67 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
68 cmds->names[j++] = cmds->names[i];
70 cmds->cnt = j;
73 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
75 size_t ci, cj, ei;
76 int cmp;
78 ci = cj = ei = 0;
79 while (ci < cmds->cnt && ei < excludes->cnt) {
80 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
81 if (cmp < 0)
82 cmds->names[cj++] = cmds->names[ci++];
83 else if (cmp == 0)
84 ci++, ei++;
85 else if (cmp > 0)
86 ei++;
89 while (ci < cmds->cnt)
90 cmds->names[cj++] = cmds->names[ci++];
92 cmds->cnt = cj;
95 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
97 int cols = 1, rows;
98 int space = longest + 1; /* min 1 SP between words */
99 int max_cols = term_columns() - 1; /* don't print *on* the edge */
100 int i, j;
102 if (space < max_cols)
103 cols = max_cols / space;
104 rows = (cmds->cnt + cols - 1) / cols;
106 for (i = 0; i < rows; i++) {
107 printf(" ");
109 for (j = 0; j < cols; j++) {
110 unsigned int n = j * rows + i;
111 unsigned int size = space;
113 if (n >= cmds->cnt)
114 break;
115 if (j == cols-1 || n + rows >= cmds->cnt)
116 size = 1;
117 printf("%-*s", size, cmds->names[n]->name);
119 putchar('\n');
123 static int is_executable(const char *name)
125 struct stat st;
127 if (stat(name, &st) || /* stat, not lstat */
128 !S_ISREG(st.st_mode))
129 return 0;
131 return st.st_mode & S_IXUSR;
134 static void list_commands_in_dir(struct cmdnames *cmds,
135 const char *path,
136 const char *prefix)
138 int prefix_len;
139 DIR *dir = opendir(path);
140 struct dirent *de;
141 struct strbuf buf = STRBUF_INIT;
142 int len;
144 if (!dir)
145 return;
146 if (!prefix)
147 prefix = "perf-";
148 prefix_len = strlen(prefix);
150 strbuf_addf(&buf, "%s/", path);
151 len = buf.len;
153 while ((de = readdir(dir)) != NULL) {
154 int entlen;
156 if (prefixcmp(de->d_name, prefix))
157 continue;
159 strbuf_setlen(&buf, len);
160 strbuf_addstr(&buf, de->d_name);
161 if (!is_executable(buf.buf))
162 continue;
164 entlen = strlen(de->d_name) - prefix_len;
165 if (has_extension(de->d_name, ".exe"))
166 entlen -= 4;
168 add_cmdname(cmds, de->d_name + prefix_len, entlen);
170 closedir(dir);
171 strbuf_release(&buf);
174 void load_command_list(const char *prefix,
175 struct cmdnames *main_cmds,
176 struct cmdnames *other_cmds)
178 const char *env_path = getenv("PATH");
179 const char *exec_path = perf_exec_path();
181 if (exec_path) {
182 list_commands_in_dir(main_cmds, exec_path, prefix);
183 qsort(main_cmds->names, main_cmds->cnt,
184 sizeof(*main_cmds->names), cmdname_compare);
185 uniq(main_cmds);
188 if (env_path) {
189 char *paths, *path, *colon;
190 path = paths = strdup(env_path);
191 while (1) {
192 if ((colon = strchr(path, PATH_SEP)))
193 *colon = 0;
194 if (!exec_path || strcmp(path, exec_path))
195 list_commands_in_dir(other_cmds, path, prefix);
197 if (!colon)
198 break;
199 path = colon + 1;
201 free(paths);
203 qsort(other_cmds->names, other_cmds->cnt,
204 sizeof(*other_cmds->names), cmdname_compare);
205 uniq(other_cmds);
207 exclude_cmds(other_cmds, main_cmds);
210 void list_commands(const char *title, struct cmdnames *main_cmds,
211 struct cmdnames *other_cmds)
213 unsigned int i, longest = 0;
215 for (i = 0; i < main_cmds->cnt; i++)
216 if (longest < main_cmds->names[i]->len)
217 longest = main_cmds->names[i]->len;
218 for (i = 0; i < other_cmds->cnt; i++)
219 if (longest < other_cmds->names[i]->len)
220 longest = other_cmds->names[i]->len;
222 if (main_cmds->cnt) {
223 const char *exec_path = perf_exec_path();
224 printf("available %s in '%s'\n", title, exec_path);
225 printf("----------------");
226 mput_char('-', strlen(title) + strlen(exec_path));
227 putchar('\n');
228 pretty_print_string_list(main_cmds, longest);
229 putchar('\n');
232 if (other_cmds->cnt) {
233 printf("%s available from elsewhere on your $PATH\n", title);
234 printf("---------------------------------------");
235 mput_char('-', strlen(title));
236 putchar('\n');
237 pretty_print_string_list(other_cmds, longest);
238 putchar('\n');
242 int is_in_cmdlist(struct cmdnames *c, const char *s)
244 unsigned int i;
246 for (i = 0; i < c->cnt; i++)
247 if (!strcmp(s, c->names[i]->name))
248 return 1;
249 return 0;
252 static int autocorrect;
253 static struct cmdnames aliases;
255 static int perf_unknown_cmd_config(const char *var, const char *value, void *cb)
257 if (!strcmp(var, "help.autocorrect"))
258 autocorrect = perf_config_int(var,value);
259 /* Also use aliases for command lookup */
260 if (!prefixcmp(var, "alias."))
261 add_cmdname(&aliases, var + 6, strlen(var + 6));
263 return perf_default_config(var, value, cb);
266 static int levenshtein_compare(const void *p1, const void *p2)
268 const struct cmdname *const *c1 = p1, *const *c2 = p2;
269 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
270 int l1 = (*c1)->len;
271 int l2 = (*c2)->len;
272 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
275 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
277 unsigned int i;
279 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
281 for (i = 0; i < old->cnt; i++)
282 cmds->names[cmds->cnt++] = old->names[i];
283 free(old->names);
284 old->cnt = 0;
285 old->names = NULL;
288 const char *help_unknown_cmd(const char *cmd)
290 unsigned int i, n = 0, best_similarity = 0;
291 struct cmdnames main_cmds, other_cmds;
293 memset(&main_cmds, 0, sizeof(main_cmds));
294 memset(&other_cmds, 0, sizeof(main_cmds));
295 memset(&aliases, 0, sizeof(aliases));
297 perf_config(perf_unknown_cmd_config, NULL);
299 load_command_list("perf-", &main_cmds, &other_cmds);
301 add_cmd_list(&main_cmds, &aliases);
302 add_cmd_list(&main_cmds, &other_cmds);
303 qsort(main_cmds.names, main_cmds.cnt,
304 sizeof(main_cmds.names), cmdname_compare);
305 uniq(&main_cmds);
307 if (main_cmds.cnt) {
308 /* This reuses cmdname->len for similarity index */
309 for (i = 0; i < main_cmds.cnt; ++i)
310 main_cmds.names[i]->len =
311 levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
313 qsort(main_cmds.names, main_cmds.cnt,
314 sizeof(*main_cmds.names), levenshtein_compare);
316 best_similarity = main_cmds.names[0]->len;
317 n = 1;
318 while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
319 ++n;
322 if (autocorrect && n == 1) {
323 const char *assumed = main_cmds.names[0]->name;
325 main_cmds.names[0] = NULL;
326 clean_cmdnames(&main_cmds);
327 fprintf(stderr, "WARNING: You called a Git program named '%s', "
328 "which does not exist.\n"
329 "Continuing under the assumption that you meant '%s'\n",
330 cmd, assumed);
331 if (autocorrect > 0) {
332 fprintf(stderr, "in %0.1f seconds automatically...\n",
333 (float)autocorrect/10.0);
334 poll(NULL, 0, autocorrect * 100);
336 return assumed;
339 fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd);
341 if (main_cmds.cnt && best_similarity < 6) {
342 fprintf(stderr, "\nDid you mean %s?\n",
343 n < 2 ? "this": "one of these");
345 for (i = 0; i < n; i++)
346 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
349 exit(1);
352 int cmd_version(int argc __used, const char **argv __used, const char *prefix __used)
354 printf("perf version %s\n", perf_version_string);
355 return 0;