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