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

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