xref: /openbsd-src/usr.bin/tmux/cmd-set-option.c (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
1 /* $OpenBSD: cmd-set-option.c,v 1.119 2017/12/22 10:18:51 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 cmd_retval	cmd_set_option_exec(struct cmd *, struct cmdq_item *);
31 
32 static int	cmd_set_option_set(struct cmd *, struct cmdq_item *,
33 		    struct options *, struct options_entry *, const char *);
34 static int	cmd_set_option_flag(struct cmdq_item *,
35 		    const struct options_table_entry *, struct options *,
36 		    const char *);
37 static int	cmd_set_option_choice(struct cmdq_item *,
38 		    const struct options_table_entry *, struct options *,
39 		    const char *);
40 
41 const struct cmd_entry cmd_set_option_entry = {
42 	.name = "set-option",
43 	.alias = "set",
44 
45 	.args = { "aFgoqst:uw", 1, 2 },
46 	.usage = "[-aFgosquw] [-t target-window] option [value]",
47 
48 	.target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
49 
50 	.flags = CMD_AFTERHOOK,
51 	.exec = cmd_set_option_exec
52 };
53 
54 const struct cmd_entry cmd_set_window_option_entry = {
55 	.name = "set-window-option",
56 	.alias = "setw",
57 
58 	.args = { "aFgoqt:u", 1, 2 },
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 static enum cmd_retval
68 cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
69 {
70 	struct args			*args = self->args;
71 	int				 append = args_has(args, 'a');
72 	struct cmd_find_state		*fs = &item->target;
73 	struct client			*c, *loop;
74 	struct session			*s = fs->s;
75 	struct winlink			*wl = fs->wl;
76 	struct window			*w;
77 	enum options_table_scope	 scope;
78 	struct options			*oo;
79 	struct options_entry		*parent, *o;
80 	char				*name, *argument, *value = NULL, *cause;
81 	const char			*target;
82 	int				 window, idx, already, error, ambiguous;
83 
84 	/* Expand argument. */
85 	c = cmd_find_client(item, NULL, 1);
86 	argument = format_single(item, args->argv[0], c, s, wl, NULL);
87 
88 	/* Parse option name and index. */
89 	name = options_match(argument, &idx, &ambiguous);
90 	if (name == NULL) {
91 		if (args_has(args, 'q'))
92 			goto out;
93 		if (ambiguous)
94 			cmdq_error(item, "ambiguous option: %s", argument);
95 		else
96 			cmdq_error(item, "invalid option: %s", argument);
97 		goto fail;
98 	}
99 	if (args->argc < 2)
100 		value = NULL;
101 	else if (args_has(args, 'F'))
102 		value = format_single(item, args->argv[1], c, s, wl, NULL);
103 	else
104 		value = xstrdup(args->argv[1]);
105 
106 	/*
107 	 * Figure out the scope: for user options it comes from the arguments,
108 	 * otherwise from the option name.
109 	 */
110 	if (*name == '@') {
111 		window = (self->entry == &cmd_set_window_option_entry);
112 		scope = options_scope_from_flags(args, window, fs, &oo, &cause);
113 	} else {
114 		if (options_get_only(global_options, name) != NULL)
115 			scope = OPTIONS_TABLE_SERVER;
116 		else if (options_get_only(global_s_options, name) != NULL)
117 			scope = OPTIONS_TABLE_SESSION;
118 		else if (options_get_only(global_w_options, name) != NULL)
119 			scope = OPTIONS_TABLE_WINDOW;
120 		else {
121 			scope = OPTIONS_TABLE_NONE;
122 			xasprintf(&cause, "unknown option: %s", argument);
123 		}
124 	}
125 	if (scope == OPTIONS_TABLE_NONE) {
126 		if (args_has(args, 'q'))
127 			goto out;
128 		cmdq_error(item, "%s", cause);
129 		free(cause);
130 		goto fail;
131 	}
132 
133 	/* Which table should this option go into? */
134 	if (scope == OPTIONS_TABLE_SERVER)
135 		oo = global_options;
136 	else if (scope == OPTIONS_TABLE_SESSION) {
137 		if (args_has(self->args, 'g'))
138 			oo = global_s_options;
139 		else if (s == NULL) {
140 			target = args_get(args, 't');
141 			if (target != NULL)
142 				cmdq_error(item, "no such session: %s", target);
143 			else
144 				cmdq_error(item, "no current session");
145 			goto fail;
146 		} else
147 			oo = s->options;
148 	} else if (scope == OPTIONS_TABLE_WINDOW) {
149 		if (args_has(self->args, 'g'))
150 			oo = global_w_options;
151 		else if (wl == NULL) {
152 			target = args_get(args, 't');
153 			if (target != NULL)
154 				cmdq_error(item, "no such window: %s", target);
155 			else
156 				cmdq_error(item, "no current window");
157 			goto fail;
158 		} else
159 			oo = wl->window->options;
160 	}
161 	o = options_get_only(oo, name);
162 	parent = options_get(oo, name);
163 
164 	/* Check that array options and indexes match up. */
165 	if (idx != -1) {
166 		if (*name == '@' || options_array_size(parent, NULL) == -1) {
167 			cmdq_error(item, "not an array: %s", argument);
168 			goto fail;
169 		}
170 	}
171 
172 	/* With -o, check this option is not already set. */
173 	if (!args_has(args, 'u') && args_has(args, 'o')) {
174 		if (idx == -1)
175 			already = (o != NULL);
176 		else {
177 			if (o == NULL)
178 				already = 0;
179 			else
180 				already = (options_array_get(o, idx) != NULL);
181 		}
182 		if (already) {
183 			if (args_has(args, 'q'))
184 				goto out;
185 			cmdq_error(item, "already set: %s", argument);
186 			goto fail;
187 		}
188 	}
189 
190 	/* Change the option. */
191 	if (args_has(args, 'u')) {
192 		if (o == NULL)
193 			goto out;
194 		if (idx == -1) {
195 			if (*name == '@')
196 				options_remove(o);
197 			else if (oo == global_options ||
198 			    oo == global_s_options ||
199 			    oo == global_w_options)
200 				options_default(oo, options_table_entry(o));
201 			else
202 				options_remove(o);
203 		} else
204 			options_array_set(o, idx, NULL, 0);
205 	} else if (*name == '@') {
206 		if (value == NULL) {
207 			cmdq_error(item, "empty value");
208 			goto fail;
209 		}
210 		options_set_string(oo, name, append, "%s", value);
211 	} else if (idx == -1 && options_array_size(parent, NULL) == -1) {
212 		error = cmd_set_option_set(self, item, oo, parent, value);
213 		if (error != 0)
214 			goto fail;
215 	} else {
216 		if (value == NULL) {
217 			cmdq_error(item, "empty value");
218 			goto fail;
219 		}
220 		if (o == NULL)
221 			o = options_empty(oo, options_table_entry(parent));
222 		if (idx == -1) {
223 			if (!append)
224 				options_array_clear(o);
225 			options_array_assign(o, value);
226 		} else if (options_array_set(o, idx, value, append) != 0) {
227 			cmdq_error(item, "invalid index: %s", argument);
228 			goto fail;
229 		}
230 	}
231 
232 	/* Update timers and so on for various options. */
233 	if (strcmp(name, "automatic-rename") == 0) {
234 		RB_FOREACH(w, windows, &windows) {
235 			if (w->active == NULL)
236 				continue;
237 			if (options_get_number(w->options, "automatic-rename"))
238 				w->active->flags |= PANE_CHANGED;
239 		}
240 	}
241 	if (strcmp(name, "key-table") == 0) {
242 		TAILQ_FOREACH(loop, &clients, entry)
243 			server_client_set_key_table(loop, NULL);
244 	}
245 	if (strcmp(name, "user-keys") == 0) {
246 		TAILQ_FOREACH(loop, &clients, entry) {
247 			if (loop->tty.flags & TTY_OPENED)
248 				tty_keys_build(&loop->tty);
249 		}
250 	}
251 	if (strcmp(name, "status") == 0 ||
252 	    strcmp(name, "status-interval") == 0)
253 		status_timer_start_all();
254 	if (strcmp(name, "monitor-silence") == 0)
255 		alerts_reset_all();
256 	if (strcmp(name, "window-style") == 0 ||
257 	    strcmp(name, "window-active-style") == 0) {
258 		RB_FOREACH(w, windows, &windows)
259 			w->flags |= WINDOW_STYLECHANGED;
260 	}
261 	if (strcmp(name, "pane-border-status") == 0) {
262 		RB_FOREACH(w, windows, &windows)
263 			layout_fix_panes(w, w->sx, w->sy);
264 	}
265 	RB_FOREACH(s, sessions, &sessions)
266 		status_update_saved(s);
267 
268 	/*
269 	 * Update sizes and redraw. May not always be necessary but do it
270 	 * anyway.
271 	 */
272 	recalculate_sizes();
273 	TAILQ_FOREACH(loop, &clients, entry) {
274 		if (loop->session != NULL)
275 			server_redraw_client(loop);
276 	}
277 
278 out:
279 	free(argument);
280 	free(value);
281 	free(name);
282 	return (CMD_RETURN_NORMAL);
283 
284 fail:
285 	free(argument);
286 	free(value);
287 	free(name);
288 	return (CMD_RETURN_ERROR);
289 }
290 
291 static int
292 cmd_set_option_set(struct cmd *self, struct cmdq_item *item, struct options *oo,
293     struct options_entry *parent, const char *value)
294 {
295 	const struct options_table_entry	*oe;
296 	struct args				*args = self->args;
297 	int					 append = args_has(args, 'a');
298 	struct options_entry			*o;
299 	long long				 number;
300 	const char				*errstr;
301 	key_code				 key;
302 
303 	oe = options_table_entry(parent);
304 	if (value == NULL &&
305 	    oe->type != OPTIONS_TABLE_FLAG &&
306 	    oe->type != OPTIONS_TABLE_CHOICE) {
307 		cmdq_error(item, "empty value");
308 		return (-1);
309 	}
310 
311 	switch (oe->type) {
312 	case OPTIONS_TABLE_STRING:
313 		options_set_string(oo, oe->name, append, "%s", value);
314 		return (0);
315 	case OPTIONS_TABLE_NUMBER:
316 		number = strtonum(value, oe->minimum, oe->maximum, &errstr);
317 		if (errstr != NULL) {
318 			cmdq_error(item, "value is %s: %s", errstr, value);
319 			return (-1);
320 		}
321 		options_set_number(oo, oe->name, number);
322 		return (0);
323 	case OPTIONS_TABLE_KEY:
324 		key = key_string_lookup_string(value);
325 		if (key == KEYC_UNKNOWN) {
326 			cmdq_error(item, "bad key: %s", value);
327 			return (-1);
328 		}
329 		options_set_number(oo, oe->name, key);
330 		return (0);
331 	case OPTIONS_TABLE_COLOUR:
332 		if ((number = colour_fromstring(value)) == -1) {
333 			cmdq_error(item, "bad colour: %s", value);
334 			return (-1);
335 		}
336 		o = options_set_number(oo, oe->name, number);
337 		options_style_update_new(oo, o);
338 		return (0);
339 	case OPTIONS_TABLE_ATTRIBUTES:
340 		if ((number = attributes_fromstring(value)) == -1) {
341 			cmdq_error(item, "bad attributes: %s", value);
342 			return (-1);
343 		}
344 		o = options_set_number(oo, oe->name, number);
345 		options_style_update_new(oo, o);
346 		return (0);
347 	case OPTIONS_TABLE_FLAG:
348 		return (cmd_set_option_flag(item, oe, oo, value));
349 	case OPTIONS_TABLE_CHOICE:
350 		return (cmd_set_option_choice(item, oe, oo, value));
351 	case OPTIONS_TABLE_STYLE:
352 		o = options_set_style(oo, oe->name, append, value);
353 		if (o == NULL) {
354 			cmdq_error(item, "bad style: %s", value);
355 			return (-1);
356 		}
357 		options_style_update_old(oo, o);
358 		return (0);
359 	case OPTIONS_TABLE_ARRAY:
360 		break;
361 	}
362 	return (-1);
363 }
364 
365 static int
366 cmd_set_option_flag(struct cmdq_item *item,
367     const struct options_table_entry *oe, struct options *oo,
368     const char *value)
369 {
370 	int	flag;
371 
372 	if (value == NULL || *value == '\0')
373 		flag = !options_get_number(oo, oe->name);
374 	else if (strcmp(value, "1") == 0 ||
375 	    strcasecmp(value, "on") == 0 ||
376 	    strcasecmp(value, "yes") == 0)
377 		flag = 1;
378 	else if (strcmp(value, "0") == 0 ||
379 	    strcasecmp(value, "off") == 0 ||
380 	    strcasecmp(value, "no") == 0)
381 		flag = 0;
382 	else {
383 		cmdq_error(item, "bad value: %s", value);
384 		return (-1);
385 	}
386 	options_set_number(oo, oe->name, flag);
387 	return (0);
388 }
389 
390 static int
391 cmd_set_option_choice(struct cmdq_item *item,
392     const struct options_table_entry *oe, struct options *oo,
393     const char *value)
394 {
395 	const char	**cp;
396 	int		  n, choice = -1;
397 
398 	if (value == NULL) {
399 		choice = options_get_number(oo, oe->name);
400 		if (choice < 2)
401 			choice = !choice;
402 	} else {
403 		n = 0;
404 		for (cp = oe->choices; *cp != NULL; cp++) {
405 			if (strcmp(*cp, value) == 0)
406 				choice = n;
407 			n++;
408 		}
409 		if (choice == -1) {
410 			cmdq_error(item, "unknown value: %s", value);
411 			return (-1);
412 		}
413 	}
414 	options_set_number(oo, oe->name, choice);
415 	return (0);
416 }
417