[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.98

1.98    ! nicm        1: /* $OpenBSD: format.c,v 1.97 2015/11/13 12:18:52 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.77      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
                    291: format_cb_host(unused struct format_tree *ft, struct format_entry *fe)
                    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
                    303: format_cb_host_short(unused struct format_tree *ft, struct format_entry *fe)
                    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
                    318: format_cb_pid(unused struct format_tree *ft, struct format_entry *fe)
                    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.1       nicm      491:
                    492:        return (ft);
                    493: }
                    494:
                    495: /* Free a tree. */
                    496: void
                    497: format_free(struct format_tree *ft)
                    498: {
1.54      nicm      499:        struct format_entry     *fe, *fe1;
1.1       nicm      500:
1.68      nicm      501:        RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
                    502:                RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9       nicm      503:                free(fe->value);
                    504:                free(fe->key);
                    505:                free(fe);
1.1       nicm      506:        }
                    507:
1.25      nicm      508:        free(ft);
1.1       nicm      509: }
                    510:
                    511: /* Add a key-value pair. */
                    512: void
                    513: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
                    514: {
                    515:        struct format_entry     *fe;
1.28      nicm      516:        struct format_entry     *fe_now;
1.1       nicm      517:        va_list                  ap;
                    518:
                    519:        fe = xmalloc(sizeof *fe);
                    520:        fe->key = xstrdup(key);
                    521:
1.79      nicm      522:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
                    523:        if (fe_now != NULL) {
                    524:                free(fe->key);
                    525:                free(fe);
                    526:                free(fe_now->value);
                    527:                fe = fe_now;
                    528:        }
                    529:
                    530:        fe->cb = NULL;
1.87      nicm      531:        fe->t = 0;
1.79      nicm      532:
1.1       nicm      533:        va_start(ap, fmt);
                    534:        xvasprintf(&fe->value, fmt, ap);
                    535:        va_end(ap);
1.79      nicm      536: }
                    537:
1.87      nicm      538: /* Add a key and time. */
                    539: void
                    540: format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
                    541: {
                    542:        struct format_entry     *fe;
                    543:        struct format_entry     *fe_now;
                    544:
                    545:        fe = xmalloc(sizeof *fe);
                    546:        fe->key = xstrdup(key);
                    547:
                    548:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
                    549:        if (fe_now != NULL) {
                    550:                free(fe->key);
                    551:                free(fe);
                    552:                free(fe_now->value);
                    553:                fe = fe_now;
                    554:        }
                    555:
                    556:        fe->cb = NULL;
                    557:        fe->t = tv->tv_sec;
                    558:
                    559:        fe->value = NULL;
                    560: }
                    561:
1.79      nicm      562: /* Add a key and function. */
                    563: void
                    564: format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
                    565: {
                    566:        struct format_entry     *fe;
                    567:        struct format_entry     *fe_now;
                    568:
                    569:        fe = xmalloc(sizeof *fe);
                    570:        fe->key = xstrdup(key);
1.1       nicm      571:
1.68      nicm      572:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28      nicm      573:        if (fe_now != NULL) {
                    574:                free(fe->key);
                    575:                free(fe);
1.79      nicm      576:                free(fe_now->value);
                    577:                fe = fe_now;
1.28      nicm      578:        }
1.79      nicm      579:
                    580:        fe->cb = cb;
1.87      nicm      581:        fe->t = 0;
1.79      nicm      582:
                    583:        fe->value = NULL;
1.1       nicm      584: }
                    585:
                    586: /* Find a format entry. */
1.87      nicm      587: char *
                    588: format_find(struct format_tree *ft, const char *key, int modifiers)
1.1       nicm      589: {
                    590:        struct format_entry     *fe, fe_find;
1.54      nicm      591:        struct options_entry    *o;
1.76      nicm      592:        struct environ_entry    *envent;
1.87      nicm      593:        static char              s[64];
                    594:        const char              *found;
                    595:        char                    *copy, *saved;
                    596:
                    597:        found = NULL;
                    598:
                    599:        if (~modifiers & FORMAT_TIMESTRING) {
1.90      nicm      600:                o = options_find(global_options, key);
1.87      nicm      601:                if (o == NULL && ft->w != NULL)
1.90      nicm      602:                        o = options_find(ft->w->options, key);
1.87      nicm      603:                if (o == NULL)
1.90      nicm      604:                        o = options_find(global_w_options, key);
1.87      nicm      605:                if (o == NULL && ft->s != NULL)
1.90      nicm      606:                        o = options_find(ft->s->options, key);
1.87      nicm      607:                if (o == NULL)
1.90      nicm      608:                        o = options_find(global_s_options, key);
1.87      nicm      609:                if (o != NULL) {
                    610:                        switch (o->type) {
                    611:                        case OPTIONS_STRING:
                    612:                                found = o->str;
                    613:                                goto found;
                    614:                        case OPTIONS_NUMBER:
                    615:                                xsnprintf(s, sizeof s, "%lld", o->num);
                    616:                                found = s;
                    617:                                goto found;
                    618:                        case OPTIONS_STYLE:
                    619:                                found = style_tostring(&o->style);
                    620:                                goto found;
                    621:                        }
1.54      nicm      622:                }
                    623:        }
1.1       nicm      624:
                    625:        fe_find.key = (char *) key;
1.68      nicm      626:        fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.79      nicm      627:        if (fe != NULL) {
1.87      nicm      628:                if (modifiers & FORMAT_TIMESTRING) {
                    629:                        if (fe->t == 0)
                    630:                                return (NULL);
                    631:                        ctime_r(&fe->t, s);
                    632:                        s[strcspn(s, "\n")] = '\0';
                    633:                        found = s;
                    634:                        goto found;
                    635:                }
                    636:                if (fe->t != 0) {
                    637:                        xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
                    638:                        found = s;
                    639:                        goto found;
                    640:                }
1.79      nicm      641:                if (fe->value == NULL && fe->cb != NULL)
                    642:                        fe->cb(ft, fe);
1.87      nicm      643:                found = fe->value;
                    644:                goto found;
1.79      nicm      645:        }
1.76      nicm      646:
1.87      nicm      647:        if (~modifiers & FORMAT_TIMESTRING) {
                    648:                envent = NULL;
                    649:                if (ft->s != NULL)
1.91      nicm      650:                        envent = environ_find(ft->s->environ, key);
1.87      nicm      651:                if (envent == NULL)
1.91      nicm      652:                        envent = environ_find(global_environ, key);
1.87      nicm      653:                if (envent != NULL) {
                    654:                        found = envent->value;
                    655:                        goto found;
                    656:                }
                    657:        }
1.76      nicm      658:
                    659:        return (NULL);
1.87      nicm      660:
                    661: found:
1.88      nicm      662:        if (found == NULL)
                    663:                return (NULL);
1.87      nicm      664:        copy = xstrdup(found);
                    665:        if (modifiers & FORMAT_BASENAME) {
                    666:                saved = copy;
                    667:                copy = xstrdup(basename(saved));
                    668:                free(saved);
                    669:        }
                    670:        if (modifiers & FORMAT_DIRNAME) {
                    671:                saved = copy;
                    672:                copy = xstrdup(dirname(saved));
                    673:                free(saved);
                    674:        }
                    675:        return (copy);
1.1       nicm      676: }
                    677:
                    678: /*
                    679:  * Replace a key/value pair in buffer. #{blah} is expanded directly,
                    680:  * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
                    681:  */
                    682: int
1.30      nicm      683: format_replace(struct format_tree *ft, const char *key, size_t keylen,
                    684:     char **buf, size_t *len, size_t *off)
1.1       nicm      685: {
1.98    ! nicm      686:        char            *copy, *copy0, *endptr, *ptr, *found, *new, *value;
        !           687:        char            *from = NULL, *to = NULL;
        !           688:        size_t           valuelen, newlen, fromlen, tolen, used;
1.44      nicm      689:        u_long           limit = 0;
1.89      nicm      690:        int              modifiers = 0, brackets;
1.1       nicm      691:
                    692:        /* Make a copy of the key. */
1.30      nicm      693:        copy0 = copy = xmalloc(keylen + 1);
1.1       nicm      694:        memcpy(copy, key, keylen);
                    695:        copy[keylen] = '\0';
                    696:
1.30      nicm      697:        /* Is there a length limit or whatnot? */
1.87      nicm      698:        switch (copy[0]) {
                    699:        case '=':
                    700:                errno = 0;
                    701:                limit = strtoul(copy + 1, &endptr, 10);
                    702:                if (errno == ERANGE && limit == ULONG_MAX)
                    703:                        break;
                    704:                if (*endptr != ':')
                    705:                        break;
                    706:                copy = endptr + 1;
                    707:                break;
                    708:        case 'b':
                    709:                if (copy[1] != ':')
                    710:                        break;
                    711:                modifiers |= FORMAT_BASENAME;
                    712:                copy += 2;
                    713:                break;
                    714:        case 'd':
                    715:                if (copy[1] != ':')
                    716:                        break;
                    717:                modifiers |= FORMAT_DIRNAME;
                    718:                copy += 2;
                    719:                break;
                    720:        case 't':
                    721:                if (copy[1] != ':')
                    722:                        break;
                    723:                modifiers |= FORMAT_TIMESTRING;
                    724:                copy += 2;
                    725:                break;
1.98    ! nicm      726:        case 's':
        !           727:                if (copy[1] != '/')
        !           728:                        break;
        !           729:                from = copy + 2;
        !           730:                for (copy = from; *copy != '\0' && *copy != '/'; copy++)
        !           731:                        /* nothing */;
        !           732:                if (copy[0] != '/' || copy == from) {
        !           733:                        copy = copy0;
        !           734:                        break;
        !           735:                }
        !           736:                copy[0] = '\0';
        !           737:                to = copy + 1;
        !           738:                for (copy = to; *copy != '\0' && *copy != '/'; copy++)
        !           739:                        /* nothing */;
        !           740:                if (copy[0] != '/' || copy[1] != ':') {
        !           741:                        copy = copy0;
        !           742:                        break;
        !           743:                }
        !           744:                copy[0] = '\0';
        !           745:
        !           746:                modifiers |= FORMAT_SUBSTITUTE;
        !           747:                copy += 2;
        !           748:                break;
1.30      nicm      749:        }
                    750:
1.1       nicm      751:        /*
                    752:         * Is this a conditional? If so, check it exists and extract either the
                    753:         * first or second element. If not, look up the key directly.
                    754:         */
                    755:        if (*copy == '?') {
                    756:                ptr = strchr(copy, ',');
                    757:                if (ptr == NULL)
                    758:                        goto fail;
                    759:                *ptr = '\0';
                    760:
1.89      nicm      761:                value = ptr + 1;
1.98    ! nicm      762:                found = format_find(ft, copy + 1, modifiers);
1.89      nicm      763:
                    764:                brackets = 0;
                    765:                for (ptr = ptr + 1; *ptr != '\0'; ptr++) {
                    766:                        if (*ptr == '{')
                    767:                                brackets++;
                    768:                        if (*ptr == '}')
                    769:                                brackets--;
                    770:                        if (*ptr == ',' && brackets == 0)
                    771:                                break;
                    772:                }
                    773:                if (*ptr == '\0')
                    774:                        goto fail;
                    775:
1.98    ! nicm      776:                if (found != NULL && *found != '\0' &&
        !           777:                    (found[0] != '0' || found[1] != '\0')) {
1.1       nicm      778:                        *ptr = '\0';
1.89      nicm      779:                } else
1.1       nicm      780:                        value = ptr + 1;
1.87      nicm      781:                value = format_expand(ft, value);
1.98    ! nicm      782:                free(found);
1.1       nicm      783:        } else {
1.98    ! nicm      784:                value = format_find(ft, copy, modifiers);
1.1       nicm      785:                if (value == NULL)
1.98    ! nicm      786:                        value = xstrdup("");
        !           787:        }
        !           788:
        !           789:        /* Perform substitution if any. */
        !           790:        if (modifiers & FORMAT_SUBSTITUTE) {
        !           791:                fromlen = strlen(from);
        !           792:                tolen = strlen(to);
        !           793:
        !           794:                newlen = strlen(value) + 1;
        !           795:                copy = new = xmalloc(newlen);
        !           796:                for (ptr = value; *ptr != '\0'; /* nothing */) {
        !           797:                        if (strncmp(ptr, from, fromlen) != 0) {
        !           798:                                *new++ = *ptr++;
        !           799:                                continue;
        !           800:                        }
        !           801:                        used = new - copy;
        !           802:
        !           803:                        newlen += tolen;
        !           804:                        copy = xrealloc(copy, newlen);
        !           805:
        !           806:                        new = copy + used;
        !           807:                        memcpy(new, to, tolen);
        !           808:
        !           809:                        new += tolen;
        !           810:                        ptr += fromlen;
        !           811:                }
        !           812:                *new = '\0';
        !           813:                free(value);
        !           814:                value = copy;
1.1       nicm      815:        }
                    816:
1.30      nicm      817:        /* Truncate the value if needed. */
1.44      nicm      818:        if (limit != 0) {
1.98    ! nicm      819:                new = utf8_trimcstr(value, limit);
        !           820:                free(value);
        !           821:                value = new;
1.44      nicm      822:        }
1.30      nicm      823:
1.1       nicm      824:        /* Expand the buffer and copy in the value. */
1.98    ! nicm      825:        valuelen = strlen(value);
1.1       nicm      826:        while (*len - *off < valuelen + 1) {
1.50      nicm      827:                *buf = xreallocarray(*buf, 2, *len);
1.1       nicm      828:                *len *= 2;
                    829:        }
                    830:        memcpy(*buf + *off, value, valuelen);
                    831:        *off += valuelen;
                    832:
1.98    ! nicm      833:        free(value);
1.30      nicm      834:        free(copy0);
1.1       nicm      835:        return (0);
                    836:
                    837: fail:
1.30      nicm      838:        free(copy0);
1.1       nicm      839:        return (-1);
                    840: }
                    841:
1.58      nicm      842: /* Expand keys in a template, passing through strftime first. */
                    843: char *
                    844: format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
                    845: {
                    846:        char            *tmp, *expanded;
                    847:        size_t           tmplen;
                    848:        struct tm       *tm;
                    849:
1.67      nicm      850:        if (fmt == NULL || *fmt == '\0')
1.58      nicm      851:                return (xstrdup(""));
                    852:
                    853:        tm = localtime(&t);
                    854:
                    855:        tmp = NULL;
                    856:        tmplen = strlen(fmt);
                    857:
                    858:        do {
                    859:                tmp = xreallocarray(tmp, 2, tmplen);
                    860:                tmplen *= 2;
                    861:        } while (strftime(tmp, tmplen, fmt, tm) == 0);
                    862:
                    863:        expanded = format_expand(ft, tmp);
                    864:        free(tmp);
                    865:
                    866:        return (expanded);
                    867: }
                    868:
1.1       nicm      869: /* Expand keys in a template. */
                    870: char *
                    871: format_expand(struct format_tree *ft, const char *fmt)
                    872: {
1.86      nicm      873:        char            *buf, *tmp, *cmd, *out;
1.79      nicm      874:        const char      *ptr, *s, *saved = fmt;
1.86      nicm      875:        size_t           off, len, n, outlen;
1.31      nicm      876:        int              ch, brackets;
1.58      nicm      877:
                    878:        if (fmt == NULL)
                    879:                return (xstrdup(""));
1.1       nicm      880:
                    881:        len = 64;
                    882:        buf = xmalloc(len);
                    883:        off = 0;
                    884:
                    885:        while (*fmt != '\0') {
                    886:                if (*fmt != '#') {
                    887:                        while (len - off < 2) {
1.50      nicm      888:                                buf = xreallocarray(buf, 2, len);
1.1       nicm      889:                                len *= 2;
                    890:                        }
                    891:                        buf[off++] = *fmt++;
                    892:                        continue;
                    893:                }
                    894:                fmt++;
                    895:
                    896:                ch = (u_char) *fmt++;
                    897:                switch (ch) {
1.68      nicm      898:                case '(':
                    899:                        brackets = 1;
                    900:                        for (ptr = fmt; *ptr != '\0'; ptr++) {
                    901:                                if (*ptr == '(')
                    902:                                        brackets++;
                    903:                                if (*ptr == ')' && --brackets == 0)
                    904:                                        break;
                    905:                        }
                    906:                        if (*ptr != ')' || brackets != 0)
                    907:                                break;
                    908:                        n = ptr - fmt;
                    909:
                    910:                        tmp = xmalloc(n + 1);
                    911:                        memcpy(tmp, fmt, n);
                    912:                        tmp[n] = '\0';
1.69      nicm      913:                        cmd = format_expand(ft, tmp);
1.68      nicm      914:
1.86      nicm      915:                        out = format_job_get(ft, cmd);
                    916:                        outlen = strlen(out);
1.69      nicm      917:
                    918:                        free(cmd);
                    919:                        free(tmp);
1.68      nicm      920:
1.86      nicm      921:                        while (len - off < outlen + 1) {
1.68      nicm      922:                                buf = xreallocarray(buf, 2, len);
                    923:                                len *= 2;
                    924:                        }
1.86      nicm      925:                        memcpy(buf + off, out, outlen);
                    926:                        off += outlen;
                    927:
                    928:                        free(out);
1.68      nicm      929:
                    930:                        fmt += n + 1;
                    931:                        continue;
1.1       nicm      932:                case '{':
1.31      nicm      933:                        brackets = 1;
                    934:                        for (ptr = fmt; *ptr != '\0'; ptr++) {
                    935:                                if (*ptr == '{')
                    936:                                        brackets++;
                    937:                                if (*ptr == '}' && --brackets == 0)
                    938:                                        break;
                    939:                        }
                    940:                        if (*ptr != '}' || brackets != 0)
1.1       nicm      941:                                break;
                    942:                        n = ptr - fmt;
                    943:
                    944:                        if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
                    945:                                break;
                    946:                        fmt += n + 1;
1.40      nicm      947:                        continue;
                    948:                case '#':
                    949:                        while (len - off < 2) {
1.50      nicm      950:                                buf = xreallocarray(buf, 2, len);
1.40      nicm      951:                                len *= 2;
                    952:                        }
                    953:                        buf[off++] = '#';
1.1       nicm      954:                        continue;
                    955:                default:
1.25      nicm      956:                        s = NULL;
                    957:                        if (ch >= 'A' && ch <= 'Z')
                    958:                                s = format_upper[ch - 'A'];
                    959:                        else if (ch >= 'a' && ch <= 'z')
                    960:                                s = format_lower[ch - 'a'];
                    961:                        if (s == NULL) {
                    962:                                while (len - off < 3) {
1.50      nicm      963:                                        buf = xreallocarray(buf, 2, len);
1.25      nicm      964:                                        len *= 2;
1.1       nicm      965:                                }
1.25      nicm      966:                                buf[off++] = '#';
                    967:                                buf[off++] = ch;
                    968:                                continue;
1.1       nicm      969:                        }
1.25      nicm      970:                        n = strlen(s);
                    971:                        if (format_replace(ft, s, n, &buf, &len, &off) != 0)
                    972:                                break;
1.1       nicm      973:                        continue;
                    974:                }
                    975:
                    976:                break;
                    977:        }
                    978:        buf[off] = '\0';
                    979:
1.79      nicm      980:        log_debug("format '%s' -> '%s'", saved, buf);
1.1       nicm      981:        return (buf);
                    982: }
                    983:
1.57      nicm      984: /* Set defaults for any of arguments that are not NULL. */
                    985: void
                    986: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
                    987:     struct winlink *wl, struct window_pane *wp)
                    988: {
                    989:        if (s == NULL && c != NULL)
                    990:                s = c->session;
                    991:        if (wl == NULL && s != NULL)
                    992:                wl = s->curw;
                    993:        if (wp == NULL && wl != NULL)
                    994:                wp = wl->window->active;
                    995:
                    996:        if (c != NULL)
                    997:                format_defaults_client(ft, c);
                    998:        if (s != NULL)
                    999:                format_defaults_session(ft, s);
                   1000:        if (s != NULL && wl != NULL)
                   1001:                format_defaults_winlink(ft, s, wl);
                   1002:        if (wp != NULL)
                   1003:                format_defaults_pane(ft, wp);
                   1004: }
                   1005:
1.1       nicm     1006: /* Set default format keys for a session. */
                   1007: void
1.57      nicm     1008: format_defaults_session(struct format_tree *ft, struct session *s)
1.1       nicm     1009: {
                   1010:        struct session_group    *sg;
                   1011:
1.54      nicm     1012:        ft->s = s;
                   1013:
1.1       nicm     1014:        format_add(ft, "session_name", "%s", s->name);
                   1015:        format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
                   1016:        format_add(ft, "session_width", "%u", s->sx);
                   1017:        format_add(ft, "session_height", "%u", s->sy);
1.23      nicm     1018:        format_add(ft, "session_id", "$%u", s->id);
1.1       nicm     1019:
                   1020:        sg = session_group_find(s);
                   1021:        format_add(ft, "session_grouped", "%d", sg != NULL);
                   1022:        if (sg != NULL)
                   1023:                format_add(ft, "session_group", "%u", session_group_index(sg));
                   1024:
1.87      nicm     1025:        format_add_tv(ft, "session_created", &s->creation_time);
                   1026:        format_add_tv(ft, "session_last_attached", &s->last_attached_time);
                   1027:        format_add_tv(ft, "session_activity", &s->activity_time);
1.1       nicm     1028:
1.41      nicm     1029:        format_add(ft, "session_attached", "%u", s->attached);
1.59      nicm     1030:        format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66      nicm     1031:
1.80      nicm     1032:        format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.3       nicm     1033: }
                   1034:
                   1035: /* Set default format keys for a client. */
                   1036: void
1.57      nicm     1037: format_defaults_client(struct format_tree *ft, struct client *c)
1.3       nicm     1038: {
1.60      nicm     1039:        struct session  *s;
1.3       nicm     1040:
1.54      nicm     1041:        if (ft->s == NULL)
                   1042:                ft->s = c->session;
                   1043:
1.72      nicm     1044:        format_add(ft, "client_pid", "%ld", (long) c->pid);
1.6       nicm     1045:        format_add(ft, "client_height", "%u", c->tty.sy);
                   1046:        format_add(ft, "client_width", "%u", c->tty.sx);
1.27      nicm     1047:        if (c->tty.path != NULL)
                   1048:                format_add(ft, "client_tty", "%s", c->tty.path);
                   1049:        if (c->tty.termname != NULL)
                   1050:                format_add(ft, "client_termname", "%s", c->tty.termname);
1.75      nicm     1051:        format_add(ft, "client_control_mode", "%d",
                   1052:                !!(c->flags & CLIENT_CONTROL));
1.3       nicm     1053:
1.87      nicm     1054:        format_add_tv(ft, "client_created", &c->creation_time);
                   1055:        format_add_tv(ft, "client_activity", &c->activity_time);
1.14      nicm     1056:
1.61      nicm     1057:        if (strcmp(c->keytable->name, "root") == 0)
                   1058:                format_add(ft, "client_prefix", "%d", 0);
                   1059:        else
                   1060:                format_add(ft, "client_prefix", "%d", 1);
                   1061:        format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3       nicm     1062:
                   1063:        if (c->tty.flags & TTY_UTF8)
                   1064:                format_add(ft, "client_utf8", "%d", 1);
                   1065:        else
                   1066:                format_add(ft, "client_utf8", "%d", 0);
                   1067:
                   1068:        if (c->flags & CLIENT_READONLY)
                   1069:                format_add(ft, "client_readonly", "%d", 1);
                   1070:        else
                   1071:                format_add(ft, "client_readonly", "%d", 0);
1.15      nicm     1072:
                   1073:        s = c->session;
                   1074:        if (s != NULL)
                   1075:                format_add(ft, "client_session", "%s", s->name);
                   1076:        s = c->last_session;
                   1077:        if (s != NULL && session_alive(s))
                   1078:                format_add(ft, "client_last_session", "%s", s->name);
1.1       nicm     1079: }
                   1080:
1.32      nicm     1081: /* Set default format keys for a window. */
                   1082: void
1.57      nicm     1083: format_defaults_window(struct format_tree *ft, struct window *w)
1.32      nicm     1084: {
1.54      nicm     1085:        ft->w = w;
                   1086:
1.87      nicm     1087:        format_add_tv(ft, "window_activity", &w->activity_time);
1.32      nicm     1088:        format_add(ft, "window_id", "@%u", w->id);
                   1089:        format_add(ft, "window_name", "%s", w->name);
                   1090:        format_add(ft, "window_width", "%u", w->sx);
                   1091:        format_add(ft, "window_height", "%u", w->sy);
1.80      nicm     1092:        format_add_cb(ft, "window_layout", format_cb_window_layout);
1.96      nicm     1093:        format_add_cb(ft, "window_visible_layout",
                   1094:            format_cb_window_visible_layout);
1.32      nicm     1095:        format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59      nicm     1096:        format_add(ft, "window_zoomed_flag", "%d",
1.53      nicm     1097:            !!(w->flags & WINDOW_ZOOMED));
1.32      nicm     1098: }
                   1099:
1.1       nicm     1100: /* Set default format keys for a winlink. */
                   1101: void
1.57      nicm     1102: format_defaults_winlink(struct format_tree *ft, struct session *s,
                   1103:     struct winlink *wl)
1.1       nicm     1104: {
                   1105:        struct window   *w = wl->window;
1.32      nicm     1106:        char            *flags;
1.1       nicm     1107:
1.54      nicm     1108:        if (ft->w == NULL)
                   1109:                ft->w = wl->window;
                   1110:
1.1       nicm     1111:        flags = window_printable_flags(s, wl);
                   1112:
1.57      nicm     1113:        format_defaults_window(ft, w);
1.32      nicm     1114:
1.1       nicm     1115:        format_add(ft, "window_index", "%d", wl->idx);
                   1116:        format_add(ft, "window_flags", "%s", flags);
                   1117:        format_add(ft, "window_active", "%d", wl == s->curw);
1.29      nicm     1118:
1.59      nicm     1119:        format_add(ft, "window_bell_flag", "%d",
1.29      nicm     1120:            !!(wl->flags & WINLINK_BELL));
1.59      nicm     1121:        format_add(ft, "window_activity_flag", "%d",
1.29      nicm     1122:            !!(wl->flags & WINLINK_ACTIVITY));
1.59      nicm     1123:        format_add(ft, "window_silence_flag", "%d",
1.29      nicm     1124:            !!(wl->flags & WINLINK_SILENCE));
1.59      nicm     1125:        format_add(ft, "window_last_flag", "%d",
1.49      nicm     1126:            !!(wl == TAILQ_FIRST(&s->lastw)));
1.64      nicm     1127:        format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.32      nicm     1128:
1.9       nicm     1129:        free(flags);
1.1       nicm     1130: }
                   1131:
                   1132: /* Set default format keys for a window pane. */
                   1133: void
1.57      nicm     1134: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1       nicm     1135: {
1.80      nicm     1136:        struct grid     *gd = wp->base.grid;
                   1137:        u_int            idx;
1.85      nicm     1138:        int              status, scroll_position;
1.54      nicm     1139:
                   1140:        if (ft->w == NULL)
                   1141:                ft->w = wp->window;
1.79      nicm     1142:        ft->wp = wp;
1.1       nicm     1143:
1.16      nicm     1144:        format_add(ft, "history_size", "%u", gd->hsize);
                   1145:        format_add(ft, "history_limit", "%u", gd->hlimit);
1.80      nicm     1146:        format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.1       nicm     1147:
1.4       nicm     1148:        if (window_pane_index(wp, &idx) != 0)
                   1149:                fatalx("index not found");
1.16      nicm     1150:        format_add(ft, "pane_index", "%u", idx);
1.4       nicm     1151:
1.1       nicm     1152:        format_add(ft, "pane_width", "%u", wp->sx);
                   1153:        format_add(ft, "pane_height", "%u", wp->sy);
                   1154:        format_add(ft, "pane_title", "%s", wp->base.title);
                   1155:        format_add(ft, "pane_id", "%%%u", wp->id);
                   1156:        format_add(ft, "pane_active", "%d", wp == wp->window->active);
1.55      nicm     1157:        format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
                   1158:
                   1159:        status = wp->status;
                   1160:        if (wp->fd == -1 && WIFEXITED(status))
                   1161:                format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1       nicm     1162:        format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47      nicm     1163:
                   1164:        if (window_pane_visible(wp)) {
                   1165:                format_add(ft, "pane_left", "%u", wp->xoff);
                   1166:                format_add(ft, "pane_top", "%u", wp->yoff);
                   1167:                format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
                   1168:                format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
                   1169:        }
1.16      nicm     1170:
                   1171:        format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
1.26      nicm     1172:        format_add(ft, "pane_synchronized", "%d",
1.90      nicm     1173:            !!options_get_number(wp->window->options, "synchronize-panes"));
1.16      nicm     1174:
1.71      nicm     1175:        format_add(ft, "pane_tty", "%s", wp->tty);
1.16      nicm     1176:        format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79      nicm     1177:        format_add_cb(ft, "pane_start_command", format_cb_start_command);
                   1178:        format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.16      nicm     1179:
1.59      nicm     1180:        format_add(ft, "cursor_x", "%u", wp->base.cx);
                   1181:        format_add(ft, "cursor_y", "%u", wp->base.cy);
                   1182:        format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
                   1183:        format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.85      nicm     1184:
                   1185:        scroll_position = window_copy_scroll_position(wp);
                   1186:        if (scroll_position != -1)
                   1187:                format_add(ft, "scroll_position", "%d", scroll_position);
1.16      nicm     1188:
                   1189:        format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59      nicm     1190:        format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
                   1191:        format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16      nicm     1192:
                   1193:        format_add(ft, "cursor_flag", "%d",
                   1194:            !!(wp->base.mode & MODE_CURSOR));
                   1195:        format_add(ft, "insert_flag", "%d",
                   1196:            !!(wp->base.mode & MODE_INSERT));
                   1197:        format_add(ft, "keypad_cursor_flag", "%d",
                   1198:            !!(wp->base.mode & MODE_KCURSOR));
                   1199:        format_add(ft, "keypad_flag", "%d",
                   1200:            !!(wp->base.mode & MODE_KKEYPAD));
                   1201:        format_add(ft, "wrap_flag", "%d",
                   1202:            !!(wp->base.mode & MODE_WRAP));
                   1203:
1.62      nicm     1204:        format_add(ft, "mouse_any_flag", "%d",
                   1205:            !!(wp->base.mode & (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON)));
1.16      nicm     1206:        format_add(ft, "mouse_standard_flag", "%d",
                   1207:            !!(wp->base.mode & MODE_MOUSE_STANDARD));
                   1208:        format_add(ft, "mouse_button_flag", "%d",
                   1209:            !!(wp->base.mode & MODE_MOUSE_BUTTON));
1.19      nicm     1210:
1.80      nicm     1211:        format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8       nicm     1212: }
                   1213:
1.19      nicm     1214: /* Set default format keys for paste buffer. */
1.8       nicm     1215: void
1.94      nicm     1216: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
1.8       nicm     1217: {
1.81      nicm     1218:        size_t   bufsize;
1.42      nicm     1219:        char    *s;
1.8       nicm     1220:
1.81      nicm     1221:        paste_buffer_data(pb, &bufsize);
                   1222:        format_add(ft, "buffer_size", "%zu", bufsize);
                   1223:        format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.8       nicm     1224:
1.94      nicm     1225:        s = paste_make_sample(pb);
1.42      nicm     1226:        format_add(ft, "buffer_sample", "%s", s);
                   1227:        free(s);
1.1       nicm     1228: }