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

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