2009-06-02 00:58:49 +02:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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 13:53:01 +02:00
|
|
|
#include <stdlib.h>
|
2009-06-02 00:58:49 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Option handling; each option has a name, type and value and is stored in
|
2014-01-09 14:58:06 +01:00
|
|
|
* a red-black tree.
|
2009-06-02 00:58:49 +02:00
|
|
|
*/
|
|
|
|
|
2012-01-21 12:12:13 +01:00
|
|
|
RB_GENERATE(options_tree, options_entry, entry, options_cmp);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
options_cmp(struct options_entry *o1, struct options_entry *o2)
|
|
|
|
{
|
|
|
|
return (strcmp(o1->name, o2->name));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
options_init(struct options *oo, struct options *parent)
|
|
|
|
{
|
2012-01-21 12:12:13 +01:00
|
|
|
RB_INIT(&oo->tree);
|
2009-06-02 00:58:49 +02:00
|
|
|
oo->parent = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
options_free(struct options *oo)
|
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
|
2012-01-21 12:12:13 +01:00
|
|
|
while (!RB_EMPTY(&oo->tree)) {
|
|
|
|
o = RB_ROOT(&oo->tree);
|
|
|
|
RB_REMOVE(options_tree, &oo->tree, o);
|
2012-07-10 13:53:01 +02:00
|
|
|
free(o->name);
|
2009-06-02 00:58:49 +02:00
|
|
|
if (o->type == OPTIONS_STRING)
|
2012-07-10 13:53:01 +02:00
|
|
|
free(o->str);
|
|
|
|
free(o);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct options_entry *
|
|
|
|
options_find1(struct options *oo, const char *name)
|
|
|
|
{
|
|
|
|
struct options_entry p;
|
|
|
|
|
|
|
|
p.name = (char *) name;
|
2012-01-21 12:12:13 +01:00
|
|
|
return (RB_FIND(options_tree, &oo->tree, &p));
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct options_entry *
|
|
|
|
options_find(struct options *oo, const char *name)
|
|
|
|
{
|
|
|
|
struct options_entry *o, p;
|
|
|
|
|
|
|
|
p.name = (char *) name;
|
2012-01-21 12:12:13 +01:00
|
|
|
o = RB_FIND(options_tree, &oo->tree, &p);
|
2009-06-02 00:58:49 +02:00
|
|
|
while (o == NULL) {
|
|
|
|
oo = oo->parent;
|
|
|
|
if (oo == NULL)
|
|
|
|
break;
|
2012-01-21 12:12:13 +01:00
|
|
|
o = RB_FIND(options_tree, &oo->tree, &p);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
return (o);
|
|
|
|
}
|
|
|
|
|
2009-07-21 21:54:22 +02:00
|
|
|
void
|
2009-06-02 00:58:49 +02:00
|
|
|
options_remove(struct options *oo, const char *name)
|
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
|
|
|
|
if ((o = options_find1(oo, name)) == NULL)
|
2009-07-21 21:54:22 +02:00
|
|
|
return;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2012-01-21 12:12:13 +01:00
|
|
|
RB_REMOVE(options_tree, &oo->tree, o);
|
2012-07-10 13:53:01 +02:00
|
|
|
free(o->name);
|
2009-06-02 00:58:49 +02:00
|
|
|
if (o->type == OPTIONS_STRING)
|
2012-07-10 13:53:01 +02:00
|
|
|
free(o->str);
|
|
|
|
free(o);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
2009-09-21 16:56:03 +02:00
|
|
|
struct options_entry *printflike3
|
2009-06-02 00:58:49 +02:00
|
|
|
options_set_string(struct options *oo, const char *name, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if ((o = options_find1(oo, name)) == NULL) {
|
|
|
|
o = xmalloc(sizeof *o);
|
|
|
|
o->name = xstrdup(name);
|
2012-01-21 12:12:13 +01:00
|
|
|
RB_INSERT(options_tree, &oo->tree, o);
|
2014-01-29 00:07:09 +01:00
|
|
|
memcpy(&o->style, &grid_default_cell, sizeof o->style);
|
2009-06-02 00:58:49 +02:00
|
|
|
} else if (o->type == OPTIONS_STRING)
|
2012-07-10 13:53:01 +02:00
|
|
|
free(o->str);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
o->type = OPTIONS_STRING;
|
2009-09-21 09:00:09 +02:00
|
|
|
xvasprintf(&o->str, fmt, ap);
|
2009-06-02 00:58:49 +02:00
|
|
|
va_end(ap);
|
2009-09-21 16:56:03 +02:00
|
|
|
return (o);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
options_get_string(struct options *oo, const char *name)
|
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
|
|
|
|
if ((o = options_find(oo, name)) == NULL)
|
|
|
|
fatalx("missing option");
|
|
|
|
if (o->type != OPTIONS_STRING)
|
|
|
|
fatalx("option not a string");
|
2009-09-21 09:00:09 +02:00
|
|
|
return (o->str);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
2009-09-21 16:56:03 +02:00
|
|
|
struct options_entry *
|
2009-06-02 00:58:49 +02:00
|
|
|
options_set_number(struct options *oo, const char *name, long long value)
|
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
|
|
|
|
if ((o = options_find1(oo, name)) == NULL) {
|
|
|
|
o = xmalloc(sizeof *o);
|
|
|
|
o->name = xstrdup(name);
|
2012-01-21 12:12:13 +01:00
|
|
|
RB_INSERT(options_tree, &oo->tree, o);
|
2014-01-29 00:07:09 +01:00
|
|
|
memcpy(&o->style, &grid_default_cell, sizeof o->style);
|
2009-06-02 00:58:49 +02:00
|
|
|
} else if (o->type == OPTIONS_STRING)
|
2012-07-10 13:53:01 +02:00
|
|
|
free(o->str);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
o->type = OPTIONS_NUMBER;
|
2009-09-21 09:00:09 +02:00
|
|
|
o->num = value;
|
2009-09-21 16:56:03 +02:00
|
|
|
return (o);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
long long
|
|
|
|
options_get_number(struct options *oo, const char *name)
|
|
|
|
{
|
|
|
|
struct options_entry *o;
|
|
|
|
|
|
|
|
if ((o = options_find(oo, name)) == NULL)
|
|
|
|
fatalx("missing option");
|
|
|
|
if (o->type != OPTIONS_NUMBER)
|
|
|
|
fatalx("option not a number");
|
2009-09-21 09:00:09 +02:00
|
|
|
return (o->num);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
2014-01-29 00:07:09 +01:00
|
|
|
|
|
|
|
struct options_entry *
|
|
|
|
options_set_style(struct options *oo, const char *name, const char *value,
|
|
|
|
int append)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
} else if (o->type == OPTIONS_STRING)
|
|
|
|
free(o->str);
|
|
|
|
|
|
|
|
if (!append)
|
|
|
|
memcpy(&o->style, &grid_default_cell, sizeof o->style);
|
|
|
|
|
|
|
|
o->type = OPTIONS_STYLE;
|
|
|
|
if (style_parse(&grid_default_cell, &o->style, value) == -1)
|
|
|
|
return (NULL);
|
|
|
|
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)
|
|
|
|
fatalx("missing option");
|
|
|
|
if (o->type != OPTIONS_STYLE)
|
|
|
|
fatalx("option not a style");
|
|
|
|
return (&o->style);
|
|
|
|
}
|