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