1#include "cache.h"
2#include "../builtin.h"
3#include "exec_cmd.h"
4#include "levenshtein.h"
5#include "help.h"
6
7void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
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
19static void clean_cmdnames(struct cmdnames *cmds)
20{
21	unsigned int i;
22
23	for (i = 0; i < cmds->cnt; ++i)
24		free(cmds->names[i]);
25	free(cmds->names);
26	cmds->cnt = 0;
27	cmds->alloc = 0;
28}
29
30static int cmdname_compare(const void *a_, const void *b_)
31{
32	struct cmdname *a = *(struct cmdname **)a_;
33	struct cmdname *b = *(struct cmdname **)b_;
34	return strcmp(a->name, b->name);
35}
36
37static void uniq(struct cmdnames *cmds)
38{
39	unsigned int i, j;
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{
53	size_t ci, cj, ei;
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 */
77	int max_cols;
78	int i, j;
79        /* ANDROID_CHANGE_BEGIN */
80#ifdef __BIONIC__
81        max_cols = 75;
82#else
83	struct winsize win;
84
85	get_term_dimensions(&win);
86	max_cols = win.ws_col - 1; /* don't print *on* the edge */
87#endif
88        /* ANDROID_CHANGE_END */
89
90	if (space < max_cols)
91		cols = max_cols / space;
92	rows = (cmds->cnt + cols - 1) / cols;
93
94	for (i = 0; i < rows; i++) {
95		printf("  ");
96
97		for (j = 0; j < cols; j++) {
98			unsigned int n = j * rows + i;
99			unsigned int size = space;
100
101			if (n >= cmds->cnt)
102				break;
103			if (j == cols-1 || n + rows >= cmds->cnt)
104				size = 1;
105			printf("%-*s", size, cmds->names[n]->name);
106		}
107		putchar('\n');
108	}
109}
110
111static int is_executable(const char *name)
112{
113	struct stat st;
114
115	if (stat(name, &st) || /* stat, not lstat */
116	    !S_ISREG(st.st_mode))
117		return 0;
118
119	return st.st_mode & S_IXUSR;
120}
121
122static void list_commands_in_dir(struct cmdnames *cmds,
123					 const char *path,
124					 const char *prefix)
125{
126	int prefix_len;
127	DIR *dir = opendir(path);
128	struct dirent *de;
129	struct strbuf buf = STRBUF_INIT;
130	int len;
131
132	if (!dir)
133		return;
134	if (!prefix)
135		prefix = "perf-";
136	prefix_len = strlen(prefix);
137
138	strbuf_addf(&buf, "%s/", path);
139	len = buf.len;
140
141	while ((de = readdir(dir)) != NULL) {
142		int entlen;
143
144		if (prefixcmp(de->d_name, prefix))
145			continue;
146
147		strbuf_setlen(&buf, len);
148		strbuf_addstr(&buf, de->d_name);
149		if (!is_executable(buf.buf))
150			continue;
151
152		entlen = strlen(de->d_name) - prefix_len;
153		if (has_extension(de->d_name, ".exe"))
154			entlen -= 4;
155
156		add_cmdname(cmds, de->d_name + prefix_len, entlen);
157	}
158	closedir(dir);
159	strbuf_release(&buf);
160}
161
162void load_command_list(const char *prefix,
163		struct cmdnames *main_cmds,
164		struct cmdnames *other_cmds)
165{
166	const char *env_path = getenv("PATH");
167	const char *exec_path = perf_exec_path();
168
169	if (exec_path) {
170		list_commands_in_dir(main_cmds, exec_path, prefix);
171		qsort(main_cmds->names, main_cmds->cnt,
172		      sizeof(*main_cmds->names), cmdname_compare);
173		uniq(main_cmds);
174	}
175
176	if (env_path) {
177		char *paths, *path, *colon;
178		path = paths = strdup(env_path);
179		while (1) {
180			if ((colon = strchr(path, PATH_SEP)))
181				*colon = 0;
182			if (!exec_path || strcmp(path, exec_path))
183				list_commands_in_dir(other_cmds, path, prefix);
184
185			if (!colon)
186				break;
187			path = colon + 1;
188		}
189		free(paths);
190
191		qsort(other_cmds->names, other_cmds->cnt,
192		      sizeof(*other_cmds->names), cmdname_compare);
193		uniq(other_cmds);
194	}
195	exclude_cmds(other_cmds, main_cmds);
196}
197
198void list_commands(const char *title, struct cmdnames *main_cmds,
199		   struct cmdnames *other_cmds)
200{
201	unsigned int i, longest = 0;
202
203	for (i = 0; i < main_cmds->cnt; i++)
204		if (longest < main_cmds->names[i]->len)
205			longest = main_cmds->names[i]->len;
206	for (i = 0; i < other_cmds->cnt; i++)
207		if (longest < other_cmds->names[i]->len)
208			longest = other_cmds->names[i]->len;
209
210	if (main_cmds->cnt) {
211		const char *exec_path = perf_exec_path();
212		printf("available %s in '%s'\n", title, exec_path);
213		printf("----------------");
214		mput_char('-', strlen(title) + strlen(exec_path));
215		putchar('\n');
216		pretty_print_string_list(main_cmds, longest);
217		putchar('\n');
218	}
219
220	if (other_cmds->cnt) {
221		printf("%s available from elsewhere on your $PATH\n", title);
222		printf("---------------------------------------");
223		mput_char('-', strlen(title));
224		putchar('\n');
225		pretty_print_string_list(other_cmds, longest);
226		putchar('\n');
227	}
228}
229
230int is_in_cmdlist(struct cmdnames *c, const char *s)
231{
232	unsigned int i;
233
234	for (i = 0; i < c->cnt; i++)
235		if (!strcmp(s, c->names[i]->name))
236			return 1;
237	return 0;
238}
239
240static int autocorrect;
241static struct cmdnames aliases;
242
243static int perf_unknown_cmd_config(const char *var, const char *value, void *cb)
244{
245	if (!strcmp(var, "help.autocorrect"))
246		autocorrect = perf_config_int(var,value);
247	/* Also use aliases for command lookup */
248	if (!prefixcmp(var, "alias."))
249		add_cmdname(&aliases, var + 6, strlen(var + 6));
250
251	return perf_default_config(var, value, cb);
252}
253
254static int levenshtein_compare(const void *p1, const void *p2)
255{
256	const struct cmdname *const *c1 = p1, *const *c2 = p2;
257	const char *s1 = (*c1)->name, *s2 = (*c2)->name;
258	int l1 = (*c1)->len;
259	int l2 = (*c2)->len;
260	return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
261}
262
263static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
264{
265	unsigned int i;
266
267	ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
268
269	for (i = 0; i < old->cnt; i++)
270		cmds->names[cmds->cnt++] = old->names[i];
271	free(old->names);
272	old->cnt = 0;
273	old->names = NULL;
274}
275
276const char *help_unknown_cmd(const char *cmd)
277{
278	unsigned int i, n = 0, best_similarity = 0;
279	struct cmdnames main_cmds, other_cmds;
280
281	memset(&main_cmds, 0, sizeof(main_cmds));
282	memset(&other_cmds, 0, sizeof(main_cmds));
283	memset(&aliases, 0, sizeof(aliases));
284
285	perf_config(perf_unknown_cmd_config, NULL);
286
287	load_command_list("perf-", &main_cmds, &other_cmds);
288
289	add_cmd_list(&main_cmds, &aliases);
290	add_cmd_list(&main_cmds, &other_cmds);
291	qsort(main_cmds.names, main_cmds.cnt,
292	      sizeof(main_cmds.names), cmdname_compare);
293	uniq(&main_cmds);
294
295	if (main_cmds.cnt) {
296		/* This reuses cmdname->len for similarity index */
297		for (i = 0; i < main_cmds.cnt; ++i)
298			main_cmds.names[i]->len =
299				levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
300
301		qsort(main_cmds.names, main_cmds.cnt,
302		      sizeof(*main_cmds.names), levenshtein_compare);
303
304		best_similarity = main_cmds.names[0]->len;
305		n = 1;
306		while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
307			++n;
308	}
309
310	if (autocorrect && n == 1) {
311		const char *assumed = main_cmds.names[0]->name;
312
313		main_cmds.names[0] = NULL;
314		clean_cmdnames(&main_cmds);
315		fprintf(stderr, "WARNING: You called a perf program named '%s', "
316			"which does not exist.\n"
317			"Continuing under the assumption that you meant '%s'\n",
318			cmd, assumed);
319		if (autocorrect > 0) {
320			fprintf(stderr, "in %0.1f seconds automatically...\n",
321				(float)autocorrect/10.0);
322			poll(NULL, 0, autocorrect * 100);
323		}
324		return assumed;
325	}
326
327	fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd);
328
329	if (main_cmds.cnt && best_similarity < 6) {
330		fprintf(stderr, "\nDid you mean %s?\n",
331			n < 2 ? "this": "one of these");
332
333		for (i = 0; i < n; i++)
334			fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
335	}
336
337	exit(1);
338}
339
340int cmd_version(int argc __used, const char **argv __used, const char *prefix __used)
341{
342	printf("perf version %s\n", perf_version_string);
343	return 0;
344}
345