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

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