]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/util/help.c
perf tools: Remove 'perf' from subcmd function and variable names
[mirror_ubuntu-artful-kernel.git] / tools / perf / util / help.c
CommitLineData
07800601 1#include "cache.h"
148be2c1 2#include "../builtin.h"
07800601 3#include "exec_cmd.h"
07800601 4#include "help.h"
901421a5 5#include "subcmd-util.h"
07800601 6
f37a291c 7void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
07800601
IM
8{
9 struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
10
11 ent->len = len;
12 memcpy(ent->name, name, len);
13 ent->name[len] = 0;
14
15 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
16 cmds->names[cmds->cnt++] = ent;
17}
18
5feaac24 19void clean_cmdnames(struct cmdnames *cmds)
07800601 20{
f37a291c
IM
21 unsigned int i;
22
07800601 23 for (i = 0; i < cmds->cnt; ++i)
74cf249d
ACM
24 zfree(&cmds->names[i]);
25 zfree(&cmds->names);
07800601
IM
26 cmds->cnt = 0;
27 cmds->alloc = 0;
28}
29
5feaac24 30int cmdname_compare(const void *a_, const void *b_)
07800601
IM
31{
32 struct cmdname *a = *(struct cmdname **)a_;
33 struct cmdname *b = *(struct cmdname **)b_;
34 return strcmp(a->name, b->name);
35}
36
5feaac24 37void uniq(struct cmdnames *cmds)
07800601 38{
f37a291c 39 unsigned int i, j;
07800601
IM
40
41 if (!cmds->cnt)
42 return;
43
44 for (i = j = 1; i < cmds->cnt; i++)
45 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
46 cmds->names[j++] = cmds->names[i];
47
48 cmds->cnt = j;
49}
50
51void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
52{
f37a291c 53 size_t ci, cj, ei;
07800601
IM
54 int cmp;
55
56 ci = cj = ei = 0;
57 while (ci < cmds->cnt && ei < excludes->cnt) {
58 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
59 if (cmp < 0)
60 cmds->names[cj++] = cmds->names[ci++];
61 else if (cmp == 0)
62 ci++, ei++;
63 else if (cmp > 0)
64 ei++;
65 }
66
67 while (ci < cmds->cnt)
68 cmds->names[cj++] = cmds->names[ci++];
69
70 cmds->cnt = cj;
71}
72
73static void pretty_print_string_list(struct cmdnames *cmds, int longest)
74{
75 int cols = 1, rows;
76 int space = longest + 1; /* min 1 SP between words */
a41794cd
ACM
77 struct winsize win;
78 int max_cols;
07800601
IM
79 int i, j;
80
a41794cd
ACM
81 get_term_dimensions(&win);
82 max_cols = win.ws_col - 1; /* don't print *on* the edge */
83
07800601
IM
84 if (space < max_cols)
85 cols = max_cols / space;
86 rows = (cmds->cnt + cols - 1) / cols;
87
88 for (i = 0; i < rows; i++) {
89 printf(" ");
90
91 for (j = 0; j < cols; j++) {
f37a291c
IM
92 unsigned int n = j * rows + i;
93 unsigned int size = space;
94
07800601
IM
95 if (n >= cmds->cnt)
96 break;
97 if (j == cols-1 || n + rows >= cmds->cnt)
98 size = 1;
99 printf("%-*s", size, cmds->names[n]->name);
100 }
101 putchar('\n');
102 }
103}
104
105static int is_executable(const char *name)
106{
107 struct stat st;
108
109 if (stat(name, &st) || /* stat, not lstat */
110 !S_ISREG(st.st_mode))
111 return 0;
112
07800601
IM
113 return st.st_mode & S_IXUSR;
114}
115
116static void list_commands_in_dir(struct cmdnames *cmds,
117 const char *path,
118 const char *prefix)
119{
120 int prefix_len;
121 DIR *dir = opendir(path);
122 struct dirent *de;
901421a5 123 char *buf = NULL;
07800601
IM
124
125 if (!dir)
126 return;
127 if (!prefix)
128 prefix = "perf-";
129 prefix_len = strlen(prefix);
130
901421a5 131 astrcatf(&buf, "%s/", path);
07800601
IM
132
133 while ((de = readdir(dir)) != NULL) {
134 int entlen;
135
136 if (prefixcmp(de->d_name, prefix))
137 continue;
138
901421a5
JP
139 astrcat(&buf, de->d_name);
140 if (!is_executable(buf))
07800601
IM
141 continue;
142
143 entlen = strlen(de->d_name) - prefix_len;
144 if (has_extension(de->d_name, ".exe"))
145 entlen -= 4;
146
147 add_cmdname(cmds, de->d_name + prefix_len, entlen);
148 }
149 closedir(dir);
901421a5 150 free(buf);
07800601
IM
151}
152
153void load_command_list(const char *prefix,
154 struct cmdnames *main_cmds,
155 struct cmdnames *other_cmds)
156{
157 const char *env_path = getenv("PATH");
46113a54 158 char *exec_path = get_argv_exec_path();
07800601
IM
159
160 if (exec_path) {
161 list_commands_in_dir(main_cmds, exec_path, prefix);
162 qsort(main_cmds->names, main_cmds->cnt,
163 sizeof(*main_cmds->names), cmdname_compare);
164 uniq(main_cmds);
165 }
166
167 if (env_path) {
168 char *paths, *path, *colon;
169 path = paths = strdup(env_path);
170 while (1) {
171 if ((colon = strchr(path, PATH_SEP)))
172 *colon = 0;
173 if (!exec_path || strcmp(path, exec_path))
174 list_commands_in_dir(other_cmds, path, prefix);
175
176 if (!colon)
177 break;
178 path = colon + 1;
179 }
180 free(paths);
181
182 qsort(other_cmds->names, other_cmds->cnt,
183 sizeof(*other_cmds->names), cmdname_compare);
184 uniq(other_cmds);
185 }
c4068f51 186 free(exec_path);
07800601
IM
187 exclude_cmds(other_cmds, main_cmds);
188}
189
190void list_commands(const char *title, struct cmdnames *main_cmds,
191 struct cmdnames *other_cmds)
192{
f37a291c 193 unsigned int i, longest = 0;
07800601
IM
194
195 for (i = 0; i < main_cmds->cnt; i++)
196 if (longest < main_cmds->names[i]->len)
197 longest = main_cmds->names[i]->len;
198 for (i = 0; i < other_cmds->cnt; i++)
199 if (longest < other_cmds->names[i]->len)
200 longest = other_cmds->names[i]->len;
201
202 if (main_cmds->cnt) {
46113a54 203 char *exec_path = get_argv_exec_path();
07800601
IM
204 printf("available %s in '%s'\n", title, exec_path);
205 printf("----------------");
206 mput_char('-', strlen(title) + strlen(exec_path));
207 putchar('\n');
208 pretty_print_string_list(main_cmds, longest);
209 putchar('\n');
c4068f51 210 free(exec_path);
07800601
IM
211 }
212
213 if (other_cmds->cnt) {
214 printf("%s available from elsewhere on your $PATH\n", title);
215 printf("---------------------------------------");
216 mput_char('-', strlen(title));
217 putchar('\n');
218 pretty_print_string_list(other_cmds, longest);
219 putchar('\n');
220 }
221}
222
223int is_in_cmdlist(struct cmdnames *c, const char *s)
224{
f37a291c
IM
225 unsigned int i;
226
07800601
IM
227 for (i = 0; i < c->cnt; i++)
228 if (!strcmp(s, c->names[i]->name))
229 return 1;
230 return 0;
231}