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

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