1 /* $OpenBSD: format.c,v 1.151 2018/01/18 14:28:11 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com> 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 #include <sys/wait.h> 21 22 #include <errno.h> 23 #include <fnmatch.h> 24 #include <libgen.h> 25 #include <stdarg.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <time.h> 29 #include <unistd.h> 30 31 #include "tmux.h" 32 33 /* 34 * Build a list of key-value pairs and use them to expand #{key} entries in a 35 * string. 36 */ 37 38 struct format_entry; 39 typedef void (*format_cb)(struct format_tree *, struct format_entry *); 40 41 static char *format_job_get(struct format_tree *, const char *); 42 static void format_job_timer(int, short, void *); 43 44 static char *format_find(struct format_tree *, const char *, int); 45 static void format_add_cb(struct format_tree *, const char *, format_cb); 46 static void format_add_tv(struct format_tree *, const char *, 47 struct timeval *); 48 static int format_replace(struct format_tree *, const char *, size_t, 49 char **, size_t *, size_t *); 50 51 static void format_defaults_session(struct format_tree *, 52 struct session *); 53 static void format_defaults_client(struct format_tree *, struct client *); 54 static void format_defaults_winlink(struct format_tree *, 55 struct winlink *); 56 57 /* Entry in format job tree. */ 58 struct format_job { 59 struct client *client; 60 u_int tag; 61 const char *cmd; 62 const char *expanded; 63 64 time_t last; 65 char *out; 66 int updated; 67 68 struct job *job; 69 int status; 70 71 RB_ENTRY(format_job) entry; 72 }; 73 74 /* Format job tree. */ 75 static struct event format_job_event; 76 static int format_job_cmp(struct format_job *, struct format_job *); 77 static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER(); 78 RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp); 79 80 /* Format job tree comparison function. */ 81 static int 82 format_job_cmp(struct format_job *fj1, struct format_job *fj2) 83 { 84 if (fj1->tag < fj2->tag) 85 return (-1); 86 if (fj1->tag > fj2->tag) 87 return (1); 88 return (strcmp(fj1->cmd, fj2->cmd)); 89 } 90 91 /* Format modifiers. */ 92 #define FORMAT_TIMESTRING 0x1 93 #define FORMAT_BASENAME 0x2 94 #define FORMAT_DIRNAME 0x4 95 #define FORMAT_SUBSTITUTE 0x8 96 97 /* Entry in format tree. */ 98 struct format_entry { 99 char *key; 100 char *value; 101 time_t t; 102 format_cb cb; 103 RB_ENTRY(format_entry) entry; 104 }; 105 106 /* Format entry tree. */ 107 struct format_tree { 108 struct window *w; 109 struct winlink *wl; 110 struct session *s; 111 struct window_pane *wp; 112 113 struct client *client; 114 u_int tag; 115 int flags; 116 117 RB_HEAD(format_entry_tree, format_entry) tree; 118 }; 119 static int format_entry_cmp(struct format_entry *, struct format_entry *); 120 RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp); 121 122 /* Format entry tree comparison function. */ 123 static int 124 format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2) 125 { 126 return (strcmp(fe1->key, fe2->key)); 127 } 128 129 /* Single-character uppercase aliases. */ 130 static const char *format_upper[] = { 131 NULL, /* A */ 132 NULL, /* B */ 133 NULL, /* C */ 134 "pane_id", /* D */ 135 NULL, /* E */ 136 "window_flags", /* F */ 137 NULL, /* G */ 138 "host", /* H */ 139 "window_index", /* I */ 140 NULL, /* J */ 141 NULL, /* K */ 142 NULL, /* L */ 143 NULL, /* M */ 144 NULL, /* N */ 145 NULL, /* O */ 146 "pane_index", /* P */ 147 NULL, /* Q */ 148 NULL, /* R */ 149 "session_name", /* S */ 150 "pane_title", /* T */ 151 NULL, /* U */ 152 NULL, /* V */ 153 "window_name", /* W */ 154 NULL, /* X */ 155 NULL, /* Y */ 156 NULL /* Z */ 157 }; 158 159 /* Single-character lowercase aliases. */ 160 static const char *format_lower[] = { 161 NULL, /* a */ 162 NULL, /* b */ 163 NULL, /* c */ 164 NULL, /* d */ 165 NULL, /* e */ 166 NULL, /* f */ 167 NULL, /* g */ 168 "host_short", /* h */ 169 NULL, /* i */ 170 NULL, /* j */ 171 NULL, /* k */ 172 NULL, /* l */ 173 NULL, /* m */ 174 NULL, /* n */ 175 NULL, /* o */ 176 NULL, /* p */ 177 NULL, /* q */ 178 NULL, /* r */ 179 NULL, /* s */ 180 NULL, /* t */ 181 NULL, /* u */ 182 NULL, /* v */ 183 NULL, /* w */ 184 NULL, /* x */ 185 NULL, /* y */ 186 NULL /* z */ 187 }; 188 189 /* Format job update callback. */ 190 static void 191 format_job_update(struct job *job) 192 { 193 struct format_job *fj = job->data; 194 struct evbuffer *evb = job->event->input; 195 char *line = NULL, *next; 196 time_t t; 197 198 while ((next = evbuffer_readline(evb)) != NULL) { 199 free(line); 200 line = next; 201 } 202 if (line == NULL) 203 return; 204 fj->updated = 1; 205 206 free(fj->out); 207 fj->out = line; 208 209 log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out); 210 211 t = time(NULL); 212 if (fj->status && fj->last != t) { 213 if (fj->client != NULL) 214 server_status_client(fj->client); 215 fj->last = t; 216 } 217 } 218 219 /* Format job complete callback. */ 220 static void 221 format_job_complete(struct job *job) 222 { 223 struct format_job *fj = job->data; 224 char *line, *buf; 225 size_t len; 226 227 fj->job = NULL; 228 229 buf = NULL; 230 if ((line = evbuffer_readline(job->event->input)) == NULL) { 231 len = EVBUFFER_LENGTH(job->event->input); 232 buf = xmalloc(len + 1); 233 if (len != 0) 234 memcpy(buf, EVBUFFER_DATA(job->event->input), len); 235 buf[len] = '\0'; 236 } else 237 buf = line; 238 239 log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf); 240 241 if (*buf != '\0' || !fj->updated) { 242 free(fj->out); 243 fj->out = buf; 244 } else 245 free(buf); 246 247 if (fj->status) { 248 if (fj->client != NULL) 249 server_status_client(fj->client); 250 fj->status = 0; 251 } 252 } 253 254 /* Find a job. */ 255 static char * 256 format_job_get(struct format_tree *ft, const char *cmd) 257 { 258 struct format_job_tree *jobs; 259 struct format_job fj0, *fj; 260 time_t t; 261 char *expanded; 262 int force; 263 264 if (ft->client == NULL) 265 jobs = &format_jobs; 266 else if (ft->client->jobs != NULL) 267 jobs = ft->client->jobs; 268 else { 269 jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs); 270 RB_INIT(jobs); 271 } 272 273 fj0.tag = ft->tag; 274 fj0.cmd = cmd; 275 if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) { 276 fj = xcalloc(1, sizeof *fj); 277 fj->client = ft->client; 278 fj->tag = ft->tag; 279 fj->cmd = xstrdup(cmd); 280 fj->expanded = NULL; 281 282 xasprintf(&fj->out, "<'%s' not ready>", fj->cmd); 283 284 RB_INSERT(format_job_tree, jobs, fj); 285 } 286 287 expanded = format_expand(ft, cmd); 288 if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) { 289 free((void *)fj->expanded); 290 fj->expanded = xstrdup(expanded); 291 force = 1; 292 } else 293 force = (ft->flags & FORMAT_FORCE); 294 295 t = time(NULL); 296 if (fj->job == NULL && (force || fj->last != t)) { 297 fj->job = job_run(expanded, NULL, NULL, format_job_update, 298 format_job_complete, NULL, fj); 299 if (fj->job == NULL) { 300 free(fj->out); 301 xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd); 302 } 303 fj->last = t; 304 fj->updated = 0; 305 } 306 307 if (ft->flags & FORMAT_STATUS) 308 fj->status = 1; 309 310 free(expanded); 311 return (format_expand(ft, fj->out)); 312 } 313 314 /* Remove old jobs. */ 315 static void 316 format_job_tidy(struct format_job_tree *jobs, int force) 317 { 318 struct format_job *fj, *fj1; 319 time_t now; 320 321 now = time(NULL); 322 RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) { 323 if (!force && (fj->last > now || now - fj->last < 3600)) 324 continue; 325 RB_REMOVE(format_job_tree, jobs, fj); 326 327 log_debug("%s: %s", __func__, fj->cmd); 328 329 if (fj->job != NULL) 330 job_free(fj->job); 331 332 free((void *)fj->expanded); 333 free((void *)fj->cmd); 334 free(fj->out); 335 336 free(fj); 337 } 338 } 339 340 /* Remove old jobs for client. */ 341 void 342 format_lost_client(struct client *c) 343 { 344 if (c->jobs != NULL) 345 format_job_tidy(c->jobs, 1); 346 free(c->jobs); 347 } 348 349 /* Remove old jobs periodically. */ 350 static void 351 format_job_timer(__unused int fd, __unused short events, __unused void *arg) 352 { 353 struct client *c; 354 struct timeval tv = { .tv_sec = 60 }; 355 356 format_job_tidy(&format_jobs, 0); 357 TAILQ_FOREACH(c, &clients, entry) { 358 if (c->jobs != NULL) 359 format_job_tidy(c->jobs, 0); 360 } 361 362 evtimer_del(&format_job_event); 363 evtimer_add(&format_job_event, &tv); 364 } 365 366 /* Callback for host. */ 367 static void 368 format_cb_host(__unused struct format_tree *ft, struct format_entry *fe) 369 { 370 char host[HOST_NAME_MAX + 1]; 371 372 if (gethostname(host, sizeof host) != 0) 373 fe->value = xstrdup(""); 374 else 375 fe->value = xstrdup(host); 376 } 377 378 /* Callback for host_short. */ 379 static void 380 format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe) 381 { 382 char host[HOST_NAME_MAX + 1], *cp; 383 384 if (gethostname(host, sizeof host) != 0) 385 fe->value = xstrdup(""); 386 else { 387 if ((cp = strchr(host, '.')) != NULL) 388 *cp = '\0'; 389 fe->value = xstrdup(host); 390 } 391 } 392 393 /* Callback for pid. */ 394 static void 395 format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe) 396 { 397 xasprintf(&fe->value, "%ld", (long)getpid()); 398 } 399 400 /* Callback for session_alerts. */ 401 static void 402 format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe) 403 { 404 struct session *s = ft->s; 405 struct winlink *wl; 406 char alerts[1024], tmp[16]; 407 408 if (s == NULL) 409 return; 410 411 *alerts = '\0'; 412 RB_FOREACH(wl, winlinks, &s->windows) { 413 if ((wl->flags & WINLINK_ALERTFLAGS) == 0) 414 continue; 415 xsnprintf(tmp, sizeof tmp, "%u", wl->idx); 416 417 if (*alerts != '\0') 418 strlcat(alerts, ",", sizeof alerts); 419 strlcat(alerts, tmp, sizeof alerts); 420 if (wl->flags & WINLINK_ACTIVITY) 421 strlcat(alerts, "#", sizeof alerts); 422 if (wl->flags & WINLINK_BELL) 423 strlcat(alerts, "!", sizeof alerts); 424 if (wl->flags & WINLINK_SILENCE) 425 strlcat(alerts, "~", sizeof alerts); 426 } 427 fe->value = xstrdup(alerts); 428 } 429 430 /* Callback for session_stack. */ 431 static void 432 format_cb_session_stack(struct format_tree *ft, struct format_entry *fe) 433 { 434 struct session *s = ft->s; 435 struct winlink *wl; 436 char result[1024], tmp[16]; 437 438 if (s == NULL) 439 return; 440 441 xsnprintf(result, sizeof result, "%u", s->curw->idx); 442 TAILQ_FOREACH(wl, &s->lastw, sentry) { 443 xsnprintf(tmp, sizeof tmp, "%u", wl->idx); 444 445 if (*result != '\0') 446 strlcat(result, ",", sizeof result); 447 strlcat(result, tmp, sizeof result); 448 } 449 fe->value = xstrdup(result); 450 } 451 452 /* Callback for window_stack_index. */ 453 static void 454 format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe) 455 { 456 struct session *s = ft->wl->session; 457 struct winlink *wl; 458 u_int idx; 459 460 idx = 0; 461 TAILQ_FOREACH(wl, &s->lastw, sentry) { 462 idx++; 463 if (wl == ft->wl) 464 break; 465 } 466 if (wl != NULL) 467 xasprintf(&fe->value, "%u", idx); 468 else 469 fe->value = xstrdup("0"); 470 } 471 472 /* Callback for window_layout. */ 473 static void 474 format_cb_window_layout(struct format_tree *ft, struct format_entry *fe) 475 { 476 struct window *w = ft->w; 477 478 if (w == NULL) 479 return; 480 481 if (w->saved_layout_root != NULL) 482 fe->value = layout_dump(w->saved_layout_root); 483 else 484 fe->value = layout_dump(w->layout_root); 485 } 486 487 /* Callback for window_visible_layout. */ 488 static void 489 format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe) 490 { 491 struct window *w = ft->w; 492 493 if (w == NULL) 494 return; 495 496 fe->value = layout_dump(w->layout_root); 497 } 498 499 /* Callback for pane_start_command. */ 500 static void 501 format_cb_start_command(struct format_tree *ft, struct format_entry *fe) 502 { 503 struct window_pane *wp = ft->wp; 504 505 if (wp == NULL) 506 return; 507 508 fe->value = cmd_stringify_argv(wp->argc, wp->argv); 509 } 510 511 /* Callback for pane_current_command. */ 512 static void 513 format_cb_current_command(struct format_tree *ft, struct format_entry *fe) 514 { 515 struct window_pane *wp = ft->wp; 516 char *cmd; 517 518 if (wp == NULL) 519 return; 520 521 cmd = get_proc_name(wp->fd, wp->tty); 522 if (cmd == NULL || *cmd == '\0') { 523 free(cmd); 524 cmd = cmd_stringify_argv(wp->argc, wp->argv); 525 if (cmd == NULL || *cmd == '\0') { 526 free(cmd); 527 cmd = xstrdup(wp->shell); 528 } 529 } 530 fe->value = parse_window_name(cmd); 531 free(cmd); 532 } 533 534 /* Callback for history_bytes. */ 535 static void 536 format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe) 537 { 538 struct window_pane *wp = ft->wp; 539 struct grid *gd; 540 struct grid_line *gl; 541 unsigned long long size; 542 u_int i; 543 544 if (wp == NULL) 545 return; 546 gd = wp->base.grid; 547 548 size = 0; 549 for (i = 0; i < gd->hsize; i++) { 550 gl = &gd->linedata[i]; 551 size += gl->cellsize * sizeof *gl->celldata; 552 size += gl->extdsize * sizeof *gl->extddata; 553 } 554 size += gd->hsize * sizeof *gd->linedata; 555 556 xasprintf(&fe->value, "%llu", size); 557 } 558 559 /* Callback for pane_tabs. */ 560 static void 561 format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe) 562 { 563 struct window_pane *wp = ft->wp; 564 struct evbuffer *buffer; 565 u_int i; 566 int size; 567 568 if (wp == NULL) 569 return; 570 571 buffer = evbuffer_new(); 572 for (i = 0; i < wp->base.grid->sx; i++) { 573 if (!bit_test(wp->base.tabs, i)) 574 continue; 575 576 if (EVBUFFER_LENGTH(buffer) > 0) 577 evbuffer_add(buffer, ",", 1); 578 evbuffer_add_printf(buffer, "%u", i); 579 } 580 if ((size = EVBUFFER_LENGTH(buffer)) != 0) 581 xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer)); 582 evbuffer_free(buffer); 583 } 584 585 /* Callback for session_group_list. */ 586 static void 587 format_cb_session_group_list(struct format_tree *ft, struct format_entry *fe) 588 { 589 struct session *s = ft->s; 590 struct session_group *sg; 591 struct session *loop; 592 struct evbuffer *buffer; 593 int size; 594 595 if (s == NULL) 596 return; 597 sg = session_group_contains(s); 598 if (sg == NULL) 599 return; 600 601 buffer = evbuffer_new(); 602 TAILQ_FOREACH(loop, &sg->sessions, gentry) { 603 if (EVBUFFER_LENGTH(buffer) > 0) 604 evbuffer_add(buffer, ",", 1); 605 evbuffer_add_printf(buffer, "%s", loop->name); 606 } 607 if ((size = EVBUFFER_LENGTH(buffer)) != 0) 608 xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer)); 609 evbuffer_free(buffer); 610 } 611 612 /* Merge a format tree. */ 613 static void 614 format_merge(struct format_tree *ft, struct format_tree *from) 615 { 616 struct format_entry *fe; 617 618 RB_FOREACH(fe, format_entry_tree, &from->tree) { 619 if (fe->value != NULL) 620 format_add(ft, fe->key, "%s", fe->value); 621 } 622 } 623 624 /* Create a new tree. */ 625 struct format_tree * 626 format_create(struct client *c, struct cmdq_item *item, int tag, int flags) 627 { 628 struct format_tree *ft; 629 630 if (!event_initialized(&format_job_event)) { 631 evtimer_set(&format_job_event, format_job_timer, NULL); 632 format_job_timer(-1, 0, NULL); 633 } 634 635 ft = xcalloc(1, sizeof *ft); 636 RB_INIT(&ft->tree); 637 638 if (c != NULL) { 639 ft->client = c; 640 ft->client->references++; 641 } 642 643 ft->tag = tag; 644 ft->flags = flags; 645 646 format_add_cb(ft, "host", format_cb_host); 647 format_add_cb(ft, "host_short", format_cb_host_short); 648 format_add_cb(ft, "pid", format_cb_pid); 649 format_add(ft, "socket_path", "%s", socket_path); 650 format_add_tv(ft, "start_time", &start_time); 651 652 if (item != NULL) { 653 if (item->cmd != NULL) 654 format_add(ft, "command", "%s", item->cmd->entry->name); 655 if (item->shared != NULL && item->shared->formats != NULL) 656 format_merge(ft, item->shared->formats); 657 } 658 659 return (ft); 660 } 661 662 /* Free a tree. */ 663 void 664 format_free(struct format_tree *ft) 665 { 666 struct format_entry *fe, *fe1; 667 668 RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) { 669 RB_REMOVE(format_entry_tree, &ft->tree, fe); 670 free(fe->value); 671 free(fe->key); 672 free(fe); 673 } 674 675 if (ft->client != NULL) 676 server_client_unref(ft->client); 677 free(ft); 678 } 679 680 /* Add a key-value pair. */ 681 void 682 format_add(struct format_tree *ft, const char *key, const char *fmt, ...) 683 { 684 struct format_entry *fe; 685 struct format_entry *fe_now; 686 va_list ap; 687 688 fe = xmalloc(sizeof *fe); 689 fe->key = xstrdup(key); 690 691 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe); 692 if (fe_now != NULL) { 693 free(fe->key); 694 free(fe); 695 free(fe_now->value); 696 fe = fe_now; 697 } 698 699 fe->cb = NULL; 700 fe->t = 0; 701 702 va_start(ap, fmt); 703 xvasprintf(&fe->value, fmt, ap); 704 va_end(ap); 705 } 706 707 /* Add a key and time. */ 708 static void 709 format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv) 710 { 711 struct format_entry *fe; 712 struct format_entry *fe_now; 713 714 fe = xmalloc(sizeof *fe); 715 fe->key = xstrdup(key); 716 717 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe); 718 if (fe_now != NULL) { 719 free(fe->key); 720 free(fe); 721 free(fe_now->value); 722 fe = fe_now; 723 } 724 725 fe->cb = NULL; 726 fe->t = tv->tv_sec; 727 728 fe->value = NULL; 729 } 730 731 /* Add a key and function. */ 732 static void 733 format_add_cb(struct format_tree *ft, const char *key, format_cb cb) 734 { 735 struct format_entry *fe; 736 struct format_entry *fe_now; 737 738 fe = xmalloc(sizeof *fe); 739 fe->key = xstrdup(key); 740 741 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe); 742 if (fe_now != NULL) { 743 free(fe->key); 744 free(fe); 745 free(fe_now->value); 746 fe = fe_now; 747 } 748 749 fe->cb = cb; 750 fe->t = 0; 751 752 fe->value = NULL; 753 } 754 755 /* Find a format entry. */ 756 static char * 757 format_find(struct format_tree *ft, const char *key, int modifiers) 758 { 759 struct format_entry *fe, fe_find; 760 struct environ_entry *envent; 761 static char s[64]; 762 struct options_entry *o; 763 const char *found; 764 int idx; 765 char *copy, *saved; 766 767 if (~modifiers & FORMAT_TIMESTRING) { 768 o = options_parse_get(global_options, key, &idx, 0); 769 if (o == NULL && ft->w != NULL) 770 o = options_parse_get(ft->w->options, key, &idx, 0); 771 if (o == NULL) 772 o = options_parse_get(global_w_options, key, &idx, 0); 773 if (o == NULL && ft->s != NULL) 774 o = options_parse_get(ft->s->options, key, &idx, 0); 775 if (o == NULL) 776 o = options_parse_get(global_s_options, key, &idx, 0); 777 if (o != NULL) { 778 found = options_tostring(o, idx, 1); 779 goto found; 780 } 781 } 782 found = NULL; 783 784 fe_find.key = (char *) key; 785 fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find); 786 if (fe != NULL) { 787 if (modifiers & FORMAT_TIMESTRING) { 788 if (fe->t == 0) 789 return (NULL); 790 ctime_r(&fe->t, s); 791 s[strcspn(s, "\n")] = '\0'; 792 found = s; 793 goto found; 794 } 795 if (fe->t != 0) { 796 xsnprintf(s, sizeof s, "%lld", (long long)fe->t); 797 found = s; 798 goto found; 799 } 800 if (fe->value == NULL && fe->cb != NULL) { 801 fe->cb(ft, fe); 802 if (fe->value == NULL) 803 fe->value = xstrdup(""); 804 } 805 found = fe->value; 806 goto found; 807 } 808 809 if (~modifiers & FORMAT_TIMESTRING) { 810 envent = NULL; 811 if (ft->s != NULL) 812 envent = environ_find(ft->s->environ, key); 813 if (envent == NULL) 814 envent = environ_find(global_environ, key); 815 if (envent != NULL) { 816 found = envent->value; 817 goto found; 818 } 819 } 820 821 return (NULL); 822 823 found: 824 if (found == NULL) 825 return (NULL); 826 copy = xstrdup(found); 827 if (modifiers & FORMAT_BASENAME) { 828 saved = copy; 829 copy = xstrdup(basename(saved)); 830 free(saved); 831 } 832 if (modifiers & FORMAT_DIRNAME) { 833 saved = copy; 834 copy = xstrdup(dirname(saved)); 835 free(saved); 836 } 837 return (copy); 838 } 839 840 /* Skip until comma. */ 841 static char * 842 format_skip(char *s) 843 { 844 int brackets = 0; 845 846 for (; *s != '\0'; s++) { 847 if (*s == '{') 848 brackets++; 849 if (*s == '}') 850 brackets--; 851 if (*s == ',' && brackets == 0) 852 break; 853 } 854 if (*s == '\0') 855 return (NULL); 856 return (s); 857 } 858 859 /* Return left and right alternatives separated by commas. */ 860 static int 861 format_choose(char *s, char **left, char **right) 862 { 863 char *cp; 864 865 cp = format_skip(s); 866 if (cp == NULL) 867 return (-1); 868 *cp = '\0'; 869 870 *left = s; 871 *right = cp + 1; 872 return (0); 873 } 874 875 /* Is this true? */ 876 int 877 format_true(const char *s) 878 { 879 if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0')) 880 return (1); 881 return (0); 882 } 883 884 /* Replace a key. */ 885 static int 886 format_replace(struct format_tree *ft, const char *key, size_t keylen, 887 char **buf, size_t *len, size_t *off) 888 { 889 struct window_pane *wp = ft->wp; 890 char *copy, *copy0, *endptr, *ptr, *found, *new; 891 char *value, *from = NULL, *to = NULL, *left, *right; 892 size_t valuelen, newlen, fromlen, tolen, used; 893 long limit = 0; 894 int modifiers = 0, compare = 0, search = 0; 895 896 /* Make a copy of the key. */ 897 copy0 = copy = xmalloc(keylen + 1); 898 memcpy(copy, key, keylen); 899 copy[keylen] = '\0'; 900 901 /* Is there a length limit or whatnot? */ 902 switch (copy[0]) { 903 case 'm': 904 if (copy[1] != ':') 905 break; 906 compare = -2; 907 copy += 2; 908 break; 909 case 'C': 910 if (copy[1] != ':') 911 break; 912 search = 1; 913 copy += 2; 914 break; 915 case '|': 916 if (copy[1] != '|' || copy[2] != ':') 917 break; 918 compare = -3; 919 copy += 3; 920 break; 921 case '&': 922 if (copy[1] != '&' || copy[2] != ':') 923 break; 924 compare = -4; 925 copy += 3; 926 break; 927 case '!': 928 if (copy[1] == '=' && copy[2] == ':') { 929 compare = -1; 930 copy += 3; 931 break; 932 } 933 break; 934 case '=': 935 if (copy[1] == '=' && copy[2] == ':') { 936 compare = 1; 937 copy += 3; 938 break; 939 } 940 errno = 0; 941 limit = strtol(copy + 1, &endptr, 10); 942 if (errno == ERANGE && (limit == LONG_MIN || limit == LONG_MAX)) 943 break; 944 if (*endptr != ':') 945 break; 946 copy = endptr + 1; 947 break; 948 case 'b': 949 if (copy[1] != ':') 950 break; 951 modifiers |= FORMAT_BASENAME; 952 copy += 2; 953 break; 954 case 'd': 955 if (copy[1] != ':') 956 break; 957 modifiers |= FORMAT_DIRNAME; 958 copy += 2; 959 break; 960 case 't': 961 if (copy[1] != ':') 962 break; 963 modifiers |= FORMAT_TIMESTRING; 964 copy += 2; 965 break; 966 case 's': 967 if (copy[1] != '/') 968 break; 969 from = copy + 2; 970 for (copy = from; *copy != '\0' && *copy != '/'; copy++) 971 /* nothing */; 972 if (copy[0] != '/' || copy == from) { 973 copy = copy0; 974 break; 975 } 976 copy[0] = '\0'; 977 to = copy + 1; 978 for (copy = to; *copy != '\0' && *copy != '/'; copy++) 979 /* nothing */; 980 if (copy[0] != '/' || copy[1] != ':') { 981 copy = copy0; 982 break; 983 } 984 copy[0] = '\0'; 985 986 modifiers |= FORMAT_SUBSTITUTE; 987 copy += 2; 988 break; 989 } 990 991 /* Is this a comparison or a conditional? */ 992 if (search) { 993 /* Search in pane. */ 994 if (wp == NULL) 995 value = xstrdup("0"); 996 else 997 xasprintf(&value, "%u", window_pane_search(wp, copy)); 998 } else if (compare != 0) { 999 /* Comparison: compare comma-separated left and right. */ 1000 if (format_choose(copy, &left, &right) != 0) 1001 goto fail; 1002 left = format_expand(ft, left); 1003 right = format_expand(ft, right); 1004 if (compare == -3 && 1005 (format_true(left) || format_true(right))) 1006 value = xstrdup("1"); 1007 else if (compare == -4 && 1008 (format_true(left) && format_true(right))) 1009 value = xstrdup("1"); 1010 else if (compare == 1 && strcmp(left, right) == 0) 1011 value = xstrdup("1"); 1012 else if (compare == -1 && strcmp(left, right) != 0) 1013 value = xstrdup("1"); 1014 else if (compare == -2 && fnmatch(left, right, 0) == 0) 1015 value = xstrdup("1"); 1016 else 1017 value = xstrdup("0"); 1018 free(right); 1019 free(left); 1020 } else if (*copy == '?') { 1021 /* Conditional: check first and choose second or third. */ 1022 ptr = format_skip(copy); 1023 if (ptr == NULL) 1024 goto fail; 1025 *ptr = '\0'; 1026 1027 found = format_find(ft, copy + 1, modifiers); 1028 if (found == NULL) 1029 found = format_expand(ft, copy + 1); 1030 if (format_choose(ptr + 1, &left, &right) != 0) 1031 goto fail; 1032 1033 if (format_true(found)) 1034 value = format_expand(ft, left); 1035 else 1036 value = format_expand(ft, right); 1037 free(found); 1038 } else { 1039 /* Neither: look up directly. */ 1040 value = format_find(ft, copy, modifiers); 1041 if (value == NULL) 1042 value = xstrdup(""); 1043 } 1044 1045 /* Perform substitution if any. */ 1046 if (modifiers & FORMAT_SUBSTITUTE) { 1047 fromlen = strlen(from); 1048 tolen = strlen(to); 1049 1050 newlen = strlen(value) + 1; 1051 copy = new = xmalloc(newlen); 1052 for (ptr = value; *ptr != '\0'; /* nothing */) { 1053 if (strncmp(ptr, from, fromlen) != 0) { 1054 *new++ = *ptr++; 1055 continue; 1056 } 1057 used = new - copy; 1058 1059 newlen += tolen; 1060 copy = xrealloc(copy, newlen); 1061 1062 new = copy + used; 1063 memcpy(new, to, tolen); 1064 1065 new += tolen; 1066 ptr += fromlen; 1067 } 1068 *new = '\0'; 1069 free(value); 1070 value = copy; 1071 } 1072 1073 /* Truncate the value if needed. */ 1074 if (limit > 0) { 1075 new = utf8_trimcstr(value, limit); 1076 free(value); 1077 value = new; 1078 } else if (limit < 0) { 1079 new = utf8_rtrimcstr(value, -limit); 1080 free(value); 1081 value = new; 1082 } 1083 1084 /* Expand the buffer and copy in the value. */ 1085 valuelen = strlen(value); 1086 while (*len - *off < valuelen + 1) { 1087 *buf = xreallocarray(*buf, 2, *len); 1088 *len *= 2; 1089 } 1090 memcpy(*buf + *off, value, valuelen); 1091 *off += valuelen; 1092 1093 free(value); 1094 free(copy0); 1095 return (0); 1096 1097 fail: 1098 free(copy0); 1099 return (-1); 1100 } 1101 1102 /* Expand keys in a template, passing through strftime first. */ 1103 char * 1104 format_expand_time(struct format_tree *ft, const char *fmt, time_t t) 1105 { 1106 struct tm *tm; 1107 char s[2048]; 1108 1109 if (fmt == NULL || *fmt == '\0') 1110 return (xstrdup("")); 1111 1112 tm = localtime(&t); 1113 1114 if (strftime(s, sizeof s, fmt, tm) == 0) 1115 return (xstrdup("")); 1116 1117 return (format_expand(ft, s)); 1118 } 1119 1120 /* Expand keys in a template. */ 1121 char * 1122 format_expand(struct format_tree *ft, const char *fmt) 1123 { 1124 char *buf, *out; 1125 const char *ptr, *s, *saved = fmt; 1126 size_t off, len, n, outlen; 1127 int ch, brackets; 1128 1129 if (fmt == NULL) 1130 return (xstrdup("")); 1131 1132 len = 64; 1133 buf = xmalloc(len); 1134 off = 0; 1135 1136 while (*fmt != '\0') { 1137 if (*fmt != '#') { 1138 while (len - off < 2) { 1139 buf = xreallocarray(buf, 2, len); 1140 len *= 2; 1141 } 1142 buf[off++] = *fmt++; 1143 continue; 1144 } 1145 fmt++; 1146 1147 ch = (u_char) *fmt++; 1148 switch (ch) { 1149 case '(': 1150 brackets = 1; 1151 for (ptr = fmt; *ptr != '\0'; ptr++) { 1152 if (*ptr == '(') 1153 brackets++; 1154 if (*ptr == ')' && --brackets == 0) 1155 break; 1156 } 1157 if (*ptr != ')' || brackets != 0) 1158 break; 1159 n = ptr - fmt; 1160 1161 if (ft->flags & FORMAT_NOJOBS) 1162 out = xstrdup(""); 1163 else 1164 out = format_job_get(ft, xstrndup(fmt, n)); 1165 outlen = strlen(out); 1166 1167 while (len - off < outlen + 1) { 1168 buf = xreallocarray(buf, 2, len); 1169 len *= 2; 1170 } 1171 memcpy(buf + off, out, outlen); 1172 off += outlen; 1173 1174 free(out); 1175 1176 fmt += n + 1; 1177 continue; 1178 case '{': 1179 brackets = 1; 1180 for (ptr = fmt; *ptr != '\0'; ptr++) { 1181 if (*ptr == '{') 1182 brackets++; 1183 if (*ptr == '}' && --brackets == 0) 1184 break; 1185 } 1186 if (*ptr != '}' || brackets != 0) 1187 break; 1188 n = ptr - fmt; 1189 1190 if (format_replace(ft, fmt, n, &buf, &len, &off) != 0) 1191 break; 1192 fmt += n + 1; 1193 continue; 1194 case '#': 1195 while (len - off < 2) { 1196 buf = xreallocarray(buf, 2, len); 1197 len *= 2; 1198 } 1199 buf[off++] = '#'; 1200 continue; 1201 default: 1202 s = NULL; 1203 if (ch >= 'A' && ch <= 'Z') 1204 s = format_upper[ch - 'A']; 1205 else if (ch >= 'a' && ch <= 'z') 1206 s = format_lower[ch - 'a']; 1207 if (s == NULL) { 1208 while (len - off < 3) { 1209 buf = xreallocarray(buf, 2, len); 1210 len *= 2; 1211 } 1212 buf[off++] = '#'; 1213 buf[off++] = ch; 1214 continue; 1215 } 1216 n = strlen(s); 1217 if (format_replace(ft, s, n, &buf, &len, &off) != 0) 1218 break; 1219 continue; 1220 } 1221 1222 break; 1223 } 1224 buf[off] = '\0'; 1225 1226 log_debug("format '%s' -> '%s'", saved, buf); 1227 return (buf); 1228 } 1229 1230 /* Expand a single string. */ 1231 char * 1232 format_single(struct cmdq_item *item, const char *fmt, struct client *c, 1233 struct session *s, struct winlink *wl, struct window_pane *wp) 1234 { 1235 struct format_tree *ft; 1236 char *expanded; 1237 1238 if (item != NULL) 1239 ft = format_create(item->client, item, FORMAT_NONE, 0); 1240 else 1241 ft = format_create(NULL, item, FORMAT_NONE, 0); 1242 format_defaults(ft, c, s, wl, wp); 1243 1244 expanded = format_expand(ft, fmt); 1245 format_free(ft); 1246 return (expanded); 1247 } 1248 1249 /* Set defaults for any of arguments that are not NULL. */ 1250 void 1251 format_defaults(struct format_tree *ft, struct client *c, struct session *s, 1252 struct winlink *wl, struct window_pane *wp) 1253 { 1254 format_add(ft, "session_format", "%d", s != NULL); 1255 format_add(ft, "window_format", "%d", wl != NULL); 1256 format_add(ft, "pane_format", "%d", wp != NULL); 1257 1258 if (s == NULL && c != NULL) 1259 s = c->session; 1260 if (wl == NULL && s != NULL) 1261 wl = s->curw; 1262 if (wp == NULL && wl != NULL) 1263 wp = wl->window->active; 1264 1265 if (c != NULL) 1266 format_defaults_client(ft, c); 1267 if (s != NULL) 1268 format_defaults_session(ft, s); 1269 if (wl != NULL) 1270 format_defaults_winlink(ft, wl); 1271 if (wp != NULL) 1272 format_defaults_pane(ft, wp); 1273 } 1274 1275 /* Set default format keys for a session. */ 1276 static void 1277 format_defaults_session(struct format_tree *ft, struct session *s) 1278 { 1279 struct session_group *sg; 1280 1281 ft->s = s; 1282 1283 format_add(ft, "session_name", "%s", s->name); 1284 format_add(ft, "session_windows", "%u", winlink_count(&s->windows)); 1285 format_add(ft, "session_width", "%u", s->sx); 1286 format_add(ft, "session_height", "%u", s->sy); 1287 format_add(ft, "session_id", "$%u", s->id); 1288 1289 sg = session_group_contains(s); 1290 format_add(ft, "session_grouped", "%d", sg != NULL); 1291 if (sg != NULL) { 1292 format_add(ft, "session_group", "%s", sg->name); 1293 format_add(ft, "session_group_size", "%u", 1294 session_group_count (sg)); 1295 format_add_cb(ft, "session_group_list", 1296 format_cb_session_group_list); 1297 } 1298 1299 format_add_tv(ft, "session_created", &s->creation_time); 1300 format_add_tv(ft, "session_last_attached", &s->last_attached_time); 1301 format_add_tv(ft, "session_activity", &s->activity_time); 1302 1303 format_add(ft, "session_attached", "%u", s->attached); 1304 format_add(ft, "session_many_attached", "%d", s->attached > 1); 1305 1306 format_add_cb(ft, "session_alerts", format_cb_session_alerts); 1307 format_add_cb(ft, "session_stack", format_cb_session_stack); 1308 } 1309 1310 /* Set default format keys for a client. */ 1311 static void 1312 format_defaults_client(struct format_tree *ft, struct client *c) 1313 { 1314 struct session *s; 1315 const char *name; 1316 struct tty *tty = &c->tty; 1317 const char *types[] = TTY_TYPES; 1318 1319 if (ft->s == NULL) 1320 ft->s = c->session; 1321 1322 format_add(ft, "client_name", "%s", c->name); 1323 format_add(ft, "client_pid", "%ld", (long) c->pid); 1324 format_add(ft, "client_height", "%u", tty->sy); 1325 format_add(ft, "client_width", "%u", tty->sx); 1326 format_add(ft, "client_tty", "%s", c->ttyname); 1327 format_add(ft, "client_control_mode", "%d", 1328 !!(c->flags & CLIENT_CONTROL)); 1329 1330 if (tty->term_name != NULL) 1331 format_add(ft, "client_termname", "%s", tty->term_name); 1332 if (tty->term_name != NULL) 1333 format_add(ft, "client_termtype", "%s", types[tty->term_type]); 1334 1335 format_add_tv(ft, "client_created", &c->creation_time); 1336 format_add_tv(ft, "client_activity", &c->activity_time); 1337 1338 format_add(ft, "client_written", "%zu", c->written); 1339 format_add(ft, "client_discarded", "%zu", c->discarded); 1340 1341 name = server_client_get_key_table(c); 1342 if (strcmp(c->keytable->name, name) == 0) 1343 format_add(ft, "client_prefix", "%d", 0); 1344 else 1345 format_add(ft, "client_prefix", "%d", 1); 1346 format_add(ft, "client_key_table", "%s", c->keytable->name); 1347 1348 if (tty->flags & TTY_UTF8) 1349 format_add(ft, "client_utf8", "%d", 1); 1350 else 1351 format_add(ft, "client_utf8", "%d", 0); 1352 1353 if (c->flags & CLIENT_READONLY) 1354 format_add(ft, "client_readonly", "%d", 1); 1355 else 1356 format_add(ft, "client_readonly", "%d", 0); 1357 1358 s = c->session; 1359 if (s != NULL) 1360 format_add(ft, "client_session", "%s", s->name); 1361 s = c->last_session; 1362 if (s != NULL && session_alive(s)) 1363 format_add(ft, "client_last_session", "%s", s->name); 1364 } 1365 1366 /* Set default format keys for a window. */ 1367 void 1368 format_defaults_window(struct format_tree *ft, struct window *w) 1369 { 1370 ft->w = w; 1371 1372 format_add_tv(ft, "window_activity", &w->activity_time); 1373 format_add(ft, "window_id", "@%u", w->id); 1374 format_add(ft, "window_name", "%s", w->name); 1375 format_add(ft, "window_width", "%u", w->sx); 1376 format_add(ft, "window_height", "%u", w->sy); 1377 format_add_cb(ft, "window_layout", format_cb_window_layout); 1378 format_add_cb(ft, "window_visible_layout", 1379 format_cb_window_visible_layout); 1380 format_add(ft, "window_panes", "%u", window_count_panes(w)); 1381 format_add(ft, "window_zoomed_flag", "%d", 1382 !!(w->flags & WINDOW_ZOOMED)); 1383 } 1384 1385 /* Set default format keys for a winlink. */ 1386 static void 1387 format_defaults_winlink(struct format_tree *ft, struct winlink *wl) 1388 { 1389 struct session *s = wl->session; 1390 struct window *w = wl->window; 1391 1392 if (ft->w == NULL) 1393 ft->w = wl->window; 1394 ft->wl = wl; 1395 1396 format_defaults_window(ft, w); 1397 1398 format_add(ft, "window_index", "%d", wl->idx); 1399 format_add_cb(ft, "window_stack_index", format_cb_window_stack_index); 1400 format_add(ft, "window_flags", "%s", window_printable_flags(wl)); 1401 format_add(ft, "window_active", "%d", wl == s->curw); 1402 1403 format_add(ft, "window_bell_flag", "%d", 1404 !!(wl->flags & WINLINK_BELL)); 1405 format_add(ft, "window_activity_flag", "%d", 1406 !!(wl->flags & WINLINK_ACTIVITY)); 1407 format_add(ft, "window_silence_flag", "%d", 1408 !!(wl->flags & WINLINK_SILENCE)); 1409 format_add(ft, "window_last_flag", "%d", 1410 !!(wl == TAILQ_FIRST(&s->lastw))); 1411 format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window)); 1412 } 1413 1414 /* Set default format keys for a window pane. */ 1415 void 1416 format_defaults_pane(struct format_tree *ft, struct window_pane *wp) 1417 { 1418 struct grid *gd = wp->base.grid; 1419 int status = wp->status; 1420 u_int idx; 1421 1422 if (ft->w == NULL) 1423 ft->w = wp->window; 1424 ft->wp = wp; 1425 1426 format_add(ft, "history_size", "%u", gd->hsize); 1427 format_add(ft, "history_limit", "%u", gd->hlimit); 1428 format_add_cb(ft, "history_bytes", format_cb_history_bytes); 1429 1430 if (window_pane_index(wp, &idx) != 0) 1431 fatalx("index not found"); 1432 format_add(ft, "pane_index", "%u", idx); 1433 1434 format_add(ft, "pane_width", "%u", wp->sx); 1435 format_add(ft, "pane_height", "%u", wp->sy); 1436 format_add(ft, "pane_title", "%s", wp->base.title); 1437 format_add(ft, "pane_id", "%%%u", wp->id); 1438 format_add(ft, "pane_active", "%d", wp == wp->window->active); 1439 format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF)); 1440 format_add(ft, "pane_pipe", "%d", wp->pipe_fd != -1); 1441 1442 if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(status)) 1443 format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status)); 1444 format_add(ft, "pane_dead", "%d", wp->fd == -1); 1445 1446 if (window_pane_visible(wp)) { 1447 format_add(ft, "pane_left", "%u", wp->xoff); 1448 format_add(ft, "pane_top", "%u", wp->yoff); 1449 format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1); 1450 format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1); 1451 format_add(ft, "pane_at_left", "%d", wp->xoff == 0); 1452 format_add(ft, "pane_at_top", "%d", wp->yoff == 0); 1453 format_add(ft, "pane_at_right", "%d", 1454 wp->xoff + wp->sx == wp->window->sx); 1455 format_add(ft, "pane_at_bottom", "%d", 1456 wp->yoff + wp->sy == wp->window->sy); 1457 } 1458 1459 format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base); 1460 if (wp->mode != NULL) 1461 format_add(ft, "pane_mode", "%s", wp->mode->name); 1462 1463 format_add(ft, "pane_synchronized", "%d", 1464 !!options_get_number(wp->window->options, "synchronize-panes")); 1465 if (wp->searchstr != NULL) 1466 format_add(ft, "pane_search_string", "%s", wp->searchstr); 1467 1468 format_add(ft, "pane_tty", "%s", wp->tty); 1469 format_add(ft, "pane_pid", "%ld", (long) wp->pid); 1470 format_add_cb(ft, "pane_start_command", format_cb_start_command); 1471 format_add_cb(ft, "pane_current_command", format_cb_current_command); 1472 1473 format_add(ft, "cursor_x", "%u", wp->base.cx); 1474 format_add(ft, "cursor_y", "%u", wp->base.cy); 1475 format_add(ft, "scroll_region_upper", "%u", wp->base.rupper); 1476 format_add(ft, "scroll_region_lower", "%u", wp->base.rlower); 1477 1478 window_copy_add_formats(wp, ft); 1479 1480 format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0); 1481 format_add(ft, "alternate_saved_x", "%u", wp->saved_cx); 1482 format_add(ft, "alternate_saved_y", "%u", wp->saved_cy); 1483 1484 format_add(ft, "cursor_flag", "%d", 1485 !!(wp->base.mode & MODE_CURSOR)); 1486 format_add(ft, "insert_flag", "%d", 1487 !!(wp->base.mode & MODE_INSERT)); 1488 format_add(ft, "keypad_cursor_flag", "%d", 1489 !!(wp->base.mode & MODE_KCURSOR)); 1490 format_add(ft, "keypad_flag", "%d", 1491 !!(wp->base.mode & MODE_KKEYPAD)); 1492 format_add(ft, "wrap_flag", "%d", 1493 !!(wp->base.mode & MODE_WRAP)); 1494 1495 format_add(ft, "mouse_any_flag", "%d", 1496 !!(wp->base.mode & ALL_MOUSE_MODES)); 1497 format_add(ft, "mouse_standard_flag", "%d", 1498 !!(wp->base.mode & MODE_MOUSE_STANDARD)); 1499 format_add(ft, "mouse_button_flag", "%d", 1500 !!(wp->base.mode & MODE_MOUSE_BUTTON)); 1501 format_add(ft, "mouse_all_flag", "%d", 1502 !!(wp->base.mode & MODE_MOUSE_ALL)); 1503 1504 format_add_cb(ft, "pane_tabs", format_cb_pane_tabs); 1505 } 1506 1507 /* Set default format keys for paste buffer. */ 1508 void 1509 format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb) 1510 { 1511 struct timeval tv; 1512 size_t size; 1513 char *s; 1514 1515 timerclear(&tv); 1516 tv.tv_sec = paste_buffer_created(pb); 1517 paste_buffer_data(pb, &size); 1518 1519 format_add(ft, "buffer_size", "%zu", size); 1520 format_add(ft, "buffer_name", "%s", paste_buffer_name(pb)); 1521 format_add_tv(ft, "buffer_created", &tv); 1522 1523 s = paste_make_sample(pb); 1524 format_add(ft, "buffer_sample", "%s", s); 1525 free(s); 1526 } 1527