4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
30 static enum args_parse_type
cmd_set_option_args_parse(struct args
*,
32 static enum cmd_retval
cmd_set_option_exec(struct cmd
*,
35 const struct cmd_entry cmd_set_option_entry
= {
39 .args
= { "aFgopqst:uUw", 1, 2, cmd_set_option_args_parse
},
40 .usage
= "[-aFgopqsuUw] " CMD_TARGET_PANE_USAGE
" option [value]",
42 .target
= { 't', CMD_FIND_PANE
, CMD_FIND_CANFAIL
},
44 .flags
= CMD_AFTERHOOK
,
45 .exec
= cmd_set_option_exec
48 const struct cmd_entry cmd_set_window_option_entry
= {
49 .name
= "set-window-option",
52 .args
= { "aFgoqt:u", 1, 2, cmd_set_option_args_parse
},
53 .usage
= "[-aFgoqu] " CMD_TARGET_WINDOW_USAGE
" option [value]",
55 .target
= { 't', CMD_FIND_WINDOW
, CMD_FIND_CANFAIL
},
57 .flags
= CMD_AFTERHOOK
,
58 .exec
= cmd_set_option_exec
61 const struct cmd_entry cmd_set_hook_entry
= {
65 .args
= { "agpRt:uw", 1, 2, cmd_set_option_args_parse
},
66 .usage
= "[-agpRuw] " CMD_TARGET_PANE_USAGE
" hook [command]",
68 .target
= { 't', CMD_FIND_PANE
, CMD_FIND_CANFAIL
},
70 .flags
= CMD_AFTERHOOK
,
71 .exec
= cmd_set_option_exec
74 static enum args_parse_type
75 cmd_set_option_args_parse(__unused
struct args
*args
, u_int idx
,
76 __unused
char **cause
)
79 return (ARGS_PARSE_COMMANDS_OR_STRING
);
80 return (ARGS_PARSE_STRING
);
83 static enum cmd_retval
84 cmd_set_option_exec(struct cmd
*self
, struct cmdq_item
*item
)
86 struct args
*args
= cmd_get_args(self
);
87 int append
= args_has(args
, 'a');
88 struct cmd_find_state
*target
= cmdq_get_target(item
);
89 struct window_pane
*loop
;
91 struct options_entry
*parent
, *o
, *po
;
92 char *name
, *argument
, *expanded
= NULL
;
95 int window
, idx
, already
, error
, ambiguous
;
98 window
= (cmd_get_entry(self
) == &cmd_set_window_option_entry
);
100 /* Expand argument. */
101 argument
= format_single_from_target(item
, args_string(args
, 0));
103 /* If set-hook -R, fire the hook straight away. */
104 if (cmd_get_entry(self
) == &cmd_set_hook_entry
&& args_has(args
, 'R')) {
105 notify_hook(item
, argument
);
107 return (CMD_RETURN_NORMAL
);
110 /* Parse option name and index. */
111 name
= options_match(argument
, &idx
, &ambiguous
);
113 if (args_has(args
, 'q'))
116 cmdq_error(item
, "ambiguous option: %s", argument
);
118 cmdq_error(item
, "invalid option: %s", argument
);
121 if (args_count(args
) < 2)
124 value
= args_string(args
, 1);
125 if (value
!= NULL
&& args_has(args
, 'F')) {
126 expanded
= format_single_from_target(item
, value
);
130 /* Get the scope and table for the option .*/
131 scope
= options_scope_from_name(args
, window
, name
, target
, &oo
,
133 if (scope
== OPTIONS_TABLE_NONE
) {
134 if (args_has(args
, 'q'))
136 cmdq_error(item
, "%s", cause
);
140 o
= options_get_only(oo
, name
);
141 parent
= options_get(oo
, name
);
143 /* Check that array options and indexes match up. */
144 if (idx
!= -1 && (*name
== '@' || !options_is_array(parent
))) {
145 cmdq_error(item
, "not an array: %s", argument
);
149 /* With -o, check this option is not already set. */
150 if (!args_has(args
, 'u') && args_has(args
, 'o')) {
152 already
= (o
!= NULL
);
157 already
= (options_array_get(o
, idx
) != NULL
);
160 if (args_has(args
, 'q'))
162 cmdq_error(item
, "already set: %s", argument
);
167 /* Change the option. */
168 if (args_has(args
, 'U') && scope
== OPTIONS_TABLE_WINDOW
) {
169 TAILQ_FOREACH(loop
, &target
->w
->panes
, entry
) {
170 po
= options_get_only(loop
->options
, name
);
173 if (options_remove_or_default(po
, idx
, &cause
) != 0) {
174 cmdq_error(item
, "%s", cause
);
180 if (args_has(args
, 'u') || args_has(args
, 'U')) {
183 if (options_remove_or_default(o
, idx
, &cause
) != 0) {
184 cmdq_error(item
, "%s", cause
);
188 } else if (*name
== '@') {
190 cmdq_error(item
, "empty value");
193 options_set_string(oo
, name
, append
, "%s", value
);
194 } else if (idx
== -1 && !options_is_array(parent
)) {
195 error
= options_from_string(oo
, options_table_entry(parent
),
196 options_table_entry(parent
)->name
, value
,
197 args_has(args
, 'a'), &cause
);
199 cmdq_error(item
, "%s", cause
);
205 cmdq_error(item
, "empty value");
209 o
= options_empty(oo
, options_table_entry(parent
));
212 options_array_clear(o
);
213 if (options_array_assign(o
, value
, &cause
) != 0) {
214 cmdq_error(item
, "%s", cause
);
218 } else if (options_array_set(o
, idx
, value
, append
,
220 cmdq_error(item
, "%s", cause
);
226 options_push_changes(name
);
232 return (CMD_RETURN_NORMAL
);
238 return (CMD_RETURN_ERROR
);