tmux/cmd-set-option.c

391 lines
9.6 KiB
C
Raw Normal View History

2008-09-26 06:45:28 +00:00
/* $Id: cmd-set-option.c,v 1.43 2008-09-26 06:45:25 nicm Exp $ */
2007-10-04 10:11:32 +00:00
/*
* Copyright (c) 2007 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 <getopt.h>
#include <stdlib.h>
#include <string.h>
#include "tmux.h"
/*
* Set an option.
*/
int cmd_set_option_parse(struct cmd *, int, char **, char **);
void cmd_set_option_exec(struct cmd *, struct cmd_ctx *);
void cmd_set_option_send(struct cmd *, struct buffer *);
void cmd_set_option_recv(struct cmd *, struct buffer *);
void cmd_set_option_free(struct cmd *);
2008-06-05 17:12:11 +00:00
void cmd_set_option_print(struct cmd *, char *, size_t);
2007-10-04 10:11:32 +00:00
struct cmd_set_option_data {
char *target;
2008-06-03 21:42:37 +00:00
int flag_global;
2007-10-04 10:11:32 +00:00
char *option;
char *value;
};
const struct cmd_entry cmd_set_option_entry = {
"set-option", "set",
2008-06-16 06:33:50 +00:00
"[-t target-session] option value",
0,
NULL,
2007-10-04 10:11:32 +00:00
cmd_set_option_parse,
cmd_set_option_exec,
2007-10-04 10:11:32 +00:00
cmd_set_option_send,
cmd_set_option_recv,
cmd_set_option_free,
2008-06-05 17:12:11 +00:00
cmd_set_option_print
2007-10-04 10:11:32 +00:00
};
const char *set_option_bell_action_list[] = {
"none", "any", "current", NULL
};
const char *set_option_mode_keys_list[] = {
2008-09-26 06:45:28 +00:00
"emacs", "vi", NULL
};
const struct set_option_entry set_option_table[NSETOPTION] = {
{ "bell-action", SET_OPTION_CHOICE, 0, 0, set_option_bell_action_list },
{ "buffer-limit", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
{ "default-command", SET_OPTION_STRING, 0, 0, NULL },
{ "display-time", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
{ "history-limit", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
{ "mode-keys", SET_OPTION_CHOICE, 0, 0, set_option_mode_keys_list },
{ "prefix", SET_OPTION_KEY, 0, 0, NULL },
{ "remain-by-default", SET_OPTION_FLAG, 0, 0, NULL },
{ "set-titles", SET_OPTION_FLAG, 0, 0, NULL },
{ "status", SET_OPTION_FLAG, 0, 0, NULL },
{ "status-bg", SET_OPTION_COLOUR, 0, 0, NULL },
{ "status-fg", SET_OPTION_COLOUR, 0, 0, NULL },
{ "status-interval", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
{ "status-left", SET_OPTION_STRING, 0, 0, NULL },
{ "status-right", SET_OPTION_STRING, 0, 0, NULL },
};
2008-09-26 06:45:28 +00:00
void set_option_string(struct cmd_ctx *,
struct options *, const struct set_option_entry *, char *);
void set_option_number(struct cmd_ctx *,
struct options *, const struct set_option_entry *, char *);
void set_option_key(struct cmd_ctx *,
struct options *, const struct set_option_entry *, char *);
void set_option_colour(struct cmd_ctx *,
struct options *, const struct set_option_entry *, char *);
void set_option_flag(struct cmd_ctx *,
struct options *, const struct set_option_entry *, char *);
void set_option_choice(struct cmd_ctx *,
struct options *, const struct set_option_entry *, char *);
2007-10-04 10:11:32 +00:00
int
cmd_set_option_parse(struct cmd *self, int argc, char **argv, char **cause)
2007-10-04 10:11:32 +00:00
{
struct cmd_set_option_data *data;
int opt;
self->data = data = xmalloc(sizeof *data);
data->target = NULL;
2008-06-03 21:42:37 +00:00
data->flag_global = 1;
2007-10-04 10:11:32 +00:00
data->option = NULL;
data->value = NULL;
2008-09-25 23:28:15 +00:00
while ((opt = getopt(argc, argv, GETOPT_PREFIX "t:s:")) != EOF) {
2007-10-04 10:11:32 +00:00
switch (opt) {
case 't':
if (data->target == NULL)
data->target = xstrdup(optarg);
2008-06-03 21:42:37 +00:00
data->flag_global = 0;
break;
2007-10-04 10:11:32 +00:00
default:
goto usage;
}
}
2007-10-04 10:11:32 +00:00
argc -= optind;
argv += optind;
if (argc != 1 && argc != 2)
goto usage;
data->option = xstrdup(argv[0]);
if (argc == 2)
data->value = xstrdup(argv[1]);
return (0);
usage:
xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
2007-10-04 10:11:32 +00:00
self->entry->free(self);
2007-10-04 10:11:32 +00:00
return (-1);
}
void
cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx)
2007-10-04 10:11:32 +00:00
{
struct cmd_set_option_data *data = self->data;
2008-06-03 21:42:37 +00:00
struct session *s;
struct client *c;
2008-06-03 21:42:37 +00:00
struct options *oo;
const struct set_option_entry *entry;
2007-10-30 10:59:43 +00:00
u_int i;
2007-10-04 10:11:32 +00:00
if (data == NULL)
return;
2008-06-03 21:42:37 +00:00
if (data->flag_global ||
((s = cmd_find_session(ctx, data->target))) == NULL)
2008-06-03 21:42:37 +00:00
oo = &global_options;
else
oo = &s->options;
2007-10-04 10:11:32 +00:00
if (*data->option == '\0') {
ctx->error(ctx, "invalid option");
return;
}
entry = NULL;
for (i = 0; i < NSETOPTION; i++) {
if (strncmp(set_option_table[i].name,
data->option, strlen(data->option)) != 0)
continue;
if (entry != NULL) {
ctx->error(ctx, "ambiguous option: %s", data->option);
2007-10-12 12:08:51 +00:00
return;
}
entry = &set_option_table[i];
2008-06-03 21:42:37 +00:00
2008-07-19 10:07:50 +00:00
/* Bail now if an exact match. */
if (strcmp(entry->name, data->option) == 0)
break;
}
if (entry == NULL) {
ctx->error(ctx, "unknown option: %s", data->option);
return;
}
2008-06-03 21:42:37 +00:00
switch (entry->type) {
case SET_OPTION_STRING:
set_option_string(ctx, oo, entry, data->value);
break;
case SET_OPTION_NUMBER:
set_option_number(ctx, oo, entry, data->value);
break;
case SET_OPTION_KEY:
set_option_key(ctx, oo, entry, data->value);
break;
case SET_OPTION_COLOUR:
set_option_colour(ctx, oo, entry, data->value);
break;
case SET_OPTION_FLAG:
set_option_flag(ctx, oo, entry, data->value);
break;
case SET_OPTION_CHOICE:
set_option_choice(ctx, oo, entry, data->value);
break;
}
2008-06-03 21:42:37 +00:00
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);
}
2008-06-03 21:42:37 +00:00
if (ctx->cmdclient != NULL)
server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);
}
void
set_option_string(struct cmd_ctx *ctx, struct options *oo,
const struct set_option_entry *entry, char *value)
{
if (value == NULL) {
ctx->error(ctx, "empty value");
return;
}
options_set_string(oo, entry->name, "%s", value);
}
void
set_option_number(struct cmd_ctx *ctx, struct options *oo,
const struct set_option_entry *entry, char *value)
{
long long number;
const char *errstr;
if (value == NULL) {
ctx->error(ctx, "empty value");
return;
}
number = strtonum(value, entry->minimum, entry->maximum, &errstr);
if (errstr != NULL) {
ctx->error(ctx, "value is %s: %s", errstr, value);
return;
}
options_set_number(oo, entry->name, number);
}
2008-09-26 06:45:28 +00:00
void
set_option_key(struct cmd_ctx *ctx, struct options *oo,
const struct set_option_entry *entry, char *value)
{
int key;
if (value == NULL) {
ctx->error(ctx, "empty value");
return;
}
if ((key = key_string_lookup_string(value)) == KEYC_NONE) {
ctx->error(ctx, "unknown key: %s", value);
return;
}
options_set_number(oo, entry->name, key);
2008-09-26 06:45:28 +00:00
}
2008-09-26 06:45:28 +00:00
void
set_option_colour(struct cmd_ctx *ctx, struct options *oo,
const struct set_option_entry *entry, char *value)
{
u_char colour;
if (value == NULL) {
ctx->error(ctx, "empty value");
return;
}
2008-09-26 06:45:28 +00:00
2008-09-10 18:59:29 +00:00
if ((colour = colour_fromstring(value)) > 8) {
ctx->error(ctx, "bad colour: %s", value);
return;
}
2008-09-26 06:45:28 +00:00
options_set_number(oo, entry->name, colour);
}
2008-09-26 06:45:28 +00:00
void
set_option_flag(struct cmd_ctx *ctx, struct options *oo,
const struct set_option_entry *entry, char *value)
{
int flag;
2008-09-26 06:45:28 +00:00
if (value == NULL || *value == '\0')
flag = !options_get_number(oo, entry->name);
else {
if ((value[0] == '1' && value[1] == '\0') ||
strcasecmp(value, "on") == 0 ||
strcasecmp(value, "yes") == 0)
flag = 1;
else if ((value[0] == '0' && value[1] == '\0') ||
strcasecmp(value, "off") == 0 ||
strcasecmp(value, "no") == 0)
flag = 0;
2007-10-19 10:21:36 +00:00
else {
ctx->error(ctx, "bad value: %s", value);
return;
}
}
options_set_number(oo, entry->name, flag);
}
2008-09-26 06:45:28 +00:00
void
set_option_choice(struct cmd_ctx *ctx, struct options *oo,
const struct set_option_entry *entry, char *value)
{
const char **choicep;
int n, choice = -1;
if (value == NULL) {
ctx->error(ctx, "empty value");
return;
}
n = 0;
for (choicep = entry->choices; *choicep != NULL; choicep++) {
n++;
if (strncmp(*choicep, value, strlen(value)) != 0)
continue;
if (choice != -1) {
ctx->error(ctx, "ambiguous option: %s", value);
return;
}
choice = n - 1;
}
if (choice == -1) {
ctx->error(ctx, "unknown option: %s", value);
2007-10-04 10:11:32 +00:00
return;
}
options_set_number(oo, entry->name, choice);
2007-10-04 10:11:32 +00:00
}
void
cmd_set_option_send(struct cmd *self, struct buffer *b)
2007-10-04 10:11:32 +00:00
{
struct cmd_set_option_data *data = self->data;
2007-10-04 10:11:32 +00:00
2008-06-03 21:42:37 +00:00
buffer_write(b, data, sizeof *data);
cmd_send_string(b, data->target);
2007-10-04 10:11:32 +00:00
cmd_send_string(b, data->option);
cmd_send_string(b, data->value);
}
void
cmd_set_option_recv(struct cmd *self, struct buffer *b)
2007-10-04 10:11:32 +00:00
{
struct cmd_set_option_data *data;
self->data = data = xmalloc(sizeof *data);
2008-06-03 21:42:37 +00:00
buffer_read(b, data, sizeof *data);
data->target = cmd_recv_string(b);
2007-10-04 10:11:32 +00:00
data->option = cmd_recv_string(b);
data->value = cmd_recv_string(b);
}
void
cmd_set_option_free(struct cmd *self)
2007-10-04 10:11:32 +00:00
{
struct cmd_set_option_data *data = self->data;
2007-10-04 10:11:32 +00:00
if (data->target != NULL)
xfree(data->target);
2007-10-04 10:11:32 +00:00
if (data->option != NULL)
xfree(data->option);
if (data->value != NULL)
xfree(data->value);
xfree(data);
}
2008-06-05 17:12:11 +00:00
void
cmd_set_option_print(struct cmd *self, char *buf, size_t len)
{
struct cmd_set_option_data *data = self->data;
size_t off = 0;
off += xsnprintf(buf, len, "%s", self->entry->name);
if (data == NULL)
return;
if (off < len && data->option != NULL)
off += xsnprintf(buf + off, len - off, " %s", data->option);
if (off < len && data->value != NULL)
2008-06-18 22:21:51 +00:00
off += xsnprintf(buf + off, len - off, " %s", data->value);
2008-06-05 17:12:11 +00:00
}