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

1.130   ! nicm        1: /* $OpenBSD: format.c,v 1.129 2017/04/20 09:43:45 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.104     nicm        4:  * Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
1.55      nicm       20: #include <sys/wait.h>
1.1       nicm       21:
1.30      nicm       22: #include <ctype.h>
                     23: #include <errno.h>
1.87      nicm       24: #include <libgen.h>
1.1       nicm       25: #include <netdb.h>
                     26: #include <stdarg.h>
1.9       nicm       27: #include <stdlib.h>
1.1       nicm       28: #include <string.h>
                     29: #include <time.h>
                     30: #include <unistd.h>
                     31:
                     32: #include "tmux.h"
                     33:
                     34: /*
                     35:  * Build a list of key-value pairs and use them to expand #{key} entries in a
                     36:  * string.
                     37:  */
                     38:
1.79      nicm       39: struct format_entry;
                     40: typedef void (*format_cb)(struct format_tree *, struct format_entry *);
                     41:
1.108     nicm       42: static char    *format_job_get(struct format_tree *, const char *);
                     43: static void     format_job_timer(int, short, void *);
                     44:
                     45: static void     format_cb_host(struct format_tree *, struct format_entry *);
                     46: static void     format_cb_host_short(struct format_tree *,
                     47:                     struct format_entry *);
                     48: static void     format_cb_pid(struct format_tree *, struct format_entry *);
                     49: static void     format_cb_session_alerts(struct format_tree *,
                     50:                     struct format_entry *);
                     51: static void     format_cb_window_layout(struct format_tree *,
                     52:                     struct format_entry *);
                     53: static void     format_cb_window_visible_layout(struct format_tree *,
                     54:                     struct format_entry *);
                     55: static void     format_cb_start_command(struct format_tree *,
                     56:                     struct format_entry *);
                     57: static void     format_cb_current_command(struct format_tree *,
                     58:                     struct format_entry *);
                     59: static void     format_cb_history_bytes(struct format_tree *,
                     60:                     struct format_entry *);
                     61: static void     format_cb_pane_tabs(struct format_tree *,
                     62:                     struct format_entry *);
                     63:
                     64: static char    *format_find(struct format_tree *, const char *, int);
                     65: static void     format_add_cb(struct format_tree *, const char *, format_cb);
                     66: static void     format_add_tv(struct format_tree *, const char *,
                     67:                     struct timeval *);
                     68: static int      format_replace(struct format_tree *, const char *, size_t,
                     69:                     char **, size_t *, size_t *);
                     70:
                     71: static void     format_defaults_session(struct format_tree *,
                     72:                     struct session *);
                     73: static void     format_defaults_client(struct format_tree *, struct client *);
1.129     nicm       74: static void     format_defaults_winlink(struct format_tree *,
1.108     nicm       75:                     struct winlink *);
1.1       nicm       76:
1.68      nicm       77: /* Entry in format job tree. */
                     78: struct format_job {
1.120     nicm       79:        u_int                    tag;
1.68      nicm       80:        const char              *cmd;
1.113     nicm       81:        const char              *expanded;
1.68      nicm       82:
                     83:        time_t                   last;
                     84:        char                    *out;
1.127     nicm       85:        int                      updated;
1.68      nicm       86:
                     87:        struct job              *job;
                     88:        int                      status;
                     89:
                     90:        RB_ENTRY(format_job)     entry;
                     91: };
                     92:
                     93: /* Format job tree. */
1.108     nicm       94: static struct event format_job_event;
                     95: static int format_job_cmp(struct format_job *, struct format_job *);
1.109     nicm       96: static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
1.108     nicm       97: RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
1.68      nicm       98:
                     99: /* Format job tree comparison function. */
1.108     nicm      100: static int
1.68      nicm      101: format_job_cmp(struct format_job *fj1, struct format_job *fj2)
                    102: {
1.120     nicm      103:        if (fj1->tag < fj2->tag)
                    104:                return (-1);
                    105:        if (fj1->tag > fj2->tag)
                    106:                return (1);
1.68      nicm      107:        return (strcmp(fj1->cmd, fj2->cmd));
                    108: }
                    109:
1.87      nicm      110: /* Format modifiers. */
                    111: #define FORMAT_TIMESTRING 0x1
                    112: #define FORMAT_BASENAME 0x2
                    113: #define FORMAT_DIRNAME 0x4
1.98      nicm      114: #define FORMAT_SUBSTITUTE 0x8
1.87      nicm      115:
1.54      nicm      116: /* Entry in format tree. */
                    117: struct format_entry {
1.79      nicm      118:        char                    *key;
                    119:        char                    *value;
1.87      nicm      120:        time_t                   t;
1.79      nicm      121:        format_cb                cb;
                    122:        RB_ENTRY(format_entry)   entry;
1.54      nicm      123: };
                    124:
1.68      nicm      125: /* Format entry tree. */
1.54      nicm      126: struct format_tree {
1.79      nicm      127:        struct window           *w;
                    128:        struct session          *s;
                    129:        struct window_pane      *wp;
1.54      nicm      130:
1.120     nicm      131:        u_int                    tag;
1.84      nicm      132:        int                      flags;
1.68      nicm      133:
                    134:        RB_HEAD(format_entry_tree, format_entry) tree;
1.54      nicm      135: };
1.108     nicm      136: static int format_entry_cmp(struct format_entry *, struct format_entry *);
                    137: RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
1.54      nicm      138:
1.68      nicm      139: /* Format entry tree comparison function. */
1.108     nicm      140: static int
1.68      nicm      141: format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
1.1       nicm      142: {
                    143:        return (strcmp(fe1->key, fe2->key));
                    144: }
                    145:
1.25      nicm      146: /* Single-character uppercase aliases. */
1.108     nicm      147: static const char *format_upper[] = {
1.1       nicm      148:        NULL,           /* A */
                    149:        NULL,           /* B */
                    150:        NULL,           /* C */
                    151:        "pane_id",      /* D */
                    152:        NULL,           /* E */
                    153:        "window_flags", /* F */
                    154:        NULL,           /* G */
                    155:        "host",         /* H */
                    156:        "window_index", /* I */
                    157:        NULL,           /* J */
                    158:        NULL,           /* K */
                    159:        NULL,           /* L */
                    160:        NULL,           /* M */
                    161:        NULL,           /* N */
                    162:        NULL,           /* O */
                    163:        "pane_index",   /* P */
                    164:        NULL,           /* Q */
                    165:        NULL,           /* R */
                    166:        "session_name", /* S */
                    167:        "pane_title",   /* T */
                    168:        NULL,           /* U */
                    169:        NULL,           /* V */
                    170:        "window_name",  /* W */
                    171:        NULL,           /* X */
                    172:        NULL,           /* Y */
                    173:        NULL            /* Z */
                    174: };
                    175:
1.25      nicm      176: /* Single-character lowercase aliases. */
1.108     nicm      177: static const char *format_lower[] = {
1.25      nicm      178:        NULL,           /* a */
                    179:        NULL,           /* b */
                    180:        NULL,           /* c */
                    181:        NULL,           /* d */
                    182:        NULL,           /* e */
                    183:        NULL,           /* f */
                    184:        NULL,           /* g */
                    185:        "host_short",   /* h */
                    186:        NULL,           /* i */
                    187:        NULL,           /* j */
                    188:        NULL,           /* k */
                    189:        NULL,           /* l */
                    190:        NULL,           /* m */
                    191:        NULL,           /* n */
                    192:        NULL,           /* o */
                    193:        NULL,           /* p */
                    194:        NULL,           /* q */
                    195:        NULL,           /* r */
                    196:        NULL,           /* s */
                    197:        NULL,           /* t */
                    198:        NULL,           /* u */
                    199:        NULL,           /* v */
                    200:        NULL,           /* w */
                    201:        NULL,           /* x */
                    202:        NULL,           /* y */
                    203:        NULL            /* z */
                    204: };
                    205:
1.127     nicm      206: /* Format job update callback. */
1.108     nicm      207: static void
1.127     nicm      208: format_job_update(struct job *job)
                    209: {
                    210:        struct format_job       *fj = job->data;
                    211:        char                    *line;
                    212:        time_t                   t;
                    213:        struct client           *c;
                    214:
                    215:        if ((line = evbuffer_readline(job->event->input)) == NULL)
                    216:                return;
                    217:        fj->updated = 1;
                    218:
                    219:        free(fj->out);
                    220:        fj->out = line;
                    221:
                    222:        log_debug("%s: %s: %s", __func__, fj->cmd, fj->out);
                    223:
                    224:        t = time (NULL);
                    225:        if (fj->status && fj->last != t) {
                    226:                TAILQ_FOREACH(c, &clients, entry)
                    227:                    server_status_client(c);
                    228:                fj->last = t;
                    229:        }
                    230: }
                    231:
                    232: /* Format job complete callback. */
                    233: static void
                    234: format_job_complete(struct job *job)
1.68      nicm      235: {
                    236:        struct format_job       *fj = job->data;
                    237:        char                    *line, *buf;
                    238:        size_t                   len;
                    239:        struct client           *c;
                    240:
                    241:        fj->job = NULL;
                    242:
                    243:        buf = NULL;
                    244:        if ((line = evbuffer_readline(job->event->input)) == NULL) {
                    245:                len = EVBUFFER_LENGTH(job->event->input);
                    246:                buf = xmalloc(len + 1);
                    247:                if (len != 0)
                    248:                        memcpy(buf, EVBUFFER_DATA(job->event->input), len);
                    249:                buf[len] = '\0';
                    250:        } else
                    251:                buf = line;
1.127     nicm      252:
                    253:        if (*buf != '\0' || !fj->updated) {
                    254:                free(fj->out);
                    255:                fj->out = buf;
                    256:                log_debug("%s: %s: %s", __func__, fj->cmd, fj->out);
                    257:        } else
                    258:                free(buf);
1.68      nicm      259:
                    260:        if (fj->status) {
                    261:                TAILQ_FOREACH(c, &clients, entry)
                    262:                    server_status_client(c);
                    263:                fj->status = 0;
                    264:        }
                    265: }
                    266:
                    267: /* Find a job. */
1.108     nicm      268: static char *
1.68      nicm      269: format_job_get(struct format_tree *ft, const char *cmd)
                    270: {
1.113     nicm      271:        struct format_job        fj0, *fj;
                    272:        time_t                   t;
                    273:        char                    *expanded;
                    274:        int                      force;
1.68      nicm      275:
1.120     nicm      276:        fj0.tag = ft->tag;
1.68      nicm      277:        fj0.cmd = cmd;
1.82      nicm      278:        if ((fj = RB_FIND(format_job_tree, &format_jobs, &fj0)) == NULL) {
1.68      nicm      279:                fj = xcalloc(1, sizeof *fj);
1.120     nicm      280:                fj->tag = ft->tag;
1.68      nicm      281:                fj->cmd = xstrdup(cmd);
1.113     nicm      282:                fj->expanded = NULL;
1.68      nicm      283:
                    284:                xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
                    285:
                    286:                RB_INSERT(format_job_tree, &format_jobs, fj);
                    287:        }
                    288:
1.113     nicm      289:        expanded = format_expand(ft, cmd);
                    290:        if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
                    291:                free((void *)fj->expanded);
                    292:                fj->expanded = xstrdup(expanded);
                    293:                force = 1;
                    294:        } else
                    295:                force = (ft->flags & FORMAT_FORCE);
                    296:
1.84      nicm      297:        t = time(NULL);
1.113     nicm      298:        if (fj->job == NULL && (force || fj->last != t)) {
1.127     nicm      299:                fj->job = job_run(expanded, NULL, NULL, format_job_update,
                    300:                    format_job_complete, NULL, fj);
1.68      nicm      301:                if (fj->job == NULL) {
                    302:                        free(fj->out);
                    303:                        xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
                    304:                }
1.84      nicm      305:                fj->last = t;
1.68      nicm      306:        }
1.84      nicm      307:
                    308:        if (ft->flags & FORMAT_STATUS)
                    309:                fj->status = 1;
1.68      nicm      310:
1.113     nicm      311:        free(expanded);
1.86      nicm      312:        return (format_expand(ft, fj->out));
1.68      nicm      313: }
                    314:
                    315: /* Remove old jobs. */
1.108     nicm      316: static void
1.99      nicm      317: format_job_timer(__unused int fd, __unused short events, __unused void *arg)
1.68      nicm      318: {
                    319:        struct format_job       *fj, *fj1;
                    320:        time_t                   now;
1.77      nicm      321:        struct timeval           tv = { .tv_sec = 60 };
1.68      nicm      322:
                    323:        now = time(NULL);
                    324:        RB_FOREACH_SAFE(fj, format_job_tree, &format_jobs, fj1) {
                    325:                if (fj->last > now || now - fj->last < 3600)
                    326:                        continue;
                    327:                RB_REMOVE(format_job_tree, &format_jobs, fj);
                    328:
1.77      nicm      329:                log_debug("%s: %s", __func__, fj->cmd);
                    330:
1.68      nicm      331:                if (fj->job != NULL)
                    332:                        job_free(fj->job);
                    333:
1.113     nicm      334:                free((void *)fj->expanded);
1.78      nicm      335:                free((void *)fj->cmd);
1.68      nicm      336:                free(fj->out);
                    337:
                    338:                free(fj);
                    339:        }
1.77      nicm      340:
                    341:        evtimer_del(&format_job_event);
                    342:        evtimer_add(&format_job_event, &tv);
1.68      nicm      343: }
                    344:
1.79      nicm      345: /* Callback for host. */
1.108     nicm      346: static void
1.99      nicm      347: format_cb_host(__unused struct format_tree *ft, struct format_entry *fe)
1.79      nicm      348: {
                    349:        char host[HOST_NAME_MAX + 1];
                    350:
                    351:        if (gethostname(host, sizeof host) != 0)
                    352:                fe->value = xstrdup("");
                    353:        else
                    354:                fe->value = xstrdup(host);
                    355: }
                    356:
                    357: /* Callback for host_short. */
1.108     nicm      358: static void
1.99      nicm      359: format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe)
1.79      nicm      360: {
                    361:        char host[HOST_NAME_MAX + 1], *cp;
                    362:
                    363:        if (gethostname(host, sizeof host) != 0)
                    364:                fe->value = xstrdup("");
                    365:        else {
                    366:                if ((cp = strchr(host, '.')) != NULL)
                    367:                        *cp = '\0';
                    368:                fe->value = xstrdup(host);
                    369:        }
                    370: }
                    371:
                    372: /* Callback for pid. */
1.108     nicm      373: static void
1.99      nicm      374: format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe)
1.79      nicm      375: {
                    376:        xasprintf(&fe->value, "%ld", (long)getpid());
                    377: }
                    378:
1.80      nicm      379: /* Callback for session_alerts. */
1.108     nicm      380: static void
1.80      nicm      381: format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
                    382: {
                    383:        struct session  *s = ft->s;
                    384:        struct winlink  *wl;
                    385:        char             alerts[256], tmp[16];
                    386:
                    387:        if (s == NULL)
                    388:                return;
                    389:
                    390:        *alerts = '\0';
                    391:        RB_FOREACH(wl, winlinks, &s->windows) {
                    392:                if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
                    393:                        continue;
                    394:                xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
                    395:
                    396:                if (*alerts != '\0')
                    397:                        strlcat(alerts, ",", sizeof alerts);
                    398:                strlcat(alerts, tmp, sizeof alerts);
                    399:                if (wl->flags & WINLINK_ACTIVITY)
                    400:                        strlcat(alerts, "#", sizeof alerts);
                    401:                if (wl->flags & WINLINK_BELL)
                    402:                        strlcat(alerts, "!", sizeof alerts);
                    403:                if (wl->flags & WINLINK_SILENCE)
                    404:                        strlcat(alerts, "~", sizeof alerts);
                    405:        }
                    406:        fe->value = xstrdup(alerts);
                    407: }
                    408:
                    409: /* Callback for window_layout. */
1.108     nicm      410: static void
1.80      nicm      411: format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
                    412: {
                    413:        struct window   *w = ft->w;
                    414:
                    415:        if (w == NULL)
                    416:                return;
                    417:
                    418:        if (w->saved_layout_root != NULL)
                    419:                fe->value = layout_dump(w->saved_layout_root);
                    420:        else
                    421:                fe->value = layout_dump(w->layout_root);
                    422: }
                    423:
1.96      nicm      424: /* Callback for window_visible_layout. */
1.108     nicm      425: static void
1.96      nicm      426: format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe)
                    427: {
                    428:        struct window   *w = ft->w;
                    429:
                    430:        if (w == NULL)
                    431:                return;
                    432:
                    433:        fe->value = layout_dump(w->layout_root);
                    434: }
                    435:
1.79      nicm      436: /* Callback for pane_start_command. */
1.108     nicm      437: static void
1.79      nicm      438: format_cb_start_command(struct format_tree *ft, struct format_entry *fe)
                    439: {
                    440:        struct window_pane      *wp = ft->wp;
                    441:
                    442:        if (wp == NULL)
                    443:                return;
                    444:
                    445:        fe->value = cmd_stringify_argv(wp->argc, wp->argv);
                    446: }
                    447:
                    448: /* Callback for pane_current_command. */
1.108     nicm      449: static void
1.79      nicm      450: format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
                    451: {
                    452:        struct window_pane      *wp = ft->wp;
                    453:        char                    *cmd;
                    454:
                    455:        if (wp == NULL)
                    456:                return;
                    457:
                    458:        cmd = get_proc_name(wp->fd, wp->tty);
                    459:        if (cmd == NULL || *cmd == '\0') {
                    460:                free(cmd);
                    461:                cmd = cmd_stringify_argv(wp->argc, wp->argv);
                    462:                if (cmd == NULL || *cmd == '\0') {
                    463:                        free(cmd);
                    464:                        cmd = xstrdup(wp->shell);
                    465:                }
                    466:        }
                    467:        fe->value = parse_window_name(cmd);
                    468:        free(cmd);
                    469: }
                    470:
1.80      nicm      471: /* Callback for history_bytes. */
1.108     nicm      472: static void
1.80      nicm      473: format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe)
                    474: {
                    475:        struct window_pane      *wp = ft->wp;
                    476:        struct grid             *gd;
                    477:        struct grid_line        *gl;
                    478:        unsigned long long       size;
                    479:        u_int                    i;
                    480:
                    481:        if (wp == NULL)
                    482:                return;
                    483:        gd = wp->base.grid;
                    484:
                    485:        size = 0;
                    486:        for (i = 0; i < gd->hsize; i++) {
                    487:                gl = &gd->linedata[i];
                    488:                size += gl->cellsize * sizeof *gl->celldata;
1.95      nicm      489:                size += gl->extdsize * sizeof *gl->extddata;
1.80      nicm      490:        }
                    491:        size += gd->hsize * sizeof *gd->linedata;
                    492:
                    493:        xasprintf(&fe->value, "%llu", size);
                    494: }
                    495:
                    496: /* Callback for pane_tabs. */
1.108     nicm      497: static void
1.80      nicm      498: format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe)
                    499: {
                    500:        struct window_pane      *wp = ft->wp;
                    501:        struct evbuffer         *buffer;
                    502:        u_int                    i;
                    503:        int                      size;
                    504:
                    505:        if (wp == NULL)
                    506:                return;
                    507:
                    508:        buffer = evbuffer_new();
                    509:        for (i = 0; i < wp->base.grid->sx; i++) {
                    510:                if (!bit_test(wp->base.tabs, i))
                    511:                        continue;
                    512:
                    513:                if (EVBUFFER_LENGTH(buffer) > 0)
                    514:                        evbuffer_add(buffer, ",", 1);
                    515:                evbuffer_add_printf(buffer, "%u", i);
                    516:        }
                    517:        size = EVBUFFER_LENGTH(buffer);
                    518:        xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
                    519:        evbuffer_free(buffer);
                    520: }
                    521:
1.112     nicm      522: /* Merge a format tree. */
                    523: static void
                    524: format_merge(struct format_tree *ft, struct format_tree *from)
                    525: {
                    526:        struct format_entry     *fe;
                    527:
                    528:        RB_FOREACH(fe, format_entry_tree, &from->tree) {
                    529:                if (fe->value != NULL)
                    530:                        format_add(ft, fe->key, "%s", fe->value);
                    531:        }
                    532: }
                    533:
1.1       nicm      534: /* Create a new tree. */
                    535: struct format_tree *
1.120     nicm      536: format_create(struct cmdq_item *item, int tag, int flags)
1.68      nicm      537: {
1.1       nicm      538:        struct format_tree      *ft;
1.77      nicm      539:
                    540:        if (!event_initialized(&format_job_event)) {
                    541:                evtimer_set(&format_job_event, format_job_timer, NULL);
                    542:                format_job_timer(-1, 0, NULL);
                    543:        }
1.1       nicm      544:
1.54      nicm      545:        ft = xcalloc(1, sizeof *ft);
                    546:        RB_INIT(&ft->tree);
1.120     nicm      547:
                    548:        ft->tag = tag;
1.84      nicm      549:        ft->flags = flags;
1.1       nicm      550:
1.79      nicm      551:        format_add_cb(ft, "host", format_cb_host);
                    552:        format_add_cb(ft, "host_short", format_cb_host_short);
                    553:        format_add_cb(ft, "pid", format_cb_pid);
1.100     nicm      554:        format_add(ft, "socket_path", "%s", socket_path);
                    555:        format_add_tv(ft, "start_time", &start_time);
1.102     nicm      556:
1.130   ! nicm      557:        if (item != NULL) {
        !           558:                if (item->cmd != NULL)
        !           559:                        format_add(ft, "command", "%s", item->cmd->entry->name);
        !           560:                if (item->shared != NULL && item->shared->formats != NULL)
        !           561:                        format_merge(ft, item->shared->formats);
        !           562:        }
1.1       nicm      563:
                    564:        return (ft);
                    565: }
                    566:
                    567: /* Free a tree. */
                    568: void
                    569: format_free(struct format_tree *ft)
                    570: {
1.54      nicm      571:        struct format_entry     *fe, *fe1;
1.1       nicm      572:
1.68      nicm      573:        RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
                    574:                RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9       nicm      575:                free(fe->value);
                    576:                free(fe->key);
                    577:                free(fe);
1.1       nicm      578:        }
                    579:
1.25      nicm      580:        free(ft);
1.1       nicm      581: }
                    582:
                    583: /* Add a key-value pair. */
                    584: void
                    585: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
                    586: {
                    587:        struct format_entry     *fe;
1.28      nicm      588:        struct format_entry     *fe_now;
1.1       nicm      589:        va_list                  ap;
                    590:
                    591:        fe = xmalloc(sizeof *fe);
                    592:        fe->key = xstrdup(key);
                    593:
1.79      nicm      594:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
                    595:        if (fe_now != NULL) {
                    596:                free(fe->key);
                    597:                free(fe);
                    598:                free(fe_now->value);
                    599:                fe = fe_now;
                    600:        }
                    601:
                    602:        fe->cb = NULL;
1.87      nicm      603:        fe->t = 0;
1.79      nicm      604:
1.1       nicm      605:        va_start(ap, fmt);
                    606:        xvasprintf(&fe->value, fmt, ap);
                    607:        va_end(ap);
1.79      nicm      608: }
                    609:
1.87      nicm      610: /* Add a key and time. */
1.108     nicm      611: static void
1.87      nicm      612: format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
                    613: {
                    614:        struct format_entry     *fe;
                    615:        struct format_entry     *fe_now;
                    616:
                    617:        fe = xmalloc(sizeof *fe);
                    618:        fe->key = xstrdup(key);
                    619:
                    620:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
                    621:        if (fe_now != NULL) {
                    622:                free(fe->key);
                    623:                free(fe);
                    624:                free(fe_now->value);
                    625:                fe = fe_now;
                    626:        }
                    627:
                    628:        fe->cb = NULL;
                    629:        fe->t = tv->tv_sec;
                    630:
                    631:        fe->value = NULL;
                    632: }
                    633:
1.79      nicm      634: /* Add a key and function. */
1.108     nicm      635: static void
1.79      nicm      636: format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
                    637: {
                    638:        struct format_entry     *fe;
                    639:        struct format_entry     *fe_now;
                    640:
                    641:        fe = xmalloc(sizeof *fe);
                    642:        fe->key = xstrdup(key);
1.1       nicm      643:
1.68      nicm      644:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28      nicm      645:        if (fe_now != NULL) {
                    646:                free(fe->key);
                    647:                free(fe);
1.79      nicm      648:                free(fe_now->value);
                    649:                fe = fe_now;
1.28      nicm      650:        }
1.79      nicm      651:
                    652:        fe->cb = cb;
1.87      nicm      653:        fe->t = 0;
1.79      nicm      654:
                    655:        fe->value = NULL;
1.1       nicm      656: }
                    657:
                    658: /* Find a format entry. */
1.108     nicm      659: static char *
1.87      nicm      660: format_find(struct format_tree *ft, const char *key, int modifiers)
1.1       nicm      661: {
                    662:        struct format_entry     *fe, fe_find;
1.76      nicm      663:        struct environ_entry    *envent;
1.87      nicm      664:        static char              s[64];
1.117     nicm      665:        struct options_entry    *o;
1.87      nicm      666:        const char              *found;
1.116     nicm      667:        int                      idx;
1.87      nicm      668:        char                    *copy, *saved;
                    669:
                    670:        if (~modifiers & FORMAT_TIMESTRING) {
1.116     nicm      671:                o = options_parse_get(global_options, key, &idx, 0);
1.87      nicm      672:                if (o == NULL && ft->w != NULL)
1.116     nicm      673:                        o = options_parse_get(ft->w->options, key, &idx, 0);
1.87      nicm      674:                if (o == NULL)
1.116     nicm      675:                        o = options_parse_get(global_w_options, key, &idx, 0);
1.87      nicm      676:                if (o == NULL && ft->s != NULL)
1.116     nicm      677:                        o = options_parse_get(ft->s->options, key, &idx, 0);
1.87      nicm      678:                if (o == NULL)
1.116     nicm      679:                        o = options_parse_get(global_s_options, key, &idx, 0);
1.87      nicm      680:                if (o != NULL) {
1.118     nicm      681:                        found = options_tostring(o, idx, 1);
1.116     nicm      682:                        goto found;
1.54      nicm      683:                }
                    684:        }
1.116     nicm      685:        found = NULL;
1.1       nicm      686:
                    687:        fe_find.key = (char *) key;
1.68      nicm      688:        fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.79      nicm      689:        if (fe != NULL) {
1.87      nicm      690:                if (modifiers & FORMAT_TIMESTRING) {
                    691:                        if (fe->t == 0)
                    692:                                return (NULL);
                    693:                        ctime_r(&fe->t, s);
                    694:                        s[strcspn(s, "\n")] = '\0';
                    695:                        found = s;
                    696:                        goto found;
                    697:                }
                    698:                if (fe->t != 0) {
                    699:                        xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
                    700:                        found = s;
                    701:                        goto found;
                    702:                }
1.79      nicm      703:                if (fe->value == NULL && fe->cb != NULL)
                    704:                        fe->cb(ft, fe);
1.87      nicm      705:                found = fe->value;
                    706:                goto found;
1.79      nicm      707:        }
1.76      nicm      708:
1.87      nicm      709:        if (~modifiers & FORMAT_TIMESTRING) {
                    710:                envent = NULL;
                    711:                if (ft->s != NULL)
1.91      nicm      712:                        envent = environ_find(ft->s->environ, key);
1.87      nicm      713:                if (envent == NULL)
1.91      nicm      714:                        envent = environ_find(global_environ, key);
1.87      nicm      715:                if (envent != NULL) {
                    716:                        found = envent->value;
                    717:                        goto found;
                    718:                }
                    719:        }
1.76      nicm      720:
                    721:        return (NULL);
1.87      nicm      722:
                    723: found:
1.88      nicm      724:        if (found == NULL)
                    725:                return (NULL);
1.87      nicm      726:        copy = xstrdup(found);
                    727:        if (modifiers & FORMAT_BASENAME) {
                    728:                saved = copy;
                    729:                copy = xstrdup(basename(saved));
                    730:                free(saved);
                    731:        }
                    732:        if (modifiers & FORMAT_DIRNAME) {
                    733:                saved = copy;
                    734:                copy = xstrdup(dirname(saved));
                    735:                free(saved);
                    736:        }
                    737:        return (copy);
1.1       nicm      738: }
                    739:
1.114     nicm      740: /* Skip until comma. */
                    741: static char *
                    742: format_skip(char *s)
                    743: {
                    744:        int     brackets = 0;
                    745:
                    746:        for (; *s != '\0'; s++) {
                    747:                if (*s == '{')
                    748:                        brackets++;
                    749:                if (*s == '}')
                    750:                        brackets--;
                    751:                if (*s == ',' && brackets == 0)
                    752:                        break;
                    753:        }
                    754:        if (*s == '\0')
                    755:                return (NULL);
                    756:        return (s);
                    757: }
                    758:
                    759: /* Return left and right alternatives separated by commas. */
                    760: static int
                    761: format_choose(char *s, char **left, char **right)
                    762: {
                    763:        char    *cp;
                    764:
                    765:        cp = format_skip(s);
                    766:        if (cp == NULL)
                    767:                return (-1);
                    768:        *cp = '\0';
                    769:
                    770:        *left = s;
                    771:        *right = cp + 1;
                    772:        return (0);
                    773: }
                    774:
                    775: /* Is this true? */
                    776: static int
                    777: format_true(const char *s)
                    778: {
                    779:        if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
                    780:                return (1);
                    781:        return (0);
                    782: }
                    783:
1.1       nicm      784: /*
                    785:  * Replace a key/value pair in buffer. #{blah} is expanded directly,
                    786:  * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
                    787:  */
1.108     nicm      788: static int
1.30      nicm      789: format_replace(struct format_tree *ft, const char *key, size_t keylen,
                    790:     char **buf, size_t *len, size_t *off)
1.1       nicm      791: {
1.98      nicm      792:        char            *copy, *copy0, *endptr, *ptr, *found, *new, *value;
1.114     nicm      793:        char            *from = NULL, *to = NULL, *left, *right;
1.98      nicm      794:        size_t           valuelen, newlen, fromlen, tolen, used;
1.105     nicm      795:        long             limit = 0;
1.114     nicm      796:        int              modifiers = 0, compare = 0;
1.1       nicm      797:
                    798:        /* Make a copy of the key. */
1.30      nicm      799:        copy0 = copy = xmalloc(keylen + 1);
1.1       nicm      800:        memcpy(copy, key, keylen);
                    801:        copy[keylen] = '\0';
                    802:
1.30      nicm      803:        /* Is there a length limit or whatnot? */
1.87      nicm      804:        switch (copy[0]) {
1.114     nicm      805:        case '!':
                    806:                if (copy[1] == '=' && copy[2] == ':') {
                    807:                        compare = -1;
                    808:                        copy += 3;
                    809:                        break;
                    810:                }
                    811:                break;
1.87      nicm      812:        case '=':
1.114     nicm      813:                if (copy[1] == '=' && copy[2] == ':') {
                    814:                        compare = 1;
                    815:                        copy += 3;
                    816:                        break;
                    817:                }
1.87      nicm      818:                errno = 0;
1.105     nicm      819:                limit = strtol(copy + 1, &endptr, 10);
                    820:                if (errno == ERANGE && (limit == LONG_MIN || limit == LONG_MAX))
1.87      nicm      821:                        break;
                    822:                if (*endptr != ':')
                    823:                        break;
                    824:                copy = endptr + 1;
                    825:                break;
                    826:        case 'b':
                    827:                if (copy[1] != ':')
                    828:                        break;
                    829:                modifiers |= FORMAT_BASENAME;
                    830:                copy += 2;
                    831:                break;
                    832:        case 'd':
                    833:                if (copy[1] != ':')
                    834:                        break;
                    835:                modifiers |= FORMAT_DIRNAME;
                    836:                copy += 2;
                    837:                break;
                    838:        case 't':
                    839:                if (copy[1] != ':')
                    840:                        break;
                    841:                modifiers |= FORMAT_TIMESTRING;
                    842:                copy += 2;
                    843:                break;
1.98      nicm      844:        case 's':
                    845:                if (copy[1] != '/')
                    846:                        break;
                    847:                from = copy + 2;
                    848:                for (copy = from; *copy != '\0' && *copy != '/'; copy++)
                    849:                        /* nothing */;
                    850:                if (copy[0] != '/' || copy == from) {
                    851:                        copy = copy0;
                    852:                        break;
                    853:                }
                    854:                copy[0] = '\0';
                    855:                to = copy + 1;
                    856:                for (copy = to; *copy != '\0' && *copy != '/'; copy++)
                    857:                        /* nothing */;
                    858:                if (copy[0] != '/' || copy[1] != ':') {
                    859:                        copy = copy0;
                    860:                        break;
                    861:                }
                    862:                copy[0] = '\0';
                    863:
                    864:                modifiers |= FORMAT_SUBSTITUTE;
                    865:                copy += 2;
                    866:                break;
1.30      nicm      867:        }
                    868:
1.114     nicm      869:        /* Is this a comparison or a conditional? */
                    870:        if (compare != 0) {
                    871:                /* Comparison: compare comma-separated left and right. */
                    872:                if (format_choose(copy, &left, &right) != 0)
                    873:                        goto fail;
                    874:                left = format_expand(ft, left);
                    875:                right = format_expand(ft, right);
                    876:                if (compare == 1 && strcmp(left, right) == 0)
                    877:                        value = xstrdup("1");
                    878:                else if (compare == -1 && strcmp(left, right) != 0)
                    879:                        value = xstrdup("1");
                    880:                else
                    881:                        value = xstrdup("0");
                    882:                free(right);
                    883:                free(left);
                    884:        } else if (*copy == '?') {
                    885:                /* Conditional: check first and choose second or third. */
                    886:                ptr = format_skip(copy);
1.1       nicm      887:                if (ptr == NULL)
                    888:                        goto fail;
                    889:                *ptr = '\0';
                    890:
1.98      nicm      891:                found = format_find(ft, copy + 1, modifiers);
1.121     nicm      892:                if (found == NULL)
                    893:                        found = format_expand(ft, copy + 1);
1.114     nicm      894:                if (format_choose(ptr + 1, &left, &right) != 0)
1.89      nicm      895:                        goto fail;
                    896:
1.114     nicm      897:                if (format_true(found))
                    898:                        value = format_expand(ft, left);
                    899:                else
                    900:                        value = format_expand(ft, right);
1.98      nicm      901:                free(found);
1.1       nicm      902:        } else {
1.114     nicm      903:                /* Neither: look up directly. */
1.98      nicm      904:                value = format_find(ft, copy, modifiers);
1.1       nicm      905:                if (value == NULL)
1.98      nicm      906:                        value = xstrdup("");
                    907:        }
                    908:
                    909:        /* Perform substitution if any. */
                    910:        if (modifiers & FORMAT_SUBSTITUTE) {
                    911:                fromlen = strlen(from);
                    912:                tolen = strlen(to);
                    913:
                    914:                newlen = strlen(value) + 1;
                    915:                copy = new = xmalloc(newlen);
                    916:                for (ptr = value; *ptr != '\0'; /* nothing */) {
                    917:                        if (strncmp(ptr, from, fromlen) != 0) {
                    918:                                *new++ = *ptr++;
                    919:                                continue;
                    920:                        }
                    921:                        used = new - copy;
                    922:
                    923:                        newlen += tolen;
                    924:                        copy = xrealloc(copy, newlen);
                    925:
                    926:                        new = copy + used;
                    927:                        memcpy(new, to, tolen);
                    928:
                    929:                        new += tolen;
                    930:                        ptr += fromlen;
                    931:                }
                    932:                *new = '\0';
                    933:                free(value);
                    934:                value = copy;
1.1       nicm      935:        }
                    936:
1.30      nicm      937:        /* Truncate the value if needed. */
1.105     nicm      938:        if (limit > 0) {
1.98      nicm      939:                new = utf8_trimcstr(value, limit);
1.105     nicm      940:                free(value);
                    941:                value = new;
                    942:        } else if (limit < 0) {
                    943:                new = utf8_rtrimcstr(value, -limit);
1.98      nicm      944:                free(value);
                    945:                value = new;
1.44      nicm      946:        }
1.30      nicm      947:
1.1       nicm      948:        /* Expand the buffer and copy in the value. */
1.98      nicm      949:        valuelen = strlen(value);
1.1       nicm      950:        while (*len - *off < valuelen + 1) {
1.50      nicm      951:                *buf = xreallocarray(*buf, 2, *len);
1.1       nicm      952:                *len *= 2;
                    953:        }
                    954:        memcpy(*buf + *off, value, valuelen);
                    955:        *off += valuelen;
                    956:
1.98      nicm      957:        free(value);
1.30      nicm      958:        free(copy0);
1.1       nicm      959:        return (0);
                    960:
                    961: fail:
1.30      nicm      962:        free(copy0);
1.1       nicm      963:        return (-1);
                    964: }
                    965:
1.58      nicm      966: /* Expand keys in a template, passing through strftime first. */
                    967: char *
                    968: format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
                    969: {
                    970:        struct tm       *tm;
1.107     nicm      971:        char             s[2048];
1.58      nicm      972:
1.67      nicm      973:        if (fmt == NULL || *fmt == '\0')
1.58      nicm      974:                return (xstrdup(""));
                    975:
                    976:        tm = localtime(&t);
                    977:
1.107     nicm      978:        if (strftime(s, sizeof s, fmt, tm) == 0)
                    979:                return (xstrdup(""));
1.58      nicm      980:
1.107     nicm      981:        return (format_expand(ft, s));
1.58      nicm      982: }
                    983:
1.1       nicm      984: /* Expand keys in a template. */
                    985: char *
                    986: format_expand(struct format_tree *ft, const char *fmt)
                    987: {
1.113     nicm      988:        char            *buf, *out;
1.79      nicm      989:        const char      *ptr, *s, *saved = fmt;
1.86      nicm      990:        size_t           off, len, n, outlen;
1.31      nicm      991:        int              ch, brackets;
1.58      nicm      992:
                    993:        if (fmt == NULL)
                    994:                return (xstrdup(""));
1.1       nicm      995:
                    996:        len = 64;
                    997:        buf = xmalloc(len);
                    998:        off = 0;
                    999:
                   1000:        while (*fmt != '\0') {
                   1001:                if (*fmt != '#') {
                   1002:                        while (len - off < 2) {
1.50      nicm     1003:                                buf = xreallocarray(buf, 2, len);
1.1       nicm     1004:                                len *= 2;
                   1005:                        }
                   1006:                        buf[off++] = *fmt++;
                   1007:                        continue;
                   1008:                }
                   1009:                fmt++;
                   1010:
                   1011:                ch = (u_char) *fmt++;
                   1012:                switch (ch) {
1.68      nicm     1013:                case '(':
                   1014:                        brackets = 1;
                   1015:                        for (ptr = fmt; *ptr != '\0'; ptr++) {
                   1016:                                if (*ptr == '(')
                   1017:                                        brackets++;
                   1018:                                if (*ptr == ')' && --brackets == 0)
                   1019:                                        break;
                   1020:                        }
                   1021:                        if (*ptr != ')' || brackets != 0)
                   1022:                                break;
                   1023:                        n = ptr - fmt;
                   1024:
1.114     nicm     1025:                        if (ft->flags & FORMAT_NOJOBS)
                   1026:                                out = xstrdup("");
                   1027:                        else
                   1028:                                out = format_job_get(ft, xstrndup(fmt, n));
1.86      nicm     1029:                        outlen = strlen(out);
1.68      nicm     1030:
1.86      nicm     1031:                        while (len - off < outlen + 1) {
1.68      nicm     1032:                                buf = xreallocarray(buf, 2, len);
                   1033:                                len *= 2;
                   1034:                        }
1.86      nicm     1035:                        memcpy(buf + off, out, outlen);
                   1036:                        off += outlen;
                   1037:
                   1038:                        free(out);
1.68      nicm     1039:
                   1040:                        fmt += n + 1;
                   1041:                        continue;
1.1       nicm     1042:                case '{':
1.31      nicm     1043:                        brackets = 1;
                   1044:                        for (ptr = fmt; *ptr != '\0'; ptr++) {
                   1045:                                if (*ptr == '{')
                   1046:                                        brackets++;
                   1047:                                if (*ptr == '}' && --brackets == 0)
                   1048:                                        break;
                   1049:                        }
                   1050:                        if (*ptr != '}' || brackets != 0)
1.1       nicm     1051:                                break;
                   1052:                        n = ptr - fmt;
                   1053:
                   1054:                        if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
                   1055:                                break;
                   1056:                        fmt += n + 1;
1.40      nicm     1057:                        continue;
                   1058:                case '#':
                   1059:                        while (len - off < 2) {
1.50      nicm     1060:                                buf = xreallocarray(buf, 2, len);
1.40      nicm     1061:                                len *= 2;
                   1062:                        }
                   1063:                        buf[off++] = '#';
1.1       nicm     1064:                        continue;
                   1065:                default:
1.25      nicm     1066:                        s = NULL;
                   1067:                        if (ch >= 'A' && ch <= 'Z')
                   1068:                                s = format_upper[ch - 'A'];
                   1069:                        else if (ch >= 'a' && ch <= 'z')
                   1070:                                s = format_lower[ch - 'a'];
                   1071:                        if (s == NULL) {
                   1072:                                while (len - off < 3) {
1.50      nicm     1073:                                        buf = xreallocarray(buf, 2, len);
1.25      nicm     1074:                                        len *= 2;
1.1       nicm     1075:                                }
1.25      nicm     1076:                                buf[off++] = '#';
                   1077:                                buf[off++] = ch;
                   1078:                                continue;
1.1       nicm     1079:                        }
1.25      nicm     1080:                        n = strlen(s);
                   1081:                        if (format_replace(ft, s, n, &buf, &len, &off) != 0)
                   1082:                                break;
1.1       nicm     1083:                        continue;
                   1084:                }
                   1085:
                   1086:                break;
                   1087:        }
                   1088:        buf[off] = '\0';
                   1089:
1.79      nicm     1090:        log_debug("format '%s' -> '%s'", saved, buf);
1.1       nicm     1091:        return (buf);
1.123     nicm     1092: }
                   1093:
                   1094: /* Expand a single string. */
                   1095: char *
                   1096: format_single(struct cmdq_item *item, const char *fmt, struct client *c,
                   1097:     struct session *s, struct winlink *wl, struct window_pane *wp)
                   1098: {
                   1099:        struct format_tree      *ft;
                   1100:        char                    *expanded;
                   1101:
                   1102:        ft = format_create(item, FORMAT_NONE, 0);
                   1103:        format_defaults(ft, c, s, wl, wp);
                   1104:
                   1105:        expanded = format_expand(ft, fmt);
                   1106:        format_free(ft);
                   1107:        return (expanded);
1.1       nicm     1108: }
                   1109:
1.57      nicm     1110: /* Set defaults for any of arguments that are not NULL. */
                   1111: void
                   1112: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
                   1113:     struct winlink *wl, struct window_pane *wp)
                   1114: {
                   1115:        if (s == NULL && c != NULL)
                   1116:                s = c->session;
                   1117:        if (wl == NULL && s != NULL)
                   1118:                wl = s->curw;
                   1119:        if (wp == NULL && wl != NULL)
                   1120:                wp = wl->window->active;
                   1121:
                   1122:        if (c != NULL)
                   1123:                format_defaults_client(ft, c);
                   1124:        if (s != NULL)
                   1125:                format_defaults_session(ft, s);
1.129     nicm     1126:        if (wl != NULL)
                   1127:                format_defaults_winlink(ft, wl);
1.57      nicm     1128:        if (wp != NULL)
                   1129:                format_defaults_pane(ft, wp);
                   1130: }
                   1131:
1.1       nicm     1132: /* Set default format keys for a session. */
1.108     nicm     1133: static void
1.57      nicm     1134: format_defaults_session(struct format_tree *ft, struct session *s)
1.1       nicm     1135: {
                   1136:        struct session_group    *sg;
                   1137:
1.54      nicm     1138:        ft->s = s;
                   1139:
1.1       nicm     1140:        format_add(ft, "session_name", "%s", s->name);
                   1141:        format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
                   1142:        format_add(ft, "session_width", "%u", s->sx);
                   1143:        format_add(ft, "session_height", "%u", s->sy);
1.23      nicm     1144:        format_add(ft, "session_id", "$%u", s->id);
1.1       nicm     1145:
1.122     nicm     1146:        sg = session_group_contains(s);
1.1       nicm     1147:        format_add(ft, "session_grouped", "%d", sg != NULL);
                   1148:        if (sg != NULL)
1.122     nicm     1149:                format_add(ft, "session_group", "%s", sg->name);
1.1       nicm     1150:
1.87      nicm     1151:        format_add_tv(ft, "session_created", &s->creation_time);
                   1152:        format_add_tv(ft, "session_last_attached", &s->last_attached_time);
                   1153:        format_add_tv(ft, "session_activity", &s->activity_time);
1.1       nicm     1154:
1.41      nicm     1155:        format_add(ft, "session_attached", "%u", s->attached);
1.59      nicm     1156:        format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66      nicm     1157:
1.80      nicm     1158:        format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.3       nicm     1159: }
                   1160:
                   1161: /* Set default format keys for a client. */
1.108     nicm     1162: static void
1.57      nicm     1163: format_defaults_client(struct format_tree *ft, struct client *c)
1.3       nicm     1164: {
1.60      nicm     1165:        struct session  *s;
1.103     nicm     1166:        const char      *name;
1.115     nicm     1167:        struct tty      *tty = &c->tty;
                   1168:        const char      *types[] = TTY_TYPES;
1.3       nicm     1169:
1.54      nicm     1170:        if (ft->s == NULL)
                   1171:                ft->s = c->session;
                   1172:
1.124     nicm     1173:        format_add(ft, "client_name", "%s", c->name);
1.72      nicm     1174:        format_add(ft, "client_pid", "%ld", (long) c->pid);
1.115     nicm     1175:        format_add(ft, "client_height", "%u", tty->sy);
                   1176:        format_add(ft, "client_width", "%u", tty->sx);
1.124     nicm     1177:        format_add(ft, "client_tty", "%s", c->ttyname);
1.75      nicm     1178:        format_add(ft, "client_control_mode", "%d",
                   1179:                !!(c->flags & CLIENT_CONTROL));
1.3       nicm     1180:
1.115     nicm     1181:        if (tty->term_name != NULL)
                   1182:                format_add(ft, "client_termname", "%s", tty->term_name);
                   1183:        if (tty->term_name != NULL)
                   1184:                format_add(ft, "client_termtype", "%s", types[tty->term_type]);
                   1185:
1.87      nicm     1186:        format_add_tv(ft, "client_created", &c->creation_time);
                   1187:        format_add_tv(ft, "client_activity", &c->activity_time);
1.126     nicm     1188:
                   1189:        format_add(ft, "client_written", "%zu", c->written);
                   1190:        format_add(ft, "client_discarded", "%zu", c->discarded);
1.14      nicm     1191:
1.103     nicm     1192:        name = server_client_get_key_table(c);
                   1193:        if (strcmp(c->keytable->name, name) == 0)
1.61      nicm     1194:                format_add(ft, "client_prefix", "%d", 0);
                   1195:        else
                   1196:                format_add(ft, "client_prefix", "%d", 1);
                   1197:        format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3       nicm     1198:
1.115     nicm     1199:        if (tty->flags & TTY_UTF8)
1.3       nicm     1200:                format_add(ft, "client_utf8", "%d", 1);
                   1201:        else
                   1202:                format_add(ft, "client_utf8", "%d", 0);
                   1203:
                   1204:        if (c->flags & CLIENT_READONLY)
                   1205:                format_add(ft, "client_readonly", "%d", 1);
                   1206:        else
                   1207:                format_add(ft, "client_readonly", "%d", 0);
1.15      nicm     1208:
                   1209:        s = c->session;
                   1210:        if (s != NULL)
                   1211:                format_add(ft, "client_session", "%s", s->name);
                   1212:        s = c->last_session;
                   1213:        if (s != NULL && session_alive(s))
                   1214:                format_add(ft, "client_last_session", "%s", s->name);
1.1       nicm     1215: }
                   1216:
1.32      nicm     1217: /* Set default format keys for a window. */
                   1218: void
1.57      nicm     1219: format_defaults_window(struct format_tree *ft, struct window *w)
1.32      nicm     1220: {
1.54      nicm     1221:        ft->w = w;
                   1222:
1.87      nicm     1223:        format_add_tv(ft, "window_activity", &w->activity_time);
1.32      nicm     1224:        format_add(ft, "window_id", "@%u", w->id);
                   1225:        format_add(ft, "window_name", "%s", w->name);
                   1226:        format_add(ft, "window_width", "%u", w->sx);
                   1227:        format_add(ft, "window_height", "%u", w->sy);
1.80      nicm     1228:        format_add_cb(ft, "window_layout", format_cb_window_layout);
1.96      nicm     1229:        format_add_cb(ft, "window_visible_layout",
                   1230:            format_cb_window_visible_layout);
1.32      nicm     1231:        format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59      nicm     1232:        format_add(ft, "window_zoomed_flag", "%d",
1.53      nicm     1233:            !!(w->flags & WINDOW_ZOOMED));
1.32      nicm     1234: }
                   1235:
1.1       nicm     1236: /* Set default format keys for a winlink. */
1.108     nicm     1237: static void
1.129     nicm     1238: format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
1.1       nicm     1239: {
1.129     nicm     1240:        struct session  *s = wl->session;
1.1       nicm     1241:        struct window   *w = wl->window;
                   1242:
1.54      nicm     1243:        if (ft->w == NULL)
                   1244:                ft->w = wl->window;
                   1245:
1.57      nicm     1246:        format_defaults_window(ft, w);
1.32      nicm     1247:
1.1       nicm     1248:        format_add(ft, "window_index", "%d", wl->idx);
1.129     nicm     1249:        format_add(ft, "window_flags", "%s", window_printable_flags(wl));
1.1       nicm     1250:        format_add(ft, "window_active", "%d", wl == s->curw);
1.29      nicm     1251:
1.59      nicm     1252:        format_add(ft, "window_bell_flag", "%d",
1.29      nicm     1253:            !!(wl->flags & WINLINK_BELL));
1.59      nicm     1254:        format_add(ft, "window_activity_flag", "%d",
1.29      nicm     1255:            !!(wl->flags & WINLINK_ACTIVITY));
1.59      nicm     1256:        format_add(ft, "window_silence_flag", "%d",
1.29      nicm     1257:            !!(wl->flags & WINLINK_SILENCE));
1.59      nicm     1258:        format_add(ft, "window_last_flag", "%d",
1.49      nicm     1259:            !!(wl == TAILQ_FIRST(&s->lastw)));
1.64      nicm     1260:        format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.1       nicm     1261: }
                   1262:
                   1263: /* Set default format keys for a window pane. */
                   1264: void
1.57      nicm     1265: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1       nicm     1266: {
1.80      nicm     1267:        struct grid     *gd = wp->base.grid;
                   1268:        u_int            idx;
1.85      nicm     1269:        int              status, scroll_position;
1.54      nicm     1270:
                   1271:        if (ft->w == NULL)
                   1272:                ft->w = wp->window;
1.79      nicm     1273:        ft->wp = wp;
1.1       nicm     1274:
1.16      nicm     1275:        format_add(ft, "history_size", "%u", gd->hsize);
                   1276:        format_add(ft, "history_limit", "%u", gd->hlimit);
1.80      nicm     1277:        format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.1       nicm     1278:
1.4       nicm     1279:        if (window_pane_index(wp, &idx) != 0)
                   1280:                fatalx("index not found");
1.16      nicm     1281:        format_add(ft, "pane_index", "%u", idx);
1.4       nicm     1282:
1.1       nicm     1283:        format_add(ft, "pane_width", "%u", wp->sx);
                   1284:        format_add(ft, "pane_height", "%u", wp->sy);
                   1285:        format_add(ft, "pane_title", "%s", wp->base.title);
                   1286:        format_add(ft, "pane_id", "%%%u", wp->id);
                   1287:        format_add(ft, "pane_active", "%d", wp == wp->window->active);
1.55      nicm     1288:        format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
                   1289:
                   1290:        status = wp->status;
                   1291:        if (wp->fd == -1 && WIFEXITED(status))
                   1292:                format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1       nicm     1293:        format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47      nicm     1294:
                   1295:        if (window_pane_visible(wp)) {
                   1296:                format_add(ft, "pane_left", "%u", wp->xoff);
                   1297:                format_add(ft, "pane_top", "%u", wp->yoff);
                   1298:                format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
                   1299:                format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
                   1300:        }
1.16      nicm     1301:
                   1302:        format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
1.26      nicm     1303:        format_add(ft, "pane_synchronized", "%d",
1.90      nicm     1304:            !!options_get_number(wp->window->options, "synchronize-panes"));
1.16      nicm     1305:
1.71      nicm     1306:        format_add(ft, "pane_tty", "%s", wp->tty);
1.16      nicm     1307:        format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79      nicm     1308:        format_add_cb(ft, "pane_start_command", format_cb_start_command);
                   1309:        format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.16      nicm     1310:
1.59      nicm     1311:        format_add(ft, "cursor_x", "%u", wp->base.cx);
                   1312:        format_add(ft, "cursor_y", "%u", wp->base.cy);
                   1313:        format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
                   1314:        format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.85      nicm     1315:
                   1316:        scroll_position = window_copy_scroll_position(wp);
                   1317:        if (scroll_position != -1)
                   1318:                format_add(ft, "scroll_position", "%d", scroll_position);
1.16      nicm     1319:
                   1320:        format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59      nicm     1321:        format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
                   1322:        format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16      nicm     1323:
                   1324:        format_add(ft, "cursor_flag", "%d",
                   1325:            !!(wp->base.mode & MODE_CURSOR));
                   1326:        format_add(ft, "insert_flag", "%d",
                   1327:            !!(wp->base.mode & MODE_INSERT));
                   1328:        format_add(ft, "keypad_cursor_flag", "%d",
                   1329:            !!(wp->base.mode & MODE_KCURSOR));
                   1330:        format_add(ft, "keypad_flag", "%d",
                   1331:            !!(wp->base.mode & MODE_KKEYPAD));
                   1332:        format_add(ft, "wrap_flag", "%d",
                   1333:            !!(wp->base.mode & MODE_WRAP));
                   1334:
1.62      nicm     1335:        format_add(ft, "mouse_any_flag", "%d",
1.119     nicm     1336:            !!(wp->base.mode & ALL_MOUSE_MODES));
1.16      nicm     1337:        format_add(ft, "mouse_standard_flag", "%d",
                   1338:            !!(wp->base.mode & MODE_MOUSE_STANDARD));
                   1339:        format_add(ft, "mouse_button_flag", "%d",
                   1340:            !!(wp->base.mode & MODE_MOUSE_BUTTON));
1.119     nicm     1341:        format_add(ft, "mouse_all_flag", "%d",
                   1342:            !!(wp->base.mode & MODE_MOUSE_ALL));
1.19      nicm     1343:
1.80      nicm     1344:        format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8       nicm     1345: }
                   1346:
1.19      nicm     1347: /* Set default format keys for paste buffer. */
1.8       nicm     1348: void
1.94      nicm     1349: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
1.8       nicm     1350: {
1.81      nicm     1351:        size_t   bufsize;
1.42      nicm     1352:        char    *s;
1.8       nicm     1353:
1.81      nicm     1354:        paste_buffer_data(pb, &bufsize);
                   1355:        format_add(ft, "buffer_size", "%zu", bufsize);
                   1356:        format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.8       nicm     1357:
1.94      nicm     1358:        s = paste_make_sample(pb);
1.42      nicm     1359:        format_add(ft, "buffer_sample", "%s", s);
                   1360:        free(s);
1.1       nicm     1361: }