[BACK]Return to format.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/format.c, Revision 1.100

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