xref: /openbsd-src/usr.bin/tmux/cmd-set-option.c (revision 920e4eae2f0092e64e452564b25371d836e4cd37)
1 /* $OpenBSD: cmd-set-option.c,v 1.102 2016/11/04 18:56:25 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 enum cmd_retval	cmd_set_option_user(struct cmd *, struct cmdq_item *,
33 			    const char *, const char *);
34 
35 static int	cmd_set_option_unset(struct cmd *, struct cmdq_item *,
36 		    const struct options_table_entry *, struct options *,
37 		    const char *);
38 static int	cmd_set_option_set(struct cmd *, struct cmdq_item *,
39 		    const struct options_table_entry *, struct options *,
40 		    const char *);
41 
42 static struct options_entry *cmd_set_option_string(struct cmd *,
43 	    struct cmdq_item *, const struct options_table_entry *,
44 	    struct options *, const char *);
45 static struct options_entry *cmd_set_option_number(struct cmd *,
46 	    struct cmdq_item *, const struct options_table_entry *,
47 	    struct options *, const char *);
48 static struct options_entry *cmd_set_option_key(struct cmd *,
49 	    struct cmdq_item *, const struct options_table_entry *,
50 	    struct options *, const char *);
51 static struct options_entry *cmd_set_option_colour(struct cmd *,
52 	    struct cmdq_item *, const struct options_table_entry *,
53 	    struct options *, const char *);
54 static struct options_entry *cmd_set_option_attributes(struct cmd *,
55 	    struct cmdq_item *, const struct options_table_entry *,
56 	    struct options *, const char *);
57 static struct options_entry *cmd_set_option_flag(struct cmd *,
58 	    struct cmdq_item *, const struct options_table_entry *,
59 	    struct options *, const char *);
60 static struct options_entry *cmd_set_option_choice(struct cmd *,
61 	    struct cmdq_item *, const struct options_table_entry *,
62 	    struct options *, const char *);
63 static struct options_entry *cmd_set_option_style(struct cmd *,
64 	    struct cmdq_item *, const struct options_table_entry *,
65 	    struct options *, const char *);
66 
67 const struct cmd_entry cmd_set_option_entry = {
68 	.name = "set-option",
69 	.alias = "set",
70 
71 	.args = { "agoqst:uw", 1, 2 },
72 	.usage = "[-agosquw] [-t target-window] option [value]",
73 
74 	.tflag = CMD_WINDOW_CANFAIL,
75 
76 	.flags = CMD_AFTERHOOK,
77 	.exec = cmd_set_option_exec
78 };
79 
80 const struct cmd_entry cmd_set_window_option_entry = {
81 	.name = "set-window-option",
82 	.alias = "setw",
83 
84 	.args = { "agoqt:u", 1, 2 },
85 	.usage = "[-agoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
86 
87 	.tflag = CMD_WINDOW_CANFAIL,
88 
89 	.flags = CMD_AFTERHOOK,
90 	.exec = cmd_set_option_exec
91 };
92 
93 static enum cmd_retval
94 cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
95 {
96 	struct args				*args = self->args;
97 	struct session				*s = item->state.tflag.s;
98 	struct winlink				*wl = item->state.tflag.wl;
99 	struct window				*w;
100 	struct client				*c;
101 	const struct options_table_entry	*oe;
102 	struct options				*oo;
103 	const char				*optstr, *valstr, *target;
104 
105 	/* Get the option name and value. */
106 	optstr = args->argv[0];
107 	if (*optstr == '\0') {
108 		cmdq_error(item, "invalid option");
109 		return (CMD_RETURN_ERROR);
110 	}
111 	if (args->argc < 2)
112 		valstr = NULL;
113 	else
114 		valstr = args->argv[1];
115 
116 	/* Is this a user option? */
117 	if (*optstr == '@')
118 		return (cmd_set_option_user(self, item, optstr, valstr));
119 
120 	/* Find the option entry, try each table. */
121 	oe = NULL;
122 	if (options_table_find(optstr, &oe) != 0) {
123 		if (!args_has(args, 'q')) {
124 			cmdq_error(item, "ambiguous option: %s", optstr);
125 			return (CMD_RETURN_ERROR);
126 		}
127 		return (CMD_RETURN_NORMAL);
128 	}
129 	if (oe == NULL) {
130 		if (!args_has(args, 'q')) {
131 			cmdq_error(item, "unknown option: %s", optstr);
132 			return (CMD_RETURN_ERROR);
133 		}
134 		return (CMD_RETURN_NORMAL);
135 	}
136 
137 	/* Work out the tree from the scope of the option. */
138 	if (oe->scope == OPTIONS_TABLE_SERVER)
139 		oo = global_options;
140 	else if (oe->scope == OPTIONS_TABLE_WINDOW) {
141 		if (args_has(self->args, 'g'))
142 			oo = global_w_options;
143 		else if (wl == NULL) {
144 			target = args_get(args, 't');
145 			if (target != NULL) {
146 				cmdq_error(item, "no such window: %s",
147 				    target);
148 			} else
149 				cmdq_error(item, "no current window");
150 			return (CMD_RETURN_ERROR);
151 		} else
152 			oo = wl->window->options;
153 	} else if (oe->scope == OPTIONS_TABLE_SESSION) {
154 		if (args_has(self->args, 'g'))
155 			oo = global_s_options;
156 		else if (s == NULL) {
157 			target = args_get(args, 't');
158 			if (target != NULL) {
159 				cmdq_error(item, "no such session: %s",
160 				    target);
161 			} else
162 				cmdq_error(item, "no current session");
163 			return (CMD_RETURN_ERROR);
164 		} else
165 			oo = s->options;
166 	} else {
167 		cmdq_error(item, "unknown table");
168 		return (CMD_RETURN_ERROR);
169 	}
170 
171 	/* Unset or set the option. */
172 	if (args_has(args, 'u')) {
173 		if (cmd_set_option_unset(self, item, oe, oo, valstr) != 0)
174 			return (CMD_RETURN_ERROR);
175 	} else {
176 		if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) {
177 			if (!args_has(args, 'q')) {
178 				cmdq_error(item, "already set: %s", optstr);
179 				return (CMD_RETURN_ERROR);
180 			}
181 			return (CMD_RETURN_NORMAL);
182 		}
183 		if (cmd_set_option_set(self, item, oe, oo, valstr) != 0)
184 			return (CMD_RETURN_ERROR);
185 	}
186 
187 	/* Start or stop timers if necessary. */
188 	if (strcmp(oe->name, "automatic-rename") == 0) {
189 		RB_FOREACH(w, windows, &windows) {
190 			if (w->active == NULL)
191 				continue;
192 			if (options_get_number(w->options, "automatic-rename"))
193 				w->active->flags |= PANE_CHANGED;
194 		}
195 	}
196 	if (strcmp(oe->name, "key-table") == 0) {
197 		TAILQ_FOREACH(c, &clients, entry)
198 			server_client_set_key_table(c, NULL);
199 	}
200 	if (strcmp(oe->name, "status") == 0 ||
201 	    strcmp(oe->name, "status-interval") == 0)
202 		status_timer_start_all();
203 	if (strcmp(oe->name, "monitor-silence") == 0)
204 		alerts_reset_all();
205 	if (strcmp(oe->name, "window-style") == 0 ||
206 	    strcmp(oe->name, "window-active-style") == 0) {
207 		RB_FOREACH(w, windows, &windows)
208 			w->flags |= WINDOW_STYLECHANGED;
209 	}
210 
211 	/* When the pane-border-status option has been changed, resize panes. */
212 	if (strcmp(oe->name, "pane-border-status") == 0) {
213 		RB_FOREACH(w, windows, &windows)
214 			layout_fix_panes(w, w->sx, w->sy);
215 	}
216 
217 	/* Update sizes and redraw. May not need it but meh. */
218 	recalculate_sizes();
219 	TAILQ_FOREACH(c, &clients, entry) {
220 		if (c->session != NULL)
221 			server_redraw_client(c);
222 	}
223 
224 	return (CMD_RETURN_NORMAL);
225 }
226 
227 /* Set user option. */
228 static enum cmd_retval
229 cmd_set_option_user(struct cmd *self, struct cmdq_item *item,
230     const char *optstr, const char *valstr)
231 {
232 	struct args		*args = self->args;
233 	struct session		*s = item->state.tflag.s;
234 	struct winlink		*wl = item->state.tflag.wl;
235 	struct options		*oo;
236 	struct options_entry	*o;
237 	const char		*target;
238 
239 	if (args_has(args, 's'))
240 		oo = global_options;
241 	else if (args_has(self->args, 'w') ||
242 	    self->entry == &cmd_set_window_option_entry) {
243 		if (args_has(self->args, 'g'))
244 			oo = global_w_options;
245 		else if (wl == NULL) {
246 			target = args_get(args, 't');
247 			if (target != NULL) {
248 				cmdq_error(item, "no such window: %s",
249 				    target);
250 			} else
251 				cmdq_error(item, "no current window");
252 			return (CMD_RETURN_ERROR);
253 		} else
254 			oo = wl->window->options;
255 	} else {
256 		if (args_has(self->args, 'g'))
257 			oo = global_s_options;
258 		else if (s == NULL) {
259 			target = args_get(args, 't');
260 			if (target != NULL) {
261 				cmdq_error(item, "no such session: %s",
262 				    target);
263 			} else
264 				cmdq_error(item, "no current session");
265 			return (CMD_RETURN_ERROR);
266 		} else
267 			oo = s->options;
268 	}
269 
270 	if (args_has(args, 'u')) {
271 		if (options_find1(oo, optstr) == NULL) {
272 			if (!args_has(args, 'q')) {
273 				cmdq_error(item, "unknown option: %s", optstr);
274 				return (CMD_RETURN_ERROR);
275 			}
276 			return (CMD_RETURN_NORMAL);
277 		}
278 		if (valstr != NULL) {
279 			cmdq_error(item, "value passed to unset option: %s",
280 			    optstr);
281 			return (CMD_RETURN_ERROR);
282 		}
283 		options_remove(oo, optstr);
284 	} else {
285 		o = options_find1(oo, optstr);
286 		if (args_has(args, 'o') && o != NULL) {
287 			if (!args_has(args, 'q')) {
288 				cmdq_error(item, "already set: %s", optstr);
289 				return (CMD_RETURN_ERROR);
290 			}
291 			return (CMD_RETURN_NORMAL);
292 		}
293 		if (valstr == NULL) {
294 			cmdq_error(item, "empty value");
295 			return (CMD_RETURN_ERROR);
296 		}
297 		if (o != NULL && args_has(args, 'a'))
298 			options_set_string(oo, optstr, "%s%s", o->str, valstr);
299 		else
300 			options_set_string(oo, optstr, "%s", valstr);
301 	}
302 	return (CMD_RETURN_NORMAL);
303 }
304 
305 /* Unset an option. */
306 static int
307 cmd_set_option_unset(struct cmd *self, struct cmdq_item *item,
308     const struct options_table_entry *oe, struct options *oo,
309     const char *value)
310 {
311 	struct args	*args = self->args;
312 
313 	if (value != NULL) {
314 		cmdq_error(item, "value passed to unset option: %s", oe->name);
315 		return (-1);
316 	}
317 
318 	if (args_has(args, 'g') || oo == global_options) {
319 		switch (oe->type) {
320 		case OPTIONS_TABLE_STRING:
321 			options_set_string(oo, oe->name, "%s", oe->default_str);
322 			break;
323 		case OPTIONS_TABLE_STYLE:
324 			options_set_style(oo, oe->name, oe->default_str, 0);
325 			break;
326 		default:
327 			options_set_number(oo, oe->name, oe->default_num);
328 			break;
329 		}
330 	} else
331 		options_remove(oo, oe->name);
332 	return (0);
333 }
334 
335 /* Set an option. */
336 static int
337 cmd_set_option_set(struct cmd *self, struct cmdq_item *item,
338     const struct options_table_entry *oe, struct options *oo,
339     const char *value)
340 {
341 	struct options_entry	*o;
342 
343 	switch (oe->type) {
344 	case OPTIONS_TABLE_FLAG:
345 	case OPTIONS_TABLE_CHOICE:
346 		break;
347 	default:
348 		if (value == NULL) {
349 			cmdq_error(item, "empty value");
350 			return (-1);
351 		}
352 	}
353 
354 	o = NULL;
355 	switch (oe->type) {
356 	case OPTIONS_TABLE_STRING:
357 		o = cmd_set_option_string(self, item, oe, oo, value);
358 		break;
359 	case OPTIONS_TABLE_NUMBER:
360 		o = cmd_set_option_number(self, item, oe, oo, value);
361 		break;
362 	case OPTIONS_TABLE_KEY:
363 		o = cmd_set_option_key(self, item, oe, oo, value);
364 		break;
365 	case OPTIONS_TABLE_COLOUR:
366 		o = cmd_set_option_colour(self, item, oe, oo, value);
367 		if (o != NULL)
368 			style_update_new(oo, o->name, oe->style);
369 		break;
370 	case OPTIONS_TABLE_ATTRIBUTES:
371 		o = cmd_set_option_attributes(self, item, oe, oo, value);
372 		if (o != NULL)
373 			style_update_new(oo, o->name, oe->style);
374 		break;
375 	case OPTIONS_TABLE_FLAG:
376 		o = cmd_set_option_flag(self, item, oe, oo, value);
377 		break;
378 	case OPTIONS_TABLE_CHOICE:
379 		o = cmd_set_option_choice(self, item, oe, oo, value);
380 		break;
381 	case OPTIONS_TABLE_STYLE:
382 		o = cmd_set_option_style(self, item, oe, oo, value);
383 		break;
384 	}
385 	if (o == NULL)
386 		return (-1);
387 	return (0);
388 }
389 
390 /* Set a string option. */
391 static struct options_entry *
392 cmd_set_option_string(struct cmd *self, __unused struct cmdq_item *item,
393     const struct options_table_entry *oe, struct options *oo,
394     const char *value)
395 {
396 	struct args		*args = self->args;
397 	struct options_entry	*o;
398 	char			*oldval, *newval;
399 
400 	if (args_has(args, 'a')) {
401 		oldval = options_get_string(oo, oe->name);
402 		xasprintf(&newval, "%s%s", oldval, value);
403 	} else
404 		newval = xstrdup(value);
405 
406 	o = options_set_string(oo, oe->name, "%s", newval);
407 
408 	free(newval);
409 	return (o);
410 }
411 
412 /* Set a number option. */
413 static struct options_entry *
414 cmd_set_option_number(__unused struct cmd *self, struct cmdq_item *item,
415     const struct options_table_entry *oe, struct options *oo,
416     const char *value)
417 {
418 	long long	 ll;
419 	const char     	*errstr;
420 
421 	ll = strtonum(value, oe->minimum, oe->maximum, &errstr);
422 	if (errstr != NULL) {
423 		cmdq_error(item, "value is %s: %s", errstr, value);
424 		return (NULL);
425 	}
426 
427 	return (options_set_number(oo, oe->name, ll));
428 }
429 
430 /* Set a key option. */
431 static struct options_entry *
432 cmd_set_option_key(__unused struct cmd *self, struct cmdq_item *item,
433     const struct options_table_entry *oe, struct options *oo,
434     const char *value)
435 {
436 	key_code	key;
437 
438 	key = key_string_lookup_string(value);
439 	if (key == KEYC_UNKNOWN) {
440 		cmdq_error(item, "bad key: %s", value);
441 		return (NULL);
442 	}
443 
444 	return (options_set_number(oo, oe->name, key));
445 }
446 
447 /* Set a colour option. */
448 static struct options_entry *
449 cmd_set_option_colour(__unused struct cmd *self, struct cmdq_item *item,
450     const struct options_table_entry *oe, struct options *oo,
451     const char *value)
452 {
453 	int	colour;
454 
455 	if ((colour = colour_fromstring(value)) == -1) {
456 		cmdq_error(item, "bad colour: %s", value);
457 		return (NULL);
458 	}
459 
460 	return (options_set_number(oo, oe->name, colour));
461 }
462 
463 /* Set an attributes option. */
464 static struct options_entry *
465 cmd_set_option_attributes(__unused struct cmd *self, struct cmdq_item *item,
466     const struct options_table_entry *oe, struct options *oo,
467     const char *value)
468 {
469 	int	attr;
470 
471 	if ((attr = attributes_fromstring(value)) == -1) {
472 		cmdq_error(item, "bad attributes: %s", value);
473 		return (NULL);
474 	}
475 
476 	return (options_set_number(oo, oe->name, attr));
477 }
478 
479 /* Set a flag option. */
480 static struct options_entry *
481 cmd_set_option_flag(__unused struct cmd *self, struct cmdq_item *item,
482     const struct options_table_entry *oe, struct options *oo,
483     const char *value)
484 {
485 	int	flag;
486 
487 	if (value == NULL || *value == '\0')
488 		flag = !options_get_number(oo, oe->name);
489 	else {
490 		if ((value[0] == '1' && value[1] == '\0') ||
491 		    strcasecmp(value, "on") == 0 ||
492 		    strcasecmp(value, "yes") == 0)
493 			flag = 1;
494 		else if ((value[0] == '0' && value[1] == '\0') ||
495 		    strcasecmp(value, "off") == 0 ||
496 		    strcasecmp(value, "no") == 0)
497 			flag = 0;
498 		else {
499 			cmdq_error(item, "bad value: %s", value);
500 			return (NULL);
501 		}
502 	}
503 
504 	return (options_set_number(oo, oe->name, flag));
505 }
506 
507 /* Set a choice option. */
508 static struct options_entry *
509 cmd_set_option_choice(__unused struct cmd *self, struct cmdq_item *item,
510     const struct options_table_entry *oe, struct options *oo,
511     const char *value)
512 {
513 	const char	**choicep;
514 	int		  n, choice = -1;
515 
516 	if (value == NULL) {
517 		choice = options_get_number(oo, oe->name);
518 		if (choice < 2)
519 			choice = !choice;
520 	} else {
521 		n = 0;
522 		for (choicep = oe->choices; *choicep != NULL; choicep++) {
523 			n++;
524 			if (strncmp(*choicep, value, strlen(value)) != 0)
525 				continue;
526 
527 			if (choice != -1) {
528 				cmdq_error(item, "ambiguous value: %s", value);
529 				return (NULL);
530 			}
531 			choice = n - 1;
532 		}
533 		if (choice == -1) {
534 			cmdq_error(item, "unknown value: %s", value);
535 			return (NULL);
536 		}
537 	}
538 
539 	return (options_set_number(oo, oe->name, choice));
540 }
541 
542 /* Set a style option. */
543 static struct options_entry *
544 cmd_set_option_style(struct cmd *self, struct cmdq_item *item,
545     const struct options_table_entry *oe, struct options *oo,
546     const char *value)
547 {
548 	struct args		*args = self->args;
549 	struct options_entry	*o;
550 	int			 append;
551 
552 	append = args_has(args, 'a');
553 	if ((o = options_set_style(oo, oe->name, value, append)) == NULL) {
554 		cmdq_error(item, "bad style: %s", value);
555 		return (NULL);
556 	}
557 
558 	style_update_old(oo, oe->name, &o->style);
559 	return (o);
560 }
561