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