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

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