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

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