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