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>
|
|
|
|
|
|
|
|
#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
|
|
|
*/
|
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
struct options {
|
|
|
|
RB_HEAD(options_tree, options_entry) tree;
|
|
|
|
struct options *parent;
|
|
|
|
};
|
|
|
|
|
2015-12-11 15:46:57 +00:00
|
|
|
static int options_cmp(struct options_entry *, struct options_entry *);
|
2016-10-10 21:29:23 +00:00
|
|
|
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
|
2009-06-01 22:58:49 +00:00
|
|
|
options_cmp(struct options_entry *o1, struct options_entry *o2)
|
|
|
|
{
|
|
|
|
return (strcmp(o1->name, o2->name));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-11 15:46:57 +00:00
|
|
|
static void
|
|
|
|
options_free1(struct options *oo, struct options_entry *o)
|
|
|
|
{
|
|
|
|
RB_REMOVE(options_tree, &oo->tree, o);
|
|
|
|
free((char *)o->name);
|
|
|
|
if (o->type == OPTIONS_STRING)
|
|
|
|
free(o->str);
|
|
|
|
free(o);
|
|
|
|
}
|
|
|
|
|
2017-01-11 14:56:44 +00:00
|
|
|
static struct options_entry *
|
2017-01-12 15:36:35 +00:00
|
|
|
options_new(struct options *oo, const char *name)
|
2017-01-11 14:56:44 +00:00
|
|
|
{
|
|
|
|
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);
|
2017-01-12 15:36:35 +00:00
|
|
|
} else if (o->type == OPTIONS_STRING)
|
|
|
|
free(o->str);
|
2017-01-11 14:56:44 +00:00
|
|
|
return (o);
|
|
|
|
}
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
|
|
|
options_free(struct options *oo)
|
|
|
|
{
|
2015-12-11 15:46:57 +00:00
|
|
|
struct options_entry *o, *o1;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2015-12-11 15:46:57 +00:00
|
|
|
RB_FOREACH_SAFE (o, options_tree, &oo->tree, o1)
|
|
|
|
options_free1(oo, o);
|
2015-10-27 15:58:42 +00:00
|
|
|
free(oo);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct options_entry *
|
|
|
|
options_first(struct options *oo)
|
|
|
|
{
|
|
|
|
return (RB_MIN(options_tree, &oo->tree));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct options_entry *
|
|
|
|
options_next(struct options_entry *o)
|
|
|
|
{
|
|
|
|
return (RB_NEXT(options_tree, &oo->tree, o));
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct options_entry *
|
|
|
|
options_find1(struct options *oo, const char *name)
|
|
|
|
{
|
|
|
|
struct options_entry p;
|
|
|
|
|
2015-12-11 15:46:57 +00:00
|
|
|
p.name = (char *)name;
|
2012-01-21 11:12:13 +00:00
|
|
|
return (RB_FIND(options_tree, &oo->tree, &p));
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct options_entry *
|
|
|
|
options_find(struct options *oo, const char *name)
|
|
|
|
{
|
|
|
|
struct options_entry *o, p;
|
|
|
|
|
2015-12-11 15:46:57 +00:00
|
|
|
p.name = (char *)name;
|
2012-01-21 11:12:13 +00:00
|
|
|
o = RB_FIND(options_tree, &oo->tree, &p);
|
2009-06-01 22:58:49 +00:00
|
|
|
while (o == NULL) {
|
|
|
|
oo = oo->parent;
|
|
|
|
if (oo == NULL)
|
|
|
|
break;
|
2012-01-21 11:12:13 +00:00
|
|
|
o = RB_FIND(options_tree, &oo->tree, &p);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
return (o);
|
|
|
|
}
|
|
|
|
|
2009-07-21 19:54:22 +00:00
|
|
|
void
|
2009-06-01 22:58:49 +00:00
|
|
|
options_remove(struct options *oo, const char *name)
|
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
|
2015-12-11 15:46:57 +00:00
|
|
|
if ((o = options_find1(oo, name)) != NULL)
|
|
|
|
options_free1(oo, o);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 23:57:13 +00:00
|
|
|
struct options_entry *
|
2017-01-12 15:36:35 +00:00
|
|
|
options_set_string(struct options *oo, const char *name, int append,
|
|
|
|
const char *fmt, ...)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
va_list ap;
|
2017-01-12 15:36:35 +00:00
|
|
|
char *s, *value;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2017-01-12 15:36:35 +00:00
|
|
|
xvasprintf(&s, fmt, ap);
|
|
|
|
va_end(ap);
|
2017-01-11 14:56:44 +00:00
|
|
|
|
2017-01-12 15:36:35 +00:00
|
|
|
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);
|
2009-06-01 22:58:49 +00:00
|
|
|
o->type = OPTIONS_STRING;
|
2017-01-12 15:36:35 +00:00
|
|
|
o->str = value;
|
2017-01-11 14:56:44 +00:00
|
|
|
|
2009-09-21 14:56:03 +00:00
|
|
|
return (o);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
options_get_string(struct options *oo, const char *name)
|
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
|
|
|
|
if ((o = options_find(oo, name)) == NULL)
|
2015-11-18 13:06:54 +00:00
|
|
|
fatalx("missing option %s", name);
|
2009-06-01 22:58:49 +00:00
|
|
|
if (o->type != OPTIONS_STRING)
|
2015-11-18 13:06:54 +00:00
|
|
|
fatalx("option %s not a string", name);
|
2009-09-21 07:00:09 +00:00
|
|
|
return (o->str);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-09-21 14:56:03 +00:00
|
|
|
struct options_entry *
|
2009-06-01 22:58:49 +00:00
|
|
|
options_set_number(struct options *oo, const char *name, long long value)
|
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
|
2017-01-12 15:36:35 +00:00
|
|
|
o = options_new(oo, name);
|
2009-06-01 22:58:49 +00:00
|
|
|
o->type = OPTIONS_NUMBER;
|
2009-09-21 07:00:09 +00:00
|
|
|
o->num = value;
|
2017-01-11 14:56:44 +00:00
|
|
|
|
2009-09-21 14:56:03 +00:00
|
|
|
return (o);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long long
|
|
|
|
options_get_number(struct options *oo, const char *name)
|
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
|
|
|
|
if ((o = options_find(oo, name)) == NULL)
|
2015-11-18 13:06:54 +00:00
|
|
|
fatalx("missing option %s", name);
|
2009-06-01 22:58:49 +00:00
|
|
|
if (o->type != OPTIONS_NUMBER)
|
2015-11-18 13:06:54 +00:00
|
|
|
fatalx("option %s not a number", name);
|
2009-09-21 07:00:09 +00:00
|
|
|
return (o->num);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2014-01-28 23:07:09 +00:00
|
|
|
|
|
|
|
struct options_entry *
|
2017-01-12 15:36:35 +00:00
|
|
|
options_set_style(struct options *oo, const char *name, int append,
|
|
|
|
const char *value)
|
2014-01-28 23:07:09 +00:00
|
|
|
{
|
|
|
|
struct options_entry *o;
|
2015-02-18 15:32:37 +00:00
|
|
|
struct grid_cell tmpgc;
|
2014-01-28 23:07:09 +00:00
|
|
|
|
2015-02-18 15:32:37 +00:00
|
|
|
o = options_find1(oo, name);
|
|
|
|
if (o == NULL || !append)
|
|
|
|
memcpy(&tmpgc, &grid_default_cell, sizeof tmpgc);
|
|
|
|
else
|
|
|
|
memcpy(&tmpgc, &o->style, sizeof tmpgc);
|
|
|
|
|
|
|
|
if (style_parse(&grid_default_cell, &tmpgc, value) == -1)
|
|
|
|
return (NULL);
|
|
|
|
|
2017-01-12 15:36:35 +00:00
|
|
|
o = options_new(oo, name);
|
2014-01-28 23:07:09 +00:00
|
|
|
o->type = OPTIONS_STYLE;
|
2015-02-18 15:32:37 +00:00
|
|
|
memcpy(&o->style, &tmpgc, sizeof o->style);
|
2017-01-11 14:56:44 +00:00
|
|
|
|
2014-01-28 23:07:09 +00:00
|
|
|
return (o);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct grid_cell *
|
|
|
|
options_get_style(struct options *oo, const char *name)
|
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
|
|
|
|
if ((o = options_find(oo, name)) == NULL)
|
2015-11-18 13:06:54 +00:00
|
|
|
fatalx("missing option %s", name);
|
2014-01-28 23:07:09 +00:00
|
|
|
if (o->type != OPTIONS_STYLE)
|
2015-11-18 13:06:54 +00:00
|
|
|
fatalx("option %s not a style", name);
|
2014-01-28 23:07:09 +00:00
|
|
|
return (&o->style);
|
|
|
|
}
|