summaryrefslogtreecommitdiffstats
path: root/options.c
diff options
context:
space:
mode:
authornicm <nicm>2017-01-24 19:11:46 +0000
committernicm <nicm>2017-01-24 19:11:46 +0000
commit61fce272ea60f5096711ee554c56aef9cca8c654 (patch)
treeaab0f65300ba9d33dfb7798d02aeacfb00164aa7 /options.c
parent3d74e89a395838e5c9c4ea101a9366be09bf03c7 (diff)
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).
Diffstat (limited to 'options.c')
-rw-r--r--options.c85
1 files changed, 51 insertions, 34 deletions
diff --git a/options.c b/options.c
index 4f6aa6fe..37263b53 100644
--- a/options.c
+++ b/options.c
@@ -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);
}