From 4841ba5bd0117ca7b8dfc5d8eaa6ae8d58f5b92e Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Sun, 3 May 2015 17:38:01 +0200
Subject: degesch: halfplement option assignment
---
degesch.c | 100 +++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 66 insertions(+), 34 deletions(-)
diff --git a/degesch.c b/degesch.c
index 1d66a9d..ecc15d8 100644
--- a/degesch.c
+++ b/degesch.c
@@ -3773,6 +3773,63 @@ handle_command_buffer (struct app_context *ctx, char *arguments)
return true;
}
+static bool
+handle_command_set_assign
+ (struct app_context *ctx, struct str_vector *all, char *arguments)
+{
+ char *op = cut_word (&arguments);
+ bool add = false;
+ bool remove = false;
+
+ if (!strcmp (op, "+=")) add = true;
+ else if (!strcmp (op, "-=")) remove = true;
+ else if (strcmp (op, "=")) return false;
+
+ if (!arguments)
+ return false;
+
+ char *value = cut_word (&arguments);
+ struct error *e = NULL;
+ struct config_item_ *new_ =
+ config_item_parse (value, strlen (value), true, &e);
+ if (e)
+ {
+ buffer_send_error (ctx, ctx->global_buffer,
+ "Invalid value: %s", e->message);
+ error_free (e);
+ return true;
+ }
+
+ if ((add | remove) && !config_item_type_is_string (new_->type))
+ {
+ buffer_send_error (ctx, ctx->global_buffer,
+ "+= / -= operators need a string argument");
+ config_item_destroy (new_);
+ return true;
+ }
+
+ for (size_t i = 0; i < all->len; i++)
+ {
+ char *key = xstrndup (all->vector[i], strcspn (all->vector[i], " "));
+ struct config_item_ *item =
+ config_item_get (ctx->config.root, key, NULL);
+ hard_assert (item);
+
+ if ((add | remove) && item->type != CONFIG_ITEM_STRING_ARRAY)
+ buffer_send_error (ctx, ctx->global_buffer,
+ "Option is not a string array: %s", key);
+ else if (add)
+ ; // TODO: add to string array (or log error)
+ else if (remove)
+ ; // TODO: remove from string array (or log error)
+ else
+ ; // TODO: reset the value
+ free (key);
+ }
+ config_item_destroy (new_);
+ return true;
+}
+
static int
str_vector_sort_cb (const void *a, const void *b)
{
@@ -3800,51 +3857,26 @@ handle_command_set (struct app_context *ctx, char *arguments)
// Filter out results by wildcard matching
for (size_t i = 0; i < all.len; i++)
{
- char *key = xstrdup (all.vector[i]);
- char *end = strchr (key, ' ');
- if (end)
- *end = '\0';
+ char *key = xstrndup (all.vector[i], strcspn (all.vector[i], " "));
if (fnmatch (option, key, 0))
str_vector_remove (&all, i--);
free (key);
}
- if (!*arguments)
+ bool result = true;
+ if (!all.len)
+ buffer_send_error (ctx, ctx->global_buffer, "No matches: %s", option);
+ else if (!*arguments)
{
buffer_send_status (ctx, ctx->global_buffer, "%s", "");
for (size_t i = 0; i < all.len; i++)
buffer_send_status (ctx, ctx->global_buffer, "%s", all.vector[i]);
- str_vector_free (&all);
- return true;
- }
-
- char *op = cut_word (&arguments);
- bool add = false;
- bool remove = false;
-
- if (!strcmp (op, "+=")) add = true;
- else if (!strcmp (op, "-=")) remove = true;
- else if (strcmp (op, "=")) return false;
-
- if (!arguments)
- return false;
-
- char *value = cut_word (&arguments);
- struct error *e = NULL;
- struct config_item_ *new_ =
- config_item_parse (value, strlen (value), true, &e);
- if (e)
- {
- buffer_send_error (ctx, ctx->global_buffer,
- "Invalid value: %s", e->message);
- error_free (e);
- return true;
}
+ else
+ result = handle_command_set_assign (ctx, &all, arguments);
- // TODO: try to set the value, or modify the string list
- buffer_send_error (ctx, ctx->global_buffer, "Not implemented");
- config_item_destroy (new_);
- return true;
+ str_vector_free (&all);
+ return result;
}
static bool
--
cgit v1.2.3-70-g09d2