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