From 92fbbcc4c6d6756349929b529a316183604b7575 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sun, 9 May 2021 21:45:29 +0200 Subject: [PATCH] MINOR: cli: sort the output of the "help" keywords It's still very difficult to find all commands starting with a given keyword like "set", "show" etc. Let's sort the lines by usage message, this is much more convenient. --- include/haproxy/cli-t.h | 1 + src/cli.c | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/haproxy/cli-t.h b/include/haproxy/cli-t.h index 3d52f27b4..3760789d1 100644 --- a/include/haproxy/cli-t.h +++ b/include/haproxy/cli-t.h @@ -44,6 +44,7 @@ #define CLI_PREFIX_KW_NB 5 #define CLI_MAX_MATCHES 5 +#define CLI_MAX_HELP_ENTRIES 1024 /* CLI states */ enum { diff --git a/src/cli.c b/src/cli.c index 9a979a13f..c2464b9e2 100644 --- a/src/cli.c +++ b/src/cli.c @@ -84,12 +84,21 @@ extern const char *stat_status_codes[]; struct proxy *mworker_proxy; /* CLI proxy of the master */ +static int cmp_kw_entries(const void *a, const void *b) +{ + const struct cli_kw *l = *(const struct cli_kw **)a; + const struct cli_kw *r = *(const struct cli_kw **)b; + + return strcmp(l->usage ? l->usage : "", r->usage ? r->usage : ""); +} + /* This will show the help message and list the commands supported at the * current level that match all of the first words of if args is not * NULL, or all args if none matches or if args is null. */ static char *cli_gen_usage_msg(struct appctx *appctx, char * const *args) { + struct cli_kw *entries[CLI_MAX_HELP_ENTRIES]; struct cli_kw_list *kw_list; struct cli_kw *kw; struct buffer *tmp = get_trash_chunk(); @@ -98,6 +107,7 @@ static char *cli_gen_usage_msg(struct appctx *appctx, char * const *args) int idx; int ishelp = 0; int length = 0; + int help_entries = 0; ha_free(&dynamic_usage_msg); @@ -220,7 +230,8 @@ static char *cli_gen_usage_msg(struct appctx *appctx, char * const *args) if (matches[idx].dist > 5*matches[0].dist/2) break; - chunk_appendf(tmp, " %s\n", kw->usage); + if (help_entries < CLI_MAX_HELP_ENTRIES) + entries[help_entries++] = kw; } } @@ -253,11 +264,16 @@ static char *cli_gen_usage_msg(struct appctx *appctx, char * const *args) break; } - if (kw->usage && idx == length) - chunk_appendf(tmp, " %s\n", kw->usage); + if (kw->usage && idx == length && help_entries < CLI_MAX_HELP_ENTRIES) + entries[help_entries++] = kw; } } + qsort(entries, help_entries, sizeof(*entries), cmp_kw_entries); + + for (idx = 0; idx < help_entries; idx++) + chunk_appendf(tmp, " %s\n", entries[idx]->usage); + /* always show the prompt/help/quit commands */ chunk_strcat(tmp, " help [] : list matching or all commands\n"