degesch: halfplement option assignment

This commit is contained in:
Přemysl Eric Janouch 2015-05-03 17:38:01 +02:00
parent 4eca3fb4db
commit 4841ba5bd0
1 changed files with 66 additions and 34 deletions

102
degesch.c
View File

@ -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]);
}
else
result = handle_command_set_assign (ctx, &all, arguments);
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;
}
// 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;
return result;
}
static bool