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);
23 memcpy(ent->name, name, len);
26 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
27 cmds->names[cmds->cnt++] = ent;
30 void clean_cmdnames(struct cmdnames *cmds)
34 for (i = 0; i < cmds->cnt; ++i)
35 zfree(&cmds->names[i]);
41 int cmdname_compare(const void *a_, const void *b_)
43 struct cmdname *a = *(struct cmdname **)a_;
44 struct cmdname *b = *(struct cmdname **)b_;
45 return strcmp(a->name, b->name);
48 void uniq(struct cmdnames *cmds)
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];
62 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
68 while (ci < cmds->cnt && ei < excludes->cnt) {
69 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
75 zfree(&cmds->names[cj]);
76 cmds->names[cj++] = cmds->names[ci++];
78 } else if (cmp == 0) {
86 while (ci < cmds->cnt) {
87 zfree(&cmds->names[cj]);
88 cmds->names[cj++] = cmds->names[ci++];
91 for (ci = cj; ci < cmds->cnt; ci++)
92 zfree(&cmds->names[ci]);
96 static void get_term_dimensions(struct winsize *ws)
98 char *s = getenv("LINES");
101 ws->ws_row = atoi(s);
102 s = getenv("COLUMNS");
104 ws->ws_col = atoi(s);
105 if (ws->ws_row && ws->ws_col)
110 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
111 ws->ws_row && ws->ws_col)
118 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
121 int space = longest + 1; /* min 1 SP between words */
126 get_term_dimensions(&win);
127 max_cols = win.ws_col - 1; /* don't print *on* the edge */
129 if (space < max_cols)
130 cols = max_cols / space;
131 rows = (cmds->cnt + cols - 1) / cols;
133 for (i = 0; i < rows; i++) {
136 for (j = 0; j < cols; j++) {
137 unsigned int n = j * rows + i;
138 unsigned int size = space;
142 if (j == cols-1 || n + rows >= cmds->cnt)
144 printf("%-*s", size, cmds->names[n]->name);
150 static int is_executable(const char *name)
154 if (stat(name, &st) || /* stat, not lstat */
155 !S_ISREG(st.st_mode))
158 return st.st_mode & S_IXUSR;
161 static int has_extension(const char *filename, const char *ext)
163 size_t len = strlen(filename);
164 size_t extlen = strlen(ext);
166 return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
169 static void list_commands_in_dir(struct cmdnames *cmds,
174 DIR *dir = opendir(path);
182 prefix_len = strlen(prefix);
184 astrcatf(&buf, "%s/", path);
186 while ((de = readdir(dir)) != NULL) {
189 if (!strstarts(de->d_name, prefix))
192 astrcat(&buf, de->d_name);
193 if (!is_executable(buf))
196 entlen = strlen(de->d_name) - prefix_len;
197 if (has_extension(de->d_name, ".exe"))
200 add_cmdname(cmds, de->d_name + prefix_len, entlen);
206 void load_command_list(const char *prefix,
207 struct cmdnames *main_cmds,
208 struct cmdnames *other_cmds)
210 const char *env_path = getenv("PATH");
211 char *exec_path = get_argv_exec_path();
214 list_commands_in_dir(main_cmds, exec_path, prefix);
215 qsort(main_cmds->names, main_cmds->cnt,
216 sizeof(*main_cmds->names), cmdname_compare);
221 char *paths, *path, *colon;
222 path = paths = strdup(env_path);
224 if ((colon = strchr(path, ':')))
226 if (!exec_path || strcmp(path, exec_path))
227 list_commands_in_dir(other_cmds, path, prefix);
235 qsort(other_cmds->names, other_cmds->cnt,
236 sizeof(*other_cmds->names), cmdname_compare);
240 exclude_cmds(other_cmds, main_cmds);
243 void list_commands(const char *title, struct cmdnames *main_cmds,
244 struct cmdnames *other_cmds)
246 unsigned int i, longest = 0;
248 for (i = 0; i < main_cmds->cnt; i++)
249 if (longest < main_cmds->names[i]->len)
250 longest = main_cmds->names[i]->len;
251 for (i = 0; i < other_cmds->cnt; i++)
252 if (longest < other_cmds->names[i]->len)
253 longest = other_cmds->names[i]->len;
255 if (main_cmds->cnt) {
256 char *exec_path = get_argv_exec_path();
257 printf("available %s in '%s'\n", title, exec_path);
258 printf("----------------");
259 mput_char('-', strlen(title) + strlen(exec_path));
261 pretty_print_string_list(main_cmds, longest);
266 if (other_cmds->cnt) {
267 printf("%s available from elsewhere on your $PATH\n", title);
268 printf("---------------------------------------");
269 mput_char('-', strlen(title));
271 pretty_print_string_list(other_cmds, longest);
276 int is_in_cmdlist(struct cmdnames *c, const char *s)
280 for (i = 0; i < c->cnt; i++)
281 if (!strcmp(s, c->names[i]->name))