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