help.c 5.6 KB

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