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

1.110   ! nicm        1: /* $OpenBSD: format.c,v 1.109 2016/10/11 13:45:47 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.1       nicm      471: /* Create a new tree. */
                    472: struct format_tree *
1.102     nicm      473: format_create(struct cmd_q *cmdq, int flags)
1.68      nicm      474: {
1.1       nicm      475:        struct format_tree      *ft;
1.77      nicm      476:
                    477:        if (!event_initialized(&format_job_event)) {
                    478:                evtimer_set(&format_job_event, format_job_timer, NULL);
                    479:                format_job_timer(-1, 0, NULL);
                    480:        }
1.1       nicm      481:
1.54      nicm      482:        ft = xcalloc(1, sizeof *ft);
                    483:        RB_INIT(&ft->tree);
1.84      nicm      484:        ft->flags = flags;
1.1       nicm      485:
1.79      nicm      486:        format_add_cb(ft, "host", format_cb_host);
                    487:        format_add_cb(ft, "host_short", format_cb_host_short);
                    488:        format_add_cb(ft, "pid", format_cb_pid);
1.100     nicm      489:        format_add(ft, "socket_path", "%s", socket_path);
                    490:        format_add_tv(ft, "start_time", &start_time);
1.102     nicm      491:
                    492:        if (cmdq != NULL && cmdq->cmd != NULL)
1.110   ! nicm      493:                format_add(ft, "command", "%s", cmdq->cmd->entry->name);
        !           494:        if (cmdq != NULL && cmdq->hook != NULL)
        !           495:                format_add(ft, "hook", "%s", cmdq->hook);
1.1       nicm      496:
                    497:        return (ft);
                    498: }
                    499:
                    500: /* Free a tree. */
                    501: void
                    502: format_free(struct format_tree *ft)
                    503: {
1.54      nicm      504:        struct format_entry     *fe, *fe1;
1.1       nicm      505:
1.68      nicm      506:        RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
                    507:                RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9       nicm      508:                free(fe->value);
                    509:                free(fe->key);
                    510:                free(fe);
1.1       nicm      511:        }
                    512:
1.25      nicm      513:        free(ft);
1.1       nicm      514: }
                    515:
                    516: /* Add a key-value pair. */
                    517: void
                    518: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
                    519: {
                    520:        struct format_entry     *fe;
1.28      nicm      521:        struct format_entry     *fe_now;
1.1       nicm      522:        va_list                  ap;
                    523:
                    524:        fe = xmalloc(sizeof *fe);
                    525:        fe->key = xstrdup(key);
                    526:
1.79      nicm      527:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
                    528:        if (fe_now != NULL) {
                    529:                free(fe->key);
                    530:                free(fe);
                    531:                free(fe_now->value);
                    532:                fe = fe_now;
                    533:        }
                    534:
                    535:        fe->cb = NULL;
1.87      nicm      536:        fe->t = 0;
1.79      nicm      537:
1.1       nicm      538:        va_start(ap, fmt);
                    539:        xvasprintf(&fe->value, fmt, ap);
                    540:        va_end(ap);
1.79      nicm      541: }
                    542:
1.87      nicm      543: /* Add a key and time. */
1.108     nicm      544: static void
1.87      nicm      545: format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
                    546: {
                    547:        struct format_entry     *fe;
                    548:        struct format_entry     *fe_now;
                    549:
                    550:        fe = xmalloc(sizeof *fe);
                    551:        fe->key = xstrdup(key);
                    552:
                    553:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
                    554:        if (fe_now != NULL) {
                    555:                free(fe->key);
                    556:                free(fe);
                    557:                free(fe_now->value);
                    558:                fe = fe_now;
                    559:        }
                    560:
                    561:        fe->cb = NULL;
                    562:        fe->t = tv->tv_sec;
                    563:
                    564:        fe->value = NULL;
                    565: }
                    566:
1.79      nicm      567: /* Add a key and function. */
1.108     nicm      568: static void
1.79      nicm      569: format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
                    570: {
                    571:        struct format_entry     *fe;
                    572:        struct format_entry     *fe_now;
                    573:
                    574:        fe = xmalloc(sizeof *fe);
                    575:        fe->key = xstrdup(key);
1.1       nicm      576:
1.68      nicm      577:        fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28      nicm      578:        if (fe_now != NULL) {
                    579:                free(fe->key);
                    580:                free(fe);
1.79      nicm      581:                free(fe_now->value);
                    582:                fe = fe_now;
1.28      nicm      583:        }
1.79      nicm      584:
                    585:        fe->cb = cb;
1.87      nicm      586:        fe->t = 0;
1.79      nicm      587:
                    588:        fe->value = NULL;
1.1       nicm      589: }
                    590:
                    591: /* Find a format entry. */
1.108     nicm      592: static char *
1.87      nicm      593: format_find(struct format_tree *ft, const char *key, int modifiers)
1.1       nicm      594: {
                    595:        struct format_entry     *fe, fe_find;
1.54      nicm      596:        struct options_entry    *o;
1.76      nicm      597:        struct environ_entry    *envent;
1.87      nicm      598:        static char              s[64];
                    599:        const char              *found;
                    600:        char                    *copy, *saved;
                    601:
                    602:        found = NULL;
                    603:
                    604:        if (~modifiers & FORMAT_TIMESTRING) {
1.90      nicm      605:                o = options_find(global_options, key);
1.87      nicm      606:                if (o == NULL && ft->w != NULL)
1.90      nicm      607:                        o = options_find(ft->w->options, key);
1.87      nicm      608:                if (o == NULL)
1.90      nicm      609:                        o = options_find(global_w_options, key);
1.87      nicm      610:                if (o == NULL && ft->s != NULL)
1.90      nicm      611:                        o = options_find(ft->s->options, key);
1.87      nicm      612:                if (o == NULL)
1.90      nicm      613:                        o = options_find(global_s_options, key);
1.87      nicm      614:                if (o != NULL) {
                    615:                        switch (o->type) {
                    616:                        case OPTIONS_STRING:
                    617:                                found = o->str;
                    618:                                goto found;
                    619:                        case OPTIONS_NUMBER:
                    620:                                xsnprintf(s, sizeof s, "%lld", o->num);
                    621:                                found = s;
                    622:                                goto found;
                    623:                        case OPTIONS_STYLE:
                    624:                                found = style_tostring(&o->style);
                    625:                                goto found;
                    626:                        }
1.54      nicm      627:                }
                    628:        }
1.1       nicm      629:
                    630:        fe_find.key = (char *) key;
1.68      nicm      631:        fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.79      nicm      632:        if (fe != NULL) {
1.87      nicm      633:                if (modifiers & FORMAT_TIMESTRING) {
                    634:                        if (fe->t == 0)
                    635:                                return (NULL);
                    636:                        ctime_r(&fe->t, s);
                    637:                        s[strcspn(s, "\n")] = '\0';
                    638:                        found = s;
                    639:                        goto found;
                    640:                }
                    641:                if (fe->t != 0) {
                    642:                        xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
                    643:                        found = s;
                    644:                        goto found;
                    645:                }
1.79      nicm      646:                if (fe->value == NULL && fe->cb != NULL)
                    647:                        fe->cb(ft, fe);
1.87      nicm      648:                found = fe->value;
                    649:                goto found;
1.79      nicm      650:        }
1.76      nicm      651:
1.87      nicm      652:        if (~modifiers & FORMAT_TIMESTRING) {
                    653:                envent = NULL;
                    654:                if (ft->s != NULL)
1.91      nicm      655:                        envent = environ_find(ft->s->environ, key);
1.87      nicm      656:                if (envent == NULL)
1.91      nicm      657:                        envent = environ_find(global_environ, key);
1.87      nicm      658:                if (envent != NULL) {
                    659:                        found = envent->value;
                    660:                        goto found;
                    661:                }
                    662:        }
1.76      nicm      663:
                    664:        return (NULL);
1.87      nicm      665:
                    666: found:
1.88      nicm      667:        if (found == NULL)
                    668:                return (NULL);
1.87      nicm      669:        copy = xstrdup(found);
                    670:        if (modifiers & FORMAT_BASENAME) {
                    671:                saved = copy;
                    672:                copy = xstrdup(basename(saved));
                    673:                free(saved);
                    674:        }
                    675:        if (modifiers & FORMAT_DIRNAME) {
                    676:                saved = copy;
                    677:                copy = xstrdup(dirname(saved));
                    678:                free(saved);
                    679:        }
                    680:        return (copy);
1.1       nicm      681: }
                    682:
                    683: /*
                    684:  * Replace a key/value pair in buffer. #{blah} is expanded directly,
                    685:  * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
                    686:  */
1.108     nicm      687: static int
1.30      nicm      688: format_replace(struct format_tree *ft, const char *key, size_t keylen,
                    689:     char **buf, size_t *len, size_t *off)
1.1       nicm      690: {
1.98      nicm      691:        char            *copy, *copy0, *endptr, *ptr, *found, *new, *value;
                    692:        char            *from = NULL, *to = NULL;
                    693:        size_t           valuelen, newlen, fromlen, tolen, used;
1.105     nicm      694:        long             limit = 0;
1.89      nicm      695:        int              modifiers = 0, brackets;
1.1       nicm      696:
                    697:        /* Make a copy of the key. */
1.30      nicm      698:        copy0 = copy = xmalloc(keylen + 1);
1.1       nicm      699:        memcpy(copy, key, keylen);
                    700:        copy[keylen] = '\0';
                    701:
1.30      nicm      702:        /* Is there a length limit or whatnot? */
1.87      nicm      703:        switch (copy[0]) {
                    704:        case '=':
                    705:                errno = 0;
1.105     nicm      706:                limit = strtol(copy + 1, &endptr, 10);
                    707:                if (errno == ERANGE && (limit == LONG_MIN || limit == LONG_MAX))
1.87      nicm      708:                        break;
                    709:                if (*endptr != ':')
                    710:                        break;
                    711:                copy = endptr + 1;
                    712:                break;
                    713:        case 'b':
                    714:                if (copy[1] != ':')
                    715:                        break;
                    716:                modifiers |= FORMAT_BASENAME;
                    717:                copy += 2;
                    718:                break;
                    719:        case 'd':
                    720:                if (copy[1] != ':')
                    721:                        break;
                    722:                modifiers |= FORMAT_DIRNAME;
                    723:                copy += 2;
                    724:                break;
                    725:        case 't':
                    726:                if (copy[1] != ':')
                    727:                        break;
                    728:                modifiers |= FORMAT_TIMESTRING;
                    729:                copy += 2;
                    730:                break;
1.98      nicm      731:        case 's':
                    732:                if (copy[1] != '/')
                    733:                        break;
                    734:                from = copy + 2;
                    735:                for (copy = from; *copy != '\0' && *copy != '/'; copy++)
                    736:                        /* nothing */;
                    737:                if (copy[0] != '/' || copy == from) {
                    738:                        copy = copy0;
                    739:                        break;
                    740:                }
                    741:                copy[0] = '\0';
                    742:                to = copy + 1;
                    743:                for (copy = to; *copy != '\0' && *copy != '/'; copy++)
                    744:                        /* nothing */;
                    745:                if (copy[0] != '/' || copy[1] != ':') {
                    746:                        copy = copy0;
                    747:                        break;
                    748:                }
                    749:                copy[0] = '\0';
                    750:
                    751:                modifiers |= FORMAT_SUBSTITUTE;
                    752:                copy += 2;
                    753:                break;
1.30      nicm      754:        }
                    755:
1.1       nicm      756:        /*
                    757:         * Is this a conditional? If so, check it exists and extract either the
                    758:         * first or second element. If not, look up the key directly.
                    759:         */
                    760:        if (*copy == '?') {
                    761:                ptr = strchr(copy, ',');
                    762:                if (ptr == NULL)
                    763:                        goto fail;
                    764:                *ptr = '\0';
                    765:
1.89      nicm      766:                value = ptr + 1;
1.98      nicm      767:                found = format_find(ft, copy + 1, modifiers);
1.89      nicm      768:
                    769:                brackets = 0;
                    770:                for (ptr = ptr + 1; *ptr != '\0'; ptr++) {
                    771:                        if (*ptr == '{')
                    772:                                brackets++;
                    773:                        if (*ptr == '}')
                    774:                                brackets--;
                    775:                        if (*ptr == ',' && brackets == 0)
                    776:                                break;
                    777:                }
                    778:                if (*ptr == '\0')
                    779:                        goto fail;
                    780:
1.98      nicm      781:                if (found != NULL && *found != '\0' &&
                    782:                    (found[0] != '0' || found[1] != '\0')) {
1.1       nicm      783:                        *ptr = '\0';
1.89      nicm      784:                } else
1.1       nicm      785:                        value = ptr + 1;
1.87      nicm      786:                value = format_expand(ft, value);
1.98      nicm      787:                free(found);
1.1       nicm      788:        } else {
1.98      nicm      789:                value = format_find(ft, copy, modifiers);
1.1       nicm      790:                if (value == NULL)
1.98      nicm      791:                        value = xstrdup("");
                    792:        }
                    793:
                    794:        /* Perform substitution if any. */
                    795:        if (modifiers & FORMAT_SUBSTITUTE) {
                    796:                fromlen = strlen(from);
                    797:                tolen = strlen(to);
                    798:
                    799:                newlen = strlen(value) + 1;
                    800:                copy = new = xmalloc(newlen);
                    801:                for (ptr = value; *ptr != '\0'; /* nothing */) {
                    802:                        if (strncmp(ptr, from, fromlen) != 0) {
                    803:                                *new++ = *ptr++;
                    804:                                continue;
                    805:                        }
                    806:                        used = new - copy;
                    807:
                    808:                        newlen += tolen;
                    809:                        copy = xrealloc(copy, newlen);
                    810:
                    811:                        new = copy + used;
                    812:                        memcpy(new, to, tolen);
                    813:
                    814:                        new += tolen;
                    815:                        ptr += fromlen;
                    816:                }
                    817:                *new = '\0';
                    818:                free(value);
                    819:                value = copy;
1.1       nicm      820:        }
                    821:
1.30      nicm      822:        /* Truncate the value if needed. */
1.105     nicm      823:        if (limit > 0) {
1.98      nicm      824:                new = utf8_trimcstr(value, limit);
1.105     nicm      825:                free(value);
                    826:                value = new;
                    827:        } else if (limit < 0) {
                    828:                new = utf8_rtrimcstr(value, -limit);
1.98      nicm      829:                free(value);
                    830:                value = new;
1.44      nicm      831:        }
1.30      nicm      832:
1.1       nicm      833:        /* Expand the buffer and copy in the value. */
1.98      nicm      834:        valuelen = strlen(value);
1.1       nicm      835:        while (*len - *off < valuelen + 1) {
1.50      nicm      836:                *buf = xreallocarray(*buf, 2, *len);
1.1       nicm      837:                *len *= 2;
                    838:        }
                    839:        memcpy(*buf + *off, value, valuelen);
                    840:        *off += valuelen;
                    841:
1.98      nicm      842:        free(value);
1.30      nicm      843:        free(copy0);
1.1       nicm      844:        return (0);
                    845:
                    846: fail:
1.30      nicm      847:        free(copy0);
1.1       nicm      848:        return (-1);
                    849: }
                    850:
1.58      nicm      851: /* Expand keys in a template, passing through strftime first. */
                    852: char *
                    853: format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
                    854: {
                    855:        struct tm       *tm;
1.107     nicm      856:        char             s[2048];
1.58      nicm      857:
1.67      nicm      858:        if (fmt == NULL || *fmt == '\0')
1.58      nicm      859:                return (xstrdup(""));
                    860:
                    861:        tm = localtime(&t);
                    862:
1.107     nicm      863:        if (strftime(s, sizeof s, fmt, tm) == 0)
                    864:                return (xstrdup(""));
1.58      nicm      865:
1.107     nicm      866:        return (format_expand(ft, s));
1.58      nicm      867: }
                    868:
1.1       nicm      869: /* Expand keys in a template. */
                    870: char *
                    871: format_expand(struct format_tree *ft, const char *fmt)
                    872: {
1.86      nicm      873:        char            *buf, *tmp, *cmd, *out;
1.79      nicm      874:        const char      *ptr, *s, *saved = fmt;
1.86      nicm      875:        size_t           off, len, n, outlen;
1.31      nicm      876:        int              ch, brackets;
1.58      nicm      877:
                    878:        if (fmt == NULL)
                    879:                return (xstrdup(""));
1.1       nicm      880:
                    881:        len = 64;
                    882:        buf = xmalloc(len);
                    883:        off = 0;
                    884:
                    885:        while (*fmt != '\0') {
                    886:                if (*fmt != '#') {
                    887:                        while (len - off < 2) {
1.50      nicm      888:                                buf = xreallocarray(buf, 2, len);
1.1       nicm      889:                                len *= 2;
                    890:                        }
                    891:                        buf[off++] = *fmt++;
                    892:                        continue;
                    893:                }
                    894:                fmt++;
                    895:
                    896:                ch = (u_char) *fmt++;
                    897:                switch (ch) {
1.68      nicm      898:                case '(':
                    899:                        brackets = 1;
                    900:                        for (ptr = fmt; *ptr != '\0'; ptr++) {
                    901:                                if (*ptr == '(')
                    902:                                        brackets++;
                    903:                                if (*ptr == ')' && --brackets == 0)
                    904:                                        break;
                    905:                        }
                    906:                        if (*ptr != ')' || brackets != 0)
                    907:                                break;
                    908:                        n = ptr - fmt;
                    909:
                    910:                        tmp = xmalloc(n + 1);
                    911:                        memcpy(tmp, fmt, n);
                    912:                        tmp[n] = '\0';
1.69      nicm      913:                        cmd = format_expand(ft, tmp);
1.68      nicm      914:
1.86      nicm      915:                        out = format_job_get(ft, cmd);
                    916:                        outlen = strlen(out);
1.69      nicm      917:
                    918:                        free(cmd);
                    919:                        free(tmp);
1.68      nicm      920:
1.86      nicm      921:                        while (len - off < outlen + 1) {
1.68      nicm      922:                                buf = xreallocarray(buf, 2, len);
                    923:                                len *= 2;
                    924:                        }
1.86      nicm      925:                        memcpy(buf + off, out, outlen);
                    926:                        off += outlen;
                    927:
                    928:                        free(out);
1.68      nicm      929:
                    930:                        fmt += n + 1;
                    931:                        continue;
1.1       nicm      932:                case '{':
1.31      nicm      933:                        brackets = 1;
                    934:                        for (ptr = fmt; *ptr != '\0'; ptr++) {
                    935:                                if (*ptr == '{')
                    936:                                        brackets++;
                    937:                                if (*ptr == '}' && --brackets == 0)
                    938:                                        break;
                    939:                        }
                    940:                        if (*ptr != '}' || brackets != 0)
1.1       nicm      941:                                break;
                    942:                        n = ptr - fmt;
                    943:
                    944:                        if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
                    945:                                break;
                    946:                        fmt += n + 1;
1.40      nicm      947:                        continue;
                    948:                case '#':
                    949:                        while (len - off < 2) {
1.50      nicm      950:                                buf = xreallocarray(buf, 2, len);
1.40      nicm      951:                                len *= 2;
                    952:                        }
                    953:                        buf[off++] = '#';
1.1       nicm      954:                        continue;
                    955:                default:
1.25      nicm      956:                        s = NULL;
                    957:                        if (ch >= 'A' && ch <= 'Z')
                    958:                                s = format_upper[ch - 'A'];
                    959:                        else if (ch >= 'a' && ch <= 'z')
                    960:                                s = format_lower[ch - 'a'];
                    961:                        if (s == NULL) {
                    962:                                while (len - off < 3) {
1.50      nicm      963:                                        buf = xreallocarray(buf, 2, len);
1.25      nicm      964:                                        len *= 2;
1.1       nicm      965:                                }
1.25      nicm      966:                                buf[off++] = '#';
                    967:                                buf[off++] = ch;
                    968:                                continue;
1.1       nicm      969:                        }
1.25      nicm      970:                        n = strlen(s);
                    971:                        if (format_replace(ft, s, n, &buf, &len, &off) != 0)
                    972:                                break;
1.1       nicm      973:                        continue;
                    974:                }
                    975:
                    976:                break;
                    977:        }
                    978:        buf[off] = '\0';
                    979:
1.79      nicm      980:        log_debug("format '%s' -> '%s'", saved, buf);
1.1       nicm      981:        return (buf);
                    982: }
                    983:
1.57      nicm      984: /* Set defaults for any of arguments that are not NULL. */
                    985: void
                    986: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
                    987:     struct winlink *wl, struct window_pane *wp)
                    988: {
                    989:        if (s == NULL && c != NULL)
                    990:                s = c->session;
                    991:        if (wl == NULL && s != NULL)
                    992:                wl = s->curw;
                    993:        if (wp == NULL && wl != NULL)
                    994:                wp = wl->window->active;
                    995:
                    996:        if (c != NULL)
                    997:                format_defaults_client(ft, c);
                    998:        if (s != NULL)
                    999:                format_defaults_session(ft, s);
                   1000:        if (s != NULL && wl != NULL)
                   1001:                format_defaults_winlink(ft, s, wl);
                   1002:        if (wp != NULL)
                   1003:                format_defaults_pane(ft, wp);
                   1004: }
                   1005:
1.1       nicm     1006: /* Set default format keys for a session. */
1.108     nicm     1007: static void
1.57      nicm     1008: format_defaults_session(struct format_tree *ft, struct session *s)
1.1       nicm     1009: {
                   1010:        struct session_group    *sg;
                   1011:
1.54      nicm     1012:        ft->s = s;
                   1013:
1.1       nicm     1014:        format_add(ft, "session_name", "%s", s->name);
                   1015:        format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
                   1016:        format_add(ft, "session_width", "%u", s->sx);
                   1017:        format_add(ft, "session_height", "%u", s->sy);
1.23      nicm     1018:        format_add(ft, "session_id", "$%u", s->id);
1.1       nicm     1019:
                   1020:        sg = session_group_find(s);
                   1021:        format_add(ft, "session_grouped", "%d", sg != NULL);
                   1022:        if (sg != NULL)
                   1023:                format_add(ft, "session_group", "%u", session_group_index(sg));
                   1024:
1.87      nicm     1025:        format_add_tv(ft, "session_created", &s->creation_time);
                   1026:        format_add_tv(ft, "session_last_attached", &s->last_attached_time);
                   1027:        format_add_tv(ft, "session_activity", &s->activity_time);
1.1       nicm     1028:
1.41      nicm     1029:        format_add(ft, "session_attached", "%u", s->attached);
1.59      nicm     1030:        format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66      nicm     1031:
1.80      nicm     1032:        format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.3       nicm     1033: }
                   1034:
                   1035: /* Set default format keys for a client. */
1.108     nicm     1036: static void
1.57      nicm     1037: format_defaults_client(struct format_tree *ft, struct client *c)
1.3       nicm     1038: {
1.60      nicm     1039:        struct session  *s;
1.103     nicm     1040:        const char      *name;
1.3       nicm     1041:
1.54      nicm     1042:        if (ft->s == NULL)
                   1043:                ft->s = c->session;
                   1044:
1.72      nicm     1045:        format_add(ft, "client_pid", "%ld", (long) c->pid);
1.6       nicm     1046:        format_add(ft, "client_height", "%u", c->tty.sy);
                   1047:        format_add(ft, "client_width", "%u", c->tty.sx);
1.27      nicm     1048:        if (c->tty.path != NULL)
                   1049:                format_add(ft, "client_tty", "%s", c->tty.path);
                   1050:        if (c->tty.termname != NULL)
                   1051:                format_add(ft, "client_termname", "%s", c->tty.termname);
1.75      nicm     1052:        format_add(ft, "client_control_mode", "%d",
                   1053:                !!(c->flags & CLIENT_CONTROL));
1.3       nicm     1054:
1.87      nicm     1055:        format_add_tv(ft, "client_created", &c->creation_time);
                   1056:        format_add_tv(ft, "client_activity", &c->activity_time);
1.14      nicm     1057:
1.103     nicm     1058:        name = server_client_get_key_table(c);
                   1059:        if (strcmp(c->keytable->name, name) == 0)
1.61      nicm     1060:                format_add(ft, "client_prefix", "%d", 0);
                   1061:        else
                   1062:                format_add(ft, "client_prefix", "%d", 1);
                   1063:        format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3       nicm     1064:
                   1065:        if (c->tty.flags & TTY_UTF8)
                   1066:                format_add(ft, "client_utf8", "%d", 1);
                   1067:        else
                   1068:                format_add(ft, "client_utf8", "%d", 0);
                   1069:
                   1070:        if (c->flags & CLIENT_READONLY)
                   1071:                format_add(ft, "client_readonly", "%d", 1);
                   1072:        else
                   1073:                format_add(ft, "client_readonly", "%d", 0);
1.15      nicm     1074:
                   1075:        s = c->session;
                   1076:        if (s != NULL)
                   1077:                format_add(ft, "client_session", "%s", s->name);
                   1078:        s = c->last_session;
                   1079:        if (s != NULL && session_alive(s))
                   1080:                format_add(ft, "client_last_session", "%s", s->name);
1.1       nicm     1081: }
                   1082:
1.32      nicm     1083: /* Set default format keys for a window. */
                   1084: void
1.57      nicm     1085: format_defaults_window(struct format_tree *ft, struct window *w)
1.32      nicm     1086: {
1.54      nicm     1087:        ft->w = w;
                   1088:
1.87      nicm     1089:        format_add_tv(ft, "window_activity", &w->activity_time);
1.32      nicm     1090:        format_add(ft, "window_id", "@%u", w->id);
                   1091:        format_add(ft, "window_name", "%s", w->name);
                   1092:        format_add(ft, "window_width", "%u", w->sx);
                   1093:        format_add(ft, "window_height", "%u", w->sy);
1.80      nicm     1094:        format_add_cb(ft, "window_layout", format_cb_window_layout);
1.96      nicm     1095:        format_add_cb(ft, "window_visible_layout",
                   1096:            format_cb_window_visible_layout);
1.32      nicm     1097:        format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59      nicm     1098:        format_add(ft, "window_zoomed_flag", "%d",
1.53      nicm     1099:            !!(w->flags & WINDOW_ZOOMED));
1.32      nicm     1100: }
                   1101:
1.1       nicm     1102: /* Set default format keys for a winlink. */
1.108     nicm     1103: static void
1.57      nicm     1104: format_defaults_winlink(struct format_tree *ft, struct session *s,
                   1105:     struct winlink *wl)
1.1       nicm     1106: {
                   1107:        struct window   *w = wl->window;
1.32      nicm     1108:        char            *flags;
1.1       nicm     1109:
1.54      nicm     1110:        if (ft->w == NULL)
                   1111:                ft->w = wl->window;
                   1112:
1.1       nicm     1113:        flags = window_printable_flags(s, wl);
                   1114:
1.57      nicm     1115:        format_defaults_window(ft, w);
1.32      nicm     1116:
1.1       nicm     1117:        format_add(ft, "window_index", "%d", wl->idx);
                   1118:        format_add(ft, "window_flags", "%s", flags);
                   1119:        format_add(ft, "window_active", "%d", wl == s->curw);
1.29      nicm     1120:
1.59      nicm     1121:        format_add(ft, "window_bell_flag", "%d",
1.29      nicm     1122:            !!(wl->flags & WINLINK_BELL));
1.59      nicm     1123:        format_add(ft, "window_activity_flag", "%d",
1.29      nicm     1124:            !!(wl->flags & WINLINK_ACTIVITY));
1.59      nicm     1125:        format_add(ft, "window_silence_flag", "%d",
1.29      nicm     1126:            !!(wl->flags & WINLINK_SILENCE));
1.59      nicm     1127:        format_add(ft, "window_last_flag", "%d",
1.49      nicm     1128:            !!(wl == TAILQ_FIRST(&s->lastw)));
1.64      nicm     1129:        format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.32      nicm     1130:
1.9       nicm     1131:        free(flags);
1.1       nicm     1132: }
                   1133:
                   1134: /* Set default format keys for a window pane. */
                   1135: void
1.57      nicm     1136: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1       nicm     1137: {
1.80      nicm     1138:        struct grid     *gd = wp->base.grid;
                   1139:        u_int            idx;
1.85      nicm     1140:        int              status, scroll_position;
1.54      nicm     1141:
                   1142:        if (ft->w == NULL)
                   1143:                ft->w = wp->window;
1.79      nicm     1144:        ft->wp = wp;
1.1       nicm     1145:
1.16      nicm     1146:        format_add(ft, "history_size", "%u", gd->hsize);
                   1147:        format_add(ft, "history_limit", "%u", gd->hlimit);
1.80      nicm     1148:        format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.1       nicm     1149:
1.4       nicm     1150:        if (window_pane_index(wp, &idx) != 0)
                   1151:                fatalx("index not found");
1.16      nicm     1152:        format_add(ft, "pane_index", "%u", idx);
1.4       nicm     1153:
1.1       nicm     1154:        format_add(ft, "pane_width", "%u", wp->sx);
                   1155:        format_add(ft, "pane_height", "%u", wp->sy);
                   1156:        format_add(ft, "pane_title", "%s", wp->base.title);
                   1157:        format_add(ft, "pane_id", "%%%u", wp->id);
                   1158:        format_add(ft, "pane_active", "%d", wp == wp->window->active);
1.55      nicm     1159:        format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
                   1160:
                   1161:        status = wp->status;
                   1162:        if (wp->fd == -1 && WIFEXITED(status))
                   1163:                format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1       nicm     1164:        format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47      nicm     1165:
                   1166:        if (window_pane_visible(wp)) {
                   1167:                format_add(ft, "pane_left", "%u", wp->xoff);
                   1168:                format_add(ft, "pane_top", "%u", wp->yoff);
                   1169:                format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
                   1170:                format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
                   1171:        }
1.16      nicm     1172:
                   1173:        format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
1.26      nicm     1174:        format_add(ft, "pane_synchronized", "%d",
1.90      nicm     1175:            !!options_get_number(wp->window->options, "synchronize-panes"));
1.16      nicm     1176:
1.71      nicm     1177:        format_add(ft, "pane_tty", "%s", wp->tty);
1.16      nicm     1178:        format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79      nicm     1179:        format_add_cb(ft, "pane_start_command", format_cb_start_command);
                   1180:        format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.16      nicm     1181:
1.59      nicm     1182:        format_add(ft, "cursor_x", "%u", wp->base.cx);
                   1183:        format_add(ft, "cursor_y", "%u", wp->base.cy);
                   1184:        format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
                   1185:        format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.85      nicm     1186:
                   1187:        scroll_position = window_copy_scroll_position(wp);
                   1188:        if (scroll_position != -1)
                   1189:                format_add(ft, "scroll_position", "%d", scroll_position);
1.16      nicm     1190:
                   1191:        format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59      nicm     1192:        format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
                   1193:        format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16      nicm     1194:
                   1195:        format_add(ft, "cursor_flag", "%d",
                   1196:            !!(wp->base.mode & MODE_CURSOR));
                   1197:        format_add(ft, "insert_flag", "%d",
                   1198:            !!(wp->base.mode & MODE_INSERT));
                   1199:        format_add(ft, "keypad_cursor_flag", "%d",
                   1200:            !!(wp->base.mode & MODE_KCURSOR));
                   1201:        format_add(ft, "keypad_flag", "%d",
                   1202:            !!(wp->base.mode & MODE_KKEYPAD));
                   1203:        format_add(ft, "wrap_flag", "%d",
                   1204:            !!(wp->base.mode & MODE_WRAP));
                   1205:
1.62      nicm     1206:        format_add(ft, "mouse_any_flag", "%d",
                   1207:            !!(wp->base.mode & (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON)));
1.16      nicm     1208:        format_add(ft, "mouse_standard_flag", "%d",
                   1209:            !!(wp->base.mode & MODE_MOUSE_STANDARD));
                   1210:        format_add(ft, "mouse_button_flag", "%d",
                   1211:            !!(wp->base.mode & MODE_MOUSE_BUTTON));
1.19      nicm     1212:
1.80      nicm     1213:        format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8       nicm     1214: }
                   1215:
1.19      nicm     1216: /* Set default format keys for paste buffer. */
1.8       nicm     1217: void
1.94      nicm     1218: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
1.8       nicm     1219: {
1.81      nicm     1220:        size_t   bufsize;
1.42      nicm     1221:        char    *s;
1.8       nicm     1222:
1.81      nicm     1223:        paste_buffer_data(pb, &bufsize);
                   1224:        format_add(ft, "buffer_size", "%zu", bufsize);
                   1225:        format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.8       nicm     1226:
1.94      nicm     1227:        s = paste_make_sample(pb);
1.42      nicm     1228:        format_add(ft, "buffer_sample", "%s", s);
                   1229:        free(s);
1.1       nicm     1230: }