xref: /openbmc/linux/tools/perf/util/help-unknown-cmd.c (revision 4b6ab94eabe4f55371cff4569750bb3996c55db6)
15feaac24SJosh Poimboeuf #include "cache.h"
2*4b6ab94eSJosh Poimboeuf #include <subcmd/help.h>
35feaac24SJosh Poimboeuf #include "../builtin.h"
45feaac24SJosh Poimboeuf #include "levenshtein.h"
55feaac24SJosh Poimboeuf 
65feaac24SJosh Poimboeuf static int autocorrect;
75feaac24SJosh Poimboeuf static struct cmdnames aliases;
85feaac24SJosh Poimboeuf 
95feaac24SJosh Poimboeuf static int perf_unknown_cmd_config(const char *var, const char *value, void *cb)
105feaac24SJosh Poimboeuf {
115feaac24SJosh Poimboeuf 	if (!strcmp(var, "help.autocorrect"))
125feaac24SJosh Poimboeuf 		autocorrect = perf_config_int(var,value);
135feaac24SJosh Poimboeuf 	/* Also use aliases for command lookup */
145feaac24SJosh Poimboeuf 	if (!prefixcmp(var, "alias."))
155feaac24SJosh Poimboeuf 		add_cmdname(&aliases, var + 6, strlen(var + 6));
165feaac24SJosh Poimboeuf 
175feaac24SJosh Poimboeuf 	return perf_default_config(var, value, cb);
185feaac24SJosh Poimboeuf }
195feaac24SJosh Poimboeuf 
205feaac24SJosh Poimboeuf static int levenshtein_compare(const void *p1, const void *p2)
215feaac24SJosh Poimboeuf {
225feaac24SJosh Poimboeuf 	const struct cmdname *const *c1 = p1, *const *c2 = p2;
235feaac24SJosh Poimboeuf 	const char *s1 = (*c1)->name, *s2 = (*c2)->name;
245feaac24SJosh Poimboeuf 	int l1 = (*c1)->len;
255feaac24SJosh Poimboeuf 	int l2 = (*c2)->len;
265feaac24SJosh Poimboeuf 	return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
275feaac24SJosh Poimboeuf }
285feaac24SJosh Poimboeuf 
295feaac24SJosh Poimboeuf static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
305feaac24SJosh Poimboeuf {
315feaac24SJosh Poimboeuf 	unsigned int i;
325feaac24SJosh Poimboeuf 
335feaac24SJosh Poimboeuf 	ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
345feaac24SJosh Poimboeuf 
355feaac24SJosh Poimboeuf 	for (i = 0; i < old->cnt; i++)
365feaac24SJosh Poimboeuf 		cmds->names[cmds->cnt++] = old->names[i];
375feaac24SJosh Poimboeuf 	zfree(&old->names);
385feaac24SJosh Poimboeuf 	old->cnt = 0;
395feaac24SJosh Poimboeuf }
405feaac24SJosh Poimboeuf 
415feaac24SJosh Poimboeuf const char *help_unknown_cmd(const char *cmd)
425feaac24SJosh Poimboeuf {
435feaac24SJosh Poimboeuf 	unsigned int i, n = 0, best_similarity = 0;
445feaac24SJosh Poimboeuf 	struct cmdnames main_cmds, other_cmds;
455feaac24SJosh Poimboeuf 
465feaac24SJosh Poimboeuf 	memset(&main_cmds, 0, sizeof(main_cmds));
475feaac24SJosh Poimboeuf 	memset(&other_cmds, 0, sizeof(main_cmds));
485feaac24SJosh Poimboeuf 	memset(&aliases, 0, sizeof(aliases));
495feaac24SJosh Poimboeuf 
505feaac24SJosh Poimboeuf 	perf_config(perf_unknown_cmd_config, NULL);
515feaac24SJosh Poimboeuf 
525feaac24SJosh Poimboeuf 	load_command_list("perf-", &main_cmds, &other_cmds);
535feaac24SJosh Poimboeuf 
545feaac24SJosh Poimboeuf 	add_cmd_list(&main_cmds, &aliases);
555feaac24SJosh Poimboeuf 	add_cmd_list(&main_cmds, &other_cmds);
565feaac24SJosh Poimboeuf 	qsort(main_cmds.names, main_cmds.cnt,
575feaac24SJosh Poimboeuf 	      sizeof(main_cmds.names), cmdname_compare);
585feaac24SJosh Poimboeuf 	uniq(&main_cmds);
595feaac24SJosh Poimboeuf 
605feaac24SJosh Poimboeuf 	if (main_cmds.cnt) {
615feaac24SJosh Poimboeuf 		/* This reuses cmdname->len for similarity index */
625feaac24SJosh Poimboeuf 		for (i = 0; i < main_cmds.cnt; ++i)
635feaac24SJosh Poimboeuf 			main_cmds.names[i]->len =
645feaac24SJosh Poimboeuf 				levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
655feaac24SJosh Poimboeuf 
665feaac24SJosh Poimboeuf 		qsort(main_cmds.names, main_cmds.cnt,
675feaac24SJosh Poimboeuf 		      sizeof(*main_cmds.names), levenshtein_compare);
685feaac24SJosh Poimboeuf 
695feaac24SJosh Poimboeuf 		best_similarity = main_cmds.names[0]->len;
705feaac24SJosh Poimboeuf 		n = 1;
715feaac24SJosh Poimboeuf 		while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
725feaac24SJosh Poimboeuf 			++n;
735feaac24SJosh Poimboeuf 	}
745feaac24SJosh Poimboeuf 
755feaac24SJosh Poimboeuf 	if (autocorrect && n == 1) {
765feaac24SJosh Poimboeuf 		const char *assumed = main_cmds.names[0]->name;
775feaac24SJosh Poimboeuf 
785feaac24SJosh Poimboeuf 		main_cmds.names[0] = NULL;
795feaac24SJosh Poimboeuf 		clean_cmdnames(&main_cmds);
805feaac24SJosh Poimboeuf 		fprintf(stderr, "WARNING: You called a perf program named '%s', "
815feaac24SJosh Poimboeuf 			"which does not exist.\n"
825feaac24SJosh Poimboeuf 			"Continuing under the assumption that you meant '%s'\n",
835feaac24SJosh Poimboeuf 			cmd, assumed);
845feaac24SJosh Poimboeuf 		if (autocorrect > 0) {
855feaac24SJosh Poimboeuf 			fprintf(stderr, "in %0.1f seconds automatically...\n",
865feaac24SJosh Poimboeuf 				(float)autocorrect/10.0);
875feaac24SJosh Poimboeuf 			poll(NULL, 0, autocorrect * 100);
885feaac24SJosh Poimboeuf 		}
895feaac24SJosh Poimboeuf 		return assumed;
905feaac24SJosh Poimboeuf 	}
915feaac24SJosh Poimboeuf 
925feaac24SJosh Poimboeuf 	fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd);
935feaac24SJosh Poimboeuf 
945feaac24SJosh Poimboeuf 	if (main_cmds.cnt && best_similarity < 6) {
955feaac24SJosh Poimboeuf 		fprintf(stderr, "\nDid you mean %s?\n",
965feaac24SJosh Poimboeuf 			n < 2 ? "this": "one of these");
975feaac24SJosh Poimboeuf 
985feaac24SJosh Poimboeuf 		for (i = 0; i < n; i++)
995feaac24SJosh Poimboeuf 			fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
1005feaac24SJosh Poimboeuf 	}
1015feaac24SJosh Poimboeuf 
1025feaac24SJosh Poimboeuf 	exit(1);
1035feaac24SJosh Poimboeuf }
104