summaryrefslogtreecommitdiffstats
path: root/options.c
diff options
context:
space:
mode:
authornicm <nicm>2017-01-11 14:56:44 +0000
committernicm <nicm>2017-01-11 14:56:44 +0000
commit458b6eb6001fe4f1bff379ac56b91273662abe8c (patch)
treedcb7746345f6f8b772cba35b9dcc3ac82e26f3dc /options.c
parent55266275587f3bb6f0dfdb1623cb870315a38213 (diff)
Some tidying and tweaks to options code.
Diffstat (limited to 'options.c')
-rw-r--r--options.c58
1 files changed, 30 insertions, 28 deletions
diff --git a/options.c b/options.c
index 845bd7d5..cb7f3d87 100644
--- a/options.c
+++ b/options.c
@@ -37,8 +37,6 @@ struct options {
static int options_cmp(struct options_entry *, struct options_entry *);
RB_GENERATE_STATIC(options_tree, options_entry, entry, options_cmp);
-static void options_free1(struct options *, struct options_entry *);
-
static int
options_cmp(struct options_entry *o1, struct options_entry *o2)
{
@@ -66,6 +64,28 @@ options_free1(struct options *oo, struct options_entry *o)
free(o);
}
+static struct options_entry *
+options_new(struct options *oo, const char *name, char **s)
+{
+ struct options_entry *o;
+
+ if (s != NULL)
+ *s = NULL;
+
+ if ((o = options_find1(oo, name)) == NULL) {
+ o = xmalloc(sizeof *o);
+ o->name = xstrdup(name);
+ RB_INSERT(options_tree, &oo->tree, o);
+ memcpy(&o->style, &grid_default_cell, sizeof o->style);
+ } else if (o->type == OPTIONS_STRING) {
+ if (s != NULL)
+ *s = o->str;
+ else
+ free(o->str);
+ }
+ return (o);
+}
+
void
options_free(struct options *oo)
{
@@ -129,21 +149,14 @@ options_set_string(struct options *oo, const char *name, const char *fmt, ...)
va_list ap;
char *s;
- s = NULL;
- if ((o = options_find1(oo, name)) == NULL) {
- o = xmalloc(sizeof *o);
- o->name = xstrdup(name);
- RB_INSERT(options_tree, &oo->tree, o);
- memcpy(&o->style, &grid_default_cell, sizeof o->style);
- } else if (o->type == OPTIONS_STRING)
- s = o->str;
-
va_start(ap, fmt);
+
+ o = options_new(oo, name, &s);
o->type = OPTIONS_STRING;
xvasprintf(&o->str, fmt, ap);
- va_end(ap);
-
free(s);
+
+ va_end(ap);
return (o);
}
@@ -164,16 +177,10 @@ options_set_number(struct options *oo, const char *name, long long value)
{
struct options_entry *o;
- if ((o = options_find1(oo, name)) == NULL) {
- o = xmalloc(sizeof *o);
- o->name = xstrdup(name);
- RB_INSERT(options_tree, &oo->tree, o);
- memcpy(&o->style, &grid_default_cell, sizeof o->style);
- } else if (o->type == OPTIONS_STRING)
- free(o->str);
-
+ o = options_new(oo, name, NULL);
o->type = OPTIONS_NUMBER;
o->num = value;
+
return (o);
}
@@ -205,15 +212,10 @@ options_set_style(struct options *oo, const char *name, const char *value,
if (style_parse(&grid_default_cell, &tmpgc, value) == -1)
return (NULL);
- if (o == NULL) {
- o = xmalloc(sizeof *o);
- o->name = xstrdup(name);
- RB_INSERT(options_tree, &oo->tree, o);
- } else if (o->type == OPTIONS_STRING)
- free(o->str);
-
+ o = options_new(oo, name, NULL);
o->type = OPTIONS_STYLE;
memcpy(&o->style, &tmpgc, sizeof o->style);
+
return (o);
}