Linux 4.17.12
[linux/fpc-iii.git] / tools / lib / subcmd / help.c
blob2859f107abc802ef14c34e6847a50c13f8a798fe
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"
16 void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
18 struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
20 ent->len = len;
21 memcpy(ent->name, name, len);
22 ent->name[len] = 0;
24 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
25 cmds->names[cmds->cnt++] = ent;
28 void clean_cmdnames(struct cmdnames *cmds)
30 unsigned int i;
32 for (i = 0; i < cmds->cnt; ++i)
33 zfree(&cmds->names[i]);
34 zfree(&cmds->names);
35 cmds->cnt = 0;
36 cmds->alloc = 0;
39 int cmdname_compare(const void *a_, const void *b_)
41 struct cmdname *a = *(struct cmdname **)a_;
42 struct cmdname *b = *(struct cmdname **)b_;
43 return strcmp(a->name, b->name);
46 void uniq(struct cmdnames *cmds)
48 unsigned int i, j;
50 if (!cmds->cnt)
51 return;
53 for (i = j = 1; i < cmds->cnt; i++)
54 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
55 cmds->names[j++] = cmds->names[i];
57 cmds->cnt = j;
60 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
62 size_t ci, cj, ei;
63 int cmp;
65 ci = cj = ei = 0;
66 while (ci < cmds->cnt && ei < excludes->cnt) {
67 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
68 if (cmp < 0)
69 cmds->names[cj++] = cmds->names[ci++];
70 else if (cmp == 0)
71 ci++, ei++;
72 else if (cmp > 0)
73 ei++;
76 while (ci < cmds->cnt)
77 cmds->names[cj++] = cmds->names[ci++];
79 cmds->cnt = cj;
82 static void get_term_dimensions(struct winsize *ws)
84 char *s = getenv("LINES");
86 if (s != NULL) {
87 ws->ws_row = atoi(s);
88 s = getenv("COLUMNS");
89 if (s != NULL) {
90 ws->ws_col = atoi(s);
91 if (ws->ws_row && ws->ws_col)
92 return;
95 #ifdef TIOCGWINSZ
96 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
97 ws->ws_row && ws->ws_col)
98 return;
99 #endif
100 ws->ws_row = 25;
101 ws->ws_col = 80;
104 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
106 int cols = 1, rows;
107 int space = longest + 1; /* min 1 SP between words */
108 struct winsize win;
109 int max_cols;
110 int i, j;
112 get_term_dimensions(&win);
113 max_cols = win.ws_col - 1; /* don't print *on* the edge */
115 if (space < max_cols)
116 cols = max_cols / space;
117 rows = (cmds->cnt + cols - 1) / cols;
119 for (i = 0; i < rows; i++) {
120 printf(" ");
122 for (j = 0; j < cols; j++) {
123 unsigned int n = j * rows + i;
124 unsigned int size = space;
126 if (n >= cmds->cnt)
127 break;
128 if (j == cols-1 || n + rows >= cmds->cnt)
129 size = 1;
130 printf("%-*s", size, cmds->names[n]->name);
132 putchar('\n');
136 static int is_executable(const char *name)
138 struct stat st;
140 if (stat(name, &st) || /* stat, not lstat */
141 !S_ISREG(st.st_mode))
142 return 0;
144 return st.st_mode & S_IXUSR;
147 static int has_extension(const char *filename, const char *ext)
149 size_t len = strlen(filename);
150 size_t extlen = strlen(ext);
152 return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
155 static void list_commands_in_dir(struct cmdnames *cmds,
156 const char *path,
157 const char *prefix)
159 int prefix_len;
160 DIR *dir = opendir(path);
161 struct dirent *de;
162 char *buf = NULL;
164 if (!dir)
165 return;
166 if (!prefix)
167 prefix = "perf-";
168 prefix_len = strlen(prefix);
170 astrcatf(&buf, "%s/", path);
172 while ((de = readdir(dir)) != NULL) {
173 int entlen;
175 if (!strstarts(de->d_name, prefix))
176 continue;
178 astrcat(&buf, de->d_name);
179 if (!is_executable(buf))
180 continue;
182 entlen = strlen(de->d_name) - prefix_len;
183 if (has_extension(de->d_name, ".exe"))
184 entlen -= 4;
186 add_cmdname(cmds, de->d_name + prefix_len, entlen);
188 closedir(dir);
189 free(buf);
192 void load_command_list(const char *prefix,
193 struct cmdnames *main_cmds,
194 struct cmdnames *other_cmds)
196 const char *env_path = getenv("PATH");
197 char *exec_path = get_argv_exec_path();
199 if (exec_path) {
200 list_commands_in_dir(main_cmds, exec_path, prefix);
201 qsort(main_cmds->names, main_cmds->cnt,
202 sizeof(*main_cmds->names), cmdname_compare);
203 uniq(main_cmds);
206 if (env_path) {
207 char *paths, *path, *colon;
208 path = paths = strdup(env_path);
209 while (1) {
210 if ((colon = strchr(path, ':')))
211 *colon = 0;
212 if (!exec_path || strcmp(path, exec_path))
213 list_commands_in_dir(other_cmds, path, prefix);
215 if (!colon)
216 break;
217 path = colon + 1;
219 free(paths);
221 qsort(other_cmds->names, other_cmds->cnt,
222 sizeof(*other_cmds->names), cmdname_compare);
223 uniq(other_cmds);
225 free(exec_path);
226 exclude_cmds(other_cmds, main_cmds);
229 void list_commands(const char *title, struct cmdnames *main_cmds,
230 struct cmdnames *other_cmds)
232 unsigned int i, longest = 0;
234 for (i = 0; i < main_cmds->cnt; i++)
235 if (longest < main_cmds->names[i]->len)
236 longest = main_cmds->names[i]->len;
237 for (i = 0; i < other_cmds->cnt; i++)
238 if (longest < other_cmds->names[i]->len)
239 longest = other_cmds->names[i]->len;
241 if (main_cmds->cnt) {
242 char *exec_path = get_argv_exec_path();
243 printf("available %s in '%s'\n", title, exec_path);
244 printf("----------------");
245 mput_char('-', strlen(title) + strlen(exec_path));
246 putchar('\n');
247 pretty_print_string_list(main_cmds, longest);
248 putchar('\n');
249 free(exec_path);
252 if (other_cmds->cnt) {
253 printf("%s available from elsewhere on your $PATH\n", title);
254 printf("---------------------------------------");
255 mput_char('-', strlen(title));
256 putchar('\n');
257 pretty_print_string_list(other_cmds, longest);
258 putchar('\n');
262 int is_in_cmdlist(struct cmdnames *c, const char *s)
264 unsigned int i;
266 for (i = 0; i < c->cnt; i++)
267 if (!strcmp(s, c->names[i]->name))
268 return 1;
269 return 0;