2 #include "../builtin.h"
4 #include "levenshtein.h"
8 void add_cmdname(struct cmdnames
*cmds
, const char *name
, size_t len
)
10 struct cmdname
*ent
= malloc(sizeof(*ent
) + len
+ 1);
13 memcpy(ent
->name
, name
, len
);
16 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
17 cmds
->names
[cmds
->cnt
++] = ent
;
20 static void clean_cmdnames(struct cmdnames
*cmds
)
24 for (i
= 0; i
< cmds
->cnt
; ++i
)
31 static int cmdname_compare(const void *a_
, const void *b_
)
33 struct cmdname
*a
= *(struct cmdname
**)a_
;
34 struct cmdname
*b
= *(struct cmdname
**)b_
;
35 return strcmp(a
->name
, b
->name
);
38 static void uniq(struct cmdnames
*cmds
)
45 for (i
= j
= 1; i
< cmds
->cnt
; i
++)
46 if (strcmp(cmds
->names
[i
]->name
, cmds
->names
[i
-1]->name
))
47 cmds
->names
[j
++] = cmds
->names
[i
];
52 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
58 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
59 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
61 cmds
->names
[cj
++] = cmds
->names
[ci
++];
68 while (ci
< cmds
->cnt
)
69 cmds
->names
[cj
++] = cmds
->names
[ci
++];
74 static void pretty_print_string_list(struct cmdnames
*cmds
, int longest
)
77 int space
= longest
+ 1; /* min 1 SP between words */
82 get_term_dimensions(&win
);
83 max_cols
= win
.ws_col
- 1; /* don't print *on* the edge */
86 cols
= max_cols
/ space
;
87 rows
= (cmds
->cnt
+ cols
- 1) / cols
;
89 for (i
= 0; i
< rows
; i
++) {
92 for (j
= 0; j
< cols
; j
++) {
93 unsigned int n
= j
* rows
+ i
;
94 unsigned int size
= space
;
98 if (j
== cols
-1 || n
+ rows
>= cmds
->cnt
)
100 printf("%-*s", size
, cmds
->names
[n
]->name
);
106 static int is_executable(const char *name
)
110 if (stat(name
, &st
) || /* stat, not lstat */
111 !S_ISREG(st
.st_mode
))
114 return st
.st_mode
& S_IXUSR
;
117 static void list_commands_in_dir(struct cmdnames
*cmds
,
122 DIR *dir
= opendir(path
);
124 struct strbuf buf
= STRBUF_INIT
;
131 prefix_len
= strlen(prefix
);
133 strbuf_addf(&buf
, "%s/", path
);
136 while ((de
= readdir(dir
)) != NULL
) {
139 if (prefixcmp(de
->d_name
, prefix
))
142 strbuf_setlen(&buf
, len
);
143 strbuf_addstr(&buf
, de
->d_name
);
144 if (!is_executable(buf
.buf
))
147 entlen
= strlen(de
->d_name
) - prefix_len
;
148 if (has_extension(de
->d_name
, ".exe"))
151 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
154 strbuf_release(&buf
);
157 void load_command_list(const char *prefix
,
158 struct cmdnames
*main_cmds
,
159 struct cmdnames
*other_cmds
)
161 const char *env_path
= getenv("PATH");
162 const char *exec_path
= perf_exec_path();
165 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
166 qsort(main_cmds
->names
, main_cmds
->cnt
,
167 sizeof(*main_cmds
->names
), cmdname_compare
);
172 char *paths
, *path
, *colon
;
173 path
= paths
= strdup(env_path
);
175 if ((colon
= strchr(path
, PATH_SEP
)))
177 if (!exec_path
|| strcmp(path
, exec_path
))
178 list_commands_in_dir(other_cmds
, path
, prefix
);
186 qsort(other_cmds
->names
, other_cmds
->cnt
,
187 sizeof(*other_cmds
->names
), cmdname_compare
);
190 exclude_cmds(other_cmds
, main_cmds
);
193 void list_commands(const char *title
, struct cmdnames
*main_cmds
,
194 struct cmdnames
*other_cmds
)
196 unsigned int i
, longest
= 0;
198 for (i
= 0; i
< main_cmds
->cnt
; i
++)
199 if (longest
< main_cmds
->names
[i
]->len
)
200 longest
= main_cmds
->names
[i
]->len
;
201 for (i
= 0; i
< other_cmds
->cnt
; i
++)
202 if (longest
< other_cmds
->names
[i
]->len
)
203 longest
= other_cmds
->names
[i
]->len
;
205 if (main_cmds
->cnt
) {
206 const char *exec_path
= perf_exec_path();
207 printf("available %s in '%s'\n", title
, exec_path
);
208 printf("----------------");
209 mput_char('-', strlen(title
) + strlen(exec_path
));
211 pretty_print_string_list(main_cmds
, longest
);
215 if (other_cmds
->cnt
) {
216 printf("%s available from elsewhere on your $PATH\n", title
);
217 printf("---------------------------------------");
218 mput_char('-', strlen(title
));
220 pretty_print_string_list(other_cmds
, longest
);
225 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
229 for (i
= 0; i
< c
->cnt
; i
++)
230 if (!strcmp(s
, c
->names
[i
]->name
))
235 static int autocorrect
;
236 static struct cmdnames aliases
;
238 static int perf_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
240 if (!strcmp(var
, "help.autocorrect"))
241 autocorrect
= perf_config_int(var
,value
);
242 /* Also use aliases for command lookup */
243 if (!prefixcmp(var
, "alias."))
244 add_cmdname(&aliases
, var
+ 6, strlen(var
+ 6));
246 return perf_default_config(var
, value
, cb
);
249 static int levenshtein_compare(const void *p1
, const void *p2
)
251 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
252 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
255 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
258 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
262 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
264 for (i
= 0; i
< old
->cnt
; i
++)
265 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
271 const char *help_unknown_cmd(const char *cmd
)
273 unsigned int i
, n
= 0, best_similarity
= 0;
274 struct cmdnames main_cmds
, other_cmds
;
276 memset(&main_cmds
, 0, sizeof(main_cmds
));
277 memset(&other_cmds
, 0, sizeof(main_cmds
));
278 memset(&aliases
, 0, sizeof(aliases
));
280 perf_config(perf_unknown_cmd_config
, NULL
);
282 load_command_list("perf-", &main_cmds
, &other_cmds
);
284 add_cmd_list(&main_cmds
, &aliases
);
285 add_cmd_list(&main_cmds
, &other_cmds
);
286 qsort(main_cmds
.names
, main_cmds
.cnt
,
287 sizeof(main_cmds
.names
), cmdname_compare
);
291 /* This reuses cmdname->len for similarity index */
292 for (i
= 0; i
< main_cmds
.cnt
; ++i
)
293 main_cmds
.names
[i
]->len
=
294 levenshtein(cmd
, main_cmds
.names
[i
]->name
, 0, 2, 1, 4);
296 qsort(main_cmds
.names
, main_cmds
.cnt
,
297 sizeof(*main_cmds
.names
), levenshtein_compare
);
299 best_similarity
= main_cmds
.names
[0]->len
;
301 while (n
< main_cmds
.cnt
&& best_similarity
== main_cmds
.names
[n
]->len
)
305 if (autocorrect
&& n
== 1) {
306 const char *assumed
= main_cmds
.names
[0]->name
;
308 main_cmds
.names
[0] = NULL
;
309 clean_cmdnames(&main_cmds
);
310 fprintf(stderr
, "WARNING: You called a perf program named '%s', "
311 "which does not exist.\n"
312 "Continuing under the assumption that you meant '%s'\n",
314 if (autocorrect
> 0) {
315 fprintf(stderr
, "in %0.1f seconds automatically...\n",
316 (float)autocorrect
/10.0);
317 poll(NULL
, 0, autocorrect
* 100);
322 fprintf(stderr
, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd
);
324 if (main_cmds
.cnt
&& best_similarity
< 6) {
325 fprintf(stderr
, "\nDid you mean %s?\n",
326 n
< 2 ? "this": "one of these");
328 for (i
= 0; i
< n
; i
++)
329 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
335 int cmd_version(int argc __maybe_unused
, const char **argv __maybe_unused
,
336 const char *prefix __maybe_unused
)
338 printf("perf version %s\n", perf_version_string
);