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

1.48    ! nicm        1: /* $OpenBSD: status.c,v 1.47 2009/11/19 16:22:10 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(
                     40:            struct client *, struct winlink *, time_t, struct grid_cell *);
                     41: void   status_replace1(struct client *,
                     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.7       nicm      124:        /* No status line?*/
                    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.48    ! 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);
        !           182:                wl->status_width =
        !           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) {
        !           196:                screen_write_cnputs(&ctx,
        !           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:                }
        !           230:
        !           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:
        !           274:        /* Begin drawing. */
        !           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. */
        !           305:        wloffset = 1;
        !           306:        if (wlwidth < wlavailable) {
        !           307:                switch (options_get_number(&s->options, "status-justify")) {
        !           308:                case 1: /* centered */
        !           309:                        wloffset = 1 + (wlavailable - wlwidth) / 2;
        !           310:                        break;
        !           311:                case 2: /* right */
        !           312:                        wloffset = 1 + (wlavailable - wlwidth);
        !           313:                        break;
        !           314:                }
        !           315:        }
        !           316:        wloffset += llen;
        !           317:        if (larrow != 0)
        !           318:                wloffset++;
        !           319:
        !           320:        /* Copy the window list. */
        !           321:        screen_write_cursormove(&ctx, wloffset, 0);
        !           322:        screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1);
        !           323:        screen_free(&window_list);
1.1       nicm      324:
1.48    ! nicm      325:        screen_write_stop(&ctx);
1.1       nicm      326:
                    327: out:
                    328:        if (left != NULL)
                    329:                xfree(left);
                    330:        if (right != NULL)
                    331:                xfree(right);
                    332:
                    333:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    334:                screen_free(&old_status);
                    335:                return (0);
                    336:        }
                    337:        screen_free(&old_status);
                    338:        return (1);
                    339: }
                    340:
1.46      nicm      341: /* Replace a single special sequence (prefixed by #). */
                    342: void
1.47      nicm      343: status_replace1(struct client *c,struct winlink *wl,
1.46      nicm      344:     char **iptr, char **optr, char *out, size_t outsize, int jobsflag)
1.1       nicm      345: {
1.38      nicm      346:        struct session *s = c->session;
1.46      nicm      347:        char            ch, tmp[256], *ptr, *endptr, *freeptr;
                    348:        size_t          ptrlen;
                    349:        long            limit;
                    350:
1.47      nicm      351:        if (wl == NULL)
                    352:                wl = s->curw;
                    353:
1.46      nicm      354:        errno = 0;
                    355:        limit = strtol(*iptr, &endptr, 10);
                    356:        if ((limit == 0 && errno != EINVAL) ||
                    357:            (limit == LONG_MIN && errno != ERANGE) ||
                    358:            (limit == LONG_MAX && errno != ERANGE) ||
                    359:            limit != 0)
                    360:                *iptr = endptr;
                    361:        if (limit <= 0)
                    362:                limit = LONG_MAX;
                    363:
                    364:        freeptr = NULL;
                    365:
                    366:        switch (*(*iptr)++) {
                    367:        case '(':
                    368:                if (!jobsflag) {
                    369:                        ch = ')';
                    370:                        goto skip_to;
                    371:                }
                    372:                if ((ptr = status_job(c, iptr)) == NULL)
                    373:                        return;
                    374:                freeptr = ptr;
                    375:                goto do_replace;
                    376:        case 'H':
                    377:                if (gethostname(tmp, sizeof tmp) != 0)
                    378:                        fatal("gethostname failed");
                    379:                ptr = tmp;
                    380:                goto do_replace;
                    381:        case 'I':
                    382:                xsnprintf(tmp, sizeof tmp, "%d", wl->idx);
                    383:                ptr = tmp;
                    384:                goto do_replace;
                    385:        case 'P':
                    386:                xsnprintf(tmp, sizeof tmp, "%u",
                    387:                    window_pane_index(wl->window, wl->window->active));
                    388:                ptr = tmp;
                    389:                goto do_replace;
                    390:        case 'S':
                    391:                ptr = s->name;
                    392:                goto do_replace;
                    393:        case 'T':
                    394:                ptr = wl->window->active->base.title;
                    395:                goto do_replace;
                    396:        case 'W':
                    397:                ptr = wl->window->name;
                    398:                goto do_replace;
1.47      nicm      399:        case 'F':
                    400:                tmp[0] = ' ';
                    401:                if (session_alert_has(s, wl, WINDOW_CONTENT))
                    402:                        tmp[0] = '+';
                    403:                else if (session_alert_has(s, wl, WINDOW_BELL))
                    404:                        tmp[0] = '!';
                    405:                else if (session_alert_has(s, wl, WINDOW_ACTIVITY))
                    406:                        tmp[0] = '#';
                    407:                else if (wl == s->curw)
                    408:                        tmp[0] = '*';
                    409:                else if (wl == TAILQ_FIRST(&s->lastw))
                    410:                        tmp[0] = '-';
                    411:                tmp[1] = '\0';
                    412:                ptr = tmp;
                    413:                goto do_replace;
1.46      nicm      414:        case '[':
                    415:                /*
                    416:                 * Embedded style, handled at display time. Leave present and
                    417:                 * skip input until ].
                    418:                 */
                    419:                ch = ']';
                    420:                goto skip_to;
                    421:        case '#':
                    422:                *(*optr++) = '#';
                    423:                break;
                    424:        }
                    425:
                    426:        return;
                    427:
                    428: do_replace:
                    429:        ptrlen = strlen(ptr);
                    430:        if ((size_t) limit < ptrlen)
                    431:                ptrlen = limit;
                    432:
                    433:        if (*optr + ptrlen >= out + outsize - 1)
                    434:                return;
                    435:        while (ptrlen > 0 && *ptr != '\0') {
                    436:                *(*optr)++ = *ptr++;
                    437:                ptrlen--;
                    438:        }
                    439:
                    440:        if (freeptr != NULL)
                    441:                xfree(freeptr);
                    442:        return;
                    443:
                    444: skip_to:
                    445:        *(*optr)++ = '#';
                    446:
                    447:        (*iptr)--;      /* include ch */
                    448:        while (**iptr != ch && **iptr != '\0') {
                    449:                if (*optr >=  out + outsize - 1)
                    450:                        break;
                    451:                *(*optr)++ = *(*iptr)++;
                    452:        }
                    453: }
                    454:
                    455: /* Replace special sequences in fmt. */
                    456: char *
1.47      nicm      457: status_replace(struct client *c,
                    458:     struct winlink *wl, const char *fmt, time_t t, int jobsflag)
1.46      nicm      459: {
1.1       nicm      460:        static char     out[BUFSIZ];
1.46      nicm      461:        char            in[BUFSIZ], ch, *iptr, *optr;
1.47      nicm      462:
1.1       nicm      463:        strftime(in, sizeof in, fmt, localtime(&t));
                    464:        in[(sizeof in) - 1] = '\0';
                    465:
                    466:        iptr = in;
                    467:        optr = out;
                    468:
                    469:        while (*iptr != '\0') {
                    470:                if (optr >= out + (sizeof out) - 1)
                    471:                        break;
1.46      nicm      472:                ch = *iptr++;
                    473:
                    474:                if (ch != '#') {
1.1       nicm      475:                        *optr++ = ch;
1.46      nicm      476:                        continue;
1.1       nicm      477:                }
1.47      nicm      478:                status_replace1(c, wl, &iptr, &optr, out, sizeof out, jobsflag);
1.1       nicm      479:        }
                    480:        *optr = '\0';
                    481:
                    482:        return (xstrdup(out));
                    483: }
                    484:
1.46      nicm      485: /* Figure out job name and get its result, starting it off if necessary. */
1.1       nicm      486: char *
1.38      nicm      487: status_job(struct client *c, char **iptr)
1.1       nicm      488: {
1.38      nicm      489:        struct job      *job;
1.40      nicm      490:        char            *cmd;
1.38      nicm      491:        int              lastesc;
                    492:        size_t           len;
1.1       nicm      493:
                    494:        if (**iptr == '\0')
                    495:                return (NULL);
                    496:        if (**iptr == ')') {            /* no command given */
                    497:                (*iptr)++;
                    498:                return (NULL);
                    499:        }
                    500:
                    501:        cmd = xmalloc(strlen(*iptr) + 1);
                    502:        len = 0;
                    503:
                    504:        lastesc = 0;
                    505:        for (; **iptr != '\0'; (*iptr)++) {
                    506:                if (!lastesc && **iptr == ')')
                    507:                        break;          /* unescaped ) is the end */
                    508:                if (!lastesc && **iptr == '\\') {
                    509:                        lastesc = 1;
                    510:                        continue;       /* skip \ if not escaped */
                    511:                }
                    512:                lastesc = 0;
                    513:                cmd[len++] = **iptr;
                    514:        }
1.38      nicm      515:        if (**iptr == '\0')             /* no terminating ) */ {
                    516:                xfree(cmd);
                    517:                return (NULL);
                    518:        }
1.1       nicm      519:        (*iptr)++;                      /* skip final ) */
                    520:        cmd[len] = '\0';
                    521:
1.38      nicm      522:        job = job_get(&c->status_jobs, cmd);
                    523:        if (job == NULL) {
1.39      nicm      524:                job = job_add(&c->status_jobs,
                    525:                    JOB_PERSIST, c, cmd, status_job_callback, xfree, NULL);
1.38      nicm      526:                job_run(job);
                    527:        }
                    528:        if (job->data == NULL)
                    529:                return (xstrdup(""));
                    530:        return (xstrdup(job->data));
                    531: }
1.1       nicm      532:
1.46      nicm      533: /* Job has finished: save its result. */
1.38      nicm      534: void
                    535: status_job_callback(struct job *job)
                    536: {
1.41      nicm      537:        char    *line, *buf;
1.38      nicm      538:        size_t   len;
                    539:
1.41      nicm      540:        buf = NULL;
                    541:        if ((line = evbuffer_readline(job->event->input)) == NULL) {
                    542:                len = EVBUFFER_LENGTH(job->event->input);
                    543:                buf = xmalloc(len + 1);
                    544:                if (len != 0)
                    545:                        memcpy(buf, EVBUFFER_DATA(job->event->input), len);
                    546:                buf[len] = '\0';
                    547:        }
1.38      nicm      548:
                    549:        if (job->data != NULL)
                    550:                xfree(job->data);
                    551:        else
                    552:                server_redraw_client(job->client);
1.41      nicm      553:        job->data = xstrdup(line);
1.1       nicm      554:
1.41      nicm      555:        if (buf != NULL)
                    556:                xfree(buf);
1.1       nicm      557: }
                    558:
1.46      nicm      559: /* Calculate winlink status line entry width. */
1.1       nicm      560: size_t
1.47      nicm      561: status_width(struct client *c, struct winlink *wl, time_t t)
1.1       nicm      562: {
1.47      nicm      563:        struct options  *oo = &wl->window->options;
                    564:        struct session  *s = c->session;
                    565:        const char      *fmt;
                    566:        char            *text;
                    567:        size_t           size;
                    568:        int              utf8flag;
                    569:
                    570:        utf8flag = options_get_number(&s->options, "status-utf8");
                    571:
                    572:        fmt = options_get_string(&wl->window->options, "window-status-format");
                    573:        if (wl == s->curw)
                    574:                fmt = options_get_string(oo, "window-status-current-format");
                    575:
                    576:        text = status_replace(c, wl, fmt, t, 1);
                    577:        size = screen_write_cstrlen(utf8flag, "%s", text);
                    578:        xfree(text);
                    579:
                    580:        return (size);
1.1       nicm      581: }
                    582:
1.46      nicm      583: /* Return winlink status line entry and adjust gc as necessary. */
1.1       nicm      584: char *
1.47      nicm      585: status_print(
                    586:     struct client *c, struct winlink *wl, time_t t, struct grid_cell *gc)
1.1       nicm      587: {
1.33      nicm      588:        struct options  *oo = &wl->window->options;
1.47      nicm      589:        struct session  *s = c->session;
                    590:        const char      *fmt;
                    591:        char            *text;
1.33      nicm      592:        u_char           fg, bg, attr;
1.1       nicm      593:
1.33      nicm      594:        fg = options_get_number(oo, "window-status-fg");
1.1       nicm      595:        if (fg != 8)
1.33      nicm      596:                colour_set_fg(gc, fg);
                    597:        bg = options_get_number(oo, "window-status-bg");
1.1       nicm      598:        if (bg != 8)
1.33      nicm      599:                colour_set_bg(gc, bg);
                    600:        attr = options_get_number(oo, "window-status-attr");
1.1       nicm      601:        if (attr != 0)
                    602:                gc->attr = attr;
1.47      nicm      603:        fmt = options_get_string(oo, "window-status-format");
1.14      nicm      604:        if (wl == s->curw) {
1.33      nicm      605:                fg = options_get_number(oo, "window-status-current-fg");
1.14      nicm      606:                if (fg != 8)
1.33      nicm      607:                        colour_set_fg(gc, fg);
                    608:                bg = options_get_number(oo, "window-status-current-bg");
1.14      nicm      609:                if (bg != 8)
1.33      nicm      610:                        colour_set_bg(gc, bg);
                    611:                attr = options_get_number(oo, "window-status-current-attr");
1.14      nicm      612:                if (attr != 0)
                    613:                        gc->attr = attr;
1.47      nicm      614:                fmt = options_get_string(oo, "window-status-current-format");
1.14      nicm      615:        }
1.1       nicm      616:
1.47      nicm      617:        if (session_alert_has(s, wl, WINDOW_ACTIVITY) ||
                    618:            session_alert_has(s, wl, WINDOW_BELL) ||
                    619:            session_alert_has(s, wl, WINDOW_CONTENT))
1.1       nicm      620:                gc->attr ^= GRID_ATTR_REVERSE;
                    621:
1.47      nicm      622:        text = status_replace(c, wl, fmt, t, 1);
1.1       nicm      623:        return (text);
                    624: }
                    625:
1.46      nicm      626: /* Set a status line message. */
1.10      nicm      627: void printflike2
                    628: status_message_set(struct client *c, const char *fmt, ...)
1.1       nicm      629: {
1.44      nicm      630:        struct timeval           tv;
                    631:        struct session          *s = c->session;
                    632:        struct message_entry    *msg;
                    633:        va_list                  ap;
                    634:        int                      delay;
                    635:        u_int                    i, limit;
1.1       nicm      636:
1.12      nicm      637:        status_prompt_clear(c);
                    638:        status_message_clear(c);
                    639:
1.28      nicm      640:        va_start(ap, fmt);
                    641:        xvasprintf(&c->message_string, fmt, ap);
                    642:        va_end(ap);
                    643:
1.44      nicm      644:        ARRAY_EXPAND(&c->message_log, 1);
                    645:        msg = &ARRAY_LAST(&c->message_log);
                    646:        msg->msg_time = time(NULL);
                    647:        msg->msg = xstrdup(c->message_string);
                    648:
                    649:        if (s == NULL)
                    650:                limit = 0;
                    651:        else
                    652:                limit = options_get_number(&s->options, "message-limit");
                    653:        for (i = ARRAY_LENGTH(&c->message_log); i > limit; i--) {
                    654:                msg = &ARRAY_ITEM(&c->message_log, i - 1);
                    655:                xfree(msg->msg);
                    656:                ARRAY_REMOVE(&c->message_log, i - 1);
                    657:        }
                    658:
1.1       nicm      659:        delay = options_get_number(&c->session->options, "display-time");
                    660:        tv.tv_sec = delay / 1000;
                    661:        tv.tv_usec = (delay % 1000) * 1000L;
1.44      nicm      662:
1.42      nicm      663:        evtimer_del(&c->message_timer);
                    664:        evtimer_set(&c->message_timer, status_message_callback, c);
                    665:        evtimer_add(&c->message_timer, &tv);
1.1       nicm      666:
                    667:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
                    668:        c->flags |= CLIENT_STATUS;
                    669: }
                    670:
1.46      nicm      671: /* Clear status line message. */
1.1       nicm      672: void
                    673: status_message_clear(struct client *c)
                    674: {
                    675:        if (c->message_string == NULL)
                    676:                return;
                    677:
                    678:        xfree(c->message_string);
                    679:        c->message_string = NULL;
                    680:
                    681:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12      nicm      682:        c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7       nicm      683:
                    684:        screen_reinit(&c->status);
1.42      nicm      685: }
                    686:
1.46      nicm      687: /* Clear status line message after timer expires. */
1.42      nicm      688: void
                    689: status_message_callback(unused int fd, unused short event, void *data)
                    690: {
                    691:        struct client   *c = data;
                    692:
                    693:        status_message_clear(c);
1.1       nicm      694: }
                    695:
                    696: /* Draw client message on status line of present else on last line. */
                    697: int
                    698: status_message_redraw(struct client *c)
                    699: {
                    700:        struct screen_write_ctx         ctx;
                    701:        struct session                 *s = c->session;
                    702:        struct screen                   old_status;
                    703:        size_t                          len;
                    704:        struct grid_cell                gc;
                    705:
                    706:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    707:                return (0);
                    708:        memcpy(&old_status, &c->status, sizeof old_status);
                    709:        screen_init(&c->status, c->tty.sx, 1, 0);
                    710:
                    711:        len = strlen(c->message_string);
                    712:        if (len > c->tty.sx)
                    713:                len = c->tty.sx;
                    714:
                    715:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.33      nicm      716:        colour_set_fg(&gc, options_get_number(&s->options, "message-fg"));
                    717:        colour_set_bg(&gc, options_get_number(&s->options, "message-bg"));
1.1       nicm      718:        gc.attr |= options_get_number(&s->options, "message-attr");
                    719:
                    720:        screen_write_start(&ctx, NULL, &c->status);
                    721:
                    722:        screen_write_cursormove(&ctx, 0, 0);
                    723:        screen_write_puts(&ctx, &gc, "%.*s", (int) len, c->message_string);
                    724:        for (; len < c->tty.sx; len++)
                    725:                screen_write_putc(&ctx, &gc, ' ');
                    726:
                    727:        screen_write_stop(&ctx);
                    728:
                    729:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    730:                screen_free(&old_status);
                    731:                return (0);
                    732:        }
                    733:        screen_free(&old_status);
                    734:        return (1);
                    735: }
                    736:
1.46      nicm      737: /* Enable status line prompt. */
1.1       nicm      738: void
1.12      nicm      739: status_prompt_set(struct client *c, const char *msg,
                    740:     int (*callbackfn)(void *, const char *), void (*freefn)(void *),
                    741:     void *data, int flags)
1.1       nicm      742: {
1.20      nicm      743:        int     keys;
                    744:
1.12      nicm      745:        status_message_clear(c);
                    746:        status_prompt_clear(c);
                    747:
1.1       nicm      748:        c->prompt_string = xstrdup(msg);
                    749:
                    750:        c->prompt_buffer = xstrdup("");
                    751:        c->prompt_index = 0;
                    752:
1.12      nicm      753:        c->prompt_callbackfn = callbackfn;
                    754:        c->prompt_freefn = freefn;
1.1       nicm      755:        c->prompt_data = data;
                    756:
                    757:        c->prompt_hindex = 0;
                    758:
                    759:        c->prompt_flags = flags;
                    760:
1.20      nicm      761:        keys = options_get_number(&c->session->options, "status-keys");
                    762:        if (keys == MODEKEY_EMACS)
1.21      nicm      763:                mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit);
1.20      nicm      764:        else
1.21      nicm      765:                mode_key_init(&c->prompt_mdata, &mode_key_tree_vi_edit);
1.1       nicm      766:
                    767:        c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
                    768:        c->flags |= CLIENT_STATUS;
                    769: }
                    770:
1.46      nicm      771: /* Remove status line prompt. */
1.1       nicm      772: void
                    773: status_prompt_clear(struct client *c)
                    774: {
1.12      nicm      775:        if (c->prompt_string == NULL)
1.1       nicm      776:                return;
                    777:
1.12      nicm      778:        if (c->prompt_freefn != NULL && c->prompt_data != NULL)
                    779:                c->prompt_freefn(c->prompt_data);
1.1       nicm      780:
                    781:        xfree(c->prompt_string);
                    782:        c->prompt_string = NULL;
                    783:
                    784:        xfree(c->prompt_buffer);
                    785:        c->prompt_buffer = NULL;
                    786:
                    787:        c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12      nicm      788:        c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7       nicm      789:
                    790:        screen_reinit(&c->status);
1.27      nicm      791: }
                    792:
1.46      nicm      793: /* Update status line prompt with a new prompt string. */
1.27      nicm      794: void
                    795: status_prompt_update(struct client *c, const char *msg)
                    796: {
                    797:        xfree(c->prompt_string);
                    798:        c->prompt_string = xstrdup(msg);
                    799:
                    800:        *c->prompt_buffer = '\0';
                    801:        c->prompt_index = 0;
                    802:
                    803:        c->prompt_hindex = 0;
                    804:
                    805:        c->flags |= CLIENT_STATUS;
1.1       nicm      806: }
                    807:
                    808: /* Draw client prompt on status line of present else on last line. */
                    809: int
                    810: status_prompt_redraw(struct client *c)
                    811: {
                    812:        struct screen_write_ctx ctx;
                    813:        struct session                 *s = c->session;
                    814:        struct screen                   old_status;
1.29      nicm      815:        size_t                          i, size, left, len, off;
1.1       nicm      816:        char                            ch;
                    817:        struct grid_cell                gc;
                    818:
                    819:        if (c->tty.sx == 0 || c->tty.sy == 0)
                    820:                return (0);
                    821:        memcpy(&old_status, &c->status, sizeof old_status);
                    822:        screen_init(&c->status, c->tty.sx, 1, 0);
1.17      nicm      823:        off = 0;
1.1       nicm      824:
                    825:        len = strlen(c->prompt_string);
                    826:        if (len > c->tty.sx)
                    827:                len = c->tty.sx;
                    828:
                    829:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.33      nicm      830:        colour_set_fg(&gc, options_get_number(&s->options, "message-fg"));
                    831:        colour_set_bg(&gc, options_get_number(&s->options, "message-bg"));
1.1       nicm      832:        gc.attr |= options_get_number(&s->options, "message-attr");
                    833:
                    834:        screen_write_start(&ctx, NULL, &c->status);
                    835:
                    836:        screen_write_cursormove(&ctx, 0, 0);
                    837:        screen_write_puts(&ctx, &gc, "%.*s", (int) len, c->prompt_string);
                    838:
                    839:        left = c->tty.sx - len;
                    840:        if (left != 0) {
                    841:                if (c->prompt_index < left)
                    842:                        size = strlen(c->prompt_buffer);
                    843:                else {
1.17      nicm      844:                        off = c->prompt_index - left + 1;
1.1       nicm      845:                        if (c->prompt_index == strlen(c->prompt_buffer))
                    846:                                left--;
                    847:                        size = left;
                    848:                }
1.36      nicm      849:                screen_write_puts(
                    850:                    &ctx, &gc, "%.*s", (int) left, c->prompt_buffer + off);
1.1       nicm      851:
                    852:                for (i = len + size; i < c->tty.sx; i++)
                    853:                        screen_write_putc(&ctx, &gc, ' ');
                    854:
1.17      nicm      855:                /* Draw a fake cursor. */
1.29      nicm      856:                ch = ' ';
1.36      nicm      857:                screen_write_cursormove(&ctx, len + c->prompt_index - off, 0);
                    858:                if (c->prompt_index < strlen(c->prompt_buffer))
                    859:                        ch = c->prompt_buffer[c->prompt_index];
1.17      nicm      860:                gc.attr ^= GRID_ATTR_REVERSE;
                    861:                screen_write_putc(&ctx, &gc, ch);
1.1       nicm      862:        }
                    863:
                    864:        screen_write_stop(&ctx);
                    865:
                    866:        if (grid_compare(c->status.grid, old_status.grid) == 0) {
                    867:                screen_free(&old_status);
                    868:                return (0);
                    869:        }
                    870:        screen_free(&old_status);
                    871:        return (1);
                    872: }
                    873:
                    874: /* Handle keys in prompt. */
                    875: void
                    876: status_prompt_key(struct client *c, int key)
                    877: {
                    878:        struct paste_buffer     *pb;
1.30      nicm      879:        char                    *s, *first, *last, word[64], swapc;
1.1       nicm      880:        size_t                   size, n, off, idx;
                    881:
                    882:        size = strlen(c->prompt_buffer);
                    883:        switch (mode_key_lookup(&c->prompt_mdata, key)) {
1.20      nicm      884:        case MODEKEYEDIT_CURSORLEFT:
1.1       nicm      885:                if (c->prompt_index > 0) {
                    886:                        c->prompt_index--;
                    887:                        c->flags |= CLIENT_STATUS;
                    888:                }
                    889:                break;
1.20      nicm      890:        case MODEKEYEDIT_SWITCHMODEAPPEND:
                    891:        case MODEKEYEDIT_CURSORRIGHT:
1.1       nicm      892:                if (c->prompt_index < size) {
                    893:                        c->prompt_index++;
                    894:                        c->flags |= CLIENT_STATUS;
                    895:                }
                    896:                break;
1.20      nicm      897:        case MODEKEYEDIT_STARTOFLINE:
1.1       nicm      898:                if (c->prompt_index != 0) {
                    899:                        c->prompt_index = 0;
                    900:                        c->flags |= CLIENT_STATUS;
                    901:                }
                    902:                break;
1.20      nicm      903:        case MODEKEYEDIT_ENDOFLINE:
1.1       nicm      904:                if (c->prompt_index != size) {
                    905:                        c->prompt_index = size;
                    906:                        c->flags |= CLIENT_STATUS;
                    907:                }
                    908:                break;
1.20      nicm      909:        case MODEKEYEDIT_COMPLETE:
1.1       nicm      910:                if (*c->prompt_buffer == '\0')
                    911:                        break;
                    912:
                    913:                idx = c->prompt_index;
                    914:                if (idx != 0)
                    915:                        idx--;
                    916:
                    917:                /* Find the word we are in. */
                    918:                first = c->prompt_buffer + idx;
                    919:                while (first > c->prompt_buffer && *first != ' ')
                    920:                        first--;
                    921:                while (*first == ' ')
                    922:                        first++;
                    923:                last = c->prompt_buffer + idx;
                    924:                while (*last != '\0' && *last != ' ')
                    925:                        last++;
                    926:                while (*last == ' ')
                    927:                        last--;
                    928:                if (*last != '\0')
                    929:                        last++;
                    930:                if (last <= first ||
                    931:                    ((size_t) (last - first)) > (sizeof word) - 1)
                    932:                        break;
                    933:                memcpy(word, first, last - first);
                    934:                word[last - first] = '\0';
                    935:
                    936:                /* And try to complete it. */
                    937:                if ((s = status_prompt_complete(word)) == NULL)
                    938:                        break;
                    939:
                    940:                /* Trim out word. */
                    941:                n = size - (last - c->prompt_buffer) + 1; /* with \0 */
                    942:                memmove(first, last, n);
                    943:                size -= last - first;
                    944:
                    945:                /* Insert the new word. */
                    946:                size += strlen(s);
                    947:                off = first - c->prompt_buffer;
                    948:                c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 1);
                    949:                first = c->prompt_buffer + off;
                    950:                memmove(first + strlen(s), first, n);
                    951:                memcpy(first, s, strlen(s));
                    952:
                    953:                c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1.22      nicm      954:                xfree(s);
1.1       nicm      955:
                    956:                c->flags |= CLIENT_STATUS;
                    957:                break;
1.20      nicm      958:        case MODEKEYEDIT_BACKSPACE:
1.1       nicm      959:                if (c->prompt_index != 0) {
                    960:                        if (c->prompt_index == size)
                    961:                                c->prompt_buffer[--c->prompt_index] = '\0';
                    962:                        else {
                    963:                                memmove(c->prompt_buffer + c->prompt_index - 1,
                    964:                                    c->prompt_buffer + c->prompt_index,
                    965:                                    size + 1 - c->prompt_index);
                    966:                                c->prompt_index--;
                    967:                        }
                    968:                        c->flags |= CLIENT_STATUS;
                    969:                }
                    970:                break;
1.20      nicm      971:        case MODEKEYEDIT_DELETE:
1.1       nicm      972:                if (c->prompt_index != size) {
                    973:                        memmove(c->prompt_buffer + c->prompt_index,
                    974:                            c->prompt_buffer + c->prompt_index + 1,
                    975:                            size + 1 - c->prompt_index);
1.18      nicm      976:                        c->flags |= CLIENT_STATUS;
                    977:                }
1.26      nicm      978:                break;
                    979:        case MODEKEYEDIT_DELETELINE:
                    980:                *c->prompt_buffer = '\0';
                    981:                c->prompt_index = 0;
                    982:                c->flags |= CLIENT_STATUS;
1.18      nicm      983:                break;
1.20      nicm      984:        case MODEKEYEDIT_DELETETOENDOFLINE:
1.18      nicm      985:                if (c->prompt_index < size) {
                    986:                        c->prompt_buffer[c->prompt_index] = '\0';
1.1       nicm      987:                        c->flags |= CLIENT_STATUS;
                    988:                }
                    989:                break;
1.20      nicm      990:        case MODEKEYEDIT_HISTORYUP:
1.1       nicm      991:                if (ARRAY_LENGTH(&c->prompt_hdata) == 0)
                    992:                        break;
                    993:                xfree(c->prompt_buffer);
                    994:
                    995:                c->prompt_buffer = xstrdup(ARRAY_ITEM(&c->prompt_hdata,
                    996:                    ARRAY_LENGTH(&c->prompt_hdata) - 1 - c->prompt_hindex));
                    997:                if (c->prompt_hindex != ARRAY_LENGTH(&c->prompt_hdata) - 1)
                    998:                        c->prompt_hindex++;
                    999:
                   1000:                c->prompt_index = strlen(c->prompt_buffer);
                   1001:                c->flags |= CLIENT_STATUS;
                   1002:                break;
1.20      nicm     1003:        case MODEKEYEDIT_HISTORYDOWN:
1.1       nicm     1004:                xfree(c->prompt_buffer);
                   1005:
                   1006:                if (c->prompt_hindex != 0) {
                   1007:                        c->prompt_hindex--;
                   1008:                        c->prompt_buffer = xstrdup(ARRAY_ITEM(
                   1009:                            &c->prompt_hdata, ARRAY_LENGTH(
                   1010:                            &c->prompt_hdata) - 1 - c->prompt_hindex));
                   1011:                } else
                   1012:                        c->prompt_buffer = xstrdup("");
                   1013:
                   1014:                c->prompt_index = strlen(c->prompt_buffer);
                   1015:                c->flags |= CLIENT_STATUS;
                   1016:                break;
1.20      nicm     1017:        case MODEKEYEDIT_PASTE:
1.1       nicm     1018:                if ((pb = paste_get_top(&c->session->buffers)) == NULL)
                   1019:                        break;
1.32      nicm     1020:                for (n = 0; n < pb->size; n++) {
                   1021:                        if (pb->data[n] < 32 || pb->data[n] == 127)
                   1022:                                break;
                   1023:                }
1.1       nicm     1024:
                   1025:                c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + n + 1);
                   1026:                if (c->prompt_index == size) {
                   1027:                        memcpy(c->prompt_buffer + c->prompt_index, pb->data, n);
                   1028:                        c->prompt_index += n;
                   1029:                        c->prompt_buffer[c->prompt_index] = '\0';
                   1030:                } else {
                   1031:                        memmove(c->prompt_buffer + c->prompt_index + n,
                   1032:                            c->prompt_buffer + c->prompt_index,
                   1033:                            size + 1 - c->prompt_index);
                   1034:                        memcpy(c->prompt_buffer + c->prompt_index, pb->data, n);
                   1035:                        c->prompt_index += n;
                   1036:                }
                   1037:
                   1038:                c->flags |= CLIENT_STATUS;
1.30      nicm     1039:                break;
                   1040:        case MODEKEYEDIT_TRANSPOSECHARS:
                   1041:                idx = c->prompt_index;
                   1042:                if (idx < size)
                   1043:                        idx++;
                   1044:                if (idx >= 2) {
                   1045:                        swapc = c->prompt_buffer[idx - 2];
                   1046:                        c->prompt_buffer[idx - 2] = c->prompt_buffer[idx - 1];
                   1047:                        c->prompt_buffer[idx - 1] = swapc;
                   1048:                        c->prompt_index = idx;
                   1049:                        c->flags |= CLIENT_STATUS;
                   1050:                }
1.1       nicm     1051:                break;
1.20      nicm     1052:        case MODEKEYEDIT_ENTER:
1.25      nicm     1053:                if (*c->prompt_buffer != '\0')
1.1       nicm     1054:                        status_prompt_add_history(c);
1.25      nicm     1055:                if (c->prompt_callbackfn(c->prompt_data, c->prompt_buffer) == 0)
                   1056:                        status_prompt_clear(c);
                   1057:                break;
1.20      nicm     1058:        case MODEKEYEDIT_CANCEL:
1.12      nicm     1059:                if (c->prompt_callbackfn(c->prompt_data, NULL) == 0)
1.1       nicm     1060:                        status_prompt_clear(c);
                   1061:                break;
1.20      nicm     1062:        case MODEKEY_OTHER:
1.43      nicm     1063:                if (key < 32 || key == 127)
1.1       nicm     1064:                        break;
                   1065:                c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 2);
                   1066:
                   1067:                if (c->prompt_index == size) {
                   1068:                        c->prompt_buffer[c->prompt_index++] = key;
                   1069:                        c->prompt_buffer[c->prompt_index] = '\0';
                   1070:                } else {
                   1071:                        memmove(c->prompt_buffer + c->prompt_index + 1,
                   1072:                            c->prompt_buffer + c->prompt_index,
                   1073:                            size + 1 - c->prompt_index);
                   1074:                        c->prompt_buffer[c->prompt_index++] = key;
                   1075:                }
                   1076:
                   1077:                if (c->prompt_flags & PROMPT_SINGLE) {
1.12      nicm     1078:                        if (c->prompt_callbackfn(
1.1       nicm     1079:                            c->prompt_data, c->prompt_buffer) == 0)
                   1080:                                status_prompt_clear(c);
                   1081:                }
                   1082:
                   1083:                c->flags |= CLIENT_STATUS;
                   1084:                break;
                   1085:        default:
                   1086:                break;
                   1087:        }
                   1088: }
                   1089:
                   1090: /* Add line to the history. */
                   1091: void
                   1092: status_prompt_add_history(struct client *c)
                   1093: {
                   1094:        if (ARRAY_LENGTH(&c->prompt_hdata) > 0 &&
                   1095:            strcmp(ARRAY_LAST(&c->prompt_hdata), c->prompt_buffer) == 0)
                   1096:                return;
                   1097:
                   1098:        if (ARRAY_LENGTH(&c->prompt_hdata) == PROMPT_HISTORY) {
                   1099:                xfree(ARRAY_FIRST(&c->prompt_hdata));
                   1100:                ARRAY_REMOVE(&c->prompt_hdata, 0);
                   1101:        }
                   1102:
                   1103:        ARRAY_ADD(&c->prompt_hdata, xstrdup(c->prompt_buffer));
                   1104: }
                   1105:
                   1106: /* Complete word. */
                   1107: char *
                   1108: status_prompt_complete(const char *s)
                   1109: {
                   1110:        const struct cmd_entry        **cmdent;
                   1111:        const struct set_option_entry  *optent;
                   1112:        ARRAY_DECL(, const char *)      list;
                   1113:        char                           *prefix, *s2;
1.9       nicm     1114:        u_int                           i;
1.1       nicm     1115:        size_t                          j;
                   1116:
                   1117:        if (*s == '\0')
                   1118:                return (NULL);
                   1119:
                   1120:        /* First, build a list of all the possible matches. */
                   1121:        ARRAY_INIT(&list);
                   1122:        for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
                   1123:                if (strncmp((*cmdent)->name, s, strlen(s)) == 0)
                   1124:                        ARRAY_ADD(&list, (*cmdent)->name);
                   1125:        }
1.9       nicm     1126:        for (optent = set_option_table; optent->name != NULL; optent++) {
1.1       nicm     1127:                if (strncmp(optent->name, s, strlen(s)) == 0)
                   1128:                        ARRAY_ADD(&list, optent->name);
                   1129:        }
1.9       nicm     1130:        for (optent = set_window_option_table; optent->name != NULL; optent++) {
1.1       nicm     1131:                if (strncmp(optent->name, s, strlen(s)) == 0)
                   1132:                        ARRAY_ADD(&list, optent->name);
                   1133:        }
                   1134:
                   1135:        /* If none, bail now. */
                   1136:        if (ARRAY_LENGTH(&list) == 0) {
                   1137:                ARRAY_FREE(&list);
                   1138:                return (NULL);
                   1139:        }
                   1140:
                   1141:        /* If an exact match, return it, with a trailing space. */
                   1142:        if (ARRAY_LENGTH(&list) == 1) {
                   1143:                xasprintf(&s2, "%s ", ARRAY_FIRST(&list));
                   1144:                ARRAY_FREE(&list);
                   1145:                return (s2);
                   1146:        }
                   1147:
                   1148:        /* Now loop through the list and find the longest common prefix. */
                   1149:        prefix = xstrdup(ARRAY_FIRST(&list));
                   1150:        for (i = 1; i < ARRAY_LENGTH(&list); i++) {
                   1151:                s = ARRAY_ITEM(&list, i);
                   1152:
                   1153:                j = strlen(s);
                   1154:                if (j > strlen(prefix))
                   1155:                        j = strlen(prefix);
                   1156:                for (; j > 0; j--) {
                   1157:                        if (prefix[j - 1] != s[j - 1])
                   1158:                                prefix[j - 1] = '\0';
                   1159:                }
                   1160:        }
                   1161:
                   1162:        ARRAY_FREE(&list);
                   1163:        return (prefix);
                   1164: }