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. void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
  7. {
  8. struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
  9. ent->len = len;
  10. memcpy(ent->name, name, len);
  11. ent->name[len] = 0;
  12. ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
  13. cmds->names[cmds->cnt++] = ent;
  14. }
  15. static void clean_cmdnames(struct cmdnames *cmds)
  16. {
  17. unsigned int i;
  18. for (i = 0; i < cmds->cnt; ++i)
  19. free(cmds->names[i]);
  20. free(cmds->names);
  21. cmds->cnt = 0;
  22. cmds->alloc = 0;
  23. }
  24. static int cmdname_compare(const void *a_, const void *b_)
  25. {
  26. struct cmdname *a = *(struct cmdname **)a_;
  27. struct cmdname *b = *(struct cmdname **)b_;
  28. return strcmp(a->name, b->name);
  29. }
  30. static void uniq(struct cmdnames *cmds)
  31. {
  32. unsigned int i, j;
  33. if (!cmds->cnt)
  34. return;
  35. for (i = j = 1; i < cmds->cnt; i++)
  36. if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
  37. cmds->names[j++] = cmds->names[i];
  38. cmds->cnt = j;
  39. }
  40. void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
  41. {
  42. size_t ci, cj, ei;
  43. int cmp;
  44. ci = cj = ei = 0;
  45. while (ci < cmds->cnt && ei < excludes->cnt) {
  46. cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
  47. if (cmp < 0)
  48. cmds->names[cj++] = cmds->names[ci++];
  49. else if (cmp == 0)
  50. ci++, ei++;
  51. else if (cmp > 0)
  52. ei++;
  53. }
  54. while (ci < cmds->cnt)
  55. cmds->names[cj++] = cmds->names[ci++];
  56. cmds->cnt = cj;
  57. }
  58. static void pretty_print_string_list(struct cmdnames *cmds, int longest)
  59. {
  60. int cols = 1, rows;
  61. int space = longest + 1; /* min 1 SP between words */
  62. struct winsize win;
  63. int max_cols;
  64. int i, j;
  65. get_term_dimensions(&win);
  66. max_cols = win.ws_col - 1; /* don't print *on* the edge */
  67. if (space < max_cols)
  68. cols = max_cols / space;
  69. rows = (cmds->cnt + cols - 1) / cols;
  70. for (i = 0; i < rows; i++) {
  71. printf(" ");
  72. for (j = 0; j < cols; j++) {
  73. unsigned int n = j * rows + i;
  74. unsigned int size = space;
  75. if (n >= cmds->cnt)
  76. break;
  77. if (j == cols-1 || n + rows >= cmds->cnt)
  78. size = 1;
  79. printf("%-*s", size, cmds->names[n]->name);
  80. }
  81. putchar('\n');
  82. }
  83. }
  84. static int is_executable(const char *name)
  85. {
  86. struct stat st;
  87. if (stat(name, &st) || /* stat, not lstat */
  88. !S_ISREG(st.st_mode))
  89. return 0;
  90. return st.st_mode & S_IXUSR;
  91. }
  92. static void list_commands_in_dir(struct cmdnames *cmds,
  93. const char *path,
  94. const char *prefix)
  95. {
  96. int prefix_len;
  97. DIR *dir = opendir(path);
  98. struct dirent *de;
  99. struct strbuf buf = STRBUF_INIT;
  100. int len;
  101. if (!dir)
  102. return;
  103. if (!prefix)
  104. prefix = "perf-";
  105. prefix_len = strlen(prefix);
  106. strbuf_addf(&buf, "%s/", path);
  107. len = buf.len;
  108. while ((de = readdir(dir)) != NULL) {
  109. int entlen;
  110. if (prefixcmp(de->d_name, prefix))
  111. continue;
  112. strbuf_setlen(&buf, len);
  113. strbuf_addstr(&buf, de->d_name);
  114. if (!is_executable(buf.buf))
  115. continue;
  116. entlen = strlen(de->d_name) - prefix_len;
  117. if (has_extension(de->d_name, ".exe"))
  118. entlen -= 4;
  119. add_cmdname(cmds, de->d_name + prefix_len, entlen);
  120. }
  121. closedir(dir);
  122. strbuf_release(&buf);
  123. }
  124. void load_command_list(const char *prefix,
  125. struct cmdnames *main_cmds,
  126. struct cmdnames *other_cmds)
  127. {
  128. const char *env_path = getenv("PATH");
  129. const char *exec_path = perf_exec_path();
  130. if (exec_path) {
  131. list_commands_in_dir(main_cmds, exec_path, prefix);
  132. qsort(main_cmds->names, main_cmds->cnt,
  133. sizeof(*main_cmds->names), cmdname_compare);
  134. uniq(main_cmds);
  135. }
  136. if (env_path) {
  137. char *paths, *path, *colon;
  138. path = paths = strdup(env_path);
  139. while (1) {
  140. if ((colon = strchr(path, PATH_SEP)))
  141. *colon = 0;
  142. if (!exec_path || strcmp(path, exec_path))
  143. list_commands_in_dir(other_cmds, path, prefix);
  144. if (!colon)
  145. break;
  146. path = colon + 1;
  147. }
  148. free(paths);
  149. qsort(other_cmds->names, other_cmds->cnt,
  150. sizeof(*other_cmds->names), cmdname_compare);
  151. uniq(other_cmds);
  152. }
  153. exclude_cmds(other_cmds, main_cmds);
  154. }
  155. void list_commands(const char *title, struct cmdnames *main_cmds,
  156. struct cmdnames *other_cmds)
  157. {
  158. unsigned int i, longest = 0;
  159. for (i = 0; i < main_cmds->cnt; i++)
  160. if (longest < main_cmds->names[i]->len)
  161. longest = main_cmds->names[i]->len;
  162. for (i = 0; i < other_cmds->cnt; i++)
  163. if (longest < other_cmds->names[i]->len)
  164. longest = other_cmds->names[i]->len;
  165. if (main_cmds->cnt) {
  166. const char *exec_path = perf_exec_path();
  167. printf("available %s in '%s'\n", title, exec_path);
  168. printf("----------------");
  169. mput_char('-', strlen(title) + strlen(exec_path));
  170. putchar('\n');
  171. pretty_print_string_list(main_cmds, longest);
  172. putchar('\n');
  173. }
  174. if (other_cmds->cnt) {
  175. printf("%s available from elsewhere on your $PATH\n", title);
  176. printf("---------------------------------------");
  177. mput_char('-', strlen(title));
  178. putchar('\n');
  179. pretty_print_string_list(other_cmds, longest);
  180. putchar('\n');
  181. }
  182. }
  183. int is_in_cmdlist(struct cmdnames *c, const char *s)
  184. {
  185. unsigned int i;
  186. for (i = 0; i < c->cnt; i++)
  187. if (!strcmp(s, c->names[i]->name))
  188. return 1;
  189. return 0;
  190. }
  191. static int autocorrect;
  192. static struct cmdnames aliases;
  193. static int perf_unknown_cmd_config(const char *var, const char *value, void *cb)
  194. {
  195. if (!strcmp(var, "help.autocorrect"))
  196. autocorrect = perf_config_int(var,value);
  197. /* Also use aliases for command lookup */
  198. if (!prefixcmp(var, "alias."))
  199. add_cmdname(&aliases, var + 6, strlen(var + 6));
  200. return perf_default_config(var, value, cb);
  201. }
  202. static int levenshtein_compare(const void *p1, const void *p2)
  203. {
  204. const struct cmdname *const *c1 = p1, *const *c2 = p2;
  205. const char *s1 = (*c1)->name, *s2 = (*c2)->name;
  206. int l1 = (*c1)->len;
  207. int l2 = (*c2)->len;
  208. return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
  209. }
  210. static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
  211. {
  212. unsigned int i;
  213. ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
  214. for (i = 0; i < old->cnt; i++)
  215. cmds->names[cmds->cnt++] = old->names[i];
  216. free(old->names);
  217. old->cnt = 0;
  218. old->names = NULL;
  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 __used, const char **argv __used, const char *prefix __used)
  271. {
  272. printf("perf version %s\n", perf_version_string);
  273. return 0;
  274. }