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

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