/src/tmux/cmd-set-option.c
Line | Count | Source |
1 | | /* $OpenBSD: cmd-set-option.c,v 1.147 2026/10/09 13:12:14 nicm Exp $ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> |
5 | | * |
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. |
9 | | * |
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. |
17 | | */ |
18 | | |
19 | | #include <sys/types.h> |
20 | | |
21 | | #include <stdlib.h> |
22 | | #include <string.h> |
23 | | |
24 | | #include "tmux.h" |
25 | | |
26 | | /* |
27 | | * Set an option. |
28 | | */ |
29 | | |
30 | | static enum args_parse_type cmd_set_option_args_parse(struct args *, |
31 | | u_int, char **); |
32 | | static enum cmd_retval cmd_set_option_exec(struct cmd *, |
33 | | struct cmdq_item *); |
34 | | static enum cmd_retval cmd_set_hook_event_exec(struct cmd *, |
35 | | struct cmdq_item *); |
36 | | static enum cmd_retval cmd_set_hook_monitor_exec(struct cmdq_item *, |
37 | | struct args *, int); |
38 | | |
39 | | const struct cmd_entry cmd_set_option_entry = { |
40 | | .name = "set-option", |
41 | | .description = "Set a server, session, or pane option.", |
42 | | .alias = "set", |
43 | | |
44 | | .args = { "aFgopqst:uUw", 1, 2, cmd_set_option_args_parse }, |
45 | | .usage = "[-aFgopqsuUw] " CMD_TARGET_PANE_USAGE " option [value]", |
46 | | |
47 | | .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, |
48 | | |
49 | | .flags = CMD_AFTERHOOK, |
50 | | .exec = cmd_set_option_exec |
51 | | }; |
52 | | |
53 | | const struct cmd_entry cmd_set_window_option_entry = { |
54 | | .name = "set-window-option", |
55 | | .description = "Set a window option.", |
56 | | .alias = "setw", |
57 | | |
58 | | .args = { "aFgoqt:u", 1, 2, cmd_set_option_args_parse }, |
59 | | .usage = "[-aFgoqu] " CMD_TARGET_WINDOW_USAGE " option [value]", |
60 | | |
61 | | .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL }, |
62 | | |
63 | | .flags = CMD_AFTERHOOK, |
64 | | .exec = cmd_set_option_exec |
65 | | }; |
66 | | |
67 | | const struct cmd_entry cmd_set_hook_entry = { |
68 | | .name = "set-hook", |
69 | | .description = "Set or remove a hook.", |
70 | | .alias = NULL, |
71 | | |
72 | | .args = { "agpERTt:uB:w", 0, 2, cmd_set_option_args_parse }, |
73 | | .usage = "[-agpERTuw] [-B name:what:format] " CMD_TARGET_PANE_USAGE " " |
74 | | "[hook] [command]", |
75 | | |
76 | | .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, |
77 | | |
78 | | .flags = CMD_AFTERHOOK, |
79 | | .exec = cmd_set_option_exec |
80 | | }; |
81 | | |
82 | | static enum args_parse_type |
83 | | cmd_set_option_args_parse(struct args *args, u_int idx, |
84 | | __unused char **cause) |
85 | 0 | { |
86 | 0 | if (args_has(args, 'B')) |
87 | 0 | return (ARGS_PARSE_COMMANDS_OR_STRING); |
88 | 0 | if (idx == 1) |
89 | 0 | return (ARGS_PARSE_COMMANDS_OR_STRING); |
90 | 0 | return (ARGS_PARSE_STRING); |
91 | 0 | } |
92 | | |
93 | | static enum cmd_retval |
94 | | cmd_set_hook_event_exec(struct cmd *self, struct cmdq_item *item) |
95 | 0 | { |
96 | 0 | struct args *args = cmd_get_args(self); |
97 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
98 | 0 | struct event_payload *ep; |
99 | 0 | struct client *c; |
100 | 0 | char *argument; |
101 | |
|
102 | 0 | if (args_count(args) == 0) { |
103 | 0 | cmdq_error(item, "missing argument"); |
104 | 0 | return (CMD_RETURN_ERROR); |
105 | 0 | } |
106 | 0 | if (args_count(args) != 1) { |
107 | 0 | cmdq_error(item, "too many arguments"); |
108 | 0 | return (CMD_RETURN_ERROR); |
109 | 0 | } |
110 | | |
111 | 0 | argument = format_single_from_target(item, args_string(args, 0)); |
112 | 0 | if (*argument != '@') { |
113 | 0 | cmdq_error(item, "event name must start with @"); |
114 | 0 | free(argument); |
115 | 0 | return (CMD_RETURN_ERROR); |
116 | 0 | } |
117 | | |
118 | 0 | ep = event_payload_create(); |
119 | 0 | event_payload_set_target(ep, target); |
120 | 0 | c = cmdq_get_client(item); |
121 | 0 | if (c != NULL) |
122 | 0 | event_payload_set_client(ep, "client", c); |
123 | 0 | if (target->s != NULL) |
124 | 0 | event_payload_set_session(ep, "session", target->s); |
125 | 0 | if (target->w != NULL) |
126 | 0 | event_payload_set_window(ep, "window", target->w); |
127 | 0 | if (target->wl != NULL) |
128 | 0 | event_payload_set_int(ep, "window_index", target->wl->idx); |
129 | 0 | else if (target->idx != -1) |
130 | 0 | event_payload_set_int(ep, "window_index", target->idx); |
131 | 0 | if (target->wp != NULL) |
132 | 0 | event_payload_set_pane(ep, "pane", target->wp); |
133 | 0 | events_fire(argument, ep); |
134 | 0 | free(argument); |
135 | 0 | return (CMD_RETURN_NORMAL); |
136 | 0 | } |
137 | | |
138 | | static enum cmd_retval |
139 | | cmd_set_hook_monitor_exec(struct cmdq_item *item, struct args *args, int window) |
140 | 0 | { |
141 | 0 | struct cmd_find_state *target = cmdq_get_target(item), fs; |
142 | 0 | struct options *oo; |
143 | 0 | struct options_entry *o; |
144 | 0 | struct session *s = NULL; |
145 | 0 | char *cause = NULL, *name = NULL, *format = NULL; |
146 | 0 | char *expanded = NULL, *newvalue = NULL; |
147 | 0 | const char *value, *old; |
148 | 0 | enum monitor_type type; |
149 | 0 | int id, scope, flags = 0; |
150 | |
|
151 | 0 | if (args_count(args) > 1) { |
152 | 0 | cmdq_error(item, "too many arguments"); |
153 | 0 | return (CMD_RETURN_ERROR); |
154 | 0 | } |
155 | | |
156 | 0 | value = args_get(args, 'B'); |
157 | 0 | if (args_has(args, 'u')) { |
158 | 0 | if (monitor_parse(value, &name, &type, &id, &format) != 0) |
159 | 0 | name = xstrdup(value); |
160 | 0 | free(format); |
161 | 0 | format = NULL; |
162 | 0 | } else { |
163 | 0 | if (monitor_parse(value, &name, &type, &id, &format) != 0) { |
164 | 0 | cmdq_error(item, "invalid subscription: %s", value); |
165 | 0 | return (CMD_RETURN_ERROR); |
166 | 0 | } |
167 | 0 | } |
168 | | |
169 | 0 | if (*name != '@') { |
170 | 0 | cmdq_error(item, "monitor hook name must start with @"); |
171 | 0 | goto fail; |
172 | 0 | } |
173 | | |
174 | 0 | scope = options_scope_from_name(args, window, name, target, &oo, |
175 | 0 | &cause); |
176 | 0 | if (scope == OPTIONS_TABLE_NONE) { |
177 | 0 | cmdq_error(item, "%s", cause); |
178 | 0 | free(cause); |
179 | 0 | goto fail; |
180 | 0 | } |
181 | 0 | cmd_find_copy_state(&fs, target); |
182 | |
|
183 | 0 | if (args_has(args, 'u')) { |
184 | 0 | hooks_monitor_remove(oo, name); |
185 | 0 | goto out; |
186 | 0 | } |
187 | | |
188 | 0 | if (args_count(args) != 0) { |
189 | 0 | value = args_string(args, 0); |
190 | 0 | if (args_has(args, 'F')) { |
191 | 0 | expanded = format_single_from_target(item, value); |
192 | 0 | value = expanded; |
193 | 0 | } |
194 | 0 | o = options_get_only(oo, name); |
195 | 0 | if (!args_has(args, 'o') || o == NULL) { |
196 | 0 | if (args_has(args, 'a') && o != NULL) { |
197 | 0 | old = options_get_string(oo, name); |
198 | 0 | xasprintf(&newvalue, "%s%s", old, value); |
199 | 0 | value = newvalue; |
200 | 0 | } |
201 | 0 | options_set_string(oo, name, 0, "%s", value); |
202 | 0 | options_push_changes(name); |
203 | 0 | } |
204 | 0 | } |
205 | |
|
206 | 0 | if (oo != global_options && |
207 | 0 | oo != global_s_options && |
208 | 0 | oo != global_w_options) |
209 | 0 | s = target->s; |
210 | 0 | if (args_has(args, 'T')) |
211 | 0 | flags |= MONITOR_NOTIFY_TRUE; |
212 | 0 | hooks_monitor_add(item, oo, name, type, id, format, flags, &fs, s); |
213 | |
|
214 | 0 | out: |
215 | 0 | free(newvalue); |
216 | 0 | free(expanded); |
217 | 0 | free(name); |
218 | 0 | free(format); |
219 | 0 | return (CMD_RETURN_NORMAL); |
220 | | |
221 | 0 | fail: |
222 | 0 | free(newvalue); |
223 | 0 | free(expanded); |
224 | 0 | free(name); |
225 | 0 | free(format); |
226 | 0 | return (CMD_RETURN_ERROR); |
227 | 0 | } |
228 | | |
229 | | static enum cmd_retval |
230 | | cmd_set_option_exec(struct cmd *self, struct cmdq_item *item) |
231 | 0 | { |
232 | 0 | struct args *args = cmd_get_args(self); |
233 | 0 | int append = args_has(args, 'a'); |
234 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
235 | 0 | struct window_pane *loop; |
236 | 0 | struct options *oo; |
237 | 0 | struct options_entry *parent, *o, *po; |
238 | 0 | char *name, *argument, *cause; |
239 | 0 | char *expanded = NULL, *array_key = NULL; |
240 | 0 | const char *value; |
241 | 0 | int window, already, error, ambiguous; |
242 | 0 | int scope; |
243 | |
|
244 | 0 | window = (cmd_get_entry(self) == &cmd_set_window_option_entry); |
245 | 0 | if (cmd_get_entry(self) == &cmd_set_hook_entry && args_has(args, 'E')) |
246 | 0 | return (cmd_set_hook_event_exec(self, item)); |
247 | 0 | if (cmd_get_entry(self) == &cmd_set_hook_entry && args_has(args, 'B')) |
248 | 0 | return (cmd_set_hook_monitor_exec(item, args, window)); |
249 | 0 | if (args_count(args) == 0) { |
250 | 0 | cmdq_error(item, "missing argument"); |
251 | 0 | return (CMD_RETURN_ERROR); |
252 | 0 | } |
253 | | |
254 | | /* Expand argument. */ |
255 | 0 | argument = format_single_from_target(item, args_string(args, 0)); |
256 | | |
257 | | /* If set-hook -R, fire the hook straight away. */ |
258 | 0 | if (cmd_get_entry(self) == &cmd_set_hook_entry && args_has(args, 'R')) { |
259 | 0 | hooks_run(item, argument); |
260 | 0 | free(argument); |
261 | 0 | return (CMD_RETURN_NORMAL); |
262 | 0 | } |
263 | | |
264 | | /* Parse option name and array key. */ |
265 | 0 | name = options_match(argument, &array_key, &ambiguous); |
266 | 0 | if (name == NULL) { |
267 | 0 | if (args_has(args, 'q')) |
268 | 0 | goto out; |
269 | 0 | if (ambiguous) |
270 | 0 | cmdq_error(item, "ambiguous option: %s", argument); |
271 | 0 | else |
272 | 0 | cmdq_error(item, "invalid option: %s", argument); |
273 | 0 | goto fail; |
274 | 0 | } |
275 | 0 | if (args_count(args) < 2) |
276 | 0 | value = NULL; |
277 | 0 | else |
278 | 0 | value = args_string(args, 1); |
279 | 0 | if (value != NULL && args_has(args, 'F')) { |
280 | 0 | expanded = format_single_from_target(item, value); |
281 | 0 | value = expanded; |
282 | 0 | } |
283 | | |
284 | | /* Get the scope and table for the option .*/ |
285 | 0 | scope = options_scope_from_name(args, window, name, target, &oo, |
286 | 0 | &cause); |
287 | 0 | if (scope == OPTIONS_TABLE_NONE) { |
288 | 0 | if (args_has(args, 'q')) |
289 | 0 | goto out; |
290 | 0 | cmdq_error(item, "%s", cause); |
291 | 0 | free(cause); |
292 | 0 | goto fail; |
293 | 0 | } |
294 | 0 | o = options_get_only(oo, name); |
295 | 0 | parent = options_get(oo, name); |
296 | | |
297 | | /* Check that array options and keys match up. */ |
298 | 0 | if (array_key != NULL && (*name == '@' || !options_is_array(parent))) { |
299 | 0 | cmdq_error(item, "not an array: %s", argument); |
300 | 0 | goto fail; |
301 | 0 | } |
302 | | |
303 | | /* With -o, check this option is not already set. */ |
304 | 0 | if (!args_has(args, 'u') && args_has(args, 'o')) { |
305 | 0 | if (array_key == NULL) |
306 | 0 | already = (o != NULL); |
307 | 0 | else { |
308 | 0 | if (o == NULL) |
309 | 0 | already = 0; |
310 | 0 | else if (options_array_get(o, array_key) != NULL) |
311 | 0 | already = 1; |
312 | 0 | else |
313 | 0 | already = 0; |
314 | 0 | } |
315 | 0 | if (already) { |
316 | 0 | if (args_has(args, 'q')) |
317 | 0 | goto out; |
318 | 0 | cmdq_error(item, "already set: %s", argument); |
319 | 0 | goto fail; |
320 | 0 | } |
321 | 0 | } |
322 | | |
323 | | /* Change the option. */ |
324 | 0 | if (args_has(args, 'U') && scope == OPTIONS_TABLE_WINDOW) { |
325 | 0 | TAILQ_FOREACH(loop, &target->w->panes, entry) { |
326 | 0 | po = options_get_only(loop->options, name); |
327 | 0 | if (po == NULL) |
328 | 0 | continue; |
329 | 0 | if (options_remove_or_default(po, array_key, |
330 | 0 | &cause) != 0) { |
331 | 0 | cmdq_error(item, "%s", cause); |
332 | 0 | free(cause); |
333 | 0 | goto fail; |
334 | 0 | } |
335 | 0 | } |
336 | 0 | } |
337 | 0 | if (args_has(args, 'u') || args_has(args, 'U')) { |
338 | 0 | if (o == NULL) |
339 | 0 | goto out; |
340 | 0 | if (options_remove_or_default(o, array_key, &cause) != 0) { |
341 | 0 | cmdq_error(item, "%s", cause); |
342 | 0 | free(cause); |
343 | 0 | goto fail; |
344 | 0 | } |
345 | 0 | } else if (*name == '@') { |
346 | 0 | if (value == NULL) { |
347 | 0 | cmdq_error(item, "empty value"); |
348 | 0 | goto fail; |
349 | 0 | } |
350 | 0 | options_set_string(oo, name, append, "%s", value); |
351 | 0 | if (cmd_get_entry(self) == &cmd_set_hook_entry) |
352 | 0 | hooks_add_event(name); |
353 | 0 | } else if (array_key == NULL && !options_is_array(parent)) { |
354 | 0 | error = options_from_string(oo, options_table_entry(parent), |
355 | 0 | options_table_entry(parent)->name, value, |
356 | 0 | args_has(args, 'a'), &cause); |
357 | 0 | if (error != 0) { |
358 | 0 | cmdq_error(item, "%s", cause); |
359 | 0 | free(cause); |
360 | 0 | goto fail; |
361 | 0 | } |
362 | 0 | } else { |
363 | 0 | if (value == NULL) { |
364 | 0 | cmdq_error(item, "empty value"); |
365 | 0 | goto fail; |
366 | 0 | } |
367 | 0 | if (o == NULL) |
368 | 0 | o = options_empty(oo, options_table_entry(parent)); |
369 | 0 | if (array_key == NULL) { |
370 | 0 | if (!append) |
371 | 0 | options_array_clear(o); |
372 | 0 | if (options_array_assign(o, value, &cause) != 0) { |
373 | 0 | cmdq_error(item, "%s", cause); |
374 | 0 | free(cause); |
375 | 0 | goto fail; |
376 | 0 | } |
377 | 0 | } else if (options_array_set(o, array_key, value, append, |
378 | 0 | &cause) != 0) { |
379 | 0 | cmdq_error(item, "%s", cause); |
380 | 0 | free(cause); |
381 | 0 | goto fail; |
382 | 0 | } |
383 | 0 | } |
384 | | |
385 | 0 | options_push_changes(name); |
386 | |
|
387 | 0 | out: |
388 | 0 | free(argument); |
389 | 0 | free(expanded); |
390 | 0 | free(name); |
391 | 0 | free(array_key); |
392 | 0 | return (CMD_RETURN_NORMAL); |
393 | | |
394 | 0 | fail: |
395 | 0 | free(argument); |
396 | 0 | free(expanded); |
397 | 0 | free(name); |
398 | 0 | free(array_key); |
399 | 0 | return (CMD_RETURN_ERROR); |
400 | 0 | } |