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