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

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