2 #include "../builtin.h"
4 #include "levenshtein.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");
13 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
19 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
29 void add_cmdname(struct cmdnames
*cmds
, const char *name
, size_t len
)
31 struct cmdname
*ent
= malloc(sizeof(*ent
) + len
+ 1);
34 memcpy(ent
->name
, name
, len
);
37 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
38 cmds
->names
[cmds
->cnt
++] = ent
;
41 static void clean_cmdnames(struct cmdnames
*cmds
)
45 for (i
= 0; i
< cmds
->cnt
; ++i
)
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
)
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
];
73 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
79 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
80 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
82 cmds
->names
[cj
++] = cmds
->names
[ci
++];
89 while (ci
< cmds
->cnt
)
90 cmds
->names
[cj
++] = cmds
->names
[ci
++];
95 static void pretty_print_string_list(struct cmdnames
*cmds
, int longest
)
98 int space
= longest
+ 1; /* min 1 SP between words */
99 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
102 if (space
< max_cols
)
103 cols
= max_cols
/ space
;
104 rows
= (cmds
->cnt
+ cols
- 1) / cols
;
106 for (i
= 0; i
< rows
; i
++) {
109 for (j
= 0; j
< cols
; j
++) {
110 unsigned int n
= j
* rows
+ i
;
111 unsigned int size
= space
;
115 if (j
== cols
-1 || n
+ rows
>= cmds
->cnt
)
117 printf("%-*s", size
, cmds
->names
[n
]->name
);
123 static int is_executable(const char *name
)
127 if (stat(name
, &st
) || /* stat, not lstat */
128 !S_ISREG(st
.st_mode
))
131 return st
.st_mode
& S_IXUSR
;
134 static void list_commands_in_dir(struct cmdnames
*cmds
,
139 DIR *dir
= opendir(path
);
141 struct strbuf buf
= STRBUF_INIT
;
148 prefix_len
= strlen(prefix
);
150 strbuf_addf(&buf
, "%s/", path
);
153 while ((de
= readdir(dir
)) != NULL
) {
156 if (prefixcmp(de
->d_name
, prefix
))
159 strbuf_setlen(&buf
, len
);
160 strbuf_addstr(&buf
, de
->d_name
);
161 if (!is_executable(buf
.buf
))
164 entlen
= strlen(de
->d_name
) - prefix_len
;
165 if (has_extension(de
->d_name
, ".exe"))
168 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
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();
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
);
189 char *paths
, *path
, *colon
;
190 path
= paths
= strdup(env_path
);
192 if ((colon
= strchr(path
, PATH_SEP
)))
194 if (!exec_path
|| strcmp(path
, exec_path
))
195 list_commands_in_dir(other_cmds
, path
, prefix
);
203 qsort(other_cmds
->names
, other_cmds
->cnt
,
204 sizeof(*other_cmds
->names
), cmdname_compare
);
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
));
228 pretty_print_string_list(main_cmds
, longest
);
232 if (other_cmds
->cnt
) {
233 printf("%s available from elsewhere on your $PATH\n", title
);
234 printf("---------------------------------------");
235 mput_char('-', strlen(title
));
237 pretty_print_string_list(other_cmds
, longest
);
242 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
246 for (i
= 0; i
< c
->cnt
; i
++)
247 if (!strcmp(s
, c
->names
[i
]->name
))
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
;
272 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
275 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
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
];
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
);
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
;
318 while (n
< main_cmds
.cnt
&& best_similarity
== main_cmds
.names
[n
]->len
)
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",
331 if (autocorrect
> 0) {
332 fprintf(stderr
, "in %0.1f seconds automatically...\n",
333 (float)autocorrect
/10.0);
334 poll(NULL
, 0, autocorrect
* 100);
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
);
352 int cmd_version(int argc __used
, const char **argv __used
, const char *prefix __used
)
354 printf("perf version %s\n", perf_version_string
);