degesch: halfplement option assignment
This commit is contained in:
parent
4eca3fb4db
commit
4841ba5bd0
100
degesch.c
100
degesch.c
|
@ -3773,6 +3773,63 @@ handle_command_buffer (struct app_context *ctx, char *arguments)
|
||||||
return true;
|
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
|
static int
|
||||||
str_vector_sort_cb (const void *a, const void *b)
|
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
|
// Filter out results by wildcard matching
|
||||||
for (size_t i = 0; i < all.len; i++)
|
for (size_t i = 0; i < all.len; i++)
|
||||||
{
|
{
|
||||||
char *key = xstrdup (all.vector[i]);
|
char *key = xstrndup (all.vector[i], strcspn (all.vector[i], " "));
|
||||||
char *end = strchr (key, ' ');
|
|
||||||
if (end)
|
|
||||||
*end = '\0';
|
|
||||||
if (fnmatch (option, key, 0))
|
if (fnmatch (option, key, 0))
|
||||||
str_vector_remove (&all, i--);
|
str_vector_remove (&all, i--);
|
||||||
free (key);
|
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", "");
|
buffer_send_status (ctx, ctx->global_buffer, "%s", "");
|
||||||
for (size_t i = 0; i < all.len; i++)
|
for (size_t i = 0; i < all.len; i++)
|
||||||
buffer_send_status (ctx, ctx->global_buffer, "%s", all.vector[i]);
|
buffer_send_status (ctx, ctx->global_buffer, "%s", all.vector[i]);
|
||||||
str_vector_free (&all);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
result = handle_command_set_assign (ctx, &all, arguments);
|
||||||
|
|
||||||
char *op = cut_word (&arguments);
|
str_vector_free (&all);
|
||||||
bool add = false;
|
return result;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
|
|
Loading…
Reference in New Issue