]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: map: slightly reorder the add map function
authorWilly Tarreau <w@1wt.eu>
Thu, 29 Apr 2021 14:02:48 +0000 (16:02 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 30 Apr 2021 13:36:31 +0000 (15:36 +0200)
The function uses two distinct code paths for single the key/value pair
and multiple pairs inserted as payload, each with a copy-paste of the
error handling. Let's modify the loop to factor them out.

src/map.c

index 041c339f448605c91f70384038b933c395bffadc..75356ab8ebcbcad837fc94272c3013b34ffb6a10 100644 (file)
--- a/src/map.c
+++ b/src/map.c
@@ -804,25 +804,24 @@ static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx,
                                       "This ACL is shared with a map containing samples. "
                                       "You must use the command 'add map' to add values.\n");
                }
-               /* Add value(s). */
+
+               /* Add value(s). If no payload is used, key and value are read
+                * from the command line and only one key is set. If a payload
+                * is passed, one key/value pair is read per line till the end
+                * of the payload is reached.
+                */
                err = NULL;
-               if (!payload) {
-                       ret = map_add_key_value(appctx, args[3], args[4], &err);
-                       if (!ret) {
-                               if (err)
-                                       return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
-                               else
-                                       return cli_err(appctx, "Failed to add an entry.\n");
-                       }
-               }
-               else {
-                       const char *end = payload + strlen(payload);
 
-                       while (payload < end) {
-                               char *key, *value;
-                               size_t l;
+               do {
+                       char *key   = args[3];
+                       char *value = args[4];
+                       size_t l;
+
+                       if (payload) {
+                               /* key and value passed as payload, one pair per line */
+                               if (!*payload)
+                                       break;
 
-                               /* key */
                                key = payload;
                                l = strcspn(key, " \t");
                                payload += l;
@@ -841,16 +840,16 @@ static int cli_parse_add_map(char **args, char *payload, struct appctx *appctx,
                                if (*payload)
                                        payload++;
                                value[l] = 0;
+                       }
 
-                               ret = map_add_key_value(appctx, key, value, &err);
-                               if (!ret) {
-                                       if (err)
-                                               return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
-                                       else
-                                               return cli_err(appctx, "Failed to add a key.\n");
-                               }
+                       ret = map_add_key_value(appctx, key, value, &err);
+                       if (!ret) {
+                               if (err)
+                                       return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
+                               else
+                                       return cli_err(appctx, "Failed to add a key.\n");
                        }
-               }
+               } while (payload && *payload);
 
                /* The add is done, send message. */
                appctx->st0 = CLI_ST_PROMPT;