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