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

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