Esempio n. 1
0
File: style.c Progetto: Darkoe/tmux
/* Synchronize new -style option with the old one. */
void
style_update_new(struct options *oo, const char *name, const char *newname)
{
	int			 value;
	struct grid_cell	*gc;
	struct options_entry	*o;

	/* It's a colour or attribute, but with no -style equivalent. */
	if (newname == NULL)
		return;

	o = options_find1(oo, newname);
	if (o == NULL)
		o = options_set_style(oo, newname, "default", 0);
	gc = &o->style;

	o = options_find1(oo, name);
	if (o == NULL)
		o = options_set_number(oo, name, 8);
	value = o->num;

	if (strstr(name, "-bg") != NULL)
		colour_set_bg(gc, value);
	else if (strstr(name, "-fg") != NULL)
		colour_set_fg(gc, value);
	else if (strstr(name, "-attr") != NULL)
		gc->attr = value;
}
Esempio n. 2
0
enum cmd_retval
cmd_show_options_one(struct cmd *self, struct cmd_q *cmdq,
    struct options *oo, int quiet)
{
	struct args				*args = self->args;
	const char				*name = args->argv[0];
	const struct options_table_entry	*table, *oe;
	struct options_entry			*o;
	const char				*optval;

retry:
	if (*name == '@') {
		if ((o = options_find1(oo, name)) == NULL) {
			if (quiet)
				return (CMD_RETURN_NORMAL);
			cmdq_error(cmdq, "unknown option: %s", name);
			return (CMD_RETURN_ERROR);
		}
		if (args_has(self->args, 'v'))
			cmdq_print(cmdq, "%s", o->str);
		else
			cmdq_print(cmdq, "%s \"%s\"", o->name, o->str);
		return (CMD_RETURN_NORMAL);
	}

	table = oe = NULL;
	if (options_table_find(name, &table, &oe) != 0) {
		cmdq_error(cmdq, "ambiguous option: %s", name);
		return (CMD_RETURN_ERROR);
	}
	if (oe == NULL) {
		if (quiet)
		    return (CMD_RETURN_NORMAL);
		cmdq_error(cmdq, "unknown option: %s", name);
		return (CMD_RETURN_ERROR);
	}
	if (oe->style != NULL) {
		name = oe->style;
		goto retry;
	}
	if ((o = options_find1(oo, oe->name)) == NULL)
		return (CMD_RETURN_NORMAL);
	optval = options_table_print_entry(oe, o, args_has(self->args, 'v'));
	if (args_has(self->args, 'v'))
		cmdq_print(cmdq, "%s", optval);
	else
		cmdq_print(cmdq, "%s %s", oe->name, optval);
	return (CMD_RETURN_NORMAL);
}
Esempio n. 3
0
enum cmd_retval
cmd_show_options_all(struct cmd *self, struct cmd_q *cmdq,
    const struct options_table_entry *table, struct options *oo)
{
	const struct options_table_entry	*oe;
	struct options_entry			*o;
	const char				*optval;

	o = options_first(oo);
	while (o != NULL) {
		if (*o->name == '@') {
			if (args_has(self->args, 'v'))
				cmdq_print(cmdq, "%s", o->str);
			else
				cmdq_print(cmdq, "%s \"%s\"", o->name, o->str);
		}
		o = options_next(o);
	}

	for (oe = table; oe->name != NULL; oe++) {
		if (oe->style != NULL)
			continue;
		if ((o = options_find1(oo, oe->name)) == NULL)
			continue;
		optval = options_table_print_entry(oe, o,
		    args_has(self->args, 'v'));
		if (args_has(self->args, 'v'))
			cmdq_print(cmdq, "%s", optval);
		else
			cmdq_print(cmdq, "%s %s", oe->name, optval);
	}

	return (CMD_RETURN_NORMAL);
}
Esempio n. 4
0
int
cmd_show_window_options_exec(struct cmd *self, struct cmd_ctx *ctx)
{
    struct cmd_target_data		*data = self->data;
    struct winlink			*wl;
    struct options			*oo;
    struct options_entry		*o;
    const struct set_option_entry	*entry;
    const char			*optval;

    if (data->chflags & CMD_CHFLAG('g'))
        oo = &global_w_options;
    else {
        if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
            return (-1);
        oo = &wl->window->options;
    }

    for (entry = set_window_option_table; entry->name != NULL; entry++) {
        if ((o = options_find1(oo, entry->name)) == NULL)
            continue;
        optval = set_option_print(entry, o);
        ctx->print(ctx, "%s %s", entry->name, optval);
    }

    return (0);
}
Esempio n. 5
0
struct options_entry *
options_set_style(struct options *oo, const char *name, const char *value,
    int append)
{
	struct options_entry	*o;
	struct grid_cell	 tmpgc;

	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);

	if (o == 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);

	o->type = OPTIONS_STYLE;
	memcpy(&o->style, &tmpgc, sizeof o->style);
	return (o);
}
Esempio n. 6
0
void
options_remove(struct options *oo, const char *name)
{
	struct options_entry	*o;

	if ((o = options_find1(oo, name)) != NULL)
		options_free1(oo, o);
}
Esempio n. 7
0
/* Set user option. */
enum cmd_retval
cmd_set_option_user(struct cmd *self, struct cmd_ctx *ctx, const char* optstr,
    const char *valstr)
{
	struct args	*args = self->args;
	struct session	*s;
	struct winlink	*wl;
	struct options	*oo;

	if (args_has(args, 's'))
		oo = &global_options;
	else if (args_has(self->args, 'w') ||
	    self->entry == &cmd_set_window_option_entry) {
		if (args_has(self->args, 'g'))
			oo = &global_w_options;
		else {
			wl = cmd_find_window(ctx, args_get(args, 't'), NULL);
			if (wl == NULL)
				return (CMD_RETURN_ERROR);
			oo = &wl->window->options;
		}
	} else {
		if (args_has(self->args, 'g'))
			oo = &global_s_options;
		else {
			s = cmd_find_session(ctx, args_get(args, 't'), 0);
			if (s == NULL)
				return (CMD_RETURN_ERROR);
			oo = &s->options;
		}
	}

	if (args_has(args, 'u')) {
		if (options_find1(oo, optstr) == NULL) {
			ctx->error(ctx, "unknown option: %s", optstr);
			return (CMD_RETURN_ERROR);
		}
		if (valstr != NULL) {
			ctx->error(ctx, "value passed to unset option: %s",
			    optstr);
			return (CMD_RETURN_ERROR);
		}
		options_remove(oo, optstr);
	} else {
		if (valstr == NULL) {
			ctx->error(ctx, "empty value");
			return (CMD_RETURN_ERROR);
		}
		options_set_string(oo, optstr, "%s", valstr);
		if (!args_has(args, 'q'))
			ctx->info(ctx, "set option: %s -> %s", optstr, valstr);
	}
	return (CMD_RETURN_NORMAL);
}
Esempio n. 8
0
void
options_remove(struct options *oo, const char *name)
{
	struct options_entry	*o;

	if ((o = options_find1(oo, name)) == NULL)
		return;

	SPLAY_REMOVE(options_tree, &oo->tree, o);
	xfree(o->name);
	if (o->type == OPTIONS_STRING)
		xfree(o->value.string);
	xfree(o);
}
Esempio n. 9
0
void
options_remove(struct options *oo, const char *name)
{
	struct options_entry	*o;

	if ((o = options_find1(oo, name)) == NULL)
		return;

	RB_REMOVE(options_tree, &oo->tree, o);
	free(o->name);
	if (o->type == OPTIONS_STRING)
		free(o->str);
	free(o);
}
Esempio n. 10
0
void
options_remove(struct options *oo, const char *name)
{
	struct options_entry	*o;

	if ((o = options_find1(oo, name)) == NULL)
		return;

	SPLAY_REMOVE(options_tree, &oo->tree, o);
	xfree(o->name);
	if (o->type == OPTIONS_STRING)
		xfree(o->str);
	else if (o->type == OPTIONS_DATA)
		o->freefn(o->data);
	xfree(o);
}
Esempio n. 11
0
void
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);
		SPLAY_INSERT(options_tree, &oo->tree, o);
	} else if (o->type == OPTIONS_STRING)
		xfree(o->value.string);

	o->type = OPTIONS_NUMBER;
	o->value.number = value;

}
Esempio n. 12
0
struct options_entry *
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);
		RB_INSERT(options_tree, &oo->tree, o);
		memcpy(&o->style, &grid_default_cell, sizeof o->style);
	} else if (o->type == OPTIONS_STRING)
		free(o->str);

	o->type = OPTIONS_NUMBER;
	o->num = value;
	return (o);
}
Esempio n. 13
0
int
cmd_show_options_exec(struct cmd *self, struct cmd_ctx *ctx)
{
	struct args				*args = self->args;
	const struct options_table_entry	*table, *oe;
	struct session				*s;
	struct winlink				*wl;
	struct options				*oo;
	struct options_entry			*o;
	const char				*optval;

	if (args_has(self->args, 's')) {
		oo = &global_options;
		table = server_options_table;
	} else if (args_has(self->args, 'w') ||
	    self->entry == &cmd_show_window_options_entry) {
		table = window_options_table;
		if (args_has(self->args, 'g'))
			oo = &global_w_options;
		else {
			wl = cmd_find_window(ctx, args_get(args, 't'), NULL);
			if (wl == NULL)
				return (-1);
			oo = &wl->window->options;
		}
	} else {
		table = session_options_table;
		if (args_has(self->args, 'g'))
			oo = &global_s_options;
		else {
			s = cmd_find_session(ctx, args_get(args, 't'), 0);
			if (s == NULL)
				return (-1);
			oo = &s->options;
		}
	}

	for (oe = table; oe->name != NULL; oe++) {
		if ((o = options_find1(oo, oe->name)) == NULL)
			continue;
		optval = options_table_print_entry(oe, o);
		ctx->print(ctx, "%s %s", oe->name, optval);
	}

	return (0);
}
Esempio n. 14
0
void printflike3
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);
		SPLAY_INSERT(options_tree, &oo->tree, o);
	} else if (o->type == OPTIONS_STRING)
		xfree(o->value.string);

	va_start(ap, fmt);
	o->type = OPTIONS_STRING;
	xvasprintf(&o->value.string, fmt, ap);
	va_end(ap);
}
Esempio n. 15
0
struct options_entry *
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);
		SPLAY_INSERT(options_tree, &oo->tree, o);
	} else if (o->type == OPTIONS_STRING)
		xfree(o->str);
	else if (o->type == OPTIONS_DATA)
		o->freefn(o->data);

	o->type = OPTIONS_NUMBER;
	o->num = value;
	return (o);
}
Esempio n. 16
0
struct options_entry *
options_set_data(
    struct options *oo, const char *name, void *value, void (*freefn)(void *))
{
	struct options_entry	*o;

	if ((o = options_find1(oo, name)) == NULL) {
		o = xmalloc(sizeof *o);
		o->name = xstrdup(name);
		SPLAY_INSERT(options_tree, &oo->tree, o);
	} else if (o->type == OPTIONS_STRING)
		xfree(o->str);
	else if (o->type == OPTIONS_DATA)
		o->freefn(o->data);

	o->type = OPTIONS_DATA;
	o->data = value;
	o->freefn = freefn;
	return (o);
}
Esempio n. 17
0
struct options_entry *
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);
		RB_INSERT(options_tree, &oo->tree, o);
		memcpy(&o->style, &grid_default_cell, sizeof o->style);
	} else if (o->type == OPTIONS_STRING)
		free(o->str);

	va_start(ap, fmt);
	o->type = OPTIONS_STRING;
	xvasprintf(&o->str, fmt, ap);
	va_end(ap);
	return (o);
}
Esempio n. 18
0
enum cmd_retval
cmd_set_option_exec(struct cmd *self, struct cmd_q *cmdq)
{
	struct args				*args = self->args;
	const struct options_table_entry	*table, *oe;
	struct session				*s;
	struct winlink				*wl;
	struct client				*c;
	struct options				*oo;
	struct window				*w;
	const char				*optstr, *valstr;
	u_int					 i;

	/* Get the option name and value. */
	optstr = args->argv[0];
	if (*optstr == '\0') {
		cmdq_error(cmdq, "invalid option");
		return (CMD_RETURN_ERROR);
	}
	if (args->argc < 2)
		valstr = NULL;
	else
		valstr = args->argv[1];

	/* Is this a user option? */
	if (*optstr == '@')
		return (cmd_set_option_user(self, cmdq, optstr, valstr));

	/* Find the option entry, try each table. */
	table = oe = NULL;
	if (options_table_find(optstr, &table, &oe) != 0) {
		cmdq_error(cmdq, "ambiguous option: %s", optstr);
		return (CMD_RETURN_ERROR);
	}
	if (oe == NULL) {
		cmdq_error(cmdq, "unknown option: %s", optstr);
		return (CMD_RETURN_ERROR);
	}

	/* Work out the tree from the table. */
	if (table == server_options_table)
		oo = &global_options;
	else if (table == window_options_table) {
		if (args_has(self->args, 'g'))
			oo = &global_w_options;
		else {
			wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
			if (wl == NULL) {
				cmdq_error(cmdq,
				    "couldn't set '%s'%s", optstr,
				    (!args_has(args, 't') && !args_has(args,
				    'g')) ? " need target window or -g" : "");
				return (CMD_RETURN_ERROR);
			}
			oo = &wl->window->options;
		}
	} else if (table == session_options_table) {
		if (args_has(self->args, 'g'))
			oo = &global_s_options;
		else {
			s = cmd_find_session(cmdq, args_get(args, 't'), 0);
			if (s == NULL) {
				cmdq_error(cmdq,
				    "couldn't set '%s'%s", optstr,
				    (!args_has(args, 't') && !args_has(args,
				    'g')) ? " need target session or -g" : "");
				return (CMD_RETURN_ERROR);
			}
			oo = &s->options;
		}
	} else {
		cmdq_error(cmdq, "unknown table");
		return (CMD_RETURN_ERROR);
	}

	/* Unset or set the option. */
	if (args_has(args, 'u')) {
		if (cmd_set_option_unset(self, cmdq, oe, oo, valstr) != 0)
			return (CMD_RETURN_ERROR);
	} else {
		if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) {
			if (!args_has(args, 'q'))
				cmdq_print(cmdq, "already set: %s", optstr);
			return (CMD_RETURN_NORMAL);
		}
		if (cmd_set_option_set(self, cmdq, oe, oo, valstr) != 0)
			return (CMD_RETURN_ERROR);
	}

	/* Start or stop timers when automatic-rename changed. */
	if (strcmp(oe->name, "automatic-rename") == 0) {
		for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
			if ((w = ARRAY_ITEM(&windows, i)) == NULL)
				continue;
			if (options_get_number(&w->options, "automatic-rename"))
				queue_window_name(w);
			else if (event_initialized(&w->name_timer))
				evtimer_del(&w->name_timer);
		}
	}

	/* Update sizes and redraw. May not need it but meh. */
	recalculate_sizes();
	for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
		c = ARRAY_ITEM(&clients, i);
		if (c != NULL && c->session != NULL)
			server_redraw_client(c);
	}

	return (CMD_RETURN_NORMAL);
}
Esempio n. 19
0
int
cmd_show_options_exec(struct cmd *self, struct cmd_ctx *ctx)
{
	struct cmd_target_data		*data = self->data;
	struct session			*s;
	struct options			*oo;
	const struct set_option_entry   *entry;
	u_int				 i;
	char				*vs;
	long long			 vn;

	if (data->flags & CMD_GFLAG)
		oo = &global_options;
	else {
		if ((s = cmd_find_session(ctx, data->target)) == NULL)
			return (-1);
		oo = &s->options;
	}

	for (i = 0; i < NSETOPTION; i++) {
		entry = &set_option_table[i];

		if (options_find1(oo, entry->name) == NULL)
			continue;

		switch (entry->type) {
		case SET_OPTION_STRING:
			vs = options_get_string(oo, entry->name);
			ctx->print(ctx, "%s \"%s\"", entry->name, vs);
			break;
		case SET_OPTION_NUMBER:
			vn = options_get_number(oo, entry->name);
			ctx->print(ctx, "%s %lld", entry->name, vn);
			break;
		case SET_OPTION_KEY:
			vn = options_get_number(oo, entry->name);
 			ctx->print(ctx, "%s %s",
			    entry->name, key_string_lookup_key(vn));
			break;
		case SET_OPTION_COLOUR:
			vn = options_get_number(oo, entry->name);
 			ctx->print(ctx, "%s %s",
			    entry->name, colour_tostring(vn));
			break;
		case SET_OPTION_ATTRIBUTES:
			vn = options_get_number(oo, entry->name);
 			ctx->print(ctx, "%s %s",
			    entry->name, attributes_tostring(vn));
			break;
		case SET_OPTION_FLAG:
			vn = options_get_number(oo, entry->name);
			if (vn)
				ctx->print(ctx, "%s on", entry->name);
			else
				ctx->print(ctx, "%s off", entry->name);
			break;
		case SET_OPTION_CHOICE:
			vn = options_get_number(oo, entry->name);
			ctx->print(ctx, "%s %s",
			    entry->name, entry->choices[vn]);
			break;
		}
	}

	return (0);
}