help.c 5.6 KB

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