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

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