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

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