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

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