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

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