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

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