MINOR: cli: add a function to look up a CLI service description
authorThierry Fournier <thierry.fournier@ozon.io>
Sat, 28 Nov 2020 19:10:08 +0000 (20:10 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Mon, 14 Dec 2020 10:42:47 +0000 (11:42 +0100)
This function will be useful to check if the keyword is already registered.
Also add a define for the max number of args.

This will be needed by a next patch to fix a bug and will have to be
backported.

(cherry picked from commit a51a1fd17420a96bb766afbae354e041fc9e1d9b)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit b2239a79344a24e0e04aaa159020e4681aadf631)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit fce9e8164b7d305eea87cd4a95b133f8cf0369cf)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>

include/proto/cli.h
include/types/cli.h
src/cli.c

index 0668508..f81d0a8 100644 (file)
@@ -31,6 +31,7 @@
 
 
 void cli_register_kw(struct cli_kw_list *kw_list);
+struct cli_kw* cli_find_kw_exact(char **args);
 
 int cli_has_level(struct appctx *appctx, int level);
 
index 8a44124..8e90797 100644 (file)
 
 #define ACCESS_EXPERT       0x20 /* access to dangerous commands reserved to experts */
 
+#define CLI_PREFIX_KW_NB 5
+
 struct cli_kw {
-       const char *str_kw[5];   /* keywords ended by NULL, limited to 5
+       const char *str_kw[CLI_PREFIX_KW_NB]; /* keywords ended by NULL, limited to CLI_PREFIX_KW_NB
                                 separated keywords combination */
        const char *usage;   /* usage message */
        int (*parse)(char **args, char *payload, struct appctx *appctx, void *private);
index f5b3a76..54ff911 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -187,6 +187,39 @@ struct cli_kw* cli_find_kw(char **args)
        return NULL;
 }
 
+struct cli_kw* cli_find_kw_exact(char **args)
+{
+       struct cli_kw_list *kw_list;
+       int found = 0;
+       int i;
+       int j;
+
+       if (LIST_ISEMPTY(&cli_keywords.list))
+               return NULL;
+
+       list_for_each_entry(kw_list, &cli_keywords.list, list) {
+               for (i = 0; kw_list->kw[i].str_kw[0]; i++) {
+                       found = 1;
+                       for (j = 0; j < CLI_PREFIX_KW_NB; j++) {
+                               if (args[j] == NULL && kw_list->kw[i].str_kw[j] == NULL) {
+                                       break;
+                               }
+                               if (args[j] == NULL || kw_list->kw[i].str_kw[j] == NULL) {
+                                       found = 0;
+                                       break;
+                               }
+                               if (strcmp(args[j], kw_list->kw[i].str_kw[j]) != 0) {
+                                       found = 0;
+                                       break;
+                               }
+                       }
+                       if (found)
+                               return &kw_list->kw[i];
+               }
+       }
+       return NULL;
+}
+
 void cli_register_kw(struct cli_kw_list *kw_list)
 {
        LIST_ADDQ(&cli_keywords.list, &kw_list->list);