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

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