help.c 5.6 KB

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