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

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