blob: 0310520f918e450c06babea8042d13b5dd390d10 [file] [log] [blame]
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -06001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
Arnaldo Carvalho de Melo96395cb2017-04-26 15:49:21 -03004#include <linux/string.h>
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -06005#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>
Josh Poimboeuf901421a2015-12-15 09:39:36 -060011#include "subcmd-util.h"
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -060012#include "help.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060013#include "exec-cmd.h"
Ingo Molnar07800602009-04-20 15:00:56 +020014
Ingo Molnarf37a2912009-07-01 12:37:06 +020015void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
Ingo Molnar07800602009-04-20 15:00:56 +020016{
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
Josh Poimboeuf5feaac22015-12-13 22:18:09 -060027void clean_cmdnames(struct cmdnames *cmds)
Ingo Molnar07800602009-04-20 15:00:56 +020028{
Ingo Molnarf37a2912009-07-01 12:37:06 +020029 unsigned int i;
30
Ingo Molnar07800602009-04-20 15:00:56 +020031 for (i = 0; i < cmds->cnt; ++i)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -030032 zfree(&cmds->names[i]);
33 zfree(&cmds->names);
Ingo Molnar07800602009-04-20 15:00:56 +020034 cmds->cnt = 0;
35 cmds->alloc = 0;
36}
37
Josh Poimboeuf5feaac22015-12-13 22:18:09 -060038int cmdname_compare(const void *a_, const void *b_)
Ingo Molnar07800602009-04-20 15:00:56 +020039{
40 struct cmdname *a = *(struct cmdname **)a_;
41 struct cmdname *b = *(struct cmdname **)b_;
42 return strcmp(a->name, b->name);
43}
44
Josh Poimboeuf5feaac22015-12-13 22:18:09 -060045void uniq(struct cmdnames *cmds)
Ingo Molnar07800602009-04-20 15:00:56 +020046{
Ingo Molnarf37a2912009-07-01 12:37:06 +020047 unsigned int i, j;
Ingo Molnar07800602009-04-20 15:00:56 +020048
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
59void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
60{
Ingo Molnarf37a2912009-07-01 12:37:06 +020061 size_t ci, cj, ei;
Ingo Molnar07800602009-04-20 15:00:56 +020062 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
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -060081static 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
Ingo Molnar07800602009-04-20 15:00:56 +0200103static 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 */
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -0300107 struct winsize win;
108 int max_cols;
Ingo Molnar07800602009-04-20 15:00:56 +0200109 int i, j;
110
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -0300111 get_term_dimensions(&win);
112 max_cols = win.ws_col - 1; /* don't print *on* the edge */
113
Ingo Molnar07800602009-04-20 15:00:56 +0200114 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++) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200122 unsigned int n = j * rows + i;
123 unsigned int size = space;
124
Ingo Molnar07800602009-04-20 15:00:56 +0200125 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
135static 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
Ingo Molnar07800602009-04-20 15:00:56 +0200143 return st.st_mode & S_IXUSR;
144}
145
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -0600146static 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
Ingo Molnar07800602009-04-20 15:00:56 +0200154static 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;
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600161 char *buf = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200162
163 if (!dir)
164 return;
165 if (!prefix)
166 prefix = "perf-";
167 prefix_len = strlen(prefix);
168
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600169 astrcatf(&buf, "%s/", path);
Ingo Molnar07800602009-04-20 15:00:56 +0200170
171 while ((de = readdir(dir)) != NULL) {
172 int entlen;
173
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300174 if (!strstarts(de->d_name, prefix))
Ingo Molnar07800602009-04-20 15:00:56 +0200175 continue;
176
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600177 astrcat(&buf, de->d_name);
178 if (!is_executable(buf))
Ingo Molnar07800602009-04-20 15:00:56 +0200179 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);
Josh Poimboeuf901421a2015-12-15 09:39:36 -0600188 free(buf);
Ingo Molnar07800602009-04-20 15:00:56 +0200189}
190
191void load_command_list(const char *prefix,
192 struct cmdnames *main_cmds,
193 struct cmdnames *other_cmds)
194{
195 const char *env_path = getenv("PATH");
Josh Poimboeuf46113a52015-12-15 09:39:37 -0600196 char *exec_path = get_argv_exec_path();
Ingo Molnar07800602009-04-20 15:00:56 +0200197
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) {
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -0600209 if ((colon = strchr(path, ':')))
Ingo Molnar07800602009-04-20 15:00:56 +0200210 *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 }
Masami Hiramatsuc4068f52015-11-19 15:04:53 +0900224 free(exec_path);
Ingo Molnar07800602009-04-20 15:00:56 +0200225 exclude_cmds(other_cmds, main_cmds);
226}
227
228void list_commands(const char *title, struct cmdnames *main_cmds,
229 struct cmdnames *other_cmds)
230{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200231 unsigned int i, longest = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200232
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) {
Josh Poimboeuf46113a52015-12-15 09:39:37 -0600241 char *exec_path = get_argv_exec_path();
Ingo Molnar07800602009-04-20 15:00:56 +0200242 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');
Masami Hiramatsuc4068f52015-11-19 15:04:53 +0900248 free(exec_path);
Ingo Molnar07800602009-04-20 15:00:56 +0200249 }
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
261int is_in_cmdlist(struct cmdnames *c, const char *s)
262{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200263 unsigned int i;
264
Ingo Molnar07800602009-04-20 15:00:56 +0200265 for (i = 0; i < c->cnt; i++)
266 if (!strcmp(s, c->names[i]->name))
267 return 1;
268 return 0;
269}