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

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