xref: /openbsd-src/usr.bin/tmux/cmd-set-option.c (revision 9d6f7ac56963ac8e6b7a525ad4f9cfe0cacafb32)
1 /* $OpenBSD: cmd-set-option.c,v 1.33 2010/02/04 18:20:16 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 int	cmd_set_option_exec(struct cmd *, struct cmd_ctx *);
31 
32 const char *cmd_set_option_print(
33 	    const struct set_option_entry *, struct options_entry *);
34 void	cmd_set_option_string(struct cmd_ctx *,
35 	    struct options *, const struct set_option_entry *, char *, int);
36 void	cmd_set_option_number(struct cmd_ctx *,
37 	    struct options *, const struct set_option_entry *, char *);
38 void	cmd_set_option_keys(struct cmd_ctx *,
39 	    struct options *, const struct set_option_entry *, char *);
40 void	cmd_set_option_colour(struct cmd_ctx *,
41 	    struct options *, const struct set_option_entry *, char *);
42 void	cmd_set_option_attributes(struct cmd_ctx *,
43 	    struct options *, const struct set_option_entry *, char *);
44 void	cmd_set_option_flag(struct cmd_ctx *,
45 	    struct options *, const struct set_option_entry *, char *);
46 void	cmd_set_option_choice(struct cmd_ctx *,
47 	    struct options *, const struct set_option_entry *, char *);
48 
49 const struct cmd_entry cmd_set_option_entry = {
50 	"set-option", "set",
51 	"[-agsuw] [-t target-session|target-window] option [value]",
52 	CMD_ARG12, "agsuw",
53 	NULL,
54 	cmd_target_parse,
55 	cmd_set_option_exec,
56 	cmd_target_free,
57 	cmd_target_print
58 };
59 
60 const char *set_option_mode_keys_list[] = {
61 	"emacs", "vi", NULL
62 };
63 const char *set_option_clock_mode_style_list[] = {
64 	"12", "24", NULL
65 };
66 const char *set_option_status_keys_list[] = {
67 	"emacs", "vi", NULL
68 };
69 const char *set_option_status_justify_list[] = {
70 	"left", "centre", "right", NULL
71 };
72 const char *set_option_bell_action_list[] = {
73 	"none", "any", "current", NULL
74 };
75 
76 const struct set_option_entry set_option_table[] = {
77 	{ "escape-time", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
78 	{ "quiet", SET_OPTION_FLAG, 0, 0, NULL },
79 	{ NULL, 0, 0, 0, NULL }
80 };
81 
82 const struct set_option_entry set_session_option_table[] = {
83 	{ "base-index", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
84 	{ "bell-action", SET_OPTION_CHOICE, 0, 0, set_option_bell_action_list },
85 	{ "buffer-limit", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
86 	{ "default-command", SET_OPTION_STRING, 0, 0, NULL },
87 	{ "default-path", SET_OPTION_STRING, 0, 0, NULL },
88 	{ "default-shell", SET_OPTION_STRING, 0, 0, NULL },
89 	{ "default-terminal", SET_OPTION_STRING, 0, 0, NULL },
90 	{ "display-panes-colour", SET_OPTION_COLOUR, 0, 0, NULL },
91 	{ "display-panes-active-colour", SET_OPTION_COLOUR, 0, 0, NULL },
92 	{ "display-panes-time", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
93 	{ "display-time", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
94 	{ "history-limit", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
95 	{ "lock-after-time", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
96 	{ "lock-command", SET_OPTION_STRING, 0, 0, NULL },
97 	{ "lock-server", SET_OPTION_FLAG, 0, 0, NULL },
98 	{ "message-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
99 	{ "message-bg", SET_OPTION_COLOUR, 0, 0, NULL },
100 	{ "message-fg", SET_OPTION_COLOUR, 0, 0, NULL },
101 	{ "message-limit", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
102 	{ "mouse-select-pane", SET_OPTION_FLAG, 0, 0, NULL },
103 	{ "pane-active-border-bg", SET_OPTION_COLOUR, 0, 0, NULL },
104 	{ "pane-active-border-fg", SET_OPTION_COLOUR, 0, 0, NULL },
105 	{ "pane-border-bg", SET_OPTION_COLOUR, 0, 0, NULL },
106 	{ "pane-border-fg", SET_OPTION_COLOUR, 0, 0, NULL },
107 	{ "prefix", SET_OPTION_KEYS, 0, 0, NULL },
108 	{ "repeat-time", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
109 	{ "set-remain-on-exit", SET_OPTION_FLAG, 0, 0, NULL },
110 	{ "set-titles", SET_OPTION_FLAG, 0, 0, NULL },
111 	{ "set-titles-string", SET_OPTION_STRING, 0, 0, NULL },
112 	{ "status", SET_OPTION_FLAG, 0, 0, NULL },
113 	{ "status-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
114 	{ "status-bg", SET_OPTION_COLOUR, 0, 0, NULL },
115 	{ "status-fg", SET_OPTION_COLOUR, 0, 0, NULL },
116 	{ "status-interval", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
117 	{ "status-justify",
118 	  SET_OPTION_CHOICE, 0, 0, set_option_status_justify_list },
119 	{ "status-keys", SET_OPTION_CHOICE, 0, 0, set_option_status_keys_list },
120 	{ "status-left", SET_OPTION_STRING, 0, 0, NULL },
121 	{ "status-left-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
122 	{ "status-left-bg", SET_OPTION_COLOUR, 0, 0, NULL },
123 	{ "status-left-fg", SET_OPTION_COLOUR, 0, 0, NULL },
124 	{ "status-left-length", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
125 	{ "status-right", SET_OPTION_STRING, 0, 0, NULL },
126 	{ "status-right-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
127 	{ "status-right-bg", SET_OPTION_COLOUR, 0, 0, NULL },
128 	{ "status-right-fg", SET_OPTION_COLOUR, 0, 0, NULL },
129 	{ "status-right-length", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
130 	{ "status-utf8", SET_OPTION_FLAG, 0, 0, NULL },
131 	{ "terminal-overrides", SET_OPTION_STRING, 0, 0, NULL },
132 	{ "update-environment", SET_OPTION_STRING, 0, 0, NULL },
133 	{ "visual-activity", SET_OPTION_FLAG, 0, 0, NULL },
134 	{ "visual-bell", SET_OPTION_FLAG, 0, 0, NULL },
135 	{ "visual-content", SET_OPTION_FLAG, 0, 0, NULL },
136 	{ NULL, 0, 0, 0, NULL }
137 };
138 
139 const struct set_option_entry set_window_option_table[] = {
140 	{ "aggressive-resize", SET_OPTION_FLAG, 0, 0, NULL },
141 	{ "automatic-rename", SET_OPTION_FLAG, 0, 0, NULL },
142 	{ "clock-mode-colour", SET_OPTION_COLOUR, 0, 0, NULL },
143 	{ "clock-mode-style",
144 	  SET_OPTION_CHOICE, 0, 0, set_option_clock_mode_style_list },
145 	{ "force-height", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
146 	{ "force-width", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
147 	{ "main-pane-height", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
148 	{ "main-pane-width", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
149 	{ "mode-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
150 	{ "mode-bg", SET_OPTION_COLOUR, 0, 0, NULL },
151 	{ "mode-fg", SET_OPTION_COLOUR, 0, 0, NULL },
152 	{ "mode-keys", SET_OPTION_CHOICE, 0, 0, set_option_mode_keys_list },
153 	{ "mode-mouse", SET_OPTION_FLAG, 0, 0, NULL },
154 	{ "monitor-activity", SET_OPTION_FLAG, 0, 0, NULL },
155 	{ "monitor-content", SET_OPTION_STRING, 0, 0, NULL },
156 	{ "remain-on-exit", SET_OPTION_FLAG, 0, 0, NULL },
157 	{ "synchronize-panes", SET_OPTION_FLAG, 0, 0, NULL },
158 	{ "utf8", SET_OPTION_FLAG, 0, 0, NULL },
159 	{ "window-status-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
160 	{ "window-status-bg", SET_OPTION_COLOUR, 0, 0, NULL },
161 	{ "window-status-current-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
162 	{ "window-status-current-bg", SET_OPTION_COLOUR, 0, 0, NULL },
163 	{ "window-status-current-fg", SET_OPTION_COLOUR, 0, 0, NULL },
164 	{ "window-status-current-format", SET_OPTION_STRING, 0, 0, NULL },
165 	{ "window-status-fg", SET_OPTION_COLOUR, 0, 0, NULL },
166 	{ "window-status-format", SET_OPTION_STRING, 0, 0, NULL },
167 	{ "xterm-keys", SET_OPTION_FLAG, 0, 0, NULL },
168 	{ NULL, 0, 0, 0, NULL }
169 };
170 
171 int
172 cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx)
173 {
174 	struct cmd_target_data		*data = self->data;
175 	const struct set_option_entry	*table;
176 	struct session			*s;
177 	struct winlink			*wl;
178 	struct client			*c;
179 	struct options			*oo;
180 	const struct set_option_entry   *entry, *opt;
181 	struct jobs			*jobs;
182 	struct job			*job, *nextjob;
183 	u_int				 i;
184 	int				 try_again;
185 
186 	if (cmd_check_flag(data->chflags, 's')) {
187 		oo = &global_options;
188 		table = set_option_table;
189 	} else if (cmd_check_flag(data->chflags, 'w')) {
190 		table = set_window_option_table;
191 		if (cmd_check_flag(data->chflags, 'g'))
192 			oo = &global_w_options;
193 		else {
194 			wl = cmd_find_window(ctx, data->target, NULL);
195 			if (wl == NULL)
196 				return (-1);
197 			oo = &wl->window->options;
198 		}
199 	} else {
200 		table = set_session_option_table;
201 		if (cmd_check_flag(data->chflags, 'g'))
202 			oo = &global_s_options;
203 		else {
204 			s = cmd_find_session(ctx, data->target);
205 			if (s == NULL)
206 				return (-1);
207 			oo = &s->options;
208 		}
209 	}
210 
211 	if (*data->arg == '\0') {
212 		ctx->error(ctx, "invalid option");
213 		return (-1);
214 	}
215 
216 	entry = NULL;
217 	for (opt = table; opt->name != NULL; opt++) {
218 		if (strncmp(opt->name, data->arg, strlen(data->arg)) != 0)
219 			continue;
220 		if (entry != NULL) {
221 			ctx->error(ctx, "ambiguous option: %s", data->arg);
222 			return (-1);
223 		}
224 		entry = opt;
225 
226 		/* Bail now if an exact match. */
227 		if (strcmp(entry->name, data->arg) == 0)
228 			break;
229 	}
230 	if (entry == NULL) {
231 		ctx->error(ctx, "unknown option: %s", data->arg);
232 		return (-1);
233 	}
234 
235 	if (cmd_check_flag(data->chflags, 'u')) {
236 		if (cmd_check_flag(data->chflags, 'g')) {
237 			ctx->error(ctx,
238 			    "can't unset global option: %s", entry->name);
239 			return (-1);
240 		}
241 		if (data->arg2 != NULL) {
242 			ctx->error(ctx,
243 			    "value passed to unset option: %s", entry->name);
244 			return (-1);
245 		}
246 
247 		options_remove(oo, entry->name);
248 		ctx->info(ctx, "unset option: %s", entry->name);
249 	} else {
250 		switch (entry->type) {
251 		case SET_OPTION_STRING:
252 			cmd_set_option_string(ctx, oo, entry,
253 			    data->arg2, cmd_check_flag(data->chflags, 'a'));
254 			break;
255 		case SET_OPTION_NUMBER:
256 			cmd_set_option_number(ctx, oo, entry, data->arg2);
257 			break;
258 		case SET_OPTION_KEYS:
259 			cmd_set_option_keys(ctx, oo, entry, data->arg2);
260 			break;
261 		case SET_OPTION_COLOUR:
262 			cmd_set_option_colour(ctx, oo, entry, data->arg2);
263 			break;
264 		case SET_OPTION_ATTRIBUTES:
265 			cmd_set_option_attributes(ctx, oo, entry, data->arg2);
266 			break;
267 		case SET_OPTION_FLAG:
268 			cmd_set_option_flag(ctx, oo, entry, data->arg2);
269 			break;
270 		case SET_OPTION_CHOICE:
271 			cmd_set_option_choice(ctx, oo, entry, data->arg2);
272 			break;
273 		}
274 	}
275 
276 	recalculate_sizes();
277 	for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
278 		c = ARRAY_ITEM(&clients, i);
279 		if (c != NULL && c->session != NULL)
280 			server_redraw_client(c);
281 	}
282 
283 	/*
284 	 * Special-case: kill all persistent jobs if status-left, status-right
285 	 * or set-titles-string have changed. Persistent jobs are only used by
286 	 * the status line at the moment so this works XXX.
287 	 */
288 	if (strcmp(entry->name, "status-left") == 0 ||
289 	    strcmp(entry->name, "status-right") == 0 ||
290 	    strcmp(entry->name, "set-titles-string") == 0 ||
291 	    strcmp(entry->name, "window-status-format") == 0) {
292 		for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
293 			c = ARRAY_ITEM(&clients, i);
294 			if (c == NULL || c->session == NULL)
295 				continue;
296 
297 			jobs = &c->status_jobs;
298 			do {
299 				try_again = 0;
300 				job = RB_ROOT(jobs);
301 				while (job != NULL) {
302 					nextjob = RB_NEXT(jobs, jobs, job);
303 					if (job->flags & JOB_PERSIST) {
304 						job_remove(jobs, job);
305 						try_again = 1;
306 						break;
307 					}
308 					job = nextjob;
309 				}
310 			} while (try_again);
311 			server_redraw_client(c);
312 		}
313 	}
314 
315 	return (0);
316 }
317 
318 const char *
319 cmd_set_option_print(
320     const struct set_option_entry *entry, struct options_entry *o)
321 {
322 	static char	out[BUFSIZ];
323 	const char     *s;
324 	struct keylist *keylist;
325 	u_int		i;
326 
327 	*out = '\0';
328 	switch (entry->type) {
329 		case SET_OPTION_STRING:
330 			xsnprintf(out, sizeof out, "\"%s\"", o->str);
331 			break;
332 		case SET_OPTION_NUMBER:
333 			xsnprintf(out, sizeof out, "%lld", o->num);
334 			break;
335 		case SET_OPTION_KEYS:
336 			keylist = o->data;
337 			for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
338 				strlcat(out, key_string_lookup_key(
339 				    ARRAY_ITEM(keylist, i)), sizeof out);
340 				if (i != ARRAY_LENGTH(keylist) - 1)
341 					strlcat(out, ",", sizeof out);
342 			}
343 			break;
344 		case SET_OPTION_COLOUR:
345 			s = colour_tostring(o->num);
346 			xsnprintf(out, sizeof out, "%s", s);
347 			break;
348 		case SET_OPTION_ATTRIBUTES:
349 			s = attributes_tostring(o->num);
350 			xsnprintf(out, sizeof out, "%s", s);
351 			break;
352 		case SET_OPTION_FLAG:
353 			if (o->num)
354 				strlcpy(out, "on", sizeof out);
355 			else
356 				strlcpy(out, "off", sizeof out);
357 			break;
358 		case SET_OPTION_CHOICE:
359 			s = entry->choices[o->num];
360 			xsnprintf(out, sizeof out, "%s", s);
361 			break;
362 	}
363 	return (out);
364 }
365 
366 void
367 cmd_set_option_string(struct cmd_ctx *ctx, struct options *oo,
368     const struct set_option_entry *entry, char *value, int append)
369 {
370 	struct options_entry	*o;
371 	char			*oldvalue, *newvalue;
372 
373 	if (value == NULL) {
374 		ctx->error(ctx, "empty value");
375 		return;
376 	}
377 
378 	if (append) {
379 		oldvalue = options_get_string(oo, entry->name);
380 		xasprintf(&newvalue, "%s%s", oldvalue, value);
381 	} else
382 		newvalue = value;
383 
384 	o = options_set_string(oo, entry->name, "%s", newvalue);
385 	ctx->info(ctx,
386 	    "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
387 
388 	if (newvalue != value)
389 		xfree(newvalue);
390 }
391 
392 void
393 cmd_set_option_number(struct cmd_ctx *ctx, struct options *oo,
394     const struct set_option_entry *entry, char *value)
395 {
396 	struct options_entry	*o;
397 	long long		 number;
398 	const char     		*errstr;
399 
400 	if (value == NULL) {
401 		ctx->error(ctx, "empty value");
402 		return;
403 	}
404 
405 	number = strtonum(value, entry->minimum, entry->maximum, &errstr);
406 	if (errstr != NULL) {
407 		ctx->error(ctx, "value is %s: %s", errstr, value);
408 		return;
409 	}
410 
411 	o = options_set_number(oo, entry->name, number);
412 	ctx->info(ctx,
413 	    "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
414 }
415 
416 void
417 cmd_set_option_keys(struct cmd_ctx *ctx, struct options *oo,
418     const struct set_option_entry *entry, char *value)
419 {
420 	struct options_entry	*o;
421 	struct keylist		*keylist;
422 	char			*copyvalue, *ptr, *str;
423 	int		 	 key;
424 
425 	if (value == NULL) {
426 		ctx->error(ctx, "empty value");
427 		return;
428 	}
429 
430 	keylist = xmalloc(sizeof *keylist);
431 	ARRAY_INIT(keylist);
432 
433 	ptr = copyvalue = xstrdup(value);
434 	while ((str = strsep(&ptr, ",")) != NULL) {
435 		if ((key = key_string_lookup_string(str)) == KEYC_NONE) {
436 			xfree(keylist);
437 			ctx->error(ctx, "unknown key: %s", str);
438 			xfree(copyvalue);
439 			return;
440 		}
441 		ARRAY_ADD(keylist, key);
442 	}
443 	xfree(copyvalue);
444 
445 	o = options_set_data(oo, entry->name, keylist, xfree);
446 	ctx->info(ctx,
447 	    "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
448 }
449 
450 void
451 cmd_set_option_colour(struct cmd_ctx *ctx, struct options *oo,
452     const struct set_option_entry *entry, char *value)
453 {
454 	struct options_entry	*o;
455 	int			 colour;
456 
457 	if (value == NULL) {
458 		ctx->error(ctx, "empty value");
459 		return;
460 	}
461 
462 	if ((colour = colour_fromstring(value)) == -1) {
463 		ctx->error(ctx, "bad colour: %s", value);
464 		return;
465 	}
466 
467 	o = options_set_number(oo, entry->name, colour);
468 	ctx->info(ctx,
469 	    "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
470 }
471 
472 void
473 cmd_set_option_attributes(struct cmd_ctx *ctx, struct options *oo,
474     const struct set_option_entry *entry, char *value)
475 {
476 	struct options_entry	*o;
477 	int			 attr;
478 
479 	if (value == NULL) {
480 		ctx->error(ctx, "empty value");
481 		return;
482 	}
483 
484 	if ((attr = attributes_fromstring(value)) == -1) {
485 		ctx->error(ctx, "bad attributes: %s", value);
486 		return;
487 	}
488 
489 	o = options_set_number(oo, entry->name, attr);
490 	ctx->info(ctx,
491 	    "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
492 }
493 
494 void
495 cmd_set_option_flag(struct cmd_ctx *ctx, struct options *oo,
496     const struct set_option_entry *entry, char *value)
497 {
498 	struct options_entry	*o;
499 	int			 flag;
500 
501 	if (value == NULL || *value == '\0')
502 		flag = !options_get_number(oo, entry->name);
503 	else {
504 		if ((value[0] == '1' && value[1] == '\0') ||
505 		    strcasecmp(value, "on") == 0 ||
506 		    strcasecmp(value, "yes") == 0)
507 			flag = 1;
508 		else if ((value[0] == '0' && value[1] == '\0') ||
509 		    strcasecmp(value, "off") == 0 ||
510 		    strcasecmp(value, "no") == 0)
511 			flag = 0;
512 		else {
513 			ctx->error(ctx, "bad value: %s", value);
514 			return;
515 		}
516 	}
517 
518 	o = options_set_number(oo, entry->name, flag);
519 	ctx->info(ctx,
520 	    "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
521 }
522 
523 void
524 cmd_set_option_choice(struct cmd_ctx *ctx, struct options *oo,
525     const struct set_option_entry *entry, char *value)
526 {
527 	struct options_entry	*o;
528 	const char     	       **choicep;
529 	int		 	 n, choice = -1;
530 
531 	if (value == NULL) {
532 		ctx->error(ctx, "empty value");
533 		return;
534 	}
535 
536 	n = 0;
537 	for (choicep = entry->choices; *choicep != NULL; choicep++) {
538 		n++;
539 		if (strncmp(*choicep, value, strlen(value)) != 0)
540 			continue;
541 
542 		if (choice != -1) {
543 			ctx->error(ctx, "ambiguous option value: %s", value);
544 			return;
545 		}
546 		choice = n - 1;
547 	}
548 	if (choice == -1) {
549 		ctx->error(ctx, "unknown option value: %s", value);
550 		return;
551 	}
552 
553 	o = options_set_number(oo, entry->name, choice);
554 	ctx->info(ctx,
555 	    "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
556 }
557