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

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