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