If given an array option without an index either show or set all items,

and support -a for array options. Allow the separator for set to be
specified in the options table (will be used for backwards compatibility
later).
This commit is contained in:
nicm
2017-01-24 19:11:46 +00:00
parent 3d74e89a39
commit 61fce272ea
4 changed files with 79 additions and 43 deletions

View File

@ -170,22 +170,11 @@ struct options_entry *
options_default(struct options *oo, const struct options_table_entry *oe)
{
struct options_entry *o;
char *cp, *copy, *next;
u_int idx = 0;
o = options_empty(oo, oe);
if (oe->type == OPTIONS_TABLE_ARRAY) {
copy = cp = xstrdup(oe->default_str);
while ((next = strsep(&cp, ",")) != NULL) {
options_array_set(o, idx, next);
idx++;
}
free(copy);
return (o);
}
if (oe->type == OPTIONS_TABLE_STRING)
if (oe->type == OPTIONS_TABLE_ARRAY)
options_array_assign(o, oe->default_str);
else if (oe->type == OPTIONS_TABLE_STRING)
o->string = xstrdup(oe->default_str);
else if (oe->type == OPTIONS_TABLE_STYLE) {
memcpy(&o->style, &grid_default_cell, sizeof o->style);
@ -242,6 +231,13 @@ options_table_entry(struct options_entry *o)
return (o->tableentry);
}
void
options_array_clear(struct options_entry *o)
{
if (OPTIONS_IS_ARRAY(o))
o->arraysize = 0;
}
const char *
options_array_get(struct options_entry *o, u_int idx)
{
@ -253,9 +249,11 @@ options_array_get(struct options_entry *o, u_int idx)
}
int
options_array_set(struct options_entry *o, u_int idx, const char *value)
options_array_set(struct options_entry *o, u_int idx, const char *value,
int append)
{
u_int i;
char *new;
u_int i;
if (!OPTIONS_IS_ARRAY(o))
return (-1);
@ -268,12 +266,17 @@ options_array_set(struct options_entry *o, u_int idx, const char *value)
o->array[i] = NULL;
o->arraysize = idx + 1;
}
if (o->array[idx] != NULL)
free((void *)o->array[idx]);
if (value != NULL)
o->array[idx] = xstrdup(value);
else
o->array[idx] = NULL;
new = NULL;
if (value != NULL) {
if (o->array[idx] != NULL && append)
xasprintf(&new, "%s%s", o->array[idx], value);
else
new = xstrdup(value);
}
free((void *)o->array[idx]);
o->array[idx] = new;
return (0);
}
@ -287,6 +290,32 @@ options_array_size(struct options_entry *o, u_int *size)
return (0);
}
void
options_array_assign(struct options_entry *o, const char *s)
{
const char *separator;
char *copy, *next, *string;
u_int i;
separator = o->tableentry->separator;
if (separator == NULL)
separator = " ,";
copy = string = xstrdup(s);
while ((next = strsep(&string, separator)) != NULL) {
if (*next == '\0')
continue;
for (i = 0; i < OPTIONS_ARRAY_LIMIT; i++) {
if (i >= o->arraysize || o->array[i] == NULL)
break;
}
if (i == OPTIONS_ARRAY_LIMIT)
break;
options_array_set(o, i, next, 0);
}
free(copy);
}
int
options_isstring(struct options_entry *o)
{
@ -384,12 +413,6 @@ options_parse_get(struct options *oo, const char *s, int *idx, int only)
else
o = options_get(oo, name);
free(name);
if (o != NULL) {
if (OPTIONS_IS_ARRAY(o) && *idx == -1)
return (NULL);
if (!OPTIONS_IS_ARRAY(o) && *idx != -1)
return (NULL);
}
return (o);
}
@ -447,12 +470,6 @@ options_match_get(struct options *oo, const char *s, int *idx, int only,
else
o = options_get(oo, name);
free(name);
if (o != NULL) {
if (OPTIONS_IS_ARRAY(o) && *idx == -1)
return (NULL);
if (!OPTIONS_IS_ARRAY(o) && *idx != -1)
return (NULL);
}
return (o);
}