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

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