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

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