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

1.228   ! nicm        1: /* $OpenBSD: format.c,v 1.227 2020/03/19 13:43:18 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.226     nicm       26: #include <math.h>
1.202     nicm       27: #include <regex.h>
1.1       nicm       28: #include <stdarg.h>
1.9       nicm       29: #include <stdlib.h>
1.1       nicm       30: #include <string.h>
                     31: #include <time.h>
                     32: #include <unistd.h>
                     33:
                     34: #include "tmux.h"
                     35:
                     36: /*
                     37:  * Build a list of key-value pairs and use them to expand #{key} entries in a
                     38:  * string.
                     39:  */
                     40:
1.79      nicm       41: struct format_entry;
                     42: typedef void (*format_cb)(struct format_tree *, struct format_entry *);
                     43:
1.108     nicm       44: static char    *format_job_get(struct format_tree *, const char *);
                     45: static void     format_job_timer(int, short, void *);
                     46:
                     47: static char    *format_find(struct format_tree *, const char *, int);
                     48: static void     format_add_cb(struct format_tree *, const char *, format_cb);
                     49: static void     format_add_tv(struct format_tree *, const char *,
                     50:                     struct timeval *);
                     51: static int      format_replace(struct format_tree *, const char *, size_t,
                     52:                     char **, size_t *, size_t *);
                     53: static void     format_defaults_session(struct format_tree *,
                     54:                     struct session *);
                     55: static void     format_defaults_client(struct format_tree *, struct client *);
1.164     nicm       56: static void     format_defaults_winlink(struct format_tree *, struct winlink *);
1.1       nicm       57:
1.68      nicm       58: /* Entry in format job tree. */
                     59: struct format_job {
1.131     nicm       60:        struct client           *client;
1.120     nicm       61:        u_int                    tag;
1.68      nicm       62:        const char              *cmd;
1.113     nicm       63:        const char              *expanded;
1.68      nicm       64:
                     65:        time_t                   last;
                     66:        char                    *out;
1.127     nicm       67:        int                      updated;
1.68      nicm       68:
                     69:        struct job              *job;
                     70:        int                      status;
                     71:
                     72:        RB_ENTRY(format_job)     entry;
                     73: };
                     74:
                     75: /* Format job tree. */
1.108     nicm       76: static struct event format_job_event;
                     77: static int format_job_cmp(struct format_job *, struct format_job *);
1.109     nicm       78: static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
1.108     nicm       79: RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
1.68      nicm       80:
                     81: /* Format job tree comparison function. */
1.108     nicm       82: static int
1.68      nicm       83: format_job_cmp(struct format_job *fj1, struct format_job *fj2)
                     84: {
1.120     nicm       85:        if (fj1->tag < fj2->tag)
                     86:                return (-1);
                     87:        if (fj1->tag > fj2->tag)
                     88:                return (1);
1.68      nicm       89:        return (strcmp(fj1->cmd, fj2->cmd));
                     90: }
                     91:
1.87      nicm       92: /* Format modifiers. */
                     93: #define FORMAT_TIMESTRING 0x1
                     94: #define FORMAT_BASENAME 0x2
                     95: #define FORMAT_DIRNAME 0x4
1.169     nicm       96: #define FORMAT_QUOTE 0x8
                     97: #define FORMAT_LITERAL 0x10
1.170     nicm       98: #define FORMAT_EXPAND 0x20
1.175     nicm       99: #define FORMAT_EXPANDTIME 0x40
                    100: #define FORMAT_SESSIONS 0x80
                    101: #define FORMAT_WINDOWS 0x100
                    102: #define FORMAT_PANES 0x200
1.87      nicm      103:
1.178     nicm      104: /* Limit on recursion. */
                    105: #define FORMAT_LOOP_LIMIT 10
                    106:
1.54      nicm      107: /* Entry in format tree. */
                    108: struct format_entry {
1.79      nicm      109:        char                    *key;
                    110:        char                    *value;
1.87      nicm      111:        time_t                   t;
1.79      nicm      112:        format_cb                cb;
                    113:        RB_ENTRY(format_entry)   entry;
1.54      nicm      114: };
                    115:
1.68      nicm      116: /* Format entry tree. */
1.54      nicm      117: struct format_tree {
1.164     nicm      118:        struct client           *c;
                    119:        struct session          *s;
                    120:        struct winlink          *wl;
1.79      nicm      121:        struct window           *w;
                    122:        struct window_pane      *wp;
1.54      nicm      123:
1.172     nicm      124:        struct cmdq_item        *item;
1.131     nicm      125:        struct client           *client;
1.120     nicm      126:        u_int                    tag;
1.84      nicm      127:        int                      flags;
1.177     nicm      128:        time_t                   time;
1.178     nicm      129:        u_int                    loop;
1.68      nicm      130:
1.197     nicm      131:        struct mouse_event       m;
                    132:
1.68      nicm      133:        RB_HEAD(format_entry_tree, format_entry) tree;
1.54      nicm      134: };
1.108     nicm      135: static int format_entry_cmp(struct format_entry *, struct format_entry *);
                    136: RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
1.54      nicm      137:
1.180     nicm      138: /* Format modifier. */
1.169     nicm      139: struct format_modifier {
                    140:        char      modifier[3];
                    141:        u_int     size;
                    142:
                    143:        char    **argv;
                    144:        int       argc;
                    145: };
                    146:
1.68      nicm      147: /* Format entry tree comparison function. */
1.108     nicm      148: static int
1.68      nicm      149: format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
1.1       nicm      150: {
                    151:        return (strcmp(fe1->key, fe2->key));
                    152: }
                    153:
1.25      nicm      154: /* Single-character uppercase aliases. */
1.108     nicm      155: static const char *format_upper[] = {
1.1       nicm      156:        NULL,           /* A */
                    157:        NULL,           /* B */
                    158:        NULL,           /* C */
                    159:        "pane_id",      /* D */
                    160:        NULL,           /* E */
                    161:        "window_flags", /* F */
                    162:        NULL,           /* G */
                    163:        "host",         /* H */
                    164:        "window_index", /* I */
                    165:        NULL,           /* J */
                    166:        NULL,           /* K */
                    167:        NULL,           /* L */
                    168:        NULL,           /* M */
                    169:        NULL,           /* N */
                    170:        NULL,           /* O */
                    171:        "pane_index",   /* P */
                    172:        NULL,           /* Q */
                    173:        NULL,           /* R */
                    174:        "session_name", /* S */
                    175:        "pane_title",   /* T */
                    176:        NULL,           /* U */
                    177:        NULL,           /* V */
                    178:        "window_name",  /* W */
                    179:        NULL,           /* X */
                    180:        NULL,           /* Y */
                    181:        NULL            /* Z */
                    182: };
                    183:
1.25      nicm      184: /* Single-character lowercase aliases. */
1.108     nicm      185: static const char *format_lower[] = {
1.25      nicm      186:        NULL,           /* a */
                    187:        NULL,           /* b */
                    188:        NULL,           /* c */
                    189:        NULL,           /* d */
                    190:        NULL,           /* e */
                    191:        NULL,           /* f */
                    192:        NULL,           /* g */
                    193:        "host_short",   /* h */
                    194:        NULL,           /* i */
                    195:        NULL,           /* j */
                    196:        NULL,           /* k */
                    197:        NULL,           /* l */
                    198:        NULL,           /* m */
                    199:        NULL,           /* n */
                    200:        NULL,           /* o */
                    201:        NULL,           /* p */
                    202:        NULL,           /* q */
                    203:        NULL,           /* r */
                    204:        NULL,           /* s */
                    205:        NULL,           /* t */
                    206:        NULL,           /* u */
                    207:        NULL,           /* v */
                    208:        NULL,           /* w */
                    209:        NULL,           /* x */
                    210:        NULL,           /* y */
                    211:        NULL            /* z */
                    212: };
                    213:
1.179     nicm      214: /* Is logging enabled? */
                    215: static inline int
                    216: format_logging(struct format_tree *ft)
                    217: {
                    218:        return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE));
                    219: }
                    220:
                    221: /* Log a message if verbose. */
                    222: static void printflike(3, 4)
                    223: format_log1(struct format_tree *ft, const char *from, const char *fmt, ...)
                    224: {
                    225:        va_list                  ap;
                    226:        char                    *s;
                    227:        static const char        spaces[] = "          ";
                    228:
                    229:        if (!format_logging(ft))
                    230:                return;
                    231:
                    232:        va_start(ap, fmt);
                    233:        vasprintf(&s, fmt, ap);
                    234:        va_end(ap);
                    235:
                    236:        log_debug("%s: %s", from, s);
1.181     nicm      237:        if (ft->item != NULL && (ft->flags & FORMAT_VERBOSE))
1.179     nicm      238:                cmdq_print(ft->item, "#%.*s%s", ft->loop, spaces, s);
                    239:
                    240:        free(s);
                    241: }
                    242: #define format_log(ft, fmt, ...) format_log1(ft, __func__, fmt, ##__VA_ARGS__)
                    243:
1.127     nicm      244: /* Format job update callback. */
1.108     nicm      245: static void
1.127     nicm      246: format_job_update(struct job *job)
                    247: {
1.160     nicm      248:        struct format_job       *fj = job_get_data(job);
                    249:        struct evbuffer         *evb = job_get_event(job)->input;
1.151     nicm      250:        char                    *line = NULL, *next;
1.127     nicm      251:        time_t                   t;
                    252:
1.151     nicm      253:        while ((next = evbuffer_readline(evb)) != NULL) {
                    254:                free(line);
                    255:                line = next;
                    256:        }
                    257:        if (line == NULL)
1.127     nicm      258:                return;
                    259:        fj->updated = 1;
                    260:
                    261:        free(fj->out);
                    262:        fj->out = line;
                    263:
1.136     nicm      264:        log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out);
1.127     nicm      265:
1.142     nicm      266:        t = time(NULL);
1.127     nicm      267:        if (fj->status && fj->last != t) {
1.136     nicm      268:                if (fj->client != NULL)
                    269:                        server_status_client(fj->client);
1.127     nicm      270:                fj->last = t;
                    271:        }
                    272: }
                    273:
                    274: /* Format job complete callback. */
                    275: static void
                    276: format_job_complete(struct job *job)
1.68      nicm      277: {
1.160     nicm      278:        struct format_job       *fj = job_get_data(job);
                    279:        struct evbuffer         *evb = job_get_event(job)->input;
1.68      nicm      280:        char                    *line, *buf;
                    281:        size_t                   len;
                    282:
                    283:        fj->job = NULL;
                    284:
                    285:        buf = NULL;
1.160     nicm      286:        if ((line = evbuffer_readline(evb)) == NULL) {
                    287:                len = EVBUFFER_LENGTH(evb);
1.68      nicm      288:                buf = xmalloc(len + 1);
                    289:                if (len != 0)
1.160     nicm      290:                        memcpy(buf, EVBUFFER_DATA(evb), len);
1.68      nicm      291:                buf[len] = '\0';
                    292:        } else
                    293:                buf = line;
1.127     nicm      294:
1.136     nicm      295:        log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf);
                    296:
1.127     nicm      297:        if (*buf != '\0' || !fj->updated) {
                    298:                free(fj->out);
                    299:                fj->out = buf;
                    300:        } else
                    301:                free(buf);
1.68      nicm      302:
                    303:        if (fj->status) {
1.131     nicm      304:                if (fj->client != NULL)
                    305:                        server_status_client(fj->client);
1.68      nicm      306:                fj->status = 0;
                    307:        }
                    308: }
                    309:
                    310: /* Find a job. */
1.108     nicm      311: static char *
1.68      nicm      312: format_job_get(struct format_tree *ft, const char *cmd)
                    313: {
1.131     nicm      314:        struct format_job_tree  *jobs;
1.113     nicm      315:        struct format_job        fj0, *fj;
                    316:        time_t                   t;
                    317:        char                    *expanded;
                    318:        int                      force;
1.68      nicm      319:
1.131     nicm      320:        if (ft->client == NULL)
                    321:                jobs = &format_jobs;
                    322:        else if (ft->client->jobs != NULL)
                    323:                jobs = ft->client->jobs;
                    324:        else {
                    325:                jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
                    326:                RB_INIT(jobs);
                    327:        }
                    328:
1.120     nicm      329:        fj0.tag = ft->tag;
1.68      nicm      330:        fj0.cmd = cmd;
1.131     nicm      331:        if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
1.68      nicm      332:                fj = xcalloc(1, sizeof *fj);
1.131     nicm      333:                fj->client = ft->client;
1.120     nicm      334:                fj->tag = ft->tag;
1.68      nicm      335:                fj->cmd = xstrdup(cmd);
1.113     nicm      336:                fj->expanded = NULL;
1.68      nicm      337:
                    338:                xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
                    339:
1.131     nicm      340:                RB_INSERT(format_job_tree, jobs, fj);
1.68      nicm      341:        }
                    342:
1.113     nicm      343:        expanded = format_expand(ft, cmd);
                    344:        if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
                    345:                free((void *)fj->expanded);
                    346:                fj->expanded = xstrdup(expanded);
                    347:                force = 1;
                    348:        } else
                    349:                force = (ft->flags & FORMAT_FORCE);
                    350:
1.84      nicm      351:        t = time(NULL);
1.183     nicm      352:        if (force && fj->job != NULL)
                    353:               job_free(fj->job);
                    354:        if (force || (fj->job == NULL && fj->last != t)) {
1.163     nicm      355:                fj->job = job_run(expanded, NULL,
                    356:                    server_client_get_cwd(ft->client, NULL), format_job_update,
1.227     nicm      357:                    format_job_complete, NULL, fj, JOB_NOWAIT, -1, -1);
1.68      nicm      358:                if (fj->job == NULL) {
                    359:                        free(fj->out);
                    360:                        xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
                    361:                }
1.84      nicm      362:                fj->last = t;
1.137     nicm      363:                fj->updated = 0;
1.68      nicm      364:        }
1.84      nicm      365:
                    366:        if (ft->flags & FORMAT_STATUS)
                    367:                fj->status = 1;
1.68      nicm      368:
1.113     nicm      369:        free(expanded);
1.86      nicm      370:        return (format_expand(ft, fj->out));
1.68      nicm      371: }
                    372:
                    373: /* Remove old jobs. */
1.108     nicm      374: static void
1.131     nicm      375: format_job_tidy(struct format_job_tree *jobs, int force)
1.68      nicm      376: {
                    377:        struct format_job       *fj, *fj1;
                    378:        time_t                   now;
                    379:
                    380:        now = time(NULL);
1.131     nicm      381:        RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
                    382:                if (!force && (fj->last > now || now - fj->last < 3600))
1.68      nicm      383:                        continue;
1.131     nicm      384:                RB_REMOVE(format_job_tree, jobs, fj);
1.68      nicm      385:
1.77      nicm      386:                log_debug("%s: %s", __func__, fj->cmd);
                    387:
1.68      nicm      388:                if (fj->job != NULL)
                    389:                        job_free(fj->job);
                    390:
1.113     nicm      391:                free((void *)fj->expanded);
1.78      nicm      392:                free((void *)fj->cmd);
1.68      nicm      393:                free(fj->out);
                    394:
                    395:                free(fj);
                    396:        }
1.131     nicm      397: }
                    398:
                    399: /* Remove old jobs for client. */
                    400: void
                    401: format_lost_client(struct client *c)
                    402: {
                    403:        if (c->jobs != NULL)
                    404:                format_job_tidy(c->jobs, 1);
                    405:        free(c->jobs);
                    406: }
                    407:
                    408: /* Remove old jobs periodically. */
                    409: static void
                    410: format_job_timer(__unused int fd, __unused short events, __unused void *arg)
                    411: {
                    412:        struct client   *c;
                    413:        struct timeval   tv = { .tv_sec = 60 };
                    414:
                    415:        format_job_tidy(&format_jobs, 0);
                    416:        TAILQ_FOREACH(c, &clients, entry) {
                    417:                if (c->jobs != NULL)
                    418:                        format_job_tidy(c->jobs, 0);
                    419:        }
1.77      nicm      420:
                    421:        evtimer_del(&format_job_event);
                    422:        evtimer_add(&format_job_event, &tv);
1.68      nicm      423: }
                    424:
1.79      nicm      425: /* Callback for host. */
1.108     nicm      426: static void
1.99      nicm      427: format_cb_host(__unused struct format_tree *ft, struct format_entry *fe)
1.79      nicm      428: {
                    429:        char host[HOST_NAME_MAX + 1];
                    430:
                    431:        if (gethostname(host, sizeof host) != 0)
                    432:                fe->value = xstrdup("");
                    433:        else
                    434:                fe->value = xstrdup(host);
                    435: }
                    436:
                    437: /* Callback for host_short. */
1.108     nicm      438: static void
1.99      nicm      439: format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe)
1.79      nicm      440: {
                    441:        char host[HOST_NAME_MAX + 1], *cp;
                    442:
                    443:        if (gethostname(host, sizeof host) != 0)
                    444:                fe->value = xstrdup("");
                    445:        else {
                    446:                if ((cp = strchr(host, '.')) != NULL)
                    447:                        *cp = '\0';
                    448:                fe->value = xstrdup(host);
                    449:        }
                    450: }
                    451:
                    452: /* Callback for pid. */
1.108     nicm      453: static void
1.99      nicm      454: format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe)
1.79      nicm      455: {
                    456:        xasprintf(&fe->value, "%ld", (long)getpid());
                    457: }
                    458:
1.221     nicm      459: /* Callback for session_attached_list. */
                    460: static void
                    461: format_cb_session_attached_list(struct format_tree *ft, struct format_entry *fe)
                    462: {
                    463:        struct session  *s = ft->s;
                    464:        struct client   *loop;
                    465:        struct evbuffer *buffer;
                    466:        int              size;
                    467:
                    468:        if (s == NULL)
                    469:                return;
                    470:
                    471:        buffer = evbuffer_new();
                    472:        if (buffer == NULL)
                    473:                fatalx("out of memory");
                    474:
                    475:        TAILQ_FOREACH(loop, &clients, entry) {
                    476:                if (loop->session == s) {
                    477:                        if (EVBUFFER_LENGTH(buffer) > 0)
                    478:                                evbuffer_add(buffer, ",", 1);
                    479:                        evbuffer_add_printf(buffer, "%s", loop->name);
                    480:                }
                    481:        }
                    482:
                    483:        if ((size = EVBUFFER_LENGTH(buffer)) != 0)
                    484:                xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
                    485:        evbuffer_free(buffer);
                    486: }
                    487:
1.80      nicm      488: /* Callback for session_alerts. */
1.108     nicm      489: static void
1.80      nicm      490: format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
                    491: {
                    492:        struct session  *s = ft->s;
                    493:        struct winlink  *wl;
1.133     nicm      494:        char             alerts[1024], tmp[16];
1.80      nicm      495:
                    496:        if (s == NULL)
                    497:                return;
                    498:
                    499:        *alerts = '\0';
                    500:        RB_FOREACH(wl, winlinks, &s->windows) {
                    501:                if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
                    502:                        continue;
                    503:                xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
                    504:
                    505:                if (*alerts != '\0')
                    506:                        strlcat(alerts, ",", sizeof alerts);
                    507:                strlcat(alerts, tmp, sizeof alerts);
                    508:                if (wl->flags & WINLINK_ACTIVITY)
                    509:                        strlcat(alerts, "#", sizeof alerts);
                    510:                if (wl->flags & WINLINK_BELL)
                    511:                        strlcat(alerts, "!", sizeof alerts);
                    512:                if (wl->flags & WINLINK_SILENCE)
                    513:                        strlcat(alerts, "~", sizeof alerts);
                    514:        }
                    515:        fe->value = xstrdup(alerts);
                    516: }
                    517:
1.133     nicm      518: /* Callback for session_stack. */
                    519: static void
                    520: format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
                    521: {
                    522:        struct session  *s = ft->s;
                    523:        struct winlink  *wl;
                    524:        char             result[1024], tmp[16];
                    525:
                    526:        if (s == NULL)
                    527:                return;
                    528:
                    529:        xsnprintf(result, sizeof result, "%u", s->curw->idx);
                    530:        TAILQ_FOREACH(wl, &s->lastw, sentry) {
                    531:                xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
                    532:
                    533:                if (*result != '\0')
                    534:                        strlcat(result, ",", sizeof result);
                    535:                strlcat(result, tmp, sizeof result);
                    536:        }
                    537:        fe->value = xstrdup(result);
                    538: }
                    539:
                    540: /* Callback for window_stack_index. */
                    541: static void
                    542: format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe)
                    543: {
                    544:        struct session  *s = ft->wl->session;
                    545:        struct winlink  *wl;
                    546:        u_int            idx;
                    547:
                    548:        idx = 0;
                    549:        TAILQ_FOREACH(wl, &s->lastw, sentry) {
                    550:                idx++;
                    551:                if (wl == ft->wl)
                    552:                        break;
                    553:        }
                    554:        if (wl != NULL)
                    555:                xasprintf(&fe->value, "%u", idx);
                    556:        else
                    557:                fe->value = xstrdup("0");
                    558: }
                    559:
1.221     nicm      560: /* Callback for window_linked_sessions_list. */
                    561: static void
                    562: format_cb_window_linked_sessions_list(struct format_tree *ft,
                    563:     struct format_entry *fe)
                    564: {
                    565:        struct window   *w = ft->wl->window;
                    566:        struct winlink  *wl;
                    567:        struct evbuffer *buffer;
                    568:        int              size;
                    569:
                    570:        buffer = evbuffer_new();
                    571:        if (buffer == NULL)
                    572:                fatalx("out of memory");
                    573:
                    574:        TAILQ_FOREACH(wl, &w->winlinks, wentry) {
                    575:                if (EVBUFFER_LENGTH(buffer) > 0)
                    576:                        evbuffer_add(buffer, ",", 1);
                    577:                evbuffer_add_printf(buffer, "%s", wl->session->name);
                    578:        }
                    579:
                    580:        if ((size = EVBUFFER_LENGTH(buffer)) != 0)
                    581:                xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
                    582:        evbuffer_free(buffer);
                    583: }
                    584:
                    585: /* Callback for window_active_sessions. */
                    586: static void
                    587: format_cb_window_active_sessions(struct format_tree *ft,
                    588:     struct format_entry *fe)
                    589: {
                    590:        struct window   *w = ft->wl->window;
                    591:        struct winlink  *wl;
                    592:        u_int            n = 0;
                    593:
                    594:        TAILQ_FOREACH(wl, &w->winlinks, wentry) {
                    595:                if (wl->session->curw == wl)
                    596:                        n++;
                    597:        }
                    598:
                    599:        xasprintf(&fe->value, "%u", n);
                    600: }
                    601:
                    602: /* Callback for window_active_sessions_list. */
                    603: static void
                    604: format_cb_window_active_sessions_list(struct format_tree *ft,
                    605:     struct format_entry *fe)
                    606: {
                    607:        struct window   *w = ft->wl->window;
                    608:        struct winlink  *wl;
                    609:        struct evbuffer *buffer;
                    610:        int              size;
                    611:
                    612:        buffer = evbuffer_new();
                    613:        if (buffer == NULL)
                    614:                fatalx("out of memory");
                    615:
                    616:        TAILQ_FOREACH(wl, &w->winlinks, wentry) {
                    617:                if (wl->session->curw == wl) {
                    618:                        if (EVBUFFER_LENGTH(buffer) > 0)
                    619:                                evbuffer_add(buffer, ",", 1);
                    620:                        evbuffer_add_printf(buffer, "%s", wl->session->name);
                    621:                }
                    622:        }
                    623:
                    624:        if ((size = EVBUFFER_LENGTH(buffer)) != 0)
                    625:                xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
                    626:        evbuffer_free(buffer);
                    627: }
                    628:
                    629: /* Callback for window_active_clients. */
                    630: static void
                    631: format_cb_window_active_clients(struct format_tree *ft, struct format_entry *fe)
                    632: {
                    633:        struct window   *w = ft->wl->window;
                    634:        struct client   *loop;
                    635:        struct session  *client_session;
                    636:        u_int            n = 0;
                    637:
                    638:        TAILQ_FOREACH(loop, &clients, entry) {
                    639:                client_session = loop->session;
                    640:                if (client_session == NULL)
                    641:                        continue;
                    642:
                    643:                if (w == client_session->curw->window)
                    644:                        n++;
                    645:        }
                    646:
                    647:        xasprintf(&fe->value, "%u", n);
                    648: }
                    649:
                    650: /* Callback for window_active_clients_list. */
                    651: static void
                    652: format_cb_window_active_clients_list(struct format_tree *ft,
                    653:     struct format_entry *fe)
                    654: {
                    655:        struct window   *w = ft->wl->window;
                    656:        struct client   *loop;
                    657:        struct session  *client_session;
                    658:        struct evbuffer *buffer;
                    659:        int              size;
                    660:
                    661:        buffer = evbuffer_new();
                    662:        if (buffer == NULL)
                    663:                fatalx("out of memory");
                    664:
                    665:        TAILQ_FOREACH(loop, &clients, entry) {
                    666:                client_session = loop->session;
                    667:                if (client_session == NULL)
                    668:                        continue;
                    669:
                    670:                if (w == client_session->curw->window) {
                    671:                        if (EVBUFFER_LENGTH(buffer) > 0)
                    672:                                evbuffer_add(buffer, ",", 1);
                    673:                        evbuffer_add_printf(buffer, "%s", loop->name);
                    674:                }
                    675:        }
                    676:
                    677:        if ((size = EVBUFFER_LENGTH(buffer)) != 0)
                    678:                xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
                    679:        evbuffer_free(buffer);
                    680: }
                    681:
1.80      nicm      682: /* Callback for window_layout. */
1.108     nicm      683: static void
1.80      nicm      684: format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
                    685: {
                    686:        struct window   *w = ft->w;
                    687:
                    688:        if (w == NULL)
                    689:                return;
                    690:
                    691:        if (w->saved_layout_root != NULL)
                    692:                fe->value = layout_dump(w->saved_layout_root);
                    693:        else
                    694:                fe->value = layout_dump(w->layout_root);
                    695: }
                    696:
1.96      nicm      697: /* Callback for window_visible_layout. */
1.108     nicm      698: static void
1.96      nicm      699: format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe)
                    700: {
                    701:        struct window   *w = ft->w;
                    702:
                    703:        if (w == NULL)
                    704:                return;
                    705:
                    706:        fe->value = layout_dump(w->layout_root);
                    707: }
                    708:
1.79      nicm      709: /* Callback for pane_start_command. */
1.108     nicm      710: static void
1.79      nicm      711: format_cb_start_command(struct format_tree *ft, struct format_entry *fe)
                    712: {
                    713:        struct window_pane      *wp = ft->wp;
                    714:
                    715:        if (wp == NULL)
                    716:                return;
                    717:
                    718:        fe->value = cmd_stringify_argv(wp->argc, wp->argv);
                    719: }
                    720:
                    721: /* Callback for pane_current_command. */
1.108     nicm      722: static void
1.79      nicm      723: format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
                    724: {
                    725:        struct window_pane      *wp = ft->wp;
                    726:        char                    *cmd;
                    727:
1.212     nicm      728:        if (wp == NULL || wp->shell == NULL)
1.79      nicm      729:                return;
                    730:
                    731:        cmd = get_proc_name(wp->fd, wp->tty);
                    732:        if (cmd == NULL || *cmd == '\0') {
                    733:                free(cmd);
                    734:                cmd = cmd_stringify_argv(wp->argc, wp->argv);
                    735:                if (cmd == NULL || *cmd == '\0') {
                    736:                        free(cmd);
                    737:                        cmd = xstrdup(wp->shell);
                    738:                }
                    739:        }
                    740:        fe->value = parse_window_name(cmd);
                    741:        free(cmd);
                    742: }
                    743:
1.80      nicm      744: /* Callback for history_bytes. */
1.108     nicm      745: static void
1.80      nicm      746: format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe)
                    747: {
                    748:        struct window_pane      *wp = ft->wp;
                    749:        struct grid             *gd;
                    750:        struct grid_line        *gl;
                    751:        unsigned long long       size;
                    752:        u_int                    i;
                    753:
                    754:        if (wp == NULL)
                    755:                return;
                    756:        gd = wp->base.grid;
                    757:
                    758:        size = 0;
                    759:        for (i = 0; i < gd->hsize; i++) {
1.158     nicm      760:                gl = grid_get_line(gd, i);
1.80      nicm      761:                size += gl->cellsize * sizeof *gl->celldata;
1.95      nicm      762:                size += gl->extdsize * sizeof *gl->extddata;
1.80      nicm      763:        }
1.158     nicm      764:        size += gd->hsize * sizeof *gl;
1.80      nicm      765:
                    766:        xasprintf(&fe->value, "%llu", size);
                    767: }
                    768:
                    769: /* Callback for pane_tabs. */
1.108     nicm      770: static void
1.80      nicm      771: format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe)
                    772: {
                    773:        struct window_pane      *wp = ft->wp;
                    774:        struct evbuffer         *buffer;
                    775:        u_int                    i;
                    776:        int                      size;
                    777:
                    778:        if (wp == NULL)
                    779:                return;
                    780:
                    781:        buffer = evbuffer_new();
1.165     nicm      782:        if (buffer == NULL)
                    783:                fatalx("out of memory");
1.80      nicm      784:        for (i = 0; i < wp->base.grid->sx; i++) {
                    785:                if (!bit_test(wp->base.tabs, i))
                    786:                        continue;
                    787:
                    788:                if (EVBUFFER_LENGTH(buffer) > 0)
                    789:                        evbuffer_add(buffer, ",", 1);
                    790:                evbuffer_add_printf(buffer, "%u", i);
                    791:        }
1.148     nicm      792:        if ((size = EVBUFFER_LENGTH(buffer)) != 0)
                    793:                xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
                    794:        evbuffer_free(buffer);
                    795: }
                    796:
1.150     nicm      797: /* Callback for session_group_list. */
1.148     nicm      798: static void
1.150     nicm      799: format_cb_session_group_list(struct format_tree *ft, struct format_entry *fe)
1.148     nicm      800: {
                    801:        struct session          *s = ft->s;
                    802:        struct session_group    *sg;
                    803:        struct session          *loop;
                    804:        struct evbuffer         *buffer;
                    805:        int                      size;
                    806:
                    807:        if (s == NULL)
                    808:                return;
                    809:        sg = session_group_contains(s);
                    810:        if (sg == NULL)
                    811:                return;
                    812:
                    813:        buffer = evbuffer_new();
1.165     nicm      814:        if (buffer == NULL)
                    815:                fatalx("out of memory");
1.221     nicm      816:
1.148     nicm      817:        TAILQ_FOREACH(loop, &sg->sessions, gentry) {
                    818:                if (EVBUFFER_LENGTH(buffer) > 0)
                    819:                        evbuffer_add(buffer, ",", 1);
                    820:                evbuffer_add_printf(buffer, "%s", loop->name);
                    821:        }
1.221     nicm      822:
                    823:        if ((size = EVBUFFER_LENGTH(buffer)) != 0)
                    824:                xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
                    825:        evbuffer_free(buffer);
                    826: }
                    827:
                    828: /* Callback for session_group_attached_list. */
                    829: static void
                    830: format_cb_session_group_attached_list(struct format_tree *ft,
                    831:     struct format_entry *fe)
                    832: {
                    833:        struct session          *s = ft->s, *client_session, *session_loop;
                    834:        struct session_group    *sg;
                    835:        struct client           *loop;
                    836:        struct evbuffer         *buffer;
                    837:        int                      size;
                    838:
                    839:        if (s == NULL)
                    840:                return;
                    841:        sg = session_group_contains(s);
                    842:        if (sg == NULL)
                    843:                return;
                    844:
                    845:        buffer = evbuffer_new();
                    846:        if (buffer == NULL)
                    847:                fatalx("out of memory");
                    848:
                    849:        TAILQ_FOREACH(loop, &clients, entry) {
                    850:                client_session = loop->session;
                    851:                if (client_session == NULL)
                    852:                        continue;
                    853:                TAILQ_FOREACH(session_loop, &sg->sessions, gentry) {
                    854:                        if (session_loop == client_session){
                    855:                                if (EVBUFFER_LENGTH(buffer) > 0)
                    856:                                        evbuffer_add(buffer, ",", 1);
                    857:                                evbuffer_add_printf(buffer, "%s", loop->name);
                    858:                        }
                    859:                }
                    860:        }
                    861:
1.148     nicm      862:        if ((size = EVBUFFER_LENGTH(buffer)) != 0)
                    863:                xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
1.80      nicm      864:        evbuffer_free(buffer);
                    865: }
                    866:
1.168     nicm      867: /* Callback for pane_in_mode. */
                    868: static void
                    869: format_cb_pane_in_mode(struct format_tree *ft, struct format_entry *fe)
                    870: {
                    871:        struct window_pane              *wp = ft->wp;
                    872:        u_int                            n = 0;
                    873:        struct window_mode_entry        *wme;
                    874:
                    875:        if (wp == NULL)
                    876:                return;
                    877:
                    878:        TAILQ_FOREACH(wme, &wp->modes, entry)
                    879:            n++;
                    880:        xasprintf(&fe->value, "%u", n);
                    881: }
                    882:
1.225     nicm      883: /* Callback for pane_at_top. */
                    884: static void
                    885: format_cb_pane_at_top(struct format_tree *ft, struct format_entry *fe)
                    886: {
                    887:        struct window_pane      *wp = ft->wp;
                    888:        struct window           *w = wp->window;
                    889:        int                      status, flag;
                    890:
                    891:        if (wp == NULL)
                    892:                return;
                    893:
                    894:        status = options_get_number(w->options, "pane-border-status");
                    895:        if (status == PANE_STATUS_TOP)
                    896:                flag = (wp->yoff == 1);
                    897:        else
                    898:                flag = (wp->yoff == 0);
                    899:        xasprintf(&fe->value, "%d", flag);
                    900: }
                    901:
                    902: /* Callback for pane_at_bottom. */
                    903: static void
                    904: format_cb_pane_at_bottom(struct format_tree *ft, struct format_entry *fe)
                    905: {
                    906:        struct window_pane      *wp = ft->wp;
                    907:        struct window           *w = wp->window;
                    908:        int                      status, flag;
                    909:
                    910:        if (wp == NULL)
                    911:                return;
                    912:
                    913:        status = options_get_number(w->options, "pane-border-status");
                    914:        if (status == PANE_STATUS_BOTTOM)
                    915:                flag = (wp->yoff + wp->sy == w->sy - 1);
                    916:        else
                    917:                flag = (wp->yoff + wp->sy == w->sy);
                    918:        xasprintf(&fe->value, "%d", flag);
                    919: }
                    920:
1.186     nicm      921: /* Callback for cursor_character. */
                    922: static void
                    923: format_cb_cursor_character(struct format_tree *ft, struct format_entry *fe)
                    924: {
                    925:        struct window_pane      *wp = ft->wp;
                    926:        struct grid_cell         gc;
                    927:
                    928:        if (wp == NULL)
                    929:                return;
                    930:
                    931:        grid_view_get_cell(wp->base.grid, wp->base.cx, wp->base.cy, &gc);
                    932:        if (~gc.flags & GRID_FLAG_PADDING)
                    933:                xasprintf(&fe->value, "%.*s", (int)gc.data.size, gc.data.data);
                    934: }
                    935:
1.213     nicm      936: /* Return word at given coordinates. Caller frees. */
                    937: char *
                    938: format_grid_word(struct grid *gd, u_int x, u_int y)
1.197     nicm      939: {
                    940:        struct grid_line        *gl;
                    941:        struct grid_cell         gc;
                    942:        const char              *ws;
                    943:        struct utf8_data        *ud = NULL;
1.213     nicm      944:        u_int                    end;
1.197     nicm      945:        size_t                   size = 0;
                    946:        int                      found = 0;
1.213     nicm      947:        char                    *s = NULL;
1.197     nicm      948:
                    949:        ws = options_get_string(global_s_options, "word-separators");
                    950:
                    951:        for (;;) {
                    952:                grid_get_cell(gd, x, y, &gc);
                    953:                if (gc.flags & GRID_FLAG_PADDING)
                    954:                        break;
                    955:                if (utf8_cstrhas(ws, &gc.data)) {
                    956:                        found = 1;
                    957:                        break;
                    958:                }
                    959:
                    960:                if (x == 0) {
                    961:                        if (y == 0)
                    962:                                break;
                    963:                        gl = &gd->linedata[y - 1];
                    964:                        if (~gl->flags & GRID_LINE_WRAPPED)
                    965:                                break;
                    966:                        y--;
                    967:                        x = grid_line_length(gd, y);
                    968:                        if (x == 0)
                    969:                                break;
                    970:                }
                    971:                x--;
                    972:        }
                    973:        for (;;) {
                    974:                if (found) {
                    975:                        end = grid_line_length(gd, y);
                    976:                        if (end == 0 || x == end - 1) {
                    977:                                if (y == gd->hsize + gd->sy - 1)
                    978:                                        break;
                    979:                                gl = &gd->linedata[y];
                    980:                                if (~gl->flags & GRID_LINE_WRAPPED)
                    981:                                        break;
                    982:                                y++;
                    983:                                x = 0;
                    984:                        } else
                    985:                                x++;
                    986:                }
                    987:                found = 1;
                    988:
                    989:                grid_get_cell(gd, x, y, &gc);
                    990:                if (gc.flags & GRID_FLAG_PADDING)
                    991:                        break;
                    992:                if (utf8_cstrhas(ws, &gc.data))
                    993:                        break;
                    994:
                    995:                ud = xreallocarray(ud, size + 2, sizeof *ud);
                    996:                memcpy(&ud[size++], &gc.data, sizeof *ud);
                    997:        }
                    998:        if (size != 0) {
                    999:                ud[size].size = 0;
1.213     nicm     1000:                s = utf8_tocstr(ud);
1.197     nicm     1001:                free(ud);
                   1002:        }
1.213     nicm     1003:        return (s);
1.197     nicm     1004: }
                   1005:
1.213     nicm     1006: /* Callback for mouse_word. */
1.197     nicm     1007: static void
1.213     nicm     1008: format_cb_mouse_word(struct format_tree *ft, struct format_entry *fe)
1.197     nicm     1009: {
                   1010:        struct window_pane      *wp;
1.228   ! nicm     1011:        struct grid             *gd;
1.197     nicm     1012:        u_int                    x, y;
1.213     nicm     1013:        char                    *s;
1.197     nicm     1014:
                   1015:        if (!ft->m.valid)
                   1016:                return;
                   1017:        wp = cmd_mouse_pane(&ft->m, NULL, NULL);
                   1018:        if (wp == NULL)
1.211     nicm     1019:                return;
1.197     nicm     1020:        if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
                   1021:                return;
1.213     nicm     1022:
1.228   ! nicm     1023:        if (!TAILQ_EMPTY(&wp->modes)) {
        !          1024:                if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode)
        !          1025:                        s = window_copy_get_word(wp, x, y);
        !          1026:                else
        !          1027:                        s = NULL;
        !          1028:        } else {
        !          1029:                gd = wp->base.grid;
        !          1030:                s = format_grid_word(gd, x, gd->hsize + y);
        !          1031:        }
1.213     nicm     1032:        if (s != NULL)
                   1033:                fe->value = s;
                   1034: }
                   1035:
                   1036: /* Return line at given coordinates. Caller frees. */
                   1037: char *
                   1038: format_grid_line(struct grid *gd, u_int y)
                   1039: {
                   1040:        struct grid_cell         gc;
                   1041:        struct utf8_data        *ud = NULL;
                   1042:        u_int                    x;
                   1043:        size_t                   size = 0;
                   1044:        char                    *s = NULL;
1.197     nicm     1045:
                   1046:        for (x = 0; x < grid_line_length(gd, y); x++) {
                   1047:                grid_get_cell(gd, x, y, &gc);
                   1048:                if (gc.flags & GRID_FLAG_PADDING)
                   1049:                        break;
                   1050:
                   1051:                ud = xreallocarray(ud, size + 2, sizeof *ud);
                   1052:                memcpy(&ud[size++], &gc.data, sizeof *ud);
                   1053:        }
                   1054:        if (size != 0) {
                   1055:                ud[size].size = 0;
1.213     nicm     1056:                s = utf8_tocstr(ud);
1.197     nicm     1057:                free(ud);
                   1058:        }
1.213     nicm     1059:        return (s);
                   1060: }
                   1061:
                   1062: /* Callback for mouse_line. */
                   1063: static void
                   1064: format_cb_mouse_line(struct format_tree *ft, struct format_entry *fe)
                   1065: {
                   1066:        struct window_pane      *wp;
1.228   ! nicm     1067:        struct grid             *gd;
1.213     nicm     1068:        u_int                    x, y;
                   1069:        char                    *s;
                   1070:
                   1071:        if (!ft->m.valid)
                   1072:                return;
                   1073:        wp = cmd_mouse_pane(&ft->m, NULL, NULL);
                   1074:        if (wp == NULL)
                   1075:                return;
                   1076:        if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
                   1077:                return;
                   1078:
1.228   ! nicm     1079:        if (!TAILQ_EMPTY(&wp->modes)) {
        !          1080:                if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode)
        !          1081:                        s = window_copy_get_line(wp, y);
        !          1082:                else
        !          1083:                        s = NULL;
        !          1084:        } else {
        !          1085:                gd = wp->base.grid;
        !          1086:                s = format_grid_line(gd, gd->hsize + y);
        !          1087:        }
1.213     nicm     1088:        if (s != NULL)
                   1089:                fe->value = s;
1.197     nicm     1090: }
                   1091:
1.112     nicm     1092: /* Merge a format tree. */
                   1093: static void
                   1094: format_merge(struct format_tree *ft, struct format_tree *from)
                   1095: {
                   1096:        struct format_entry     *fe;
                   1097:
                   1098:        RB_FOREACH(fe, format_entry_tree, &from->tree) {
                   1099:                if (fe->value != NULL)
                   1100:                        format_add(ft, fe->key, "%s", fe->value);
                   1101:        }
                   1102: }
                   1103:
1.196     nicm     1104: /* Add item bits to tree. */
                   1105: static void
                   1106: format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
                   1107: {
1.197     nicm     1108:        struct mouse_event      *m;
                   1109:        struct window_pane      *wp;
                   1110:        u_int                    x, y;
                   1111:
1.196     nicm     1112:        if (item->cmd != NULL)
                   1113:                format_add(ft, "command", "%s", item->cmd->entry->name);
1.197     nicm     1114:
                   1115:        if (item->shared == NULL)
                   1116:                return;
                   1117:        if (item->shared->formats != NULL)
1.196     nicm     1118:                format_merge(ft, item->shared->formats);
1.197     nicm     1119:
                   1120:        m = &item->shared->mouse;
                   1121:        if (m->valid && ((wp = cmd_mouse_pane(m, NULL, NULL)) != NULL)) {
                   1122:                format_add(ft, "mouse_pane", "%%%u", wp->id);
                   1123:                if (cmd_mouse_at(wp, m, &x, &y, 0) == 0) {
                   1124:                        format_add(ft, "mouse_x", "%u", x);
                   1125:                        format_add(ft, "mouse_y", "%u", y);
                   1126:                        format_add_cb(ft, "mouse_word", format_cb_mouse_word);
                   1127:                        format_add_cb(ft, "mouse_line", format_cb_mouse_line);
                   1128:                }
                   1129:        }
                   1130:        memcpy(&ft->m, m, sizeof ft->m);
1.196     nicm     1131: }
                   1132:
1.1       nicm     1133: /* Create a new tree. */
                   1134: struct format_tree *
1.131     nicm     1135: format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
1.68      nicm     1136: {
1.184     nicm     1137:        struct format_tree               *ft;
                   1138:        const struct window_mode        **wm;
                   1139:        char                              tmp[64];
1.77      nicm     1140:
                   1141:        if (!event_initialized(&format_job_event)) {
                   1142:                evtimer_set(&format_job_event, format_job_timer, NULL);
                   1143:                format_job_timer(-1, 0, NULL);
                   1144:        }
1.1       nicm     1145:
1.54      nicm     1146:        ft = xcalloc(1, sizeof *ft);
                   1147:        RB_INIT(&ft->tree);
1.120     nicm     1148:
1.131     nicm     1149:        if (c != NULL) {
                   1150:                ft->client = c;
                   1151:                ft->client->references++;
                   1152:        }
1.172     nicm     1153:        ft->item = item;
1.131     nicm     1154:
1.120     nicm     1155:        ft->tag = tag;
1.84      nicm     1156:        ft->flags = flags;
1.177     nicm     1157:        ft->time = time(NULL);
1.1       nicm     1158:
1.224     nicm     1159:        format_add(ft, "version", "%s", getversion());
1.79      nicm     1160:        format_add_cb(ft, "host", format_cb_host);
                   1161:        format_add_cb(ft, "host_short", format_cb_host_short);
                   1162:        format_add_cb(ft, "pid", format_cb_pid);
1.100     nicm     1163:        format_add(ft, "socket_path", "%s", socket_path);
                   1164:        format_add_tv(ft, "start_time", &start_time);
1.102     nicm     1165:
1.184     nicm     1166:        for (wm = all_window_modes; *wm != NULL; wm++) {
                   1167:                if ((*wm)->default_format != NULL) {
                   1168:                        xsnprintf(tmp, sizeof tmp, "%s_format", (*wm)->name);
                   1169:                        tmp[strcspn(tmp, "-")] = '_';
                   1170:                        format_add(ft, tmp, "%s", (*wm)->default_format);
                   1171:                }
                   1172:        }
                   1173:
1.196     nicm     1174:        if (item != NULL)
                   1175:                format_create_add_item(ft, item);
1.1       nicm     1176:
                   1177:        return (ft);
                   1178: }
                   1179:
                   1180: /* Free a tree. */
                   1181: void
                   1182: format_free(struct format_tree *ft)
                   1183: {
1.54      nicm     1184:        struct format_entry     *fe, *fe1;
1.1       nicm     1185:
1.68      nicm     1186:        RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
                   1187:                RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9       nicm     1188:                free(fe->value);
                   1189:                free(fe->key);
                   1190:                free(fe);
1.1       nicm     1191:        }
                   1192:
1.131     nicm     1193:        if (ft->client != NULL)
                   1194:                server_client_unref(ft->client);
1.25      nicm     1195:        free(ft);
1.1       nicm     1196: }
1.184     nicm     1197:
                   1198: /* Walk each format. */
                   1199: void
                   1200: format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
                   1201:     void *), void *arg)
                   1202: {
                   1203:        struct format_entry     *fe;
1.222     nicm     1204:        char                     s[64];
1.184     nicm     1205:
                   1206:        RB_FOREACH(fe, format_entry_tree, &ft->tree) {
                   1207:                if (fe->t != 0) {
                   1208:                        xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
1.222     nicm     1209:                        cb(fe->key, s, arg);
1.184     nicm     1210:                } else {
                   1211:                        if (fe->value == NULL && fe->cb != NULL) {
                   1212:                                fe->cb(ft, fe);
                   1213:                                if (fe->value == NULL)
                   1214:                                        fe->value = xstrdup("");
                   1215:                        }
                   1216:                        cb(fe->key, fe->value, arg);
                   1217:                }
                   1218:        }
                   1219: }
1.1       nicm     1220:
                   1221: /* Add a key-value pair. */
                   1222: void
                   1223: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
                   1224: {
                   1225:        struct format_entry     *fe;
1.28      nicm     1226:        struct format_entry     *fe_now;
1.1       nicm     1227:        va_list                  ap;
                   1228:
                   1229:        fe = xmalloc(sizeof *fe);
                   1230:        fe->key = xstrdup(key);
                   1231:
1.79      nicm     1232:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
                   1233:        if (fe_now != NULL) {
                   1234:                free(fe->key);
                   1235:                free(fe);
                   1236:                free(fe_now->value);
                   1237:                fe = fe_now;
                   1238:        }
                   1239:
                   1240:        fe->cb = NULL;
1.87      nicm     1241:        fe->t = 0;
1.79      nicm     1242:
1.1       nicm     1243:        va_start(ap, fmt);
                   1244:        xvasprintf(&fe->value, fmt, ap);
                   1245:        va_end(ap);
1.79      nicm     1246: }
                   1247:
1.87      nicm     1248: /* Add a key and time. */
1.108     nicm     1249: static void
1.87      nicm     1250: format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
                   1251: {
1.222     nicm     1252:        struct format_entry     *fe, *fe_now;
1.87      nicm     1253:
                   1254:        fe = xmalloc(sizeof *fe);
                   1255:        fe->key = xstrdup(key);
                   1256:
                   1257:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
                   1258:        if (fe_now != NULL) {
                   1259:                free(fe->key);
                   1260:                free(fe);
                   1261:                free(fe_now->value);
                   1262:                fe = fe_now;
                   1263:        }
                   1264:
                   1265:        fe->cb = NULL;
                   1266:        fe->t = tv->tv_sec;
                   1267:
                   1268:        fe->value = NULL;
                   1269: }
                   1270:
1.79      nicm     1271: /* Add a key and function. */
1.108     nicm     1272: static void
1.79      nicm     1273: format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
                   1274: {
                   1275:        struct format_entry     *fe;
                   1276:        struct format_entry     *fe_now;
                   1277:
                   1278:        fe = xmalloc(sizeof *fe);
                   1279:        fe->key = xstrdup(key);
1.1       nicm     1280:
1.68      nicm     1281:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28      nicm     1282:        if (fe_now != NULL) {
                   1283:                free(fe->key);
                   1284:                free(fe);
1.79      nicm     1285:                free(fe_now->value);
                   1286:                fe = fe_now;
1.28      nicm     1287:        }
1.79      nicm     1288:
                   1289:        fe->cb = cb;
1.87      nicm     1290:        fe->t = 0;
1.79      nicm     1291:
                   1292:        fe->value = NULL;
1.1       nicm     1293: }
                   1294:
1.161     nicm     1295: /* Quote special characters in string. */
                   1296: static char *
                   1297: format_quote(const char *s)
                   1298: {
                   1299:        const char      *cp;
                   1300:        char            *out, *at;
                   1301:
                   1302:        at = out = xmalloc(strlen(s) * 2 + 1);
                   1303:        for (cp = s; *cp != '\0'; cp++) {
                   1304:                if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
                   1305:                        *at++ = '\\';
                   1306:                *at++ = *cp;
                   1307:        }
                   1308:        *at = '\0';
                   1309:        return (out);
                   1310: }
                   1311:
1.1       nicm     1312: /* Find a format entry. */
1.108     nicm     1313: static char *
1.87      nicm     1314: format_find(struct format_tree *ft, const char *key, int modifiers)
1.1       nicm     1315: {
                   1316:        struct format_entry     *fe, fe_find;
1.76      nicm     1317:        struct environ_entry    *envent;
1.87      nicm     1318:        static char              s[64];
1.117     nicm     1319:        struct options_entry    *o;
1.116     nicm     1320:        int                      idx;
1.189     nicm     1321:        char                    *found, *saved;
1.87      nicm     1322:
                   1323:        if (~modifiers & FORMAT_TIMESTRING) {
1.116     nicm     1324:                o = options_parse_get(global_options, key, &idx, 0);
1.206     nicm     1325:                if (o == NULL && ft->wp != NULL)
                   1326:                        o = options_parse_get(ft->wp->options, key, &idx, 0);
1.87      nicm     1327:                if (o == NULL && ft->w != NULL)
1.116     nicm     1328:                        o = options_parse_get(ft->w->options, key, &idx, 0);
1.87      nicm     1329:                if (o == NULL)
1.116     nicm     1330:                        o = options_parse_get(global_w_options, key, &idx, 0);
1.87      nicm     1331:                if (o == NULL && ft->s != NULL)
1.116     nicm     1332:                        o = options_parse_get(ft->s->options, key, &idx, 0);
1.87      nicm     1333:                if (o == NULL)
1.116     nicm     1334:                        o = options_parse_get(global_s_options, key, &idx, 0);
1.87      nicm     1335:                if (o != NULL) {
1.118     nicm     1336:                        found = options_tostring(o, idx, 1);
1.116     nicm     1337:                        goto found;
1.54      nicm     1338:                }
                   1339:        }
1.116     nicm     1340:        found = NULL;
1.1       nicm     1341:
                   1342:        fe_find.key = (char *) key;
1.68      nicm     1343:        fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.79      nicm     1344:        if (fe != NULL) {
1.87      nicm     1345:                if (modifiers & FORMAT_TIMESTRING) {
                   1346:                        if (fe->t == 0)
                   1347:                                return (NULL);
                   1348:                        ctime_r(&fe->t, s);
                   1349:                        s[strcspn(s, "\n")] = '\0';
1.189     nicm     1350:                        found = xstrdup(s);
1.87      nicm     1351:                        goto found;
                   1352:                }
                   1353:                if (fe->t != 0) {
1.189     nicm     1354:                        xasprintf(&found, "%lld", (long long)fe->t);
1.87      nicm     1355:                        goto found;
                   1356:                }
1.220     nicm     1357:                if (fe->value == NULL && fe->cb != NULL)
1.79      nicm     1358:                        fe->cb(ft, fe);
1.220     nicm     1359:                if (fe->value == NULL)
                   1360:                        fe->value = xstrdup("");
1.189     nicm     1361:                found = xstrdup(fe->value);
1.87      nicm     1362:                goto found;
1.79      nicm     1363:        }
1.76      nicm     1364:
1.87      nicm     1365:        if (~modifiers & FORMAT_TIMESTRING) {
                   1366:                envent = NULL;
                   1367:                if (ft->s != NULL)
1.91      nicm     1368:                        envent = environ_find(ft->s->environ, key);
1.87      nicm     1369:                if (envent == NULL)
1.91      nicm     1370:                        envent = environ_find(global_environ, key);
1.203     nicm     1371:                if (envent != NULL && envent->value != NULL) {
1.189     nicm     1372:                        found = xstrdup(envent->value);
1.87      nicm     1373:                        goto found;
                   1374:                }
                   1375:        }
1.76      nicm     1376:
                   1377:        return (NULL);
1.87      nicm     1378:
                   1379: found:
1.88      nicm     1380:        if (found == NULL)
                   1381:                return (NULL);
1.87      nicm     1382:        if (modifiers & FORMAT_BASENAME) {
1.189     nicm     1383:                saved = found;
                   1384:                found = xstrdup(basename(saved));
1.87      nicm     1385:                free(saved);
                   1386:        }
                   1387:        if (modifiers & FORMAT_DIRNAME) {
1.189     nicm     1388:                saved = found;
                   1389:                found = xstrdup(dirname(saved));
1.87      nicm     1390:                free(saved);
                   1391:        }
1.161     nicm     1392:        if (modifiers & FORMAT_QUOTE) {
1.189     nicm     1393:                saved = found;
                   1394:                found = xstrdup(format_quote(saved));
1.161     nicm     1395:                free(saved);
                   1396:        }
1.189     nicm     1397:        return (found);
1.1       nicm     1398: }
                   1399:
1.155     nicm     1400: /* Skip until end. */
1.185     nicm     1401: const char *
1.169     nicm     1402: format_skip(const char *s, const char *end)
1.114     nicm     1403: {
                   1404:        int     brackets = 0;
                   1405:
                   1406:        for (; *s != '\0'; s++) {
1.155     nicm     1407:                if (*s == '#' && s[1] == '{')
1.114     nicm     1408:                        brackets++;
1.155     nicm     1409:                if (*s == '#' && strchr(",#{}", s[1]) != NULL) {
                   1410:                        s++;
                   1411:                        continue;
                   1412:                }
1.114     nicm     1413:                if (*s == '}')
                   1414:                        brackets--;
1.169     nicm     1415:                if (strchr(end, *s) != NULL && brackets == 0)
1.114     nicm     1416:                        break;
                   1417:        }
                   1418:        if (*s == '\0')
                   1419:                return (NULL);
                   1420:        return (s);
                   1421: }
                   1422:
                   1423: /* Return left and right alternatives separated by commas. */
                   1424: static int
1.169     nicm     1425: format_choose(struct format_tree *ft, const char *s, char **left, char **right,
                   1426:     int expand)
1.114     nicm     1427: {
1.169     nicm     1428:        const char      *cp;
                   1429:        char            *left0, *right0;
1.114     nicm     1430:
1.169     nicm     1431:        cp = format_skip(s, ",");
1.114     nicm     1432:        if (cp == NULL)
                   1433:                return (-1);
1.169     nicm     1434:        left0 = xstrndup(s, cp - s);
                   1435:        right0 = xstrdup(cp + 1);
1.114     nicm     1436:
1.169     nicm     1437:        if (expand) {
                   1438:                *left = format_expand(ft, left0);
1.188     nicm     1439:                free(left0);
1.169     nicm     1440:                *right = format_expand(ft, right0);
1.188     nicm     1441:                free(right0);
1.169     nicm     1442:        } else {
                   1443:                *left = left0;
                   1444:                *right = right0;
                   1445:        }
1.114     nicm     1446:        return (0);
                   1447: }
                   1448:
                   1449: /* Is this true? */
1.141     nicm     1450: int
1.114     nicm     1451: format_true(const char *s)
                   1452: {
                   1453:        if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
                   1454:                return (1);
                   1455:        return (0);
                   1456: }
                   1457:
1.169     nicm     1458: /* Check if modifier end. */
                   1459: static int
                   1460: format_is_end(char c)
                   1461: {
                   1462:        return (c == ';' || c == ':');
                   1463: }
                   1464:
                   1465: /* Add to modifier list. */
                   1466: static void
                   1467: format_add_modifier(struct format_modifier **list, u_int *count,
                   1468:     const char *c, size_t n, char **argv, int argc)
                   1469: {
                   1470:        struct format_modifier *fm;
                   1471:
                   1472:        *list = xreallocarray(*list, (*count) + 1, sizeof **list);
                   1473:        fm = &(*list)[(*count)++];
                   1474:
                   1475:        memcpy(fm->modifier, c, n);
                   1476:        fm->modifier[n] = '\0';
                   1477:        fm->size = n;
                   1478:
                   1479:        fm->argv = argv;
                   1480:        fm->argc = argc;
                   1481: }
                   1482:
                   1483: /* Free modifier list. */
                   1484: static void
                   1485: format_free_modifiers(struct format_modifier *list, u_int count)
                   1486: {
                   1487:        u_int   i;
                   1488:
                   1489:        for (i = 0; i < count; i++)
                   1490:                cmd_free_argv(list[i].argc, list[i].argv);
                   1491:        free(list);
                   1492: }
                   1493:
                   1494: /* Build modifier list. */
                   1495: static struct format_modifier *
                   1496: format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
                   1497: {
                   1498:        const char              *cp = *s, *end;
                   1499:        struct format_modifier  *list = NULL;
                   1500:        char                     c, last[] = "X;:", **argv, *value;
                   1501:        int                      argc;
                   1502:
                   1503:        /*
                   1504:         * Modifiers are a ; separated list of the forms:
1.195     nicm     1505:         *      l,m,C,b,d,t,q,E,T,S,W,P,<,>
1.169     nicm     1506:         *      =a
                   1507:         *      =/a
                   1508:         *      =/a/
                   1509:         *      s/a/b/
                   1510:         *      s/a/b
1.195     nicm     1511:         *      ||,&&,!=,==,<=,>=
1.169     nicm     1512:         */
                   1513:
                   1514:        *count = 0;
                   1515:
                   1516:        while (*cp != '\0' && *cp != ':') {
                   1517:                /* Skip and separator character. */
                   1518:                if (*cp == ';')
                   1519:                        cp++;
                   1520:
                   1521:                /* Check single character modifiers with no arguments. */
1.202     nicm     1522:                if (strchr("lbdtqETSWP<>", cp[0]) != NULL &&
1.172     nicm     1523:                    format_is_end(cp[1])) {
1.169     nicm     1524:                        format_add_modifier(&list, count, cp, 1, NULL, 0);
                   1525:                        cp++;
                   1526:                        continue;
                   1527:                }
                   1528:
                   1529:                /* Then try double character with no arguments. */
                   1530:                if ((memcmp("||", cp, 2) == 0 ||
                   1531:                    memcmp("&&", cp, 2) == 0 ||
                   1532:                    memcmp("!=", cp, 2) == 0 ||
1.195     nicm     1533:                    memcmp("==", cp, 2) == 0 ||
                   1534:                    memcmp("<=", cp, 2) == 0 ||
                   1535:                    memcmp(">=", cp, 2) == 0) &&
1.169     nicm     1536:                    format_is_end(cp[2])) {
                   1537:                        format_add_modifier(&list, count, cp, 2, NULL, 0);
                   1538:                        cp += 2;
                   1539:                        continue;
                   1540:                }
                   1541:
                   1542:                /* Now try single character with arguments. */
1.226     nicm     1543:                if (strchr("mCs=pe", cp[0]) == NULL)
1.169     nicm     1544:                        break;
                   1545:                c = cp[0];
                   1546:
                   1547:                /* No arguments provided. */
                   1548:                if (format_is_end(cp[1])) {
                   1549:                        format_add_modifier(&list, count, cp, 1, NULL, 0);
                   1550:                        cp++;
                   1551:                        continue;
                   1552:                }
                   1553:                argv = NULL;
                   1554:                argc = 0;
                   1555:
                   1556:                /* Single argument with no wrapper character. */
                   1557:                if (!ispunct(cp[1]) || cp[1] == '-') {
                   1558:                        end = format_skip(cp + 1, ":;");
                   1559:                        if (end == NULL)
                   1560:                                break;
                   1561:
                   1562:                        argv = xcalloc(1, sizeof *argv);
                   1563:                        value = xstrndup(cp + 1, end - (cp + 1));
                   1564:                        argv[0] = format_expand(ft, value);
                   1565:                        free(value);
                   1566:                        argc = 1;
                   1567:
                   1568:                        format_add_modifier(&list, count, &c, 1, argv, argc);
                   1569:                        cp = end;
                   1570:                        continue;
                   1571:                }
                   1572:
                   1573:                /* Multiple arguments with a wrapper character. */
                   1574:                last[0] = cp[1];
                   1575:                cp++;
                   1576:                do {
                   1577:                        if (cp[0] == last[0] && format_is_end(cp[1])) {
                   1578:                                cp++;
                   1579:                                break;
                   1580:                        }
                   1581:                        end = format_skip(cp + 1, last);
                   1582:                        if (end == NULL)
                   1583:                                break;
                   1584:                        cp++;
                   1585:
                   1586:                        argv = xreallocarray (argv, argc + 1, sizeof *argv);
                   1587:                        value = xstrndup(cp, end - cp);
                   1588:                        argv[argc++] = format_expand(ft, value);
                   1589:                        free(value);
                   1590:
                   1591:                        cp = end;
                   1592:                } while (!format_is_end(cp[0]));
                   1593:                format_add_modifier(&list, count, &c, 1, argv, argc);
                   1594:        }
                   1595:        if (*cp != ':') {
                   1596:                format_free_modifiers(list, *count);
                   1597:                *count = 0;
                   1598:                return (NULL);
                   1599:        }
                   1600:        *s = cp + 1;
                   1601:        return list;
                   1602: }
                   1603:
1.202     nicm     1604: /* Match against an fnmatch(3) pattern or regular expression. */
                   1605: static char *
                   1606: format_match(struct format_modifier *fm, const char *pattern, const char *text)
                   1607: {
                   1608:        const char      *s = "";
                   1609:        regex_t          r;
                   1610:        int              flags = 0;
                   1611:
                   1612:        if (fm->argc >= 1)
                   1613:                s = fm->argv[0];
                   1614:        if (strchr(s, 'r') == NULL) {
                   1615:                if (strchr(s, 'i') != NULL)
                   1616:                        flags |= FNM_CASEFOLD;
                   1617:                if (fnmatch(pattern, text, flags) != 0)
                   1618:                        return (xstrdup("0"));
                   1619:        } else {
                   1620:                flags = REG_EXTENDED|REG_NOSUB;
                   1621:                if (strchr(s, 'i') != NULL)
                   1622:                        flags |= REG_ICASE;
                   1623:                if (regcomp(&r, pattern, flags) != 0)
                   1624:                        return (xstrdup("0"));
                   1625:                if (regexec(&r, text, 0, NULL, 0) != 0) {
                   1626:                        regfree(&r);
                   1627:                        return (xstrdup("0"));
                   1628:                }
                   1629:                regfree(&r);
                   1630:        }
                   1631:        return (xstrdup("1"));
                   1632: }
                   1633:
1.169     nicm     1634: /* Perform substitution in string. */
                   1635: static char *
1.202     nicm     1636: format_sub(struct format_modifier *fm, const char *text, const char *pattern,
                   1637:     const char *with)
1.169     nicm     1638: {
1.202     nicm     1639:        char    *value;
                   1640:        int      flags = REG_EXTENDED;
1.169     nicm     1641:
1.202     nicm     1642:        if (fm->argc >= 3 && strchr(fm->argv[2], 'i') != NULL)
                   1643:                flags |= REG_ICASE;
                   1644:        value = regsub(pattern, with, text, flags);
                   1645:        if (value == NULL)
                   1646:                return (xstrdup(text));
                   1647:        return (value);
                   1648: }
1.169     nicm     1649:
1.202     nicm     1650: /* Search inside pane. */
                   1651: static char *
                   1652: format_search(struct format_modifier *fm, struct window_pane *wp, const char *s)
                   1653: {
                   1654:        int      ignore = 0, regex = 0;
                   1655:        char    *value;
1.169     nicm     1656:
1.202     nicm     1657:        if (fm->argc >= 1) {
                   1658:                if (strchr(fm->argv[0], 'i') != NULL)
                   1659:                        ignore = 1;
                   1660:                if (strchr(fm->argv[0], 'r') != NULL)
                   1661:                        regex = 1;
1.169     nicm     1662:        }
1.202     nicm     1663:        xasprintf(&value, "%u", window_pane_search(wp, s, regex, ignore));
                   1664:        return (value);
1.169     nicm     1665: }
                   1666:
1.172     nicm     1667: /* Loop over sessions. */
                   1668: static char *
                   1669: format_loop_sessions(struct format_tree *ft, const char *fmt)
                   1670: {
1.180     nicm     1671:        struct client           *c = ft->client;
1.172     nicm     1672:        struct cmdq_item        *item = ft->item;
1.180     nicm     1673:        struct format_tree      *nft;
1.172     nicm     1674:        char                    *expanded, *value;
                   1675:        size_t                   valuelen;
                   1676:        struct session          *s;
                   1677:
                   1678:        value = xcalloc(1, 1);
                   1679:        valuelen = 1;
                   1680:
                   1681:        RB_FOREACH(s, sessions, &sessions) {
1.179     nicm     1682:                format_log(ft, "session loop: $%u", s->id);
1.180     nicm     1683:                nft = format_create(c, item, FORMAT_NONE, ft->flags);
1.182     nicm     1684:                nft->loop = ft->loop;
1.180     nicm     1685:                format_defaults(nft, ft->c, s, NULL, NULL);
                   1686:                expanded = format_expand(nft, fmt);
                   1687:                format_free(nft);
1.172     nicm     1688:
                   1689:                valuelen += strlen(expanded);
                   1690:                value = xrealloc(value, valuelen);
                   1691:
                   1692:                strlcat(value, expanded, valuelen);
                   1693:                free(expanded);
                   1694:        }
                   1695:
                   1696:        return (value);
                   1697: }
                   1698:
                   1699: /* Loop over windows. */
                   1700: static char *
                   1701: format_loop_windows(struct format_tree *ft, const char *fmt)
                   1702: {
1.180     nicm     1703:        struct client           *c = ft->client;
1.172     nicm     1704:        struct cmdq_item        *item = ft->item;
1.180     nicm     1705:        struct format_tree      *nft;
1.172     nicm     1706:        char                    *all, *active, *use, *expanded, *value;
                   1707:        size_t                   valuelen;
                   1708:        struct winlink          *wl;
1.180     nicm     1709:        struct window           *w;
1.172     nicm     1710:
1.179     nicm     1711:        if (ft->s == NULL) {
                   1712:                format_log(ft, "window loop but no session");
1.172     nicm     1713:                return (NULL);
1.179     nicm     1714:        }
1.172     nicm     1715:
                   1716:        if (format_choose(ft, fmt, &all, &active, 0) != 0) {
                   1717:                all = xstrdup(fmt);
                   1718:                active = NULL;
                   1719:        }
                   1720:
                   1721:        value = xcalloc(1, 1);
                   1722:        valuelen = 1;
                   1723:
                   1724:        RB_FOREACH(wl, winlinks, &ft->s->windows) {
1.180     nicm     1725:                w = wl->window;
                   1726:                format_log(ft, "window loop: %u @%u", wl->idx, w->id);
1.172     nicm     1727:                if (active != NULL && wl == ft->s->curw)
                   1728:                        use = active;
                   1729:                else
                   1730:                        use = all;
1.180     nicm     1731:                nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
1.182     nicm     1732:                nft->loop = ft->loop;
1.180     nicm     1733:                format_defaults(nft, ft->c, ft->s, wl, NULL);
                   1734:                expanded = format_expand(nft, use);
                   1735:                format_free(nft);
1.172     nicm     1736:
                   1737:                valuelen += strlen(expanded);
                   1738:                value = xrealloc(value, valuelen);
                   1739:
                   1740:                strlcat(value, expanded, valuelen);
                   1741:                free(expanded);
                   1742:        }
                   1743:
                   1744:        free(active);
                   1745:        free(all);
                   1746:
                   1747:        return (value);
                   1748: }
                   1749:
                   1750: /* Loop over panes. */
                   1751: static char *
                   1752: format_loop_panes(struct format_tree *ft, const char *fmt)
                   1753: {
1.180     nicm     1754:        struct client           *c = ft->client;
1.172     nicm     1755:        struct cmdq_item        *item = ft->item;
1.180     nicm     1756:        struct format_tree      *nft;
1.172     nicm     1757:        char                    *all, *active, *use, *expanded, *value;
                   1758:        size_t                   valuelen;
                   1759:        struct window_pane      *wp;
                   1760:
1.179     nicm     1761:        if (ft->w == NULL) {
                   1762:                format_log(ft, "pane loop but no window");
1.172     nicm     1763:                return (NULL);
1.179     nicm     1764:        }
1.172     nicm     1765:
                   1766:        if (format_choose(ft, fmt, &all, &active, 0) != 0) {
                   1767:                all = xstrdup(fmt);
                   1768:                active = NULL;
                   1769:        }
                   1770:
                   1771:        value = xcalloc(1, 1);
                   1772:        valuelen = 1;
                   1773:
                   1774:        TAILQ_FOREACH(wp, &ft->w->panes, entry) {
1.179     nicm     1775:                format_log(ft, "pane loop: %%%u", wp->id);
1.172     nicm     1776:                if (active != NULL && wp == ft->w->active)
                   1777:                        use = active;
                   1778:                else
                   1779:                        use = all;
1.180     nicm     1780:                nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
1.182     nicm     1781:                nft->loop = ft->loop;
1.180     nicm     1782:                format_defaults(nft, ft->c, ft->s, ft->wl, wp);
                   1783:                expanded = format_expand(nft, use);
                   1784:                format_free(nft);
1.172     nicm     1785:
                   1786:                valuelen += strlen(expanded);
                   1787:                value = xrealloc(value, valuelen);
                   1788:
                   1789:                strlcat(value, expanded, valuelen);
                   1790:                free(expanded);
                   1791:        }
                   1792:
                   1793:        free(active);
                   1794:        free(all);
                   1795:
                   1796:        return (value);
                   1797: }
                   1798:
1.226     nicm     1799: static char *
                   1800: format_replace_expression(struct format_modifier *mexp, struct format_tree *ft,
                   1801:     const char *copy)
                   1802: {
                   1803:        int              argc = mexp->argc;
                   1804:        const char      *errstr;
                   1805:        char            *endch, *value, *left = NULL, *right = NULL;
                   1806:        int              use_fp = 0;
                   1807:        u_int            prec = 0;
                   1808:        double           mleft, mright, result;
                   1809:        enum { ADD, SUBTRACT, MULTIPLY, DIVIDE, MODULUS } operator;
                   1810:
                   1811:        if (strcmp(mexp->argv[0], "+") == 0)
                   1812:                operator = ADD;
                   1813:        else if (strcmp(mexp->argv[0], "-") == 0)
                   1814:                operator = SUBTRACT;
                   1815:        else if (strcmp(mexp->argv[0], "*") == 0)
                   1816:                operator = MULTIPLY;
                   1817:        else if (strcmp(mexp->argv[0], "/") == 0)
                   1818:                operator = DIVIDE;
                   1819:        else if (strcmp(mexp->argv[0], "%") == 0 ||
                   1820:            strcmp(mexp->argv[0], "m") == 0)
                   1821:                operator = MODULUS;
                   1822:        else {
                   1823:                format_log(ft, "expression has no valid operator: '%s'",
                   1824:                    mexp->argv[0]);
                   1825:                goto fail;
                   1826:        }
                   1827:
                   1828:        /* The second argument may be flags. */
                   1829:        if (argc >= 2 && strchr(mexp->argv[1], 'f') != NULL) {
                   1830:                use_fp = 1;
                   1831:                prec = 2;
                   1832:        }
                   1833:
                   1834:        /* The third argument may be precision. */
                   1835:        if (argc >= 3) {
                   1836:                prec = strtonum(mexp->argv[2], INT_MIN, INT_MAX, &errstr);
                   1837:                if (errstr != NULL) {
                   1838:                        format_log (ft, "expression precision %s: %s", errstr,
                   1839:                            mexp->argv[2]);
                   1840:                        goto fail;
                   1841:                }
                   1842:        }
                   1843:
                   1844:        if (format_choose(ft, copy, &left, &right, 1) != 0) {
                   1845:                format_log(ft, "expression syntax error");
                   1846:                goto fail;
                   1847:        }
                   1848:
                   1849:        mleft = strtod(left, &endch);
                   1850:        if (*endch != '\0') {
                   1851:                format_log(ft, "expression left side is invalid: %s", left);
                   1852:                goto fail;
                   1853:        }
                   1854:
                   1855:        mright = strtod(right, &endch);
                   1856:        if (*endch != '\0') {
                   1857:                format_log(ft, "expression right side is invalid: %s", right);
                   1858:                goto fail;
                   1859:        }
                   1860:
                   1861:        if (!use_fp) {
                   1862:                mleft = (long long)mleft;
                   1863:                mright = (long long)mright;
                   1864:        }
                   1865:        format_log(ft, "expression left side is: %.*f", prec, mleft);
                   1866:        format_log(ft, "expression right side is:  %.*f", prec, mright);
                   1867:
                   1868:        switch (operator) {
                   1869:        case ADD:
                   1870:                result = mleft + mright;
                   1871:                break;
                   1872:        case SUBTRACT:
                   1873:                result = mleft - mright;
                   1874:                break;
                   1875:        case MULTIPLY:
                   1876:                result = mleft * mright;
                   1877:                break;
                   1878:        case DIVIDE:
                   1879:                result = mleft / mright;
                   1880:                break;
                   1881:        case MODULUS:
                   1882:                result = fmod(mleft, mright);
                   1883:                break;
                   1884:        }
                   1885:        if (use_fp)
                   1886:                xasprintf(&value, "%.*f", prec, result);
                   1887:        else
                   1888:                xasprintf(&value, "%.*f", prec, (double)(long long)result);
                   1889:        format_log(ft, "expression result is %s", value);
                   1890:
                   1891:        free(right);
                   1892:        free(left);
                   1893:        return value;
                   1894:
                   1895: fail:
                   1896:        free(right);
                   1897:        free(left);
                   1898:        return (NULL);
                   1899: }
                   1900:
1.140     nicm     1901: /* Replace a key. */
1.108     nicm     1902: static int
1.30      nicm     1903: format_replace(struct format_tree *ft, const char *key, size_t keylen,
                   1904:     char **buf, size_t *len, size_t *off)
1.1       nicm     1905: {
1.216     nicm     1906:        struct window_pane       *wp = ft->wp;
                   1907:        const char               *errptr, *copy, *cp, *marker = NULL;
                   1908:        char                     *copy0, *condition, *found, *new;
                   1909:        char                     *value, *left, *right;
                   1910:        size_t                    valuelen;
1.217     nicm     1911:        int                       modifiers = 0, limit = 0, width = 0, j;
1.216     nicm     1912:        struct format_modifier   *list, *fm, *cmp = NULL, *search = NULL;
1.226     nicm     1913:        struct format_modifier  **sub = NULL, *mexp = NULL;
1.216     nicm     1914:        u_int                     i, count, nsub = 0;
1.1       nicm     1915:
                   1916:        /* Make a copy of the key. */
1.169     nicm     1917:        copy = copy0 = xstrndup(key, keylen);
                   1918:
                   1919:        /* Process modifier list. */
                   1920:        list = format_build_modifiers(ft, &copy, &count);
                   1921:        for (i = 0; i < count; i++) {
                   1922:                fm = &list[i];
1.179     nicm     1923:                if (format_logging(ft)) {
                   1924:                        format_log(ft, "modifier %u is %s", i, fm->modifier);
                   1925:                        for (j = 0; j < fm->argc; j++) {
                   1926:                                format_log(ft, "modifier %u argument %d: %s", i,
                   1927:                                    j, fm->argv[j]);
                   1928:                        }
                   1929:                }
1.169     nicm     1930:                if (fm->size == 1) {
                   1931:                        switch (fm->modifier[0]) {
                   1932:                        case 'm':
1.195     nicm     1933:                        case '<':
                   1934:                        case '>':
1.169     nicm     1935:                                cmp = fm;
                   1936:                                break;
                   1937:                        case 'C':
                   1938:                                search = fm;
                   1939:                                break;
                   1940:                        case 's':
1.202     nicm     1941:                                if (fm->argc < 2)
1.169     nicm     1942:                                        break;
1.216     nicm     1943:                                sub = xreallocarray (sub, nsub + 1,
                   1944:                                    sizeof *sub);
                   1945:                                sub[nsub++] = fm;
1.169     nicm     1946:                                break;
                   1947:                        case '=':
1.202     nicm     1948:                                if (fm->argc < 1)
1.169     nicm     1949:                                        break;
                   1950:                                limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
                   1951:                                    &errptr);
                   1952:                                if (errptr != NULL)
                   1953:                                        limit = 0;
1.202     nicm     1954:                                if (fm->argc >= 2 && fm->argv[1] != NULL)
1.196     nicm     1955:                                        marker = fm->argv[1];
1.169     nicm     1956:                                break;
1.217     nicm     1957:                        case 'p':
                   1958:                                if (fm->argc < 1)
                   1959:                                        break;
                   1960:                                width = strtonum(fm->argv[0], INT_MIN, INT_MAX,
                   1961:                                    &errptr);
                   1962:                                if (errptr != NULL)
                   1963:                                        width = 0;
                   1964:                                break;
1.226     nicm     1965:                        case 'e':
                   1966:                                if (fm->argc < 1 || fm->argc > 3)
                   1967:                                        break;
                   1968:                            mexp = fm;
                   1969:                                break;
1.169     nicm     1970:                        case 'l':
                   1971:                                modifiers |= FORMAT_LITERAL;
                   1972:                                break;
                   1973:                        case 'b':
                   1974:                                modifiers |= FORMAT_BASENAME;
                   1975:                                break;
                   1976:                        case 'd':
                   1977:                                modifiers |= FORMAT_DIRNAME;
                   1978:                                break;
                   1979:                        case 't':
                   1980:                                modifiers |= FORMAT_TIMESTRING;
                   1981:                                break;
                   1982:                        case 'q':
                   1983:                                modifiers |= FORMAT_QUOTE;
                   1984:                                break;
1.170     nicm     1985:                        case 'E':
                   1986:                                modifiers |= FORMAT_EXPAND;
                   1987:                                break;
1.175     nicm     1988:                        case 'T':
                   1989:                                modifiers |= FORMAT_EXPANDTIME;
                   1990:                                break;
1.172     nicm     1991:                        case 'S':
                   1992:                                modifiers |= FORMAT_SESSIONS;
                   1993:                                break;
                   1994:                        case 'W':
                   1995:                                modifiers |= FORMAT_WINDOWS;
                   1996:                                break;
                   1997:                        case 'P':
                   1998:                                modifiers |= FORMAT_PANES;
                   1999:                                break;
1.169     nicm     2000:                        }
                   2001:                } else if (fm->size == 2) {
                   2002:                        if (strcmp(fm->modifier, "||") == 0 ||
                   2003:                            strcmp(fm->modifier, "&&") == 0 ||
                   2004:                            strcmp(fm->modifier, "==") == 0 ||
1.195     nicm     2005:                            strcmp(fm->modifier, "!=") == 0 ||
                   2006:                            strcmp(fm->modifier, ">=") == 0 ||
                   2007:                            strcmp(fm->modifier, "<=") == 0)
1.169     nicm     2008:                                cmp = fm;
1.98      nicm     2009:                }
1.30      nicm     2010:        }
                   2011:
1.155     nicm     2012:        /* Is this a literal string? */
1.169     nicm     2013:        if (modifiers & FORMAT_LITERAL) {
1.155     nicm     2014:                value = xstrdup(copy);
                   2015:                goto done;
                   2016:        }
                   2017:
1.172     nicm     2018:        /* Is this a loop, comparison or condition? */
                   2019:        if (modifiers & FORMAT_SESSIONS) {
                   2020:                value = format_loop_sessions(ft, copy);
                   2021:                if (value == NULL)
                   2022:                        goto fail;
                   2023:        } else if (modifiers & FORMAT_WINDOWS) {
                   2024:                value = format_loop_windows(ft, copy);
                   2025:                if (value == NULL)
                   2026:                        goto fail;
                   2027:        } else if (modifiers & FORMAT_PANES) {
                   2028:                value = format_loop_panes(ft, copy);
                   2029:                if (value == NULL)
                   2030:                        goto fail;
                   2031:        } else if (search != NULL) {
1.140     nicm     2032:                /* Search in pane. */
1.207     nicm     2033:                new = format_expand(ft, copy);
1.179     nicm     2034:                if (wp == NULL) {
1.207     nicm     2035:                        format_log(ft, "search '%s' but no pane", new);
1.140     nicm     2036:                        value = xstrdup("0");
1.179     nicm     2037:                } else {
1.207     nicm     2038:                        format_log(ft, "search '%s' pane %%%u", new,  wp->id);
                   2039:                        value = format_search(fm, wp, new);
1.179     nicm     2040:                }
1.207     nicm     2041:                free(new);
1.169     nicm     2042:        } else if (cmp != NULL) {
                   2043:                /* Comparison of left and right. */
1.179     nicm     2044:                if (format_choose(ft, copy, &left, &right, 1) != 0) {
                   2045:                        format_log(ft, "compare %s syntax error: %s",
                   2046:                            cmp->modifier, copy);
1.114     nicm     2047:                        goto fail;
1.179     nicm     2048:                }
                   2049:                format_log(ft, "compare %s left is: %s", cmp->modifier, left);
                   2050:                format_log(ft, "compare %s right is: %s", cmp->modifier, right);
1.169     nicm     2051:
                   2052:                if (strcmp(cmp->modifier, "||") == 0) {
                   2053:                        if (format_true(left) || format_true(right))
                   2054:                                value = xstrdup("1");
                   2055:                        else
                   2056:                                value = xstrdup("0");
                   2057:                } else if (strcmp(cmp->modifier, "&&") == 0) {
                   2058:                        if (format_true(left) && format_true(right))
                   2059:                                value = xstrdup("1");
                   2060:                        else
                   2061:                                value = xstrdup("0");
                   2062:                } else if (strcmp(cmp->modifier, "==") == 0) {
                   2063:                        if (strcmp(left, right) == 0)
                   2064:                                value = xstrdup("1");
                   2065:                        else
                   2066:                                value = xstrdup("0");
                   2067:                } else if (strcmp(cmp->modifier, "!=") == 0) {
                   2068:                        if (strcmp(left, right) != 0)
                   2069:                                value = xstrdup("1");
                   2070:                        else
                   2071:                                value = xstrdup("0");
1.195     nicm     2072:                } else if (strcmp(cmp->modifier, "<") == 0) {
                   2073:                        if (strcmp(left, right) < 0)
                   2074:                                value = xstrdup("1");
                   2075:                        else
                   2076:                                value = xstrdup("0");
                   2077:                } else if (strcmp(cmp->modifier, ">") == 0) {
                   2078:                        if (strcmp(left, right) > 0)
                   2079:                                value = xstrdup("1");
                   2080:                        else
                   2081:                                value = xstrdup("0");
                   2082:                } else if (strcmp(cmp->modifier, "<=") == 0) {
                   2083:                        if (strcmp(left, right) <= 0)
                   2084:                                value = xstrdup("1");
                   2085:                        else
                   2086:                                value = xstrdup("0");
                   2087:                } else if (strcmp(cmp->modifier, ">=") == 0) {
                   2088:                        if (strcmp(left, right) >= 0)
                   2089:                                value = xstrdup("1");
                   2090:                        else
                   2091:                                value = xstrdup("0");
1.202     nicm     2092:                } else if (strcmp(cmp->modifier, "m") == 0)
1.204     nicm     2093:                        value = format_match(cmp, left, right);
1.169     nicm     2094:
1.114     nicm     2095:                free(right);
                   2096:                free(left);
                   2097:        } else if (*copy == '?') {
                   2098:                /* Conditional: check first and choose second or third. */
1.169     nicm     2099:                cp = format_skip(copy + 1, ",");
1.179     nicm     2100:                if (cp == NULL) {
                   2101:                        format_log(ft, "condition syntax error: %s", copy + 1);
1.1       nicm     2102:                        goto fail;
1.179     nicm     2103:                }
1.169     nicm     2104:                condition = xstrndup(copy + 1, cp - (copy + 1));
1.179     nicm     2105:                format_log(ft, "condition is: %s", condition);
1.1       nicm     2106:
1.169     nicm     2107:                found = format_find(ft, condition, modifiers);
1.156     nicm     2108:                if (found == NULL) {
                   2109:                        /*
1.169     nicm     2110:                         * If the condition not found, try to expand it. If
1.156     nicm     2111:                         * the expansion doesn't have any effect, then assume
                   2112:                         * false.
                   2113:                         */
1.169     nicm     2114:                        found = format_expand(ft, condition);
                   2115:                        if (strcmp(found, condition) == 0) {
1.156     nicm     2116:                                free(found);
                   2117:                                found = xstrdup("");
1.179     nicm     2118:                                format_log(ft, "condition '%s' found: %s",
                   2119:                                    condition, found);
                   2120:                        } else {
                   2121:                                format_log(ft,
                   2122:                                    "condition '%s' not found; assuming false",
                   2123:                                    condition);
1.156     nicm     2124:                        }
1.179     nicm     2125:                } else
                   2126:                        format_log(ft, "condition '%s' found", condition);
1.169     nicm     2127:
                   2128:                if (format_choose(ft, cp + 1, &left, &right, 0) != 0) {
1.179     nicm     2129:                        format_log(ft, "condition '%s' syntax error: %s",
                   2130:                            condition, cp + 1);
1.162     nicm     2131:                        free(found);
1.89      nicm     2132:                        goto fail;
1.162     nicm     2133:                }
1.179     nicm     2134:                if (format_true(found)) {
                   2135:                        format_log(ft, "condition '%s' is true", condition);
1.114     nicm     2136:                        value = format_expand(ft, left);
1.179     nicm     2137:                } else {
                   2138:                        format_log(ft, "condition '%s' is false", condition);
1.114     nicm     2139:                        value = format_expand(ft, right);
1.179     nicm     2140:                }
1.169     nicm     2141:                free(right);
                   2142:                free(left);
                   2143:
1.179     nicm     2144:                free(condition);
1.98      nicm     2145:                free(found);
1.226     nicm     2146:        } else if (mexp != NULL) {
                   2147:                value = format_replace_expression(mexp, ft, copy);
                   2148:                if (value == NULL)
                   2149:                        value = xstrdup("");
1.1       nicm     2150:        } else {
1.114     nicm     2151:                /* Neither: look up directly. */
1.98      nicm     2152:                value = format_find(ft, copy, modifiers);
1.179     nicm     2153:                if (value == NULL) {
                   2154:                        format_log(ft, "format '%s' not found", copy);
1.98      nicm     2155:                        value = xstrdup("");
1.179     nicm     2156:                } else
                   2157:                        format_log(ft, "format '%s' found: %s", copy, value);
1.170     nicm     2158:        }
                   2159:
1.171     nicm     2160: done:
1.170     nicm     2161:        /* Expand again if required. */
                   2162:        if (modifiers & FORMAT_EXPAND) {
                   2163:                new = format_expand(ft, value);
1.175     nicm     2164:                free(value);
                   2165:                value = new;
                   2166:        }
                   2167:        else if (modifiers & FORMAT_EXPANDTIME) {
1.177     nicm     2168:                new = format_expand_time(ft, value);
1.170     nicm     2169:                free(value);
                   2170:                value = new;
1.98      nicm     2171:        }
                   2172:
                   2173:        /* Perform substitution if any. */
1.216     nicm     2174:        for (i = 0; i < nsub; i++) {
                   2175:                left = format_expand(ft, sub[i]->argv[0]);
                   2176:                right = format_expand(ft, sub[i]->argv[1]);
                   2177:                new = format_sub(sub[i], value, left, right);
1.207     nicm     2178:                format_log(ft, "substitute '%s' to '%s': %s", left, right, new);
1.98      nicm     2179:                free(value);
1.169     nicm     2180:                value = new;
1.207     nicm     2181:                free(right);
                   2182:                free(left);
1.1       nicm     2183:        }
                   2184:
1.30      nicm     2185:        /* Truncate the value if needed. */
1.105     nicm     2186:        if (limit > 0) {
1.185     nicm     2187:                new = format_trim_left(value, limit);
1.196     nicm     2188:                if (marker != NULL && strcmp(new, value) != 0) {
                   2189:                        free(value);
                   2190:                        xasprintf(&value, "%s%s", new, marker);
                   2191:                } else {
                   2192:                        free(value);
                   2193:                        value = new;
                   2194:                }
                   2195:                format_log(ft, "applied length limit %d: %s", limit, value);
1.105     nicm     2196:        } else if (limit < 0) {
1.185     nicm     2197:                new = format_trim_right(value, -limit);
1.196     nicm     2198:                if (marker != NULL && strcmp(new, value) != 0) {
                   2199:                        free(value);
                   2200:                        xasprintf(&value, "%s%s", marker, new);
                   2201:                } else {
                   2202:                        free(value);
                   2203:                        value = new;
                   2204:                }
                   2205:                format_log(ft, "applied length limit %d: %s", limit, value);
1.217     nicm     2206:        }
                   2207:
                   2208:        /* Pad the value if needed. */
                   2209:        if (width > 0) {
                   2210:                new = utf8_padcstr(value, width);
                   2211:                free(value);
                   2212:                value = new;
                   2213:                format_log(ft, "applied padding width %d: %s", width, value);
                   2214:        } else if (width < 0) {
                   2215:                new = utf8_rpadcstr(value, -width);
                   2216:                free(value);
                   2217:                value = new;
                   2218:                format_log(ft, "applied padding width %d: %s", width, value);
1.44      nicm     2219:        }
1.30      nicm     2220:
1.1       nicm     2221:        /* Expand the buffer and copy in the value. */
1.98      nicm     2222:        valuelen = strlen(value);
1.1       nicm     2223:        while (*len - *off < valuelen + 1) {
1.50      nicm     2224:                *buf = xreallocarray(*buf, 2, *len);
1.1       nicm     2225:                *len *= 2;
                   2226:        }
                   2227:        memcpy(*buf + *off, value, valuelen);
                   2228:        *off += valuelen;
                   2229:
1.179     nicm     2230:        format_log(ft, "replaced '%s' with '%s'", copy0, value);
1.98      nicm     2231:        free(value);
1.179     nicm     2232:
1.216     nicm     2233:        free(sub);
1.169     nicm     2234:        format_free_modifiers(list, count);
1.30      nicm     2235:        free(copy0);
1.1       nicm     2236:        return (0);
                   2237:
                   2238: fail:
1.179     nicm     2239:        format_log(ft, "failed %s", copy0);
1.216     nicm     2240:
                   2241:        free(sub);
1.169     nicm     2242:        format_free_modifiers(list, count);
1.30      nicm     2243:        free(copy0);
1.1       nicm     2244:        return (-1);
                   2245: }
                   2246:
                   2247: /* Expand keys in a template. */
1.179     nicm     2248: static char *
                   2249: format_expand1(struct format_tree *ft, const char *fmt, int time)
1.1       nicm     2250: {
1.152     nicm     2251:        char            *buf, *out, *name;
1.179     nicm     2252:        const char      *ptr, *s;
1.86      nicm     2253:        size_t           off, len, n, outlen;
1.31      nicm     2254:        int              ch, brackets;
1.179     nicm     2255:        struct tm       *tm;
                   2256:        char             expanded[8192];
1.58      nicm     2257:
1.179     nicm     2258:        if (fmt == NULL || *fmt == '\0')
1.58      nicm     2259:                return (xstrdup(""));
1.1       nicm     2260:
1.178     nicm     2261:        if (ft->loop == FORMAT_LOOP_LIMIT)
                   2262:                return (xstrdup(""));
                   2263:        ft->loop++;
                   2264:
1.179     nicm     2265:        format_log(ft, "expanding format: %s", fmt);
                   2266:
                   2267:        if (time) {
                   2268:                tm = localtime(&ft->time);
                   2269:                if (strftime(expanded, sizeof expanded, fmt, tm) == 0) {
                   2270:                        format_log(ft, "format is too long");
                   2271:                        return (xstrdup(""));
                   2272:                }
                   2273:                if (format_logging(ft) && strcmp(expanded, fmt) != 0)
                   2274:                        format_log(ft, "after time expanded: %s", expanded);
                   2275:                fmt = expanded;
                   2276:        }
                   2277:
1.1       nicm     2278:        len = 64;
                   2279:        buf = xmalloc(len);
                   2280:        off = 0;
                   2281:
                   2282:        while (*fmt != '\0') {
                   2283:                if (*fmt != '#') {
                   2284:                        while (len - off < 2) {
1.50      nicm     2285:                                buf = xreallocarray(buf, 2, len);
1.1       nicm     2286:                                len *= 2;
                   2287:                        }
                   2288:                        buf[off++] = *fmt++;
                   2289:                        continue;
                   2290:                }
                   2291:                fmt++;
                   2292:
1.179     nicm     2293:                ch = (u_char)*fmt++;
1.1       nicm     2294:                switch (ch) {
1.68      nicm     2295:                case '(':
                   2296:                        brackets = 1;
                   2297:                        for (ptr = fmt; *ptr != '\0'; ptr++) {
                   2298:                                if (*ptr == '(')
                   2299:                                        brackets++;
                   2300:                                if (*ptr == ')' && --brackets == 0)
                   2301:                                        break;
                   2302:                        }
                   2303:                        if (*ptr != ')' || brackets != 0)
                   2304:                                break;
                   2305:                        n = ptr - fmt;
                   2306:
1.179     nicm     2307:                        name = xstrndup(fmt, n);
                   2308:                        format_log(ft, "found #(): %s", name);
                   2309:
                   2310:                        if (ft->flags & FORMAT_NOJOBS) {
1.114     nicm     2311:                                out = xstrdup("");
1.179     nicm     2312:                                format_log(ft, "#() is disabled");
                   2313:                        } else {
1.152     nicm     2314:                                out = format_job_get(ft, name);
1.179     nicm     2315:                                format_log(ft, "#() result: %s", out);
1.152     nicm     2316:                        }
1.179     nicm     2317:                        free(name);
                   2318:
1.86      nicm     2319:                        outlen = strlen(out);
                   2320:                        while (len - off < outlen + 1) {
1.68      nicm     2321:                                buf = xreallocarray(buf, 2, len);
                   2322:                                len *= 2;
                   2323:                        }
1.86      nicm     2324:                        memcpy(buf + off, out, outlen);
                   2325:                        off += outlen;
                   2326:
                   2327:                        free(out);
1.68      nicm     2328:
                   2329:                        fmt += n + 1;
                   2330:                        continue;
1.1       nicm     2331:                case '{':
1.169     nicm     2332:                        ptr = format_skip((char *)fmt - 2, "}");
1.155     nicm     2333:                        if (ptr == NULL)
1.1       nicm     2334:                                break;
                   2335:                        n = ptr - fmt;
                   2336:
1.179     nicm     2337:                        format_log(ft, "found #{}: %.*s", (int)n, fmt);
1.1       nicm     2338:                        if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
                   2339:                                break;
                   2340:                        fmt += n + 1;
1.40      nicm     2341:                        continue;
1.155     nicm     2342:                case '}':
1.40      nicm     2343:                case '#':
1.155     nicm     2344:                case ',':
1.179     nicm     2345:                        format_log(ft, "found #%c", ch);
1.40      nicm     2346:                        while (len - off < 2) {
1.50      nicm     2347:                                buf = xreallocarray(buf, 2, len);
1.40      nicm     2348:                                len *= 2;
                   2349:                        }
1.155     nicm     2350:                        buf[off++] = ch;
1.1       nicm     2351:                        continue;
                   2352:                default:
1.25      nicm     2353:                        s = NULL;
                   2354:                        if (ch >= 'A' && ch <= 'Z')
                   2355:                                s = format_upper[ch - 'A'];
                   2356:                        else if (ch >= 'a' && ch <= 'z')
                   2357:                                s = format_lower[ch - 'a'];
                   2358:                        if (s == NULL) {
                   2359:                                while (len - off < 3) {
1.50      nicm     2360:                                        buf = xreallocarray(buf, 2, len);
1.25      nicm     2361:                                        len *= 2;
1.1       nicm     2362:                                }
1.25      nicm     2363:                                buf[off++] = '#';
                   2364:                                buf[off++] = ch;
                   2365:                                continue;
1.1       nicm     2366:                        }
1.25      nicm     2367:                        n = strlen(s);
1.179     nicm     2368:                        format_log(ft, "found #%c: %s", ch, s);
1.25      nicm     2369:                        if (format_replace(ft, s, n, &buf, &len, &off) != 0)
                   2370:                                break;
1.1       nicm     2371:                        continue;
                   2372:                }
                   2373:
                   2374:                break;
                   2375:        }
                   2376:        buf[off] = '\0';
                   2377:
1.179     nicm     2378:        format_log(ft, "result is: %s", buf);
                   2379:        ft->loop--;
1.178     nicm     2380:
1.1       nicm     2381:        return (buf);
1.179     nicm     2382: }
                   2383:
                   2384: /* Expand keys in a template, passing through strftime first. */
                   2385: char *
                   2386: format_expand_time(struct format_tree *ft, const char *fmt)
                   2387: {
                   2388:        return (format_expand1(ft, fmt, 1));
                   2389: }
                   2390:
                   2391: /* Expand keys in a template. */
                   2392: char *
                   2393: format_expand(struct format_tree *ft, const char *fmt)
                   2394: {
                   2395:        return (format_expand1(ft, fmt, 0));
1.123     nicm     2396: }
                   2397:
                   2398: /* Expand a single string. */
                   2399: char *
                   2400: format_single(struct cmdq_item *item, const char *fmt, struct client *c,
                   2401:     struct session *s, struct winlink *wl, struct window_pane *wp)
                   2402: {
                   2403:        struct format_tree      *ft;
                   2404:        char                    *expanded;
                   2405:
1.131     nicm     2406:        if (item != NULL)
                   2407:                ft = format_create(item->client, item, FORMAT_NONE, 0);
                   2408:        else
                   2409:                ft = format_create(NULL, item, FORMAT_NONE, 0);
1.123     nicm     2410:        format_defaults(ft, c, s, wl, wp);
                   2411:
                   2412:        expanded = format_expand(ft, fmt);
                   2413:        format_free(ft);
                   2414:        return (expanded);
1.1       nicm     2415: }
                   2416:
1.57      nicm     2417: /* Set defaults for any of arguments that are not NULL. */
                   2418: void
                   2419: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
                   2420:     struct winlink *wl, struct window_pane *wp)
                   2421: {
1.205     nicm     2422:        if (c != NULL && c->name != NULL)
1.187     nicm     2423:                log_debug("%s: c=%s", __func__, c->name);
                   2424:        else
1.205     nicm     2425:                log_debug("%s: c=none", __func__);
1.187     nicm     2426:        if (s != NULL)
                   2427:                log_debug("%s: s=$%u", __func__, s->id);
                   2428:        else
                   2429:                log_debug("%s: s=none", __func__);
                   2430:        if (wl != NULL)
                   2431:                log_debug("%s: wl=%u w=@%u", __func__, wl->idx, wl->window->id);
                   2432:        else
                   2433:                log_debug("%s: wl=none", __func__);
                   2434:        if (wp != NULL)
                   2435:                log_debug("%s: wp=%%%u", __func__, wp->id);
                   2436:        else
                   2437:                log_debug("%s: wp=none", __func__);
                   2438:
1.154     nicm     2439:        if (c != NULL && s != NULL && c->session != s)
                   2440:                log_debug("%s: session does not match", __func__);
                   2441:
1.146     nicm     2442:        format_add(ft, "session_format", "%d", s != NULL);
                   2443:        format_add(ft, "window_format", "%d", wl != NULL);
                   2444:        format_add(ft, "pane_format", "%d", wp != NULL);
                   2445:
1.57      nicm     2446:        if (s == NULL && c != NULL)
                   2447:                s = c->session;
                   2448:        if (wl == NULL && s != NULL)
                   2449:                wl = s->curw;
                   2450:        if (wp == NULL && wl != NULL)
                   2451:                wp = wl->window->active;
                   2452:
                   2453:        if (c != NULL)
                   2454:                format_defaults_client(ft, c);
                   2455:        if (s != NULL)
                   2456:                format_defaults_session(ft, s);
1.129     nicm     2457:        if (wl != NULL)
                   2458:                format_defaults_winlink(ft, wl);
1.57      nicm     2459:        if (wp != NULL)
                   2460:                format_defaults_pane(ft, wp);
                   2461: }
                   2462:
1.1       nicm     2463: /* Set default format keys for a session. */
1.108     nicm     2464: static void
1.57      nicm     2465: format_defaults_session(struct format_tree *ft, struct session *s)
1.1       nicm     2466: {
                   2467:        struct session_group    *sg;
                   2468:
1.54      nicm     2469:        ft->s = s;
                   2470:
1.1       nicm     2471:        format_add(ft, "session_name", "%s", s->name);
                   2472:        format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
1.23      nicm     2473:        format_add(ft, "session_id", "$%u", s->id);
1.1       nicm     2474:
1.122     nicm     2475:        sg = session_group_contains(s);
1.1       nicm     2476:        format_add(ft, "session_grouped", "%d", sg != NULL);
1.148     nicm     2477:        if (sg != NULL) {
1.122     nicm     2478:                format_add(ft, "session_group", "%s", sg->name);
1.148     nicm     2479:                format_add(ft, "session_group_size", "%u",
                   2480:                    session_group_count (sg));
1.221     nicm     2481:                format_add(ft, "session_group_attached", "%u",
                   2482:                    session_group_attached_count (sg));
                   2483:                format_add(ft, "session_group_many_attached", "%u",
                   2484:                    session_group_attached_count (sg) > 1);
1.150     nicm     2485:                format_add_cb(ft, "session_group_list",
                   2486:                    format_cb_session_group_list);
1.221     nicm     2487:                format_add_cb(ft, "session_group_attached_list",
                   2488:                    format_cb_session_group_attached_list);
1.148     nicm     2489:        }
1.1       nicm     2490:
1.87      nicm     2491:        format_add_tv(ft, "session_created", &s->creation_time);
                   2492:        format_add_tv(ft, "session_last_attached", &s->last_attached_time);
                   2493:        format_add_tv(ft, "session_activity", &s->activity_time);
1.1       nicm     2494:
1.41      nicm     2495:        format_add(ft, "session_attached", "%u", s->attached);
1.59      nicm     2496:        format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.221     nicm     2497:        format_add_cb(ft, "session_attached_list",
                   2498:            format_cb_session_attached_list);
1.66      nicm     2499:
1.80      nicm     2500:        format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.133     nicm     2501:        format_add_cb(ft, "session_stack", format_cb_session_stack);
1.3       nicm     2502: }
                   2503:
                   2504: /* Set default format keys for a client. */
1.108     nicm     2505: static void
1.57      nicm     2506: format_defaults_client(struct format_tree *ft, struct client *c)
1.3       nicm     2507: {
1.60      nicm     2508:        struct session  *s;
1.103     nicm     2509:        const char      *name;
1.115     nicm     2510:        struct tty      *tty = &c->tty;
1.3       nicm     2511:
1.54      nicm     2512:        if (ft->s == NULL)
                   2513:                ft->s = c->session;
1.164     nicm     2514:        ft->c = c;
1.54      nicm     2515:
1.124     nicm     2516:        format_add(ft, "client_name", "%s", c->name);
1.72      nicm     2517:        format_add(ft, "client_pid", "%ld", (long) c->pid);
1.115     nicm     2518:        format_add(ft, "client_height", "%u", tty->sy);
                   2519:        format_add(ft, "client_width", "%u", tty->sx);
1.218     nicm     2520:        format_add(ft, "client_cell_width", "%u", tty->xpixel);
                   2521:        format_add(ft, "client_cell_height", "%u", tty->ypixel);
1.124     nicm     2522:        format_add(ft, "client_tty", "%s", c->ttyname);
1.75      nicm     2523:        format_add(ft, "client_control_mode", "%d",
                   2524:                !!(c->flags & CLIENT_CONTROL));
1.3       nicm     2525:
1.115     nicm     2526:        if (tty->term_name != NULL)
                   2527:                format_add(ft, "client_termname", "%s", tty->term_name);
                   2528:
1.87      nicm     2529:        format_add_tv(ft, "client_created", &c->creation_time);
                   2530:        format_add_tv(ft, "client_activity", &c->activity_time);
1.126     nicm     2531:
                   2532:        format_add(ft, "client_written", "%zu", c->written);
                   2533:        format_add(ft, "client_discarded", "%zu", c->discarded);
1.14      nicm     2534:
1.103     nicm     2535:        name = server_client_get_key_table(c);
                   2536:        if (strcmp(c->keytable->name, name) == 0)
1.61      nicm     2537:                format_add(ft, "client_prefix", "%d", 0);
                   2538:        else
                   2539:                format_add(ft, "client_prefix", "%d", 1);
                   2540:        format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3       nicm     2541:
1.115     nicm     2542:        if (tty->flags & TTY_UTF8)
1.3       nicm     2543:                format_add(ft, "client_utf8", "%d", 1);
                   2544:        else
                   2545:                format_add(ft, "client_utf8", "%d", 0);
                   2546:
                   2547:        if (c->flags & CLIENT_READONLY)
                   2548:                format_add(ft, "client_readonly", "%d", 1);
                   2549:        else
                   2550:                format_add(ft, "client_readonly", "%d", 0);
1.15      nicm     2551:
                   2552:        s = c->session;
                   2553:        if (s != NULL)
                   2554:                format_add(ft, "client_session", "%s", s->name);
                   2555:        s = c->last_session;
                   2556:        if (s != NULL && session_alive(s))
                   2557:                format_add(ft, "client_last_session", "%s", s->name);
1.1       nicm     2558: }
                   2559:
1.32      nicm     2560: /* Set default format keys for a window. */
                   2561: void
1.57      nicm     2562: format_defaults_window(struct format_tree *ft, struct window *w)
1.32      nicm     2563: {
1.54      nicm     2564:        ft->w = w;
                   2565:
1.87      nicm     2566:        format_add_tv(ft, "window_activity", &w->activity_time);
1.32      nicm     2567:        format_add(ft, "window_id", "@%u", w->id);
                   2568:        format_add(ft, "window_name", "%s", w->name);
                   2569:        format_add(ft, "window_width", "%u", w->sx);
                   2570:        format_add(ft, "window_height", "%u", w->sy);
1.219     nicm     2571:        format_add(ft, "window_cell_width", "%u", w->xpixel);
                   2572:        format_add(ft, "window_cell_height", "%u", w->ypixel);
1.80      nicm     2573:        format_add_cb(ft, "window_layout", format_cb_window_layout);
1.96      nicm     2574:        format_add_cb(ft, "window_visible_layout",
                   2575:            format_cb_window_visible_layout);
1.32      nicm     2576:        format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59      nicm     2577:        format_add(ft, "window_zoomed_flag", "%d",
1.53      nicm     2578:            !!(w->flags & WINDOW_ZOOMED));
1.32      nicm     2579: }
                   2580:
1.1       nicm     2581: /* Set default format keys for a winlink. */
1.108     nicm     2582: static void
1.129     nicm     2583: format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
1.1       nicm     2584: {
1.164     nicm     2585:        struct client   *c = ft->c;
1.129     nicm     2586:        struct session  *s = wl->session;
1.1       nicm     2587:        struct window   *w = wl->window;
1.164     nicm     2588:        int              flag;
                   2589:        u_int            ox, oy, sx, sy;
1.1       nicm     2590:
1.54      nicm     2591:        if (ft->w == NULL)
                   2592:                ft->w = wl->window;
1.133     nicm     2593:        ft->wl = wl;
1.54      nicm     2594:
1.57      nicm     2595:        format_defaults_window(ft, w);
1.32      nicm     2596:
1.164     nicm     2597:        if (c != NULL) {
                   2598:                flag = tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
                   2599:                format_add(ft, "window_bigger", "%d", flag);
                   2600:                if (flag) {
                   2601:                        format_add(ft, "window_offset_x", "%u", ox);
                   2602:                        format_add(ft, "window_offset_y", "%u", oy);
                   2603:                }
                   2604:        }
                   2605:
1.1       nicm     2606:        format_add(ft, "window_index", "%d", wl->idx);
1.133     nicm     2607:        format_add_cb(ft, "window_stack_index", format_cb_window_stack_index);
1.129     nicm     2608:        format_add(ft, "window_flags", "%s", window_printable_flags(wl));
1.1       nicm     2609:        format_add(ft, "window_active", "%d", wl == s->curw);
1.221     nicm     2610:        format_add_cb(ft, "window_active_sessions",
                   2611:            format_cb_window_active_sessions);
                   2612:        format_add_cb(ft, "window_active_sessions_list",
                   2613:            format_cb_window_active_sessions_list);
                   2614:        format_add_cb(ft, "window_active_clients",
                   2615:            format_cb_window_active_clients);
                   2616:        format_add_cb(ft, "window_active_clients_list",
                   2617:            format_cb_window_active_clients_list);
1.176     nicm     2618:
                   2619:        format_add(ft, "window_start_flag", "%d",
                   2620:            !!(wl == RB_MIN(winlinks, &s->windows)));
                   2621:        format_add(ft, "window_end_flag", "%d",
                   2622:            !!(wl == RB_MAX(winlinks, &s->windows)));
1.210     nicm     2623:
                   2624:        if (server_check_marked() && marked_pane.wl == wl)
                   2625:            format_add(ft, "window_marked_flag", "1");
                   2626:        else
                   2627:            format_add(ft, "window_marked_flag", "0");
1.29      nicm     2628:
1.59      nicm     2629:        format_add(ft, "window_bell_flag", "%d",
1.29      nicm     2630:            !!(wl->flags & WINLINK_BELL));
1.59      nicm     2631:        format_add(ft, "window_activity_flag", "%d",
1.29      nicm     2632:            !!(wl->flags & WINLINK_ACTIVITY));
1.59      nicm     2633:        format_add(ft, "window_silence_flag", "%d",
1.29      nicm     2634:            !!(wl->flags & WINLINK_SILENCE));
1.59      nicm     2635:        format_add(ft, "window_last_flag", "%d",
1.49      nicm     2636:            !!(wl == TAILQ_FIRST(&s->lastw)));
1.64      nicm     2637:        format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.221     nicm     2638:
                   2639:        format_add_cb(ft, "window_linked_sessions_list",
                   2640:            format_cb_window_linked_sessions_list);
                   2641:        format_add(ft, "window_linked_sessions", "%u",
                   2642:            wl->window->references);
1.1       nicm     2643: }
                   2644:
                   2645: /* Set default format keys for a window pane. */
                   2646: void
1.57      nicm     2647: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1       nicm     2648: {
1.168     nicm     2649:        struct window                   *w = wp->window;
                   2650:        struct grid                     *gd = wp->base.grid;
                   2651:        int                              status = wp->status;
                   2652:        u_int                            idx;
                   2653:        struct window_mode_entry        *wme;
1.54      nicm     2654:
                   2655:        if (ft->w == NULL)
1.159     nicm     2656:                ft->w = w;
1.79      nicm     2657:        ft->wp = wp;
1.1       nicm     2658:
1.16      nicm     2659:        format_add(ft, "history_size", "%u", gd->hsize);
                   2660:        format_add(ft, "history_limit", "%u", gd->hlimit);
1.80      nicm     2661:        format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.1       nicm     2662:
1.4       nicm     2663:        if (window_pane_index(wp, &idx) != 0)
                   2664:                fatalx("index not found");
1.16      nicm     2665:        format_add(ft, "pane_index", "%u", idx);
1.4       nicm     2666:
1.1       nicm     2667:        format_add(ft, "pane_width", "%u", wp->sx);
                   2668:        format_add(ft, "pane_height", "%u", wp->sy);
                   2669:        format_add(ft, "pane_title", "%s", wp->base.title);
1.215     nicm     2670:        if (wp->base.path != NULL)
                   2671:            format_add(ft, "pane_path", "%s", wp->base.path);
1.1       nicm     2672:        format_add(ft, "pane_id", "%%%u", wp->id);
1.159     nicm     2673:        format_add(ft, "pane_active", "%d", wp == w->active);
1.55      nicm     2674:        format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
1.143     nicm     2675:        format_add(ft, "pane_pipe", "%d", wp->pipe_fd != -1);
1.55      nicm     2676:
1.147     nicm     2677:        if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(status))
1.55      nicm     2678:                format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.190     nicm     2679:        if (~wp->flags & PANE_EMPTY)
                   2680:                format_add(ft, "pane_dead", "%d", wp->fd == -1);
                   2681:        else
                   2682:                format_add(ft, "pane_dead", "0");
1.191     nicm     2683:
                   2684:        if (server_check_marked() && marked_pane.wp == wp)
                   2685:                format_add(ft, "pane_marked", "1");
                   2686:        else
                   2687:                format_add(ft, "pane_marked", "0");
                   2688:        format_add(ft, "pane_marked_set", "%d", server_check_marked());
1.47      nicm     2689:
1.164     nicm     2690:        format_add(ft, "pane_left", "%u", wp->xoff);
                   2691:        format_add(ft, "pane_top", "%u", wp->yoff);
                   2692:        format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
                   2693:        format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
                   2694:        format_add(ft, "pane_at_left", "%d", wp->xoff == 0);
1.225     nicm     2695:        format_add_cb(ft, "pane_at_top", format_cb_pane_at_top);
1.164     nicm     2696:        format_add(ft, "pane_at_right", "%d", wp->xoff + wp->sx == w->sx);
1.225     nicm     2697:        format_add_cb(ft, "pane_at_bottom", format_cb_pane_at_bottom);
1.16      nicm     2698:
1.168     nicm     2699:        wme = TAILQ_FIRST(&wp->modes);
                   2700:        if (wme != NULL) {
                   2701:                format_add(ft, "pane_mode", "%s", wme->mode->name);
                   2702:                if (wme->mode->formats != NULL)
                   2703:                        wme->mode->formats(wme, ft);
                   2704:        }
                   2705:        format_add_cb(ft, "pane_in_mode", format_cb_pane_in_mode);
1.134     nicm     2706:
1.26      nicm     2707:        format_add(ft, "pane_synchronized", "%d",
1.159     nicm     2708:            !!options_get_number(w->options, "synchronize-panes"));
1.135     nicm     2709:        if (wp->searchstr != NULL)
                   2710:                format_add(ft, "pane_search_string", "%s", wp->searchstr);
1.16      nicm     2711:
1.71      nicm     2712:        format_add(ft, "pane_tty", "%s", wp->tty);
1.16      nicm     2713:        format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79      nicm     2714:        format_add_cb(ft, "pane_start_command", format_cb_start_command);
                   2715:        format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.16      nicm     2716:
1.59      nicm     2717:        format_add(ft, "cursor_x", "%u", wp->base.cx);
                   2718:        format_add(ft, "cursor_y", "%u", wp->base.cy);
1.186     nicm     2719:        format_add_cb(ft, "cursor_character", format_cb_cursor_character);
                   2720:
1.59      nicm     2721:        format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
                   2722:        format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.16      nicm     2723:
                   2724:        format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59      nicm     2725:        format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
                   2726:        format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16      nicm     2727:
                   2728:        format_add(ft, "cursor_flag", "%d",
                   2729:            !!(wp->base.mode & MODE_CURSOR));
                   2730:        format_add(ft, "insert_flag", "%d",
                   2731:            !!(wp->base.mode & MODE_INSERT));
                   2732:        format_add(ft, "keypad_cursor_flag", "%d",
                   2733:            !!(wp->base.mode & MODE_KCURSOR));
                   2734:        format_add(ft, "keypad_flag", "%d",
                   2735:            !!(wp->base.mode & MODE_KKEYPAD));
                   2736:        format_add(ft, "wrap_flag", "%d",
                   2737:            !!(wp->base.mode & MODE_WRAP));
1.208     nicm     2738:        format_add(ft, "origin_flag", "%d",
                   2739:            !!(wp->base.mode & MODE_ORIGIN));
1.16      nicm     2740:
1.62      nicm     2741:        format_add(ft, "mouse_any_flag", "%d",
1.119     nicm     2742:            !!(wp->base.mode & ALL_MOUSE_MODES));
1.16      nicm     2743:        format_add(ft, "mouse_standard_flag", "%d",
                   2744:            !!(wp->base.mode & MODE_MOUSE_STANDARD));
                   2745:        format_add(ft, "mouse_button_flag", "%d",
                   2746:            !!(wp->base.mode & MODE_MOUSE_BUTTON));
1.119     nicm     2747:        format_add(ft, "mouse_all_flag", "%d",
                   2748:            !!(wp->base.mode & MODE_MOUSE_ALL));
1.208     nicm     2749:        format_add(ft, "mouse_utf8_flag", "%d",
                   2750:            !!(wp->base.mode & MODE_MOUSE_UTF8));
                   2751:        format_add(ft, "mouse_sgr_flag", "%d",
                   2752:            !!(wp->base.mode & MODE_MOUSE_SGR));
1.19      nicm     2753:
1.80      nicm     2754:        format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8       nicm     2755: }
                   2756:
1.19      nicm     2757: /* Set default format keys for paste buffer. */
1.8       nicm     2758: void
1.94      nicm     2759: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
1.8       nicm     2760: {
1.146     nicm     2761:        struct timeval   tv;
                   2762:        size_t           size;
                   2763:        char            *s;
                   2764:
                   2765:        timerclear(&tv);
                   2766:        tv.tv_sec = paste_buffer_created(pb);
                   2767:        paste_buffer_data(pb, &size);
1.8       nicm     2768:
1.146     nicm     2769:        format_add(ft, "buffer_size", "%zu", size);
1.81      nicm     2770:        format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.146     nicm     2771:        format_add_tv(ft, "buffer_created", &tv);
1.8       nicm     2772:
1.94      nicm     2773:        s = paste_make_sample(pb);
1.42      nicm     2774:        format_add(ft, "buffer_sample", "%s", s);
                   2775:        free(s);
1.1       nicm     2776: }