1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
25feaac24SJosh Poimboeuf #include "cache.h"
341840d21STaeung Song #include "config.h"
44208735dSArnaldo Carvalho de Melo #include <poll.h>
5d0761e37SArnaldo Carvalho de Melo #include <stdio.h>
67f7c536fSArnaldo Carvalho de Melo #include <stdlib.h>
74b6ab94eSJosh Poimboeuf #include <subcmd/help.h>
85feaac24SJosh Poimboeuf #include "../builtin.h"
95feaac24SJosh Poimboeuf #include "levenshtein.h"
107f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
115feaac24SJosh Poimboeuf
125feaac24SJosh Poimboeuf static int autocorrect;
135feaac24SJosh Poimboeuf
perf_unknown_cmd_config(const char * var,const char * value,void * cb __maybe_unused)14b8cbb349SWang Nan static int perf_unknown_cmd_config(const char *var, const char *value,
15b8cbb349SWang Nan void *cb __maybe_unused)
165feaac24SJosh Poimboeuf {
175feaac24SJosh Poimboeuf if (!strcmp(var, "help.autocorrect"))
1825ce4bb8SArnaldo Carvalho de Melo return perf_config_int(&autocorrect, var,value);
195feaac24SJosh Poimboeuf
20b8cbb349SWang Nan return 0;
215feaac24SJosh Poimboeuf }
225feaac24SJosh Poimboeuf
levenshtein_compare(const void * p1,const void * p2)235feaac24SJosh Poimboeuf static int levenshtein_compare(const void *p1, const void *p2)
245feaac24SJosh Poimboeuf {
255feaac24SJosh Poimboeuf const struct cmdname *const *c1 = p1, *const *c2 = p2;
265feaac24SJosh Poimboeuf const char *s1 = (*c1)->name, *s2 = (*c2)->name;
275feaac24SJosh Poimboeuf int l1 = (*c1)->len;
285feaac24SJosh Poimboeuf int l2 = (*c2)->len;
295feaac24SJosh Poimboeuf return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
305feaac24SJosh Poimboeuf }
315feaac24SJosh Poimboeuf
add_cmd_list(struct cmdnames * cmds,struct cmdnames * old)32682f4f03SMasami Hiramatsu static int add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
335feaac24SJosh Poimboeuf {
34682f4f03SMasami Hiramatsu unsigned int i, nr = cmds->cnt + old->cnt;
35682f4f03SMasami Hiramatsu void *tmp;
365feaac24SJosh Poimboeuf
37682f4f03SMasami Hiramatsu if (nr > cmds->alloc) {
38682f4f03SMasami Hiramatsu /* Choose bigger one to alloc */
39682f4f03SMasami Hiramatsu if (alloc_nr(cmds->alloc) < nr)
40682f4f03SMasami Hiramatsu cmds->alloc = nr;
41682f4f03SMasami Hiramatsu else
42682f4f03SMasami Hiramatsu cmds->alloc = alloc_nr(cmds->alloc);
43682f4f03SMasami Hiramatsu tmp = realloc(cmds->names, cmds->alloc * sizeof(*cmds->names));
44682f4f03SMasami Hiramatsu if (!tmp)
45682f4f03SMasami Hiramatsu return -1;
46682f4f03SMasami Hiramatsu cmds->names = tmp;
47682f4f03SMasami Hiramatsu }
485feaac24SJosh Poimboeuf for (i = 0; i < old->cnt; i++)
495feaac24SJosh Poimboeuf cmds->names[cmds->cnt++] = old->names[i];
505feaac24SJosh Poimboeuf zfree(&old->names);
515feaac24SJosh Poimboeuf old->cnt = 0;
52682f4f03SMasami Hiramatsu return 0;
535feaac24SJosh Poimboeuf }
545feaac24SJosh Poimboeuf
help_unknown_cmd(const char * cmd)555feaac24SJosh Poimboeuf const char *help_unknown_cmd(const char *cmd)
565feaac24SJosh Poimboeuf {
575feaac24SJosh Poimboeuf unsigned int i, n = 0, best_similarity = 0;
585feaac24SJosh Poimboeuf struct cmdnames main_cmds, other_cmds;
595feaac24SJosh Poimboeuf
605feaac24SJosh Poimboeuf memset(&main_cmds, 0, sizeof(main_cmds));
615feaac24SJosh Poimboeuf memset(&other_cmds, 0, sizeof(main_cmds));
625feaac24SJosh Poimboeuf
635feaac24SJosh Poimboeuf perf_config(perf_unknown_cmd_config, NULL);
645feaac24SJosh Poimboeuf
655feaac24SJosh Poimboeuf load_command_list("perf-", &main_cmds, &other_cmds);
665feaac24SJosh Poimboeuf
67c6867701SArnaldo Carvalho de Melo if (add_cmd_list(&main_cmds, &other_cmds) < 0) {
68682f4f03SMasami Hiramatsu fprintf(stderr, "ERROR: Failed to allocate command list for unknown command.\n");
69682f4f03SMasami Hiramatsu goto end;
70682f4f03SMasami Hiramatsu }
715feaac24SJosh Poimboeuf qsort(main_cmds.names, main_cmds.cnt,
725feaac24SJosh Poimboeuf sizeof(main_cmds.names), cmdname_compare);
735feaac24SJosh Poimboeuf uniq(&main_cmds);
745feaac24SJosh Poimboeuf
755feaac24SJosh Poimboeuf if (main_cmds.cnt) {
765feaac24SJosh Poimboeuf /* This reuses cmdname->len for similarity index */
775feaac24SJosh Poimboeuf for (i = 0; i < main_cmds.cnt; ++i)
785feaac24SJosh Poimboeuf main_cmds.names[i]->len =
795feaac24SJosh Poimboeuf levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
805feaac24SJosh Poimboeuf
815feaac24SJosh Poimboeuf qsort(main_cmds.names, main_cmds.cnt,
825feaac24SJosh Poimboeuf sizeof(*main_cmds.names), levenshtein_compare);
835feaac24SJosh Poimboeuf
845feaac24SJosh Poimboeuf best_similarity = main_cmds.names[0]->len;
855feaac24SJosh Poimboeuf n = 1;
865feaac24SJosh Poimboeuf while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
875feaac24SJosh Poimboeuf ++n;
885feaac24SJosh Poimboeuf }
895feaac24SJosh Poimboeuf
905feaac24SJosh Poimboeuf if (autocorrect && n == 1) {
915feaac24SJosh Poimboeuf const char *assumed = main_cmds.names[0]->name;
925feaac24SJosh Poimboeuf
935feaac24SJosh Poimboeuf main_cmds.names[0] = NULL;
945feaac24SJosh Poimboeuf clean_cmdnames(&main_cmds);
95*0f0d1354SIan Rogers clean_cmdnames(&other_cmds);
965feaac24SJosh Poimboeuf fprintf(stderr, "WARNING: You called a perf program named '%s', "
975feaac24SJosh Poimboeuf "which does not exist.\n"
985feaac24SJosh Poimboeuf "Continuing under the assumption that you meant '%s'\n",
995feaac24SJosh Poimboeuf cmd, assumed);
1005feaac24SJosh Poimboeuf if (autocorrect > 0) {
1015feaac24SJosh Poimboeuf fprintf(stderr, "in %0.1f seconds automatically...\n",
1025feaac24SJosh Poimboeuf (float)autocorrect/10.0);
1035feaac24SJosh Poimboeuf poll(NULL, 0, autocorrect * 100);
1045feaac24SJosh Poimboeuf }
1055feaac24SJosh Poimboeuf return assumed;
1065feaac24SJosh Poimboeuf }
1075feaac24SJosh Poimboeuf
1085feaac24SJosh Poimboeuf fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd);
1095feaac24SJosh Poimboeuf
1105feaac24SJosh Poimboeuf if (main_cmds.cnt && best_similarity < 6) {
1115feaac24SJosh Poimboeuf fprintf(stderr, "\nDid you mean %s?\n",
1125feaac24SJosh Poimboeuf n < 2 ? "this": "one of these");
1135feaac24SJosh Poimboeuf
1145feaac24SJosh Poimboeuf for (i = 0; i < n; i++)
1155feaac24SJosh Poimboeuf fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
1165feaac24SJosh Poimboeuf }
117682f4f03SMasami Hiramatsu end:
118*0f0d1354SIan Rogers clean_cmdnames(&main_cmds);
119*0f0d1354SIan Rogers clean_cmdnames(&other_cmds);
1205feaac24SJosh Poimboeuf exit(1);
1215feaac24SJosh Poimboeuf }
122