help.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <linux/string.h>
  6. #include <termios.h>
  7. #include <sys/ioctl.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <unistd.h>
  11. #include <dirent.h>
  12. #include <assert.h>
  13. #include "subcmd-util.h"
  14. #include "help.h"
  15. #include "exec-cmd.h"
  16. void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
  17. {
  18. struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
  19. if (!ent)
  20. return;
  21. ent->len = len;
  22. memcpy(ent->name, name, len);
  23. ent->name[len] = 0;
  24. ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
  25. cmds->names[cmds->cnt++] = ent;
  26. }
  27. void clean_cmdnames(struct cmdnames *cmds)
  28. {
  29. unsigned int i;
  30. for (i = 0; i < cmds->cnt; ++i)
  31. zfree(&cmds->names[i]);
  32. zfree(&cmds->names);
  33. cmds->cnt = 0;
  34. cmds->alloc = 0;
  35. }
  36. int cmdname_compare(const void *a_, const void *b_)
  37. {
  38. struct cmdname *a = *(struct cmdname **)a_;
  39. struct cmdname *b = *(struct cmdname **)b_;
  40. return strcmp(a->name, b->name);
  41. }
  42. void uniq(struct cmdnames *cmds)
  43. {
  44. unsigned int i, j;
  45. if (!cmds->cnt)
  46. return;
  47. for (i = 1; i < cmds->cnt; i++) {
  48. if (!strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
  49. zfree(&cmds->names[i - 1]);
  50. }
  51. for (i = 0, j = 0; i < cmds->cnt; i++) {
  52. if (cmds->names[i]) {
  53. if (i == j)
  54. j++;
  55. else
  56. cmds->names[j++] = cmds->names[i];
  57. }
  58. }
  59. cmds->cnt = j;
  60. while (j < i)
  61. cmds->names[j++] = NULL;
  62. }
  63. void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
  64. {
  65. size_t ci, cj, ei;
  66. int cmp;
  67. if (!excludes->cnt)
  68. return;
  69. ci = cj = ei = 0;
  70. while (ci < cmds->cnt && ei < excludes->cnt) {
  71. cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
  72. if (cmp < 0) {
  73. if (ci == cj) {
  74. ci++;
  75. cj++;
  76. } else {
  77. cmds->names[cj++] = cmds->names[ci];
  78. cmds->names[ci++] = NULL;
  79. }
  80. } else if (cmp == 0) {
  81. zfree(&cmds->names[ci]);
  82. ci++;
  83. ei++;
  84. } else if (cmp > 0) {
  85. ei++;
  86. }
  87. }
  88. while (ci < cmds->cnt) {
  89. if (ci != cj) {
  90. cmds->names[cj] = cmds->names[ci];
  91. cmds->names[ci] = NULL;
  92. }
  93. ci++;
  94. cj++;
  95. }
  96. for (ci = cj; ci < cmds->cnt; ci++)
  97. assert(cmds->names[ci] == NULL);
  98. cmds->cnt = cj;
  99. }
  100. static void get_term_dimensions(struct winsize *ws)
  101. {
  102. char *s = getenv("LINES");
  103. if (s != NULL) {
  104. ws->ws_row = atoi(s);
  105. s = getenv("COLUMNS");
  106. if (s != NULL) {
  107. ws->ws_col = atoi(s);
  108. if (ws->ws_row && ws->ws_col)
  109. return;
  110. }
  111. }
  112. #ifdef TIOCGWINSZ
  113. if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
  114. ws->ws_row && ws->ws_col)
  115. return;
  116. #endif
  117. ws->ws_row = 25;
  118. ws->ws_col = 80;
  119. }
  120. static void pretty_print_string_list(struct cmdnames *cmds, int longest)
  121. {
  122. int cols = 1, rows;
  123. int space = longest + 1; /* min 1 SP between words */
  124. struct winsize win;
  125. int max_cols;
  126. int i, j;
  127. get_term_dimensions(&win);
  128. max_cols = win.ws_col - 1; /* don't print *on* the edge */
  129. if (space < max_cols)
  130. cols = max_cols / space;
  131. rows = (cmds->cnt + cols - 1) / cols;
  132. for (i = 0; i < rows; i++) {
  133. printf(" ");
  134. for (j = 0; j < cols; j++) {
  135. unsigned int n = j * rows + i;
  136. unsigned int size = space;
  137. if (n >= cmds->cnt)
  138. break;
  139. if (j == cols-1 || n + rows >= cmds->cnt)
  140. size = 1;
  141. printf("%-*s", size, cmds->names[n]->name);
  142. }
  143. putchar('\n');
  144. }
  145. }
  146. static int is_executable(const char *name)
  147. {
  148. struct stat st;
  149. if (stat(name, &st) || /* stat, not lstat */
  150. !S_ISREG(st.st_mode))
  151. return 0;
  152. return st.st_mode & S_IXUSR;
  153. }
  154. static int has_extension(const char *filename, const char *ext)
  155. {
  156. size_t len = strlen(filename);
  157. size_t extlen = strlen(ext);
  158. return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
  159. }
  160. static void list_commands_in_dir(struct cmdnames *cmds,
  161. const char *path,
  162. const char *prefix)
  163. {
  164. int prefix_len;
  165. DIR *dir = opendir(path);
  166. struct dirent *de;
  167. char *buf = NULL;
  168. if (!dir)
  169. return;
  170. if (!prefix)
  171. prefix = "perf-";
  172. prefix_len = strlen(prefix);
  173. astrcatf(&buf, "%s/", path);
  174. while ((de = readdir(dir)) != NULL) {
  175. int entlen;
  176. if (!strstarts(de->d_name, prefix))
  177. continue;
  178. astrcat(&buf, de->d_name);
  179. if (!is_executable(buf))
  180. continue;
  181. entlen = strlen(de->d_name) - prefix_len;
  182. if (has_extension(de->d_name, ".exe"))
  183. entlen -= 4;
  184. add_cmdname(cmds, de->d_name + prefix_len, entlen);
  185. }
  186. closedir(dir);
  187. free(buf);
  188. }
  189. void load_command_list(const char *prefix,
  190. struct cmdnames *main_cmds,
  191. struct cmdnames *other_cmds)
  192. {
  193. const char *env_path = getenv("PATH");
  194. char *exec_path = get_argv_exec_path();
  195. if (exec_path) {
  196. list_commands_in_dir(main_cmds, exec_path, prefix);
  197. qsort(main_cmds->names, main_cmds->cnt,
  198. sizeof(*main_cmds->names), cmdname_compare);
  199. uniq(main_cmds);
  200. }
  201. if (env_path) {
  202. char *paths, *path, *colon;
  203. path = paths = strdup(env_path);
  204. while (1) {
  205. if ((colon = strchr(path, ':')))
  206. *colon = 0;
  207. if (!exec_path || strcmp(path, exec_path))
  208. list_commands_in_dir(other_cmds, path, prefix);
  209. if (!colon)
  210. break;
  211. path = colon + 1;
  212. }
  213. free(paths);
  214. qsort(other_cmds->names, other_cmds->cnt,
  215. sizeof(*other_cmds->names), cmdname_compare);
  216. uniq(other_cmds);
  217. }
  218. free(exec_path);
  219. exclude_cmds(other_cmds, main_cmds);
  220. }
  221. void list_commands(const char *title, struct cmdnames *main_cmds,
  222. struct cmdnames *other_cmds)
  223. {
  224. unsigned int i, longest = 0;
  225. for (i = 0; i < main_cmds->cnt; i++)
  226. if (longest < main_cmds->names[i]->len)
  227. longest = main_cmds->names[i]->len;
  228. for (i = 0; i < other_cmds->cnt; i++)
  229. if (longest < other_cmds->names[i]->len)
  230. longest = other_cmds->names[i]->len;
  231. if (main_cmds->cnt) {
  232. char *exec_path = get_argv_exec_path();
  233. printf("available %s in '%s'\n", title, exec_path);
  234. printf("----------------");
  235. mput_char('-', strlen(title) + strlen(exec_path));
  236. putchar('\n');
  237. pretty_print_string_list(main_cmds, longest);
  238. putchar('\n');
  239. free(exec_path);
  240. }
  241. if (other_cmds->cnt) {
  242. printf("%s available from elsewhere on your $PATH\n", title);
  243. printf("---------------------------------------");
  244. mput_char('-', strlen(title));
  245. putchar('\n');
  246. pretty_print_string_list(other_cmds, longest);
  247. putchar('\n');
  248. }
  249. }
  250. int is_in_cmdlist(struct cmdnames *c, const char *s)
  251. {
  252. unsigned int i;
  253. for (i = 0; i < c->cnt; i++)
  254. if (!strcmp(s, c->names[i]->name))
  255. return 1;
  256. return 0;
  257. }