help.c 7.8 KB

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