diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2015-05-03 18:45:05 +0200 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2015-05-03 19:26:39 +0200 |
commit | 1e3a27369080c6f189fd0d8d5151f23ec082a61f (patch) | |
tree | 099e303c7b80b39b229170cc7c367f7b81963acc /degesch.c | |
parent | 3b5c3c3b4e7c24ef774f801c13b8b17d76c1ed94 (diff) | |
download | xK-1e3a27369080c6f189fd0d8d5151f23ec082a61f.tar.gz xK-1e3a27369080c6f189fd0d8d5151f23ec082a61f.tar.xz xK-1e3a27369080c6f189fd0d8d5151f23ec082a61f.zip |
degesch: refactor handle_command_set_assign()
Diffstat (limited to 'degesch.c')
-rw-r--r-- | degesch.c | 68 |
1 files changed, 37 insertions, 31 deletions
@@ -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_); |