help.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include "cache.h"
  2. #include "../builtin.h"
  3. #include "exec_cmd.h"
  4. #include "levenshtein.h"
  5. #include "help.h"
  6. /* most GUI terminals set COLUMNS (although some don't export it) */
  7. static int term_columns(void)
  8. {
  9. char *col_string = getenv("COLUMNS");
  10. int n_cols;
  11. if (col_string && (n_cols = atoi(col_string)) > 0)
  12. return n_cols;
  13. #ifdef TIOCGWINSZ
  14. {
  15. struct winsize ws;
  16. if (!ioctl(1, TIOCGWINSZ, &ws)) {
  17. if (ws.ws_col)
  18. return ws.ws_col;
  19. }
  20. }
  21. #endif
  22. return 80;
  23. }
  24. void add_cmdname(struct cmdnames *cmds, const char *name, int len)
  25. {
  26. struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
  27. ent->len = len;
  28. memcpy(ent->name, name, len);
  29. ent->name[len] = 0;
  30. ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
  31. cmds->names[cmds->cnt++] = ent;
  32. }
  33. static void clean_cmdnames(struct cmdnames *cmds)
  34. {
  35. int i;
  36. for (i = 0; i < cmds->cnt; ++i)
  37. free(cmds->names[i]);
  38. free(cmds->names);
  39. cmds->cnt = 0;
  40. cmds->alloc = 0;
  41. }
  42. static int cmdname_compare(const void *a_, const void *b_)
  43. {
  44. struct cmdname *a = *(struct cmdname **)a_;
  45. struct cmdname *b = *(struct cmdname **)b_;
  46. return strcmp(a->name, b->name);
  47. }
  48. static void uniq(struct cmdnames *cmds)
  49. {
  50. int i, j;
  51. if (!cmds->cnt)
  52. return;
  53. for (i = j = 1; i < cmds->cnt; i++)
  54. if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
  55. cmds->names[j++] = cmds->names[i];
  56. cmds->cnt = j;
  57. }
  58. void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
  59. {
  60. int ci, cj, ei;
  61. int cmp;
  62. ci = cj = ei = 0;
  63. while (ci < cmds->cnt && ei < excludes->cnt) {
  64. cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
  65. if (cmp < 0)
  66. cmds->names[cj++] = cmds->names[ci++];
  67. else if (cmp == 0)
  68. ci++, ei++;
  69. else if (cmp > 0)
  70. ei++;
  71. }
  72. while (ci < cmds->cnt)
  73. cmds->names[cj++] = cmds->names[ci++];
  74. cmds->cnt = cj;
  75. }
  76. static void pretty_print_string_list(struct cmdnames *cmds, int longest)
  77. {
  78. int cols = 1, rows;
  79. int space = longest + 1; /* min 1 SP between words */
  80. int max_cols = term_columns() - 1; /* don't print *on* the edge */
  81. int i, j;
  82. if (space < max_cols)
  83. cols = max_cols / space;
  84. rows = (cmds->cnt + cols - 1) / cols;
  85. for (i = 0; i < rows; i++) {
  86. printf(" ");
  87. for (j = 0; j < cols; j++) {
  88. int n = j * rows + i;
  89. int size = space;
  90. if (n >= cmds->cnt)
  91. break;
  92. if (j == cols-1 || n + rows >= cmds->cnt)
  93. size = 1;
  94. printf("%-*s", size, cmds->names[n]->name);
  95. }
  96. putchar('\n');
  97. }
  98. }
  99. static int is_executable(const char *name)
  100. {
  101. struct stat st;
  102. if (stat(name, &st) || /* stat, not lstat */
  103. !S_ISREG(st.st_mode))
  104. return 0;
  105. #ifdef __MINGW32__
  106. /* cannot trust the executable bit, peek into the file instead */
  107. char buf[3] = { 0 };
  108. int n;
  109. int fd = open(name, O_RDONLY);
  110. st.st_mode &= ~S_IXUSR;
  111. if (fd >= 0) {
  112. n = read(fd, buf, 2);
  113. if (n == 2)
  114. /* DOS executables start with "MZ" */
  115. if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
  116. st.st_mode |= S_IXUSR;
  117. close(fd);
  118. }
  119. #endif
  120. return st.st_mode & S_IXUSR;
  121. }
  122. static void list_commands_in_dir(struct cmdnames *cmds,
  123. const char *path,
  124. const char *prefix)
  125. {
  126. int prefix_len;
  127. DIR *dir = opendir(path);
  128. struct dirent *de;
  129. struct strbuf buf = STRBUF_INIT;
  130. int len;
  131. if (!dir)
  132. return;
  133. if (!prefix)
  134. prefix = "perf-";
  135. prefix_len = strlen(prefix);
  136. strbuf_addf(&buf, "%s/", path);
  137. len = buf.len;
  138. while ((de = readdir(dir)) != NULL) {
  139. int entlen;
  140. if (prefixcmp(de->d_name, prefix))
  141. continue;
  142. strbuf_setlen(&buf, len);
  143. strbuf_addstr(&buf, de->d_name);
  144. if (!is_executable(buf.buf))
  145. continue;
  146. entlen = strlen(de->d_name) - prefix_len;
  147. if (has_extension(de->d_name, ".exe"))
  148. entlen -= 4;
  149. add_cmdname(cmds, de->d_name + prefix_len, entlen);
  150. }
  151. closedir(dir);
  152. strbuf_release(&buf);
  153. }
  154. void load_command_list(const char *prefix,
  155. struct cmdnames *main_cmds,
  156. struct cmdnames *other_cmds)
  157. {
  158. const char *env_path = getenv("PATH");
  159. const char *exec_path = perf_exec_path();
  160. if (exec_path) {
  161. list_commands_in_dir(main_cmds, exec_path, prefix);
  162. qsort(main_cmds->names, main_cmds->cnt,
  163. sizeof(*main_cmds->names), cmdname_compare);
  164. uniq(main_cmds);
  165. }
  166. if (env_path) {
  167. char *paths, *path, *colon;
  168. path = paths = strdup(env_path);
  169. while (1) {
  170. if ((colon = strchr(path, PATH_SEP)))
  171. *colon = 0;
  172. if (!exec_path || strcmp(path, exec_path))
  173. list_commands_in_dir(other_cmds, path, prefix);
  174. if (!colon)
  175. break;
  176. path = colon + 1;
  177. }
  178. free(paths);
  179. qsort(other_cmds->names, other_cmds->cnt,
  180. sizeof(*other_cmds->names), cmdname_compare);
  181. uniq(other_cmds);
  182. }
  183. exclude_cmds(other_cmds, main_cmds);
  184. }
  185. void list_commands(const char *title, struct cmdnames *main_cmds,
  186. struct cmdnames *other_cmds)
  187. {
  188. int i, longest = 0;
  189. for (i = 0; i < main_cmds->cnt; i++)
  190. if (longest < main_cmds->names[i]->len)
  191. longest = main_cmds->names[i]->len;
  192. for (i = 0; i < other_cmds->cnt; i++)
  193. if (longest < other_cmds->names[i]->len)
  194. longest = other_cmds->names[i]->len;
  195. if (main_cmds->cnt) {
  196. const char *exec_path = perf_exec_path();
  197. printf("available %s in '%s'\n", title, exec_path);
  198. printf("----------------");
  199. mput_char('-', strlen(title) + strlen(exec_path));
  200. putchar('\n');
  201. pretty_print_string_list(main_cmds, longest);
  202. putchar('\n');
  203. }
  204. if (other_cmds->cnt) {
  205. printf("%s available from elsewhere on your $PATH\n", title);
  206. printf("---------------------------------------");
  207. mput_char('-', strlen(title));
  208. putchar('\n');
  209. pretty_print_string_list(other_cmds, longest);
  210. putchar('\n');
  211. }
  212. }
  213. int is_in_cmdlist(struct cmdnames *c, const char *s)
  214. {
  215. int i;
  216. for (i = 0; i < c->cnt; i++)
  217. if (!strcmp(s, c->names[i]->name))
  218. return 1;
  219. return 0;
  220. }
  221. static int autocorrect;
  222. static struct cmdnames aliases;
  223. static int perf_unknown_cmd_config(const char *var, const char *value, void *cb)
  224. {
  225. if (!strcmp(var, "help.autocorrect"))
  226. autocorrect = perf_config_int(var,value);
  227. /* Also use aliases for command lookup */
  228. if (!prefixcmp(var, "alias."))
  229. add_cmdname(&aliases, var + 6, strlen(var + 6));
  230. return perf_default_config(var, value, cb);
  231. }
  232. static int levenshtein_compare(const void *p1, const void *p2)
  233. {
  234. const struct cmdname *const *c1 = p1, *const *c2 = p2;
  235. const char *s1 = (*c1)->name, *s2 = (*c2)->name;
  236. int l1 = (*c1)->len;
  237. int l2 = (*c2)->len;
  238. return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
  239. }
  240. static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
  241. {
  242. int i;
  243. ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
  244. for (i = 0; i < old->cnt; i++)
  245. cmds->names[cmds->cnt++] = old->names[i];
  246. free(old->names);
  247. old->cnt = 0;
  248. old->names = NULL;
  249. }
  250. const char *help_unknown_cmd(const char *cmd)
  251. {
  252. int i, n = 0, best_similarity = 0;
  253. struct cmdnames main_cmds, other_cmds;
  254. memset(&main_cmds, 0, sizeof(main_cmds));
  255. memset(&other_cmds, 0, sizeof(main_cmds));
  256. memset(&aliases, 0, sizeof(aliases));
  257. perf_config(perf_unknown_cmd_config, NULL);
  258. load_command_list("perf-", &main_cmds, &other_cmds);
  259. add_cmd_list(&main_cmds, &aliases);
  260. add_cmd_list(&main_cmds, &other_cmds);
  261. qsort(main_cmds.names, main_cmds.cnt,
  262. sizeof(main_cmds.names), cmdname_compare);
  263. uniq(&main_cmds);
  264. if (main_cmds.cnt) {
  265. /* This reuses cmdname->len for similarity index */
  266. for (i = 0; i < main_cmds.cnt; ++i)
  267. main_cmds.names[i]->len =
  268. levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
  269. qsort(main_cmds.names, main_cmds.cnt,
  270. sizeof(*main_cmds.names), levenshtein_compare);
  271. best_similarity = main_cmds.names[0]->len;
  272. n = 1;
  273. while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
  274. ++n;
  275. }
  276. if (autocorrect && n == 1) {
  277. const char *assumed = main_cmds.names[0]->name;
  278. main_cmds.names[0] = NULL;
  279. clean_cmdnames(&main_cmds);
  280. fprintf(stderr, "WARNING: You called a Git program named '%s', "
  281. "which does not exist.\n"
  282. "Continuing under the assumption that you meant '%s'\n",
  283. cmd, assumed);
  284. if (autocorrect > 0) {
  285. fprintf(stderr, "in %0.1f seconds automatically...\n",
  286. (float)autocorrect/10.0);
  287. poll(NULL, 0, autocorrect * 100);
  288. }
  289. return assumed;
  290. }
  291. fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd);
  292. if (main_cmds.cnt && best_similarity < 6) {
  293. fprintf(stderr, "\nDid you mean %s?\n",
  294. n < 2 ? "this": "one of these");
  295. for (i = 0; i < n; i++)
  296. fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
  297. }
  298. exit(1);
  299. }
  300. int cmd_version(int argc, const char **argv, const char *prefix)
  301. {
  302. printf("perf version %s\n", perf_version_string);
  303. return 0;
  304. }