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

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