2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
#include <ctype.h>
|
2020-05-16 16:02:24 +00:00
|
|
|
#include <fnmatch.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <stdarg.h>
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Option handling; each option has a name, type and value and is stored in
|
2014-01-09 13:58:06 +00:00
|
|
|
* a red-black tree.
|
2009-06-01 22:58:49 +00:00
|
|
|
*/
|
|
|
|
|
2019-03-18 11:58:40 +00:00
|
|
|
struct options_array_item {
|
|
|
|
u_int index;
|
2019-04-23 20:36:55 +00:00
|
|
|
union options_value value;
|
2019-03-18 11:58:40 +00:00
|
|
|
RB_ENTRY(options_array_item) entry;
|
|
|
|
};
|
|
|
|
static int
|
|
|
|
options_array_cmp(struct options_array_item *a1, struct options_array_item *a2)
|
|
|
|
{
|
|
|
|
if (a1->index < a2->index)
|
|
|
|
return (-1);
|
|
|
|
if (a1->index > a2->index)
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
RB_GENERATE_STATIC(options_array, options_array_item, entry, options_array_cmp);
|
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry {
|
2019-04-23 20:36:55 +00:00
|
|
|
struct options *owner;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
2019-04-23 20:36:55 +00:00
|
|
|
const char *name;
|
|
|
|
const struct options_table_entry *tableentry;
|
|
|
|
union options_value value;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
2020-05-16 15:01:30 +00:00
|
|
|
int cached;
|
|
|
|
struct style style;
|
|
|
|
|
2019-04-23 20:36:55 +00:00
|
|
|
RB_ENTRY(options_entry) entry;
|
2017-01-15 20:48:41 +00:00
|
|
|
};
|
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
struct options {
|
2017-01-16 14:49:14 +00:00
|
|
|
RB_HEAD(options_tree, options_entry) tree;
|
2017-01-15 20:48:41 +00:00
|
|
|
struct options *parent;
|
2015-10-27 15:58:42 +00:00
|
|
|
};
|
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
static struct options_entry *options_add(struct options *, const char *);
|
2020-06-16 08:18:34 +00:00
|
|
|
static void options_remove(struct options_entry *);
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
#define OPTIONS_IS_STRING(o) \
|
|
|
|
((o)->tableentry == NULL || \
|
|
|
|
(o)->tableentry->type == OPTIONS_TABLE_STRING)
|
|
|
|
#define OPTIONS_IS_NUMBER(o) \
|
|
|
|
((o)->tableentry != NULL && \
|
|
|
|
((o)->tableentry->type == OPTIONS_TABLE_NUMBER || \
|
|
|
|
(o)->tableentry->type == OPTIONS_TABLE_KEY || \
|
|
|
|
(o)->tableentry->type == OPTIONS_TABLE_COLOUR || \
|
|
|
|
(o)->tableentry->type == OPTIONS_TABLE_FLAG || \
|
|
|
|
(o)->tableentry->type == OPTIONS_TABLE_CHOICE))
|
2019-04-26 11:38:51 +00:00
|
|
|
#define OPTIONS_IS_COMMAND(o) \
|
|
|
|
((o)->tableentry != NULL && \
|
|
|
|
(o)->tableentry->type == OPTIONS_TABLE_COMMAND)
|
2019-04-23 20:36:55 +00:00
|
|
|
|
|
|
|
#define OPTIONS_IS_ARRAY(o) \
|
2017-01-15 20:48:41 +00:00
|
|
|
((o)->tableentry != NULL && \
|
2019-04-23 20:36:55 +00:00
|
|
|
((o)->tableentry->flags & OPTIONS_TABLE_IS_ARRAY))
|
2017-01-15 20:48:41 +00:00
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
static int options_cmp(struct options_entry *, struct options_entry *);
|
|
|
|
RB_GENERATE_STATIC(options_tree, options_entry, entry, options_cmp);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2015-12-11 15:46:57 +00:00
|
|
|
static int
|
2017-01-16 14:49:14 +00:00
|
|
|
options_cmp(struct options_entry *lhs, struct options_entry *rhs)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2017-01-15 20:48:41 +00:00
|
|
|
return (strcmp(lhs->name, rhs->name));
|
|
|
|
}
|
|
|
|
|
2020-08-25 11:35:32 +00:00
|
|
|
static const char *
|
|
|
|
options_map_name(const char *name)
|
|
|
|
{
|
|
|
|
const struct options_name_map *map;
|
|
|
|
|
|
|
|
for (map = options_other_names; map->from != NULL; map++) {
|
|
|
|
if (strcmp(map->from, name) == 0)
|
|
|
|
return (map->to);
|
|
|
|
}
|
|
|
|
return (name);
|
|
|
|
}
|
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
static const struct options_table_entry *
|
|
|
|
options_parent_table_entry(struct options *oo, const char *s)
|
|
|
|
{
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
if (oo->parent == NULL)
|
|
|
|
fatalx("no parent options for %s", s);
|
2019-06-20 11:59:59 +00:00
|
|
|
o = options_get(oo->parent, s);
|
2017-01-15 20:48:41 +00:00
|
|
|
if (o == NULL)
|
|
|
|
fatalx("%s not in parent options", s);
|
|
|
|
return (o->tableentry);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 20:36:55 +00:00
|
|
|
static void
|
|
|
|
options_value_free(struct options_entry *o, union options_value *ov)
|
|
|
|
{
|
|
|
|
if (OPTIONS_IS_STRING(o))
|
|
|
|
free(ov->string);
|
2019-04-26 11:38:51 +00:00
|
|
|
if (OPTIONS_IS_COMMAND(o) && ov->cmdlist != NULL)
|
|
|
|
cmd_list_free(ov->cmdlist);
|
2019-04-23 20:36:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 18:18:55 +00:00
|
|
|
static char *
|
2020-05-16 16:02:24 +00:00
|
|
|
options_value_to_string(struct options_entry *o, union options_value *ov,
|
2019-04-23 20:36:55 +00:00
|
|
|
int numeric)
|
|
|
|
{
|
2019-04-25 18:18:55 +00:00
|
|
|
char *s;
|
2019-04-23 20:36:55 +00:00
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
if (OPTIONS_IS_COMMAND(o))
|
2019-05-23 14:03:44 +00:00
|
|
|
return (cmd_list_print(ov->cmdlist, 0));
|
2019-04-23 20:36:55 +00:00
|
|
|
if (OPTIONS_IS_NUMBER(o)) {
|
|
|
|
switch (o->tableentry->type) {
|
|
|
|
case OPTIONS_TABLE_NUMBER:
|
2019-04-25 18:18:55 +00:00
|
|
|
xasprintf(&s, "%lld", ov->number);
|
2019-04-23 20:36:55 +00:00
|
|
|
break;
|
|
|
|
case OPTIONS_TABLE_KEY:
|
2020-05-16 16:35:13 +00:00
|
|
|
s = xstrdup(key_string_lookup_key(ov->number, 0));
|
2019-04-23 20:36:55 +00:00
|
|
|
break;
|
|
|
|
case OPTIONS_TABLE_COLOUR:
|
2019-04-25 18:18:55 +00:00
|
|
|
s = xstrdup(colour_tostring(ov->number));
|
2019-04-23 20:36:55 +00:00
|
|
|
break;
|
|
|
|
case OPTIONS_TABLE_FLAG:
|
|
|
|
if (numeric)
|
2019-04-25 18:18:55 +00:00
|
|
|
xasprintf(&s, "%lld", ov->number);
|
2019-04-23 20:36:55 +00:00
|
|
|
else
|
2019-04-25 18:18:55 +00:00
|
|
|
s = xstrdup(ov->number ? "on" : "off");
|
2019-04-23 20:36:55 +00:00
|
|
|
break;
|
|
|
|
case OPTIONS_TABLE_CHOICE:
|
2019-04-25 18:18:55 +00:00
|
|
|
s = xstrdup(o->tableentry->choices[ov->number]);
|
2019-04-23 20:36:55 +00:00
|
|
|
break;
|
|
|
|
case OPTIONS_TABLE_STRING:
|
2019-04-26 11:38:51 +00:00
|
|
|
case OPTIONS_TABLE_COMMAND:
|
2019-04-25 18:18:55 +00:00
|
|
|
fatalx("not a number option type");
|
2019-04-23 20:36:55 +00:00
|
|
|
}
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
if (OPTIONS_IS_STRING(o))
|
2019-04-25 18:18:55 +00:00
|
|
|
return (xstrdup(ov->string));
|
|
|
|
return (xstrdup(""));
|
2019-04-23 20:36:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
struct options *
|
|
|
|
options_create(struct options *parent)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2015-12-08 08:14:04 +00:00
|
|
|
struct options *oo;
|
2015-10-27 15:58:42 +00:00
|
|
|
|
|
|
|
oo = xcalloc(1, sizeof *oo);
|
2012-01-21 11:12:13 +00:00
|
|
|
RB_INIT(&oo->tree);
|
2009-06-01 22:58:49 +00:00
|
|
|
oo->parent = parent;
|
2015-10-27 15:58:42 +00:00
|
|
|
return (oo);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
options_free(struct options *oo)
|
|
|
|
{
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o, *tmp;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-05-31 17:56:48 +00:00
|
|
|
RB_FOREACH_SAFE(o, options_tree, &oo->tree, tmp)
|
2017-01-15 20:48:41 +00:00
|
|
|
options_remove(o);
|
2015-10-27 15:58:42 +00:00
|
|
|
free(oo);
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:02:24 +00:00
|
|
|
struct options *
|
|
|
|
options_get_parent(struct options *oo)
|
|
|
|
{
|
|
|
|
return (oo->parent);
|
|
|
|
}
|
|
|
|
|
2019-06-20 11:59:59 +00:00
|
|
|
void
|
|
|
|
options_set_parent(struct options *oo, struct options *parent)
|
|
|
|
{
|
|
|
|
oo->parent = parent;
|
|
|
|
}
|
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *
|
2015-10-27 15:58:42 +00:00
|
|
|
options_first(struct options *oo)
|
|
|
|
{
|
|
|
|
return (RB_MIN(options_tree, &oo->tree));
|
|
|
|
}
|
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *
|
|
|
|
options_next(struct options_entry *o)
|
2015-10-27 15:58:42 +00:00
|
|
|
{
|
|
|
|
return (RB_NEXT(options_tree, &oo->tree, o));
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *
|
2017-01-15 20:48:41 +00:00
|
|
|
options_get_only(struct options *oo, const char *name)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2020-08-25 11:35:32 +00:00
|
|
|
struct options_entry o = { .name = name }, *found;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2020-08-25 11:35:32 +00:00
|
|
|
found = RB_FIND(options_tree, &oo->tree, &o);
|
|
|
|
if (found == NULL) {
|
|
|
|
o.name = options_map_name(name);
|
|
|
|
return (RB_FIND(options_tree, &oo->tree, &o));
|
|
|
|
}
|
|
|
|
return (found);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *
|
2017-01-15 20:48:41 +00:00
|
|
|
options_get(struct options *oo, const char *name)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_get_only(oo, name);
|
2009-06-01 22:58:49 +00:00
|
|
|
while (o == NULL) {
|
|
|
|
oo = oo->parent;
|
|
|
|
if (oo == NULL)
|
|
|
|
break;
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_get_only(oo, name);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
return (o);
|
|
|
|
}
|
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *
|
2017-01-15 20:48:41 +00:00
|
|
|
options_empty(struct options *oo, const struct options_table_entry *oe)
|
|
|
|
{
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
o = options_add(oo, oe->name);
|
|
|
|
o->tableentry = oe;
|
|
|
|
|
2019-04-25 18:18:55 +00:00
|
|
|
if (oe->flags & OPTIONS_TABLE_IS_ARRAY)
|
2019-04-23 20:36:55 +00:00
|
|
|
RB_INIT(&o->value.array);
|
2019-03-18 11:58:40 +00:00
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
return (o);
|
|
|
|
}
|
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *
|
2017-01-15 20:48:41 +00:00
|
|
|
options_default(struct options *oo, const struct options_table_entry *oe)
|
|
|
|
{
|
2019-04-23 20:36:55 +00:00
|
|
|
struct options_entry *o;
|
|
|
|
union options_value *ov;
|
|
|
|
u_int i;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
o = options_empty(oo, oe);
|
2019-04-23 20:36:55 +00:00
|
|
|
ov = &o->value;
|
|
|
|
|
|
|
|
if (oe->flags & OPTIONS_TABLE_IS_ARRAY) {
|
2019-04-26 11:38:51 +00:00
|
|
|
if (oe->default_arr == NULL) {
|
|
|
|
options_array_assign(o, oe->default_str, NULL);
|
|
|
|
return (o);
|
|
|
|
}
|
|
|
|
for (i = 0; oe->default_arr[i] != NULL; i++)
|
|
|
|
options_array_set(o, i, oe->default_arr[i], 0, NULL);
|
2019-04-23 20:36:55 +00:00
|
|
|
return (o);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (oe->type) {
|
|
|
|
case OPTIONS_TABLE_STRING:
|
|
|
|
ov->string = xstrdup(oe->default_str);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ov->number = oe->default_num;
|
|
|
|
break;
|
|
|
|
}
|
2017-01-15 20:48:41 +00:00
|
|
|
return (o);
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:02:24 +00:00
|
|
|
char *
|
|
|
|
options_default_to_string(const struct options_table_entry *oe)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
switch (oe->type) {
|
|
|
|
case OPTIONS_TABLE_STRING:
|
|
|
|
case OPTIONS_TABLE_COMMAND:
|
|
|
|
s = xstrdup(oe->default_str);
|
|
|
|
break;
|
|
|
|
case OPTIONS_TABLE_NUMBER:
|
|
|
|
xasprintf(&s, "%lld", oe->default_num);
|
|
|
|
break;
|
|
|
|
case OPTIONS_TABLE_KEY:
|
2020-05-16 16:35:13 +00:00
|
|
|
s = xstrdup(key_string_lookup_key(oe->default_num, 0));
|
2020-05-16 16:02:24 +00:00
|
|
|
break;
|
|
|
|
case OPTIONS_TABLE_COLOUR:
|
|
|
|
s = xstrdup(colour_tostring(oe->default_num));
|
|
|
|
break;
|
|
|
|
case OPTIONS_TABLE_FLAG:
|
|
|
|
s = xstrdup(oe->default_num ? "on" : "off");
|
|
|
|
break;
|
|
|
|
case OPTIONS_TABLE_CHOICE:
|
|
|
|
s = xstrdup(oe->choices[oe->default_num]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
static struct options_entry *
|
2017-01-15 20:48:41 +00:00
|
|
|
options_add(struct options *oo, const char *name)
|
|
|
|
{
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
o = options_get_only(oo, name);
|
|
|
|
if (o != NULL)
|
|
|
|
options_remove(o);
|
|
|
|
|
|
|
|
o = xcalloc(1, sizeof *o);
|
|
|
|
o->owner = oo;
|
|
|
|
o->name = xstrdup(name);
|
|
|
|
|
|
|
|
RB_INSERT(options_tree, &oo->tree, o);
|
|
|
|
return (o);
|
|
|
|
}
|
|
|
|
|
2020-06-16 08:18:34 +00:00
|
|
|
static void
|
2017-01-16 14:49:14 +00:00
|
|
|
options_remove(struct options_entry *o)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2017-01-15 20:48:41 +00:00
|
|
|
struct options *oo = o->owner;
|
|
|
|
|
2019-04-23 20:36:55 +00:00
|
|
|
if (OPTIONS_IS_ARRAY(o))
|
2019-03-18 11:58:40 +00:00
|
|
|
options_array_clear(o);
|
2019-04-23 20:36:55 +00:00
|
|
|
else
|
|
|
|
options_value_free(o, &o->value);
|
2017-01-15 20:48:41 +00:00
|
|
|
RB_REMOVE(options_tree, &oo->tree, o);
|
2019-10-14 08:38:07 +00:00
|
|
|
free((void *)o->name);
|
2017-01-15 20:48:41 +00:00
|
|
|
free(o);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
const char *
|
2017-01-16 14:49:14 +00:00
|
|
|
options_name(struct options_entry *o)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2017-01-15 20:48:41 +00:00
|
|
|
return (o->name);
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2020-05-16 16:02:24 +00:00
|
|
|
struct options *
|
|
|
|
options_owner(struct options_entry *o)
|
|
|
|
{
|
|
|
|
return (o->owner);
|
|
|
|
}
|
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
const struct options_table_entry *
|
2017-01-16 14:49:14 +00:00
|
|
|
options_table_entry(struct options_entry *o)
|
2017-01-15 20:48:41 +00:00
|
|
|
{
|
|
|
|
return (o->tableentry);
|
|
|
|
}
|
2017-01-11 14:56:44 +00:00
|
|
|
|
2019-03-18 11:58:40 +00:00
|
|
|
static struct options_array_item *
|
|
|
|
options_array_item(struct options_entry *o, u_int idx)
|
|
|
|
{
|
|
|
|
struct options_array_item a;
|
|
|
|
|
|
|
|
a.index = idx;
|
2019-04-23 20:36:55 +00:00
|
|
|
return (RB_FIND(options_array, &o->value.array, &a));
|
2019-03-18 11:58:40 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 08:30:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-03-18 11:58:40 +00:00
|
|
|
static void
|
|
|
|
options_array_free(struct options_entry *o, struct options_array_item *a)
|
|
|
|
{
|
2019-04-23 20:36:55 +00:00
|
|
|
options_value_free(o, &a->value);
|
|
|
|
RB_REMOVE(options_array, &o->value.array, a);
|
2019-03-18 11:58:40 +00:00
|
|
|
free(a);
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:11:46 +00:00
|
|
|
void
|
|
|
|
options_array_clear(struct options_entry *o)
|
|
|
|
{
|
2019-03-18 11:58:40 +00:00
|
|
|
struct options_array_item *a, *a1;
|
|
|
|
|
|
|
|
if (!OPTIONS_IS_ARRAY(o))
|
|
|
|
return;
|
|
|
|
|
2019-04-23 20:36:55 +00:00
|
|
|
RB_FOREACH_SAFE(a, options_array, &o->value.array, a1)
|
2019-03-18 11:58:40 +00:00
|
|
|
options_array_free(o, a);
|
2017-01-24 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 20:36:55 +00:00
|
|
|
union options_value *
|
2017-01-16 14:49:14 +00:00
|
|
|
options_array_get(struct options_entry *o, u_int idx)
|
2017-01-15 20:48:41 +00:00
|
|
|
{
|
2019-03-18 11:58:40 +00:00
|
|
|
struct options_array_item *a;
|
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
if (!OPTIONS_IS_ARRAY(o))
|
|
|
|
return (NULL);
|
2019-03-18 11:58:40 +00:00
|
|
|
a = options_array_item(o, idx);
|
|
|
|
if (a == NULL)
|
2017-01-15 20:48:41 +00:00
|
|
|
return (NULL);
|
2019-04-23 20:36:55 +00:00
|
|
|
return (&a->value);
|
2017-01-15 20:48:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-01-24 19:11:46 +00:00
|
|
|
options_array_set(struct options_entry *o, u_int idx, const char *value,
|
2019-04-26 11:38:51 +00:00
|
|
|
int append, char **cause)
|
2017-01-15 20:48:41 +00:00
|
|
|
{
|
2019-03-18 11:58:40 +00:00
|
|
|
struct options_array_item *a;
|
|
|
|
char *new;
|
2019-05-23 11:13:30 +00:00
|
|
|
struct cmd_parse_result *pr;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
if (!OPTIONS_IS_ARRAY(o)) {
|
2019-05-12 18:16:33 +00:00
|
|
|
if (cause != NULL)
|
|
|
|
*cause = xstrdup("not an array");
|
2017-01-15 20:48:41 +00:00
|
|
|
return (-1);
|
2019-04-26 11:38:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 08:30:36 +00:00
|
|
|
if (value == NULL) {
|
|
|
|
a = options_array_item(o, idx);
|
|
|
|
if (a != NULL)
|
|
|
|
options_array_free(o, a);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OPTIONS_IS_COMMAND(o)) {
|
2019-05-23 11:13:30 +00:00
|
|
|
pr = cmd_parse_from_string(value, NULL);
|
|
|
|
switch (pr->status) {
|
|
|
|
case CMD_PARSE_EMPTY:
|
2019-05-30 20:54:03 +00:00
|
|
|
if (cause != NULL)
|
|
|
|
*cause = xstrdup("empty command");
|
2019-05-23 11:13:30 +00:00
|
|
|
return (-1);
|
|
|
|
case CMD_PARSE_ERROR:
|
2019-05-12 18:16:33 +00:00
|
|
|
if (cause != NULL)
|
2019-05-23 11:13:30 +00:00
|
|
|
*cause = pr->error;
|
2019-05-12 18:16:33 +00:00
|
|
|
else
|
2019-05-23 11:13:30 +00:00
|
|
|
free(pr->error);
|
2019-04-26 11:38:51 +00:00
|
|
|
return (-1);
|
2019-05-23 11:13:30 +00:00
|
|
|
case CMD_PARSE_SUCCESS:
|
|
|
|
break;
|
2019-05-12 18:16:33 +00:00
|
|
|
}
|
2017-01-15 20:48:41 +00:00
|
|
|
|
2019-10-15 08:30:36 +00:00
|
|
|
a = options_array_item(o, idx);
|
|
|
|
if (a == NULL)
|
|
|
|
a = options_array_new(o, idx);
|
|
|
|
else
|
|
|
|
options_value_free(o, &a->value);
|
|
|
|
a->value.cmdlist = pr->cmdlist;
|
2019-03-18 11:58:40 +00:00
|
|
|
return (0);
|
2017-01-12 15:36:35 +00:00
|
|
|
}
|
2017-01-24 19:11:46 +00:00
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
if (OPTIONS_IS_STRING(o)) {
|
2019-10-15 08:30:36 +00:00
|
|
|
a = options_array_item(o, idx);
|
2019-03-18 11:58:40 +00:00
|
|
|
if (a != NULL && append)
|
2019-04-23 20:36:55 +00:00
|
|
|
xasprintf(&new, "%s%s", a->value.string, value);
|
2017-01-24 19:11:46 +00:00
|
|
|
else
|
|
|
|
new = xstrdup(value);
|
2019-10-15 08:30:36 +00:00
|
|
|
if (a == NULL)
|
|
|
|
a = options_array_new(o, idx);
|
|
|
|
else
|
|
|
|
options_value_free(o, &a->value);
|
|
|
|
a->value.string = new;
|
|
|
|
return (0);
|
2017-01-24 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 08:30:36 +00:00
|
|
|
if (cause != NULL)
|
|
|
|
*cause = xstrdup("wrong array type");
|
|
|
|
return (-1);
|
2017-01-15 20:48:41 +00:00
|
|
|
}
|
2017-01-11 14:56:44 +00:00
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
int
|
|
|
|
options_array_assign(struct options_entry *o, const char *s, char **cause)
|
2017-01-24 19:11:46 +00:00
|
|
|
{
|
|
|
|
const char *separator;
|
|
|
|
char *copy, *next, *string;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
separator = o->tableentry->separator;
|
|
|
|
if (separator == NULL)
|
|
|
|
separator = " ,";
|
2019-04-26 11:38:51 +00:00
|
|
|
if (*separator == '\0') {
|
|
|
|
if (*s == '\0')
|
|
|
|
return (0);
|
|
|
|
for (i = 0; i < UINT_MAX; i++) {
|
|
|
|
if (options_array_item(o, i) == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (options_array_set(o, i, s, 0, cause));
|
|
|
|
}
|
2017-01-24 19:11:46 +00:00
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
if (*s == '\0')
|
|
|
|
return (0);
|
2017-01-24 19:11:46 +00:00
|
|
|
copy = string = xstrdup(s);
|
|
|
|
while ((next = strsep(&string, separator)) != NULL) {
|
|
|
|
if (*next == '\0')
|
|
|
|
continue;
|
2019-03-18 11:58:40 +00:00
|
|
|
for (i = 0; i < UINT_MAX; i++) {
|
|
|
|
if (options_array_item(o, i) == NULL)
|
2017-01-24 19:11:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-03-18 11:58:40 +00:00
|
|
|
if (i == UINT_MAX)
|
2017-01-24 19:11:46 +00:00
|
|
|
break;
|
2019-04-26 11:38:51 +00:00
|
|
|
if (options_array_set(o, i, next, 0, cause) != 0) {
|
|
|
|
free(copy);
|
|
|
|
return (-1);
|
|
|
|
}
|
2017-01-24 19:11:46 +00:00
|
|
|
}
|
|
|
|
free(copy);
|
2019-04-26 11:38:51 +00:00
|
|
|
return (0);
|
2017-01-24 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 11:58:40 +00:00
|
|
|
struct options_array_item *
|
|
|
|
options_array_first(struct options_entry *o)
|
|
|
|
{
|
|
|
|
if (!OPTIONS_IS_ARRAY(o))
|
|
|
|
return (NULL);
|
2019-04-23 20:36:55 +00:00
|
|
|
return (RB_MIN(options_array, &o->value.array));
|
2019-03-18 11:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct options_array_item *
|
|
|
|
options_array_next(struct options_array_item *a)
|
|
|
|
{
|
2019-04-23 20:36:55 +00:00
|
|
|
return (RB_NEXT(options_array, &o->value.array, a));
|
2019-03-18 11:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u_int
|
|
|
|
options_array_item_index(struct options_array_item *a)
|
|
|
|
{
|
|
|
|
return (a->index);
|
|
|
|
}
|
|
|
|
|
2019-04-23 20:36:55 +00:00
|
|
|
union options_value *
|
2019-03-18 11:58:40 +00:00
|
|
|
options_array_item_value(struct options_array_item *a)
|
|
|
|
{
|
2019-04-23 20:36:55 +00:00
|
|
|
return (&a->value);
|
2019-03-18 11:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-05-16 16:02:24 +00:00
|
|
|
options_is_array(struct options_entry *o)
|
2019-03-18 11:58:40 +00:00
|
|
|
{
|
|
|
|
return (OPTIONS_IS_ARRAY(o));
|
|
|
|
}
|
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
int
|
2020-05-16 16:02:24 +00:00
|
|
|
options_is_string(struct options_entry *o)
|
2017-01-15 20:48:41 +00:00
|
|
|
{
|
2019-04-23 20:36:55 +00:00
|
|
|
return (OPTIONS_IS_STRING(o));
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 18:18:55 +00:00
|
|
|
char *
|
2020-05-16 16:02:24 +00:00
|
|
|
options_to_string(struct options_entry *o, int idx, int numeric)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2019-03-18 11:58:40 +00:00
|
|
|
struct options_array_item *a;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
if (OPTIONS_IS_ARRAY(o)) {
|
|
|
|
if (idx == -1)
|
2019-04-25 18:18:55 +00:00
|
|
|
return (xstrdup(""));
|
2019-03-18 11:58:40 +00:00
|
|
|
a = options_array_item(o, idx);
|
|
|
|
if (a == NULL)
|
2019-04-25 18:18:55 +00:00
|
|
|
return (xstrdup(""));
|
2020-05-16 16:02:24 +00:00
|
|
|
return (options_value_to_string(o, &a->value, numeric));
|
2017-01-15 20:48:41 +00:00
|
|
|
}
|
2020-05-16 16:02:24 +00:00
|
|
|
return (options_value_to_string(o, &o->value, numeric));
|
2017-01-15 20:48:41 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
char *
|
|
|
|
options_parse(const char *name, int *idx)
|
|
|
|
{
|
|
|
|
char *copy, *cp, *end;
|
|
|
|
|
|
|
|
if (*name == '\0')
|
|
|
|
return (NULL);
|
|
|
|
copy = xstrdup(name);
|
|
|
|
if ((cp = strchr(copy, '[')) == NULL) {
|
|
|
|
*idx = -1;
|
|
|
|
return (copy);
|
|
|
|
}
|
|
|
|
end = strchr(cp + 1, ']');
|
|
|
|
if (end == NULL || end[1] != '\0' || !isdigit((u_char)end[-1])) {
|
|
|
|
free(copy);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (sscanf(cp, "[%d]", idx) != 1 || *idx < 0) {
|
|
|
|
free(copy);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
*cp = '\0';
|
|
|
|
return (copy);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *
|
2017-01-15 20:48:41 +00:00
|
|
|
options_parse_get(struct options *oo, const char *s, int *idx, int only)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o;
|
|
|
|
char *name;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
name = options_parse(s, idx);
|
|
|
|
if (name == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (only)
|
|
|
|
o = options_get_only(oo, name);
|
|
|
|
else
|
|
|
|
o = options_get(oo, name);
|
|
|
|
free(name);
|
|
|
|
return (o);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2019-06-20 11:59:59 +00:00
|
|
|
options_match(const char *s, int *idx, int *ambiguous)
|
2017-01-15 20:48:41 +00:00
|
|
|
{
|
|
|
|
const struct options_table_entry *oe, *found;
|
2020-08-25 11:35:32 +00:00
|
|
|
char *parsed;
|
|
|
|
const char *name;
|
2017-01-15 20:48:41 +00:00
|
|
|
size_t namelen;
|
2017-01-11 14:56:44 +00:00
|
|
|
|
2020-08-25 11:35:32 +00:00
|
|
|
parsed = options_parse(s, idx);
|
|
|
|
if (parsed == NULL)
|
2017-03-08 14:43:40 +00:00
|
|
|
return (NULL);
|
2020-08-25 11:35:32 +00:00
|
|
|
if (*parsed == '@') {
|
2017-01-18 08:40:50 +00:00
|
|
|
*ambiguous = 0;
|
2020-08-25 11:35:32 +00:00
|
|
|
return (parsed);
|
2017-01-18 08:40:50 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 11:35:32 +00:00
|
|
|
name = options_map_name(parsed);
|
|
|
|
namelen = strlen(name);
|
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
found = NULL;
|
|
|
|
for (oe = options_table; oe->name != NULL; oe++) {
|
|
|
|
if (strcmp(oe->name, name) == 0) {
|
|
|
|
found = oe;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strncmp(oe->name, name, namelen) == 0) {
|
|
|
|
if (found != NULL) {
|
|
|
|
*ambiguous = 1;
|
2020-08-25 11:35:32 +00:00
|
|
|
free(parsed);
|
2017-01-15 20:48:41 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
found = oe;
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 11:35:32 +00:00
|
|
|
free(parsed);
|
2017-01-15 20:48:41 +00:00
|
|
|
if (found == NULL) {
|
|
|
|
*ambiguous = 0;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (xstrdup(found->name));
|
|
|
|
}
|
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *
|
2017-01-15 20:48:41 +00:00
|
|
|
options_match_get(struct options *oo, const char *s, int *idx, int only,
|
2019-12-03 10:47:22 +00:00
|
|
|
int *ambiguous)
|
2017-01-15 20:48:41 +00:00
|
|
|
{
|
2017-01-16 14:49:14 +00:00
|
|
|
char *name;
|
|
|
|
struct options_entry *o;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
name = options_match(s, idx, ambiguous);
|
|
|
|
if (name == NULL)
|
|
|
|
return (NULL);
|
|
|
|
*ambiguous = 0;
|
|
|
|
if (only)
|
|
|
|
o = options_get_only(oo, name);
|
|
|
|
else
|
|
|
|
o = options_get(oo, name);
|
|
|
|
free(name);
|
2009-09-21 14:56:03 +00:00
|
|
|
return (o);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
const char *
|
|
|
|
options_get_string(struct options *oo, const char *name)
|
|
|
|
{
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
o = options_get(oo, name);
|
|
|
|
if (o == NULL)
|
|
|
|
fatalx("missing option %s", name);
|
|
|
|
if (!OPTIONS_IS_STRING(o))
|
|
|
|
fatalx("option %s is not a string", name);
|
2019-04-23 20:36:55 +00:00
|
|
|
return (o->value.string);
|
2017-01-15 20:48:41 +00:00
|
|
|
}
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
long long
|
|
|
|
options_get_number(struct options *oo, const char *name)
|
|
|
|
{
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_get(oo, name);
|
|
|
|
if (o == NULL)
|
2015-11-18 13:06:54 +00:00
|
|
|
fatalx("missing option %s", name);
|
2017-01-15 20:48:41 +00:00
|
|
|
if (!OPTIONS_IS_NUMBER(o))
|
|
|
|
fatalx("option %s is not a number", name);
|
2019-04-23 20:36:55 +00:00
|
|
|
return (o->value.number);
|
2017-01-15 20:48:41 +00:00
|
|
|
}
|
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *
|
2017-01-15 20:48:41 +00:00
|
|
|
options_set_string(struct options *oo, const char *name, int append,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o;
|
|
|
|
va_list ap;
|
2020-05-16 15:01:30 +00:00
|
|
|
const char *separator = "";
|
2017-01-16 14:49:14 +00:00
|
|
|
char *s, *value;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&s, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
o = options_get_only(oo, name);
|
|
|
|
if (o != NULL && append && OPTIONS_IS_STRING(o)) {
|
2020-05-16 15:01:30 +00:00
|
|
|
if (*name != '@') {
|
|
|
|
separator = o->tableentry->separator;
|
|
|
|
if (separator == NULL)
|
|
|
|
separator = "";
|
|
|
|
}
|
|
|
|
xasprintf(&value, "%s%s%s", o->value.string, separator, s);
|
2017-01-15 20:48:41 +00:00
|
|
|
free(s);
|
|
|
|
} else
|
|
|
|
value = s;
|
|
|
|
if (o == NULL && *name == '@')
|
|
|
|
o = options_add(oo, name);
|
|
|
|
else if (o == NULL) {
|
|
|
|
o = options_default(oo, options_parent_table_entry(oo, name));
|
|
|
|
if (o == NULL)
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!OPTIONS_IS_STRING(o))
|
|
|
|
fatalx("option %s is not a string", name);
|
2019-04-23 20:36:55 +00:00
|
|
|
free(o->value.string);
|
|
|
|
o->value.string = value;
|
2020-05-16 15:01:30 +00:00
|
|
|
o->cached = 0;
|
2017-01-15 20:48:41 +00:00
|
|
|
return (o);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2014-01-28 23:07:09 +00:00
|
|
|
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *
|
2017-01-15 20:48:41 +00:00
|
|
|
options_set_number(struct options *oo, const char *name, long long value)
|
|
|
|
{
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
if (*name == '@')
|
|
|
|
fatalx("user option %s must be a string", name);
|
|
|
|
|
|
|
|
o = options_get_only(oo, name);
|
|
|
|
if (o == NULL) {
|
|
|
|
o = options_default(oo, options_parent_table_entry(oo, name));
|
|
|
|
if (o == NULL)
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!OPTIONS_IS_NUMBER(o))
|
|
|
|
fatalx("option %s is not a number", name);
|
2019-04-23 20:36:55 +00:00
|
|
|
o->value.number = value;
|
2017-01-15 20:48:41 +00:00
|
|
|
return (o);
|
|
|
|
}
|
|
|
|
|
2019-06-20 11:59:59 +00:00
|
|
|
int
|
2019-06-20 07:41:29 +00:00
|
|
|
options_scope_from_name(struct args *args, int window,
|
|
|
|
const char *name, struct cmd_find_state *fs, struct options **oo,
|
|
|
|
char **cause)
|
|
|
|
{
|
2019-06-20 11:59:59 +00:00
|
|
|
struct session *s = fs->s;
|
|
|
|
struct winlink *wl = fs->wl;
|
|
|
|
struct window_pane *wp = fs->wp;
|
|
|
|
const char *target = args_get(args, 't');
|
|
|
|
const struct options_table_entry *oe;
|
2019-06-20 18:13:04 +00:00
|
|
|
int scope = OPTIONS_TABLE_NONE;
|
2019-06-20 07:41:29 +00:00
|
|
|
|
|
|
|
if (*name == '@')
|
|
|
|
return (options_scope_from_flags(args, window, fs, oo, cause));
|
|
|
|
|
2019-06-20 11:59:59 +00:00
|
|
|
for (oe = options_table; oe->name != NULL; oe++) {
|
|
|
|
if (strcmp(oe->name, name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (oe->name == NULL) {
|
2019-06-20 07:41:29 +00:00
|
|
|
xasprintf(cause, "unknown option: %s", name);
|
|
|
|
return (OPTIONS_TABLE_NONE);
|
|
|
|
}
|
2019-06-20 18:13:04 +00:00
|
|
|
switch (oe->scope) {
|
2019-06-20 11:59:59 +00:00
|
|
|
case OPTIONS_TABLE_SERVER:
|
2019-06-20 07:41:29 +00:00
|
|
|
*oo = global_options;
|
2019-06-20 18:13:04 +00:00
|
|
|
scope = OPTIONS_TABLE_SERVER;
|
2019-06-20 11:59:59 +00:00
|
|
|
break;
|
|
|
|
case OPTIONS_TABLE_SESSION:
|
2019-06-20 18:13:04 +00:00
|
|
|
if (args_has(args, 'g')) {
|
2019-06-20 07:41:29 +00:00
|
|
|
*oo = global_s_options;
|
2019-06-20 18:13:04 +00:00
|
|
|
scope = OPTIONS_TABLE_SESSION;
|
|
|
|
} else if (s == NULL && target != NULL)
|
2019-06-20 11:59:59 +00:00
|
|
|
xasprintf(cause, "no such session: %s", target);
|
|
|
|
else if (s == NULL)
|
|
|
|
xasprintf(cause, "no current session");
|
2019-06-20 18:13:04 +00:00
|
|
|
else {
|
2019-06-20 07:41:29 +00:00
|
|
|
*oo = s->options;
|
2019-06-20 18:13:04 +00:00
|
|
|
scope = OPTIONS_TABLE_SESSION;
|
|
|
|
}
|
2019-06-20 11:59:59 +00:00
|
|
|
break;
|
|
|
|
case OPTIONS_TABLE_WINDOW|OPTIONS_TABLE_PANE:
|
|
|
|
if (args_has(args, 'p')) {
|
|
|
|
if (wp == NULL && target != NULL)
|
|
|
|
xasprintf(cause, "no such pane: %s", target);
|
|
|
|
else if (wp == NULL)
|
|
|
|
xasprintf(cause, "no current pane");
|
2019-06-20 18:13:04 +00:00
|
|
|
else {
|
2019-06-20 11:59:59 +00:00
|
|
|
*oo = wp->options;
|
2019-06-20 18:13:04 +00:00
|
|
|
scope = OPTIONS_TABLE_PANE;
|
|
|
|
}
|
2019-06-20 11:59:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case OPTIONS_TABLE_WINDOW:
|
2019-06-20 18:13:04 +00:00
|
|
|
if (args_has(args, 'g')) {
|
2019-06-20 07:41:29 +00:00
|
|
|
*oo = global_w_options;
|
2019-06-20 18:13:04 +00:00
|
|
|
scope = OPTIONS_TABLE_WINDOW;
|
|
|
|
} else if (wl == NULL && target != NULL)
|
2019-06-20 11:59:59 +00:00
|
|
|
xasprintf(cause, "no such window: %s", target);
|
|
|
|
else if (wl == NULL)
|
|
|
|
xasprintf(cause, "no current window");
|
2019-06-20 18:13:04 +00:00
|
|
|
else {
|
2019-06-20 07:41:29 +00:00
|
|
|
*oo = wl->window->options;
|
2019-06-20 18:13:04 +00:00
|
|
|
scope = OPTIONS_TABLE_WINDOW;
|
|
|
|
}
|
2019-06-20 11:59:59 +00:00
|
|
|
break;
|
2019-06-20 07:41:29 +00:00
|
|
|
}
|
|
|
|
return (scope);
|
|
|
|
}
|
|
|
|
|
2019-06-20 11:59:59 +00:00
|
|
|
int
|
2017-01-15 20:48:41 +00:00
|
|
|
options_scope_from_flags(struct args *args, int window,
|
|
|
|
struct cmd_find_state *fs, struct options **oo, char **cause)
|
2014-01-28 23:07:09 +00:00
|
|
|
{
|
2019-06-20 11:59:59 +00:00
|
|
|
struct session *s = fs->s;
|
|
|
|
struct winlink *wl = fs->wl;
|
|
|
|
struct window_pane *wp = fs->wp;
|
|
|
|
const char *target = args_get(args, 't');
|
2014-01-28 23:07:09 +00:00
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
if (args_has(args, 's')) {
|
|
|
|
*oo = global_options;
|
|
|
|
return (OPTIONS_TABLE_SERVER);
|
|
|
|
}
|
|
|
|
|
2019-06-20 11:59:59 +00:00
|
|
|
if (args_has(args, 'p')) {
|
|
|
|
if (wp == NULL) {
|
|
|
|
if (target != NULL)
|
|
|
|
xasprintf(cause, "no such pane: %s", target);
|
|
|
|
else
|
|
|
|
xasprintf(cause, "no current pane");
|
|
|
|
return (OPTIONS_TABLE_NONE);
|
|
|
|
}
|
|
|
|
*oo = wp->options;
|
|
|
|
return (OPTIONS_TABLE_PANE);
|
|
|
|
} else if (window || args_has(args, 'w')) {
|
2017-01-15 20:48:41 +00:00
|
|
|
if (args_has(args, 'g')) {
|
|
|
|
*oo = global_w_options;
|
|
|
|
return (OPTIONS_TABLE_WINDOW);
|
|
|
|
}
|
|
|
|
if (wl == NULL) {
|
|
|
|
if (target != NULL)
|
|
|
|
xasprintf(cause, "no such window: %s", target);
|
|
|
|
else
|
|
|
|
xasprintf(cause, "no current window");
|
|
|
|
return (OPTIONS_TABLE_NONE);
|
|
|
|
}
|
|
|
|
*oo = wl->window->options;
|
|
|
|
return (OPTIONS_TABLE_WINDOW);
|
|
|
|
} else {
|
|
|
|
if (args_has(args, 'g')) {
|
|
|
|
*oo = global_s_options;
|
|
|
|
return (OPTIONS_TABLE_SESSION);
|
|
|
|
}
|
|
|
|
if (s == NULL) {
|
|
|
|
if (target != NULL)
|
|
|
|
xasprintf(cause, "no such session: %s", target);
|
|
|
|
else
|
|
|
|
xasprintf(cause, "no current session");
|
|
|
|
return (OPTIONS_TABLE_NONE);
|
|
|
|
}
|
|
|
|
*oo = s->options;
|
|
|
|
return (OPTIONS_TABLE_SESSION);
|
|
|
|
}
|
|
|
|
}
|
2020-05-16 15:01:30 +00:00
|
|
|
|
|
|
|
struct style *
|
|
|
|
options_string_to_style(struct options *oo, const char *name,
|
|
|
|
struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
const char *s;
|
|
|
|
char *expanded;
|
|
|
|
|
|
|
|
o = options_get(oo, name);
|
|
|
|
if (o == NULL || !OPTIONS_IS_STRING(o))
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (o->cached)
|
|
|
|
return (&o->style);
|
|
|
|
s = o->value.string;
|
|
|
|
log_debug("%s: %s is '%s'", __func__, name, s);
|
|
|
|
|
|
|
|
style_set(&o->style, &grid_default_cell);
|
|
|
|
o->cached = (strstr(s, "#{") == NULL);
|
|
|
|
|
|
|
|
if (ft != NULL && !o->cached) {
|
|
|
|
expanded = format_expand(ft, s);
|
|
|
|
if (style_parse(&o->style, &grid_default_cell, expanded) != 0) {
|
|
|
|
free(expanded);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
free(expanded);
|
|
|
|
} else {
|
|
|
|
if (style_parse(&o->style, &grid_default_cell, s) != 0)
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (&o->style);
|
|
|
|
}
|
2020-05-16 16:02:24 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
options_from_string_check(const struct options_table_entry *oe,
|
|
|
|
const char *value, char **cause)
|
|
|
|
{
|
|
|
|
struct style sy;
|
|
|
|
|
|
|
|
if (oe == NULL)
|
|
|
|
return (0);
|
|
|
|
if (strcmp(oe->name, "default-shell") == 0 && !checkshell(value)) {
|
|
|
|
xasprintf(cause, "not a suitable shell: %s", value);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (oe->pattern != NULL && fnmatch(oe->pattern, value, 0) != 0) {
|
|
|
|
xasprintf(cause, "value is invalid: %s", value);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if ((oe->flags & OPTIONS_TABLE_IS_STYLE) &&
|
|
|
|
strstr(value, "#{") == NULL &&
|
|
|
|
style_parse(&sy, &grid_default_cell, value) != 0) {
|
|
|
|
xasprintf(cause, "invalid style: %s", value);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
options_from_string_flag(struct options *oo, const char *name,
|
|
|
|
const char *value, char **cause)
|
|
|
|
{
|
|
|
|
int flag;
|
|
|
|
|
|
|
|
if (value == NULL || *value == '\0')
|
|
|
|
flag = !options_get_number(oo, name);
|
|
|
|
else if (strcmp(value, "1") == 0 ||
|
|
|
|
strcasecmp(value, "on") == 0 ||
|
|
|
|
strcasecmp(value, "yes") == 0)
|
|
|
|
flag = 1;
|
|
|
|
else if (strcmp(value, "0") == 0 ||
|
|
|
|
strcasecmp(value, "off") == 0 ||
|
|
|
|
strcasecmp(value, "no") == 0)
|
|
|
|
flag = 0;
|
|
|
|
else {
|
|
|
|
xasprintf(cause, "bad value: %s", value);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
options_set_number(oo, name, flag);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
options_from_string_choice(const struct options_table_entry *oe,
|
|
|
|
struct options *oo, const char *name, const char *value, char **cause)
|
|
|
|
{
|
|
|
|
const char **cp;
|
|
|
|
int n, choice = -1;
|
|
|
|
|
|
|
|
if (value == NULL) {
|
|
|
|
choice = options_get_number(oo, name);
|
|
|
|
if (choice < 2)
|
|
|
|
choice = !choice;
|
|
|
|
} else {
|
|
|
|
n = 0;
|
|
|
|
for (cp = oe->choices; *cp != NULL; cp++) {
|
|
|
|
if (strcmp(*cp, value) == 0)
|
|
|
|
choice = n;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (choice == -1) {
|
|
|
|
xasprintf(cause, "unknown value: %s", value);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
options_set_number(oo, name, choice);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
options_from_string(struct options *oo, const struct options_table_entry *oe,
|
|
|
|
const char *name, const char *value, int append, char **cause)
|
|
|
|
{
|
|
|
|
enum options_table_type type;
|
|
|
|
long long number;
|
|
|
|
const char *errstr, *new;
|
|
|
|
char *old;
|
|
|
|
key_code key;
|
|
|
|
|
|
|
|
if (oe != NULL) {
|
|
|
|
if (value == NULL &&
|
|
|
|
oe->type != OPTIONS_TABLE_FLAG &&
|
|
|
|
oe->type != OPTIONS_TABLE_CHOICE) {
|
|
|
|
xasprintf(cause, "empty value");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
type = oe->type;
|
|
|
|
} else {
|
|
|
|
if (*name != '@') {
|
|
|
|
xasprintf(cause, "bad option name");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
type = OPTIONS_TABLE_STRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case OPTIONS_TABLE_STRING:
|
|
|
|
old = xstrdup(options_get_string(oo, name));
|
|
|
|
options_set_string(oo, name, append, "%s", value);
|
|
|
|
|
|
|
|
new = options_get_string(oo, name);
|
|
|
|
if (options_from_string_check(oe, new, cause) != 0) {
|
|
|
|
options_set_string(oo, name, 0, "%s", old);
|
|
|
|
free(old);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
free(old);
|
|
|
|
return (0);
|
|
|
|
case OPTIONS_TABLE_NUMBER:
|
|
|
|
number = strtonum(value, oe->minimum, oe->maximum, &errstr);
|
|
|
|
if (errstr != NULL) {
|
|
|
|
xasprintf(cause, "value is %s: %s", errstr, value);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
options_set_number(oo, name, number);
|
|
|
|
return (0);
|
|
|
|
case OPTIONS_TABLE_KEY:
|
|
|
|
key = key_string_lookup_string(value);
|
|
|
|
if (key == KEYC_UNKNOWN) {
|
|
|
|
xasprintf(cause, "bad key: %s", value);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
options_set_number(oo, name, key);
|
|
|
|
return (0);
|
|
|
|
case OPTIONS_TABLE_COLOUR:
|
|
|
|
if ((number = colour_fromstring(value)) == -1) {
|
|
|
|
xasprintf(cause, "bad colour: %s", value);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
options_set_number(oo, name, number);
|
|
|
|
return (0);
|
|
|
|
case OPTIONS_TABLE_FLAG:
|
|
|
|
return (options_from_string_flag(oo, name, value, cause));
|
|
|
|
case OPTIONS_TABLE_CHOICE:
|
|
|
|
return (options_from_string_choice(oe, oo, name, value, cause));
|
|
|
|
case OPTIONS_TABLE_COMMAND:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
options_push_changes(const char *name)
|
|
|
|
{
|
|
|
|
struct client *loop;
|
|
|
|
struct session *s;
|
|
|
|
struct window *w;
|
|
|
|
struct window_pane *wp;
|
|
|
|
|
|
|
|
if (strcmp(name, "automatic-rename") == 0) {
|
|
|
|
RB_FOREACH(w, windows, &windows) {
|
|
|
|
if (w->active == NULL)
|
|
|
|
continue;
|
|
|
|
if (options_get_number(w->options, "automatic-rename"))
|
|
|
|
w->active->flags |= PANE_CHANGED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strcmp(name, "key-table") == 0) {
|
|
|
|
TAILQ_FOREACH(loop, &clients, entry)
|
|
|
|
server_client_set_key_table(loop, NULL);
|
|
|
|
}
|
|
|
|
if (strcmp(name, "user-keys") == 0) {
|
|
|
|
TAILQ_FOREACH(loop, &clients, entry) {
|
|
|
|
if (loop->tty.flags & TTY_OPENED)
|
|
|
|
tty_keys_build(&loop->tty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strcmp(name, "status") == 0 ||
|
|
|
|
strcmp(name, "status-interval") == 0)
|
|
|
|
status_timer_start_all();
|
|
|
|
if (strcmp(name, "monitor-silence") == 0)
|
|
|
|
alerts_reset_all();
|
|
|
|
if (strcmp(name, "window-style") == 0 ||
|
|
|
|
strcmp(name, "window-active-style") == 0) {
|
|
|
|
RB_FOREACH(wp, window_pane_tree, &all_window_panes)
|
|
|
|
wp->flags |= PANE_STYLECHANGED;
|
|
|
|
}
|
|
|
|
if (strcmp(name, "pane-border-status") == 0) {
|
|
|
|
RB_FOREACH(w, windows, &windows)
|
|
|
|
layout_fix_panes(w);
|
|
|
|
}
|
|
|
|
RB_FOREACH(s, sessions, &sessions)
|
|
|
|
status_update_cache(s);
|
|
|
|
|
|
|
|
recalculate_sizes();
|
|
|
|
TAILQ_FOREACH(loop, &clients, entry) {
|
|
|
|
if (loop->session != NULL)
|
|
|
|
server_redraw_client(loop);
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 08:18:34 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
options_remove_or_default(struct options_entry *o, int idx, char **cause)
|
|
|
|
{
|
|
|
|
struct options *oo = o->owner;
|
|
|
|
|
|
|
|
if (idx == -1) {
|
|
|
|
if (o->tableentry != NULL &&
|
|
|
|
(oo == global_options ||
|
|
|
|
oo == global_s_options ||
|
|
|
|
oo == global_w_options))
|
|
|
|
options_default(oo, o->tableentry);
|
|
|
|
else
|
|
|
|
options_remove(o);
|
|
|
|
} else if (options_array_set(o, idx, NULL, 0, cause) != 0)
|
|
|
|
return (-1);
|
|
|
|
return (0);
|
|
|
|
}
|