xref: /linux/tools/perf/util/help-unknown-cmd.c (revision 3d689ed6099a1a11c38bb78aff7498e78e287e0b)
1 #include "cache.h"
2 #include "config.h"
3 #include <stdio.h>
4 #include <subcmd/help.h>
5 #include "../builtin.h"
6 #include "levenshtein.h"
7 
8 static int autocorrect;
9 
10 static int perf_unknown_cmd_config(const char *var, const char *value,
11 				   void *cb __maybe_unused)
12 {
13 	if (!strcmp(var, "help.autocorrect"))
14 		autocorrect = perf_config_int(var,value);
15 
16 	return 0;
17 }
18 
19 static int levenshtein_compare(const void *p1, const void *p2)
20 {
21 	const struct cmdname *const *c1 = p1, *const *c2 = p2;
22 	const char *s1 = (*c1)->name, *s2 = (*c2)->name;
23 	int l1 = (*c1)->len;
24 	int l2 = (*c2)->len;
25 	return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
26 }
27 
28 static int add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
29 {
30 	unsigned int i, nr = cmds->cnt + old->cnt;
31 	void *tmp;
32 
33 	if (nr > cmds->alloc) {
34 		/* Choose bigger one to alloc */
35 		if (alloc_nr(cmds->alloc) < nr)
36 			cmds->alloc = nr;
37 		else
38 			cmds->alloc = alloc_nr(cmds->alloc);
39 		tmp = realloc(cmds->names, cmds->alloc * sizeof(*cmds->names));
40 		if (!tmp)
41 			return -1;
42 		cmds->names = tmp;
43 	}
44 	for (i = 0; i < old->cnt; i++)
45 		cmds->names[cmds->cnt++] = old->names[i];
46 	zfree(&old->names);
47 	old->cnt = 0;
48 	return 0;
49 }
50 
51 const char *help_unknown_cmd(const char *cmd)
52 {
53 	unsigned int i, n = 0, best_similarity = 0;
54 	struct cmdnames main_cmds, other_cmds;
55 
56 	memset(&main_cmds, 0, sizeof(main_cmds));
57 	memset(&other_cmds, 0, sizeof(main_cmds));
58 
59 	perf_config(perf_unknown_cmd_config, NULL);
60 
61 	load_command_list("perf-", &main_cmds, &other_cmds);
62 
63 	if (add_cmd_list(&main_cmds, &other_cmds) < 0) {
64 		fprintf(stderr, "ERROR: Failed to allocate command list for unknown command.\n");
65 		goto end;
66 	}
67 	qsort(main_cmds.names, main_cmds.cnt,
68 	      sizeof(main_cmds.names), cmdname_compare);
69 	uniq(&main_cmds);
70 
71 	if (main_cmds.cnt) {
72 		/* This reuses cmdname->len for similarity index */
73 		for (i = 0; i < main_cmds.cnt; ++i)
74 			main_cmds.names[i]->len =
75 				levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
76 
77 		qsort(main_cmds.names, main_cmds.cnt,
78 		      sizeof(*main_cmds.names), levenshtein_compare);
79 
80 		best_similarity = main_cmds.names[0]->len;
81 		n = 1;
82 		while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
83 			++n;
84 	}
85 
86 	if (autocorrect && n == 1) {
87 		const char *assumed = main_cmds.names[0]->name;
88 
89 		main_cmds.names[0] = NULL;
90 		clean_cmdnames(&main_cmds);
91 		fprintf(stderr, "WARNING: You called a perf program named '%s', "
92 			"which does not exist.\n"
93 			"Continuing under the assumption that you meant '%s'\n",
94 			cmd, assumed);
95 		if (autocorrect > 0) {
96 			fprintf(stderr, "in %0.1f seconds automatically...\n",
97 				(float)autocorrect/10.0);
98 			poll(NULL, 0, autocorrect * 100);
99 		}
100 		return assumed;
101 	}
102 
103 	fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd);
104 
105 	if (main_cmds.cnt && best_similarity < 6) {
106 		fprintf(stderr, "\nDid you mean %s?\n",
107 			n < 2 ? "this": "one of these");
108 
109 		for (i = 0; i < n; i++)
110 			fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
111 	}
112 end:
113 	exit(1);
114 }
115