help.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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, size_t 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. unsigned 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. unsigned 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. size_t 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. unsigned int n = j * rows + i;
  89. unsigned 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. return st.st_mode & S_IXUSR;
  106. }
  107. static void list_commands_in_dir(struct cmdnames *cmds,
  108. const char *path,
  109. const char *prefix)
  110. {
  111. int prefix_len;
  112. DIR *dir = opendir(path);
  113. struct dirent *de;
  114. struct strbuf buf = STRBUF_INIT;
  115. int len;
  116. if (!dir)
  117. return;
  118. if (!prefix)
  119. prefix = "perf-";
  120. prefix_len = strlen(prefix);
  121. strbuf_addf(&buf, "%s/", path);
  122. len = buf.len;
  123. while ((de = readdir(dir)) != NULL) {
  124. int entlen;
  125. if (prefixcmp(de->d_name, prefix))
  126. continue;
  127. strbuf_setlen(&buf, len);
  128. strbuf_addstr(&buf, de->d_name);
  129. if (!is_executable(buf.buf))
  130. continue;
  131. entlen = strlen(de->d_name) - prefix_len;
  132. if (has_extension(de->d_name, ".exe"))
  133. entlen -= 4;
  134. add_cmdname(cmds, de->d_name + prefix_len, entlen);
  135. }
  136. closedir(dir);
  137. strbuf_release(&buf);
  138. }
  139. void load_command_list(const char *prefix,
  140. struct cmdnames *main_cmds,
  141. struct cmdnames *other_cmds)
  142. {
  143. const char *env_path = getenv("PATH");
  144. const char *exec_path = perf_exec_path();
  145. if (exec_path) {
  146. list_commands_in_dir(main_cmds, exec_path, prefix);
  147. qsort(main_cmds->names, main_cmds->cnt,
  148. sizeof(*main_cmds->names), cmdname_compare);
  149. uniq(main_cmds);
  150. }
  151. if (env_path) {
  152. char *paths, *path, *colon;
  153. path = paths = strdup(env_path);
  154. while (1) {
  155. if ((colon = strchr(path, PATH_SEP)))
  156. *colon = 0;
  157. if (!exec_path || strcmp(path, exec_path))
  158. list_commands_in_dir(other_cmds, path, prefix);
  159. if (!colon)
  160. break;
  161. path = colon + 1;
  162. }
  163. free(paths);
  164. qsort(other_cmds->names, other_cmds->cnt,
  165. sizeof(*other_cmds->names), cmdname_compare);
  166. uniq(other_cmds);
  167. }
  168. exclude_cmds(other_cmds, main_cmds);
  169. }
  170. void list_commands(const char *title, struct cmdnames *main_cmds,
  171. struct cmdnames *other_cmds)
  172. {
  173. unsigned int i, longest = 0;
  174. for (i = 0; i < main_cmds->cnt; i++)
  175. if (longest < main_cmds->names[i]->len)
  176. longest = main_cmds->names[i]->len;
  177. for (i = 0; i < other_cmds->cnt; i++)
  178. if (longest < other_cmds->names[i]->len)
  179. longest = other_cmds->names[i]->len;
  180. if (main_cmds->cnt) {
  181. const char *exec_path = perf_exec_path();
  182. printf("available %s in '%s'\n", title, exec_path);
  183. printf("----------------");
  184. mput_char('-', strlen(title) + strlen(exec_path));
  185. putchar('\n');
  186. pretty_print_string_list(main_cmds, longest);
  187. putchar('\n');
  188. }
  189. if (other_cmds->cnt) {
  190. printf("%s available from elsewhere on your $PATH\n", title);
  191. printf("---------------------------------------");
  192. mput_char('-', strlen(title));
  193. putchar('\n');
  194. pretty_print_string_list(other_cmds, longest);
  195. putchar('\n');
  196. }
  197. }
  198. int is_in_cmdlist(struct cmdnames *c, const char *s)
  199. {
  200. unsigned int i;
  201. for (i = 0; i < c->cnt; i++)
  202. if (!strcmp(s, c->names[i]->name))
  203. return 1;
  204. return 0;
  205. }
  206. static int autocorrect;
  207. static struct cmdnames aliases;
  208. static int perf_unknown_cmd_config(const char *var, const char *value, void *cb)
  209. {
  210. if (!strcmp(var, "help.autocorrect"))
  211. autocorrect = perf_config_int(var,value);
  212. /* Also use aliases for command lookup */
  213. if (!prefixcmp(var, "alias."))
  214. add_cmdname(&aliases, var + 6, strlen(var + 6));
  215. return perf_default_config(var, value, cb);
  216. }
  217. static int levenshtein_compare(const void *p1, const void *p2)
  218. {
  219. const struct cmdname *const *c1 = p1, *const *c2 = p2;
  220. const char *s1 = (*c1)->name, *s2 = (*c2)->name;
  221. int l1 = (*c1)->len;
  222. int l2 = (*c2)->len;
  223. return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
  224. }
  225. static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
  226. {
  227. unsigned int i;
  228. ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
  229. for (i = 0; i < old->cnt; i++)
  230. cmds->names[cmds->cnt++] = old->names[i];
  231. free(old->names);
  232. old->cnt = 0;
  233. old->names = NULL;
  234. }
  235. const char *help_unknown_cmd(const char *cmd)
  236. {
  237. unsigned int i, n = 0, best_similarity = 0;
  238. struct cmdnames main_cmds, other_cmds;
  239. memset(&main_cmds, 0, sizeof(main_cmds));
  240. memset(&other_cmds, 0, sizeof(main_cmds));
  241. memset(&aliases, 0, sizeof(aliases));
  242. perf_config(perf_unknown_cmd_config, NULL);
  243. load_command_list("perf-", &main_cmds, &other_cmds);
  244. add_cmd_list(&main_cmds, &aliases);
  245. add_cmd_list(&main_cmds, &other_cmds);
  246. qsort(main_cmds.names, main_cmds.cnt,
  247. sizeof(main_cmds.names), cmdname_compare);
  248. uniq(&main_cmds);
  249. if (main_cmds.cnt) {
  250. /* This reuses cmdname->len for similarity index */
  251. for (i = 0; i < main_cmds.cnt; ++i)
  252. main_cmds.names[i]->len =
  253. levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
  254. qsort(main_cmds.names, main_cmds.cnt,
  255. sizeof(*main_cmds.names), levenshtein_compare);
  256. best_similarity = main_cmds.names[0]->len;
  257. n = 1;
  258. while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
  259. ++n;
  260. }
  261. if (autocorrect && n == 1) {
  262. const char *assumed = main_cmds.names[0]->name;
  263. main_cmds.names[0] = NULL;
  264. clean_cmdnames(&main_cmds);
  265. fprintf(stderr, "WARNING: You called a Git program named '%s', "
  266. "which does not exist.\n"
  267. "Continuing under the assumption that you meant '%s'\n",
  268. cmd, assumed);
  269. if (autocorrect > 0) {
  270. fprintf(stderr, "in %0.1f seconds automatically...\n",
  271. (float)autocorrect/10.0);
  272. poll(NULL, 0, autocorrect * 100);
  273. }
  274. return assumed;
  275. }
  276. fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd);
  277. if (main_cmds.cnt && best_similarity < 6) {
  278. fprintf(stderr, "\nDid you mean %s?\n",
  279. n < 2 ? "this": "one of these");
  280. for (i = 0; i < n; i++)
  281. fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
  282. }
  283. exit(1);
  284. }
  285. int cmd_version(int argc __used, const char **argv __used, const char *prefix __used)
  286. {
  287. printf("perf version %s\n", perf_version_string);
  288. return 0;
  289. }