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

1.12    ! nicm        1: /* $OpenBSD: format.c,v 1.11 2013/01/18 02:16:21 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>
                     20:
                     21: #include <netdb.h>
                     22: #include <stdarg.h>
1.9       nicm       23: #include <stdlib.h>
1.1       nicm       24: #include <string.h>
                     25: #include <time.h>
                     26: #include <unistd.h>
                     27:
                     28: #include "tmux.h"
                     29:
                     30: /*
                     31:  * Build a list of key-value pairs and use them to expand #{key} entries in a
                     32:  * string.
                     33:  */
                     34:
                     35: int    format_replace(struct format_tree *,
                     36:            const char *, size_t, char **, size_t *, size_t *);
                     37:
                     38: /* Format key-value replacement entry. */
                     39: RB_GENERATE(format_tree, format_entry, entry, format_cmp);
                     40:
                     41: /* Format tree comparison function. */
                     42: int
                     43: format_cmp(struct format_entry *fe1, struct format_entry *fe2)
                     44: {
                     45:        return (strcmp(fe1->key, fe2->key));
                     46: }
                     47:
                     48: /* Single-character aliases. */
                     49: const char *format_aliases[26] = {
                     50:        NULL,           /* A */
                     51:        NULL,           /* B */
                     52:        NULL,           /* C */
                     53:        "pane_id",      /* D */
                     54:        NULL,           /* E */
                     55:        "window_flags", /* F */
                     56:        NULL,           /* G */
                     57:        "host",         /* H */
                     58:        "window_index", /* I */
                     59:        NULL,           /* J */
                     60:        NULL,           /* K */
                     61:        NULL,           /* L */
                     62:        NULL,           /* M */
                     63:        NULL,           /* N */
                     64:        NULL,           /* O */
                     65:        "pane_index",   /* P */
                     66:        NULL,           /* Q */
                     67:        NULL,           /* R */
                     68:        "session_name", /* S */
                     69:        "pane_title",   /* T */
                     70:        NULL,           /* U */
                     71:        NULL,           /* V */
                     72:        "window_name",  /* W */
                     73:        NULL,           /* X */
                     74:        NULL,           /* Y */
                     75:        NULL            /* Z */
                     76: };
                     77:
                     78: /* Create a new tree. */
                     79: struct format_tree *
                     80: format_create(void)
                     81: {
                     82:        struct format_tree      *ft;
                     83:        char                     host[MAXHOSTNAMELEN];
                     84:
                     85:        ft = xmalloc(sizeof *ft);
                     86:        RB_INIT(ft);
                     87:
                     88:        if (gethostname(host, sizeof host) == 0)
                     89:                format_add(ft, "host", "%s", host);
                     90:
                     91:        return (ft);
                     92: }
                     93:
                     94: /* Free a tree. */
                     95: void
                     96: format_free(struct format_tree *ft)
                     97: {
                     98:        struct format_entry     *fe, *fe_next;
                     99:
                    100:        fe_next = RB_MIN(format_tree, ft);
                    101:        while (fe_next != NULL) {
                    102:                fe = fe_next;
                    103:                fe_next = RB_NEXT(format_tree, ft, fe);
                    104:
                    105:                RB_REMOVE(format_tree, ft, fe);
1.9       nicm      106:                free(fe->value);
                    107:                free(fe->key);
                    108:                free(fe);
1.1       nicm      109:        }
                    110:
1.9       nicm      111:        free (ft);
1.1       nicm      112: }
                    113:
                    114: /* Add a key-value pair. */
                    115: void
                    116: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
                    117: {
                    118:        struct format_entry     *fe;
                    119:        va_list                  ap;
                    120:
                    121:        fe = xmalloc(sizeof *fe);
                    122:        fe->key = xstrdup(key);
                    123:
                    124:        va_start(ap, fmt);
                    125:        xvasprintf(&fe->value, fmt, ap);
                    126:        va_end(ap);
                    127:
                    128:        RB_INSERT(format_tree, ft, fe);
                    129: }
                    130:
                    131: /* Find a format entry. */
                    132: const char *
                    133: format_find(struct format_tree *ft, const char *key)
                    134: {
                    135:        struct format_entry     *fe, fe_find;
                    136:
                    137:        fe_find.key = (char *) key;
                    138:        fe = RB_FIND(format_tree, ft, &fe_find);
                    139:        if (fe == NULL)
                    140:                return (NULL);
                    141:        return (fe->value);
                    142: }
                    143:
                    144: /*
                    145:  * Replace a key/value pair in buffer. #{blah} is expanded directly,
                    146:  * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
                    147:  */
                    148: int
                    149: format_replace(struct format_tree *ft,
                    150:     const char *key, size_t keylen, char **buf, size_t *len, size_t *off)
                    151: {
                    152:        char            *copy, *ptr;
                    153:        const char      *value;
                    154:        size_t           valuelen;
                    155:
                    156:        /* Make a copy of the key. */
                    157:        copy = xmalloc(keylen + 1);
                    158:        memcpy(copy, key, keylen);
                    159:        copy[keylen] = '\0';
                    160:
                    161:        /*
                    162:         * Is this a conditional? If so, check it exists and extract either the
                    163:         * first or second element. If not, look up the key directly.
                    164:         */
                    165:        if (*copy == '?') {
                    166:                ptr = strchr(copy, ',');
                    167:                if (ptr == NULL)
                    168:                        goto fail;
                    169:                *ptr = '\0';
                    170:
                    171:                value = format_find(ft, copy + 1);
                    172:                if (value != NULL && (value[0] != '0' || value[1] != '\0')) {
                    173:                        value = ptr + 1;
                    174:                        ptr = strchr(value, ',');
                    175:                        if (ptr == NULL)
                    176:                                goto fail;
                    177:                        *ptr = '\0';
                    178:                } else {
                    179:                        ptr = strchr(ptr + 1, ',');
                    180:                        if (ptr == NULL)
                    181:                                goto fail;
                    182:                        value = ptr + 1;
                    183:                }
                    184:        } else {
                    185:                value = format_find(ft, copy);
                    186:                if (value == NULL)
                    187:                        value = "";
                    188:        }
                    189:        valuelen = strlen(value);
                    190:
                    191:        /* Expand the buffer and copy in the value. */
                    192:        while (*len - *off < valuelen + 1) {
                    193:                *buf = xrealloc(*buf, 2, *len);
                    194:                *len *= 2;
                    195:        }
                    196:        memcpy(*buf + *off, value, valuelen);
                    197:        *off += valuelen;
                    198:
1.9       nicm      199:        free(copy);
1.1       nicm      200:        return (0);
                    201:
                    202: fail:
1.9       nicm      203:        free(copy);
1.1       nicm      204:        return (-1);
                    205: }
                    206:
                    207: /* Expand keys in a template. */
                    208: char *
                    209: format_expand(struct format_tree *ft, const char *fmt)
                    210: {
                    211:        char            *buf, *ptr;
                    212:        const char      *s;
                    213:        size_t           off, len, n;
                    214:        int              ch;
                    215:
                    216:        len = 64;
                    217:        buf = xmalloc(len);
                    218:        off = 0;
                    219:
                    220:        while (*fmt != '\0') {
                    221:                if (*fmt != '#') {
                    222:                        while (len - off < 2) {
                    223:                                buf = xrealloc(buf, 2, len);
                    224:                                len *= 2;
                    225:                        }
                    226:                        buf[off++] = *fmt++;
                    227:                        continue;
                    228:                }
                    229:                fmt++;
                    230:
                    231:                ch = (u_char) *fmt++;
                    232:                switch (ch) {
                    233:                case '{':
                    234:                        ptr = strchr(fmt, '}');
                    235:                        if (ptr == NULL)
                    236:                                break;
                    237:                        n = ptr - fmt;
                    238:
                    239:                        if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
                    240:                                break;
                    241:                        fmt += n + 1;
                    242:                        continue;
                    243:                default:
                    244:                        if (ch >= 'A' && ch <= 'Z') {
                    245:                                s = format_aliases[ch - 'A'];
                    246:                                if (s != NULL) {
                    247:                                        n = strlen(s);
                    248:                                        if (format_replace (
                    249:                                            ft, s, n, &buf, &len, &off) != 0)
                    250:                                                break;
                    251:                                        continue;
                    252:                                }
                    253:                        }
                    254:                        while (len - off < 2) {
                    255:                                buf = xrealloc(buf, 2, len);
                    256:                                len *= 2;
                    257:                        }
                    258:                        buf[off++] = ch;
                    259:                        continue;
                    260:                }
                    261:
                    262:                break;
                    263:        }
                    264:        buf[off] = '\0';
                    265:
                    266:        return (buf);
                    267: }
                    268:
                    269: /* Set default format keys for a session. */
                    270: void
                    271: format_session(struct format_tree *ft, struct session *s)
                    272: {
                    273:        struct session_group    *sg;
                    274:        char                    *tim;
                    275:        time_t                   t;
                    276:
                    277:        format_add(ft, "session_name", "%s", s->name);
                    278:        format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
                    279:        format_add(ft, "session_width", "%u", s->sx);
                    280:        format_add(ft, "session_height", "%u", s->sy);
                    281:
                    282:        sg = session_group_find(s);
                    283:        format_add(ft, "session_grouped", "%d", sg != NULL);
                    284:        if (sg != NULL)
                    285:                format_add(ft, "session_group", "%u", session_group_index(sg));
                    286:
                    287:        t = s->creation_time.tv_sec;
                    288:        format_add(ft, "session_created", "%ld", (long) t);
                    289:        tim = ctime(&t);
                    290:        *strchr(tim, '\n') = '\0';
                    291:        format_add(ft, "session_created_string", "%s", tim);
                    292:
                    293:        if (s->flags & SESSION_UNATTACHED)
                    294:                format_add(ft, "session_attached", "%d", 0);
                    295:        else
                    296:                format_add(ft, "session_attached", "%d", 1);
1.3       nicm      297: }
                    298:
                    299: /* Set default format keys for a client. */
                    300: void
                    301: format_client(struct format_tree *ft, struct client *c)
                    302: {
                    303:        char    *tim;
                    304:        time_t   t;
                    305:
                    306:        format_add(ft, "client_cwd", "%s", c->cwd);
1.6       nicm      307:        format_add(ft, "client_height", "%u", c->tty.sy);
                    308:        format_add(ft, "client_width", "%u", c->tty.sx);
1.3       nicm      309:        format_add(ft, "client_tty", "%s", c->tty.path);
                    310:        format_add(ft, "client_termname", "%s", c->tty.termname);
                    311:
                    312:        t = c->creation_time.tv_sec;
                    313:        format_add(ft, "client_created", "%ld", (long) t);
                    314:        tim = ctime(&t);
                    315:        *strchr(tim, '\n') = '\0';
                    316:        format_add(ft, "client_created_string", "%s", tim);
                    317:
                    318:        t = c->activity_time.tv_sec;
                    319:        format_add(ft, "client_activity", "%ld", (long) t);
                    320:        tim = ctime(&t);
                    321:        *strchr(tim, '\n') = '\0';
                    322:        format_add(ft, "client_activity_string", "%s", tim);
                    323:
                    324:        if (c->tty.flags & TTY_UTF8)
                    325:                format_add(ft, "client_utf8", "%d", 1);
                    326:        else
                    327:                format_add(ft, "client_utf8", "%d", 0);
                    328:
                    329:        if (c->flags & CLIENT_READONLY)
                    330:                format_add(ft, "client_readonly", "%d", 1);
                    331:        else
                    332:                format_add(ft, "client_readonly", "%d", 0);
1.1       nicm      333: }
                    334:
                    335: /* Set default format keys for a winlink. */
                    336: void
                    337: format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
                    338: {
                    339:        struct window   *w = wl->window;
                    340:        char            *layout, *flags;
                    341:
                    342:        layout = layout_dump(w);
                    343:        flags = window_printable_flags(s, wl);
                    344:
1.5       nicm      345:        format_add(ft, "window_id", "@%u", w->id);
1.1       nicm      346:        format_add(ft, "window_index", "%d", wl->idx);
                    347:        format_add(ft, "window_name", "%s", w->name);
                    348:        format_add(ft, "window_width", "%u", w->sx);
                    349:        format_add(ft, "window_height", "%u", w->sy);
                    350:        format_add(ft, "window_flags", "%s", flags);
                    351:        format_add(ft, "window_layout", "%s", layout);
                    352:        format_add(ft, "window_active", "%d", wl == s->curw);
1.8       nicm      353:        format_add(ft, "window_panes", "%u", window_count_panes(w));
1.1       nicm      354:
1.9       nicm      355:        free(flags);
                    356:        free(layout);
1.1       nicm      357: }
                    358:
                    359: /* Set default format keys for a window pane. */
                    360: void
                    361: format_window_pane(struct format_tree *ft, struct window_pane *wp)
                    362: {
                    363:        struct grid             *gd = wp->base.grid;
                    364:        struct grid_line        *gl;
                    365:        unsigned long long       size;
                    366:        u_int                    i;
1.4       nicm      367:        u_int                    idx;
1.12    ! nicm      368:        const char              *cwd;
1.1       nicm      369:
                    370:        size = 0;
                    371:        for (i = 0; i < gd->hsize; i++) {
                    372:                gl = &gd->linedata[i];
                    373:                size += gl->cellsize * sizeof *gl->celldata;
                    374:        }
                    375:        size += gd->hsize * sizeof *gd->linedata;
                    376:
1.4       nicm      377:        if (window_pane_index(wp, &idx) != 0)
                    378:                fatalx("index not found");
                    379:
1.1       nicm      380:        format_add(ft, "pane_width", "%u", wp->sx);
                    381:        format_add(ft, "pane_height", "%u", wp->sy);
                    382:        format_add(ft, "pane_title", "%s", wp->base.title);
1.4       nicm      383:        format_add(ft, "pane_index", "%u", idx);
1.1       nicm      384:        format_add(ft, "history_size", "%u", gd->hsize);
                    385:        format_add(ft, "history_limit", "%u", gd->hlimit);
                    386:        format_add(ft, "history_bytes", "%llu", size);
                    387:        format_add(ft, "pane_id", "%%%u", wp->id);
                    388:        format_add(ft, "pane_active", "%d", wp == wp->window->active);
                    389:        format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.2       nicm      390:        if (wp->cmd != NULL)
                    391:                format_add(ft, "pane_start_command", "%s", wp->cmd);
                    392:        if (wp->cwd != NULL)
                    393:                format_add(ft, "pane_start_path", "%s", wp->cwd);
1.12    ! nicm      394:        if ((cwd = get_proc_cwd(wp->fd)) != NULL)
        !           395:                format_add(ft, "pane_current_path", "%s", cwd);
1.2       nicm      396:        format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.12    ! nicm      397:        if (wp->tty != NULL)
        !           398:                format_add(ft, "pane_tty", "%s", wp->tty);
1.8       nicm      399: }
                    400:
                    401: void
                    402: format_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
                    403: {
                    404:        char    *pb_print = paste_print(pb, 50);
                    405:
                    406:        format_add(ft, "buffer_size", "%zu", pb->size);
                    407:        format_add(ft, "buffer_sample", "%s", pb_print);
                    408:
1.9       nicm      409:        free(pb_print);
1.1       nicm      410: }