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

1.23    ! nicm        1: /* $OpenBSD: format.c,v 1.22 2013/03/25 10:11:45 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:
1.19      nicm       35: int    format_replace(struct format_tree *, const char *, size_t, char **,
                     36:            size_t *, size_t *);
                     37: void   format_window_pane_tabs(struct format_tree *, struct window_pane *);
1.1       nicm       38:
                     39: /* Format key-value replacement entry. */
                     40: RB_GENERATE(format_tree, format_entry, entry, format_cmp);
                     41:
                     42: /* Format tree comparison function. */
                     43: int
                     44: format_cmp(struct format_entry *fe1, struct format_entry *fe2)
                     45: {
                     46:        return (strcmp(fe1->key, fe2->key));
                     47: }
                     48:
                     49: /* Single-character aliases. */
                     50: const char *format_aliases[26] = {
                     51:        NULL,           /* A */
                     52:        NULL,           /* B */
                     53:        NULL,           /* C */
                     54:        "pane_id",      /* D */
                     55:        NULL,           /* E */
                     56:        "window_flags", /* F */
                     57:        NULL,           /* G */
                     58:        "host",         /* H */
                     59:        "window_index", /* I */
                     60:        NULL,           /* J */
                     61:        NULL,           /* K */
                     62:        NULL,           /* L */
                     63:        NULL,           /* M */
                     64:        NULL,           /* N */
                     65:        NULL,           /* O */
                     66:        "pane_index",   /* P */
                     67:        NULL,           /* Q */
                     68:        NULL,           /* R */
                     69:        "session_name", /* S */
                     70:        "pane_title",   /* T */
                     71:        NULL,           /* U */
                     72:        NULL,           /* V */
                     73:        "window_name",  /* W */
                     74:        NULL,           /* X */
                     75:        NULL,           /* Y */
                     76:        NULL            /* Z */
                     77: };
                     78:
                     79: /* Create a new tree. */
                     80: struct format_tree *
                     81: format_create(void)
                     82: {
                     83:        struct format_tree      *ft;
                     84:        char                     host[MAXHOSTNAMELEN];
                     85:
                     86:        ft = xmalloc(sizeof *ft);
                     87:        RB_INIT(ft);
                     88:
                     89:        if (gethostname(host, sizeof host) == 0)
                     90:                format_add(ft, "host", "%s", host);
                     91:
                     92:        return (ft);
                     93: }
                     94:
                     95: /* Free a tree. */
                     96: void
                     97: format_free(struct format_tree *ft)
                     98: {
                     99:        struct format_entry     *fe, *fe_next;
                    100:
                    101:        fe_next = RB_MIN(format_tree, ft);
                    102:        while (fe_next != NULL) {
                    103:                fe = fe_next;
                    104:                fe_next = RB_NEXT(format_tree, ft, fe);
                    105:
                    106:                RB_REMOVE(format_tree, ft, fe);
1.9       nicm      107:                free(fe->value);
                    108:                free(fe->key);
                    109:                free(fe);
1.1       nicm      110:        }
                    111:
1.9       nicm      112:        free (ft);
1.1       nicm      113: }
                    114:
                    115: /* Add a key-value pair. */
                    116: void
                    117: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
                    118: {
                    119:        struct format_entry     *fe;
                    120:        va_list                  ap;
                    121:
                    122:        fe = xmalloc(sizeof *fe);
                    123:        fe->key = xstrdup(key);
                    124:
                    125:        va_start(ap, fmt);
                    126:        xvasprintf(&fe->value, fmt, ap);
                    127:        va_end(ap);
                    128:
                    129:        RB_INSERT(format_tree, ft, fe);
                    130: }
                    131:
                    132: /* Find a format entry. */
                    133: const char *
                    134: format_find(struct format_tree *ft, const char *key)
                    135: {
                    136:        struct format_entry     *fe, fe_find;
                    137:
                    138:        fe_find.key = (char *) key;
                    139:        fe = RB_FIND(format_tree, ft, &fe_find);
                    140:        if (fe == NULL)
                    141:                return (NULL);
                    142:        return (fe->value);
                    143: }
                    144:
                    145: /*
                    146:  * Replace a key/value pair in buffer. #{blah} is expanded directly,
                    147:  * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
                    148:  */
                    149: int
                    150: format_replace(struct format_tree *ft,
                    151:     const char *key, size_t keylen, char **buf, size_t *len, size_t *off)
                    152: {
                    153:        char            *copy, *ptr;
                    154:        const char      *value;
                    155:        size_t           valuelen;
                    156:
                    157:        /* Make a copy of the key. */
                    158:        copy = xmalloc(keylen + 1);
                    159:        memcpy(copy, key, keylen);
                    160:        copy[keylen] = '\0';
                    161:
                    162:        /*
                    163:         * Is this a conditional? If so, check it exists and extract either the
                    164:         * first or second element. If not, look up the key directly.
                    165:         */
                    166:        if (*copy == '?') {
                    167:                ptr = strchr(copy, ',');
                    168:                if (ptr == NULL)
                    169:                        goto fail;
                    170:                *ptr = '\0';
                    171:
                    172:                value = format_find(ft, copy + 1);
                    173:                if (value != NULL && (value[0] != '0' || value[1] != '\0')) {
                    174:                        value = ptr + 1;
                    175:                        ptr = strchr(value, ',');
                    176:                        if (ptr == NULL)
                    177:                                goto fail;
                    178:                        *ptr = '\0';
                    179:                } else {
                    180:                        ptr = strchr(ptr + 1, ',');
                    181:                        if (ptr == NULL)
                    182:                                goto fail;
                    183:                        value = ptr + 1;
                    184:                }
                    185:        } else {
                    186:                value = format_find(ft, copy);
                    187:                if (value == NULL)
                    188:                        value = "";
                    189:        }
                    190:        valuelen = strlen(value);
                    191:
                    192:        /* Expand the buffer and copy in the value. */
                    193:        while (*len - *off < valuelen + 1) {
                    194:                *buf = xrealloc(*buf, 2, *len);
                    195:                *len *= 2;
                    196:        }
                    197:        memcpy(*buf + *off, value, valuelen);
                    198:        *off += valuelen;
                    199:
1.9       nicm      200:        free(copy);
1.1       nicm      201:        return (0);
                    202:
                    203: fail:
1.9       nicm      204:        free(copy);
1.1       nicm      205:        return (-1);
                    206: }
                    207:
                    208: /* Expand keys in a template. */
                    209: char *
                    210: format_expand(struct format_tree *ft, const char *fmt)
                    211: {
                    212:        char            *buf, *ptr;
                    213:        const char      *s;
                    214:        size_t           off, len, n;
                    215:        int              ch;
                    216:
                    217:        len = 64;
                    218:        buf = xmalloc(len);
                    219:        off = 0;
                    220:
                    221:        while (*fmt != '\0') {
                    222:                if (*fmt != '#') {
                    223:                        while (len - off < 2) {
                    224:                                buf = xrealloc(buf, 2, len);
                    225:                                len *= 2;
                    226:                        }
                    227:                        buf[off++] = *fmt++;
                    228:                        continue;
                    229:                }
                    230:                fmt++;
                    231:
                    232:                ch = (u_char) *fmt++;
                    233:                switch (ch) {
                    234:                case '{':
                    235:                        ptr = strchr(fmt, '}');
                    236:                        if (ptr == NULL)
                    237:                                break;
                    238:                        n = ptr - fmt;
                    239:
                    240:                        if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
                    241:                                break;
                    242:                        fmt += n + 1;
                    243:                        continue;
                    244:                default:
                    245:                        if (ch >= 'A' && ch <= 'Z') {
                    246:                                s = format_aliases[ch - 'A'];
                    247:                                if (s != NULL) {
                    248:                                        n = strlen(s);
                    249:                                        if (format_replace (
                    250:                                            ft, s, n, &buf, &len, &off) != 0)
                    251:                                                break;
                    252:                                        continue;
                    253:                                }
                    254:                        }
1.13      nicm      255:                        while (len - off < 3) {
1.1       nicm      256:                                buf = xrealloc(buf, 2, len);
                    257:                                len *= 2;
                    258:                        }
1.13      nicm      259:                        buf[off++] = '#';
1.1       nicm      260:                        buf[off++] = ch;
                    261:                        continue;
                    262:                }
                    263:
                    264:                break;
                    265:        }
                    266:        buf[off] = '\0';
                    267:
                    268:        return (buf);
                    269: }
                    270:
                    271: /* Set default format keys for a session. */
                    272: void
                    273: format_session(struct format_tree *ft, struct session *s)
                    274: {
                    275:        struct session_group    *sg;
                    276:        char                    *tim;
                    277:        time_t                   t;
                    278:
                    279:        format_add(ft, "session_name", "%s", s->name);
                    280:        format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
                    281:        format_add(ft, "session_width", "%u", s->sx);
                    282:        format_add(ft, "session_height", "%u", s->sy);
1.23    ! nicm      283:        format_add(ft, "session_id", "$%u", s->id);
1.1       nicm      284:
                    285:        sg = session_group_find(s);
                    286:        format_add(ft, "session_grouped", "%d", sg != NULL);
                    287:        if (sg != NULL)
                    288:                format_add(ft, "session_group", "%u", session_group_index(sg));
                    289:
                    290:        t = s->creation_time.tv_sec;
                    291:        format_add(ft, "session_created", "%ld", (long) t);
                    292:        tim = ctime(&t);
                    293:        *strchr(tim, '\n') = '\0';
                    294:        format_add(ft, "session_created_string", "%s", tim);
                    295:
                    296:        if (s->flags & SESSION_UNATTACHED)
                    297:                format_add(ft, "session_attached", "%d", 0);
                    298:        else
                    299:                format_add(ft, "session_attached", "%d", 1);
1.3       nicm      300: }
                    301:
                    302: /* Set default format keys for a client. */
                    303: void
                    304: format_client(struct format_tree *ft, struct client *c)
                    305: {
1.15      nicm      306:        char            *tim;
                    307:        time_t           t;
                    308:        struct session  *s;
1.3       nicm      309:
                    310:        format_add(ft, "client_cwd", "%s", c->cwd);
1.6       nicm      311:        format_add(ft, "client_height", "%u", c->tty.sy);
                    312:        format_add(ft, "client_width", "%u", c->tty.sx);
1.3       nicm      313:        format_add(ft, "client_tty", "%s", c->tty.path);
                    314:        format_add(ft, "client_termname", "%s", c->tty.termname);
                    315:
                    316:        t = c->creation_time.tv_sec;
                    317:        format_add(ft, "client_created", "%ld", (long) t);
                    318:        tim = ctime(&t);
                    319:        *strchr(tim, '\n') = '\0';
                    320:        format_add(ft, "client_created_string", "%s", tim);
                    321:
                    322:        t = c->activity_time.tv_sec;
                    323:        format_add(ft, "client_activity", "%ld", (long) t);
                    324:        tim = ctime(&t);
                    325:        *strchr(tim, '\n') = '\0';
                    326:        format_add(ft, "client_activity_string", "%s", tim);
1.14      nicm      327:
                    328:        format_add(ft, "client_prefix", "%d", !!(c->flags & CLIENT_PREFIX));
1.3       nicm      329:
                    330:        if (c->tty.flags & TTY_UTF8)
                    331:                format_add(ft, "client_utf8", "%d", 1);
                    332:        else
                    333:                format_add(ft, "client_utf8", "%d", 0);
                    334:
                    335:        if (c->flags & CLIENT_READONLY)
                    336:                format_add(ft, "client_readonly", "%d", 1);
                    337:        else
                    338:                format_add(ft, "client_readonly", "%d", 0);
1.15      nicm      339:
                    340:        s = c->session;
                    341:        if (s != NULL)
                    342:                format_add(ft, "client_session", "%s", s->name);
                    343:        s = c->last_session;
                    344:        if (s != NULL && session_alive(s))
                    345:                format_add(ft, "client_last_session", "%s", s->name);
1.1       nicm      346: }
                    347:
                    348: /* Set default format keys for a winlink. */
                    349: void
                    350: format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
                    351: {
                    352:        struct window   *w = wl->window;
                    353:        char            *layout, *flags;
                    354:
                    355:        layout = layout_dump(w);
                    356:        flags = window_printable_flags(s, wl);
                    357:
1.5       nicm      358:        format_add(ft, "window_id", "@%u", w->id);
1.1       nicm      359:        format_add(ft, "window_index", "%d", wl->idx);
                    360:        format_add(ft, "window_name", "%s", w->name);
                    361:        format_add(ft, "window_width", "%u", w->sx);
                    362:        format_add(ft, "window_height", "%u", w->sy);
                    363:        format_add(ft, "window_flags", "%s", flags);
                    364:        format_add(ft, "window_layout", "%s", layout);
                    365:        format_add(ft, "window_active", "%d", wl == s->curw);
1.8       nicm      366:        format_add(ft, "window_panes", "%u", window_count_panes(w));
1.1       nicm      367:
1.9       nicm      368:        free(flags);
                    369:        free(layout);
1.1       nicm      370: }
                    371:
1.19      nicm      372: /* Add window pane tabs. */
                    373: void
                    374: format_window_pane_tabs(struct format_tree *ft, struct window_pane *wp)
                    375: {
                    376:        struct evbuffer *buffer;
                    377:        u_int            i;
                    378:
                    379:        buffer = evbuffer_new();
                    380:        for (i = 0; i < wp->base.grid->sx; i++) {
                    381:                if (!bit_test(wp->base.tabs, i))
                    382:                        continue;
                    383:
                    384:                if (EVBUFFER_LENGTH(buffer) > 0)
                    385:                        evbuffer_add(buffer, ",", 1);
                    386:                evbuffer_add_printf(buffer, "%d", i);
                    387:        }
                    388:
                    389:        format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer),
                    390:            EVBUFFER_DATA(buffer));
                    391:        evbuffer_free(buffer);
                    392: }
                    393:
1.1       nicm      394: /* Set default format keys for a window pane. */
                    395: void
                    396: format_window_pane(struct format_tree *ft, struct window_pane *wp)
                    397: {
                    398:        struct grid             *gd = wp->base.grid;
                    399:        struct grid_line        *gl;
                    400:        unsigned long long       size;
1.20      nicm      401:        u_int                    i, idx;
1.21      nicm      402:        const char              *cwd;
                    403:        char                    *cmd;
1.1       nicm      404:
                    405:        size = 0;
                    406:        for (i = 0; i < gd->hsize; i++) {
                    407:                gl = &gd->linedata[i];
                    408:                size += gl->cellsize * sizeof *gl->celldata;
                    409:        }
                    410:        size += gd->hsize * sizeof *gd->linedata;
1.16      nicm      411:        format_add(ft, "history_size", "%u", gd->hsize);
                    412:        format_add(ft, "history_limit", "%u", gd->hlimit);
                    413:        format_add(ft, "history_bytes", "%llu", size);
1.1       nicm      414:
1.4       nicm      415:        if (window_pane_index(wp, &idx) != 0)
                    416:                fatalx("index not found");
1.16      nicm      417:        format_add(ft, "pane_index", "%u", idx);
1.4       nicm      418:
1.1       nicm      419:        format_add(ft, "pane_width", "%u", wp->sx);
                    420:        format_add(ft, "pane_height", "%u", wp->sy);
                    421:        format_add(ft, "pane_title", "%s", wp->base.title);
                    422:        format_add(ft, "pane_id", "%%%u", wp->id);
                    423:        format_add(ft, "pane_active", "%d", wp == wp->window->active);
                    424:        format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.16      nicm      425:
                    426:        format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
                    427:
                    428:        if (wp->tty != NULL)
                    429:                format_add(ft, "pane_tty", "%s", wp->tty);
                    430:        format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.2       nicm      431:        if (wp->cmd != NULL)
                    432:                format_add(ft, "pane_start_command", "%s", wp->cmd);
                    433:        if (wp->cwd != NULL)
                    434:                format_add(ft, "pane_start_path", "%s", wp->cwd);
1.12      nicm      435:        if ((cwd = get_proc_cwd(wp->fd)) != NULL)
                    436:                format_add(ft, "pane_current_path", "%s", cwd);
1.21      nicm      437:        if ((cmd = get_proc_name(wp->fd, wp->tty)) != NULL) {
1.17      nicm      438:                format_add(ft, "pane_current_command", "%s", cmd);
1.21      nicm      439:                free(cmd);
                    440:        }
1.16      nicm      441:
                    442:        format_add(ft, "cursor_x", "%d", wp->base.cx);
                    443:        format_add(ft, "cursor_y", "%d", wp->base.cy);
                    444:        format_add(ft, "scroll_region_upper", "%d", wp->base.rupper);
                    445:        format_add(ft, "scroll_region_lower", "%d", wp->base.rlower);
                    446:        format_add(ft, "saved_cursor_x", "%d", wp->ictx.old_cx);
                    447:        format_add(ft, "saved_cursor_y", "%d", wp->ictx.old_cy);
                    448:
                    449:        format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
                    450:        format_add(ft, "alternate_saved_x", "%d", wp->saved_cx);
                    451:        format_add(ft, "alternate_saved_y", "%d", wp->saved_cy);
                    452:
                    453:        format_add(ft, "cursor_flag", "%d",
                    454:            !!(wp->base.mode & MODE_CURSOR));
                    455:        format_add(ft, "insert_flag", "%d",
                    456:            !!(wp->base.mode & MODE_INSERT));
                    457:        format_add(ft, "keypad_cursor_flag", "%d",
                    458:            !!(wp->base.mode & MODE_KCURSOR));
                    459:        format_add(ft, "keypad_flag", "%d",
                    460:            !!(wp->base.mode & MODE_KKEYPAD));
                    461:        format_add(ft, "wrap_flag", "%d",
                    462:            !!(wp->base.mode & MODE_WRAP));
                    463:
                    464:        format_add(ft, "mouse_standard_flag", "%d",
                    465:            !!(wp->base.mode & MODE_MOUSE_STANDARD));
                    466:        format_add(ft, "mouse_button_flag", "%d",
                    467:            !!(wp->base.mode & MODE_MOUSE_BUTTON));
                    468:        format_add(ft, "mouse_any_flag", "%d",
                    469:            !!(wp->base.mode & MODE_MOUSE_ANY));
                    470:        format_add(ft, "mouse_utf8_flag", "%d",
                    471:            !!(wp->base.mode & MODE_MOUSE_UTF8));
1.19      nicm      472:
                    473:        format_window_pane_tabs(ft, wp);
1.8       nicm      474: }
                    475:
1.19      nicm      476: /* Set default format keys for paste buffer. */
1.8       nicm      477: void
                    478: format_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
                    479: {
                    480:        char    *pb_print = paste_print(pb, 50);
                    481:
                    482:        format_add(ft, "buffer_size", "%zu", pb->size);
                    483:        format_add(ft, "buffer_sample", "%s", pb_print);
                    484:
1.9       nicm      485:        free(pb_print);
1.1       nicm      486: }