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