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

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