15494e770Schristos /* $OpenBSD$ */
2698d5317Sjmmv
3698d5317Sjmmv /*
4ed4e6cd4Schristos * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5698d5317Sjmmv *
6698d5317Sjmmv * Permission to use, copy, modify, and distribute this software for any
7698d5317Sjmmv * purpose with or without fee is hereby granted, provided that the above
8698d5317Sjmmv * copyright notice and this permission notice appear in all copies.
9698d5317Sjmmv *
10698d5317Sjmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11698d5317Sjmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12698d5317Sjmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13698d5317Sjmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14698d5317Sjmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15698d5317Sjmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16698d5317Sjmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17698d5317Sjmmv */
18698d5317Sjmmv
19698d5317Sjmmv #include <sys/types.h>
20698d5317Sjmmv
21698d5317Sjmmv #include <stdlib.h>
22698d5317Sjmmv #include <string.h>
23698d5317Sjmmv
24698d5317Sjmmv #include "tmux.h"
25698d5317Sjmmv
26698d5317Sjmmv /*
27698d5317Sjmmv * Set an option.
28698d5317Sjmmv */
29698d5317Sjmmv
30*6db26757Swiz static enum args_parse_type cmd_set_option_args_parse(struct args *,
31*6db26757Swiz u_int, char **);
32*6db26757Swiz static enum cmd_retval cmd_set_option_exec(struct cmd *,
33*6db26757Swiz struct cmdq_item *);
34698d5317Sjmmv
35698d5317Sjmmv const struct cmd_entry cmd_set_option_entry = {
36ed4e6cd4Schristos .name = "set-option",
37ed4e6cd4Schristos .alias = "set",
38ed4e6cd4Schristos
39*6db26757Swiz .args = { "aFgopqst:uUw", 1, 2, cmd_set_option_args_parse },
409fb66d81Schristos .usage = "[-aFgopqsuUw] " CMD_TARGET_PANE_USAGE " option [value]",
41ed4e6cd4Schristos
426483eba0Schristos .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
43ed4e6cd4Schristos
444e179ddaSchristos .flags = CMD_AFTERHOOK,
45ed4e6cd4Schristos .exec = cmd_set_option_exec
46698d5317Sjmmv };
47698d5317Sjmmv
48d530c4d0Sjmmv const struct cmd_entry cmd_set_window_option_entry = {
49ed4e6cd4Schristos .name = "set-window-option",
50ed4e6cd4Schristos .alias = "setw",
51ed4e6cd4Schristos
52*6db26757Swiz .args = { "aFgoqt:u", 1, 2, cmd_set_option_args_parse },
53c9ad075bSchristos .usage = "[-aFgoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
54ed4e6cd4Schristos
55c9ad075bSchristos .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
56ed4e6cd4Schristos
574e179ddaSchristos .flags = CMD_AFTERHOOK,
58ed4e6cd4Schristos .exec = cmd_set_option_exec
59698d5317Sjmmv };
60698d5317Sjmmv
616483eba0Schristos const struct cmd_entry cmd_set_hook_entry = {
626483eba0Schristos .name = "set-hook",
636483eba0Schristos .alias = NULL,
646483eba0Schristos
65*6db26757Swiz .args = { "agpRt:uw", 1, 2, cmd_set_option_args_parse },
669fb66d81Schristos .usage = "[-agpRuw] " CMD_TARGET_PANE_USAGE " hook [command]",
676483eba0Schristos
689fb66d81Schristos .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
696483eba0Schristos
706483eba0Schristos .flags = CMD_AFTERHOOK,
716483eba0Schristos .exec = cmd_set_option_exec
726483eba0Schristos };
736483eba0Schristos
74*6db26757Swiz static enum args_parse_type
cmd_set_option_args_parse(__unused struct args * args,u_int idx,__unused char ** cause)75*6db26757Swiz cmd_set_option_args_parse(__unused struct args *args, u_int idx,
76*6db26757Swiz __unused char **cause)
77*6db26757Swiz {
78*6db26757Swiz if (idx == 1)
79*6db26757Swiz return (ARGS_PARSE_COMMANDS_OR_STRING);
80*6db26757Swiz return (ARGS_PARSE_STRING);
81*6db26757Swiz }
82*6db26757Swiz
834e179ddaSchristos static enum cmd_retval
cmd_set_option_exec(struct cmd * self,struct cmdq_item * item)844e179ddaSchristos cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
85698d5317Sjmmv {
869fb66d81Schristos struct args *args = cmd_get_args(self);
874e179ddaSchristos int append = args_has(args, 'a');
889fb66d81Schristos struct cmd_find_state *target = cmdq_get_target(item);
899fb66d81Schristos struct window_pane *loop;
90ed4e6cd4Schristos struct options *oo;
919fb66d81Schristos struct options_entry *parent, *o, *po;
92*6db26757Swiz char *name, *argument, *expanded = NULL;
93*6db26757Swiz char *cause;
94*6db26757Swiz const char *value;
954e179ddaSchristos int window, idx, already, error, ambiguous;
966483eba0Schristos int scope;
97c9ad075bSchristos
989fb66d81Schristos window = (cmd_get_entry(self) == &cmd_set_window_option_entry);
996483eba0Schristos
100c9ad075bSchristos /* Expand argument. */
101*6db26757Swiz argument = format_single_from_target(item, args_string(args, 0));
102698d5317Sjmmv
1036483eba0Schristos /* If set-hook -R, fire the hook straight away. */
1049fb66d81Schristos if (cmd_get_entry(self) == &cmd_set_hook_entry && args_has(args, 'R')) {
1056483eba0Schristos notify_hook(item, argument);
1066483eba0Schristos free(argument);
1076483eba0Schristos return (CMD_RETURN_NORMAL);
1086483eba0Schristos }
1096483eba0Schristos
1104e179ddaSchristos /* Parse option name and index. */
111c9ad075bSchristos name = options_match(argument, &idx, &ambiguous);
1124e179ddaSchristos if (name == NULL) {
1134e179ddaSchristos if (args_has(args, 'q'))
114c9ad075bSchristos goto out;
1154e179ddaSchristos if (ambiguous)
116c9ad075bSchristos cmdq_error(item, "ambiguous option: %s", argument);
1174e179ddaSchristos else
118c9ad075bSchristos cmdq_error(item, "invalid option: %s", argument);
119c9ad075bSchristos goto fail;
120d530c4d0Sjmmv }
121*6db26757Swiz if (args_count(args) < 2)
1224e179ddaSchristos value = NULL;
123d530c4d0Sjmmv else
124*6db26757Swiz value = args_string(args, 1);
125*6db26757Swiz if (value != NULL && args_has(args, 'F')) {
126*6db26757Swiz expanded = format_single_from_target(item, value);
127*6db26757Swiz value = expanded;
128*6db26757Swiz }
129d530c4d0Sjmmv
1306483eba0Schristos /* Get the scope and table for the option .*/
1319fb66d81Schristos scope = options_scope_from_name(args, window, name, target, &oo,
1329fb66d81Schristos &cause);
1334e179ddaSchristos if (scope == OPTIONS_TABLE_NONE) {
1344e179ddaSchristos if (args_has(args, 'q'))
135c9ad075bSchristos goto out;
1364e179ddaSchristos cmdq_error(item, "%s", cause);
1374e179ddaSchristos free(cause);
138c9ad075bSchristos goto fail;
139d530c4d0Sjmmv }
1404e179ddaSchristos o = options_get_only(oo, name);
1414e179ddaSchristos parent = options_get(oo, name);
1424e179ddaSchristos
1434e179ddaSchristos /* Check that array options and indexes match up. */
1449fb66d81Schristos if (idx != -1 && (*name == '@' || !options_is_array(parent))) {
145c9ad075bSchristos cmdq_error(item, "not an array: %s", argument);
146c9ad075bSchristos goto fail;
1474e179ddaSchristos }
148698d5317Sjmmv
1494e179ddaSchristos /* With -o, check this option is not already set. */
1504e179ddaSchristos if (!args_has(args, 'u') && args_has(args, 'o')) {
1514e179ddaSchristos if (idx == -1)
1524e179ddaSchristos already = (o != NULL);
1534e179ddaSchristos else {
1544e179ddaSchristos if (o == NULL)
1554e179ddaSchristos already = 0;
1564e179ddaSchristos else
1574e179ddaSchristos already = (options_array_get(o, idx) != NULL);
1585494e770Schristos }
1594e179ddaSchristos if (already) {
1604e179ddaSchristos if (args_has(args, 'q'))
161c9ad075bSchristos goto out;
162c9ad075bSchristos cmdq_error(item, "already set: %s", argument);
163c9ad075bSchristos goto fail;
164928fc495Schristos }
1654e179ddaSchristos }
166928fc495Schristos
1674e179ddaSchristos /* Change the option. */
1689fb66d81Schristos if (args_has(args, 'U') && scope == OPTIONS_TABLE_WINDOW) {
1699fb66d81Schristos TAILQ_FOREACH(loop, &target->w->panes, entry) {
1709fb66d81Schristos po = options_get_only(loop->options, name);
1719fb66d81Schristos if (po == NULL)
1729fb66d81Schristos continue;
1739fb66d81Schristos if (options_remove_or_default(po, idx, &cause) != 0) {
1749fb66d81Schristos cmdq_error(item, "%s", cause);
1759fb66d81Schristos free(cause);
1769fb66d81Schristos goto fail;
1779fb66d81Schristos }
1789fb66d81Schristos }
1799fb66d81Schristos }
1809fb66d81Schristos if (args_has(args, 'u') || args_has(args, 'U')) {
1814e179ddaSchristos if (o == NULL)
182c9ad075bSchristos goto out;
1839fb66d81Schristos if (options_remove_or_default(o, idx, &cause) != 0) {
1846483eba0Schristos cmdq_error(item, "%s", cause);
1856483eba0Schristos free(cause);
1866483eba0Schristos goto fail;
1876483eba0Schristos }
1884e179ddaSchristos } else if (*name == '@') {
1894e179ddaSchristos if (value == NULL) {
1904e179ddaSchristos cmdq_error(item, "empty value");
191c9ad075bSchristos goto fail;
1924e179ddaSchristos }
1934e179ddaSchristos options_set_string(oo, name, append, "%s", value);
1949fb66d81Schristos } else if (idx == -1 && !options_is_array(parent)) {
1959fb66d81Schristos error = options_from_string(oo, options_table_entry(parent),
1969fb66d81Schristos options_table_entry(parent)->name, value,
1979fb66d81Schristos args_has(args, 'a'), &cause);
1989fb66d81Schristos if (error != 0) {
1999fb66d81Schristos cmdq_error(item, "%s", cause);
2009fb66d81Schristos free(cause);
201c9ad075bSchristos goto fail;
2029fb66d81Schristos }
2034e179ddaSchristos } else {
2044e179ddaSchristos if (value == NULL) {
2054e179ddaSchristos cmdq_error(item, "empty value");
206c9ad075bSchristos goto fail;
2074e179ddaSchristos }
2084e179ddaSchristos if (o == NULL)
2094e179ddaSchristos o = options_empty(oo, options_table_entry(parent));
2104e179ddaSchristos if (idx == -1) {
2114e179ddaSchristos if (!append)
2124e179ddaSchristos options_array_clear(o);
2136483eba0Schristos if (options_array_assign(o, value, &cause) != 0) {
2146483eba0Schristos cmdq_error(item, "%s", cause);
2156483eba0Schristos free(cause);
2166483eba0Schristos goto fail;
2176483eba0Schristos }
2186483eba0Schristos } else if (options_array_set(o, idx, value, append,
2196483eba0Schristos &cause) != 0) {
2206483eba0Schristos cmdq_error(item, "%s", cause);
2216483eba0Schristos free(cause);
222c9ad075bSchristos goto fail;
2234e179ddaSchristos }
2244e179ddaSchristos }
2254e179ddaSchristos
2269fb66d81Schristos options_push_changes(name);
227698d5317Sjmmv
228c9ad075bSchristos out:
229c9ad075bSchristos free(argument);
230*6db26757Swiz free(expanded);
231c9ad075bSchristos free(name);
232928fc495Schristos return (CMD_RETURN_NORMAL);
233c9ad075bSchristos
234c9ad075bSchristos fail:
235c9ad075bSchristos free(argument);
236*6db26757Swiz free(expanded);
237c9ad075bSchristos free(name);
238c9ad075bSchristos return (CMD_RETURN_ERROR);
239698d5317Sjmmv }
240