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

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