Update man page for update-environment.
[tmux-openbsd.git] / cmd-set-option.c
blob684f1b0452e55332f43a9d50eb52e4fccecbdb6b
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
27 * Set an option.
30 int cmd_set_option_exec(struct cmd *, struct cmd_ctx *);
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 *);
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
60 const char *set_option_mode_keys_list[] = {
61 "emacs", "vi", NULL
63 const char *set_option_clock_mode_style_list[] = {
64 "12", "24", NULL
66 const char *set_option_status_keys_list[] = {
67 "emacs", "vi", NULL
69 const char *set_option_status_justify_list[] = {
70 "left", "centre", "right", NULL
72 const char *set_option_bell_action_list[] = {
73 "none", "any", "current", NULL
76 const struct set_option_entry set_option_table[] = {
77 { "escape-time", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
78 { "exit-unattached", SET_OPTION_FLAG, 0, 0, NULL },
79 { "quiet", SET_OPTION_FLAG, 0, 0, NULL },
80 { NULL, 0, 0, 0, NULL }
83 const struct set_option_entry set_session_option_table[] = {
84 { "base-index", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
85 { "bell-action", SET_OPTION_CHOICE, 0, 0, set_option_bell_action_list },
86 { "buffer-limit", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
87 { "default-command", SET_OPTION_STRING, 0, 0, NULL },
88 { "default-path", SET_OPTION_STRING, 0, 0, NULL },
89 { "default-shell", SET_OPTION_STRING, 0, 0, NULL },
90 { "default-terminal", SET_OPTION_STRING, 0, 0, NULL },
91 { "destroy-unattached", SET_OPTION_FLAG, 0, 0, NULL },
92 { "detach-on-destroy", SET_OPTION_FLAG, 0, 0, NULL },
93 { "display-panes-colour", SET_OPTION_COLOUR, 0, 0, NULL },
94 { "display-panes-active-colour", SET_OPTION_COLOUR, 0, 0, NULL },
95 { "display-panes-time", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
96 { "display-time", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
97 { "history-limit", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
98 { "lock-after-time", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
99 { "lock-command", SET_OPTION_STRING, 0, 0, NULL },
100 { "lock-server", SET_OPTION_FLAG, 0, 0, NULL },
101 { "message-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
102 { "message-bg", SET_OPTION_COLOUR, 0, 0, NULL },
103 { "message-fg", SET_OPTION_COLOUR, 0, 0, NULL },
104 { "message-limit", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
105 { "mouse-select-pane", SET_OPTION_FLAG, 0, 0, NULL },
106 { "pane-active-border-bg", SET_OPTION_COLOUR, 0, 0, NULL },
107 { "pane-active-border-fg", SET_OPTION_COLOUR, 0, 0, NULL },
108 { "pane-border-bg", SET_OPTION_COLOUR, 0, 0, NULL },
109 { "pane-border-fg", SET_OPTION_COLOUR, 0, 0, NULL },
110 { "prefix", SET_OPTION_KEYS, 0, 0, NULL },
111 { "repeat-time", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
112 { "set-remain-on-exit", SET_OPTION_FLAG, 0, 0, NULL },
113 { "set-titles", SET_OPTION_FLAG, 0, 0, NULL },
114 { "set-titles-string", SET_OPTION_STRING, 0, 0, NULL },
115 { "status", SET_OPTION_FLAG, 0, 0, NULL },
116 { "status-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
117 { "status-bg", SET_OPTION_COLOUR, 0, 0, NULL },
118 { "status-fg", SET_OPTION_COLOUR, 0, 0, NULL },
119 { "status-interval", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
120 { "status-justify",
121 SET_OPTION_CHOICE, 0, 0, set_option_status_justify_list },
122 { "status-keys", SET_OPTION_CHOICE, 0, 0, set_option_status_keys_list },
123 { "status-left", SET_OPTION_STRING, 0, 0, NULL },
124 { "status-left-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
125 { "status-left-bg", SET_OPTION_COLOUR, 0, 0, NULL },
126 { "status-left-fg", SET_OPTION_COLOUR, 0, 0, NULL },
127 { "status-left-length", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
128 { "status-right", SET_OPTION_STRING, 0, 0, NULL },
129 { "status-right-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
130 { "status-right-bg", SET_OPTION_COLOUR, 0, 0, NULL },
131 { "status-right-fg", SET_OPTION_COLOUR, 0, 0, NULL },
132 { "status-right-length", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
133 { "status-utf8", SET_OPTION_FLAG, 0, 0, NULL },
134 { "terminal-overrides", SET_OPTION_STRING, 0, 0, NULL },
135 { "update-environment", SET_OPTION_STRING, 0, 0, NULL },
136 { "visual-activity", SET_OPTION_FLAG, 0, 0, NULL },
137 { "visual-bell", SET_OPTION_FLAG, 0, 0, NULL },
138 { "visual-content", SET_OPTION_FLAG, 0, 0, NULL },
139 { NULL, 0, 0, 0, NULL }
142 const struct set_option_entry set_window_option_table[] = {
143 { "aggressive-resize", SET_OPTION_FLAG, 0, 0, NULL },
144 { "alternate-screen", SET_OPTION_FLAG, 0, 0, NULL },
145 { "automatic-rename", SET_OPTION_FLAG, 0, 0, NULL },
146 { "clock-mode-colour", SET_OPTION_COLOUR, 0, 0, NULL },
147 { "clock-mode-style",
148 SET_OPTION_CHOICE, 0, 0, set_option_clock_mode_style_list },
149 { "force-height", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
150 { "force-width", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
151 { "main-pane-height", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
152 { "main-pane-width", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
153 { "mode-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
154 { "mode-bg", SET_OPTION_COLOUR, 0, 0, NULL },
155 { "mode-fg", SET_OPTION_COLOUR, 0, 0, NULL },
156 { "mode-keys", SET_OPTION_CHOICE, 0, 0, set_option_mode_keys_list },
157 { "mode-mouse", SET_OPTION_FLAG, 0, 0, NULL },
158 { "monitor-activity", SET_OPTION_FLAG, 0, 0, NULL },
159 { "monitor-content", SET_OPTION_STRING, 0, 0, NULL },
160 { "remain-on-exit", SET_OPTION_FLAG, 0, 0, NULL },
161 { "synchronize-panes", SET_OPTION_FLAG, 0, 0, NULL },
162 { "utf8", SET_OPTION_FLAG, 0, 0, NULL },
163 { "window-status-alert-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
164 { "window-status-alert-bg", SET_OPTION_COLOUR, 0, 0, NULL },
165 { "window-status-alert-fg", SET_OPTION_COLOUR, 0, 0, NULL },
166 { "window-status-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
167 { "window-status-bg", SET_OPTION_COLOUR, 0, 0, NULL },
168 { "window-status-current-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
169 { "window-status-current-bg", SET_OPTION_COLOUR, 0, 0, NULL },
170 { "window-status-current-fg", SET_OPTION_COLOUR, 0, 0, NULL },
171 { "window-status-current-format", SET_OPTION_STRING, 0, 0, NULL },
172 { "window-status-fg", SET_OPTION_COLOUR, 0, 0, NULL },
173 { "window-status-format", SET_OPTION_STRING, 0, 0, NULL },
174 { "word-separators", SET_OPTION_STRING, 0, 0, NULL },
175 { "xterm-keys", SET_OPTION_FLAG, 0, 0, NULL },
176 { NULL, 0, 0, 0, NULL }
180 cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx)
182 struct cmd_target_data *data = self->data;
183 const struct set_option_entry *table;
184 struct session *s;
185 struct winlink *wl;
186 struct client *c;
187 struct options *oo;
188 const struct set_option_entry *entry, *opt;
189 struct jobs *jobs;
190 struct job *job, *nextjob;
191 u_int i;
192 int try_again;
194 if (cmd_check_flag(data->chflags, 's')) {
195 oo = &global_options;
196 table = set_option_table;
197 } else if (cmd_check_flag(data->chflags, 'w')) {
198 table = set_window_option_table;
199 if (cmd_check_flag(data->chflags, 'g'))
200 oo = &global_w_options;
201 else {
202 wl = cmd_find_window(ctx, data->target, NULL);
203 if (wl == NULL)
204 return (-1);
205 oo = &wl->window->options;
207 } else {
208 table = set_session_option_table;
209 if (cmd_check_flag(data->chflags, 'g'))
210 oo = &global_s_options;
211 else {
212 s = cmd_find_session(ctx, data->target);
213 if (s == NULL)
214 return (-1);
215 oo = &s->options;
219 if (*data->arg == '\0') {
220 ctx->error(ctx, "invalid option");
221 return (-1);
224 entry = NULL;
225 for (opt = table; opt->name != NULL; opt++) {
226 if (strncmp(opt->name, data->arg, strlen(data->arg)) != 0)
227 continue;
228 if (entry != NULL) {
229 ctx->error(ctx, "ambiguous option: %s", data->arg);
230 return (-1);
232 entry = opt;
234 /* Bail now if an exact match. */
235 if (strcmp(entry->name, data->arg) == 0)
236 break;
238 if (entry == NULL) {
239 ctx->error(ctx, "unknown option: %s", data->arg);
240 return (-1);
243 if (cmd_check_flag(data->chflags, 'u')) {
244 if (cmd_check_flag(data->chflags, 'g')) {
245 ctx->error(ctx,
246 "can't unset global option: %s", entry->name);
247 return (-1);
249 if (data->arg2 != NULL) {
250 ctx->error(ctx,
251 "value passed to unset option: %s", entry->name);
252 return (-1);
255 options_remove(oo, entry->name);
256 ctx->info(ctx, "unset option: %s", entry->name);
257 } else {
258 switch (entry->type) {
259 case SET_OPTION_STRING:
260 cmd_set_option_string(ctx, oo, entry,
261 data->arg2, cmd_check_flag(data->chflags, 'a'));
262 break;
263 case SET_OPTION_NUMBER:
264 cmd_set_option_number(ctx, oo, entry, data->arg2);
265 break;
266 case SET_OPTION_KEYS:
267 cmd_set_option_keys(ctx, oo, entry, data->arg2);
268 break;
269 case SET_OPTION_COLOUR:
270 cmd_set_option_colour(ctx, oo, entry, data->arg2);
271 break;
272 case SET_OPTION_ATTRIBUTES:
273 cmd_set_option_attributes(ctx, oo, entry, data->arg2);
274 break;
275 case SET_OPTION_FLAG:
276 cmd_set_option_flag(ctx, oo, entry, data->arg2);
277 break;
278 case SET_OPTION_CHOICE:
279 cmd_set_option_choice(ctx, oo, entry, data->arg2);
280 break;
284 recalculate_sizes();
285 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
286 c = ARRAY_ITEM(&clients, i);
287 if (c != NULL && c->session != NULL)
288 server_redraw_client(c);
292 * Special-case: kill all persistent jobs if status-left, status-right
293 * or set-titles-string have changed. Persistent jobs are only used by
294 * the status line at the moment so this works XXX.
296 if (strcmp(entry->name, "status-left") == 0 ||
297 strcmp(entry->name, "status-right") == 0 ||
298 strcmp(entry->name, "status") == 0 ||
299 strcmp(entry->name, "set-titles-string") == 0 ||
300 strcmp(entry->name, "window-status-format") == 0) {
301 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
302 c = ARRAY_ITEM(&clients, i);
303 if (c == NULL || c->session == NULL)
304 continue;
306 jobs = &c->status_jobs;
307 do {
308 try_again = 0;
309 job = RB_ROOT(jobs);
310 while (job != NULL) {
311 nextjob = RB_NEXT(jobs, jobs, job);
312 if (job->flags & JOB_PERSIST) {
313 job_remove(jobs, job);
314 try_again = 1;
315 break;
317 job = nextjob;
319 } while (try_again);
320 server_redraw_client(c);
324 return (0);
327 const char *
328 cmd_set_option_print(
329 const struct set_option_entry *entry, struct options_entry *o)
331 static char out[BUFSIZ];
332 const char *s;
333 struct keylist *keylist;
334 u_int i;
336 *out = '\0';
337 switch (entry->type) {
338 case SET_OPTION_STRING:
339 xsnprintf(out, sizeof out, "\"%s\"", o->str);
340 break;
341 case SET_OPTION_NUMBER:
342 xsnprintf(out, sizeof out, "%lld", o->num);
343 break;
344 case SET_OPTION_KEYS:
345 keylist = o->data;
346 for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
347 strlcat(out, key_string_lookup_key(
348 ARRAY_ITEM(keylist, i)), sizeof out);
349 if (i != ARRAY_LENGTH(keylist) - 1)
350 strlcat(out, ",", sizeof out);
352 break;
353 case SET_OPTION_COLOUR:
354 s = colour_tostring(o->num);
355 xsnprintf(out, sizeof out, "%s", s);
356 break;
357 case SET_OPTION_ATTRIBUTES:
358 s = attributes_tostring(o->num);
359 xsnprintf(out, sizeof out, "%s", s);
360 break;
361 case SET_OPTION_FLAG:
362 if (o->num)
363 strlcpy(out, "on", sizeof out);
364 else
365 strlcpy(out, "off", sizeof out);
366 break;
367 case SET_OPTION_CHOICE:
368 s = entry->choices[o->num];
369 xsnprintf(out, sizeof out, "%s", s);
370 break;
372 return (out);
375 void
376 cmd_set_option_string(struct cmd_ctx *ctx, struct options *oo,
377 const struct set_option_entry *entry, char *value, int append)
379 struct options_entry *o;
380 char *oldvalue, *newvalue;
382 if (value == NULL) {
383 ctx->error(ctx, "empty value");
384 return;
387 if (append) {
388 oldvalue = options_get_string(oo, entry->name);
389 xasprintf(&newvalue, "%s%s", oldvalue, value);
390 } else
391 newvalue = value;
393 o = options_set_string(oo, entry->name, "%s", newvalue);
394 ctx->info(ctx,
395 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
397 if (newvalue != value)
398 xfree(newvalue);
401 void
402 cmd_set_option_number(struct cmd_ctx *ctx, struct options *oo,
403 const struct set_option_entry *entry, char *value)
405 struct options_entry *o;
406 long long number;
407 const char *errstr;
409 if (value == NULL) {
410 ctx->error(ctx, "empty value");
411 return;
414 number = strtonum(value, entry->minimum, entry->maximum, &errstr);
415 if (errstr != NULL) {
416 ctx->error(ctx, "value is %s: %s", errstr, value);
417 return;
420 o = options_set_number(oo, entry->name, number);
421 ctx->info(ctx,
422 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
425 void
426 cmd_set_option_keys(struct cmd_ctx *ctx, struct options *oo,
427 const struct set_option_entry *entry, char *value)
429 struct options_entry *o;
430 struct keylist *keylist;
431 char *copyvalue, *ptr, *str;
432 int key;
434 if (value == NULL) {
435 ctx->error(ctx, "empty value");
436 return;
439 keylist = xmalloc(sizeof *keylist);
440 ARRAY_INIT(keylist);
442 ptr = copyvalue = xstrdup(value);
443 while ((str = strsep(&ptr, ",")) != NULL) {
444 if ((key = key_string_lookup_string(str)) == KEYC_NONE) {
445 xfree(keylist);
446 ctx->error(ctx, "unknown key: %s", str);
447 xfree(copyvalue);
448 return;
450 ARRAY_ADD(keylist, key);
452 xfree(copyvalue);
454 o = options_set_data(oo, entry->name, keylist, xfree);
455 ctx->info(ctx,
456 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
459 void
460 cmd_set_option_colour(struct cmd_ctx *ctx, struct options *oo,
461 const struct set_option_entry *entry, char *value)
463 struct options_entry *o;
464 int colour;
466 if (value == NULL) {
467 ctx->error(ctx, "empty value");
468 return;
471 if ((colour = colour_fromstring(value)) == -1) {
472 ctx->error(ctx, "bad colour: %s", value);
473 return;
476 o = options_set_number(oo, entry->name, colour);
477 ctx->info(ctx,
478 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
481 void
482 cmd_set_option_attributes(struct cmd_ctx *ctx, struct options *oo,
483 const struct set_option_entry *entry, char *value)
485 struct options_entry *o;
486 int attr;
488 if (value == NULL) {
489 ctx->error(ctx, "empty value");
490 return;
493 if ((attr = attributes_fromstring(value)) == -1) {
494 ctx->error(ctx, "bad attributes: %s", value);
495 return;
498 o = options_set_number(oo, entry->name, attr);
499 ctx->info(ctx,
500 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
503 void
504 cmd_set_option_flag(struct cmd_ctx *ctx, struct options *oo,
505 const struct set_option_entry *entry, char *value)
507 struct options_entry *o;
508 int flag;
510 if (value == NULL || *value == '\0')
511 flag = !options_get_number(oo, entry->name);
512 else {
513 if ((value[0] == '1' && value[1] == '\0') ||
514 strcasecmp(value, "on") == 0 ||
515 strcasecmp(value, "yes") == 0)
516 flag = 1;
517 else if ((value[0] == '0' && value[1] == '\0') ||
518 strcasecmp(value, "off") == 0 ||
519 strcasecmp(value, "no") == 0)
520 flag = 0;
521 else {
522 ctx->error(ctx, "bad value: %s", value);
523 return;
527 o = options_set_number(oo, entry->name, flag);
528 ctx->info(ctx,
529 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
532 void
533 cmd_set_option_choice(struct cmd_ctx *ctx, struct options *oo,
534 const struct set_option_entry *entry, char *value)
536 struct options_entry *o;
537 const char **choicep;
538 int n, choice = -1;
540 if (value == NULL) {
541 ctx->error(ctx, "empty value");
542 return;
545 n = 0;
546 for (choicep = entry->choices; *choicep != NULL; choicep++) {
547 n++;
548 if (strncmp(*choicep, value, strlen(value)) != 0)
549 continue;
551 if (choice != -1) {
552 ctx->error(ctx, "ambiguous option value: %s", value);
553 return;
555 choice = n - 1;
557 if (choice == -1) {
558 ctx->error(ctx, "unknown option value: %s", value);
559 return;
562 o = options_set_number(oo, entry->name, choice);
563 ctx->info(ctx,
564 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));