CLEANUP: map: slightly reorder the add map function
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.
diff --git a/src/map.c b/src/map.c
index 041c339..75356ab 100644
--- a/src/map.c
+++ b/src/map.c
@@ -804,25 +804,24 @@
"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;
- /* key */
+ if (payload) {
+ /* key and value passed as payload, one pair per line */
+ if (!*payload)
+ break;
+
key = payload;
l = strcspn(key, " \t");
payload += l;
@@ -841,16 +840,16 @@
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;