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