]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
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)
committerWilly Tarreau <w@1wt.eu>
Wed, 2 Dec 2020 08:45:18 +0000 (09:45 +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.

include/haproxy/cli-t.h
include/haproxy/cli.h
src/cli.c

index 8f12fee4258da66ce4f90f3adac394534edb84c8..290bda1260dafa8cb66162a8292b942c55fbd236 100644 (file)
@@ -42,6 +42,8 @@
 #define APPCTX_CLI_ST1_PAYLOAD (1 << 1)
 #define APPCTX_CLI_ST1_NOLF    (1 << 2)
 
+#define CLI_PREFIX_KW_NB 5
+
 /* CLI states */
 enum {
        CLI_ST_INIT = 0,   /* initial state, must leave to zero ! */
@@ -66,7 +68,7 @@ enum {
 
 
 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 3a4525ccfd5da579e1de2c51cba723b492b8e59a..0a4433b692e826dc739be0664dc75ce011702f2b 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 135b6b216fad05326617e456935dbbeba473e3ce..ced882aceca576592662474bf6893f38744d899a 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -182,6 +182,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);