help.c 5.7 KB

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