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

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