1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/string.h>
12 #include "subcmd-util.h"
16 void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
18 struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
21 memcpy(ent->name, name, len);
24 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
25 cmds->names[cmds->cnt++] = ent;
28 void clean_cmdnames(struct cmdnames *cmds)
32 for (i = 0; i < cmds->cnt; ++i)
33 zfree(&cmds->names[i]);
39 int cmdname_compare(const void *a_, const void *b_)
41 struct cmdname *a = *(struct cmdname **)a_;
42 struct cmdname *b = *(struct cmdname **)b_;
43 return strcmp(a->name, b->name);
46 void uniq(struct cmdnames *cmds)
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]);
57 for (i = 0, j = 0; i < cmds->cnt; i++) {
62 cmds->names[j++] = cmds->names[i];
67 cmds->names[j++] = NULL;
70 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
76 while (ci < cmds->cnt && ei < excludes->cnt) {
77 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
79 cmds->names[cj++] = cmds->names[ci++];
80 } else if (cmp == 0) {
88 while (ci < cmds->cnt)
89 cmds->names[cj++] = cmds->names[ci++];
94 static void get_term_dimensions(struct winsize *ws)
96 char *s = getenv("LINES");
100 s = getenv("COLUMNS");
102 ws->ws_col = atoi(s);
103 if (ws->ws_row && ws->ws_col)
108 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
109 ws->ws_row && ws->ws_col)
116 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
119 int space = longest + 1; /* min 1 SP between words */
124 get_term_dimensions(&win);
125 max_cols = win.ws_col - 1; /* don't print *on* the edge */
127 if (space < max_cols)
128 cols = max_cols / space;
129 rows = (cmds->cnt + cols - 1) / cols;
131 for (i = 0; i < rows; i++) {
134 for (j = 0; j < cols; j++) {
135 unsigned int n = j * rows + i;
136 unsigned int size = space;
140 if (j == cols-1 || n + rows >= cmds->cnt)
142 printf("%-*s", size, cmds->names[n]->name);
148 static int is_executable(const char *name)
152 if (stat(name, &st) || /* stat, not lstat */
153 !S_ISREG(st.st_mode))
156 return st.st_mode & S_IXUSR;
159 static int has_extension(const char *filename, const char *ext)
161 size_t len = strlen(filename);
162 size_t extlen = strlen(ext);
164 return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
167 static void list_commands_in_dir(struct cmdnames *cmds,
172 DIR *dir = opendir(path);
180 prefix_len = strlen(prefix);
182 astrcatf(&buf, "%s/", path);
184 while ((de = readdir(dir)) != NULL) {
187 if (!strstarts(de->d_name, prefix))
190 astrcat(&buf, de->d_name);
191 if (!is_executable(buf))
194 entlen = strlen(de->d_name) - prefix_len;
195 if (has_extension(de->d_name, ".exe"))
198 add_cmdname(cmds, de->d_name + prefix_len, entlen);
204 void load_command_list(const char *prefix,
205 struct cmdnames *main_cmds,
206 struct cmdnames *other_cmds)
208 const char *env_path = getenv("PATH");
209 char *exec_path = get_argv_exec_path();
212 list_commands_in_dir(main_cmds, exec_path, prefix);
213 qsort(main_cmds->names, main_cmds->cnt,
214 sizeof(*main_cmds->names), cmdname_compare);
219 char *paths, *path, *colon;
220 path = paths = strdup(env_path);
222 if ((colon = strchr(path, ':')))
224 if (!exec_path || strcmp(path, exec_path))
225 list_commands_in_dir(other_cmds, path, prefix);
233 qsort(other_cmds->names, other_cmds->cnt,
234 sizeof(*other_cmds->names), cmdname_compare);
238 exclude_cmds(other_cmds, main_cmds);
241 void list_commands(const char *title, struct cmdnames *main_cmds,
242 struct cmdnames *other_cmds)
244 unsigned int i, longest = 0;
246 for (i = 0; i < main_cmds->cnt; i++)
247 if (longest < main_cmds->names[i]->len)
248 longest = main_cmds->names[i]->len;
249 for (i = 0; i < other_cmds->cnt; i++)
250 if (longest < other_cmds->names[i]->len)
251 longest = other_cmds->names[i]->len;
253 if (main_cmds->cnt) {
254 char *exec_path = get_argv_exec_path();
255 printf("available %s in '%s'\n", title, exec_path);
256 printf("----------------");
257 mput_char('-', strlen(title) + strlen(exec_path));
259 pretty_print_string_list(main_cmds, longest);
264 if (other_cmds->cnt) {
265 printf("%s available from elsewhere on your $PATH\n", title);
266 printf("---------------------------------------");
267 mput_char('-', strlen(title));
269 pretty_print_string_list(other_cmds, longest);
274 int is_in_cmdlist(struct cmdnames *c, const char *s)
278 for (i = 0; i < c->cnt; i++)
279 if (!strcmp(s, c->names[i]->name))