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

1.106   ! nicm        1: /* $OpenBSD: status.c,v 1.105 2013/07/05 14:38:22 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:
1.48      nicm       32: char   *status_redraw_get_left(
                     33:            struct client *, time_t, int, struct grid_cell *, size_t *);
                     34: char   *status_redraw_get_right(
                     35:            struct client *, time_t, int, struct grid_cell *, size_t *);
1.71      nicm       36: char   *status_find_job(struct client *, char **);
                     37: void   status_job_free(void *);
1.38      nicm       38: void   status_job_callback(struct job *);
1.47      nicm       39: char   *status_print(
1.55      nicm       40:            struct client *, struct winlink *, time_t, struct grid_cell *);
1.104     nicm       41: void   status_replace1(struct client *, char **, char **, char *, size_t, int);
1.42      nicm       42: void   status_message_callback(int, short, void *);
1.1       nicm       43:
1.65      nicm       44: const char *status_prompt_up_history(u_int *);
                     45: const char *status_prompt_down_history(u_int *);
                     46: void   status_prompt_add_history(const char *);
1.1       nicm       47: char   *status_prompt_complete(const char *);
                     48:
1.65      nicm       49: /* Status prompt history. */
                     50: ARRAY_DECL(, char *) status_prompt_history = ARRAY_INITIALIZER;
                     51:
1.71      nicm       52: /* Status output tree. */
                     53: RB_GENERATE(status_out_tree, status_out, entry, status_out_cmp);
                     54:
                     55: /* Output tree comparison function. */
                     56: int
                     57: status_out_cmp(struct status_out *so1, struct status_out *so2)
                     58: {
                     59:        return (strcmp(so1->cmd, so2->cmd));
1.87      nicm       60: }
                     61:
                     62: /* Get screen line of status line. -1 means off. */
                     63: int
                     64: status_at_line(struct client *c)
                     65: {
                     66:        struct session  *s = c->session;
                     67:
                     68:        if (!options_get_number(&s->options, "status"))
                     69:                return (-1);
                     70:
                     71:        if (options_get_number(&s->options, "status-position") == 0)
                     72:                return (0);
                     73:        return (c->tty.sy - 1);
1.71      nicm       74: }
                     75:
1.48      nicm       76: /* Retrieve options for left string. */
                     77: char *
                     78: status_redraw_get_left(struct client *c,
                     79:     time_t t, int utf8flag, struct grid_cell *gc, size_t *size)
                     80: {
                     81:        struct session  *s = c->session;
                     82:        char            *left;
1.92      nicm       83:        int              fg, bg, attr;
1.48      nicm       84:        size_t           leftlen;
                     85:
                     86:        fg = options_get_number(&s->options, "status-left-fg");
                     87:        if (fg != 8)
                     88:                colour_set_fg(gc, fg);
                     89:        bg = options_get_number(&s->options, "status-left-bg");
                     90:        if (bg != 8)
                     91:                colour_set_bg(gc, bg);
                     92:        attr = options_get_number(&s->options, "status-left-attr");
                     93:        if (attr != 0)
                     94:                gc->attr = attr;
                     95:
1.72      nicm       96:        left = status_replace(c, NULL,
                     97:            NULL, NULL, options_get_string(&s->options, "status-left"), t, 1);
1.48      nicm       98:
                     99:        *size = options_get_number(&s->options, "status-left-length");
                    100:        leftlen = screen_write_cstrlen(utf8flag, "%s", left);
                    101:        if (leftlen < *size)
                    102:                *size = leftlen;
                    103:        return (left);
                    104: }
                    105:
                    106: /* Retrieve options for right string. */
                    107: char *
                    108: status_redraw_get_right(struct client *c,
                    109:     time_t t, int utf8flag, struct grid_cell *gc, size_t *size)
                    110: {
                    111:        struct session  *s = c->session;
                    112:        char            *right;
1.92      nicm      113:        int              fg, bg, attr;
1.48      nicm      114:        size_t           rightlen;
                    115:
                    116:        fg = options_get_number(&s->options, "status-right-fg");
                    117:        if (fg != 8)
                    118:                colour_set_fg(gc, fg);
                    119:        bg = options_get_number(&s->options, "status-right-bg");
                    120:        if (bg != 8)
                    121:                colour_set_bg(gc, bg);
                    122:        attr = options_get_number(&s->options, "status-right-attr");
                    123:        if (attr != 0)
                    124:                gc->attr = attr;
                    125:
1.72      nicm      126:        right = status_replace(c, NULL,
                    127:            NULL, NULL, options_get_string(&s->options, "status-right"), t, 1);
1.48      nicm      128:
                    129:        *size = options_get_number(&s->options, "status-right-length");
                    130:        rightlen = screen_write_cstrlen(utf8flag, "%s", right);
                    131:        if (rightlen < *size)
                    132:                *size = rightlen;
                    133:        return (right);
                    134: }
                    135:
1.73      nicm      136: /* Set window at window list position. */
                    137: void
                    138: status_set_window_at(struct client *c, u_int x)
                    139: {
                    140:        struct session  *s = c->session;
                    141:        struct winlink  *wl;
                    142:
1.88      nicm      143:        x += c->wlmouse;
1.73      nicm      144:        RB_FOREACH(wl, winlinks, &s->windows) {
                    145:                if (x < wl->status_width &&
                    146:                        session_select(s, wl->idx) == 0) {
                    147:                        server_redraw_session(s);
                    148:                }
                    149:                x -= wl->status_width + 1;
                    150:        }
                    151: }
                    152:
1.1       nicm      153: /* Draw status for client on the last lines of given context. */
                    154: int
                    155: status_redraw(struct client *c)
                    156: {
1.48      nicm      157:        struct screen_write_ctx ctx;
                    158:        struct session         *s = c->session;
                    159:        struct winlink         *wl;
                    160:        struct screen           old_status, window_list;
                    161:        struct grid_cell        stdgc, lgc, rgc, gc;
1.91      nicm      162:        struct options         *oo;
1.48      nicm      163:        time_t                  t;
1.91      nicm      164:        char                   *left, *right, *sep;
1.48      nicm      165:        u_int                   offset, needed;
                    166:        u_int                   wlstart, wlwidth, wlavailable, wloffset, wlsize;
1.91      nicm      167:        size_t                  llen, rlen, seplen;
1.48      nicm      168:        int                     larrow, rarrow, utf8flag;
1.1       nicm      169:
1.49      nicm      170:        /* No status line? */
1.7       nicm      171:        if (c->tty.sy == 0 || !options_get_number(&s->options, "status"))
                    172:                return (1);
1.48      nicm      173:        left = right = NULL;
1.7       nicm      174:        larrow = rarrow = 0;
1.1       nicm      175:
1.48      nicm      176:        /* Update status timer. */
1.1       nicm      177:        if (gettimeofday(&c->status_timer, NULL) != 0)
1.34      nicm      178:                fatal("gettimeofday failed");
1.48      nicm      179:        t = c->status_timer.tv_sec;
                    180:
                    181:        /* Set up default colour. */
1.1       nicm      182:        memcpy(&stdgc, &grid_default_cell, sizeof gc);
1.33      nicm      183:        colour_set_fg(&stdgc, options_get_number(&s->options, "status-fg"));
                    184:        colour_set_bg(&stdgc, options_get_number(&s->options, "status-bg"));
1.1       nicm      185:        stdgc.attr |= options_get_number(&s->options, "status-attr");
                    186:
1.48      nicm      187:        /* Create the target screen. */
                    188:        memcpy(&old_status, &c->status, sizeof old_status);
                    189:        screen_init(&c->status, c->tty.sx, 1, 0);
                    190:        screen_write_start(&ctx, NULL, &c->status);
                    191:        for (offset = 0; offset < c->tty.sx; offset++)
                    192:                screen_write_putc(&ctx, &stdgc, ' ');
                    193:        screen_write_stop(&ctx);
                    194:
                    195:        /* If the height is one line, blank status line. */
                    196:        if (c->tty.sy <= 1)
                    197:                goto out;
1.1       nicm      198:
1.48      nicm      199:        /* Get UTF-8 flag. */
1.3       nicm      200:        utf8flag = options_get_number(&s->options, "status-utf8");
                    201:
1.48      nicm      202:        /* Work out left and right strings. */
                    203:        memcpy(&lgc, &stdgc, sizeof lgc);
                    204:        left = status_redraw_get_left(c, t, utf8flag, &lgc, &llen);
                    205:        memcpy(&rgc, &stdgc, sizeof rgc);
                    206:        right = status_redraw_get_right(c, t, utf8flag, &rgc, &rlen);
1.1       nicm      207:
                    208:        /*
1.48      nicm      209:         * Figure out how much space we have for the window list. If there
                    210:         * isn't enough space, just show a blank status line.
1.1       nicm      211:         */
1.55      nicm      212:        needed = 0;
1.1       nicm      213:        if (llen != 0)
1.48      nicm      214:                needed += llen + 1;
1.1       nicm      215:        if (rlen != 0)
1.48      nicm      216:                needed += rlen + 1;
                    217:        if (c->tty.sx == 0 || c->tty.sx <= needed)
                    218:                goto out;
                    219:        wlavailable = c->tty.sx - needed;
1.1       nicm      220:
1.48      nicm      221:        /* Calculate the total size needed for the window list. */
                    222:        wlstart = wloffset = wlwidth = 0;
1.1       nicm      223:        RB_FOREACH(wl, winlinks, &s->windows) {
1.94      nicm      224:                free(wl->status_text);
1.48      nicm      225:                memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell);
                    226:                wl->status_text = status_print(c, wl, t, &wl->status_cell);
1.55      nicm      227:                wl->status_width =
1.48      nicm      228:                    screen_write_cstrlen(utf8flag, "%s", wl->status_text);
                    229:
1.1       nicm      230:                if (wl == s->curw)
1.48      nicm      231:                        wloffset = wlwidth;
1.91      nicm      232:
                    233:                oo = &wl->window->options;
                    234:                sep = options_get_string(oo, "window-status-separator");
                    235:                seplen = screen_write_strlen(utf8flag, "%s", sep);
                    236:                wlwidth += wl->status_width + seplen;
1.48      nicm      237:        }
                    238:
                    239:        /* Create a new screen for the window list. */
                    240:        screen_init(&window_list, wlwidth, 1, 0);
                    241:
                    242:        /* And draw the window list into it. */
                    243:        screen_write_start(&ctx, NULL, &window_list);
                    244:        RB_FOREACH(wl, winlinks, &s->windows) {
1.55      nicm      245:                screen_write_cnputs(&ctx,
1.48      nicm      246:                    -1, &wl->status_cell, utf8flag, "%s", wl->status_text);
1.91      nicm      247:
                    248:                oo = &wl->window->options;
                    249:                sep = options_get_string(oo, "window-status-separator");
                    250:                screen_write_nputs(&ctx, -1, &stdgc, utf8flag, "%s", sep);
1.1       nicm      251:        }
1.48      nicm      252:        screen_write_stop(&ctx);
1.1       nicm      253:
1.48      nicm      254:        /* If there is enough space for the total width, skip to draw now. */
                    255:        if (wlwidth <= wlavailable)
1.1       nicm      256:                goto draw;
                    257:
                    258:        /* Find size of current window text. */
1.48      nicm      259:        wlsize = s->curw->status_width;
1.1       nicm      260:
                    261:        /*
1.48      nicm      262:         * If the current window is already on screen, good to draw from the
1.1       nicm      263:         * start and just leave off the end.
                    264:         */
1.48      nicm      265:        if (wloffset + wlsize < wlavailable) {
                    266:                if (wlavailable > 0) {
                    267:                        rarrow = 1;
                    268:                        wlavailable--;
                    269:                }
                    270:                wlwidth = wlavailable;
                    271:        } else {
                    272:                /*
                    273:                 * Work out how many characters we need to omit from the
                    274:                 * start. There are wlavailable characters to fill, and
                    275:                 * wloffset + wlsize must be the last. So, the start character
                    276:                 * is wloffset + wlsize - wlavailable.
                    277:                 */
                    278:                if (wlavailable > 0) {
                    279:                        larrow = 1;
                    280:                        wlavailable--;
                    281:                }
1.55      nicm      282:
1.48      nicm      283:                wlstart = wloffset + wlsize - wlavailable;
                    284:                if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) {
1.1       nicm      285:                        rarrow = 1;
1.48      nicm      286:                        wlstart++;
                    287:                        wlavailable--;
1.1       nicm      288:                }
1.48      nicm      289:                wlwidth = wlavailable;
                    290:        }
1.1       nicm      291:
1.48      nicm      292:        /* Bail if anything is now too small too. */
                    293:        if (wlwidth == 0 || wlavailable == 0) {
                    294:                screen_free(&window_list);
                    295:                goto out;
1.1       nicm      296:        }
                    297:
                    298:        /*
1.48      nicm      299:         * Now the start position is known, work out the state of the left and
                    300:         * right arrows.
1.1       nicm      301:         */
                    302:        offset = 0;
                    303:        RB_FOREACH(wl, winlinks, &s->windows) {
1.63      nicm      304:                if (wl->flags & WINLINK_ALERTFLAGS &&
                    305:                    larrow == 1 && offset < wlstart)
                    306:                        larrow = -1;
1.1       nicm      307:
1.48      nicm      308:                offset += wl->status_width;
1.1       nicm      309:
1.63      nicm      310:                if (wl->flags & WINLINK_ALERTFLAGS &&
                    311:                    rarrow == 1 && offset > wlstart + wlwidth)
                    312:                        rarrow = -1;
1.1       nicm      313:        }
                    314:
1.48      nicm      315: draw:
1.55      nicm      316:        /* Begin drawing. */
1.48      nicm      317:        screen_write_start(&ctx, NULL, &c->status);
1.1       nicm      318:
1.48      nicm      319:        /* Draw the left string and arrow. */
                    320:        screen_write_cursormove(&ctx, 0, 0);
                    321:        if (llen != 0) {
                    322:                screen_write_cnputs(&ctx, llen, &lgc, utf8flag, "%s", left);
1.5       nicm      323:                screen_write_putc(&ctx, &stdgc, ' ');
1.1       nicm      324:        }
                    325:        if (larrow != 0) {
                    326:                memcpy(&gc, &stdgc, sizeof gc);
                    327:                if (larrow == -1)
                    328:                        gc.attr ^= GRID_ATTR_REVERSE;
                    329:                screen_write_putc(&ctx, &gc, '<');
                    330:        }
1.48      nicm      331:
                    332:        /* Draw the right string and arrow. */
1.1       nicm      333:        if (rarrow != 0) {
1.48      nicm      334:                screen_write_cursormove(&ctx, c->tty.sx - rlen - 2, 0);
1.1       nicm      335:                memcpy(&gc, &stdgc, sizeof gc);
                    336:                if (rarrow == -1)
                    337:                        gc.attr ^= GRID_ATTR_REVERSE;
                    338:                screen_write_putc(&ctx, &gc, '>');
1.48      nicm      339:        } else
                    340:                screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0);
                    341:        if (rlen != 0) {
                    342:                screen_write_putc(&ctx, &stdgc, ' ');
                    343:                screen_write_cnputs(&ctx, rlen, &rgc, utf8flag, "%s", right);
1.1       nicm      344:        }
                    345:
1.48      nicm      346:        /* Figure out the offset for the window list. */
1.58      nicm      347:        if (llen != 0)
                    348:                wloffset = llen + 1;
                    349:        else
                    350:                wloffset = 0;
1.48      nicm      351:        if (wlwidth < wlavailable) {
                    352:                switch (options_get_number(&s->options, "status-justify")) {
                    353:                case 1: /* centered */
1.58      nicm      354:                        wloffset += (wlavailable - wlwidth) / 2;
1.48      nicm      355:                        break;
                    356:                case 2: /* right */
1.58      nicm      357:                        wloffset += (wlavailable - wlwidth);
1.48      nicm      358:                        break;
                    359:                }
                    360:        }
                    361:        if (larrow != 0)
                    362:                wloffset++;
                    363:
                    364:        /* Copy the window list. */
1.88      nicm      365:        c->wlmouse = -wloffset + wlstart;
1.48      nicm      366:        screen_write_cursormove(&ctx, wloffset, 0);
                    367:        screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1);
1.55      nicm      368:        screen_free(&window_list);
1.1       nicm      369:
1.48      nicm      370:        screen_write_stop(&ctx);
1.1       nicm      371:
                    372: out:
1.94      nicm      373:        free(left);
                    374:        free(right);
1.1       nicm      375:
                    376:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    377:                screen_free(&old_status);
                    378:                return (0);
                    379:        }
                    380:        screen_free(&old_status);
                    381:        return (1);
                    382: }
                    383:
1.46      nicm      384: /* Replace a single special sequence (prefixed by #). */
                    385: void
1.104     nicm      386: status_replace1(struct client *c, char **iptr, char **optr, char *out,
1.72      nicm      387:     size_t outsize, int jobsflag)
                    388: {
1.104     nicm      389:        char    ch, tmp[256], *ptr, *endptr;
1.72      nicm      390:        size_t  ptrlen;
                    391:        long    limit;
1.46      nicm      392:
                    393:        errno = 0;
                    394:        limit = strtol(*iptr, &endptr, 10);
                    395:        if ((limit == 0 && errno != EINVAL) ||
                    396:            (limit == LONG_MIN && errno != ERANGE) ||
1.55      nicm      397:            (limit == LONG_MAX && errno != ERANGE) ||
1.46      nicm      398:            limit != 0)
                    399:                *iptr = endptr;
                    400:        if (limit <= 0)
                    401:                limit = LONG_MAX;
                    402:
                    403:        switch (*(*iptr)++) {
                    404:        case '(':
                    405:                if (!jobsflag) {
                    406:                        ch = ')';
                    407:                        goto skip_to;
                    408:                }
1.71      nicm      409:                if ((ptr = status_find_job(c, iptr)) == NULL)
1.46      nicm      410:                        return;
                    411:                goto do_replace;
                    412:        case '[':
1.55      nicm      413:                /*
1.46      nicm      414:                 * Embedded style, handled at display time. Leave present and
                    415:                 * skip input until ].
                    416:                 */
                    417:                ch = ']';
                    418:                goto skip_to;
1.96      nicm      419:        case '{':
                    420:                ptr = (char *) "#{";
                    421:                goto do_replace;
1.46      nicm      422:        case '#':
1.49      nicm      423:                *(*optr)++ = '#';
1.46      nicm      424:                break;
1.104     nicm      425:        default:
                    426:                xsnprintf(tmp, sizeof tmp, "#%c", *(*iptr - 1));
                    427:                ptr = tmp;
                    428:                goto do_replace;
1.46      nicm      429:        }
                    430:
                    431:        return;
1.55      nicm      432:
1.46      nicm      433: do_replace:
                    434:        ptrlen = strlen(ptr);
                    435:        if ((size_t) limit < ptrlen)
                    436:                ptrlen = limit;
                    437:
                    438:        if (*optr + ptrlen >= out + outsize - 1)
1.104     nicm      439:                return;
1.46      nicm      440:        while (ptrlen > 0 && *ptr != '\0') {
                    441:                *(*optr)++ = *ptr++;
                    442:                ptrlen--;
                    443:        }
                    444:
                    445:        return;
                    446:
                    447: skip_to:
                    448:        *(*optr)++ = '#';
                    449:
                    450:        (*iptr)--;      /* include ch */
                    451:        while (**iptr != ch && **iptr != '\0') {
                    452:                if (*optr >=  out + outsize - 1)
                    453:                        break;
                    454:                *(*optr)++ = *(*iptr)++;
                    455:        }
                    456: }
                    457:
                    458: /* Replace special sequences in fmt. */
                    459: char *
1.72      nicm      460: status_replace(struct client *c, struct session *s, struct winlink *wl,
                    461:     struct window_pane *wp, const char *fmt, time_t t, int jobsflag)
1.46      nicm      462: {
1.96      nicm      463:        static char              out[BUFSIZ];
1.98      nicm      464:        char                     in[BUFSIZ], ch, *iptr, *optr, *expanded;
1.96      nicm      465:        size_t                   len;
                    466:        struct format_tree      *ft;
1.47      nicm      467:
1.93      nicm      468:        if (fmt == NULL)
                    469:                return (xstrdup(""));
                    470:
1.96      nicm      471:        if (s == NULL)
                    472:                s = c->session;
                    473:        if (wl == NULL)
                    474:                wl = s->curw;
                    475:        if (wp == NULL)
                    476:                wp = wl->window->active;
                    477:
1.86      nicm      478:        len = strftime(in, sizeof in, fmt, localtime(&t));
                    479:        in[len] = '\0';
1.1       nicm      480:
                    481:        iptr = in;
                    482:        optr = out;
                    483:
                    484:        while (*iptr != '\0') {
                    485:                if (optr >= out + (sizeof out) - 1)
                    486:                        break;
1.46      nicm      487:                ch = *iptr++;
                    488:
1.70      nicm      489:                if (ch != '#' || *iptr == '\0') {
1.1       nicm      490:                        *optr++ = ch;
1.46      nicm      491:                        continue;
1.1       nicm      492:                }
1.104     nicm      493:                status_replace1(c, &iptr, &optr, out, sizeof out, jobsflag);
1.1       nicm      494:        }
                    495:        *optr = '\0';
                    496:
1.96      nicm      497:        ft = format_create();
1.97      nicm      498:        format_client(ft, c);
1.96      nicm      499:        format_session(ft, s);
                    500:        format_winlink(ft, s, wl);
                    501:        format_window_pane(ft, wp);
1.99      nicm      502:        expanded = format_expand(ft, out);
                    503:        format_free(ft);
                    504:        return (expanded);
1.1       nicm      505: }
                    506:
1.46      nicm      507: /* Figure out job name and get its result, starting it off if necessary. */
1.1       nicm      508: char *
1.71      nicm      509: status_find_job(struct client *c, char **iptr)
1.1       nicm      510: {
1.71      nicm      511:        struct status_out       *so, so_find;
                    512:        char                    *cmd;
                    513:        int                      lastesc;
                    514:        size_t                   len;
1.1       nicm      515:
                    516:        if (**iptr == '\0')
                    517:                return (NULL);
                    518:        if (**iptr == ')') {            /* no command given */
                    519:                (*iptr)++;
                    520:                return (NULL);
                    521:        }
                    522:
                    523:        cmd = xmalloc(strlen(*iptr) + 1);
                    524:        len = 0;
                    525:
                    526:        lastesc = 0;
                    527:        for (; **iptr != '\0'; (*iptr)++) {
                    528:                if (!lastesc && **iptr == ')')
                    529:                        break;          /* unescaped ) is the end */
                    530:                if (!lastesc && **iptr == '\\') {
                    531:                        lastesc = 1;
                    532:                        continue;       /* skip \ if not escaped */
                    533:                }
                    534:                lastesc = 0;
                    535:                cmd[len++] = **iptr;
                    536:        }
1.38      nicm      537:        if (**iptr == '\0')             /* no terminating ) */ {
1.94      nicm      538:                free(cmd);
1.38      nicm      539:                return (NULL);
                    540:        }
1.1       nicm      541:        (*iptr)++;                      /* skip final ) */
                    542:        cmd[len] = '\0';
                    543:
1.71      nicm      544:        /* First try in the new tree. */
                    545:        so_find.cmd = cmd;
                    546:        so = RB_FIND(status_out_tree, &c->status_new, &so_find);
1.78      nicm      547:        if (so != NULL && so->out != NULL) {
1.94      nicm      548:                free(cmd);
1.71      nicm      549:                return (so->out);
1.78      nicm      550:        }
1.71      nicm      551:
                    552:        /* If not found at all, start the job and add to the tree. */
                    553:        if (so == NULL) {
1.103     nicm      554:                job_run(cmd, NULL, status_job_callback, status_job_free, c);
1.71      nicm      555:                c->references++;
                    556:
                    557:                so = xmalloc(sizeof *so);
                    558:                so->cmd = xstrdup(cmd);
                    559:                so->out = NULL;
                    560:                RB_INSERT(status_out_tree, &c->status_new, so);
                    561:        }
                    562:
                    563:        /* Lookup in the old tree. */
                    564:        so_find.cmd = cmd;
                    565:        so = RB_FIND(status_out_tree, &c->status_old, &so_find);
1.94      nicm      566:        free(cmd);
1.71      nicm      567:        if (so != NULL)
                    568:                return (so->out);
                    569:        return (NULL);
                    570: }
                    571:
                    572: /* Free job tree. */
                    573: void
                    574: status_free_jobs(struct status_out_tree *sotree)
                    575: {
                    576:        struct status_out       *so, *so_next;
                    577:
                    578:        so_next = RB_MIN(status_out_tree, sotree);
                    579:        while (so_next != NULL) {
                    580:                so = so_next;
                    581:                so_next = RB_NEXT(status_out_tree, sotree, so);
                    582:
                    583:                RB_REMOVE(status_out_tree, sotree, so);
1.94      nicm      584:                free(so->out);
                    585:                free(so->cmd);
                    586:                free(so);
1.38      nicm      587:        }
1.71      nicm      588: }
                    589:
                    590: /* Update jobs on status interval. */
                    591: void
                    592: status_update_jobs(struct client *c)
                    593: {
                    594:        /* Free the old tree. */
                    595:        status_free_jobs(&c->status_old);
                    596:
                    597:        /* Move the new to old. */
                    598:        memcpy(&c->status_old, &c->status_new, sizeof c->status_old);
                    599:        RB_INIT(&c->status_new);
                    600: }
                    601:
                    602: /* Free status job. */
                    603: void
                    604: status_job_free(void *data)
                    605: {
                    606:        struct client   *c = data;
                    607:
                    608:        c->references--;
1.38      nicm      609: }
1.1       nicm      610:
1.46      nicm      611: /* Job has finished: save its result. */
1.38      nicm      612: void
                    613: status_job_callback(struct job *job)
                    614: {
1.71      nicm      615:        struct client           *c = job->data;
                    616:        struct status_out       *so, so_find;
                    617:        char                    *line, *buf;
                    618:        size_t                   len;
                    619:
                    620:        if (c->flags & CLIENT_DEAD)
                    621:                return;
                    622:
                    623:        so_find.cmd = job->cmd;
                    624:        so = RB_FIND(status_out_tree, &c->status_new, &so_find);
                    625:        if (so == NULL || so->out != NULL)
                    626:                return;
1.38      nicm      627:
1.41      nicm      628:        buf = NULL;
                    629:        if ((line = evbuffer_readline(job->event->input)) == NULL) {
                    630:                len = EVBUFFER_LENGTH(job->event->input);
                    631:                buf = xmalloc(len + 1);
                    632:                if (len != 0)
                    633:                        memcpy(buf, EVBUFFER_DATA(job->event->input), len);
                    634:                buf[len] = '\0';
1.71      nicm      635:        } else
1.102     nicm      636:                buf = line;
1.1       nicm      637:
1.71      nicm      638:        so->out = buf;
1.75      nicm      639:        server_status_client(c);
1.1       nicm      640: }
                    641:
1.46      nicm      642: /* Return winlink status line entry and adjust gc as necessary. */
1.1       nicm      643: char *
1.47      nicm      644: status_print(
                    645:     struct client *c, struct winlink *wl, time_t t, struct grid_cell *gc)
1.1       nicm      646: {
1.33      nicm      647:        struct options  *oo = &wl->window->options;
1.47      nicm      648:        struct session  *s = c->session;
                    649:        const char      *fmt;
                    650:        char            *text;
1.92      nicm      651:        int              fg, bg, attr;
1.1       nicm      652:
1.33      nicm      653:        fg = options_get_number(oo, "window-status-fg");
1.1       nicm      654:        if (fg != 8)
1.33      nicm      655:                colour_set_fg(gc, fg);
                    656:        bg = options_get_number(oo, "window-status-bg");
1.1       nicm      657:        if (bg != 8)
1.33      nicm      658:                colour_set_bg(gc, bg);
                    659:        attr = options_get_number(oo, "window-status-attr");
1.1       nicm      660:        if (attr != 0)
                    661:                gc->attr = attr;
1.47      nicm      662:        fmt = options_get_string(oo, "window-status-format");
1.14      nicm      663:        if (wl == s->curw) {
1.33      nicm      664:                fg = options_get_number(oo, "window-status-current-fg");
1.14      nicm      665:                if (fg != 8)
1.33      nicm      666:                        colour_set_fg(gc, fg);
                    667:                bg = options_get_number(oo, "window-status-current-bg");
1.14      nicm      668:                if (bg != 8)
1.33      nicm      669:                        colour_set_bg(gc, bg);
                    670:                attr = options_get_number(oo, "window-status-current-attr");
1.14      nicm      671:                if (attr != 0)
                    672:                        gc->attr = attr;
1.47      nicm      673:                fmt = options_get_string(oo, "window-status-current-format");
1.95      nicm      674:        }
                    675:        if (wl == TAILQ_FIRST(&s->lastw)) {
                    676:                fg = options_get_number(oo, "window-status-last-fg");
                    677:                if (fg != 8)
                    678:                        colour_set_fg(gc, fg);
                    679:                bg = options_get_number(oo, "window-status-last-bg");
                    680:                if (bg != 8)
                    681:                        colour_set_bg(gc, bg);
                    682:                attr = options_get_number(oo, "window-status-last-attr");
                    683:                if (attr != 0)
                    684:                        gc->attr = attr;
1.14      nicm      685:        }
1.1       nicm      686:
1.84      nicm      687:        if (wl->flags & WINLINK_BELL) {
                    688:                fg = options_get_number(oo, "window-status-bell-fg");
1.62      nicm      689:                if (fg != 8)
                    690:                        colour_set_fg(gc, fg);
1.84      nicm      691:                bg = options_get_number(oo, "window-status-bell-bg");
1.62      nicm      692:                if (bg != 8)
                    693:                        colour_set_bg(gc, bg);
1.84      nicm      694:                attr = options_get_number(oo, "window-status-bell-attr");
                    695:                if (attr != 0)
                    696:                        gc->attr = attr;
                    697:        } else if (wl->flags & WINLINK_CONTENT) {
                    698:                fg = options_get_number(oo, "window-status-content-fg");
                    699:                if (fg != 8)
                    700:                        colour_set_fg(gc, fg);
                    701:                bg = options_get_number(oo, "window-status-content-bg");
                    702:                if (bg != 8)
                    703:                        colour_set_bg(gc, bg);
                    704:                attr = options_get_number(oo, "window-status-content-attr");
                    705:                if (attr != 0)
                    706:                        gc->attr = attr;
                    707:        } else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE)) {
                    708:                fg = options_get_number(oo, "window-status-activity-fg");
                    709:                if (fg != 8)
                    710:                        colour_set_fg(gc, fg);
                    711:                bg = options_get_number(oo, "window-status-activity-bg");
                    712:                if (bg != 8)
                    713:                        colour_set_bg(gc, bg);
                    714:                attr = options_get_number(oo, "window-status-activity-attr");
1.62      nicm      715:                if (attr != 0)
                    716:                        gc->attr = attr;
                    717:        }
1.1       nicm      718:
1.72      nicm      719:        text = status_replace(c, NULL, wl, NULL, fmt, t, 1);
1.1       nicm      720:        return (text);
                    721: }
                    722:
1.46      nicm      723: /* Set a status line message. */
1.10      nicm      724: void printflike2
                    725: status_message_set(struct client *c, const char *fmt, ...)
1.1       nicm      726: {
1.44      nicm      727:        struct timeval           tv;
                    728:        struct session          *s = c->session;
                    729:        struct message_entry    *msg;
                    730:        va_list                  ap;
                    731:        int                      delay;
                    732:        u_int                    i, limit;
1.1       nicm      733:
1.12      nicm      734:        status_prompt_clear(c);
                    735:        status_message_clear(c);
                    736:
1.28      nicm      737:        va_start(ap, fmt);
                    738:        xvasprintf(&c->message_string, fmt, ap);
                    739:        va_end(ap);
                    740:
1.44      nicm      741:        ARRAY_EXPAND(&c->message_log, 1);
                    742:        msg = &ARRAY_LAST(&c->message_log);
                    743:        msg->msg_time = time(NULL);
                    744:        msg->msg = xstrdup(c->message_string);
                    745:
                    746:        if (s == NULL)
                    747:                limit = 0;
                    748:        else
                    749:                limit = options_get_number(&s->options, "message-limit");
1.50      nicm      750:        if (ARRAY_LENGTH(&c->message_log) > limit) {
                    751:                limit = ARRAY_LENGTH(&c->message_log) - limit;
                    752:                for (i = 0; i < limit; i++) {
                    753:                        msg = &ARRAY_FIRST(&c->message_log);
1.94      nicm      754:                        free(msg->msg);
1.50      nicm      755:                        ARRAY_REMOVE(&c->message_log, 0);
                    756:                }
1.44      nicm      757:        }
                    758:
1.1       nicm      759:        delay = options_get_number(&c->session->options, "display-time");
                    760:        tv.tv_sec = delay / 1000;
                    761:        tv.tv_usec = (delay % 1000) * 1000L;
1.44      nicm      762:
1.90      nicm      763:        if (event_initialized (&c->message_timer))
                    764:                evtimer_del(&c->message_timer);
1.42      nicm      765:        evtimer_set(&c->message_timer, status_message_callback, c);
                    766:        evtimer_add(&c->message_timer, &tv);
1.1       nicm      767:
                    768:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
                    769:        c->flags |= CLIENT_STATUS;
                    770: }
                    771:
1.46      nicm      772: /* Clear status line message. */
1.1       nicm      773: void
                    774: status_message_clear(struct client *c)
                    775: {
                    776:        if (c->message_string == NULL)
                    777:                return;
                    778:
1.94      nicm      779:        free(c->message_string);
1.1       nicm      780:        c->message_string = NULL;
                    781:
                    782:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12      nicm      783:        c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7       nicm      784:
                    785:        screen_reinit(&c->status);
1.42      nicm      786: }
                    787:
1.46      nicm      788: /* Clear status line message after timer expires. */
1.42      nicm      789: void
                    790: status_message_callback(unused int fd, unused short event, void *data)
                    791: {
                    792:        struct client   *c = data;
                    793:
                    794:        status_message_clear(c);
1.1       nicm      795: }
                    796:
                    797: /* Draw client message on status line of present else on last line. */
                    798: int
                    799: status_message_redraw(struct client *c)
                    800: {
                    801:        struct screen_write_ctx         ctx;
                    802:        struct session                 *s = c->session;
                    803:        struct screen                   old_status;
                    804:        size_t                          len;
                    805:        struct grid_cell                gc;
1.51      nicm      806:        int                             utf8flag;
1.1       nicm      807:
                    808:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    809:                return (0);
                    810:        memcpy(&old_status, &c->status, sizeof old_status);
                    811:        screen_init(&c->status, c->tty.sx, 1, 0);
                    812:
1.51      nicm      813:        utf8flag = options_get_number(&s->options, "status-utf8");
                    814:
                    815:        len = screen_write_strlen(utf8flag, "%s", c->message_string);
1.1       nicm      816:        if (len > c->tty.sx)
                    817:                len = c->tty.sx;
                    818:
                    819:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.33      nicm      820:        colour_set_fg(&gc, options_get_number(&s->options, "message-fg"));
                    821:        colour_set_bg(&gc, options_get_number(&s->options, "message-bg"));
1.1       nicm      822:        gc.attr |= options_get_number(&s->options, "message-attr");
                    823:
                    824:        screen_write_start(&ctx, NULL, &c->status);
                    825:
                    826:        screen_write_cursormove(&ctx, 0, 0);
1.51      nicm      827:        screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->message_string);
1.1       nicm      828:        for (; len < c->tty.sx; len++)
                    829:                screen_write_putc(&ctx, &gc, ' ');
                    830:
                    831:        screen_write_stop(&ctx);
                    832:
                    833:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    834:                screen_free(&old_status);
                    835:                return (0);
                    836:        }
                    837:        screen_free(&old_status);
                    838:        return (1);
                    839: }
                    840:
1.46      nicm      841: /* Enable status line prompt. */
1.1       nicm      842: void
1.76      nicm      843: status_prompt_set(struct client *c, const char *msg, const char *input,
1.12      nicm      844:     int (*callbackfn)(void *, const char *), void (*freefn)(void *),
                    845:     void *data, int flags)
1.1       nicm      846: {
1.20      nicm      847:        int     keys;
                    848:
1.12      nicm      849:        status_message_clear(c);
                    850:        status_prompt_clear(c);
                    851:
1.77      nicm      852:        c->prompt_string = status_replace(c, NULL, NULL, NULL, msg,
                    853:            time(NULL), 0);
1.1       nicm      854:
1.77      nicm      855:        c->prompt_buffer = status_replace(c, NULL, NULL, NULL, input,
                    856:            time(NULL), 0);
1.76      nicm      857:        c->prompt_index = strlen(c->prompt_buffer);
1.1       nicm      858:
1.12      nicm      859:        c->prompt_callbackfn = callbackfn;
                    860:        c->prompt_freefn = freefn;
1.1       nicm      861:        c->prompt_data = data;
                    862:
                    863:        c->prompt_hindex = 0;
                    864:
                    865:        c->prompt_flags = flags;
                    866:
1.20      nicm      867:        keys = options_get_number(&c->session->options, "status-keys");
                    868:        if (keys == MODEKEY_EMACS)
1.21      nicm      869:                mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit);
1.20      nicm      870:        else
1.21      nicm      871:                mode_key_init(&c->prompt_mdata, &mode_key_tree_vi_edit);
1.1       nicm      872:
                    873:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
                    874:        c->flags |= CLIENT_STATUS;
                    875: }
                    876:
1.46      nicm      877: /* Remove status line prompt. */
1.1       nicm      878: void
                    879: status_prompt_clear(struct client *c)
                    880: {
1.55      nicm      881:        if (c->prompt_string == NULL)
1.1       nicm      882:                return;
                    883:
1.12      nicm      884:        if (c->prompt_freefn != NULL && c->prompt_data != NULL)
                    885:                c->prompt_freefn(c->prompt_data);
1.1       nicm      886:
1.94      nicm      887:        free(c->prompt_string);
1.1       nicm      888:        c->prompt_string = NULL;
                    889:
1.94      nicm      890:        free(c->prompt_buffer);
1.1       nicm      891:        c->prompt_buffer = NULL;
                    892:
                    893:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12      nicm      894:        c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7       nicm      895:
                    896:        screen_reinit(&c->status);
1.27      nicm      897: }
                    898:
1.46      nicm      899: /* Update status line prompt with a new prompt string. */
1.27      nicm      900: void
1.76      nicm      901: status_prompt_update(struct client *c, const char *msg, const char *input)
1.27      nicm      902: {
1.94      nicm      903:        free(c->prompt_string);
1.77      nicm      904:        c->prompt_string = status_replace(c, NULL, NULL, NULL, msg,
                    905:            time(NULL), 0);
1.27      nicm      906:
1.94      nicm      907:        free(c->prompt_buffer);
1.77      nicm      908:        c->prompt_buffer = status_replace(c, NULL, NULL, NULL, input,
                    909:            time(NULL), 0);
1.76      nicm      910:        c->prompt_index = strlen(c->prompt_buffer);
1.27      nicm      911:
                    912:        c->prompt_hindex = 0;
                    913:
                    914:        c->flags |= CLIENT_STATUS;
1.1       nicm      915: }
                    916:
                    917: /* Draw client prompt on status line of present else on last line. */
                    918: int
                    919: status_prompt_redraw(struct client *c)
                    920: {
1.51      nicm      921:        struct screen_write_ctx         ctx;
1.1       nicm      922:        struct session                 *s = c->session;
                    923:        struct screen                   old_status;
1.29      nicm      924:        size_t                          i, size, left, len, off;
1.51      nicm      925:        struct grid_cell                gc, *gcp;
                    926:        int                             utf8flag;
1.1       nicm      927:
                    928:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    929:                return (0);
                    930:        memcpy(&old_status, &c->status, sizeof old_status);
                    931:        screen_init(&c->status, c->tty.sx, 1, 0);
                    932:
1.51      nicm      933:        utf8flag = options_get_number(&s->options, "status-utf8");
                    934:
                    935:        len = screen_write_strlen(utf8flag, "%s", c->prompt_string);
1.1       nicm      936:        if (len > c->tty.sx)
                    937:                len = c->tty.sx;
1.51      nicm      938:        off = 0;
1.1       nicm      939:
                    940:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.106   ! nicm      941:
1.79      nicm      942:        /* Change colours for command mode. */
                    943:        if (c->prompt_mdata.mode == 1) {
                    944:                colour_set_fg(&gc, options_get_number(&s->options, "message-command-fg"));
                    945:                colour_set_bg(&gc, options_get_number(&s->options, "message-command-bg"));
                    946:                gc.attr |= options_get_number(&s->options, "message-command-attr");
                    947:        } else {
                    948:                colour_set_fg(&gc, options_get_number(&s->options, "message-fg"));
                    949:                colour_set_bg(&gc, options_get_number(&s->options, "message-bg"));
                    950:                gc.attr |= options_get_number(&s->options, "message-attr");
                    951:        }
1.1       nicm      952:
                    953:        screen_write_start(&ctx, NULL, &c->status);
                    954:
                    955:        screen_write_cursormove(&ctx, 0, 0);
1.51      nicm      956:        screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->prompt_string);
1.1       nicm      957:
                    958:        left = c->tty.sx - len;
                    959:        if (left != 0) {
1.51      nicm      960:                size = screen_write_strlen(utf8flag, "%s", c->prompt_buffer);
                    961:                if (c->prompt_index >= left) {
1.17      nicm      962:                        off = c->prompt_index - left + 1;
1.51      nicm      963:                        if (c->prompt_index == size)
1.1       nicm      964:                                left--;
                    965:                        size = left;
                    966:                }
1.51      nicm      967:                screen_write_nputs(
                    968:                    &ctx, left, &gc, utf8flag, "%s", c->prompt_buffer + off);
1.1       nicm      969:
1.55      nicm      970:                for (i = len + size; i < c->tty.sx; i++)
                    971:                        screen_write_putc(&ctx, &gc, ' ');
1.1       nicm      972:        }
                    973:
                    974:        screen_write_stop(&ctx);
1.51      nicm      975:
                    976:        /* Apply fake cursor. */
                    977:        off = len + c->prompt_index - off;
                    978:        gcp = grid_view_get_cell(c->status.grid, off, 0);
                    979:        gcp->attr ^= GRID_ATTR_REVERSE;
1.1       nicm      980:
                    981:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    982:                screen_free(&old_status);
                    983:                return (0);
                    984:        }
                    985:        screen_free(&old_status);
                    986:        return (1);
                    987: }
                    988:
                    989: /* Handle keys in prompt. */
                    990: void
                    991: status_prompt_key(struct client *c, int key)
                    992: {
1.81      nicm      993:        struct session          *sess = c->session;
                    994:        struct options          *oo = &sess->options;
1.1       nicm      995:        struct paste_buffer     *pb;
1.81      nicm      996:        char                    *s, *first, *last, word[64], swapc;
                    997:        const char              *histstr;
1.83      nicm      998:        const char              *wsep = NULL;
1.53      nicm      999:        u_char                   ch;
1.1       nicm     1000:        size_t                   size, n, off, idx;
                   1001:
                   1002:        size = strlen(c->prompt_buffer);
1.101     nicm     1003:        switch (mode_key_lookup(&c->prompt_mdata, key, NULL)) {
1.20      nicm     1004:        case MODEKEYEDIT_CURSORLEFT:
1.1       nicm     1005:                if (c->prompt_index > 0) {
                   1006:                        c->prompt_index--;
                   1007:                        c->flags |= CLIENT_STATUS;
                   1008:                }
                   1009:                break;
1.79      nicm     1010:        case MODEKEYEDIT_SWITCHMODE:
                   1011:                c->flags |= CLIENT_STATUS;
                   1012:                break;
1.20      nicm     1013:        case MODEKEYEDIT_SWITCHMODEAPPEND:
1.79      nicm     1014:                c->flags |= CLIENT_STATUS;
                   1015:                /* FALLTHROUGH */
1.20      nicm     1016:        case MODEKEYEDIT_CURSORRIGHT:
1.1       nicm     1017:                if (c->prompt_index < size) {
                   1018:                        c->prompt_index++;
                   1019:                        c->flags |= CLIENT_STATUS;
                   1020:                }
                   1021:                break;
1.89      nicm     1022:        case MODEKEYEDIT_SWITCHMODEBEGINLINE:
                   1023:                c->flags |= CLIENT_STATUS;
                   1024:                /* FALLTHROUGH */
1.20      nicm     1025:        case MODEKEYEDIT_STARTOFLINE:
1.1       nicm     1026:                if (c->prompt_index != 0) {
                   1027:                        c->prompt_index = 0;
                   1028:                        c->flags |= CLIENT_STATUS;
                   1029:                }
                   1030:                break;
1.89      nicm     1031:        case MODEKEYEDIT_SWITCHMODEAPPENDLINE:
                   1032:                c->flags |= CLIENT_STATUS;
                   1033:                /* FALLTHROUGH */
1.20      nicm     1034:        case MODEKEYEDIT_ENDOFLINE:
1.1       nicm     1035:                if (c->prompt_index != size) {
                   1036:                        c->prompt_index = size;
                   1037:                        c->flags |= CLIENT_STATUS;
                   1038:                }
                   1039:                break;
1.20      nicm     1040:        case MODEKEYEDIT_COMPLETE:
1.1       nicm     1041:                if (*c->prompt_buffer == '\0')
                   1042:                        break;
                   1043:
                   1044:                idx = c->prompt_index;
                   1045:                if (idx != 0)
                   1046:                        idx--;
                   1047:
                   1048:                /* Find the word we are in. */
                   1049:                first = c->prompt_buffer + idx;
                   1050:                while (first > c->prompt_buffer && *first != ' ')
                   1051:                        first--;
                   1052:                while (*first == ' ')
                   1053:                        first++;
                   1054:                last = c->prompt_buffer + idx;
                   1055:                while (*last != '\0' && *last != ' ')
                   1056:                        last++;
                   1057:                while (*last == ' ')
                   1058:                        last--;
                   1059:                if (*last != '\0')
                   1060:                        last++;
                   1061:                if (last <= first ||
                   1062:                    ((size_t) (last - first)) > (sizeof word) - 1)
                   1063:                        break;
                   1064:                memcpy(word, first, last - first);
                   1065:                word[last - first] = '\0';
                   1066:
                   1067:                /* And try to complete it. */
                   1068:                if ((s = status_prompt_complete(word)) == NULL)
                   1069:                        break;
                   1070:
                   1071:                /* Trim out word. */
                   1072:                n = size - (last - c->prompt_buffer) + 1; /* with \0 */
                   1073:                memmove(first, last, n);
                   1074:                size -= last - first;
                   1075:
                   1076:                /* Insert the new word. */
1.55      nicm     1077:                size += strlen(s);
1.1       nicm     1078:                off = first - c->prompt_buffer;
1.55      nicm     1079:                c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 1);
1.1       nicm     1080:                first = c->prompt_buffer + off;
1.55      nicm     1081:                memmove(first + strlen(s), first, n);
                   1082:                memcpy(first, s, strlen(s));
1.1       nicm     1083:
                   1084:                c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1.94      nicm     1085:                free(s);
1.1       nicm     1086:
                   1087:                c->flags |= CLIENT_STATUS;
                   1088:                break;
1.20      nicm     1089:        case MODEKEYEDIT_BACKSPACE:
1.1       nicm     1090:                if (c->prompt_index != 0) {
                   1091:                        if (c->prompt_index == size)
                   1092:                                c->prompt_buffer[--c->prompt_index] = '\0';
                   1093:                        else {
                   1094:                                memmove(c->prompt_buffer + c->prompt_index - 1,
                   1095:                                    c->prompt_buffer + c->prompt_index,
                   1096:                                    size + 1 - c->prompt_index);
                   1097:                                c->prompt_index--;
                   1098:                        }
                   1099:                        c->flags |= CLIENT_STATUS;
                   1100:                }
                   1101:                break;
1.55      nicm     1102:        case MODEKEYEDIT_DELETE:
1.105     nicm     1103:        case MODEKEYEDIT_SWITCHMODESUBSTITUTE:
1.1       nicm     1104:                if (c->prompt_index != size) {
                   1105:                        memmove(c->prompt_buffer + c->prompt_index,
                   1106:                            c->prompt_buffer + c->prompt_index + 1,
                   1107:                            size + 1 - c->prompt_index);
1.18      nicm     1108:                        c->flags |= CLIENT_STATUS;
                   1109:                }
1.26      nicm     1110:                break;
                   1111:        case MODEKEYEDIT_DELETELINE:
1.105     nicm     1112:        case MODEKEYEDIT_SWITCHMODESUBSTITUTELINE:
1.26      nicm     1113:                *c->prompt_buffer = '\0';
                   1114:                c->prompt_index = 0;
                   1115:                c->flags |= CLIENT_STATUS;
1.18      nicm     1116:                break;
1.20      nicm     1117:        case MODEKEYEDIT_DELETETOENDOFLINE:
1.105     nicm     1118:        case MODEKEYEDIT_SWITCHMODECHANGELINE:
1.18      nicm     1119:                if (c->prompt_index < size) {
                   1120:                        c->prompt_buffer[c->prompt_index] = '\0';
1.1       nicm     1121:                        c->flags |= CLIENT_STATUS;
                   1122:                }
                   1123:                break;
1.81      nicm     1124:        case MODEKEYEDIT_DELETEWORD:
                   1125:                wsep = options_get_string(oo, "word-separators");
                   1126:                idx = c->prompt_index;
                   1127:
                   1128:                /* Find a non-separator. */
                   1129:                while (idx != 0) {
                   1130:                        idx--;
                   1131:                        if (!strchr(wsep, c->prompt_buffer[idx]))
                   1132:                                break;
                   1133:                }
                   1134:
                   1135:                /* Find the separator at the beginning of the word. */
                   1136:                while (idx != 0) {
                   1137:                        idx--;
                   1138:                        if (strchr(wsep, c->prompt_buffer[idx])) {
                   1139:                                /* Go back to the word. */
                   1140:                                idx++;
                   1141:                                break;
                   1142:                        }
                   1143:                }
                   1144:
                   1145:                memmove(c->prompt_buffer + idx,
                   1146:                    c->prompt_buffer + c->prompt_index,
                   1147:                    size + 1 - c->prompt_index);
                   1148:                memset(c->prompt_buffer + size - (c->prompt_index - idx),
                   1149:                    '\0', c->prompt_index - idx);
                   1150:                c->prompt_index = idx;
                   1151:                c->flags |= CLIENT_STATUS;
                   1152:                break;
1.83      nicm     1153:        case MODEKEYEDIT_NEXTSPACE:
                   1154:                wsep = " ";
                   1155:                /* FALLTHROUGH */
1.81      nicm     1156:        case MODEKEYEDIT_NEXTWORD:
1.83      nicm     1157:                if (wsep == NULL)
                   1158:                        wsep = options_get_string(oo, "word-separators");
1.81      nicm     1159:
                   1160:                /* Find a separator. */
                   1161:                while (c->prompt_index != size) {
                   1162:                        c->prompt_index++;
                   1163:                        if (strchr(wsep, c->prompt_buffer[c->prompt_index]))
                   1164:                                break;
                   1165:                }
                   1166:
                   1167:                /* Find the word right after the separation. */
                   1168:                while (c->prompt_index != size) {
                   1169:                        c->prompt_index++;
                   1170:                        if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
                   1171:                                break;
                   1172:                }
                   1173:
                   1174:                c->flags |= CLIENT_STATUS;
                   1175:                break;
1.83      nicm     1176:        case MODEKEYEDIT_NEXTSPACEEND:
                   1177:                wsep = " ";
                   1178:                /* FALLTHROUGH */
1.81      nicm     1179:        case MODEKEYEDIT_NEXTWORDEND:
1.83      nicm     1180:                if (wsep == NULL)
                   1181:                        wsep = options_get_string(oo, "word-separators");
1.81      nicm     1182:
                   1183:                /* Find a word. */
                   1184:                while (c->prompt_index != size) {
                   1185:                        c->prompt_index++;
                   1186:                        if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
                   1187:                                break;
                   1188:                }
                   1189:
                   1190:                /* Find the separator at the end of the word. */
                   1191:                while (c->prompt_index != size) {
                   1192:                        c->prompt_index++;
1.82      nicm     1193:                        if (strchr(wsep, c->prompt_buffer[c->prompt_index]))
1.81      nicm     1194:                                break;
                   1195:                }
1.106   ! nicm     1196:
        !          1197:                /* Back up to the end-of-word like vi. */
        !          1198:                if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
        !          1199:                    c->prompt_index != 0)
        !          1200:                        c->prompt_index--;
1.81      nicm     1201:
                   1202:                c->flags |= CLIENT_STATUS;
                   1203:                break;
1.83      nicm     1204:        case MODEKEYEDIT_PREVIOUSSPACE:
                   1205:                wsep = " ";
                   1206:                /* FALLTHROUGH */
1.81      nicm     1207:        case MODEKEYEDIT_PREVIOUSWORD:
1.83      nicm     1208:                if (wsep == NULL)
                   1209:                        wsep = options_get_string(oo, "word-separators");
1.81      nicm     1210:
                   1211:                /* Find a non-separator. */
                   1212:                while (c->prompt_index != 0) {
                   1213:                        c->prompt_index--;
                   1214:                        if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
                   1215:                                break;
                   1216:                }
                   1217:
                   1218:                /* Find the separator at the beginning of the word. */
                   1219:                while (c->prompt_index != 0) {
                   1220:                        c->prompt_index--;
                   1221:                        if (strchr(wsep, c->prompt_buffer[c->prompt_index])) {
                   1222:                                /* Go back to the word. */
                   1223:                                c->prompt_index++;
                   1224:                                break;
                   1225:                        }
                   1226:                }
                   1227:
                   1228:                c->flags |= CLIENT_STATUS;
                   1229:                break;
1.20      nicm     1230:        case MODEKEYEDIT_HISTORYUP:
1.66      nicm     1231:                histstr = status_prompt_up_history(&c->prompt_hindex);
                   1232:                if (histstr == NULL)
1.1       nicm     1233:                        break;
1.94      nicm     1234:                free(c->prompt_buffer);
1.66      nicm     1235:                c->prompt_buffer = xstrdup(histstr);
1.1       nicm     1236:                c->prompt_index = strlen(c->prompt_buffer);
                   1237:                c->flags |= CLIENT_STATUS;
                   1238:                break;
1.20      nicm     1239:        case MODEKEYEDIT_HISTORYDOWN:
1.66      nicm     1240:                histstr = status_prompt_down_history(&c->prompt_hindex);
                   1241:                if (histstr == NULL)
1.65      nicm     1242:                        break;
1.94      nicm     1243:                free(c->prompt_buffer);
1.66      nicm     1244:                c->prompt_buffer = xstrdup(histstr);
1.1       nicm     1245:                c->prompt_index = strlen(c->prompt_buffer);
                   1246:                c->flags |= CLIENT_STATUS;
                   1247:                break;
1.20      nicm     1248:        case MODEKEYEDIT_PASTE:
1.68      nicm     1249:                if ((pb = paste_get_top(&global_buffers)) == NULL)
1.1       nicm     1250:                        break;
1.32      nicm     1251:                for (n = 0; n < pb->size; n++) {
1.53      nicm     1252:                        ch = (u_char) pb->data[n];
                   1253:                        if (ch < 32 || ch == 127)
1.32      nicm     1254:                                break;
                   1255:                }
1.1       nicm     1256:
                   1257:                c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + n + 1);
                   1258:                if (c->prompt_index == size) {
                   1259:                        memcpy(c->prompt_buffer + c->prompt_index, pb->data, n);
                   1260:                        c->prompt_index += n;
                   1261:                        c->prompt_buffer[c->prompt_index] = '\0';
                   1262:                } else {
                   1263:                        memmove(c->prompt_buffer + c->prompt_index + n,
                   1264:                            c->prompt_buffer + c->prompt_index,
                   1265:                            size + 1 - c->prompt_index);
                   1266:                        memcpy(c->prompt_buffer + c->prompt_index, pb->data, n);
                   1267:                        c->prompt_index += n;
                   1268:                }
                   1269:
                   1270:                c->flags |= CLIENT_STATUS;
1.30      nicm     1271:                break;
                   1272:        case MODEKEYEDIT_TRANSPOSECHARS:
                   1273:                idx = c->prompt_index;
                   1274:                if (idx < size)
                   1275:                        idx++;
                   1276:                if (idx >= 2) {
                   1277:                        swapc = c->prompt_buffer[idx - 2];
                   1278:                        c->prompt_buffer[idx - 2] = c->prompt_buffer[idx - 1];
                   1279:                        c->prompt_buffer[idx - 1] = swapc;
                   1280:                        c->prompt_index = idx;
                   1281:                        c->flags |= CLIENT_STATUS;
                   1282:                }
1.1       nicm     1283:                break;
1.55      nicm     1284:        case MODEKEYEDIT_ENTER:
1.25      nicm     1285:                if (*c->prompt_buffer != '\0')
1.65      nicm     1286:                        status_prompt_add_history(c->prompt_buffer);
1.25      nicm     1287:                if (c->prompt_callbackfn(c->prompt_data, c->prompt_buffer) == 0)
                   1288:                        status_prompt_clear(c);
                   1289:                break;
1.20      nicm     1290:        case MODEKEYEDIT_CANCEL:
1.12      nicm     1291:                if (c->prompt_callbackfn(c->prompt_data, NULL) == 0)
1.1       nicm     1292:                        status_prompt_clear(c);
                   1293:                break;
1.20      nicm     1294:        case MODEKEY_OTHER:
1.61      nicm     1295:                if ((key & 0xff00) != 0 || key < 32 || key == 127)
1.1       nicm     1296:                        break;
                   1297:                c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 2);
                   1298:
                   1299:                if (c->prompt_index == size) {
                   1300:                        c->prompt_buffer[c->prompt_index++] = key;
                   1301:                        c->prompt_buffer[c->prompt_index] = '\0';
                   1302:                } else {
                   1303:                        memmove(c->prompt_buffer + c->prompt_index + 1,
                   1304:                            c->prompt_buffer + c->prompt_index,
                   1305:                            size + 1 - c->prompt_index);
                   1306:                        c->prompt_buffer[c->prompt_index++] = key;
                   1307:                }
                   1308:
                   1309:                if (c->prompt_flags & PROMPT_SINGLE) {
1.12      nicm     1310:                        if (c->prompt_callbackfn(
1.1       nicm     1311:                            c->prompt_data, c->prompt_buffer) == 0)
                   1312:                                status_prompt_clear(c);
                   1313:                }
                   1314:
                   1315:                c->flags |= CLIENT_STATUS;
                   1316:                break;
                   1317:        default:
                   1318:                break;
                   1319:        }
                   1320: }
                   1321:
1.65      nicm     1322: /* Get previous line from the history. */
                   1323: const char *
                   1324: status_prompt_up_history(u_int *idx)
                   1325: {
                   1326:        u_int size;
                   1327:
                   1328:        /*
                   1329:         * History runs from 0 to size - 1.
                   1330:         *
                   1331:         * Index is from 0 to size. Zero is empty.
                   1332:         */
                   1333:
                   1334:        size = ARRAY_LENGTH(&status_prompt_history);
                   1335:        if (size == 0 || *idx == size)
                   1336:                return (NULL);
                   1337:        (*idx)++;
                   1338:        return (ARRAY_ITEM(&status_prompt_history, size - *idx));
                   1339: }
                   1340:
                   1341: /* Get next line from the history. */
                   1342: const char *
                   1343: status_prompt_down_history(u_int *idx)
                   1344: {
                   1345:        u_int size;
                   1346:
                   1347:        size = ARRAY_LENGTH(&status_prompt_history);
                   1348:        if (size == 0 || *idx == 0)
                   1349:                return ("");
                   1350:        (*idx)--;
                   1351:        if (*idx == 0)
                   1352:                return ("");
                   1353:        return (ARRAY_ITEM(&status_prompt_history, size - *idx));
                   1354: }
                   1355:
1.1       nicm     1356: /* Add line to the history. */
                   1357: void
1.65      nicm     1358: status_prompt_add_history(const char *line)
1.1       nicm     1359: {
1.65      nicm     1360:        u_int size;
                   1361:
                   1362:        size = ARRAY_LENGTH(&status_prompt_history);
                   1363:        if (size > 0 && strcmp(ARRAY_LAST(&status_prompt_history), line) == 0)
1.1       nicm     1364:                return;
                   1365:
1.65      nicm     1366:        if (size == PROMPT_HISTORY) {
1.94      nicm     1367:                free(ARRAY_FIRST(&status_prompt_history));
1.65      nicm     1368:                ARRAY_REMOVE(&status_prompt_history, 0);
1.1       nicm     1369:        }
                   1370:
1.65      nicm     1371:        ARRAY_ADD(&status_prompt_history, xstrdup(line));
1.1       nicm     1372: }
                   1373:
                   1374: /* Complete word. */
                   1375: char *
                   1376: status_prompt_complete(const char *s)
                   1377: {
1.69      nicm     1378:        const struct cmd_entry                 **cmdent;
                   1379:        const struct options_table_entry        *oe;
                   1380:        ARRAY_DECL(, const char *)               list;
                   1381:        char                                    *prefix, *s2;
                   1382:        u_int                                    i;
                   1383:        size_t                                   j;
1.1       nicm     1384:
                   1385:        if (*s == '\0')
                   1386:                return (NULL);
                   1387:
                   1388:        /* First, build a list of all the possible matches. */
                   1389:        ARRAY_INIT(&list);
                   1390:        for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
                   1391:                if (strncmp((*cmdent)->name, s, strlen(s)) == 0)
                   1392:                        ARRAY_ADD(&list, (*cmdent)->name);
1.56      nicm     1393:        }
1.69      nicm     1394:        for (oe = server_options_table; oe->name != NULL; oe++) {
                   1395:                if (strncmp(oe->name, s, strlen(s)) == 0)
                   1396:                        ARRAY_ADD(&list, oe->name);
                   1397:        }
                   1398:        for (oe = session_options_table; oe->name != NULL; oe++) {
                   1399:                if (strncmp(oe->name, s, strlen(s)) == 0)
                   1400:                        ARRAY_ADD(&list, oe->name);
                   1401:        }
                   1402:        for (oe = window_options_table; oe->name != NULL; oe++) {
                   1403:                if (strncmp(oe->name, s, strlen(s)) == 0)
                   1404:                        ARRAY_ADD(&list, oe->name);
1.1       nicm     1405:        }
                   1406:
                   1407:        /* If none, bail now. */
                   1408:        if (ARRAY_LENGTH(&list) == 0) {
                   1409:                ARRAY_FREE(&list);
                   1410:                return (NULL);
                   1411:        }
                   1412:
                   1413:        /* If an exact match, return it, with a trailing space. */
                   1414:        if (ARRAY_LENGTH(&list) == 1) {
                   1415:                xasprintf(&s2, "%s ", ARRAY_FIRST(&list));
                   1416:                ARRAY_FREE(&list);
                   1417:                return (s2);
                   1418:        }
                   1419:
                   1420:        /* Now loop through the list and find the longest common prefix. */
                   1421:        prefix = xstrdup(ARRAY_FIRST(&list));
                   1422:        for (i = 1; i < ARRAY_LENGTH(&list); i++) {
                   1423:                s = ARRAY_ITEM(&list, i);
                   1424:
                   1425:                j = strlen(s);
                   1426:                if (j > strlen(prefix))
                   1427:                        j = strlen(prefix);
                   1428:                for (; j > 0; j--) {
                   1429:                        if (prefix[j - 1] != s[j - 1])
                   1430:                                prefix[j - 1] = '\0';
                   1431:                }
                   1432:        }
                   1433:
                   1434:        ARRAY_FREE(&list);
                   1435:        return (prefix);
                   1436: }