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

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