at v2.6.32 8.3 kB view raw
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) */ 8static 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 29void add_cmdname(struct cmdnames *cmds, const char *name, size_t 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 41static void clean_cmdnames(struct cmdnames *cmds) 42{ 43 unsigned int i; 44 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; 50} 51 52static int cmdname_compare(const void *a_, const void *b_) 53{ 54 struct cmdname *a = *(struct cmdname **)a_; 55 struct cmdname *b = *(struct cmdname **)b_; 56 return strcmp(a->name, b->name); 57} 58 59static void uniq(struct cmdnames *cmds) 60{ 61 unsigned int i, j; 62 63 if (!cmds->cnt) 64 return; 65 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]; 69 70 cmds->cnt = j; 71} 72 73void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) 74{ 75 size_t ci, cj, ei; 76 int cmp; 77 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++; 87 } 88 89 while (ci < cmds->cnt) 90 cmds->names[cj++] = cmds->names[ci++]; 91 92 cmds->cnt = cj; 93} 94 95static void pretty_print_string_list(struct cmdnames *cmds, int longest) 96{ 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; 101 102 if (space < max_cols) 103 cols = max_cols / space; 104 rows = (cmds->cnt + cols - 1) / cols; 105 106 for (i = 0; i < rows; i++) { 107 printf(" "); 108 109 for (j = 0; j < cols; j++) { 110 unsigned int n = j * rows + i; 111 unsigned int size = space; 112 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); 118 } 119 putchar('\n'); 120 } 121} 122 123static int is_executable(const char *name) 124{ 125 struct stat st; 126 127 if (stat(name, &st) || /* stat, not lstat */ 128 !S_ISREG(st.st_mode)) 129 return 0; 130 131 return st.st_mode & S_IXUSR; 132} 133 134static void list_commands_in_dir(struct cmdnames *cmds, 135 const char *path, 136 const char *prefix) 137{ 138 int prefix_len; 139 DIR *dir = opendir(path); 140 struct dirent *de; 141 struct strbuf buf = STRBUF_INIT; 142 int len; 143 144 if (!dir) 145 return; 146 if (!prefix) 147 prefix = "perf-"; 148 prefix_len = strlen(prefix); 149 150 strbuf_addf(&buf, "%s/", path); 151 len = buf.len; 152 153 while ((de = readdir(dir)) != NULL) { 154 int entlen; 155 156 if (prefixcmp(de->d_name, prefix)) 157 continue; 158 159 strbuf_setlen(&buf, len); 160 strbuf_addstr(&buf, de->d_name); 161 if (!is_executable(buf.buf)) 162 continue; 163 164 entlen = strlen(de->d_name) - prefix_len; 165 if (has_extension(de->d_name, ".exe")) 166 entlen -= 4; 167 168 add_cmdname(cmds, de->d_name + prefix_len, entlen); 169 } 170 closedir(dir); 171 strbuf_release(&buf); 172} 173 174void load_command_list(const char *prefix, 175 struct cmdnames *main_cmds, 176 struct cmdnames *other_cmds) 177{ 178 const char *env_path = getenv("PATH"); 179 const char *exec_path = perf_exec_path(); 180 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); 186 } 187 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); 196 197 if (!colon) 198 break; 199 path = colon + 1; 200 } 201 free(paths); 202 203 qsort(other_cmds->names, other_cmds->cnt, 204 sizeof(*other_cmds->names), cmdname_compare); 205 uniq(other_cmds); 206 } 207 exclude_cmds(other_cmds, main_cmds); 208} 209 210void list_commands(const char *title, struct cmdnames *main_cmds, 211 struct cmdnames *other_cmds) 212{ 213 unsigned int i, longest = 0; 214 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; 221 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'); 230 } 231 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'); 239 } 240} 241 242int is_in_cmdlist(struct cmdnames *c, const char *s) 243{ 244 unsigned int i; 245 246 for (i = 0; i < c->cnt; i++) 247 if (!strcmp(s, c->names[i]->name)) 248 return 1; 249 return 0; 250} 251 252static int autocorrect; 253static struct cmdnames aliases; 254 255static int perf_unknown_cmd_config(const char *var, const char *value, void *cb) 256{ 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)); 262 263 return perf_default_config(var, value, cb); 264} 265 266static int levenshtein_compare(const void *p1, const void *p2) 267{ 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); 273} 274 275static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old) 276{ 277 unsigned int i; 278 279 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc); 280 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; 286} 287 288const char *help_unknown_cmd(const char *cmd) 289{ 290 unsigned int i, n = 0, best_similarity = 0; 291 struct cmdnames main_cmds, other_cmds; 292 293 memset(&main_cmds, 0, sizeof(main_cmds)); 294 memset(&other_cmds, 0, sizeof(main_cmds)); 295 memset(&aliases, 0, sizeof(aliases)); 296 297 perf_config(perf_unknown_cmd_config, NULL); 298 299 load_command_list("perf-", &main_cmds, &other_cmds); 300 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); 306 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); 312 313 qsort(main_cmds.names, main_cmds.cnt, 314 sizeof(*main_cmds.names), levenshtein_compare); 315 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; 320 } 321 322 if (autocorrect && n == 1) { 323 const char *assumed = main_cmds.names[0]->name; 324 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); 335 } 336 return assumed; 337 } 338 339 fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd); 340 341 if (main_cmds.cnt && best_similarity < 6) { 342 fprintf(stderr, "\nDid you mean %s?\n", 343 n < 2 ? "this": "one of these"); 344 345 for (i = 0; i < n; i++) 346 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name); 347 } 348 349 exit(1); 350} 351 352int cmd_version(int argc __used, const char **argv __used, const char *prefix __used) 353{ 354 printf("perf version %s\n", perf_version_string); 355 return 0; 356}