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

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