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