[BACK]Return to status.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/status.c, Revision 1.13

1.13    ! nicm        1: /* $OpenBSD: status.c,v 1.12 2009/07/17 06:13:27 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 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: #include <sys/time.h>
                     21:
                     22: #include <errno.h>
                     23: #include <limits.h>
                     24: #include <stdarg.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <time.h>
                     28: #include <unistd.h>
                     29:
                     30: #include "tmux.h"
                     31:
                     32: char   *status_replace_popen(char **);
                     33: size_t status_width(struct winlink *);
                     34: char   *status_print(struct session *, struct winlink *, struct grid_cell *);
                     35:
                     36: void   status_prompt_add_history(struct client *);
                     37: char   *status_prompt_complete(const char *);
                     38:
                     39: /* Draw status for client on the last lines of given context. */
                     40: int
                     41: status_redraw(struct client *c)
                     42: {
                     43:        struct screen_write_ctx         ctx;
                     44:        struct session                 *s = c->session;
                     45:        struct winlink                 *wl;
1.7       nicm       46:        struct screen                   old_status;
1.1       nicm       47:        char                           *left, *right, *text, *ptr;
1.2       nicm       48:        size_t                          llen, llen2, rlen, rlen2, offset;
1.7       nicm       49:        size_t                          xx, yy, size, start, width;
1.1       nicm       50:        struct grid_cell                stdgc, gc;
1.3       nicm       51:        int                             larrow, rarrow, utf8flag;
1.1       nicm       52:
                     53:        left = right = NULL;
                     54:
1.7       nicm       55:        /* No status line?*/
                     56:        if (c->tty.sy == 0 || !options_get_number(&s->options, "status"))
                     57:                return (1);
                     58:        larrow = rarrow = 0;
1.1       nicm       59:
1.7       nicm       60:        /* Create the target screen. */
                     61:        memcpy(&old_status, &c->status, sizeof old_status);
                     62:        screen_init(&c->status, c->tty.sx, 1, 0);
1.1       nicm       63:
                     64:        if (gettimeofday(&c->status_timer, NULL) != 0)
                     65:                fatal("gettimeofday");
                     66:        memcpy(&stdgc, &grid_default_cell, sizeof gc);
                     67:        stdgc.bg = options_get_number(&s->options, "status-fg");
                     68:        stdgc.fg = options_get_number(&s->options, "status-bg");
                     69:        stdgc.attr |= options_get_number(&s->options, "status-attr");
                     70:
                     71:        yy = c->tty.sy - 1;
                     72:        if (yy == 0)
                     73:                goto blank;
                     74:
1.3       nicm       75:        /* Caring about UTF-8 in status line? */
                     76:        utf8flag = options_get_number(&s->options, "status-utf8");
                     77:
1.1       nicm       78:        /* Work out the left and right strings. */
                     79:        left = status_replace(s, options_get_string(
                     80:            &s->options, "status-left"), c->status_timer.tv_sec);
                     81:        llen = options_get_number(&s->options, "status-left-length");
1.3       nicm       82:        llen2 = screen_write_strlen(utf8flag, "%s", left);
1.2       nicm       83:        if (llen2 < llen)
                     84:                llen = llen2;
1.1       nicm       85:
                     86:        right = status_replace(s, options_get_string(
                     87:            &s->options, "status-right"), c->status_timer.tv_sec);
                     88:        rlen = options_get_number(&s->options, "status-right-length");
1.3       nicm       89:        rlen2 = screen_write_strlen(utf8flag, "%s", right);
1.2       nicm       90:        if (rlen2 < rlen)
                     91:                rlen = rlen2;
1.1       nicm       92:
                     93:        /*
                     94:         * Figure out how much space we have for the window list. If there isn't
                     95:         * enough space, just wimp out.
                     96:         */
                     97:        xx = 0;
                     98:        if (llen != 0)
                     99:                xx += llen + 1;
                    100:        if (rlen != 0)
                    101:                xx += rlen + 1;
                    102:        if (c->tty.sx == 0 || c->tty.sx <= xx)
                    103:                goto blank;
                    104:        xx = c->tty.sx - xx;
                    105:
                    106:        /*
                    107:         * Right. We have xx characters to fill. Find out how much is to go in
                    108:         * them and the offset of the current window (it must be on screen).
                    109:         */
                    110:        width = offset = 0;
                    111:        RB_FOREACH(wl, winlinks, &s->windows) {
                    112:                size = status_width(wl) + 1;
                    113:                if (wl == s->curw)
                    114:                        offset = width;
                    115:                width += size;
                    116:        }
                    117:        start = 0;
                    118:
                    119:        /* If there is enough space for the total width, all is gravy. */
                    120:        if (width <= xx)
                    121:                goto draw;
                    122:
                    123:        /* Find size of current window text. */
                    124:        size = status_width(s->curw);
                    125:
                    126:        /*
                    127:         * If the offset is already on screen, we're good to draw from the
                    128:         * start and just leave off the end.
                    129:         */
                    130:        if (offset + size < xx) {
                    131:                if (xx > 0) {
                    132:                        rarrow = 1;
                    133:                        xx--;
                    134:                }
                    135:
                    136:                width = xx;
                    137:                goto draw;
                    138:        }
                    139:
                    140:        /*
                    141:         * Work out how many characters we need to omit from the start. There
                    142:         * are xx characters to fill, and offset + size must be the last. So,
                    143:         * the start character is offset + size - xx.
                    144:         */
                    145:        if (xx > 0) {
                    146:                larrow = 1;
                    147:                xx--;
                    148:        }
                    149:
                    150:        start = offset + size - xx;
                    151:        if (xx > 0 && width > start + xx + 1) { /* + 1, eh? */
                    152:                rarrow = 1;
                    153:                start++;
                    154:                xx--;
                    155:        }
                    156:        width = xx;
                    157:
                    158: draw:
                    159:        /* Bail here if anything is too small too. XXX. */
                    160:        if (width == 0 || xx == 0)
                    161:                goto blank;
                    162:
                    163:        /* Begin drawing and move to the starting position. */
                    164:        screen_write_start(&ctx, NULL, &c->status);
                    165:        if (llen != 0) {
                    166:                screen_write_cursormove(&ctx, 0, yy);
1.5       nicm      167:                screen_write_nputs(&ctx, llen, &stdgc, utf8flag, "%s", left);
                    168:                screen_write_putc(&ctx, &stdgc, ' ');
1.1       nicm      169:                if (larrow)
                    170:                        screen_write_putc(&ctx, &stdgc, ' ');
                    171:        } else {
                    172:                if (larrow)
                    173:                        screen_write_cursormove(&ctx, 1, yy);
                    174:                else
                    175:                        screen_write_cursormove(&ctx, 0, yy);
                    176:        }
                    177:
                    178:        /* Draw each character in succession. */
                    179:        offset = 0;
                    180:        RB_FOREACH(wl, winlinks, &s->windows) {
                    181:                memcpy(&gc, &stdgc, sizeof gc);
                    182:                text = status_print(s, wl, &gc);
                    183:
                    184:                if (larrow == 1 && offset < start) {
                    185:                        if (session_alert_has(s, wl, WINDOW_ACTIVITY))
                    186:                                larrow = -1;
                    187:                        else if (session_alert_has(s, wl, WINDOW_BELL))
                    188:                                larrow = -1;
                    189:                        else if (session_alert_has(s, wl, WINDOW_CONTENT))
                    190:                                larrow = -1;
                    191:                }
                    192:
                    193:                for (ptr = text; *ptr != '\0'; ptr++) {
                    194:                        if (offset >= start && offset < start + width)
                    195:                                screen_write_putc(&ctx, &gc, *ptr);
                    196:                        offset++;
                    197:                }
                    198:
                    199:                if (rarrow == 1 && offset > start + width) {
                    200:                        if (session_alert_has(s, wl, WINDOW_ACTIVITY))
                    201:                                rarrow = -1;
                    202:                        else if (session_alert_has(s, wl, WINDOW_BELL))
                    203:                                rarrow = -1;
                    204:                        else if (session_alert_has(s, wl, WINDOW_CONTENT))
                    205:                                rarrow = -1;
                    206:                }
                    207:
                    208:                if (offset < start + width) {
                    209:                        if (offset >= start) {
                    210:                                screen_write_putc(&ctx, &stdgc, ' ');
                    211:                        }
                    212:                        offset++;
                    213:                }
                    214:
                    215:                xfree(text);
                    216:        }
                    217:
                    218:        /* Fill the remaining space if any. */
                    219:        while (offset++ < xx)
                    220:                screen_write_putc(&ctx, &stdgc, ' ');
                    221:
                    222:        /* Draw the last item. */
                    223:        if (rlen != 0) {
                    224:                screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, yy);
1.5       nicm      225:                screen_write_putc(&ctx, &stdgc, ' ');
                    226:                screen_write_nputs(&ctx, rlen, &stdgc, utf8flag, "%s", right);
1.1       nicm      227:        }
                    228:
                    229:        /* Draw the arrows. */
                    230:        if (larrow != 0) {
                    231:                memcpy(&gc, &stdgc, sizeof gc);
                    232:                if (larrow == -1)
                    233:                        gc.attr ^= GRID_ATTR_REVERSE;
                    234:                if (llen != 0)
                    235:                        screen_write_cursormove(&ctx, llen + 1, yy);
                    236:                else
                    237:                        screen_write_cursormove(&ctx, 0, yy);
                    238:                screen_write_putc(&ctx, &gc, '<');
                    239:        }
                    240:        if (rarrow != 0) {
                    241:                memcpy(&gc, &stdgc, sizeof gc);
                    242:                if (rarrow == -1)
                    243:                        gc.attr ^= GRID_ATTR_REVERSE;
                    244:                if (rlen != 0)
                    245:                        screen_write_cursormove(&ctx, c->tty.sx - rlen - 2, yy);
                    246:                else
                    247:                        screen_write_cursormove(&ctx, c->tty.sx - 1, yy);
                    248:                screen_write_putc(&ctx, &gc, '>');
                    249:        }
                    250:
                    251:        goto out;
                    252:
                    253: blank:
                    254:        /* Just draw the whole line as blank. */
                    255:        screen_write_start(&ctx, NULL, &c->status);
                    256:        screen_write_cursormove(&ctx, 0, yy);
                    257:        for (offset = 0; offset < c->tty.sx; offset++)
                    258:                screen_write_putc(&ctx, &stdgc, ' ');
                    259:
                    260: out:
                    261:        screen_write_stop(&ctx);
                    262:
                    263:        if (left != NULL)
                    264:                xfree(left);
                    265:        if (right != NULL)
                    266:                xfree(right);
                    267:
                    268:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    269:                screen_free(&old_status);
                    270:                return (0);
                    271:        }
                    272:        screen_free(&old_status);
                    273:        return (1);
                    274: }
                    275:
                    276: char *
1.13    ! nicm      277: status_replace(struct session *s, const char *fmt, time_t t)
1.1       nicm      278: {
                    279:        struct winlink *wl = s->curw;
                    280:        static char     out[BUFSIZ];
                    281:        char            in[BUFSIZ], tmp[256], ch, *iptr, *optr, *ptr, *endptr;
                    282:        char           *savedptr;
                    283:        size_t          len;
                    284:        long            n;
                    285:
                    286:        strftime(in, sizeof in, fmt, localtime(&t));
                    287:        in[(sizeof in) - 1] = '\0';
                    288:
                    289:        iptr = in;
                    290:        optr = out;
                    291:        savedptr = NULL;
                    292:
                    293:        while (*iptr != '\0') {
                    294:                if (optr >= out + (sizeof out) - 1)
                    295:                        break;
                    296:                switch (ch = *iptr++) {
                    297:                case '#':
                    298:                        errno = 0;
                    299:                        n = strtol(iptr, &endptr, 10);
                    300:                        if ((n == 0 && errno != EINVAL) ||
                    301:                            (n == LONG_MIN && errno != ERANGE) ||
                    302:                            (n == LONG_MAX && errno != ERANGE) ||
                    303:                            n != 0)
                    304:                                iptr = endptr;
                    305:                        if (n <= 0)
                    306:                                n = LONG_MAX;
                    307:
                    308:                        ptr = NULL;
                    309:                        switch (*iptr++) {
                    310:                        case '(':
                    311:                                if (ptr == NULL) {
                    312:                                        ptr = status_replace_popen(&iptr);
                    313:                                        if (ptr == NULL)
                    314:                                                break;
                    315:                                        savedptr = ptr;
                    316:                                }
                    317:                                /* FALLTHROUGH */
                    318:                        case 'H':
                    319:                                if (ptr == NULL) {
                    320:                                        if (gethostname(tmp, sizeof tmp) != 0)
                    321:                                                fatal("gethostname");
                    322:                                        ptr = tmp;
                    323:                                }
                    324:                                /* FALLTHROUGH */
1.13    ! nicm      325:                        case 'I':
        !           326:                                if (ptr == NULL) {
        !           327:                                        xsnprintf(tmp, sizeof tmp, "%d", wl->idx);
        !           328:                                        ptr = tmp;
        !           329:                                }
        !           330:                                /* FALLTHROUGH */
        !           331:                        case 'P':
        !           332:                                if (ptr == NULL) {
        !           333:                                        xsnprintf(tmp, sizeof tmp, "%u",
        !           334:                                                  window_pane_index(wl->window,
        !           335:                                                  wl->window->active));
        !           336:                                        ptr = tmp;
        !           337:                                }
        !           338:                                /* FALLTHOUGH */
1.1       nicm      339:                        case 'S':
                    340:                                if (ptr == NULL)
                    341:                                        ptr = s->name;
                    342:                                /* FALLTHROUGH */
                    343:                        case 'T':
                    344:                                if (ptr == NULL)
                    345:                                        ptr = wl->window->active->base.title;
1.13    ! nicm      346:                                /* FALLTHROUGH */
        !           347:                        case 'W':
        !           348:                                if (ptr == NULL)
        !           349:                                        ptr = wl->window->name;
1.1       nicm      350:                                len = strlen(ptr);
                    351:                                if ((size_t) n < len)
                    352:                                        len = n;
                    353:                                if (optr + len >= out + (sizeof out) - 1)
                    354:                                        break;
                    355:                                while (len > 0 && *ptr != '\0') {
                    356:                                        *optr++ = *ptr++;
                    357:                                        len--;
                    358:                                }
                    359:                                break;
                    360:                        case '#':
                    361:                                *optr++ = '#';
                    362:                                break;
                    363:                        }
                    364:                        if (savedptr != NULL) {
                    365:                                xfree(savedptr);
                    366:                                savedptr = NULL;
                    367:                        }
                    368:                        break;
                    369:                default:
                    370:                        *optr++ = ch;
                    371:                        break;
                    372:                }
                    373:        }
                    374:        *optr = '\0';
                    375:
                    376:        return (xstrdup(out));
                    377: }
                    378:
                    379: char *
                    380: status_replace_popen(char **iptr)
                    381: {
                    382:        FILE    *f;
                    383:        char    *buf, *cmd, *ptr;
                    384:        int     lastesc;
                    385:        size_t  len;
                    386:
                    387:        if (**iptr == '\0')
                    388:                return (NULL);
                    389:        if (**iptr == ')') {            /* no command given */
                    390:                (*iptr)++;
                    391:                return (NULL);
                    392:        }
                    393:
                    394:        buf = NULL;
                    395:
                    396:        cmd = xmalloc(strlen(*iptr) + 1);
                    397:        len = 0;
                    398:
                    399:        lastesc = 0;
                    400:        for (; **iptr != '\0'; (*iptr)++) {
                    401:                if (!lastesc && **iptr == ')')
                    402:                        break;          /* unescaped ) is the end */
                    403:                if (!lastesc && **iptr == '\\') {
                    404:                        lastesc = 1;
                    405:                        continue;       /* skip \ if not escaped */
                    406:                }
                    407:                lastesc = 0;
                    408:                cmd[len++] = **iptr;
                    409:        }
                    410:        if (**iptr == '\0')             /* no terminating ) */
                    411:                goto out;
                    412:        (*iptr)++;                      /* skip final ) */
                    413:        cmd[len] = '\0';
                    414:
                    415:        if ((f = popen(cmd, "r")) == NULL)
                    416:                goto out;
                    417:
                    418:        if ((buf = fgetln(f, &len)) == NULL) {
                    419:                pclose(f);
                    420:                goto out;
                    421:        }
                    422:        if (buf[len - 1] == '\n') {
                    423:                buf[len - 1] = '\0';
                    424:                buf = xstrdup(buf);
                    425:        } else {
                    426:                ptr = xmalloc(len + 1);
                    427:                memcpy(ptr, buf, len);
                    428:                ptr[len] = '\0';
                    429:                buf = ptr;
                    430:        }
                    431:        pclose(f);
                    432:
                    433: out:
                    434:        xfree(cmd);
                    435:        return (buf);
                    436: }
                    437:
                    438: size_t
                    439: status_width(struct winlink *wl)
                    440: {
                    441:        return (xsnprintf(NULL, 0, "%d:%s ", wl->idx, wl->window->name));
                    442: }
                    443:
                    444: char *
                    445: status_print(struct session *s, struct winlink *wl, struct grid_cell *gc)
                    446: {
                    447:        char   *text, flag;
                    448:        u_char  fg, bg, attr;
                    449:
                    450:        fg = options_get_number(&wl->window->options, "window-status-fg");
                    451:        if (fg != 8)
                    452:                gc->fg = fg;
                    453:        bg = options_get_number(&wl->window->options, "window-status-bg");
                    454:        if (bg != 8)
                    455:                gc->bg = bg;
                    456:        attr = options_get_number(&wl->window->options, "window-status-attr");
                    457:        if (attr != 0)
                    458:                gc->attr = attr;
                    459:
                    460:        flag = ' ';
                    461:        if (wl == SLIST_FIRST(&s->lastw))
                    462:                flag = '-';
                    463:        if (wl == s->curw)
                    464:                flag = '*';
                    465:
                    466:        if (session_alert_has(s, wl, WINDOW_ACTIVITY)) {
                    467:                flag = '#';
                    468:                gc->attr ^= GRID_ATTR_REVERSE;
                    469:        } else if (session_alert_has(s, wl, WINDOW_BELL)) {
                    470:                flag = '!';
                    471:                gc->attr ^= GRID_ATTR_REVERSE;
                    472:        } else if (session_alert_has(s, wl, WINDOW_CONTENT)) {
                    473:                flag = '+';
                    474:                gc->attr ^= GRID_ATTR_REVERSE;
                    475:        }
                    476:
                    477:        xasprintf(&text, "%d:%s%c", wl->idx, wl->window->name, flag);
                    478:        return (text);
                    479: }
                    480:
1.10      nicm      481: void printflike2
                    482: status_message_set(struct client *c, const char *fmt, ...)
1.1       nicm      483: {
                    484:        struct timeval  tv;
1.10      nicm      485:        va_list         ap;
1.1       nicm      486:        int             delay;
                    487:
1.12      nicm      488:        status_prompt_clear(c);
                    489:        status_message_clear(c);
                    490:
1.1       nicm      491:        delay = options_get_number(&c->session->options, "display-time");
                    492:        tv.tv_sec = delay / 1000;
                    493:        tv.tv_usec = (delay % 1000) * 1000L;
                    494:
1.10      nicm      495:        va_start(ap, fmt);
                    496:        xvasprintf(&c->message_string, fmt, ap);
                    497:        va_end(ap);
1.1       nicm      498:        if (gettimeofday(&c->message_timer, NULL) != 0)
                    499:                fatal("gettimeofday");
                    500:        timeradd(&c->message_timer, &tv, &c->message_timer);
                    501:
                    502:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
                    503:        c->flags |= CLIENT_STATUS;
                    504: }
                    505:
                    506: void
                    507: status_message_clear(struct client *c)
                    508: {
                    509:        if (c->message_string == NULL)
                    510:                return;
                    511:
                    512:        xfree(c->message_string);
                    513:        c->message_string = NULL;
                    514:
                    515:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12      nicm      516:        c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7       nicm      517:
                    518:        screen_reinit(&c->status);
1.1       nicm      519: }
                    520:
                    521: /* Draw client message on status line of present else on last line. */
                    522: int
                    523: status_message_redraw(struct client *c)
                    524: {
                    525:        struct screen_write_ctx         ctx;
                    526:        struct session                 *s = c->session;
                    527:        struct screen                   old_status;
                    528:        size_t                          len;
                    529:        struct grid_cell                gc;
                    530:
                    531:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    532:                return (0);
                    533:        memcpy(&old_status, &c->status, sizeof old_status);
                    534:        screen_init(&c->status, c->tty.sx, 1, 0);
                    535:
                    536:        len = strlen(c->message_string);
                    537:        if (len > c->tty.sx)
                    538:                len = c->tty.sx;
                    539:
                    540:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    541:        gc.bg = options_get_number(&s->options, "message-fg");
                    542:        gc.fg = options_get_number(&s->options, "message-bg");
                    543:        gc.attr |= options_get_number(&s->options, "message-attr");
                    544:
                    545:        screen_write_start(&ctx, NULL, &c->status);
                    546:
                    547:        screen_write_cursormove(&ctx, 0, 0);
                    548:        screen_write_puts(&ctx, &gc, "%.*s", (int) len, c->message_string);
                    549:        for (; len < c->tty.sx; len++)
                    550:                screen_write_putc(&ctx, &gc, ' ');
                    551:
                    552:        screen_write_stop(&ctx);
                    553:
                    554:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    555:                screen_free(&old_status);
                    556:                return (0);
                    557:        }
                    558:        screen_free(&old_status);
                    559:        return (1);
                    560: }
                    561:
                    562: void
1.12      nicm      563: status_prompt_set(struct client *c, const char *msg,
                    564:     int (*callbackfn)(void *, const char *), void (*freefn)(void *),
                    565:     void *data, int flags)
1.1       nicm      566: {
1.12      nicm      567:        status_message_clear(c);
                    568:        status_prompt_clear(c);
                    569:
1.1       nicm      570:        c->prompt_string = xstrdup(msg);
                    571:
                    572:        c->prompt_buffer = xstrdup("");
                    573:        c->prompt_index = 0;
                    574:
1.12      nicm      575:        c->prompt_callbackfn = callbackfn;
                    576:        c->prompt_freefn = freefn;
1.1       nicm      577:        c->prompt_data = data;
                    578:
                    579:        c->prompt_hindex = 0;
                    580:
                    581:        c->prompt_flags = flags;
                    582:
                    583:        mode_key_init(&c->prompt_mdata,
                    584:            options_get_number(&c->session->options, "status-keys"),
                    585:            MODEKEY_CANEDIT);
                    586:
                    587:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
                    588:        c->flags |= CLIENT_STATUS;
                    589: }
                    590:
                    591: void
                    592: status_prompt_clear(struct client *c)
                    593: {
1.12      nicm      594:        if (c->prompt_string == NULL)
1.1       nicm      595:                return;
                    596:
1.12      nicm      597:        if (c->prompt_freefn != NULL && c->prompt_data != NULL)
                    598:                c->prompt_freefn(c->prompt_data);
                    599:
1.1       nicm      600:        mode_key_free(&c->prompt_mdata);
                    601:
                    602:        xfree(c->prompt_string);
                    603:        c->prompt_string = NULL;
                    604:
1.4       nicm      605:        if (c->prompt_flags & PROMPT_HIDDEN)
                    606:                memset(c->prompt_buffer, 0, strlen(c->prompt_buffer));
1.1       nicm      607:        xfree(c->prompt_buffer);
                    608:        c->prompt_buffer = NULL;
                    609:
                    610:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12      nicm      611:        c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7       nicm      612:
                    613:        screen_reinit(&c->status);
1.1       nicm      614: }
                    615:
                    616: /* Draw client prompt on status line of present else on last line. */
                    617: int
                    618: status_prompt_redraw(struct client *c)
                    619: {
                    620:        struct screen_write_ctx ctx;
                    621:        struct session                 *s = c->session;
                    622:        struct screen                   old_status;
                    623:        size_t                          i, size, left, len, offset, n;
                    624:        char                            ch;
                    625:        struct grid_cell                gc;
                    626:
                    627:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    628:                return (0);
                    629:        memcpy(&old_status, &c->status, sizeof old_status);
                    630:        screen_init(&c->status, c->tty.sx, 1, 0);
                    631:        offset = 0;
                    632:
                    633:        len = strlen(c->prompt_string);
                    634:        if (len > c->tty.sx)
                    635:                len = c->tty.sx;
                    636:
                    637:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    638:        gc.bg = options_get_number(&s->options, "message-fg");
                    639:        gc.fg = options_get_number(&s->options, "message-bg");
                    640:        gc.attr |= options_get_number(&s->options, "message-attr");
                    641:
                    642:        screen_write_start(&ctx, NULL, &c->status);
                    643:
                    644:        screen_write_cursormove(&ctx, 0, 0);
                    645:        screen_write_puts(&ctx, &gc, "%.*s", (int) len, c->prompt_string);
                    646:
                    647:        left = c->tty.sx - len;
                    648:        if (left != 0) {
                    649:                if (c->prompt_index < left)
                    650:                        size = strlen(c->prompt_buffer);
                    651:                else {
                    652:                        offset = c->prompt_index - left - 1;
                    653:                        if (c->prompt_index == strlen(c->prompt_buffer))
                    654:                                left--;
                    655:                        size = left;
                    656:                }
                    657:                if (c->prompt_flags & PROMPT_HIDDEN) {
                    658:                        n = strlen(c->prompt_buffer);
                    659:                        if (n > left)
                    660:                                n = left;
                    661:                        for (i = 0; i < n; i++)
                    662:                                screen_write_putc(&ctx, &gc, '*');
                    663:                } else {
                    664:                        screen_write_puts(&ctx, &gc,
                    665:                            "%.*s", (int) left, c->prompt_buffer + offset);
                    666:                }
                    667:
                    668:                for (i = len + size; i < c->tty.sx; i++)
                    669:                        screen_write_putc(&ctx, &gc, ' ');
                    670:        }
                    671:
                    672:        /* Draw a fake cursor. */
                    673:        screen_write_cursormove(&ctx, len + c->prompt_index - offset, 0);
                    674:        if (c->prompt_index == strlen(c->prompt_buffer))
                    675:                ch = ' ';
                    676:        else {
                    677:                if (c->prompt_flags & PROMPT_HIDDEN)
                    678:                        ch = '*';
                    679:                else
                    680:                        ch = c->prompt_buffer[c->prompt_index];
                    681:        }
                    682:        if (ch == '\0')
                    683:                ch = ' ';
                    684:        gc.attr ^= GRID_ATTR_REVERSE;
                    685:        screen_write_putc(&ctx, &gc, ch);
                    686:
                    687:        screen_write_stop(&ctx);
                    688:
                    689:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    690:                screen_free(&old_status);
                    691:                return (0);
                    692:        }
                    693:        screen_free(&old_status);
                    694:        return (1);
                    695: }
                    696:
                    697: /* Handle keys in prompt. */
                    698: void
                    699: status_prompt_key(struct client *c, int key)
                    700: {
                    701:        struct paste_buffer     *pb;
                    702:        char                    *s, *first, *last, word[64];
                    703:        size_t                   size, n, off, idx;
                    704:
                    705:        size = strlen(c->prompt_buffer);
                    706:        switch (mode_key_lookup(&c->prompt_mdata, key)) {
                    707:        case MODEKEYCMD_LEFT:
                    708:                if (c->prompt_index > 0) {
                    709:                        c->prompt_index--;
                    710:                        c->flags |= CLIENT_STATUS;
                    711:                }
                    712:                break;
                    713:        case MODEKEYCMD_RIGHT:
                    714:                if (c->prompt_index < size) {
                    715:                        c->prompt_index++;
                    716:                        c->flags |= CLIENT_STATUS;
                    717:                }
                    718:                break;
                    719:        case MODEKEYCMD_STARTOFLINE:
1.6       nicm      720:        case MODEKEYCMD_BACKTOINDENTATION:
1.1       nicm      721:                if (c->prompt_index != 0) {
                    722:                        c->prompt_index = 0;
                    723:                        c->flags |= CLIENT_STATUS;
                    724:                }
                    725:                break;
                    726:        case MODEKEYCMD_ENDOFLINE:
                    727:                if (c->prompt_index != size) {
                    728:                        c->prompt_index = size;
                    729:                        c->flags |= CLIENT_STATUS;
                    730:                }
                    731:                break;
                    732:        case MODEKEYCMD_COMPLETE:
                    733:                if (*c->prompt_buffer == '\0')
                    734:                        break;
                    735:
                    736:                idx = c->prompt_index;
                    737:                if (idx != 0)
                    738:                        idx--;
                    739:
                    740:                /* Find the word we are in. */
                    741:                first = c->prompt_buffer + idx;
                    742:                while (first > c->prompt_buffer && *first != ' ')
                    743:                        first--;
                    744:                while (*first == ' ')
                    745:                        first++;
                    746:                last = c->prompt_buffer + idx;
                    747:                while (*last != '\0' && *last != ' ')
                    748:                        last++;
                    749:                while (*last == ' ')
                    750:                        last--;
                    751:                if (*last != '\0')
                    752:                        last++;
                    753:                if (last <= first ||
                    754:                    ((size_t) (last - first)) > (sizeof word) - 1)
                    755:                        break;
                    756:                memcpy(word, first, last - first);
                    757:                word[last - first] = '\0';
                    758:
                    759:                /* And try to complete it. */
                    760:                if ((s = status_prompt_complete(word)) == NULL)
                    761:                        break;
                    762:
                    763:                /* Trim out word. */
                    764:                n = size - (last - c->prompt_buffer) + 1; /* with \0 */
                    765:                memmove(first, last, n);
                    766:                size -= last - first;
                    767:
                    768:                /* Insert the new word. */
                    769:                size += strlen(s);
                    770:                off = first - c->prompt_buffer;
                    771:                c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 1);
                    772:                first = c->prompt_buffer + off;
                    773:                memmove(first + strlen(s), first, n);
                    774:                memcpy(first, s, strlen(s));
                    775:
                    776:                c->prompt_index = (first - c->prompt_buffer) + strlen(s);
                    777:
                    778:                c->flags |= CLIENT_STATUS;
                    779:                break;
                    780:        case MODEKEYCMD_BACKSPACE:
                    781:                if (c->prompt_index != 0) {
                    782:                        if (c->prompt_index == size)
                    783:                                c->prompt_buffer[--c->prompt_index] = '\0';
                    784:                        else {
                    785:                                memmove(c->prompt_buffer + c->prompt_index - 1,
                    786:                                    c->prompt_buffer + c->prompt_index,
                    787:                                    size + 1 - c->prompt_index);
                    788:                                c->prompt_index--;
                    789:                        }
                    790:                        c->flags |= CLIENT_STATUS;
                    791:                }
                    792:                break;
                    793:        case MODEKEYCMD_DELETE:
                    794:                if (c->prompt_index != size) {
                    795:                        memmove(c->prompt_buffer + c->prompt_index,
                    796:                            c->prompt_buffer + c->prompt_index + 1,
                    797:                            size + 1 - c->prompt_index);
                    798:                        c->flags |= CLIENT_STATUS;
                    799:                }
                    800:                break;
                    801:        case MODEKEYCMD_UP:
                    802:                if (server_locked)
                    803:                        break;
                    804:
                    805:                if (ARRAY_LENGTH(&c->prompt_hdata) == 0)
                    806:                        break;
1.4       nicm      807:                if (c->prompt_flags & PROMPT_HIDDEN)
                    808:                        memset(c->prompt_buffer, 0, strlen(c->prompt_buffer));
1.1       nicm      809:                xfree(c->prompt_buffer);
                    810:
                    811:                c->prompt_buffer = xstrdup(ARRAY_ITEM(&c->prompt_hdata,
                    812:                    ARRAY_LENGTH(&c->prompt_hdata) - 1 - c->prompt_hindex));
                    813:                if (c->prompt_hindex != ARRAY_LENGTH(&c->prompt_hdata) - 1)
                    814:                        c->prompt_hindex++;
                    815:
                    816:                c->prompt_index = strlen(c->prompt_buffer);
                    817:                c->flags |= CLIENT_STATUS;
                    818:                break;
                    819:        case MODEKEYCMD_DOWN:
                    820:                if (server_locked)
                    821:                        break;
                    822:
1.4       nicm      823:                if (c->prompt_flags & PROMPT_HIDDEN)
                    824:                        memset(c->prompt_buffer, 0, strlen(c->prompt_buffer));
1.1       nicm      825:                xfree(c->prompt_buffer);
                    826:
                    827:                if (c->prompt_hindex != 0) {
                    828:                        c->prompt_hindex--;
                    829:                        c->prompt_buffer = xstrdup(ARRAY_ITEM(
                    830:                            &c->prompt_hdata, ARRAY_LENGTH(
                    831:                            &c->prompt_hdata) - 1 - c->prompt_hindex));
                    832:                } else
                    833:                        c->prompt_buffer = xstrdup("");
                    834:
                    835:                c->prompt_index = strlen(c->prompt_buffer);
                    836:                c->flags |= CLIENT_STATUS;
                    837:                break;
                    838:        case MODEKEYCMD_PASTE:
                    839:                if ((pb = paste_get_top(&c->session->buffers)) == NULL)
                    840:                        break;
                    841:                if ((last = strchr(pb->data, '\n')) == NULL)
                    842:                        last = strchr(pb->data, '\0');
                    843:                n = last - pb->data;
                    844:
                    845:                c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + n + 1);
                    846:                if (c->prompt_index == size) {
                    847:                        memcpy(c->prompt_buffer + c->prompt_index, pb->data, n);
                    848:                        c->prompt_index += n;
                    849:                        c->prompt_buffer[c->prompt_index] = '\0';
                    850:                } else {
                    851:                        memmove(c->prompt_buffer + c->prompt_index + n,
                    852:                            c->prompt_buffer + c->prompt_index,
                    853:                            size + 1 - c->prompt_index);
                    854:                        memcpy(c->prompt_buffer + c->prompt_index, pb->data, n);
                    855:                        c->prompt_index += n;
                    856:                }
                    857:
                    858:                c->flags |= CLIENT_STATUS;
                    859:                break;
                    860:        case MODEKEYCMD_CHOOSE:
                    861:                if (*c->prompt_buffer != '\0') {
                    862:                        status_prompt_add_history(c);
1.12      nicm      863:                        if (c->prompt_callbackfn(
1.1       nicm      864:                            c->prompt_data, c->prompt_buffer) == 0)
                    865:                                status_prompt_clear(c);
                    866:                        break;
                    867:                }
                    868:                /* FALLTHROUGH */
                    869:        case MODEKEYCMD_QUIT:
1.12      nicm      870:                if (c->prompt_callbackfn(c->prompt_data, NULL) == 0)
1.1       nicm      871:                        status_prompt_clear(c);
                    872:                break;
                    873:        case MODEKEYCMD_OTHERKEY:
                    874:                if (key < 32 || key > 126)
                    875:                        break;
                    876:                c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 2);
                    877:
                    878:                if (c->prompt_index == size) {
                    879:                        c->prompt_buffer[c->prompt_index++] = key;
                    880:                        c->prompt_buffer[c->prompt_index] = '\0';
                    881:                } else {
                    882:                        memmove(c->prompt_buffer + c->prompt_index + 1,
                    883:                            c->prompt_buffer + c->prompt_index,
                    884:                            size + 1 - c->prompt_index);
                    885:                        c->prompt_buffer[c->prompt_index++] = key;
                    886:                }
                    887:
                    888:                if (c->prompt_flags & PROMPT_SINGLE) {
1.12      nicm      889:                        if (c->prompt_callbackfn(
1.1       nicm      890:                            c->prompt_data, c->prompt_buffer) == 0)
                    891:                                status_prompt_clear(c);
                    892:                }
                    893:
                    894:                c->flags |= CLIENT_STATUS;
                    895:                break;
                    896:        default:
                    897:                break;
                    898:        }
                    899: }
                    900:
                    901: /* Add line to the history. */
                    902: void
                    903: status_prompt_add_history(struct client *c)
                    904: {
                    905:        if (server_locked)
                    906:                return;
                    907:
                    908:        if (ARRAY_LENGTH(&c->prompt_hdata) > 0 &&
                    909:            strcmp(ARRAY_LAST(&c->prompt_hdata), c->prompt_buffer) == 0)
                    910:                return;
                    911:
                    912:        if (ARRAY_LENGTH(&c->prompt_hdata) == PROMPT_HISTORY) {
                    913:                xfree(ARRAY_FIRST(&c->prompt_hdata));
                    914:                ARRAY_REMOVE(&c->prompt_hdata, 0);
                    915:        }
                    916:
                    917:        ARRAY_ADD(&c->prompt_hdata, xstrdup(c->prompt_buffer));
                    918: }
                    919:
                    920: /* Complete word. */
                    921: char *
                    922: status_prompt_complete(const char *s)
                    923: {
                    924:        const struct cmd_entry        **cmdent;
                    925:        const struct set_option_entry  *optent;
                    926:        ARRAY_DECL(, const char *)      list;
                    927:        char                           *prefix, *s2;
1.9       nicm      928:        u_int                           i;
1.1       nicm      929:        size_t                          j;
                    930:
                    931:        if (*s == '\0')
                    932:                return (NULL);
                    933:
                    934:        /* First, build a list of all the possible matches. */
                    935:        ARRAY_INIT(&list);
                    936:        for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
                    937:                if (strncmp((*cmdent)->name, s, strlen(s)) == 0)
                    938:                        ARRAY_ADD(&list, (*cmdent)->name);
                    939:        }
1.9       nicm      940:        for (optent = set_option_table; optent->name != NULL; optent++) {
1.1       nicm      941:                if (strncmp(optent->name, s, strlen(s)) == 0)
                    942:                        ARRAY_ADD(&list, optent->name);
                    943:        }
1.9       nicm      944:        for (optent = set_window_option_table; optent->name != NULL; optent++) {
1.1       nicm      945:                if (strncmp(optent->name, s, strlen(s)) == 0)
                    946:                        ARRAY_ADD(&list, optent->name);
                    947:        }
                    948:
                    949:        /* If none, bail now. */
                    950:        if (ARRAY_LENGTH(&list) == 0) {
                    951:                ARRAY_FREE(&list);
                    952:                return (NULL);
                    953:        }
                    954:
                    955:        /* If an exact match, return it, with a trailing space. */
                    956:        if (ARRAY_LENGTH(&list) == 1) {
                    957:                xasprintf(&s2, "%s ", ARRAY_FIRST(&list));
                    958:                ARRAY_FREE(&list);
                    959:                return (s2);
                    960:        }
                    961:
                    962:        /* Now loop through the list and find the longest common prefix. */
                    963:        prefix = xstrdup(ARRAY_FIRST(&list));
                    964:        for (i = 1; i < ARRAY_LENGTH(&list); i++) {
                    965:                s = ARRAY_ITEM(&list, i);
                    966:
                    967:                j = strlen(s);
                    968:                if (j > strlen(prefix))
                    969:                        j = strlen(prefix);
                    970:                for (; j > 0; j--) {
                    971:                        if (prefix[j - 1] != s[j - 1])
                    972:                                prefix[j - 1] = '\0';
                    973:                }
                    974:        }
                    975:
                    976:        ARRAY_FREE(&list);
                    977:        return (prefix);
                    978: }