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

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