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

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