From 1e3a27369080c6f189fd0d8d5151f23ec082a61f Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sun, 3 May 2015 18:45:05 +0200 Subject: degesch: refactor handle_command_set_assign() --- degesch.c | 68 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/degesch.c b/degesch.c index 8d37622..de40462 100644 --- a/degesch.c +++ b/degesch.c @@ -3878,6 +3878,42 @@ handle_command_set_replace (struct app_context *ctx, return false; } +static void +handle_command_set_assign_item (struct app_context *ctx, + char *key, struct config_item_ *new_, bool add, bool remove) +{ + struct config_item_ *item = + config_item_get (ctx->config.root, key, NULL); + hard_assert (item); + + struct error *e = NULL; + if ((add | remove) && item->type != CONFIG_ITEM_STRING_ARRAY) + // FIXME: it can also be null, which makes this message confusing + error_set (&e, "not a string array"); + else if (add) + handle_command_set_add (item, new_->value.string.str, &e); + else if (remove) + handle_command_set_remove (item, new_->value.string.str, &e); + else + handle_command_set_replace (ctx, item, new_, &e); + + if (e) + { + buffer_send_error (ctx, ctx->global_buffer, + "Failed to set option \"%s\": %s", key, e->message); + error_free (e); + } + else + { + struct str_vector tmp; + str_vector_init (&tmp); + dump_matching_options (ctx, key, &tmp); + buffer_send_status (ctx, ctx->global_buffer, + "Option changed: %s", tmp.vector[0]); + str_vector_free (&tmp); + } +} + static bool handle_command_set_assign (struct app_context *ctx, struct str_vector *all, char *arguments) @@ -3912,40 +3948,10 @@ handle_command_set_assign 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); - - struct error *e = NULL; - if ((add | remove) && item->type != CONFIG_ITEM_STRING_ARRAY) - // FIXME: it can also be null, which makes this message confusing - error_set (&e, "not a string array"); - else if (add) - handle_command_set_add (item, new_->value.string.str, &e); - else if (remove) - handle_command_set_remove (item, new_->value.string.str, &e); - else - handle_command_set_replace (ctx, item, new_, &e); - - if (e) - { - buffer_send_error (ctx, ctx->global_buffer, - "Failed to set option \"%s\": %s", key, e->message); - error_free (e); - } - else - { - struct str_vector tmp; - str_vector_init (&tmp); - dump_matching_options (ctx, key, &tmp); - buffer_send_status (ctx, ctx->global_buffer, - "Option changed: %s", tmp.vector[0]); - str_vector_free (&tmp); - } + handle_command_set_assign_item (ctx, key, new_, add, remove); free (key); } config_item_destroy (new_); -- cgit v1.2.3-70-g09d2