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

1.180   ! nicm        1: /* $OpenBSD: format.c,v 1.179 2019/03/15 10:04:13 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.164     nicm       55: static void     format_defaults_winlink(struct format_tree *, struct winlink *);
1.1       nicm       56:
1.68      nicm       57: /* Entry in format job tree. */
                     58: struct format_job {
1.131     nicm       59:        struct client           *client;
1.120     nicm       60:        u_int                    tag;
1.68      nicm       61:        const char              *cmd;
1.113     nicm       62:        const char              *expanded;
1.68      nicm       63:
                     64:        time_t                   last;
                     65:        char                    *out;
1.127     nicm       66:        int                      updated;
1.68      nicm       67:
                     68:        struct job              *job;
                     69:        int                      status;
                     70:
                     71:        RB_ENTRY(format_job)     entry;
                     72: };
                     73:
                     74: /* Format job tree. */
1.108     nicm       75: static struct event format_job_event;
                     76: static int format_job_cmp(struct format_job *, struct format_job *);
1.109     nicm       77: static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
1.108     nicm       78: RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
1.68      nicm       79:
                     80: /* Format job tree comparison function. */
1.108     nicm       81: static int
1.68      nicm       82: format_job_cmp(struct format_job *fj1, struct format_job *fj2)
                     83: {
1.120     nicm       84:        if (fj1->tag < fj2->tag)
                     85:                return (-1);
                     86:        if (fj1->tag > fj2->tag)
                     87:                return (1);
1.68      nicm       88:        return (strcmp(fj1->cmd, fj2->cmd));
                     89: }
                     90:
1.87      nicm       91: /* Format modifiers. */
                     92: #define FORMAT_TIMESTRING 0x1
                     93: #define FORMAT_BASENAME 0x2
                     94: #define FORMAT_DIRNAME 0x4
1.169     nicm       95: #define FORMAT_QUOTE 0x8
                     96: #define FORMAT_LITERAL 0x10
1.170     nicm       97: #define FORMAT_EXPAND 0x20
1.175     nicm       98: #define FORMAT_EXPANDTIME 0x40
                     99: #define FORMAT_SESSIONS 0x80
                    100: #define FORMAT_WINDOWS 0x100
                    101: #define FORMAT_PANES 0x200
1.87      nicm      102:
1.178     nicm      103: /* Limit on recursion. */
                    104: #define FORMAT_LOOP_LIMIT 10
                    105:
1.54      nicm      106: /* Entry in format tree. */
                    107: struct format_entry {
1.79      nicm      108:        char                    *key;
                    109:        char                    *value;
1.87      nicm      110:        time_t                   t;
1.79      nicm      111:        format_cb                cb;
                    112:        RB_ENTRY(format_entry)   entry;
1.54      nicm      113: };
                    114:
1.68      nicm      115: /* Format entry tree. */
1.54      nicm      116: struct format_tree {
1.164     nicm      117:        struct client           *c;
                    118:        struct session          *s;
                    119:        struct winlink          *wl;
1.79      nicm      120:        struct window           *w;
                    121:        struct window_pane      *wp;
1.54      nicm      122:
1.172     nicm      123:        struct cmdq_item        *item;
1.131     nicm      124:        struct client           *client;
1.120     nicm      125:        u_int                    tag;
1.84      nicm      126:        int                      flags;
1.177     nicm      127:        time_t                   time;
1.178     nicm      128:        u_int                    loop;
1.68      nicm      129:
                    130:        RB_HEAD(format_entry_tree, format_entry) tree;
1.54      nicm      131: };
1.108     nicm      132: static int format_entry_cmp(struct format_entry *, struct format_entry *);
                    133: RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
1.54      nicm      134:
1.180   ! nicm      135: /* Format modifier. */
1.169     nicm      136: struct format_modifier {
                    137:        char      modifier[3];
                    138:        u_int     size;
                    139:
                    140:        char    **argv;
                    141:        int       argc;
                    142: };
                    143:
1.68      nicm      144: /* Format entry tree comparison function. */
1.108     nicm      145: static int
1.68      nicm      146: format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
1.1       nicm      147: {
                    148:        return (strcmp(fe1->key, fe2->key));
                    149: }
                    150:
1.25      nicm      151: /* Single-character uppercase aliases. */
1.108     nicm      152: static const char *format_upper[] = {
1.1       nicm      153:        NULL,           /* A */
                    154:        NULL,           /* B */
                    155:        NULL,           /* C */
                    156:        "pane_id",      /* D */
                    157:        NULL,           /* E */
                    158:        "window_flags", /* F */
                    159:        NULL,           /* G */
                    160:        "host",         /* H */
                    161:        "window_index", /* I */
                    162:        NULL,           /* J */
                    163:        NULL,           /* K */
                    164:        NULL,           /* L */
                    165:        NULL,           /* M */
                    166:        NULL,           /* N */
                    167:        NULL,           /* O */
                    168:        "pane_index",   /* P */
                    169:        NULL,           /* Q */
                    170:        NULL,           /* R */
                    171:        "session_name", /* S */
                    172:        "pane_title",   /* T */
                    173:        NULL,           /* U */
                    174:        NULL,           /* V */
                    175:        "window_name",  /* W */
                    176:        NULL,           /* X */
                    177:        NULL,           /* Y */
                    178:        NULL            /* Z */
                    179: };
                    180:
1.25      nicm      181: /* Single-character lowercase aliases. */
1.108     nicm      182: static const char *format_lower[] = {
1.25      nicm      183:        NULL,           /* a */
                    184:        NULL,           /* b */
                    185:        NULL,           /* c */
                    186:        NULL,           /* d */
                    187:        NULL,           /* e */
                    188:        NULL,           /* f */
                    189:        NULL,           /* g */
                    190:        "host_short",   /* h */
                    191:        NULL,           /* i */
                    192:        NULL,           /* j */
                    193:        NULL,           /* k */
                    194:        NULL,           /* l */
                    195:        NULL,           /* m */
                    196:        NULL,           /* n */
                    197:        NULL,           /* o */
                    198:        NULL,           /* p */
                    199:        NULL,           /* q */
                    200:        NULL,           /* r */
                    201:        NULL,           /* s */
                    202:        NULL,           /* t */
                    203:        NULL,           /* u */
                    204:        NULL,           /* v */
                    205:        NULL,           /* w */
                    206:        NULL,           /* x */
                    207:        NULL,           /* y */
                    208:        NULL            /* z */
                    209: };
                    210:
1.179     nicm      211: /* Is logging enabled? */
                    212: static inline int
                    213: format_logging(struct format_tree *ft)
                    214: {
                    215:        return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE));
                    216: }
                    217:
                    218: /* Log a message if verbose. */
                    219: static void printflike(3, 4)
                    220: format_log1(struct format_tree *ft, const char *from, const char *fmt, ...)
                    221: {
                    222:        va_list                  ap;
                    223:        char                    *s;
                    224:        static const char        spaces[] = "          ";
                    225:
                    226:        if (!format_logging(ft))
                    227:                return;
                    228:
                    229:        va_start(ap, fmt);
                    230:        vasprintf(&s, fmt, ap);
                    231:        va_end(ap);
                    232:
                    233:        log_debug("%s: %s", from, s);
                    234:        if (ft->item != NULL)
                    235:                cmdq_print(ft->item, "#%.*s%s", ft->loop, spaces, s);
                    236:
                    237:        free(s);
                    238: }
                    239: #define format_log(ft, fmt, ...) format_log1(ft, __func__, fmt, ##__VA_ARGS__)
                    240:
1.127     nicm      241: /* Format job update callback. */
1.108     nicm      242: static void
1.127     nicm      243: format_job_update(struct job *job)
                    244: {
1.160     nicm      245:        struct format_job       *fj = job_get_data(job);
                    246:        struct evbuffer         *evb = job_get_event(job)->input;
1.151     nicm      247:        char                    *line = NULL, *next;
1.127     nicm      248:        time_t                   t;
                    249:
1.151     nicm      250:        while ((next = evbuffer_readline(evb)) != NULL) {
                    251:                free(line);
                    252:                line = next;
                    253:        }
                    254:        if (line == NULL)
1.127     nicm      255:                return;
                    256:        fj->updated = 1;
                    257:
                    258:        free(fj->out);
                    259:        fj->out = line;
                    260:
1.136     nicm      261:        log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out);
1.127     nicm      262:
1.142     nicm      263:        t = time(NULL);
1.127     nicm      264:        if (fj->status && fj->last != t) {
1.136     nicm      265:                if (fj->client != NULL)
                    266:                        server_status_client(fj->client);
1.127     nicm      267:                fj->last = t;
                    268:        }
                    269: }
                    270:
                    271: /* Format job complete callback. */
                    272: static void
                    273: format_job_complete(struct job *job)
1.68      nicm      274: {
1.160     nicm      275:        struct format_job       *fj = job_get_data(job);
                    276:        struct evbuffer         *evb = job_get_event(job)->input;
1.68      nicm      277:        char                    *line, *buf;
                    278:        size_t                   len;
                    279:
                    280:        fj->job = NULL;
                    281:
                    282:        buf = NULL;
1.160     nicm      283:        if ((line = evbuffer_readline(evb)) == NULL) {
                    284:                len = EVBUFFER_LENGTH(evb);
1.68      nicm      285:                buf = xmalloc(len + 1);
                    286:                if (len != 0)
1.160     nicm      287:                        memcpy(buf, EVBUFFER_DATA(evb), len);
1.68      nicm      288:                buf[len] = '\0';
                    289:        } else
                    290:                buf = line;
1.127     nicm      291:
1.136     nicm      292:        log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf);
                    293:
1.127     nicm      294:        if (*buf != '\0' || !fj->updated) {
                    295:                free(fj->out);
                    296:                fj->out = buf;
                    297:        } else
                    298:                free(buf);
1.68      nicm      299:
                    300:        if (fj->status) {
1.131     nicm      301:                if (fj->client != NULL)
                    302:                        server_status_client(fj->client);
1.68      nicm      303:                fj->status = 0;
                    304:        }
                    305: }
                    306:
                    307: /* Find a job. */
1.108     nicm      308: static char *
1.68      nicm      309: format_job_get(struct format_tree *ft, const char *cmd)
                    310: {
1.131     nicm      311:        struct format_job_tree  *jobs;
1.113     nicm      312:        struct format_job        fj0, *fj;
                    313:        time_t                   t;
                    314:        char                    *expanded;
                    315:        int                      force;
1.68      nicm      316:
1.131     nicm      317:        if (ft->client == NULL)
                    318:                jobs = &format_jobs;
                    319:        else if (ft->client->jobs != NULL)
                    320:                jobs = ft->client->jobs;
                    321:        else {
                    322:                jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
                    323:                RB_INIT(jobs);
                    324:        }
                    325:
1.120     nicm      326:        fj0.tag = ft->tag;
1.68      nicm      327:        fj0.cmd = cmd;
1.131     nicm      328:        if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
1.68      nicm      329:                fj = xcalloc(1, sizeof *fj);
1.131     nicm      330:                fj->client = ft->client;
1.120     nicm      331:                fj->tag = ft->tag;
1.68      nicm      332:                fj->cmd = xstrdup(cmd);
1.113     nicm      333:                fj->expanded = NULL;
1.68      nicm      334:
                    335:                xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
                    336:
1.131     nicm      337:                RB_INSERT(format_job_tree, jobs, fj);
1.68      nicm      338:        }
                    339:
1.113     nicm      340:        expanded = format_expand(ft, cmd);
                    341:        if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
                    342:                free((void *)fj->expanded);
                    343:                fj->expanded = xstrdup(expanded);
                    344:                force = 1;
                    345:        } else
                    346:                force = (ft->flags & FORMAT_FORCE);
                    347:
1.84      nicm      348:        t = time(NULL);
1.113     nicm      349:        if (fj->job == NULL && (force || fj->last != t)) {
1.163     nicm      350:                fj->job = job_run(expanded, NULL,
                    351:                    server_client_get_cwd(ft->client, NULL), format_job_update,
1.153     nicm      352:                    format_job_complete, NULL, fj, JOB_NOWAIT);
1.68      nicm      353:                if (fj->job == NULL) {
                    354:                        free(fj->out);
                    355:                        xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
                    356:                }
1.84      nicm      357:                fj->last = t;
1.137     nicm      358:                fj->updated = 0;
1.68      nicm      359:        }
1.84      nicm      360:
                    361:        if (ft->flags & FORMAT_STATUS)
                    362:                fj->status = 1;
1.68      nicm      363:
1.113     nicm      364:        free(expanded);
1.86      nicm      365:        return (format_expand(ft, fj->out));
1.68      nicm      366: }
                    367:
                    368: /* Remove old jobs. */
1.108     nicm      369: static void
1.131     nicm      370: format_job_tidy(struct format_job_tree *jobs, int force)
1.68      nicm      371: {
                    372:        struct format_job       *fj, *fj1;
                    373:        time_t                   now;
                    374:
                    375:        now = time(NULL);
1.131     nicm      376:        RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
                    377:                if (!force && (fj->last > now || now - fj->last < 3600))
1.68      nicm      378:                        continue;
1.131     nicm      379:                RB_REMOVE(format_job_tree, jobs, fj);
1.68      nicm      380:
1.77      nicm      381:                log_debug("%s: %s", __func__, fj->cmd);
                    382:
1.68      nicm      383:                if (fj->job != NULL)
                    384:                        job_free(fj->job);
                    385:
1.113     nicm      386:                free((void *)fj->expanded);
1.78      nicm      387:                free((void *)fj->cmd);
1.68      nicm      388:                free(fj->out);
                    389:
                    390:                free(fj);
                    391:        }
1.131     nicm      392: }
                    393:
                    394: /* Remove old jobs for client. */
                    395: void
                    396: format_lost_client(struct client *c)
                    397: {
                    398:        if (c->jobs != NULL)
                    399:                format_job_tidy(c->jobs, 1);
                    400:        free(c->jobs);
                    401: }
                    402:
                    403: /* Remove old jobs periodically. */
                    404: static void
                    405: format_job_timer(__unused int fd, __unused short events, __unused void *arg)
                    406: {
                    407:        struct client   *c;
                    408:        struct timeval   tv = { .tv_sec = 60 };
                    409:
                    410:        format_job_tidy(&format_jobs, 0);
                    411:        TAILQ_FOREACH(c, &clients, entry) {
                    412:                if (c->jobs != NULL)
                    413:                        format_job_tidy(c->jobs, 0);
                    414:        }
1.77      nicm      415:
                    416:        evtimer_del(&format_job_event);
                    417:        evtimer_add(&format_job_event, &tv);
1.68      nicm      418: }
                    419:
1.79      nicm      420: /* Callback for host. */
1.108     nicm      421: static void
1.99      nicm      422: format_cb_host(__unused struct format_tree *ft, struct format_entry *fe)
1.79      nicm      423: {
                    424:        char host[HOST_NAME_MAX + 1];
                    425:
                    426:        if (gethostname(host, sizeof host) != 0)
                    427:                fe->value = xstrdup("");
                    428:        else
                    429:                fe->value = xstrdup(host);
                    430: }
                    431:
                    432: /* Callback for host_short. */
1.108     nicm      433: static void
1.99      nicm      434: format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe)
1.79      nicm      435: {
                    436:        char host[HOST_NAME_MAX + 1], *cp;
                    437:
                    438:        if (gethostname(host, sizeof host) != 0)
                    439:                fe->value = xstrdup("");
                    440:        else {
                    441:                if ((cp = strchr(host, '.')) != NULL)
                    442:                        *cp = '\0';
                    443:                fe->value = xstrdup(host);
                    444:        }
                    445: }
                    446:
                    447: /* Callback for pid. */
1.108     nicm      448: static void
1.99      nicm      449: format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe)
1.79      nicm      450: {
                    451:        xasprintf(&fe->value, "%ld", (long)getpid());
                    452: }
                    453:
1.80      nicm      454: /* Callback for session_alerts. */
1.108     nicm      455: static void
1.80      nicm      456: format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
                    457: {
                    458:        struct session  *s = ft->s;
                    459:        struct winlink  *wl;
1.133     nicm      460:        char             alerts[1024], tmp[16];
1.80      nicm      461:
                    462:        if (s == NULL)
                    463:                return;
                    464:
                    465:        *alerts = '\0';
                    466:        RB_FOREACH(wl, winlinks, &s->windows) {
                    467:                if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
                    468:                        continue;
                    469:                xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
                    470:
                    471:                if (*alerts != '\0')
                    472:                        strlcat(alerts, ",", sizeof alerts);
                    473:                strlcat(alerts, tmp, sizeof alerts);
                    474:                if (wl->flags & WINLINK_ACTIVITY)
                    475:                        strlcat(alerts, "#", sizeof alerts);
                    476:                if (wl->flags & WINLINK_BELL)
                    477:                        strlcat(alerts, "!", sizeof alerts);
                    478:                if (wl->flags & WINLINK_SILENCE)
                    479:                        strlcat(alerts, "~", sizeof alerts);
                    480:        }
                    481:        fe->value = xstrdup(alerts);
                    482: }
                    483:
1.133     nicm      484: /* Callback for session_stack. */
                    485: static void
                    486: format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
                    487: {
                    488:        struct session  *s = ft->s;
                    489:        struct winlink  *wl;
                    490:        char             result[1024], tmp[16];
                    491:
                    492:        if (s == NULL)
                    493:                return;
                    494:
                    495:        xsnprintf(result, sizeof result, "%u", s->curw->idx);
                    496:        TAILQ_FOREACH(wl, &s->lastw, sentry) {
                    497:                xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
                    498:
                    499:                if (*result != '\0')
                    500:                        strlcat(result, ",", sizeof result);
                    501:                strlcat(result, tmp, sizeof result);
                    502:        }
                    503:        fe->value = xstrdup(result);
                    504: }
                    505:
                    506: /* Callback for window_stack_index. */
                    507: static void
                    508: format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe)
                    509: {
                    510:        struct session  *s = ft->wl->session;
                    511:        struct winlink  *wl;
                    512:        u_int            idx;
                    513:
                    514:        idx = 0;
                    515:        TAILQ_FOREACH(wl, &s->lastw, sentry) {
                    516:                idx++;
                    517:                if (wl == ft->wl)
                    518:                        break;
                    519:        }
                    520:        if (wl != NULL)
                    521:                xasprintf(&fe->value, "%u", idx);
                    522:        else
                    523:                fe->value = xstrdup("0");
                    524: }
                    525:
1.80      nicm      526: /* Callback for window_layout. */
1.108     nicm      527: static void
1.80      nicm      528: format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
                    529: {
                    530:        struct window   *w = ft->w;
                    531:
                    532:        if (w == NULL)
                    533:                return;
                    534:
                    535:        if (w->saved_layout_root != NULL)
                    536:                fe->value = layout_dump(w->saved_layout_root);
                    537:        else
                    538:                fe->value = layout_dump(w->layout_root);
                    539: }
                    540:
1.96      nicm      541: /* Callback for window_visible_layout. */
1.108     nicm      542: static void
1.96      nicm      543: format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe)
                    544: {
                    545:        struct window   *w = ft->w;
                    546:
                    547:        if (w == NULL)
                    548:                return;
                    549:
                    550:        fe->value = layout_dump(w->layout_root);
                    551: }
                    552:
1.79      nicm      553: /* Callback for pane_start_command. */
1.108     nicm      554: static void
1.79      nicm      555: format_cb_start_command(struct format_tree *ft, struct format_entry *fe)
                    556: {
                    557:        struct window_pane      *wp = ft->wp;
                    558:
                    559:        if (wp == NULL)
                    560:                return;
                    561:
                    562:        fe->value = cmd_stringify_argv(wp->argc, wp->argv);
                    563: }
                    564:
                    565: /* Callback for pane_current_command. */
1.108     nicm      566: static void
1.79      nicm      567: format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
                    568: {
                    569:        struct window_pane      *wp = ft->wp;
                    570:        char                    *cmd;
                    571:
                    572:        if (wp == NULL)
                    573:                return;
                    574:
                    575:        cmd = get_proc_name(wp->fd, wp->tty);
                    576:        if (cmd == NULL || *cmd == '\0') {
                    577:                free(cmd);
                    578:                cmd = cmd_stringify_argv(wp->argc, wp->argv);
                    579:                if (cmd == NULL || *cmd == '\0') {
                    580:                        free(cmd);
                    581:                        cmd = xstrdup(wp->shell);
                    582:                }
                    583:        }
                    584:        fe->value = parse_window_name(cmd);
                    585:        free(cmd);
                    586: }
                    587:
1.80      nicm      588: /* Callback for history_bytes. */
1.108     nicm      589: static void
1.80      nicm      590: format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe)
                    591: {
                    592:        struct window_pane      *wp = ft->wp;
                    593:        struct grid             *gd;
                    594:        struct grid_line        *gl;
                    595:        unsigned long long       size;
                    596:        u_int                    i;
                    597:
                    598:        if (wp == NULL)
                    599:                return;
                    600:        gd = wp->base.grid;
                    601:
                    602:        size = 0;
                    603:        for (i = 0; i < gd->hsize; i++) {
1.158     nicm      604:                gl = grid_get_line(gd, i);
1.80      nicm      605:                size += gl->cellsize * sizeof *gl->celldata;
1.95      nicm      606:                size += gl->extdsize * sizeof *gl->extddata;
1.80      nicm      607:        }
1.158     nicm      608:        size += gd->hsize * sizeof *gl;
1.80      nicm      609:
                    610:        xasprintf(&fe->value, "%llu", size);
                    611: }
                    612:
                    613: /* Callback for pane_tabs. */
1.108     nicm      614: static void
1.80      nicm      615: format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe)
                    616: {
                    617:        struct window_pane      *wp = ft->wp;
                    618:        struct evbuffer         *buffer;
                    619:        u_int                    i;
                    620:        int                      size;
                    621:
                    622:        if (wp == NULL)
                    623:                return;
                    624:
                    625:        buffer = evbuffer_new();
1.165     nicm      626:        if (buffer == NULL)
                    627:                fatalx("out of memory");
1.80      nicm      628:        for (i = 0; i < wp->base.grid->sx; i++) {
                    629:                if (!bit_test(wp->base.tabs, i))
                    630:                        continue;
                    631:
                    632:                if (EVBUFFER_LENGTH(buffer) > 0)
                    633:                        evbuffer_add(buffer, ",", 1);
                    634:                evbuffer_add_printf(buffer, "%u", i);
                    635:        }
1.148     nicm      636:        if ((size = EVBUFFER_LENGTH(buffer)) != 0)
                    637:                xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
                    638:        evbuffer_free(buffer);
                    639: }
                    640:
1.150     nicm      641: /* Callback for session_group_list. */
1.148     nicm      642: static void
1.150     nicm      643: format_cb_session_group_list(struct format_tree *ft, struct format_entry *fe)
1.148     nicm      644: {
                    645:        struct session          *s = ft->s;
                    646:        struct session_group    *sg;
                    647:        struct session          *loop;
                    648:        struct evbuffer         *buffer;
                    649:        int                      size;
                    650:
                    651:        if (s == NULL)
                    652:                return;
                    653:        sg = session_group_contains(s);
                    654:        if (sg == NULL)
                    655:                return;
                    656:
                    657:        buffer = evbuffer_new();
1.165     nicm      658:        if (buffer == NULL)
                    659:                fatalx("out of memory");
1.148     nicm      660:        TAILQ_FOREACH(loop, &sg->sessions, gentry) {
                    661:                if (EVBUFFER_LENGTH(buffer) > 0)
                    662:                        evbuffer_add(buffer, ",", 1);
                    663:                evbuffer_add_printf(buffer, "%s", loop->name);
                    664:        }
                    665:        if ((size = EVBUFFER_LENGTH(buffer)) != 0)
                    666:                xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
1.80      nicm      667:        evbuffer_free(buffer);
                    668: }
                    669:
1.168     nicm      670: /* Callback for pane_in_mode. */
                    671: static void
                    672: format_cb_pane_in_mode(struct format_tree *ft, struct format_entry *fe)
                    673: {
                    674:        struct window_pane              *wp = ft->wp;
                    675:        u_int                            n = 0;
                    676:        struct window_mode_entry        *wme;
                    677:
                    678:        if (wp == NULL)
                    679:                return;
                    680:
                    681:        TAILQ_FOREACH(wme, &wp->modes, entry)
                    682:            n++;
                    683:        xasprintf(&fe->value, "%u", n);
                    684: }
                    685:
1.112     nicm      686: /* Merge a format tree. */
                    687: static void
                    688: format_merge(struct format_tree *ft, struct format_tree *from)
                    689: {
                    690:        struct format_entry     *fe;
                    691:
                    692:        RB_FOREACH(fe, format_entry_tree, &from->tree) {
                    693:                if (fe->value != NULL)
                    694:                        format_add(ft, fe->key, "%s", fe->value);
                    695:        }
                    696: }
                    697:
1.1       nicm      698: /* Create a new tree. */
                    699: struct format_tree *
1.131     nicm      700: format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
1.68      nicm      701: {
1.1       nicm      702:        struct format_tree      *ft;
1.77      nicm      703:
                    704:        if (!event_initialized(&format_job_event)) {
                    705:                evtimer_set(&format_job_event, format_job_timer, NULL);
                    706:                format_job_timer(-1, 0, NULL);
                    707:        }
1.1       nicm      708:
1.54      nicm      709:        ft = xcalloc(1, sizeof *ft);
                    710:        RB_INIT(&ft->tree);
1.120     nicm      711:
1.131     nicm      712:        if (c != NULL) {
                    713:                ft->client = c;
                    714:                ft->client->references++;
                    715:        }
1.172     nicm      716:        ft->item = item;
1.131     nicm      717:
1.120     nicm      718:        ft->tag = tag;
1.84      nicm      719:        ft->flags = flags;
1.177     nicm      720:        ft->time = time(NULL);
1.1       nicm      721:
1.79      nicm      722:        format_add_cb(ft, "host", format_cb_host);
                    723:        format_add_cb(ft, "host_short", format_cb_host_short);
                    724:        format_add_cb(ft, "pid", format_cb_pid);
1.100     nicm      725:        format_add(ft, "socket_path", "%s", socket_path);
                    726:        format_add_tv(ft, "start_time", &start_time);
1.102     nicm      727:
1.130     nicm      728:        if (item != NULL) {
                    729:                if (item->cmd != NULL)
                    730:                        format_add(ft, "command", "%s", item->cmd->entry->name);
                    731:                if (item->shared != NULL && item->shared->formats != NULL)
                    732:                        format_merge(ft, item->shared->formats);
                    733:        }
1.1       nicm      734:
                    735:        return (ft);
                    736: }
                    737:
                    738: /* Free a tree. */
                    739: void
                    740: format_free(struct format_tree *ft)
                    741: {
1.54      nicm      742:        struct format_entry     *fe, *fe1;
1.1       nicm      743:
1.68      nicm      744:        RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
                    745:                RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9       nicm      746:                free(fe->value);
                    747:                free(fe->key);
                    748:                free(fe);
1.1       nicm      749:        }
                    750:
1.131     nicm      751:        if (ft->client != NULL)
                    752:                server_client_unref(ft->client);
1.25      nicm      753:        free(ft);
1.1       nicm      754: }
                    755:
                    756: /* Add a key-value pair. */
                    757: void
                    758: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
                    759: {
                    760:        struct format_entry     *fe;
1.28      nicm      761:        struct format_entry     *fe_now;
1.1       nicm      762:        va_list                  ap;
                    763:
                    764:        fe = xmalloc(sizeof *fe);
                    765:        fe->key = xstrdup(key);
                    766:
1.79      nicm      767:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
                    768:        if (fe_now != NULL) {
                    769:                free(fe->key);
                    770:                free(fe);
                    771:                free(fe_now->value);
                    772:                fe = fe_now;
                    773:        }
                    774:
                    775:        fe->cb = NULL;
1.87      nicm      776:        fe->t = 0;
1.79      nicm      777:
1.1       nicm      778:        va_start(ap, fmt);
                    779:        xvasprintf(&fe->value, fmt, ap);
                    780:        va_end(ap);
1.79      nicm      781: }
                    782:
1.87      nicm      783: /* Add a key and time. */
1.108     nicm      784: static void
1.87      nicm      785: format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
                    786: {
                    787:        struct format_entry     *fe;
                    788:        struct format_entry     *fe_now;
                    789:
                    790:        fe = xmalloc(sizeof *fe);
                    791:        fe->key = xstrdup(key);
                    792:
                    793:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
                    794:        if (fe_now != NULL) {
                    795:                free(fe->key);
                    796:                free(fe);
                    797:                free(fe_now->value);
                    798:                fe = fe_now;
                    799:        }
                    800:
                    801:        fe->cb = NULL;
                    802:        fe->t = tv->tv_sec;
                    803:
                    804:        fe->value = NULL;
                    805: }
                    806:
1.79      nicm      807: /* Add a key and function. */
1.108     nicm      808: static void
1.79      nicm      809: format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
                    810: {
                    811:        struct format_entry     *fe;
                    812:        struct format_entry     *fe_now;
                    813:
                    814:        fe = xmalloc(sizeof *fe);
                    815:        fe->key = xstrdup(key);
1.1       nicm      816:
1.68      nicm      817:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28      nicm      818:        if (fe_now != NULL) {
                    819:                free(fe->key);
                    820:                free(fe);
1.79      nicm      821:                free(fe_now->value);
                    822:                fe = fe_now;
1.28      nicm      823:        }
1.79      nicm      824:
                    825:        fe->cb = cb;
1.87      nicm      826:        fe->t = 0;
1.79      nicm      827:
                    828:        fe->value = NULL;
1.1       nicm      829: }
                    830:
1.161     nicm      831: /* Quote special characters in string. */
                    832: static char *
                    833: format_quote(const char *s)
                    834: {
                    835:        const char      *cp;
                    836:        char            *out, *at;
                    837:
                    838:        at = out = xmalloc(strlen(s) * 2 + 1);
                    839:        for (cp = s; *cp != '\0'; cp++) {
                    840:                if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
                    841:                        *at++ = '\\';
                    842:                *at++ = *cp;
                    843:        }
                    844:        *at = '\0';
                    845:        return (out);
                    846: }
                    847:
1.1       nicm      848: /* Find a format entry. */
1.108     nicm      849: static char *
1.87      nicm      850: format_find(struct format_tree *ft, const char *key, int modifiers)
1.1       nicm      851: {
                    852:        struct format_entry     *fe, fe_find;
1.76      nicm      853:        struct environ_entry    *envent;
1.87      nicm      854:        static char              s[64];
1.117     nicm      855:        struct options_entry    *o;
1.87      nicm      856:        const char              *found;
1.116     nicm      857:        int                      idx;
1.87      nicm      858:        char                    *copy, *saved;
                    859:
                    860:        if (~modifiers & FORMAT_TIMESTRING) {
1.116     nicm      861:                o = options_parse_get(global_options, key, &idx, 0);
1.87      nicm      862:                if (o == NULL && ft->w != NULL)
1.116     nicm      863:                        o = options_parse_get(ft->w->options, key, &idx, 0);
1.87      nicm      864:                if (o == NULL)
1.116     nicm      865:                        o = options_parse_get(global_w_options, key, &idx, 0);
1.87      nicm      866:                if (o == NULL && ft->s != NULL)
1.116     nicm      867:                        o = options_parse_get(ft->s->options, key, &idx, 0);
1.87      nicm      868:                if (o == NULL)
1.116     nicm      869:                        o = options_parse_get(global_s_options, key, &idx, 0);
1.87      nicm      870:                if (o != NULL) {
1.118     nicm      871:                        found = options_tostring(o, idx, 1);
1.116     nicm      872:                        goto found;
1.54      nicm      873:                }
                    874:        }
1.116     nicm      875:        found = NULL;
1.1       nicm      876:
                    877:        fe_find.key = (char *) key;
1.68      nicm      878:        fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.79      nicm      879:        if (fe != NULL) {
1.87      nicm      880:                if (modifiers & FORMAT_TIMESTRING) {
                    881:                        if (fe->t == 0)
                    882:                                return (NULL);
                    883:                        ctime_r(&fe->t, s);
                    884:                        s[strcspn(s, "\n")] = '\0';
                    885:                        found = s;
                    886:                        goto found;
                    887:                }
                    888:                if (fe->t != 0) {
                    889:                        xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
                    890:                        found = s;
                    891:                        goto found;
                    892:                }
1.149     nicm      893:                if (fe->value == NULL && fe->cb != NULL) {
1.79      nicm      894:                        fe->cb(ft, fe);
1.149     nicm      895:                        if (fe->value == NULL)
                    896:                                fe->value = xstrdup("");
                    897:                }
1.87      nicm      898:                found = fe->value;
                    899:                goto found;
1.79      nicm      900:        }
1.76      nicm      901:
1.87      nicm      902:        if (~modifiers & FORMAT_TIMESTRING) {
                    903:                envent = NULL;
                    904:                if (ft->s != NULL)
1.91      nicm      905:                        envent = environ_find(ft->s->environ, key);
1.87      nicm      906:                if (envent == NULL)
1.91      nicm      907:                        envent = environ_find(global_environ, key);
1.87      nicm      908:                if (envent != NULL) {
                    909:                        found = envent->value;
                    910:                        goto found;
                    911:                }
                    912:        }
1.76      nicm      913:
                    914:        return (NULL);
1.87      nicm      915:
                    916: found:
1.88      nicm      917:        if (found == NULL)
                    918:                return (NULL);
1.87      nicm      919:        copy = xstrdup(found);
                    920:        if (modifiers & FORMAT_BASENAME) {
                    921:                saved = copy;
                    922:                copy = xstrdup(basename(saved));
                    923:                free(saved);
                    924:        }
                    925:        if (modifiers & FORMAT_DIRNAME) {
                    926:                saved = copy;
                    927:                copy = xstrdup(dirname(saved));
                    928:                free(saved);
                    929:        }
1.161     nicm      930:        if (modifiers & FORMAT_QUOTE) {
                    931:                saved = copy;
                    932:                copy = xstrdup(format_quote(saved));
                    933:                free(saved);
                    934:        }
1.87      nicm      935:        return (copy);
1.1       nicm      936: }
                    937:
1.155     nicm      938: /* Skip until end. */
                    939: static const char *
1.169     nicm      940: format_skip(const char *s, const char *end)
1.114     nicm      941: {
                    942:        int     brackets = 0;
                    943:
                    944:        for (; *s != '\0'; s++) {
1.155     nicm      945:                if (*s == '#' && s[1] == '{')
1.114     nicm      946:                        brackets++;
1.155     nicm      947:                if (*s == '#' && strchr(",#{}", s[1]) != NULL) {
                    948:                        s++;
                    949:                        continue;
                    950:                }
1.114     nicm      951:                if (*s == '}')
                    952:                        brackets--;
1.169     nicm      953:                if (strchr(end, *s) != NULL && brackets == 0)
1.114     nicm      954:                        break;
                    955:        }
                    956:        if (*s == '\0')
                    957:                return (NULL);
                    958:        return (s);
                    959: }
                    960:
                    961: /* Return left and right alternatives separated by commas. */
                    962: static int
1.169     nicm      963: format_choose(struct format_tree *ft, const char *s, char **left, char **right,
                    964:     int expand)
1.114     nicm      965: {
1.169     nicm      966:        const char      *cp;
                    967:        char            *left0, *right0;
1.114     nicm      968:
1.169     nicm      969:        cp = format_skip(s, ",");
1.114     nicm      970:        if (cp == NULL)
                    971:                return (-1);
1.169     nicm      972:        left0 = xstrndup(s, cp - s);
                    973:        right0 = xstrdup(cp + 1);
1.114     nicm      974:
1.169     nicm      975:        if (expand) {
                    976:                *left = format_expand(ft, left0);
                    977:                *right = format_expand(ft, right0);
                    978:        } else {
                    979:                *left = left0;
                    980:                *right = right0;
                    981:        }
1.114     nicm      982:        return (0);
                    983: }
                    984:
                    985: /* Is this true? */
1.141     nicm      986: int
1.114     nicm      987: format_true(const char *s)
                    988: {
                    989:        if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
                    990:                return (1);
                    991:        return (0);
                    992: }
                    993:
1.169     nicm      994: /* Check if modifier end. */
                    995: static int
                    996: format_is_end(char c)
                    997: {
                    998:        return (c == ';' || c == ':');
                    999: }
                   1000:
                   1001: /* Add to modifier list. */
                   1002: static void
                   1003: format_add_modifier(struct format_modifier **list, u_int *count,
                   1004:     const char *c, size_t n, char **argv, int argc)
                   1005: {
                   1006:        struct format_modifier *fm;
                   1007:
                   1008:        *list = xreallocarray(*list, (*count) + 1, sizeof **list);
                   1009:        fm = &(*list)[(*count)++];
                   1010:
                   1011:        memcpy(fm->modifier, c, n);
                   1012:        fm->modifier[n] = '\0';
                   1013:        fm->size = n;
                   1014:
                   1015:        fm->argv = argv;
                   1016:        fm->argc = argc;
                   1017: }
                   1018:
                   1019: /* Free modifier list. */
                   1020: static void
                   1021: format_free_modifiers(struct format_modifier *list, u_int count)
                   1022: {
                   1023:        u_int   i;
                   1024:
                   1025:        for (i = 0; i < count; i++)
                   1026:                cmd_free_argv(list[i].argc, list[i].argv);
                   1027:        free(list);
                   1028: }
                   1029:
                   1030: /* Build modifier list. */
                   1031: static struct format_modifier *
                   1032: format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
                   1033: {
                   1034:        const char              *cp = *s, *end;
                   1035:        struct format_modifier  *list = NULL;
                   1036:        char                     c, last[] = "X;:", **argv, *value;
                   1037:        int                      argc;
                   1038:
                   1039:        /*
                   1040:         * Modifiers are a ; separated list of the forms:
1.175     nicm     1041:         *      l,m,C,b,d,t,q,E,T,S,W,P
1.169     nicm     1042:         *      =a
                   1043:         *      =/a
                   1044:         *      =/a/
                   1045:         *      s/a/b/
                   1046:         *      s/a/b
                   1047:         *      ||,&&,!=,==
                   1048:         */
                   1049:
                   1050:        *count = 0;
                   1051:
                   1052:        while (*cp != '\0' && *cp != ':') {
                   1053:                /* Skip and separator character. */
                   1054:                if (*cp == ';')
                   1055:                        cp++;
                   1056:
                   1057:                /* Check single character modifiers with no arguments. */
1.175     nicm     1058:                if (strchr("lmCbdtqETSWP", cp[0]) != NULL &&
1.172     nicm     1059:                    format_is_end(cp[1])) {
1.169     nicm     1060:                        format_add_modifier(&list, count, cp, 1, NULL, 0);
                   1061:                        cp++;
                   1062:                        continue;
                   1063:                }
                   1064:
                   1065:                /* Then try double character with no arguments. */
                   1066:                if ((memcmp("||", cp, 2) == 0 ||
                   1067:                    memcmp("&&", cp, 2) == 0 ||
                   1068:                    memcmp("!=", cp, 2) == 0 ||
                   1069:                    memcmp("==", cp, 2) == 0) &&
                   1070:                    format_is_end(cp[2])) {
                   1071:                        format_add_modifier(&list, count, cp, 2, NULL, 0);
                   1072:                        cp += 2;
                   1073:                        continue;
                   1074:                }
                   1075:
                   1076:                /* Now try single character with arguments. */
                   1077:                if (strchr("s=", cp[0]) == NULL)
                   1078:                        break;
                   1079:                c = cp[0];
                   1080:
                   1081:                /* No arguments provided. */
                   1082:                if (format_is_end(cp[1])) {
                   1083:                        format_add_modifier(&list, count, cp, 1, NULL, 0);
                   1084:                        cp++;
                   1085:                        continue;
                   1086:                }
                   1087:                argv = NULL;
                   1088:                argc = 0;
                   1089:
                   1090:                /* Single argument with no wrapper character. */
                   1091:                if (!ispunct(cp[1]) || cp[1] == '-') {
                   1092:                        end = format_skip(cp + 1, ":;");
                   1093:                        if (end == NULL)
                   1094:                                break;
                   1095:
                   1096:                        argv = xcalloc(1, sizeof *argv);
                   1097:                        value = xstrndup(cp + 1, end - (cp + 1));
                   1098:                        argv[0] = format_expand(ft, value);
                   1099:                        free(value);
                   1100:                        argc = 1;
                   1101:
                   1102:                        format_add_modifier(&list, count, &c, 1, argv, argc);
                   1103:                        cp = end;
                   1104:                        continue;
                   1105:                }
                   1106:
                   1107:                /* Multiple arguments with a wrapper character. */
                   1108:                last[0] = cp[1];
                   1109:                cp++;
                   1110:                do {
                   1111:                        if (cp[0] == last[0] && format_is_end(cp[1])) {
                   1112:                                cp++;
                   1113:                                break;
                   1114:                        }
                   1115:                        end = format_skip(cp + 1, last);
                   1116:                        if (end == NULL)
                   1117:                                break;
                   1118:                        cp++;
                   1119:
                   1120:                        argv = xreallocarray (argv, argc + 1, sizeof *argv);
                   1121:                        value = xstrndup(cp, end - cp);
                   1122:                        argv[argc++] = format_expand(ft, value);
                   1123:                        free(value);
                   1124:
                   1125:                        cp = end;
                   1126:                } while (!format_is_end(cp[0]));
                   1127:                format_add_modifier(&list, count, &c, 1, argv, argc);
                   1128:        }
                   1129:        if (*cp != ':') {
                   1130:                format_free_modifiers(list, *count);
                   1131:                *count = 0;
                   1132:                return (NULL);
                   1133:        }
                   1134:        *s = cp + 1;
                   1135:        return list;
                   1136: }
                   1137:
                   1138: /* Perform substitution in string. */
                   1139: static char *
                   1140: format_substitute(const char *source, const char *from, const char *to)
                   1141: {
                   1142:        char            *copy, *new;
                   1143:        const char      *cp;
                   1144:        size_t           fromlen, tolen, newlen, used;
                   1145:
                   1146:        fromlen = strlen(from);
                   1147:        tolen = strlen(to);
                   1148:
                   1149:        newlen = strlen(source) + 1;
                   1150:        copy = new = xmalloc(newlen);
                   1151:
                   1152:        for (cp = source; *cp != '\0'; /* nothing */) {
                   1153:                if (strncmp(cp, from, fromlen) != 0) {
                   1154:                        *new++ = *cp++;
                   1155:                        continue;
                   1156:                }
                   1157:                used = new - copy;
                   1158:
                   1159:                newlen += tolen;
                   1160:                copy = xrealloc(copy, newlen);
                   1161:
                   1162:                new = copy + used;
                   1163:                memcpy(new, to, tolen);
                   1164:
                   1165:                new += tolen;
                   1166:                cp += fromlen;
                   1167:        }
                   1168:
                   1169:        *new = '\0';
                   1170:        return (copy);
                   1171: }
                   1172:
1.172     nicm     1173: /* Loop over sessions. */
                   1174: static char *
                   1175: format_loop_sessions(struct format_tree *ft, const char *fmt)
                   1176: {
1.180   ! nicm     1177:        struct client           *c = ft->client;
1.172     nicm     1178:        struct cmdq_item        *item = ft->item;
1.180   ! nicm     1179:        struct format_tree      *nft;
1.172     nicm     1180:        char                    *expanded, *value;
                   1181:        size_t                   valuelen;
                   1182:        struct session          *s;
                   1183:
                   1184:        value = xcalloc(1, 1);
                   1185:        valuelen = 1;
                   1186:
                   1187:        RB_FOREACH(s, sessions, &sessions) {
1.179     nicm     1188:                format_log(ft, "session loop: $%u", s->id);
1.180   ! nicm     1189:                nft = format_create(c, item, FORMAT_NONE, ft->flags);
        !          1190:                format_defaults(nft, ft->c, s, NULL, NULL);
        !          1191:                expanded = format_expand(nft, fmt);
        !          1192:                format_free(nft);
1.172     nicm     1193:
                   1194:                valuelen += strlen(expanded);
                   1195:                value = xrealloc(value, valuelen);
                   1196:
                   1197:                strlcat(value, expanded, valuelen);
                   1198:                free(expanded);
                   1199:        }
                   1200:
                   1201:        return (value);
                   1202: }
                   1203:
                   1204: /* Loop over windows. */
                   1205: static char *
                   1206: format_loop_windows(struct format_tree *ft, const char *fmt)
                   1207: {
1.180   ! nicm     1208:        struct client           *c = ft->client;
1.172     nicm     1209:        struct cmdq_item        *item = ft->item;
1.180   ! nicm     1210:        struct format_tree      *nft;
1.172     nicm     1211:        char                    *all, *active, *use, *expanded, *value;
                   1212:        size_t                   valuelen;
                   1213:        struct winlink          *wl;
1.180   ! nicm     1214:        struct window           *w;
1.172     nicm     1215:
1.179     nicm     1216:        if (ft->s == NULL) {
                   1217:                format_log(ft, "window loop but no session");
1.172     nicm     1218:                return (NULL);
1.179     nicm     1219:        }
1.172     nicm     1220:
                   1221:        if (format_choose(ft, fmt, &all, &active, 0) != 0) {
                   1222:                all = xstrdup(fmt);
                   1223:                active = NULL;
                   1224:        }
                   1225:
                   1226:        value = xcalloc(1, 1);
                   1227:        valuelen = 1;
                   1228:
                   1229:        RB_FOREACH(wl, winlinks, &ft->s->windows) {
1.180   ! nicm     1230:                w = wl->window;
        !          1231:                format_log(ft, "window loop: %u @%u", wl->idx, w->id);
1.172     nicm     1232:                if (active != NULL && wl == ft->s->curw)
                   1233:                        use = active;
                   1234:                else
                   1235:                        use = all;
1.180   ! nicm     1236:                nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
        !          1237:                format_defaults(nft, ft->c, ft->s, wl, NULL);
        !          1238:                expanded = format_expand(nft, use);
        !          1239:                format_free(nft);
1.172     nicm     1240:
                   1241:                valuelen += strlen(expanded);
                   1242:                value = xrealloc(value, valuelen);
                   1243:
                   1244:                strlcat(value, expanded, valuelen);
                   1245:                free(expanded);
                   1246:        }
                   1247:
                   1248:        free(active);
                   1249:        free(all);
                   1250:
                   1251:        return (value);
                   1252: }
                   1253:
                   1254: /* Loop over panes. */
                   1255: static char *
                   1256: format_loop_panes(struct format_tree *ft, const char *fmt)
                   1257: {
1.180   ! nicm     1258:        struct client           *c = ft->client;
1.172     nicm     1259:        struct cmdq_item        *item = ft->item;
1.180   ! nicm     1260:        struct format_tree      *nft;
1.172     nicm     1261:        char                    *all, *active, *use, *expanded, *value;
                   1262:        size_t                   valuelen;
                   1263:        struct window_pane      *wp;
                   1264:
1.179     nicm     1265:        if (ft->w == NULL) {
                   1266:                format_log(ft, "pane loop but no window");
1.172     nicm     1267:                return (NULL);
1.179     nicm     1268:        }
1.172     nicm     1269:
                   1270:        if (format_choose(ft, fmt, &all, &active, 0) != 0) {
                   1271:                all = xstrdup(fmt);
                   1272:                active = NULL;
                   1273:        }
                   1274:
                   1275:        value = xcalloc(1, 1);
                   1276:        valuelen = 1;
                   1277:
                   1278:        TAILQ_FOREACH(wp, &ft->w->panes, entry) {
1.179     nicm     1279:                format_log(ft, "pane loop: %%%u", wp->id);
1.172     nicm     1280:                if (active != NULL && wp == ft->w->active)
                   1281:                        use = active;
                   1282:                else
                   1283:                        use = all;
1.180   ! nicm     1284:                nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
        !          1285:                format_defaults(nft, ft->c, ft->s, ft->wl, wp);
        !          1286:                expanded = format_expand(nft, use);
        !          1287:                format_free(nft);
1.172     nicm     1288:
                   1289:                valuelen += strlen(expanded);
                   1290:                value = xrealloc(value, valuelen);
                   1291:
                   1292:                strlcat(value, expanded, valuelen);
                   1293:                free(expanded);
                   1294:        }
                   1295:
                   1296:        free(active);
                   1297:        free(all);
                   1298:
                   1299:        return (value);
                   1300: }
                   1301:
1.140     nicm     1302: /* Replace a key. */
1.108     nicm     1303: static int
1.30      nicm     1304: format_replace(struct format_tree *ft, const char *key, size_t keylen,
                   1305:     char **buf, size_t *len, size_t *off)
1.1       nicm     1306: {
1.140     nicm     1307:        struct window_pane      *wp = ft->wp;
1.169     nicm     1308:        const char              *errptr, *copy, *cp;
                   1309:        char                    *copy0, *condition, *found, *new;
                   1310:        char                    *value, *left, *right;
                   1311:        size_t                   valuelen;
                   1312:        int                      modifiers = 0, limit = 0;
                   1313:        struct format_modifier  *list, *fm, *cmp = NULL, *search = NULL;
                   1314:        struct format_modifier  *sub = NULL;
                   1315:        u_int                    i, count;
1.179     nicm     1316:        int                      j;
1.1       nicm     1317:
                   1318:        /* Make a copy of the key. */
1.169     nicm     1319:        copy = copy0 = xstrndup(key, keylen);
                   1320:
                   1321:        /* Process modifier list. */
                   1322:        list = format_build_modifiers(ft, &copy, &count);
                   1323:        for (i = 0; i < count; i++) {
                   1324:                fm = &list[i];
1.179     nicm     1325:                if (format_logging(ft)) {
                   1326:                        format_log(ft, "modifier %u is %s", i, fm->modifier);
                   1327:                        for (j = 0; j < fm->argc; j++) {
                   1328:                                format_log(ft, "modifier %u argument %d: %s", i,
                   1329:                                    j, fm->argv[j]);
                   1330:                        }
                   1331:                }
1.169     nicm     1332:                if (fm->size == 1) {
                   1333:                        switch (fm->modifier[0]) {
                   1334:                        case 'm':
                   1335:                                cmp = fm;
                   1336:                                break;
                   1337:                        case 'C':
                   1338:                                search = fm;
                   1339:                                break;
                   1340:                        case 's':
                   1341:                                if (fm->argc != 2)
                   1342:                                        break;
                   1343:                                sub = fm;
                   1344:                                break;
                   1345:                        case '=':
                   1346:                                if (fm->argc != 1)
                   1347:                                        break;
                   1348:                                limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
                   1349:                                    &errptr);
                   1350:                                if (errptr != NULL)
                   1351:                                        limit = 0;
                   1352:                                break;
                   1353:                        case 'l':
                   1354:                                modifiers |= FORMAT_LITERAL;
                   1355:                                break;
                   1356:                        case 'b':
                   1357:                                modifiers |= FORMAT_BASENAME;
                   1358:                                break;
                   1359:                        case 'd':
                   1360:                                modifiers |= FORMAT_DIRNAME;
                   1361:                                break;
                   1362:                        case 't':
                   1363:                                modifiers |= FORMAT_TIMESTRING;
                   1364:                                break;
                   1365:                        case 'q':
                   1366:                                modifiers |= FORMAT_QUOTE;
                   1367:                                break;
1.170     nicm     1368:                        case 'E':
                   1369:                                modifiers |= FORMAT_EXPAND;
                   1370:                                break;
1.175     nicm     1371:                        case 'T':
                   1372:                                modifiers |= FORMAT_EXPANDTIME;
                   1373:                                break;
1.172     nicm     1374:                        case 'S':
                   1375:                                modifiers |= FORMAT_SESSIONS;
                   1376:                                break;
                   1377:                        case 'W':
                   1378:                                modifiers |= FORMAT_WINDOWS;
                   1379:                                break;
                   1380:                        case 'P':
                   1381:                                modifiers |= FORMAT_PANES;
                   1382:                                break;
1.169     nicm     1383:                        }
                   1384:                } else if (fm->size == 2) {
                   1385:                        if (strcmp(fm->modifier, "||") == 0 ||
                   1386:                            strcmp(fm->modifier, "&&") == 0 ||
                   1387:                            strcmp(fm->modifier, "==") == 0 ||
                   1388:                            strcmp(fm->modifier, "!=") == 0)
                   1389:                                cmp = fm;
1.98      nicm     1390:                }
1.30      nicm     1391:        }
                   1392:
1.155     nicm     1393:        /* Is this a literal string? */
1.169     nicm     1394:        if (modifiers & FORMAT_LITERAL) {
1.155     nicm     1395:                value = xstrdup(copy);
                   1396:                goto done;
                   1397:        }
                   1398:
1.172     nicm     1399:        /* Is this a loop, comparison or condition? */
                   1400:        if (modifiers & FORMAT_SESSIONS) {
                   1401:                value = format_loop_sessions(ft, copy);
                   1402:                if (value == NULL)
                   1403:                        goto fail;
                   1404:        } else if (modifiers & FORMAT_WINDOWS) {
                   1405:                value = format_loop_windows(ft, copy);
                   1406:                if (value == NULL)
                   1407:                        goto fail;
                   1408:        } else if (modifiers & FORMAT_PANES) {
                   1409:                value = format_loop_panes(ft, copy);
                   1410:                if (value == NULL)
                   1411:                        goto fail;
                   1412:        } else if (search != NULL) {
1.140     nicm     1413:                /* Search in pane. */
1.179     nicm     1414:                if (wp == NULL) {
                   1415:                        format_log(ft, "search '%s' but no pane", copy);
1.140     nicm     1416:                        value = xstrdup("0");
1.179     nicm     1417:                } else {
                   1418:                        format_log(ft, "search '%s' pane %%%u", copy,  wp->id);
1.140     nicm     1419:                        xasprintf(&value, "%u", window_pane_search(wp, copy));
1.179     nicm     1420:                }
1.169     nicm     1421:        } else if (cmp != NULL) {
                   1422:                /* Comparison of left and right. */
1.179     nicm     1423:                if (format_choose(ft, copy, &left, &right, 1) != 0) {
                   1424:                        format_log(ft, "compare %s syntax error: %s",
                   1425:                            cmp->modifier, copy);
1.114     nicm     1426:                        goto fail;
1.179     nicm     1427:                }
                   1428:                format_log(ft, "compare %s left is: %s", cmp->modifier, left);
                   1429:                format_log(ft, "compare %s right is: %s", cmp->modifier, right);
1.169     nicm     1430:
                   1431:                if (strcmp(cmp->modifier, "||") == 0) {
                   1432:                        if (format_true(left) || format_true(right))
                   1433:                                value = xstrdup("1");
                   1434:                        else
                   1435:                                value = xstrdup("0");
                   1436:                } else if (strcmp(cmp->modifier, "&&") == 0) {
                   1437:                        if (format_true(left) && format_true(right))
                   1438:                                value = xstrdup("1");
                   1439:                        else
                   1440:                                value = xstrdup("0");
                   1441:                } else if (strcmp(cmp->modifier, "==") == 0) {
                   1442:                        if (strcmp(left, right) == 0)
                   1443:                                value = xstrdup("1");
                   1444:                        else
                   1445:                                value = xstrdup("0");
                   1446:                } else if (strcmp(cmp->modifier, "!=") == 0) {
                   1447:                        if (strcmp(left, right) != 0)
                   1448:                                value = xstrdup("1");
                   1449:                        else
                   1450:                                value = xstrdup("0");
                   1451:                }
                   1452:                else if (strcmp(cmp->modifier, "m") == 0) {
                   1453:                        if (fnmatch(left, right, 0) == 0)
                   1454:                                value = xstrdup("1");
                   1455:                        else
                   1456:                                value = xstrdup("0");
                   1457:                }
                   1458:
1.114     nicm     1459:                free(right);
                   1460:                free(left);
                   1461:        } else if (*copy == '?') {
                   1462:                /* Conditional: check first and choose second or third. */
1.169     nicm     1463:                cp = format_skip(copy + 1, ",");
1.179     nicm     1464:                if (cp == NULL) {
                   1465:                        format_log(ft, "condition syntax error: %s", copy + 1);
1.1       nicm     1466:                        goto fail;
1.179     nicm     1467:                }
1.169     nicm     1468:                condition = xstrndup(copy + 1, cp - (copy + 1));
1.179     nicm     1469:                format_log(ft, "condition is: %s", condition);
1.1       nicm     1470:
1.169     nicm     1471:                found = format_find(ft, condition, modifiers);
1.156     nicm     1472:                if (found == NULL) {
                   1473:                        /*
1.169     nicm     1474:                         * If the condition not found, try to expand it. If
1.156     nicm     1475:                         * the expansion doesn't have any effect, then assume
                   1476:                         * false.
                   1477:                         */
1.169     nicm     1478:                        found = format_expand(ft, condition);
                   1479:                        if (strcmp(found, condition) == 0) {
1.156     nicm     1480:                                free(found);
                   1481:                                found = xstrdup("");
1.179     nicm     1482:                                format_log(ft, "condition '%s' found: %s",
                   1483:                                    condition, found);
                   1484:                        } else {
                   1485:                                format_log(ft,
                   1486:                                    "condition '%s' not found; assuming false",
                   1487:                                    condition);
1.156     nicm     1488:                        }
1.179     nicm     1489:                } else
                   1490:                        format_log(ft, "condition '%s' found", condition);
1.169     nicm     1491:
                   1492:                if (format_choose(ft, cp + 1, &left, &right, 0) != 0) {
1.179     nicm     1493:                        format_log(ft, "condition '%s' syntax error: %s",
                   1494:                            condition, cp + 1);
1.162     nicm     1495:                        free(found);
1.89      nicm     1496:                        goto fail;
1.162     nicm     1497:                }
1.179     nicm     1498:                if (format_true(found)) {
                   1499:                        format_log(ft, "condition '%s' is true", condition);
1.114     nicm     1500:                        value = format_expand(ft, left);
1.179     nicm     1501:                } else {
                   1502:                        format_log(ft, "condition '%s' is false", condition);
1.114     nicm     1503:                        value = format_expand(ft, right);
1.179     nicm     1504:                }
1.169     nicm     1505:                free(right);
                   1506:                free(left);
                   1507:
1.179     nicm     1508:                free(condition);
1.98      nicm     1509:                free(found);
1.1       nicm     1510:        } else {
1.114     nicm     1511:                /* Neither: look up directly. */
1.98      nicm     1512:                value = format_find(ft, copy, modifiers);
1.179     nicm     1513:                if (value == NULL) {
                   1514:                        format_log(ft, "format '%s' not found", copy);
1.98      nicm     1515:                        value = xstrdup("");
1.179     nicm     1516:                } else
                   1517:                        format_log(ft, "format '%s' found: %s", copy, value);
1.170     nicm     1518:        }
                   1519:
1.171     nicm     1520: done:
1.170     nicm     1521:        /* Expand again if required. */
                   1522:        if (modifiers & FORMAT_EXPAND) {
                   1523:                new = format_expand(ft, value);
1.175     nicm     1524:                free(value);
                   1525:                value = new;
                   1526:        }
                   1527:        else if (modifiers & FORMAT_EXPANDTIME) {
1.177     nicm     1528:                new = format_expand_time(ft, value);
1.170     nicm     1529:                free(value);
                   1530:                value = new;
1.98      nicm     1531:        }
                   1532:
                   1533:        /* Perform substitution if any. */
1.169     nicm     1534:        if (sub != NULL) {
                   1535:                new = format_substitute(value, sub->argv[0], sub->argv[1]);
1.179     nicm     1536:                format_log(ft, "substituted '%s' to '%s: %s", sub->argv[0],
                   1537:                    sub->argv[1], new);
1.98      nicm     1538:                free(value);
1.169     nicm     1539:                value = new;
1.1       nicm     1540:        }
                   1541:
1.30      nicm     1542:        /* Truncate the value if needed. */
1.105     nicm     1543:        if (limit > 0) {
1.98      nicm     1544:                new = utf8_trimcstr(value, limit);
1.179     nicm     1545:                format_log(ft, "applied length limit %d: %s", limit, new);
1.105     nicm     1546:                free(value);
                   1547:                value = new;
                   1548:        } else if (limit < 0) {
                   1549:                new = utf8_rtrimcstr(value, -limit);
1.179     nicm     1550:                format_log(ft, "applied length limit %d: %s", limit, new);
1.98      nicm     1551:                free(value);
                   1552:                value = new;
1.44      nicm     1553:        }
1.30      nicm     1554:
1.1       nicm     1555:        /* Expand the buffer and copy in the value. */
1.98      nicm     1556:        valuelen = strlen(value);
1.1       nicm     1557:        while (*len - *off < valuelen + 1) {
1.50      nicm     1558:                *buf = xreallocarray(*buf, 2, *len);
1.1       nicm     1559:                *len *= 2;
                   1560:        }
                   1561:        memcpy(*buf + *off, value, valuelen);
                   1562:        *off += valuelen;
                   1563:
1.179     nicm     1564:        format_log(ft, "replaced '%s' with '%s'", copy0, value);
1.98      nicm     1565:        free(value);
1.179     nicm     1566:
1.169     nicm     1567:        format_free_modifiers(list, count);
1.30      nicm     1568:        free(copy0);
1.1       nicm     1569:        return (0);
                   1570:
                   1571: fail:
1.179     nicm     1572:        format_log(ft, "failed %s", copy0);
1.169     nicm     1573:        format_free_modifiers(list, count);
1.30      nicm     1574:        free(copy0);
1.1       nicm     1575:        return (-1);
                   1576: }
                   1577:
                   1578: /* Expand keys in a template. */
1.179     nicm     1579: static char *
                   1580: format_expand1(struct format_tree *ft, const char *fmt, int time)
1.1       nicm     1581: {
1.152     nicm     1582:        char            *buf, *out, *name;
1.179     nicm     1583:        const char      *ptr, *s;
1.86      nicm     1584:        size_t           off, len, n, outlen;
1.31      nicm     1585:        int              ch, brackets;
1.179     nicm     1586:        struct tm       *tm;
                   1587:        char             expanded[8192];
1.58      nicm     1588:
1.179     nicm     1589:        if (fmt == NULL || *fmt == '\0')
1.58      nicm     1590:                return (xstrdup(""));
1.1       nicm     1591:
1.178     nicm     1592:        if (ft->loop == FORMAT_LOOP_LIMIT)
                   1593:                return (xstrdup(""));
                   1594:        ft->loop++;
                   1595:
1.179     nicm     1596:        format_log(ft, "expanding format: %s", fmt);
                   1597:
                   1598:        if (time) {
                   1599:                tm = localtime(&ft->time);
                   1600:                if (strftime(expanded, sizeof expanded, fmt, tm) == 0) {
                   1601:                        format_log(ft, "format is too long");
                   1602:                        return (xstrdup(""));
                   1603:                }
                   1604:                if (format_logging(ft) && strcmp(expanded, fmt) != 0)
                   1605:                        format_log(ft, "after time expanded: %s", expanded);
                   1606:                fmt = expanded;
                   1607:        }
                   1608:
1.1       nicm     1609:        len = 64;
                   1610:        buf = xmalloc(len);
                   1611:        off = 0;
                   1612:
                   1613:        while (*fmt != '\0') {
                   1614:                if (*fmt != '#') {
                   1615:                        while (len - off < 2) {
1.50      nicm     1616:                                buf = xreallocarray(buf, 2, len);
1.1       nicm     1617:                                len *= 2;
                   1618:                        }
                   1619:                        buf[off++] = *fmt++;
                   1620:                        continue;
                   1621:                }
                   1622:                fmt++;
                   1623:
1.179     nicm     1624:                ch = (u_char)*fmt++;
1.1       nicm     1625:                switch (ch) {
1.68      nicm     1626:                case '(':
                   1627:                        brackets = 1;
                   1628:                        for (ptr = fmt; *ptr != '\0'; ptr++) {
                   1629:                                if (*ptr == '(')
                   1630:                                        brackets++;
                   1631:                                if (*ptr == ')' && --brackets == 0)
                   1632:                                        break;
                   1633:                        }
                   1634:                        if (*ptr != ')' || brackets != 0)
                   1635:                                break;
                   1636:                        n = ptr - fmt;
                   1637:
1.179     nicm     1638:                        name = xstrndup(fmt, n);
                   1639:                        format_log(ft, "found #(): %s", name);
                   1640:
                   1641:                        if (ft->flags & FORMAT_NOJOBS) {
1.114     nicm     1642:                                out = xstrdup("");
1.179     nicm     1643:                                format_log(ft, "#() is disabled");
                   1644:                        } else {
1.152     nicm     1645:                                out = format_job_get(ft, name);
1.179     nicm     1646:                                format_log(ft, "#() result: %s", out);
1.152     nicm     1647:                        }
1.179     nicm     1648:                        free(name);
                   1649:
1.86      nicm     1650:                        outlen = strlen(out);
                   1651:                        while (len - off < outlen + 1) {
1.68      nicm     1652:                                buf = xreallocarray(buf, 2, len);
                   1653:                                len *= 2;
                   1654:                        }
1.86      nicm     1655:                        memcpy(buf + off, out, outlen);
                   1656:                        off += outlen;
                   1657:
                   1658:                        free(out);
1.68      nicm     1659:
                   1660:                        fmt += n + 1;
                   1661:                        continue;
1.1       nicm     1662:                case '{':
1.169     nicm     1663:                        ptr = format_skip((char *)fmt - 2, "}");
1.155     nicm     1664:                        if (ptr == NULL)
1.1       nicm     1665:                                break;
                   1666:                        n = ptr - fmt;
                   1667:
1.179     nicm     1668:                        format_log(ft, "found #{}: %.*s", (int)n, fmt);
1.1       nicm     1669:                        if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
                   1670:                                break;
                   1671:                        fmt += n + 1;
1.40      nicm     1672:                        continue;
1.155     nicm     1673:                case '}':
1.40      nicm     1674:                case '#':
1.155     nicm     1675:                case ',':
1.179     nicm     1676:                        format_log(ft, "found #%c", ch);
1.40      nicm     1677:                        while (len - off < 2) {
1.50      nicm     1678:                                buf = xreallocarray(buf, 2, len);
1.40      nicm     1679:                                len *= 2;
                   1680:                        }
1.155     nicm     1681:                        buf[off++] = ch;
1.1       nicm     1682:                        continue;
                   1683:                default:
1.25      nicm     1684:                        s = NULL;
                   1685:                        if (ch >= 'A' && ch <= 'Z')
                   1686:                                s = format_upper[ch - 'A'];
                   1687:                        else if (ch >= 'a' && ch <= 'z')
                   1688:                                s = format_lower[ch - 'a'];
                   1689:                        if (s == NULL) {
                   1690:                                while (len - off < 3) {
1.50      nicm     1691:                                        buf = xreallocarray(buf, 2, len);
1.25      nicm     1692:                                        len *= 2;
1.1       nicm     1693:                                }
1.25      nicm     1694:                                buf[off++] = '#';
                   1695:                                buf[off++] = ch;
                   1696:                                continue;
1.1       nicm     1697:                        }
1.25      nicm     1698:                        n = strlen(s);
1.179     nicm     1699:                        format_log(ft, "found #%c: %s", ch, s);
1.25      nicm     1700:                        if (format_replace(ft, s, n, &buf, &len, &off) != 0)
                   1701:                                break;
1.1       nicm     1702:                        continue;
                   1703:                }
                   1704:
                   1705:                break;
                   1706:        }
                   1707:        buf[off] = '\0';
                   1708:
1.179     nicm     1709:        format_log(ft, "result is: %s", buf);
                   1710:        ft->loop--;
1.178     nicm     1711:
1.1       nicm     1712:        return (buf);
1.179     nicm     1713: }
                   1714:
                   1715: /* Expand keys in a template, passing through strftime first. */
                   1716: char *
                   1717: format_expand_time(struct format_tree *ft, const char *fmt)
                   1718: {
                   1719:        return (format_expand1(ft, fmt, 1));
                   1720: }
                   1721:
                   1722: /* Expand keys in a template. */
                   1723: char *
                   1724: format_expand(struct format_tree *ft, const char *fmt)
                   1725: {
                   1726:        return (format_expand1(ft, fmt, 0));
1.123     nicm     1727: }
                   1728:
                   1729: /* Expand a single string. */
                   1730: char *
                   1731: format_single(struct cmdq_item *item, const char *fmt, struct client *c,
                   1732:     struct session *s, struct winlink *wl, struct window_pane *wp)
                   1733: {
                   1734:        struct format_tree      *ft;
                   1735:        char                    *expanded;
                   1736:
1.131     nicm     1737:        if (item != NULL)
                   1738:                ft = format_create(item->client, item, FORMAT_NONE, 0);
                   1739:        else
                   1740:                ft = format_create(NULL, item, FORMAT_NONE, 0);
1.123     nicm     1741:        format_defaults(ft, c, s, wl, wp);
                   1742:
                   1743:        expanded = format_expand(ft, fmt);
                   1744:        format_free(ft);
                   1745:        return (expanded);
1.1       nicm     1746: }
                   1747:
1.57      nicm     1748: /* Set defaults for any of arguments that are not NULL. */
                   1749: void
                   1750: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
                   1751:     struct winlink *wl, struct window_pane *wp)
                   1752: {
1.154     nicm     1753:        if (c != NULL && s != NULL && c->session != s)
                   1754:                log_debug("%s: session does not match", __func__);
                   1755:
1.146     nicm     1756:        format_add(ft, "session_format", "%d", s != NULL);
                   1757:        format_add(ft, "window_format", "%d", wl != NULL);
                   1758:        format_add(ft, "pane_format", "%d", wp != NULL);
                   1759:
1.57      nicm     1760:        if (s == NULL && c != NULL)
                   1761:                s = c->session;
                   1762:        if (wl == NULL && s != NULL)
                   1763:                wl = s->curw;
                   1764:        if (wp == NULL && wl != NULL)
                   1765:                wp = wl->window->active;
                   1766:
                   1767:        if (c != NULL)
                   1768:                format_defaults_client(ft, c);
                   1769:        if (s != NULL)
                   1770:                format_defaults_session(ft, s);
1.129     nicm     1771:        if (wl != NULL)
                   1772:                format_defaults_winlink(ft, wl);
1.57      nicm     1773:        if (wp != NULL)
                   1774:                format_defaults_pane(ft, wp);
                   1775: }
                   1776:
1.1       nicm     1777: /* Set default format keys for a session. */
1.108     nicm     1778: static void
1.57      nicm     1779: format_defaults_session(struct format_tree *ft, struct session *s)
1.1       nicm     1780: {
                   1781:        struct session_group    *sg;
                   1782:
1.54      nicm     1783:        ft->s = s;
                   1784:
1.1       nicm     1785:        format_add(ft, "session_name", "%s", s->name);
                   1786:        format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
1.23      nicm     1787:        format_add(ft, "session_id", "$%u", s->id);
1.1       nicm     1788:
1.122     nicm     1789:        sg = session_group_contains(s);
1.1       nicm     1790:        format_add(ft, "session_grouped", "%d", sg != NULL);
1.148     nicm     1791:        if (sg != NULL) {
1.122     nicm     1792:                format_add(ft, "session_group", "%s", sg->name);
1.148     nicm     1793:                format_add(ft, "session_group_size", "%u",
                   1794:                    session_group_count (sg));
1.150     nicm     1795:                format_add_cb(ft, "session_group_list",
                   1796:                    format_cb_session_group_list);
1.148     nicm     1797:        }
1.1       nicm     1798:
1.87      nicm     1799:        format_add_tv(ft, "session_created", &s->creation_time);
                   1800:        format_add_tv(ft, "session_last_attached", &s->last_attached_time);
                   1801:        format_add_tv(ft, "session_activity", &s->activity_time);
1.1       nicm     1802:
1.41      nicm     1803:        format_add(ft, "session_attached", "%u", s->attached);
1.59      nicm     1804:        format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66      nicm     1805:
1.80      nicm     1806:        format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.133     nicm     1807:        format_add_cb(ft, "session_stack", format_cb_session_stack);
1.3       nicm     1808: }
                   1809:
                   1810: /* Set default format keys for a client. */
1.108     nicm     1811: static void
1.57      nicm     1812: format_defaults_client(struct format_tree *ft, struct client *c)
1.3       nicm     1813: {
1.60      nicm     1814:        struct session  *s;
1.103     nicm     1815:        const char      *name;
1.115     nicm     1816:        struct tty      *tty = &c->tty;
                   1817:        const char      *types[] = TTY_TYPES;
1.3       nicm     1818:
1.54      nicm     1819:        if (ft->s == NULL)
                   1820:                ft->s = c->session;
1.164     nicm     1821:        ft->c = c;
1.54      nicm     1822:
1.124     nicm     1823:        format_add(ft, "client_name", "%s", c->name);
1.72      nicm     1824:        format_add(ft, "client_pid", "%ld", (long) c->pid);
1.115     nicm     1825:        format_add(ft, "client_height", "%u", tty->sy);
                   1826:        format_add(ft, "client_width", "%u", tty->sx);
1.124     nicm     1827:        format_add(ft, "client_tty", "%s", c->ttyname);
1.75      nicm     1828:        format_add(ft, "client_control_mode", "%d",
                   1829:                !!(c->flags & CLIENT_CONTROL));
1.3       nicm     1830:
1.115     nicm     1831:        if (tty->term_name != NULL)
                   1832:                format_add(ft, "client_termname", "%s", tty->term_name);
                   1833:        if (tty->term_name != NULL)
                   1834:                format_add(ft, "client_termtype", "%s", types[tty->term_type]);
                   1835:
1.87      nicm     1836:        format_add_tv(ft, "client_created", &c->creation_time);
                   1837:        format_add_tv(ft, "client_activity", &c->activity_time);
1.126     nicm     1838:
                   1839:        format_add(ft, "client_written", "%zu", c->written);
                   1840:        format_add(ft, "client_discarded", "%zu", c->discarded);
1.14      nicm     1841:
1.103     nicm     1842:        name = server_client_get_key_table(c);
                   1843:        if (strcmp(c->keytable->name, name) == 0)
1.61      nicm     1844:                format_add(ft, "client_prefix", "%d", 0);
                   1845:        else
                   1846:                format_add(ft, "client_prefix", "%d", 1);
                   1847:        format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3       nicm     1848:
1.115     nicm     1849:        if (tty->flags & TTY_UTF8)
1.3       nicm     1850:                format_add(ft, "client_utf8", "%d", 1);
                   1851:        else
                   1852:                format_add(ft, "client_utf8", "%d", 0);
                   1853:
                   1854:        if (c->flags & CLIENT_READONLY)
                   1855:                format_add(ft, "client_readonly", "%d", 1);
                   1856:        else
                   1857:                format_add(ft, "client_readonly", "%d", 0);
1.15      nicm     1858:
                   1859:        s = c->session;
                   1860:        if (s != NULL)
                   1861:                format_add(ft, "client_session", "%s", s->name);
                   1862:        s = c->last_session;
                   1863:        if (s != NULL && session_alive(s))
                   1864:                format_add(ft, "client_last_session", "%s", s->name);
1.1       nicm     1865: }
                   1866:
1.32      nicm     1867: /* Set default format keys for a window. */
                   1868: void
1.57      nicm     1869: format_defaults_window(struct format_tree *ft, struct window *w)
1.32      nicm     1870: {
1.54      nicm     1871:        ft->w = w;
                   1872:
1.87      nicm     1873:        format_add_tv(ft, "window_activity", &w->activity_time);
1.32      nicm     1874:        format_add(ft, "window_id", "@%u", w->id);
                   1875:        format_add(ft, "window_name", "%s", w->name);
                   1876:        format_add(ft, "window_width", "%u", w->sx);
                   1877:        format_add(ft, "window_height", "%u", w->sy);
1.80      nicm     1878:        format_add_cb(ft, "window_layout", format_cb_window_layout);
1.96      nicm     1879:        format_add_cb(ft, "window_visible_layout",
                   1880:            format_cb_window_visible_layout);
1.32      nicm     1881:        format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59      nicm     1882:        format_add(ft, "window_zoomed_flag", "%d",
1.53      nicm     1883:            !!(w->flags & WINDOW_ZOOMED));
1.32      nicm     1884: }
                   1885:
1.1       nicm     1886: /* Set default format keys for a winlink. */
1.108     nicm     1887: static void
1.129     nicm     1888: format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
1.1       nicm     1889: {
1.164     nicm     1890:        struct client   *c = ft->c;
1.129     nicm     1891:        struct session  *s = wl->session;
1.1       nicm     1892:        struct window   *w = wl->window;
1.164     nicm     1893:        int              flag;
                   1894:        u_int            ox, oy, sx, sy;
1.1       nicm     1895:
1.54      nicm     1896:        if (ft->w == NULL)
                   1897:                ft->w = wl->window;
1.133     nicm     1898:        ft->wl = wl;
1.54      nicm     1899:
1.57      nicm     1900:        format_defaults_window(ft, w);
1.32      nicm     1901:
1.164     nicm     1902:        if (c != NULL) {
                   1903:                flag = tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
                   1904:                format_add(ft, "window_bigger", "%d", flag);
                   1905:                if (flag) {
                   1906:                        format_add(ft, "window_offset_x", "%u", ox);
                   1907:                        format_add(ft, "window_offset_y", "%u", oy);
                   1908:                }
                   1909:        }
                   1910:
1.1       nicm     1911:        format_add(ft, "window_index", "%d", wl->idx);
1.133     nicm     1912:        format_add_cb(ft, "window_stack_index", format_cb_window_stack_index);
1.129     nicm     1913:        format_add(ft, "window_flags", "%s", window_printable_flags(wl));
1.1       nicm     1914:        format_add(ft, "window_active", "%d", wl == s->curw);
1.176     nicm     1915:
                   1916:        format_add(ft, "window_start_flag", "%d",
                   1917:            !!(wl == RB_MIN(winlinks, &s->windows)));
                   1918:        format_add(ft, "window_end_flag", "%d",
                   1919:            !!(wl == RB_MAX(winlinks, &s->windows)));
1.29      nicm     1920:
1.59      nicm     1921:        format_add(ft, "window_bell_flag", "%d",
1.29      nicm     1922:            !!(wl->flags & WINLINK_BELL));
1.59      nicm     1923:        format_add(ft, "window_activity_flag", "%d",
1.29      nicm     1924:            !!(wl->flags & WINLINK_ACTIVITY));
1.59      nicm     1925:        format_add(ft, "window_silence_flag", "%d",
1.29      nicm     1926:            !!(wl->flags & WINLINK_SILENCE));
1.59      nicm     1927:        format_add(ft, "window_last_flag", "%d",
1.49      nicm     1928:            !!(wl == TAILQ_FIRST(&s->lastw)));
1.64      nicm     1929:        format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.1       nicm     1930: }
                   1931:
                   1932: /* Set default format keys for a window pane. */
                   1933: void
1.57      nicm     1934: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1       nicm     1935: {
1.168     nicm     1936:        struct window                   *w = wp->window;
                   1937:        struct grid                     *gd = wp->base.grid;
                   1938:        int                              status = wp->status;
                   1939:        u_int                            idx;
                   1940:        struct window_mode_entry        *wme;
1.54      nicm     1941:
                   1942:        if (ft->w == NULL)
1.159     nicm     1943:                ft->w = w;
1.79      nicm     1944:        ft->wp = wp;
1.1       nicm     1945:
1.16      nicm     1946:        format_add(ft, "history_size", "%u", gd->hsize);
                   1947:        format_add(ft, "history_limit", "%u", gd->hlimit);
1.80      nicm     1948:        format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.1       nicm     1949:
1.4       nicm     1950:        if (window_pane_index(wp, &idx) != 0)
                   1951:                fatalx("index not found");
1.16      nicm     1952:        format_add(ft, "pane_index", "%u", idx);
1.4       nicm     1953:
1.1       nicm     1954:        format_add(ft, "pane_width", "%u", wp->sx);
                   1955:        format_add(ft, "pane_height", "%u", wp->sy);
                   1956:        format_add(ft, "pane_title", "%s", wp->base.title);
                   1957:        format_add(ft, "pane_id", "%%%u", wp->id);
1.159     nicm     1958:        format_add(ft, "pane_active", "%d", wp == w->active);
1.55      nicm     1959:        format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
1.143     nicm     1960:        format_add(ft, "pane_pipe", "%d", wp->pipe_fd != -1);
1.55      nicm     1961:
1.147     nicm     1962:        if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(status))
1.55      nicm     1963:                format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1       nicm     1964:        format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47      nicm     1965:
1.164     nicm     1966:        format_add(ft, "pane_left", "%u", wp->xoff);
                   1967:        format_add(ft, "pane_top", "%u", wp->yoff);
                   1968:        format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
                   1969:        format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
                   1970:        format_add(ft, "pane_at_left", "%d", wp->xoff == 0);
                   1971:        format_add(ft, "pane_at_top", "%d", wp->yoff == 0);
                   1972:        format_add(ft, "pane_at_right", "%d", wp->xoff + wp->sx == w->sx);
                   1973:        format_add(ft, "pane_at_bottom", "%d", wp->yoff + wp->sy == w->sy);
1.16      nicm     1974:
1.168     nicm     1975:        wme = TAILQ_FIRST(&wp->modes);
                   1976:        if (wme != NULL) {
                   1977:                format_add(ft, "pane_mode", "%s", wme->mode->name);
                   1978:                if (wme->mode->formats != NULL)
                   1979:                        wme->mode->formats(wme, ft);
                   1980:        }
                   1981:        format_add_cb(ft, "pane_in_mode", format_cb_pane_in_mode);
1.134     nicm     1982:
1.26      nicm     1983:        format_add(ft, "pane_synchronized", "%d",
1.159     nicm     1984:            !!options_get_number(w->options, "synchronize-panes"));
1.135     nicm     1985:        if (wp->searchstr != NULL)
                   1986:                format_add(ft, "pane_search_string", "%s", wp->searchstr);
1.16      nicm     1987:
1.71      nicm     1988:        format_add(ft, "pane_tty", "%s", wp->tty);
1.16      nicm     1989:        format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79      nicm     1990:        format_add_cb(ft, "pane_start_command", format_cb_start_command);
                   1991:        format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.16      nicm     1992:
1.59      nicm     1993:        format_add(ft, "cursor_x", "%u", wp->base.cx);
                   1994:        format_add(ft, "cursor_y", "%u", wp->base.cy);
                   1995:        format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
                   1996:        format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.16      nicm     1997:
                   1998:        format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59      nicm     1999:        format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
                   2000:        format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16      nicm     2001:
                   2002:        format_add(ft, "cursor_flag", "%d",
                   2003:            !!(wp->base.mode & MODE_CURSOR));
                   2004:        format_add(ft, "insert_flag", "%d",
                   2005:            !!(wp->base.mode & MODE_INSERT));
                   2006:        format_add(ft, "keypad_cursor_flag", "%d",
                   2007:            !!(wp->base.mode & MODE_KCURSOR));
                   2008:        format_add(ft, "keypad_flag", "%d",
                   2009:            !!(wp->base.mode & MODE_KKEYPAD));
                   2010:        format_add(ft, "wrap_flag", "%d",
                   2011:            !!(wp->base.mode & MODE_WRAP));
                   2012:
1.62      nicm     2013:        format_add(ft, "mouse_any_flag", "%d",
1.119     nicm     2014:            !!(wp->base.mode & ALL_MOUSE_MODES));
1.16      nicm     2015:        format_add(ft, "mouse_standard_flag", "%d",
                   2016:            !!(wp->base.mode & MODE_MOUSE_STANDARD));
                   2017:        format_add(ft, "mouse_button_flag", "%d",
                   2018:            !!(wp->base.mode & MODE_MOUSE_BUTTON));
1.119     nicm     2019:        format_add(ft, "mouse_all_flag", "%d",
                   2020:            !!(wp->base.mode & MODE_MOUSE_ALL));
1.19      nicm     2021:
1.80      nicm     2022:        format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8       nicm     2023: }
                   2024:
1.19      nicm     2025: /* Set default format keys for paste buffer. */
1.8       nicm     2026: void
1.94      nicm     2027: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
1.8       nicm     2028: {
1.146     nicm     2029:        struct timeval   tv;
                   2030:        size_t           size;
                   2031:        char            *s;
                   2032:
                   2033:        timerclear(&tv);
                   2034:        tv.tv_sec = paste_buffer_created(pb);
                   2035:        paste_buffer_data(pb, &size);
1.8       nicm     2036:
1.146     nicm     2037:        format_add(ft, "buffer_size", "%zu", size);
1.81      nicm     2038:        format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.146     nicm     2039:        format_add_tv(ft, "buffer_created", &tv);
1.8       nicm     2040:
1.94      nicm     2041:        s = paste_make_sample(pb);
1.42      nicm     2042:        format_add(ft, "buffer_sample", "%s", s);
                   2043:        free(s);
1.1       nicm     2044: }