2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set an option.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_set_option_exec(struct cmd *, struct cmdq_item *);
|
2013-03-21 16:17:01 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
const struct cmd_entry cmd_set_option_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "set-option",
|
|
|
|
.alias = "set",
|
|
|
|
|
2020-12-15 08:31:50 +00:00
|
|
|
.args = { "aFgopqst:uUw", 1, 2 },
|
|
|
|
.usage = "[-aFgopqsuUw] " CMD_TARGET_PANE_USAGE " option [value]",
|
2015-12-13 21:53:57 +00:00
|
|
|
|
2019-06-20 11:59:59 +00:00
|
|
|
.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
2016-10-14 22:14:22 +00:00
|
|
|
.flags = CMD_AFTERHOOK,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_set_option_exec
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2011-01-04 02:03:41 +00:00
|
|
|
const struct cmd_entry cmd_set_window_option_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "set-window-option",
|
|
|
|
.alias = "setw",
|
|
|
|
|
2017-05-10 13:05:41 +00:00
|
|
|
.args = { "aFgoqt:u", 1, 2 },
|
|
|
|
.usage = "[-aFgoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
|
2015-12-13 21:53:57 +00:00
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
2016-10-14 22:14:22 +00:00
|
|
|
.flags = CMD_AFTERHOOK,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_set_option_exec
|
2011-01-04 02:03:41 +00:00
|
|
|
};
|
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
const struct cmd_entry cmd_set_hook_entry = {
|
|
|
|
.name = "set-hook",
|
|
|
|
.alias = NULL,
|
|
|
|
|
2020-04-13 07:25:33 +00:00
|
|
|
.args = { "agpRt:uw", 1, 2 },
|
|
|
|
.usage = "[-agpRuw] " CMD_TARGET_PANE_USAGE " hook [command]",
|
2019-04-26 11:38:51 +00:00
|
|
|
|
2020-04-13 07:25:33 +00:00
|
|
|
.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
|
2019-04-26 11:38:51 +00:00
|
|
|
|
|
|
|
.flags = CMD_AFTERHOOK,
|
|
|
|
.exec = cmd_set_option_exec
|
|
|
|
};
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2017-01-24 20:24:54 +00:00
|
|
|
int append = args_has(args, 'a');
|
2020-04-13 10:59:58 +00:00
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
2020-12-15 08:31:50 +00:00
|
|
|
struct window_pane *loop;
|
2017-01-15 20:48:41 +00:00
|
|
|
struct options *oo;
|
2020-12-15 08:31:50 +00:00
|
|
|
struct options_entry *parent, *o, *po;
|
2021-08-20 19:50:16 +00:00
|
|
|
char *name, *argument, *expanded = NULL;
|
|
|
|
char *cause;
|
|
|
|
const char *value;
|
2017-01-15 20:48:41 +00:00
|
|
|
int window, idx, already, error, ambiguous;
|
2019-06-20 11:59:59 +00:00
|
|
|
int scope;
|
2017-05-10 13:05:41 +00:00
|
|
|
|
2020-04-13 08:26:27 +00:00
|
|
|
window = (cmd_get_entry(self) == &cmd_set_window_option_entry);
|
2019-06-20 07:41:29 +00:00
|
|
|
|
2017-05-10 13:05:41 +00:00
|
|
|
/* Expand argument. */
|
2021-08-20 19:50:16 +00:00
|
|
|
argument = format_single_from_target(item, args_string(args, 0));
|
2017-01-15 20:48:41 +00:00
|
|
|
|
2019-06-20 07:41:29 +00:00
|
|
|
/* If set-hook -R, fire the hook straight away. */
|
2020-04-13 08:26:27 +00:00
|
|
|
if (cmd_get_entry(self) == &cmd_set_hook_entry && args_has(args, 'R')) {
|
2019-04-26 11:38:51 +00:00
|
|
|
notify_hook(item, argument);
|
2019-06-20 07:41:29 +00:00
|
|
|
free(argument);
|
2019-04-26 11:38:51 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
/* Parse option name and index. */
|
2017-05-10 13:05:41 +00:00
|
|
|
name = options_match(argument, &idx, &ambiguous);
|
2017-01-15 20:48:41 +00:00
|
|
|
if (name == NULL) {
|
2017-01-15 20:50:34 +00:00
|
|
|
if (args_has(args, 'q'))
|
2017-05-10 13:05:41 +00:00
|
|
|
goto out;
|
2017-01-15 20:48:41 +00:00
|
|
|
if (ambiguous)
|
2017-05-10 13:05:41 +00:00
|
|
|
cmdq_error(item, "ambiguous option: %s", argument);
|
2017-01-15 20:48:41 +00:00
|
|
|
else
|
2017-05-10 13:05:41 +00:00
|
|
|
cmdq_error(item, "invalid option: %s", argument);
|
|
|
|
goto fail;
|
2011-03-29 21:09:13 +00:00
|
|
|
}
|
2021-08-20 19:50:16 +00:00
|
|
|
if (args_count(args) < 2)
|
2017-01-15 20:48:41 +00:00
|
|
|
value = NULL;
|
2011-03-29 21:09:13 +00:00
|
|
|
else
|
2021-08-20 19:50:16 +00:00
|
|
|
value = args_string(args, 1);
|
|
|
|
if (value != NULL && args_has(args, 'F')) {
|
|
|
|
expanded = format_single_from_target(item, value);
|
|
|
|
value = expanded;
|
|
|
|
}
|
2017-01-15 20:48:41 +00:00
|
|
|
|
2019-06-20 07:41:29 +00:00
|
|
|
/* Get the scope and table for the option .*/
|
2020-04-13 10:59:58 +00:00
|
|
|
scope = options_scope_from_name(args, window, name, target, &oo,
|
|
|
|
&cause);
|
2017-01-15 20:48:41 +00:00
|
|
|
if (scope == OPTIONS_TABLE_NONE) {
|
2017-01-15 20:50:34 +00:00
|
|
|
if (args_has(args, 'q'))
|
2017-05-10 13:05:41 +00:00
|
|
|
goto out;
|
2017-01-15 20:48:41 +00:00
|
|
|
cmdq_error(item, "%s", cause);
|
|
|
|
free(cause);
|
2017-04-22 06:13:30 +00:00
|
|
|
goto fail;
|
2011-03-29 21:09:13 +00:00
|
|
|
}
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_get_only(oo, name);
|
|
|
|
parent = options_get(oo, name);
|
|
|
|
|
|
|
|
/* Check that array options and indexes match up. */
|
2020-05-16 16:02:24 +00:00
|
|
|
if (idx != -1 && (*name == '@' || !options_is_array(parent))) {
|
2019-03-18 11:58:40 +00:00
|
|
|
cmdq_error(item, "not an array: %s", argument);
|
|
|
|
goto fail;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-15 20:48:41 +00:00
|
|
|
/* With -o, check this option is not already set. */
|
|
|
|
if (!args_has(args, 'u') && args_has(args, 'o')) {
|
|
|
|
if (idx == -1)
|
|
|
|
already = (o != NULL);
|
|
|
|
else {
|
|
|
|
if (o == NULL)
|
|
|
|
already = 0;
|
|
|
|
else
|
|
|
|
already = (options_array_get(o, idx) != NULL);
|
|
|
|
}
|
|
|
|
if (already) {
|
|
|
|
if (args_has(args, 'q'))
|
2017-05-10 13:05:41 +00:00
|
|
|
goto out;
|
|
|
|
cmdq_error(item, "already set: %s", argument);
|
2017-04-22 06:13:30 +00:00
|
|
|
goto fail;
|
2017-01-15 20:48:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change the option. */
|
2020-12-15 08:31:50 +00:00
|
|
|
if (args_has(args, 'U') && scope == OPTIONS_TABLE_WINDOW) {
|
|
|
|
TAILQ_FOREACH(loop, &target->w->panes, entry) {
|
|
|
|
po = options_get_only(loop->options, name);
|
|
|
|
if (po == NULL)
|
|
|
|
continue;
|
|
|
|
if (options_remove_or_default(po, idx, &cause) != 0) {
|
|
|
|
cmdq_error(item, "%s", cause);
|
|
|
|
free(cause);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (args_has(args, 'u') || args_has(args, 'U')) {
|
2017-01-15 20:48:41 +00:00
|
|
|
if (o == NULL)
|
2017-09-07 13:18:44 +00:00
|
|
|
goto out;
|
2020-06-16 08:18:34 +00:00
|
|
|
if (options_remove_or_default(o, idx, &cause) != 0) {
|
2019-04-26 11:38:51 +00:00
|
|
|
cmdq_error(item, "%s", cause);
|
|
|
|
free(cause);
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-01-24 20:24:54 +00:00
|
|
|
} else if (*name == '@') {
|
|
|
|
if (value == NULL) {
|
|
|
|
cmdq_error(item, "empty value");
|
2017-04-22 06:13:30 +00:00
|
|
|
goto fail;
|
2017-01-24 20:24:54 +00:00
|
|
|
}
|
|
|
|
options_set_string(oo, name, append, "%s", value);
|
2020-05-16 16:02:24 +00:00
|
|
|
} else if (idx == -1 && !options_is_array(parent)) {
|
|
|
|
error = options_from_string(oo, options_table_entry(parent),
|
|
|
|
options_table_entry(parent)->name, value,
|
|
|
|
args_has(args, 'a'), &cause);
|
|
|
|
if (error != 0) {
|
|
|
|
cmdq_error(item, "%s", cause);
|
|
|
|
free(cause);
|
2017-04-22 06:13:30 +00:00
|
|
|
goto fail;
|
2020-05-16 16:02:24 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
} else {
|
2017-01-24 20:24:54 +00:00
|
|
|
if (value == NULL) {
|
|
|
|
cmdq_error(item, "empty value");
|
2017-04-22 06:13:30 +00:00
|
|
|
goto fail;
|
2017-01-24 20:24:54 +00:00
|
|
|
}
|
2017-01-15 20:48:41 +00:00
|
|
|
if (o == NULL)
|
|
|
|
o = options_empty(oo, options_table_entry(parent));
|
2017-01-25 23:50:51 +00:00
|
|
|
if (idx == -1) {
|
|
|
|
if (!append)
|
|
|
|
options_array_clear(o);
|
2019-04-26 11:38:51 +00:00
|
|
|
if (options_array_assign(o, value, &cause) != 0) {
|
|
|
|
cmdq_error(item, "%s", cause);
|
|
|
|
free(cause);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else if (options_array_set(o, idx, value, append,
|
|
|
|
&cause) != 0) {
|
|
|
|
cmdq_error(item, "%s", cause);
|
|
|
|
free(cause);
|
2017-04-22 06:13:30 +00:00
|
|
|
goto fail;
|
2017-01-15 20:48:41 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 16:02:24 +00:00
|
|
|
options_push_changes(name);
|
2009-11-01 23:20:37 +00:00
|
|
|
|
2017-05-10 13:05:41 +00:00
|
|
|
out:
|
|
|
|
free(argument);
|
2021-08-20 19:50:16 +00:00
|
|
|
free(expanded);
|
2017-04-22 06:13:30 +00:00
|
|
|
free(name);
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2017-04-22 06:13:30 +00:00
|
|
|
|
|
|
|
fail:
|
2017-05-10 13:05:41 +00:00
|
|
|
free(argument);
|
2021-08-20 19:50:16 +00:00
|
|
|
free(expanded);
|
2017-04-22 06:13:30 +00:00
|
|
|
free(name);
|
|
|
|
return (CMD_RETURN_ERROR);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|