1
0
mirror of https://github.com/tmux/tmux.git synced 2025-03-29 10:18:49 +00:00

Rewrite options_array_set to be clearer and remove a spurious warning

with newer GCC. From Ben Boeckel.
This commit is contained in:
nicm 2019-10-15 08:30:36 +00:00
parent 0c5e9c6efa
commit 9fd62efcf0
2 changed files with 37 additions and 20 deletions

View File

@ -321,6 +321,17 @@ options_array_item(struct options_entry *o, u_int idx)
return (RB_FIND(options_array, &o->value.array, &a)); return (RB_FIND(options_array, &o->value.array, &a));
} }
static struct options_array_item *
options_array_new(struct options_entry *o, u_int idx)
{
struct options_array_item *a;
a = xcalloc(1, sizeof *a);
a->index = idx;
RB_INSERT(options_array, &o->value.array, a);
return (a);
}
static void static void
options_array_free(struct options_entry *o, struct options_array_item *a) options_array_free(struct options_entry *o, struct options_array_item *a)
{ {
@ -368,7 +379,14 @@ options_array_set(struct options_entry *o, u_int idx, const char *value,
return (-1); return (-1);
} }
if (OPTIONS_IS_COMMAND(o) && value != NULL) { if (value == NULL) {
a = options_array_item(o, idx);
if (a != NULL)
options_array_free(o, a);
return (0);
}
if (OPTIONS_IS_COMMAND(o)) {
pr = cmd_parse_from_string(value, NULL); pr = cmd_parse_from_string(value, NULL);
switch (pr->status) { switch (pr->status) {
case CMD_PARSE_EMPTY: case CMD_PARSE_EMPTY:
@ -384,34 +402,33 @@ options_array_set(struct options_entry *o, u_int idx, const char *value,
case CMD_PARSE_SUCCESS: case CMD_PARSE_SUCCESS:
break; break;
} }
}
a = options_array_item(o, idx); a = options_array_item(o, idx);
if (value == NULL) { if (a == NULL)
if (a != NULL) a = options_array_new(o, idx);
options_array_free(o, a); else
options_value_free(o, &a->value);
a->value.cmdlist = pr->cmdlist;
return (0); return (0);
} }
if (OPTIONS_IS_STRING(o)) { if (OPTIONS_IS_STRING(o)) {
a = options_array_item(o, idx);
if (a != NULL && append) if (a != NULL && append)
xasprintf(&new, "%s%s", a->value.string, value); xasprintf(&new, "%s%s", a->value.string, value);
else else
new = xstrdup(value); new = xstrdup(value);
if (a == NULL)
a = options_array_new(o, idx);
else
options_value_free(o, &a->value);
a->value.string = new;
return (0);
} }
if (a == NULL) { if (cause != NULL)
a = xcalloc(1, sizeof *a); *cause = xstrdup("wrong array type");
a->index = idx; return (-1);
RB_INSERT(options_array, &o->value.array, a);
} else
options_value_free(o, &a->value);
if (OPTIONS_IS_STRING(o))
a->value.string = new;
else if (OPTIONS_IS_COMMAND(o))
a->value.cmdlist = pr->cmdlist;
return (0);
} }
int int

View File

@ -191,7 +191,7 @@ window_tree_cmp_session(const void *a0, const void *b0)
const struct session *const *b = b0; const struct session *const *b = b0;
const struct session *sa = *a; const struct session *sa = *a;
const struct session *sb = *b; const struct session *sb = *b;
int result; int result = 0;
switch (window_tree_sort->field) { switch (window_tree_sort->field) {
case WINDOW_TREE_BY_INDEX: case WINDOW_TREE_BY_INDEX:
@ -226,7 +226,7 @@ window_tree_cmp_window(const void *a0, const void *b0)
const struct winlink *wlb = *b; const struct winlink *wlb = *b;
struct window *wa = wla->window; struct window *wa = wla->window;
struct window *wb = wlb->window; struct window *wb = wlb->window;
int result; int result = 0;
switch (window_tree_sort->field) { switch (window_tree_sort->field) {
case WINDOW_TREE_BY_INDEX: case WINDOW_TREE_BY_INDEX: