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

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