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