version 1.100, 2016/10/14 22:14:22 |
version 1.101, 2016/10/16 19:04:05 |
|
|
* Set an option. |
* Set an option. |
*/ |
*/ |
|
|
static enum cmd_retval cmd_set_option_exec(struct cmd *, struct cmd_q *); |
static enum cmd_retval cmd_set_option_exec(struct cmd *, struct cmdq_item *); |
|
|
static enum cmd_retval cmd_set_option_user(struct cmd *, struct cmd_q *, |
static enum cmd_retval cmd_set_option_user(struct cmd *, struct cmdq_item *, |
const char *, const char *); |
const char *, const char *); |
|
|
static int cmd_set_option_unset(struct cmd *, struct cmd_q *, |
static int cmd_set_option_unset(struct cmd *, struct cmdq_item *, |
const struct options_table_entry *, struct options *, |
const struct options_table_entry *, struct options *, |
const char *); |
const char *); |
static int cmd_set_option_set(struct cmd *, struct cmd_q *, |
static int cmd_set_option_set(struct cmd *, struct cmdq_item *, |
const struct options_table_entry *, struct options *, |
const struct options_table_entry *, struct options *, |
const char *); |
const char *); |
|
|
static struct options_entry *cmd_set_option_string(struct cmd *, struct cmd_q *, |
static struct options_entry *cmd_set_option_string(struct cmd *, |
const struct options_table_entry *, struct options *, |
struct cmdq_item *, const struct options_table_entry *, |
const char *); |
struct options *, const char *); |
static struct options_entry *cmd_set_option_number(struct cmd *, struct cmd_q *, |
static struct options_entry *cmd_set_option_number(struct cmd *, |
const struct options_table_entry *, struct options *, |
struct cmdq_item *, const struct options_table_entry *, |
const char *); |
struct options *, const char *); |
static struct options_entry *cmd_set_option_key(struct cmd *, struct cmd_q *, |
static struct options_entry *cmd_set_option_key(struct cmd *, |
const struct options_table_entry *, struct options *, |
struct cmdq_item *, const struct options_table_entry *, |
const char *); |
struct options *, const char *); |
static struct options_entry *cmd_set_option_colour(struct cmd *, struct cmd_q *, |
static struct options_entry *cmd_set_option_colour(struct cmd *, |
const struct options_table_entry *, struct options *, |
struct cmdq_item *, const struct options_table_entry *, |
const char *); |
struct options *, const char *); |
static struct options_entry *cmd_set_option_attributes(struct cmd *, |
static struct options_entry *cmd_set_option_attributes(struct cmd *, |
struct cmd_q *, const struct options_table_entry *, |
struct cmdq_item *, const struct options_table_entry *, |
struct options *, const char *); |
struct options *, const char *); |
static struct options_entry *cmd_set_option_flag(struct cmd *, struct cmd_q *, |
static struct options_entry *cmd_set_option_flag(struct cmd *, |
const struct options_table_entry *, struct options *, |
struct cmdq_item *, const struct options_table_entry *, |
const char *); |
struct options *, const char *); |
static struct options_entry *cmd_set_option_choice(struct cmd *, struct cmd_q *, |
static struct options_entry *cmd_set_option_choice(struct cmd *, |
const struct options_table_entry *, struct options *, |
struct cmdq_item *, const struct options_table_entry *, |
const char *); |
struct options *, const char *); |
static struct options_entry *cmd_set_option_style(struct cmd *, struct cmd_q *, |
static struct options_entry *cmd_set_option_style(struct cmd *, |
const struct options_table_entry *, struct options *, |
struct cmdq_item *, const struct options_table_entry *, |
const char *); |
struct options *, const char *); |
|
|
const struct cmd_entry cmd_set_option_entry = { |
const struct cmd_entry cmd_set_option_entry = { |
.name = "set-option", |
.name = "set-option", |
|
|
}; |
}; |
|
|
static enum cmd_retval |
static enum cmd_retval |
cmd_set_option_exec(struct cmd *self, struct cmd_q *cmdq) |
cmd_set_option_exec(struct cmd *self, struct cmdq_item *item) |
{ |
{ |
struct args *args = self->args; |
struct args *args = self->args; |
struct session *s = cmdq->state.tflag.s; |
struct session *s = item->state.tflag.s; |
struct winlink *wl = cmdq->state.tflag.wl; |
struct winlink *wl = item->state.tflag.wl; |
struct window *w; |
struct window *w; |
struct client *c; |
struct client *c; |
const struct options_table_entry *oe; |
const struct options_table_entry *oe; |
|
|
/* Get the option name and value. */ |
/* Get the option name and value. */ |
optstr = args->argv[0]; |
optstr = args->argv[0]; |
if (*optstr == '\0') { |
if (*optstr == '\0') { |
cmdq_error(cmdq, "invalid option"); |
cmdq_error(item, "invalid option"); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
if (args->argc < 2) |
if (args->argc < 2) |
|
|
|
|
/* Is this a user option? */ |
/* Is this a user option? */ |
if (*optstr == '@') |
if (*optstr == '@') |
return (cmd_set_option_user(self, cmdq, optstr, valstr)); |
return (cmd_set_option_user(self, item, optstr, valstr)); |
|
|
/* Find the option entry, try each table. */ |
/* Find the option entry, try each table. */ |
oe = NULL; |
oe = NULL; |
if (options_table_find(optstr, &oe) != 0) { |
if (options_table_find(optstr, &oe) != 0) { |
if (!args_has(args, 'q')) { |
if (!args_has(args, 'q')) { |
cmdq_error(cmdq, "ambiguous option: %s", optstr); |
cmdq_error(item, "ambiguous option: %s", optstr); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
return (CMD_RETURN_NORMAL); |
return (CMD_RETURN_NORMAL); |
} |
} |
if (oe == NULL) { |
if (oe == NULL) { |
if (!args_has(args, 'q')) { |
if (!args_has(args, 'q')) { |
cmdq_error(cmdq, "unknown option: %s", optstr); |
cmdq_error(item, "unknown option: %s", optstr); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
return (CMD_RETURN_NORMAL); |
return (CMD_RETURN_NORMAL); |
|
|
else if (wl == NULL) { |
else if (wl == NULL) { |
target = args_get(args, 't'); |
target = args_get(args, 't'); |
if (target != NULL) { |
if (target != NULL) { |
cmdq_error(cmdq, "no such window: %s", |
cmdq_error(item, "no such window: %s", |
target); |
target); |
} else |
} else |
cmdq_error(cmdq, "no current window"); |
cmdq_error(item, "no current window"); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} else |
} else |
oo = wl->window->options; |
oo = wl->window->options; |
|
|
else if (s == NULL) { |
else if (s == NULL) { |
target = args_get(args, 't'); |
target = args_get(args, 't'); |
if (target != NULL) { |
if (target != NULL) { |
cmdq_error(cmdq, "no such session: %s", |
cmdq_error(item, "no such session: %s", |
target); |
target); |
} else |
} else |
cmdq_error(cmdq, "no current session"); |
cmdq_error(item, "no current session"); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} else |
} else |
oo = s->options; |
oo = s->options; |
} else { |
} else { |
cmdq_error(cmdq, "unknown table"); |
cmdq_error(item, "unknown table"); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
|
|
/* Unset or set the option. */ |
/* Unset or set the option. */ |
if (args_has(args, 'u')) { |
if (args_has(args, 'u')) { |
if (cmd_set_option_unset(self, cmdq, oe, oo, valstr) != 0) |
if (cmd_set_option_unset(self, item, oe, oo, valstr) != 0) |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} else { |
} else { |
if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) { |
if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) { |
if (!args_has(args, 'q')) { |
if (!args_has(args, 'q')) { |
cmdq_error(cmdq, "already set: %s", optstr); |
cmdq_error(item, "already set: %s", optstr); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
return (CMD_RETURN_NORMAL); |
return (CMD_RETURN_NORMAL); |
} |
} |
if (cmd_set_option_set(self, cmdq, oe, oo, valstr) != 0) |
if (cmd_set_option_set(self, item, oe, oo, valstr) != 0) |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
|
|
|
|
|
|
/* Set user option. */ |
/* Set user option. */ |
static enum cmd_retval |
static enum cmd_retval |
cmd_set_option_user(struct cmd *self, struct cmd_q *cmdq, const char *optstr, |
cmd_set_option_user(struct cmd *self, struct cmdq_item *item, |
const char *valstr) |
const char *optstr, const char *valstr) |
{ |
{ |
struct args *args = self->args; |
struct args *args = self->args; |
struct session *s = cmdq->state.tflag.s; |
struct session *s = item->state.tflag.s; |
struct winlink *wl = cmdq->state.tflag.wl; |
struct winlink *wl = item->state.tflag.wl; |
struct options *oo; |
struct options *oo; |
struct options_entry *o; |
struct options_entry *o; |
const char *target; |
const char *target; |
|
|
else if (wl == NULL) { |
else if (wl == NULL) { |
target = args_get(args, 't'); |
target = args_get(args, 't'); |
if (target != NULL) { |
if (target != NULL) { |
cmdq_error(cmdq, "no such window: %s", |
cmdq_error(item, "no such window: %s", |
target); |
target); |
} else |
} else |
cmdq_error(cmdq, "no current window"); |
cmdq_error(item, "no current window"); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} else |
} else |
oo = wl->window->options; |
oo = wl->window->options; |
|
|
else if (s == NULL) { |
else if (s == NULL) { |
target = args_get(args, 't'); |
target = args_get(args, 't'); |
if (target != NULL) { |
if (target != NULL) { |
cmdq_error(cmdq, "no such session: %s", |
cmdq_error(item, "no such session: %s", |
target); |
target); |
} else |
} else |
cmdq_error(cmdq, "no current session"); |
cmdq_error(item, "no current session"); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} else |
} else |
oo = s->options; |
oo = s->options; |
|
|
if (args_has(args, 'u')) { |
if (args_has(args, 'u')) { |
if (options_find1(oo, optstr) == NULL) { |
if (options_find1(oo, optstr) == NULL) { |
if (!args_has(args, 'q')) { |
if (!args_has(args, 'q')) { |
cmdq_error(cmdq, "unknown option: %s", optstr); |
cmdq_error(item, "unknown option: %s", optstr); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
return (CMD_RETURN_NORMAL); |
return (CMD_RETURN_NORMAL); |
} |
} |
if (valstr != NULL) { |
if (valstr != NULL) { |
cmdq_error(cmdq, "value passed to unset option: %s", |
cmdq_error(item, "value passed to unset option: %s", |
optstr); |
optstr); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
|
|
o = options_find1(oo, optstr); |
o = options_find1(oo, optstr); |
if (args_has(args, 'o') && o != NULL) { |
if (args_has(args, 'o') && o != NULL) { |
if (!args_has(args, 'q')) { |
if (!args_has(args, 'q')) { |
cmdq_error(cmdq, "already set: %s", optstr); |
cmdq_error(item, "already set: %s", optstr); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
return (CMD_RETURN_NORMAL); |
return (CMD_RETURN_NORMAL); |
} |
} |
if (valstr == NULL) { |
if (valstr == NULL) { |
cmdq_error(cmdq, "empty value"); |
cmdq_error(item, "empty value"); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
if (o != NULL && args_has(args, 'a')) |
if (o != NULL && args_has(args, 'a')) |
|
|
|
|
/* Unset an option. */ |
/* Unset an option. */ |
static int |
static int |
cmd_set_option_unset(struct cmd *self, struct cmd_q *cmdq, |
cmd_set_option_unset(struct cmd *self, struct cmdq_item *item, |
const struct options_table_entry *oe, struct options *oo, |
const struct options_table_entry *oe, struct options *oo, |
const char *value) |
const char *value) |
{ |
{ |
struct args *args = self->args; |
struct args *args = self->args; |
|
|
if (value != NULL) { |
if (value != NULL) { |
cmdq_error(cmdq, "value passed to unset option: %s", oe->name); |
cmdq_error(item, "value passed to unset option: %s", oe->name); |
return (-1); |
return (-1); |
} |
} |
|
|
|
|
|
|
/* Set an option. */ |
/* Set an option. */ |
static int |
static int |
cmd_set_option_set(struct cmd *self, struct cmd_q *cmdq, |
cmd_set_option_set(struct cmd *self, struct cmdq_item *item, |
const struct options_table_entry *oe, struct options *oo, |
const struct options_table_entry *oe, struct options *oo, |
const char *value) |
const char *value) |
{ |
{ |
|
|
break; |
break; |
default: |
default: |
if (value == NULL) { |
if (value == NULL) { |
cmdq_error(cmdq, "empty value"); |
cmdq_error(item, "empty value"); |
return (-1); |
return (-1); |
} |
} |
} |
} |
|
|
o = NULL; |
o = NULL; |
switch (oe->type) { |
switch (oe->type) { |
case OPTIONS_TABLE_STRING: |
case OPTIONS_TABLE_STRING: |
o = cmd_set_option_string(self, cmdq, oe, oo, value); |
o = cmd_set_option_string(self, item, oe, oo, value); |
break; |
break; |
case OPTIONS_TABLE_NUMBER: |
case OPTIONS_TABLE_NUMBER: |
o = cmd_set_option_number(self, cmdq, oe, oo, value); |
o = cmd_set_option_number(self, item, oe, oo, value); |
break; |
break; |
case OPTIONS_TABLE_KEY: |
case OPTIONS_TABLE_KEY: |
o = cmd_set_option_key(self, cmdq, oe, oo, value); |
o = cmd_set_option_key(self, item, oe, oo, value); |
break; |
break; |
case OPTIONS_TABLE_COLOUR: |
case OPTIONS_TABLE_COLOUR: |
o = cmd_set_option_colour(self, cmdq, oe, oo, value); |
o = cmd_set_option_colour(self, item, oe, oo, value); |
if (o != NULL) |
if (o != NULL) |
style_update_new(oo, o->name, oe->style); |
style_update_new(oo, o->name, oe->style); |
break; |
break; |
case OPTIONS_TABLE_ATTRIBUTES: |
case OPTIONS_TABLE_ATTRIBUTES: |
o = cmd_set_option_attributes(self, cmdq, oe, oo, value); |
o = cmd_set_option_attributes(self, item, oe, oo, value); |
if (o != NULL) |
if (o != NULL) |
style_update_new(oo, o->name, oe->style); |
style_update_new(oo, o->name, oe->style); |
break; |
break; |
case OPTIONS_TABLE_FLAG: |
case OPTIONS_TABLE_FLAG: |
o = cmd_set_option_flag(self, cmdq, oe, oo, value); |
o = cmd_set_option_flag(self, item, oe, oo, value); |
break; |
break; |
case OPTIONS_TABLE_CHOICE: |
case OPTIONS_TABLE_CHOICE: |
o = cmd_set_option_choice(self, cmdq, oe, oo, value); |
o = cmd_set_option_choice(self, item, oe, oo, value); |
break; |
break; |
case OPTIONS_TABLE_STYLE: |
case OPTIONS_TABLE_STYLE: |
o = cmd_set_option_style(self, cmdq, oe, oo, value); |
o = cmd_set_option_style(self, item, oe, oo, value); |
break; |
break; |
} |
} |
if (o == NULL) |
if (o == NULL) |
|
|
|
|
/* Set a string option. */ |
/* Set a string option. */ |
static struct options_entry * |
static struct options_entry * |
cmd_set_option_string(struct cmd *self, __unused struct cmd_q *cmdq, |
cmd_set_option_string(struct cmd *self, __unused struct cmdq_item *item, |
const struct options_table_entry *oe, struct options *oo, |
const struct options_table_entry *oe, struct options *oo, |
const char *value) |
const char *value) |
{ |
{ |
|
|
|
|
/* Set a number option. */ |
/* Set a number option. */ |
static struct options_entry * |
static struct options_entry * |
cmd_set_option_number(__unused struct cmd *self, struct cmd_q *cmdq, |
cmd_set_option_number(__unused struct cmd *self, struct cmdq_item *item, |
const struct options_table_entry *oe, struct options *oo, |
const struct options_table_entry *oe, struct options *oo, |
const char *value) |
const char *value) |
{ |
{ |
|
|
|
|
ll = strtonum(value, oe->minimum, oe->maximum, &errstr); |
ll = strtonum(value, oe->minimum, oe->maximum, &errstr); |
if (errstr != NULL) { |
if (errstr != NULL) { |
cmdq_error(cmdq, "value is %s: %s", errstr, value); |
cmdq_error(item, "value is %s: %s", errstr, value); |
return (NULL); |
return (NULL); |
} |
} |
|
|
|
|
|
|
/* Set a key option. */ |
/* Set a key option. */ |
static struct options_entry * |
static struct options_entry * |
cmd_set_option_key(__unused struct cmd *self, struct cmd_q *cmdq, |
cmd_set_option_key(__unused struct cmd *self, struct cmdq_item *item, |
const struct options_table_entry *oe, struct options *oo, |
const struct options_table_entry *oe, struct options *oo, |
const char *value) |
const char *value) |
{ |
{ |
|
|
|
|
key = key_string_lookup_string(value); |
key = key_string_lookup_string(value); |
if (key == KEYC_UNKNOWN) { |
if (key == KEYC_UNKNOWN) { |
cmdq_error(cmdq, "bad key: %s", value); |
cmdq_error(item, "bad key: %s", value); |
return (NULL); |
return (NULL); |
} |
} |
|
|
|
|
|
|
/* Set a colour option. */ |
/* Set a colour option. */ |
static struct options_entry * |
static struct options_entry * |
cmd_set_option_colour(__unused struct cmd *self, struct cmd_q *cmdq, |
cmd_set_option_colour(__unused struct cmd *self, struct cmdq_item *item, |
const struct options_table_entry *oe, struct options *oo, |
const struct options_table_entry *oe, struct options *oo, |
const char *value) |
const char *value) |
{ |
{ |
int colour; |
int colour; |
|
|
if ((colour = colour_fromstring(value)) == -1) { |
if ((colour = colour_fromstring(value)) == -1) { |
cmdq_error(cmdq, "bad colour: %s", value); |
cmdq_error(item, "bad colour: %s", value); |
return (NULL); |
return (NULL); |
} |
} |
|
|
|
|
|
|
/* Set an attributes option. */ |
/* Set an attributes option. */ |
static struct options_entry * |
static struct options_entry * |
cmd_set_option_attributes(__unused struct cmd *self, struct cmd_q *cmdq, |
cmd_set_option_attributes(__unused struct cmd *self, struct cmdq_item *item, |
const struct options_table_entry *oe, struct options *oo, |
const struct options_table_entry *oe, struct options *oo, |
const char *value) |
const char *value) |
{ |
{ |
int attr; |
int attr; |
|
|
if ((attr = attributes_fromstring(value)) == -1) { |
if ((attr = attributes_fromstring(value)) == -1) { |
cmdq_error(cmdq, "bad attributes: %s", value); |
cmdq_error(item, "bad attributes: %s", value); |
return (NULL); |
return (NULL); |
} |
} |
|
|
|
|
|
|
/* Set a flag option. */ |
/* Set a flag option. */ |
static struct options_entry * |
static struct options_entry * |
cmd_set_option_flag(__unused struct cmd *self, struct cmd_q *cmdq, |
cmd_set_option_flag(__unused struct cmd *self, struct cmdq_item *item, |
const struct options_table_entry *oe, struct options *oo, |
const struct options_table_entry *oe, struct options *oo, |
const char *value) |
const char *value) |
{ |
{ |
|
|
strcasecmp(value, "no") == 0) |
strcasecmp(value, "no") == 0) |
flag = 0; |
flag = 0; |
else { |
else { |
cmdq_error(cmdq, "bad value: %s", value); |
cmdq_error(item, "bad value: %s", value); |
return (NULL); |
return (NULL); |
} |
} |
} |
} |
|
|
|
|
/* Set a choice option. */ |
/* Set a choice option. */ |
static struct options_entry * |
static struct options_entry * |
cmd_set_option_choice(__unused struct cmd *self, struct cmd_q *cmdq, |
cmd_set_option_choice(__unused struct cmd *self, struct cmdq_item *item, |
const struct options_table_entry *oe, struct options *oo, |
const struct options_table_entry *oe, struct options *oo, |
const char *value) |
const char *value) |
{ |
{ |
|
|
continue; |
continue; |
|
|
if (choice != -1) { |
if (choice != -1) { |
cmdq_error(cmdq, "ambiguous value: %s", value); |
cmdq_error(item, "ambiguous value: %s", value); |
return (NULL); |
return (NULL); |
} |
} |
choice = n - 1; |
choice = n - 1; |
} |
} |
if (choice == -1) { |
if (choice == -1) { |
cmdq_error(cmdq, "unknown value: %s", value); |
cmdq_error(item, "unknown value: %s", value); |
return (NULL); |
return (NULL); |
} |
} |
} |
} |
|
|
|
|
/* Set a style option. */ |
/* Set a style option. */ |
static struct options_entry * |
static struct options_entry * |
cmd_set_option_style(struct cmd *self, struct cmd_q *cmdq, |
cmd_set_option_style(struct cmd *self, struct cmdq_item *item, |
const struct options_table_entry *oe, struct options *oo, |
const struct options_table_entry *oe, struct options *oo, |
const char *value) |
const char *value) |
{ |
{ |
|
|
|
|
append = args_has(args, 'a'); |
append = args_has(args, 'a'); |
if ((o = options_set_style(oo, oe->name, value, append)) == NULL) { |
if ((o = options_set_style(oo, oe->name, value, append)) == NULL) { |
cmdq_error(cmdq, "bad style: %s", value); |
cmdq_error(item, "bad style: %s", value); |
return (NULL); |
return (NULL); |
} |
} |
|
|