Simplify appending to string options.

pull/717/merge
nicm 2017-01-12 15:36:35 +00:00
parent dad3090d32
commit 24cba5907b
6 changed files with 44 additions and 52 deletions

View File

@ -292,10 +292,8 @@ cmd_set_option_user(struct cmd *self, struct cmdq_item *item,
cmdq_error(item, "empty value"); cmdq_error(item, "empty value");
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
if (o != NULL && args_has(args, 'a')) options_set_string(oo, optstr, args_has(args, 'a'), "%s",
options_set_string(oo, optstr, "%s%s", o->str, valstr); valstr);
else
options_set_string(oo, optstr, "%s", valstr);
} }
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }
@ -316,10 +314,11 @@ cmd_set_option_unset(struct cmd *self, struct cmdq_item *item,
if (args_has(args, 'g') || oo == global_options) { if (args_has(args, 'g') || oo == global_options) {
switch (oe->type) { switch (oe->type) {
case OPTIONS_TABLE_STRING: case OPTIONS_TABLE_STRING:
options_set_string(oo, oe->name, "%s", oe->default_str); options_set_string(oo, oe->name, 0, "%s",
oe->default_str);
break; break;
case OPTIONS_TABLE_STYLE: case OPTIONS_TABLE_STYLE:
options_set_style(oo, oe->name, oe->default_str, 0); options_set_style(oo, oe->name, 0, oe->default_str);
break; break;
default: default:
options_set_number(oo, oe->name, oe->default_num); options_set_number(oo, oe->name, oe->default_num);
@ -391,20 +390,10 @@ cmd_set_option_string(struct cmd *self, __unused struct cmdq_item *item,
const struct options_table_entry *oe, struct options *oo, const struct options_table_entry *oe, struct options *oo,
const char *value) const char *value)
{ {
struct args *args = self->args; struct args *args = self->args;
struct options_entry *o; int append = args_has(args, 'a');
char *oldval, *newval;
if (args_has(args, 'a')) { return (options_set_string(oo, oe->name, append, "%s", value));
oldval = options_get_string(oo, oe->name);
xasprintf(&newval, "%s%s", oldval, value);
} else
newval = xstrdup(value);
o = options_set_string(oo, oe->name, "%s", newval);
free(newval);
return (o);
} }
/* Set a number option. */ /* Set a number option. */
@ -544,11 +533,10 @@ cmd_set_option_style(struct cmd *self, struct cmdq_item *item,
const char *value) const char *value)
{ {
struct args *args = self->args; struct args *args = self->args;
int append = args_has(args, 'a');
struct options_entry *o; struct options_entry *o;
int append;
append = args_has(args, 'a'); if ((o = options_set_style(oo, oe->name, append, value)) == NULL) {
if ((o = options_set_style(oo, oe->name, value, append)) == NULL) {
cmdq_error(item, "bad style: %s", value); cmdq_error(item, "bad style: %s", value);
return (NULL); return (NULL);
} }

View File

@ -915,10 +915,11 @@ options_table_populate_tree(enum options_table_scope scope, struct options *oo)
continue; continue;
switch (oe->type) { switch (oe->type) {
case OPTIONS_TABLE_STRING: case OPTIONS_TABLE_STRING:
options_set_string(oo, oe->name, "%s", oe->default_str); options_set_string(oo, oe->name, 0, "%s",
oe->default_str);
break; break;
case OPTIONS_TABLE_STYLE: case OPTIONS_TABLE_STYLE:
options_set_style(oo, oe->name, oe->default_str, 0); options_set_style(oo, oe->name, 0, oe->default_str);
break; break;
default: default:
options_set_number(oo, oe->name, oe->default_num); options_set_number(oo, oe->name, oe->default_num);

View File

@ -65,24 +65,17 @@ options_free1(struct options *oo, struct options_entry *o)
} }
static struct options_entry * static struct options_entry *
options_new(struct options *oo, const char *name, char **s) options_new(struct options *oo, const char *name)
{ {
struct options_entry *o; struct options_entry *o;
if (s != NULL)
*s = NULL;
if ((o = options_find1(oo, name)) == NULL) { if ((o = options_find1(oo, name)) == NULL) {
o = xmalloc(sizeof *o); o = xmalloc(sizeof *o);
o->name = xstrdup(name); o->name = xstrdup(name);
RB_INSERT(options_tree, &oo->tree, o); RB_INSERT(options_tree, &oo->tree, o);
memcpy(&o->style, &grid_default_cell, sizeof o->style); memcpy(&o->style, &grid_default_cell, sizeof o->style);
} else if (o->type == OPTIONS_STRING) { } else if (o->type == OPTIONS_STRING)
if (s != NULL) free(o->str);
*s = o->str;
else
free(o->str);
}
return (o); return (o);
} }
@ -143,20 +136,29 @@ options_remove(struct options *oo, const char *name)
} }
struct options_entry * struct options_entry *
options_set_string(struct options *oo, const char *name, const char *fmt, ...) options_set_string(struct options *oo, const char *name, int append,
const char *fmt, ...)
{ {
struct options_entry *o; struct options_entry *o;
va_list ap; va_list ap;
char *s; char *s, *value;
va_start(ap, fmt); va_start(ap, fmt);
xvasprintf(&s, fmt, ap);
o = options_new(oo, name, &s);
o->type = OPTIONS_STRING;
xvasprintf(&o->str, fmt, ap);
free(s);
va_end(ap); va_end(ap);
o = options_find1(oo, name);
if (o == NULL || !append)
value = s;
else {
xasprintf(&value, "%s%s", s, o->str);
free(s);
}
o = options_new(oo, name);
o->type = OPTIONS_STRING;
o->str = value;
return (o); return (o);
} }
@ -177,7 +179,7 @@ options_set_number(struct options *oo, const char *name, long long value)
{ {
struct options_entry *o; struct options_entry *o;
o = options_new(oo, name, NULL); o = options_new(oo, name);
o->type = OPTIONS_NUMBER; o->type = OPTIONS_NUMBER;
o->num = value; o->num = value;
@ -197,8 +199,8 @@ options_get_number(struct options *oo, const char *name)
} }
struct options_entry * struct options_entry *
options_set_style(struct options *oo, const char *name, const char *value, options_set_style(struct options *oo, const char *name, int append,
int append) const char *value)
{ {
struct options_entry *o; struct options_entry *o;
struct grid_cell tmpgc; struct grid_cell tmpgc;
@ -212,7 +214,7 @@ options_set_style(struct options *oo, const char *name, const char *value,
if (style_parse(&grid_default_cell, &tmpgc, value) == -1) if (style_parse(&grid_default_cell, &tmpgc, value) == -1)
return (NULL); return (NULL);
o = options_new(oo, name, NULL); o = options_new(oo, name);
o->type = OPTIONS_STYLE; o->type = OPTIONS_STYLE;
memcpy(&o->style, &tmpgc, sizeof o->style); memcpy(&o->style, &tmpgc, sizeof o->style);

View File

@ -143,7 +143,7 @@ style_update_new(struct options *oo, const char *name, const char *newname)
o = options_find1(oo, newname); o = options_find1(oo, newname);
if (o == NULL) if (o == NULL)
o = options_set_style(oo, newname, "default", 0); o = options_set_style(oo, newname, 0, "default");
gc = &o->style; gc = &o->style;
o = options_find1(oo, name); o = options_find1(oo, name);

3
tmux.c
View File

@ -295,7 +295,8 @@ main(int argc, char **argv)
global_s_options = options_create(NULL); global_s_options = options_create(NULL);
options_table_populate_tree(OPTIONS_TABLE_SESSION, global_s_options); options_table_populate_tree(OPTIONS_TABLE_SESSION, global_s_options);
options_set_string(global_s_options, "default-shell", "%s", getshell()); options_set_string(global_s_options, "default-shell", 0, "%s",
getshell());
global_w_options = options_create(NULL); global_w_options = options_create(NULL);
options_table_populate_tree(OPTIONS_TABLE_WINDOW, global_w_options); options_table_populate_tree(OPTIONS_TABLE_WINDOW, global_w_options);

8
tmux.h
View File

@ -1627,14 +1627,14 @@ struct options_entry *options_next(struct options_entry *);
struct options_entry *options_find1(struct options *, const char *); struct options_entry *options_find1(struct options *, const char *);
struct options_entry *options_find(struct options *, const char *); struct options_entry *options_find(struct options *, const char *);
void options_remove(struct options *, const char *); void options_remove(struct options *, const char *);
struct options_entry *printflike(3, 4) options_set_string(struct options *, struct options_entry * printflike(4, 5) options_set_string(struct options *,
const char *, const char *, ...); const char *, int, const char *, ...);
char *options_get_string(struct options *, const char *); char *options_get_string(struct options *, const char *);
struct options_entry *options_set_number(struct options *, const char *, struct options_entry *options_set_number(struct options *, const char *,
long long); long long);
long long options_get_number(struct options *, const char *); long long options_get_number(struct options *, const char *);
struct options_entry *options_set_style(struct options *, const char *, struct options_entry *options_set_style(struct options *, const char *, int,
const char *, int); const char *);
struct grid_cell *options_get_style(struct options *, const char *); struct grid_cell *options_get_style(struct options *, const char *);
/* options-table.c */ /* options-table.c */