]>
Commit | Line | Data |
---|---|---|
2f4ce5ec JP |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <string.h> | |
96395cbb | 4 | #include <linux/string.h> |
2f4ce5ec JP |
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> | |
901421a5 | 11 | #include "subcmd-util.h" |
2f4ce5ec | 12 | #include "help.h" |
4b6ab94e | 13 | #include "exec-cmd.h" |
07800601 | 14 | |
f37a291c | 15 | void add_cmdname(struct cmdnames *cmds, const char *name, size_t len) |
07800601 IM |
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 | ||
5feaac24 | 27 | void clean_cmdnames(struct cmdnames *cmds) |
07800601 | 28 | { |
f37a291c IM |
29 | unsigned int i; |
30 | ||
07800601 | 31 | for (i = 0; i < cmds->cnt; ++i) |
74cf249d ACM |
32 | zfree(&cmds->names[i]); |
33 | zfree(&cmds->names); | |
07800601 IM |
34 | cmds->cnt = 0; |
35 | cmds->alloc = 0; | |
36 | } | |
37 | ||
5feaac24 | 38 | int cmdname_compare(const void *a_, const void *b_) |
07800601 IM |
39 | { |
40 | struct cmdname *a = *(struct cmdname **)a_; | |
41 | struct cmdname *b = *(struct cmdname **)b_; | |
42 | return strcmp(a->name, b->name); | |
43 | } | |
44 | ||
5feaac24 | 45 | void uniq(struct cmdnames *cmds) |
07800601 | 46 | { |
f37a291c | 47 | unsigned int i, j; |
07800601 IM |
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 | { | |
f37a291c | 61 | size_t ci, cj, ei; |
07800601 IM |
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 | ||
2f4ce5ec JP |
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 | ||
07800601 IM |
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 */ | |
a41794cd ACM |
107 | struct winsize win; |
108 | int max_cols; | |
07800601 IM |
109 | int i, j; |
110 | ||
a41794cd ACM |
111 | get_term_dimensions(&win); |
112 | max_cols = win.ws_col - 1; /* don't print *on* the edge */ | |
113 | ||
07800601 IM |
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++) { | |
f37a291c IM |
122 | unsigned int n = j * rows + i; |
123 | unsigned int size = space; | |
124 | ||
07800601 IM |
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 | ||
07800601 IM |
143 | return st.st_mode & S_IXUSR; |
144 | } | |
145 | ||
2f4ce5ec JP |
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 | ||
07800601 IM |
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; | |
901421a5 | 161 | char *buf = NULL; |
07800601 IM |
162 | |
163 | if (!dir) | |
164 | return; | |
165 | if (!prefix) | |
166 | prefix = "perf-"; | |
167 | prefix_len = strlen(prefix); | |
168 | ||
901421a5 | 169 | astrcatf(&buf, "%s/", path); |
07800601 IM |
170 | |
171 | while ((de = readdir(dir)) != NULL) { | |
172 | int entlen; | |
173 | ||
174 | if (prefixcmp(de->d_name, prefix)) | |
175 | continue; | |
176 | ||
901421a5 JP |
177 | astrcat(&buf, de->d_name); |
178 | if (!is_executable(buf)) | |
07800601 IM |
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); | |
901421a5 | 188 | free(buf); |
07800601 IM |
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"); | |
46113a54 | 196 | char *exec_path = get_argv_exec_path(); |
07800601 IM |
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) { | |
2f4ce5ec | 209 | if ((colon = strchr(path, ':'))) |
07800601 IM |
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 | } | |
c4068f51 | 224 | free(exec_path); |
07800601 IM |
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 | { | |
f37a291c | 231 | unsigned int i, longest = 0; |
07800601 IM |
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) { | |
46113a54 | 241 | char *exec_path = get_argv_exec_path(); |
07800601 IM |
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'); | |
c4068f51 | 248 | free(exec_path); |
07800601 IM |
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 | { | |
f37a291c IM |
263 | unsigned int i; |
264 | ||
07800601 IM |
265 | for (i = 0; i < c->cnt; i++) |
266 | if (!strcmp(s, c->names[i]->name)) | |
267 | return 1; | |
268 | return 0; | |
269 | } |