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

1.55    ! nicm        1: /* $OpenBSD: format.c,v 1.54 2014/12/02 23:19:45 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.1       nicm       24: #include <netdb.h>
                     25: #include <stdarg.h>
1.9       nicm       26: #include <stdlib.h>
1.1       nicm       27: #include <string.h>
                     28: #include <time.h>
                     29: #include <unistd.h>
                     30:
                     31: #include "tmux.h"
                     32:
                     33: /*
                     34:  * Build a list of key-value pairs and use them to expand #{key} entries in a
                     35:  * string.
                     36:  */
                     37:
1.32      nicm       38: int     format_replace(struct format_tree *, const char *, size_t, char **,
                     39:             size_t *, size_t *);
                     40: char   *format_get_command(struct window_pane *);
                     41: void    format_window_pane_tabs(struct format_tree *, struct window_pane *);
1.1       nicm       42:
1.54      nicm       43: /* Entry in format tree. */
                     44: struct format_entry {
                     45:        char                   *key;
                     46:        char                   *value;
                     47:
                     48:        RB_ENTRY(format_entry)  entry;
                     49: };
                     50:
                     51: /* Tree of format entries. */
                     52: struct format_tree {
                     53:        struct window   *w;
                     54:        struct session  *s;
                     55:
                     56:        RB_HEAD(format_rb_tree, format_entry) tree;
                     57: };
                     58:
1.1       nicm       59: /* Format key-value replacement entry. */
1.54      nicm       60: int    format_cmp(struct format_entry *, struct format_entry *);
                     61: RB_PROTOTYPE(format_rb_tree, format_entry, entry, format_cmp);
                     62: RB_GENERATE(format_rb_tree, format_entry, entry, format_cmp);
1.1       nicm       63:
                     64: /* Format tree comparison function. */
                     65: int
                     66: format_cmp(struct format_entry *fe1, struct format_entry *fe2)
                     67: {
                     68:        return (strcmp(fe1->key, fe2->key));
                     69: }
                     70:
1.25      nicm       71: /* Single-character uppercase aliases. */
                     72: const char *format_upper[] = {
1.1       nicm       73:        NULL,           /* A */
                     74:        NULL,           /* B */
                     75:        NULL,           /* C */
                     76:        "pane_id",      /* D */
                     77:        NULL,           /* E */
                     78:        "window_flags", /* F */
                     79:        NULL,           /* G */
                     80:        "host",         /* H */
                     81:        "window_index", /* I */
                     82:        NULL,           /* J */
                     83:        NULL,           /* K */
                     84:        NULL,           /* L */
                     85:        NULL,           /* M */
                     86:        NULL,           /* N */
                     87:        NULL,           /* O */
                     88:        "pane_index",   /* P */
                     89:        NULL,           /* Q */
                     90:        NULL,           /* R */
                     91:        "session_name", /* S */
                     92:        "pane_title",   /* T */
                     93:        NULL,           /* U */
                     94:        NULL,           /* V */
                     95:        "window_name",  /* W */
                     96:        NULL,           /* X */
                     97:        NULL,           /* Y */
                     98:        NULL            /* Z */
                     99: };
                    100:
1.25      nicm      101: /* Single-character lowercase aliases. */
                    102: const char *format_lower[] = {
                    103:        NULL,           /* a */
                    104:        NULL,           /* b */
                    105:        NULL,           /* c */
                    106:        NULL,           /* d */
                    107:        NULL,           /* e */
                    108:        NULL,           /* f */
                    109:        NULL,           /* g */
                    110:        "host_short",   /* h */
                    111:        NULL,           /* i */
                    112:        NULL,           /* j */
                    113:        NULL,           /* k */
                    114:        NULL,           /* l */
                    115:        NULL,           /* m */
                    116:        NULL,           /* n */
                    117:        NULL,           /* o */
                    118:        NULL,           /* p */
                    119:        NULL,           /* q */
                    120:        NULL,           /* r */
                    121:        NULL,           /* s */
                    122:        NULL,           /* t */
                    123:        NULL,           /* u */
                    124:        NULL,           /* v */
                    125:        NULL,           /* w */
                    126:        NULL,           /* x */
                    127:        NULL,           /* y */
                    128:        NULL            /* z */
                    129: };
                    130:
1.1       nicm      131: /* Create a new tree. */
                    132: struct format_tree *
                    133: format_create(void)
                    134: {
                    135:        struct format_tree      *ft;
1.25      nicm      136:        char                     host[MAXHOSTNAMELEN], *ptr;
1.1       nicm      137:
1.54      nicm      138:        ft = xcalloc(1, sizeof *ft);
                    139:        RB_INIT(&ft->tree);
1.1       nicm      140:
1.25      nicm      141:        if (gethostname(host, sizeof host) == 0) {
1.1       nicm      142:                format_add(ft, "host", "%s", host);
1.33      nicm      143:                if ((ptr = strchr(host, '.')) != NULL)
1.25      nicm      144:                        *ptr = '\0';
                    145:                format_add(ft, "host_short", "%s", host);
                    146:        }
1.1       nicm      147:
                    148:        return (ft);
                    149: }
                    150:
                    151: /* Free a tree. */
                    152: void
                    153: format_free(struct format_tree *ft)
                    154: {
1.54      nicm      155:        struct format_entry     *fe, *fe1;
1.1       nicm      156:
1.54      nicm      157:        RB_FOREACH_SAFE(fe, format_rb_tree, &ft->tree, fe1) {
                    158:                RB_REMOVE(format_rb_tree, &ft->tree, fe);
1.9       nicm      159:                free(fe->value);
                    160:                free(fe->key);
                    161:                free(fe);
1.1       nicm      162:        }
                    163:
1.25      nicm      164:        free(ft);
1.1       nicm      165: }
                    166:
                    167: /* Add a key-value pair. */
                    168: void
                    169: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
                    170: {
                    171:        struct format_entry     *fe;
1.28      nicm      172:        struct format_entry     *fe_now;
1.1       nicm      173:        va_list                  ap;
                    174:
                    175:        fe = xmalloc(sizeof *fe);
                    176:        fe->key = xstrdup(key);
                    177:
                    178:        va_start(ap, fmt);
                    179:        xvasprintf(&fe->value, fmt, ap);
                    180:        va_end(ap);
                    181:
1.54      nicm      182:        fe_now = RB_INSERT(format_rb_tree, &ft->tree, fe);
1.28      nicm      183:        if (fe_now != NULL) {
                    184:                free(fe_now->value);
                    185:                fe_now->value = fe->value;
                    186:                free(fe->key);
                    187:                free(fe);
                    188:        }
1.1       nicm      189: }
                    190:
                    191: /* Find a format entry. */
                    192: const char *
                    193: format_find(struct format_tree *ft, const char *key)
                    194: {
                    195:        struct format_entry     *fe, fe_find;
1.54      nicm      196:        struct options_entry    *o;
                    197:        static char              s[16];
                    198:
                    199:        o = options_find(&global_options, key);
                    200:        if (o == NULL && ft->w != NULL)
                    201:                o = options_find(&ft->w->options, key);
                    202:        if (o == NULL)
                    203:                o = options_find(&global_w_options, key);
                    204:        if (o == NULL && ft->s != NULL)
                    205:                o = options_find(&ft->s->options, key);
                    206:        if (o == NULL)
                    207:                o = options_find(&global_s_options, key);
                    208:        if (o != NULL) {
                    209:                switch (o->type) {
                    210:                case OPTIONS_STRING:
                    211:                        return (o->str);
                    212:                case OPTIONS_NUMBER:
                    213:                        snprintf(s, sizeof s, "%lld", o->num);
                    214:                        return (s);
                    215:                case OPTIONS_STYLE:
                    216:                        return (style_tostring(&o->style));
                    217:                }
                    218:        }
1.1       nicm      219:
                    220:        fe_find.key = (char *) key;
1.54      nicm      221:        fe = RB_FIND(format_rb_tree, &ft->tree, &fe_find);
1.1       nicm      222:        if (fe == NULL)
                    223:                return (NULL);
                    224:        return (fe->value);
                    225: }
                    226:
                    227: /*
                    228:  * Replace a key/value pair in buffer. #{blah} is expanded directly,
                    229:  * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
                    230:  */
                    231: int
1.30      nicm      232: format_replace(struct format_tree *ft, const char *key, size_t keylen,
                    233:     char **buf, size_t *len, size_t *off)
1.1       nicm      234: {
1.44      nicm      235:        char            *copy, *copy0, *endptr, *ptr, *saved, *trimmed;
1.1       nicm      236:        const char      *value;
                    237:        size_t           valuelen;
1.44      nicm      238:        u_long           limit = 0;
1.1       nicm      239:
                    240:        /* Make a copy of the key. */
1.30      nicm      241:        copy0 = copy = xmalloc(keylen + 1);
1.1       nicm      242:        memcpy(copy, key, keylen);
                    243:        copy[keylen] = '\0';
                    244:
1.30      nicm      245:        /* Is there a length limit or whatnot? */
1.54      nicm      246:        if (!islower((u_char) *copy) && *copy != '@' && *copy != '?') {
1.30      nicm      247:                while (*copy != ':' && *copy != '\0') {
                    248:                        switch (*copy) {
                    249:                        case '=':
                    250:                                errno = 0;
                    251:                                limit = strtoul(copy + 1, &endptr, 10);
                    252:                                if (errno == ERANGE && limit == ULONG_MAX)
                    253:                                        goto fail;
                    254:                                copy = endptr;
                    255:                                break;
                    256:                        default:
                    257:                                copy++;
                    258:                                break;
                    259:                        }
                    260:                }
                    261:                if (*copy != ':')
                    262:                        goto fail;
                    263:                copy++;
                    264:        }
                    265:
1.1       nicm      266:        /*
                    267:         * Is this a conditional? If so, check it exists and extract either the
                    268:         * first or second element. If not, look up the key directly.
                    269:         */
                    270:        if (*copy == '?') {
                    271:                ptr = strchr(copy, ',');
                    272:                if (ptr == NULL)
                    273:                        goto fail;
                    274:                *ptr = '\0';
                    275:
                    276:                value = format_find(ft, copy + 1);
1.52      nicm      277:                if (value != NULL && *value != '\0' &&
                    278:                    (value[0] != '0' || value[1] != '\0')) {
1.1       nicm      279:                        value = ptr + 1;
                    280:                        ptr = strchr(value, ',');
                    281:                        if (ptr == NULL)
                    282:                                goto fail;
                    283:                        *ptr = '\0';
                    284:                } else {
                    285:                        ptr = strchr(ptr + 1, ',');
                    286:                        if (ptr == NULL)
                    287:                                goto fail;
                    288:                        value = ptr + 1;
                    289:                }
1.31      nicm      290:                saved = format_expand(ft, value);
                    291:                value = saved;
1.1       nicm      292:        } else {
                    293:                value = format_find(ft, copy);
                    294:                if (value == NULL)
                    295:                        value = "";
1.31      nicm      296:                saved = NULL;
1.1       nicm      297:        }
                    298:
1.30      nicm      299:        /* Truncate the value if needed. */
1.44      nicm      300:        if (limit != 0) {
                    301:                value = trimmed = utf8_trimcstr(value, limit);
                    302:                free(saved);
                    303:                saved = trimmed;
                    304:        }
                    305:        valuelen = strlen(value);
1.30      nicm      306:
1.1       nicm      307:        /* Expand the buffer and copy in the value. */
                    308:        while (*len - *off < valuelen + 1) {
1.50      nicm      309:                *buf = xreallocarray(*buf, 2, *len);
1.1       nicm      310:                *len *= 2;
                    311:        }
                    312:        memcpy(*buf + *off, value, valuelen);
                    313:        *off += valuelen;
                    314:
1.31      nicm      315:        free(saved);
1.30      nicm      316:        free(copy0);
1.1       nicm      317:        return (0);
                    318:
                    319: fail:
1.30      nicm      320:        free(copy0);
1.1       nicm      321:        return (-1);
                    322: }
                    323:
                    324: /* Expand keys in a template. */
                    325: char *
                    326: format_expand(struct format_tree *ft, const char *fmt)
                    327: {
1.31      nicm      328:        char            *buf;
                    329:        const char      *ptr, *s;
1.1       nicm      330:        size_t           off, len, n;
1.31      nicm      331:        int              ch, brackets;
1.1       nicm      332:
                    333:        len = 64;
                    334:        buf = xmalloc(len);
                    335:        off = 0;
                    336:
                    337:        while (*fmt != '\0') {
                    338:                if (*fmt != '#') {
                    339:                        while (len - off < 2) {
1.50      nicm      340:                                buf = xreallocarray(buf, 2, len);
1.1       nicm      341:                                len *= 2;
                    342:                        }
                    343:                        buf[off++] = *fmt++;
                    344:                        continue;
                    345:                }
                    346:                fmt++;
                    347:
                    348:                ch = (u_char) *fmt++;
                    349:                switch (ch) {
                    350:                case '{':
1.31      nicm      351:                        brackets = 1;
                    352:                        for (ptr = fmt; *ptr != '\0'; ptr++) {
                    353:                                if (*ptr == '{')
                    354:                                        brackets++;
                    355:                                if (*ptr == '}' && --brackets == 0)
                    356:                                        break;
                    357:                        }
                    358:                        if (*ptr != '}' || brackets != 0)
1.1       nicm      359:                                break;
                    360:                        n = ptr - fmt;
                    361:
                    362:                        if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
                    363:                                break;
                    364:                        fmt += n + 1;
1.40      nicm      365:                        continue;
                    366:                case '#':
                    367:                        while (len - off < 2) {
1.50      nicm      368:                                buf = xreallocarray(buf, 2, len);
1.40      nicm      369:                                len *= 2;
                    370:                        }
                    371:                        buf[off++] = '#';
1.1       nicm      372:                        continue;
                    373:                default:
1.25      nicm      374:                        s = NULL;
                    375:                        if (ch >= 'A' && ch <= 'Z')
                    376:                                s = format_upper[ch - 'A'];
                    377:                        else if (ch >= 'a' && ch <= 'z')
                    378:                                s = format_lower[ch - 'a'];
                    379:                        if (s == NULL) {
                    380:                                while (len - off < 3) {
1.50      nicm      381:                                        buf = xreallocarray(buf, 2, len);
1.25      nicm      382:                                        len *= 2;
1.1       nicm      383:                                }
1.25      nicm      384:                                buf[off++] = '#';
                    385:                                buf[off++] = ch;
                    386:                                continue;
1.1       nicm      387:                        }
1.25      nicm      388:                        n = strlen(s);
                    389:                        if (format_replace(ft, s, n, &buf, &len, &off) != 0)
                    390:                                break;
1.1       nicm      391:                        continue;
                    392:                }
                    393:
                    394:                break;
                    395:        }
                    396:        buf[off] = '\0';
                    397:
                    398:        return (buf);
                    399: }
                    400:
1.32      nicm      401: /* Get command name for format. */
                    402: char *
                    403: format_get_command(struct window_pane *wp)
                    404: {
1.37      nicm      405:        char    *cmd, *out;
1.32      nicm      406:
                    407:        cmd = get_proc_name(wp->fd, wp->tty);
                    408:        if (cmd == NULL || *cmd == '\0') {
1.39      nicm      409:                free(cmd);
1.46      nicm      410:                cmd = cmd_stringify_argv(wp->argc, wp->argv);
1.39      nicm      411:                if (cmd == NULL || *cmd == '\0') {
                    412:                        free(cmd);
1.38      nicm      413:                        cmd = xstrdup(wp->shell);
1.39      nicm      414:                }
1.32      nicm      415:        }
1.37      nicm      416:        out = parse_window_name(cmd);
                    417:        free(cmd);
                    418:        return (out);
1.32      nicm      419: }
                    420:
1.1       nicm      421: /* Set default format keys for a session. */
                    422: void
                    423: format_session(struct format_tree *ft, struct session *s)
                    424: {
                    425:        struct session_group    *sg;
                    426:        char                    *tim;
                    427:        time_t                   t;
                    428:
1.54      nicm      429:        ft->s = s;
                    430:
1.1       nicm      431:        format_add(ft, "session_name", "%s", s->name);
                    432:        format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
                    433:        format_add(ft, "session_width", "%u", s->sx);
                    434:        format_add(ft, "session_height", "%u", s->sy);
1.23      nicm      435:        format_add(ft, "session_id", "$%u", s->id);
1.1       nicm      436:
                    437:        sg = session_group_find(s);
                    438:        format_add(ft, "session_grouped", "%d", sg != NULL);
                    439:        if (sg != NULL)
                    440:                format_add(ft, "session_group", "%u", session_group_index(sg));
                    441:
                    442:        t = s->creation_time.tv_sec;
1.24      deraadt   443:        format_add(ft, "session_created", "%lld", (long long) t);
1.1       nicm      444:        tim = ctime(&t);
                    445:        *strchr(tim, '\n') = '\0';
                    446:        format_add(ft, "session_created_string", "%s", tim);
                    447:
1.41      nicm      448:        format_add(ft, "session_attached", "%u", s->attached);
                    449:        format_add(ft, "session_many_attached", "%u", s->attached > 1);
1.3       nicm      450: }
                    451:
                    452: /* Set default format keys for a client. */
                    453: void
                    454: format_client(struct format_tree *ft, struct client *c)
                    455: {
1.15      nicm      456:        char            *tim;
                    457:        time_t           t;
                    458:        struct session  *s;
1.3       nicm      459:
1.54      nicm      460:        if (ft->s == NULL)
                    461:                ft->s = c->session;
                    462:
1.6       nicm      463:        format_add(ft, "client_height", "%u", c->tty.sy);
                    464:        format_add(ft, "client_width", "%u", c->tty.sx);
1.27      nicm      465:        if (c->tty.path != NULL)
                    466:                format_add(ft, "client_tty", "%s", c->tty.path);
                    467:        if (c->tty.termname != NULL)
                    468:                format_add(ft, "client_termname", "%s", c->tty.termname);
1.3       nicm      469:
                    470:        t = c->creation_time.tv_sec;
1.24      deraadt   471:        format_add(ft, "client_created", "%lld", (long long) t);
1.3       nicm      472:        tim = ctime(&t);
                    473:        *strchr(tim, '\n') = '\0';
                    474:        format_add(ft, "client_created_string", "%s", tim);
                    475:
                    476:        t = c->activity_time.tv_sec;
1.24      deraadt   477:        format_add(ft, "client_activity", "%lld", (long long) t);
1.3       nicm      478:        tim = ctime(&t);
                    479:        *strchr(tim, '\n') = '\0';
                    480:        format_add(ft, "client_activity_string", "%s", tim);
1.14      nicm      481:
                    482:        format_add(ft, "client_prefix", "%d", !!(c->flags & CLIENT_PREFIX));
1.3       nicm      483:
                    484:        if (c->tty.flags & TTY_UTF8)
                    485:                format_add(ft, "client_utf8", "%d", 1);
                    486:        else
                    487:                format_add(ft, "client_utf8", "%d", 0);
                    488:
                    489:        if (c->flags & CLIENT_READONLY)
                    490:                format_add(ft, "client_readonly", "%d", 1);
                    491:        else
                    492:                format_add(ft, "client_readonly", "%d", 0);
1.15      nicm      493:
                    494:        s = c->session;
                    495:        if (s != NULL)
                    496:                format_add(ft, "client_session", "%s", s->name);
                    497:        s = c->last_session;
                    498:        if (s != NULL && session_alive(s))
                    499:                format_add(ft, "client_last_session", "%s", s->name);
1.1       nicm      500: }
                    501:
1.32      nicm      502: /* Set default format keys for a window. */
                    503: void
                    504: format_window(struct format_tree *ft, struct window *w)
                    505: {
                    506:        char    *layout;
                    507:
1.54      nicm      508:        ft->w = w;
                    509:
1.32      nicm      510:        layout = layout_dump(w);
                    511:
                    512:        format_add(ft, "window_id", "@%u", w->id);
                    513:        format_add(ft, "window_name", "%s", w->name);
                    514:        format_add(ft, "window_width", "%u", w->sx);
                    515:        format_add(ft, "window_height", "%u", w->sy);
                    516:        format_add(ft, "window_layout", "%s", layout);
                    517:        format_add(ft, "window_panes", "%u", window_count_panes(w));
1.53      nicm      518:        format_add(ft, "window_zoomed_flag", "%u",
                    519:            !!(w->flags & WINDOW_ZOOMED));
1.32      nicm      520:
                    521:        free(layout);
                    522: }
                    523:
1.1       nicm      524: /* Set default format keys for a winlink. */
                    525: void
                    526: format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
                    527: {
                    528:        struct window   *w = wl->window;
1.32      nicm      529:        char            *flags;
1.1       nicm      530:
1.54      nicm      531:        if (ft->w == NULL)
                    532:                ft->w = wl->window;
                    533:
1.1       nicm      534:        flags = window_printable_flags(s, wl);
                    535:
1.32      nicm      536:        format_window(ft, w);
                    537:
1.1       nicm      538:        format_add(ft, "window_index", "%d", wl->idx);
                    539:        format_add(ft, "window_flags", "%s", flags);
                    540:        format_add(ft, "window_active", "%d", wl == s->curw);
1.29      nicm      541:
                    542:        format_add(ft, "window_bell_flag", "%u",
                    543:            !!(wl->flags & WINLINK_BELL));
                    544:        format_add(ft, "window_activity_flag", "%u",
                    545:            !!(wl->flags & WINLINK_ACTIVITY));
                    546:        format_add(ft, "window_silence_flag", "%u",
                    547:            !!(wl->flags & WINLINK_SILENCE));
1.49      nicm      548:        format_add(ft, "window_last_flag", "%u",
                    549:            !!(wl == TAILQ_FIRST(&s->lastw)));
1.32      nicm      550:
1.9       nicm      551:        free(flags);
1.1       nicm      552: }
                    553:
1.19      nicm      554: /* Add window pane tabs. */
                    555: void
                    556: format_window_pane_tabs(struct format_tree *ft, struct window_pane *wp)
                    557: {
                    558:        struct evbuffer *buffer;
                    559:        u_int            i;
                    560:
                    561:        buffer = evbuffer_new();
                    562:        for (i = 0; i < wp->base.grid->sx; i++) {
                    563:                if (!bit_test(wp->base.tabs, i))
                    564:                        continue;
                    565:
                    566:                if (EVBUFFER_LENGTH(buffer) > 0)
                    567:                        evbuffer_add(buffer, ",", 1);
                    568:                evbuffer_add_printf(buffer, "%d", i);
                    569:        }
                    570:
                    571:        format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer),
                    572:            EVBUFFER_DATA(buffer));
                    573:        evbuffer_free(buffer);
                    574: }
                    575:
1.1       nicm      576: /* Set default format keys for a window pane. */
                    577: void
                    578: format_window_pane(struct format_tree *ft, struct window_pane *wp)
                    579: {
                    580:        struct grid             *gd = wp->base.grid;
                    581:        struct grid_line        *gl;
                    582:        unsigned long long       size;
1.20      nicm      583:        u_int                    i, idx;
1.21      nicm      584:        char                    *cmd;
1.55    ! nicm      585:        int                      status;
1.54      nicm      586:
                    587:        if (ft->w == NULL)
                    588:                ft->w = wp->window;
1.1       nicm      589:
                    590:        size = 0;
                    591:        for (i = 0; i < gd->hsize; i++) {
                    592:                gl = &gd->linedata[i];
                    593:                size += gl->cellsize * sizeof *gl->celldata;
                    594:        }
                    595:        size += gd->hsize * sizeof *gd->linedata;
1.16      nicm      596:        format_add(ft, "history_size", "%u", gd->hsize);
                    597:        format_add(ft, "history_limit", "%u", gd->hlimit);
                    598:        format_add(ft, "history_bytes", "%llu", size);
1.1       nicm      599:
1.4       nicm      600:        if (window_pane_index(wp, &idx) != 0)
                    601:                fatalx("index not found");
1.16      nicm      602:        format_add(ft, "pane_index", "%u", idx);
1.4       nicm      603:
1.1       nicm      604:        format_add(ft, "pane_width", "%u", wp->sx);
                    605:        format_add(ft, "pane_height", "%u", wp->sy);
                    606:        format_add(ft, "pane_title", "%s", wp->base.title);
                    607:        format_add(ft, "pane_id", "%%%u", wp->id);
                    608:        format_add(ft, "pane_active", "%d", wp == wp->window->active);
1.55    ! nicm      609:        format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
        !           610:
        !           611:        status = wp->status;
        !           612:        if (wp->fd == -1 && WIFEXITED(status))
        !           613:                format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1       nicm      614:        format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47      nicm      615:
                    616:        if (window_pane_visible(wp)) {
                    617:                format_add(ft, "pane_left", "%u", wp->xoff);
                    618:                format_add(ft, "pane_top", "%u", wp->yoff);
                    619:                format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
                    620:                format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
                    621:        }
1.16      nicm      622:
                    623:        format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
1.26      nicm      624:        format_add(ft, "pane_synchronized", "%d",
                    625:            !!options_get_number(&wp->window->options, "synchronize-panes"));
1.16      nicm      626:
                    627:        if (wp->tty != NULL)
                    628:                format_add(ft, "pane_tty", "%s", wp->tty);
                    629:        format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.46      nicm      630:        if ((cmd = cmd_stringify_argv(wp->argc, wp->argv)) != NULL) {
                    631:                format_add(ft, "pane_start_command", "%s", cmd);
                    632:                free(cmd);
                    633:        }
1.35      nicm      634:        if ((cmd = format_get_command(wp)) != NULL) {
1.17      nicm      635:                format_add(ft, "pane_current_command", "%s", cmd);
1.21      nicm      636:                free(cmd);
                    637:        }
1.16      nicm      638:
                    639:        format_add(ft, "cursor_x", "%d", wp->base.cx);
                    640:        format_add(ft, "cursor_y", "%d", wp->base.cy);
                    641:        format_add(ft, "scroll_region_upper", "%d", wp->base.rupper);
                    642:        format_add(ft, "scroll_region_lower", "%d", wp->base.rlower);
                    643:        format_add(ft, "saved_cursor_x", "%d", wp->ictx.old_cx);
                    644:        format_add(ft, "saved_cursor_y", "%d", wp->ictx.old_cy);
                    645:
                    646:        format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
                    647:        format_add(ft, "alternate_saved_x", "%d", wp->saved_cx);
                    648:        format_add(ft, "alternate_saved_y", "%d", wp->saved_cy);
                    649:
                    650:        format_add(ft, "cursor_flag", "%d",
                    651:            !!(wp->base.mode & MODE_CURSOR));
                    652:        format_add(ft, "insert_flag", "%d",
                    653:            !!(wp->base.mode & MODE_INSERT));
                    654:        format_add(ft, "keypad_cursor_flag", "%d",
                    655:            !!(wp->base.mode & MODE_KCURSOR));
                    656:        format_add(ft, "keypad_flag", "%d",
                    657:            !!(wp->base.mode & MODE_KKEYPAD));
                    658:        format_add(ft, "wrap_flag", "%d",
                    659:            !!(wp->base.mode & MODE_WRAP));
                    660:
                    661:        format_add(ft, "mouse_standard_flag", "%d",
                    662:            !!(wp->base.mode & MODE_MOUSE_STANDARD));
                    663:        format_add(ft, "mouse_button_flag", "%d",
                    664:            !!(wp->base.mode & MODE_MOUSE_BUTTON));
                    665:        format_add(ft, "mouse_utf8_flag", "%d",
                    666:            !!(wp->base.mode & MODE_MOUSE_UTF8));
1.19      nicm      667:
                    668:        format_window_pane_tabs(ft, wp);
1.8       nicm      669: }
                    670:
1.19      nicm      671: /* Set default format keys for paste buffer. */
1.8       nicm      672: void
1.42      nicm      673: format_paste_buffer(struct format_tree *ft, struct paste_buffer *pb,
                    674:     int utf8flag)
1.8       nicm      675: {
1.42      nicm      676:        char    *s;
1.8       nicm      677:
                    678:        format_add(ft, "buffer_size", "%zu", pb->size);
1.45      nicm      679:        format_add(ft, "buffer_name", "%s", pb->name);
1.8       nicm      680:
1.42      nicm      681:        s = paste_make_sample(pb, utf8flag);
                    682:        format_add(ft, "buffer_sample", "%s", s);
                    683:        free(s);
1.1       nicm      684: }