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

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