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

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