summaryrefslogtreecommitdiff
path: root/usr.bin/tmux/options.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2019-04-25 18:18:56 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2019-04-25 18:18:56 +0000
commit55343ab4044726e09c7748623bed7a52dd1aebbb (patch)
tree238d9d0ed43b571457e6ef8ab61f0751b6875258 /usr.bin/tmux/options.c
parent879760bfa34ac75961d2245586a4d2196f8a12cf (diff)
Make options_tostring allocate its result instead of using a stack
buffer (needed for something in the future).
Diffstat (limited to 'usr.bin/tmux/options.c')
-rw-r--r--usr.bin/tmux/options.c41
1 files changed, 17 insertions, 24 deletions
diff --git a/usr.bin/tmux/options.c b/usr.bin/tmux/options.c
index 441d5e991b5..c7bd422aeb9 100644
--- a/usr.bin/tmux/options.c
+++ b/usr.bin/tmux/options.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: options.c,v 1.41 2019/04/23 20:36:55 nicm Exp $ */
+/* $OpenBSD: options.c,v 1.42 2019/04/25 18:18:55 nicm Exp $ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -110,47 +110,43 @@ options_value_free(struct options_entry *o, union options_value *ov)
free(ov->string);
}
-static const char *
+static char *
options_value_tostring(struct options_entry *o, union options_value *ov,
int numeric)
{
- static char s[1024];
- const char *tmp;
+ char *s;
if (OPTIONS_IS_STYLE(o))
- return (style_tostring(&ov->style));
+ return (xstrdup(style_tostring(&ov->style)));
if (OPTIONS_IS_NUMBER(o)) {
- tmp = NULL;
switch (o->tableentry->type) {
case OPTIONS_TABLE_NUMBER:
- xsnprintf(s, sizeof s, "%lld", ov->number);
+ xasprintf(&s, "%lld", ov->number);
break;
case OPTIONS_TABLE_KEY:
- tmp = key_string_lookup_key(ov->number);
+ s = xstrdup(key_string_lookup_key(ov->number));
break;
case OPTIONS_TABLE_COLOUR:
- tmp = colour_tostring(ov->number);
+ s = xstrdup(colour_tostring(ov->number));
break;
case OPTIONS_TABLE_FLAG:
if (numeric)
- xsnprintf(s, sizeof s, "%lld", ov->number);
+ xasprintf(&s, "%lld", ov->number);
else
- tmp = (ov->number ? "on" : "off");
+ s = xstrdup(ov->number ? "on" : "off");
break;
case OPTIONS_TABLE_CHOICE:
- tmp = o->tableentry->choices[ov->number];
+ s = xstrdup(o->tableentry->choices[ov->number]);
break;
case OPTIONS_TABLE_STRING:
case OPTIONS_TABLE_STYLE:
- break;
+ fatalx("not a number option type");
}
- if (tmp != NULL)
- xsnprintf(s, sizeof s, "%s", tmp);
return (s);
}
if (OPTIONS_IS_STRING(o))
- return (ov->string);
- return ("");
+ return (xstrdup(ov->string));
+ return (xstrdup(""));
}
struct options *
@@ -218,11 +214,8 @@ options_empty(struct options *oo, const struct options_table_entry *oe)
o = options_add(oo, oe->name);
o->tableentry = oe;
- if (oe->flags & OPTIONS_TABLE_IS_ARRAY) {
- if (oe->type != OPTIONS_TABLE_STRING)
- fatalx("arrays can only be strings");
+ if (oe->flags & OPTIONS_TABLE_IS_ARRAY)
RB_INIT(&o->value.array);
- }
return (o);
}
@@ -443,17 +436,17 @@ options_isstring(struct options_entry *o)
return (OPTIONS_IS_STRING(o));
}
-const char *
+char *
options_tostring(struct options_entry *o, int idx, int numeric)
{
struct options_array_item *a;
if (OPTIONS_IS_ARRAY(o)) {
if (idx == -1)
- return (NULL);
+ return (xstrdup(""));
a = options_array_item(o, idx);
if (a == NULL)
- return ("");
+ return (xstrdup(""));
return (options_value_tostring(o, &a->value, numeric));
}
return (options_value_tostring(o, &o->value, numeric));