[BACK]Return to server-client.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/server-client.c, Revision 1.73

1.73    ! nicm        1: /* $OpenBSD: server-client.c,v 1.72 2012/05/06 07:38:17 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 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:
1.12      nicm       21: #include <event.h>
1.1       nicm       22: #include <fcntl.h>
                     23: #include <string.h>
1.4       nicm       24: #include <time.h>
1.1       nicm       25: #include <paths.h>
                     26: #include <unistd.h>
                     27:
                     28: #include "tmux.h"
                     29:
1.66      nicm       30: void   server_client_check_mouse(struct client *c,
                     31:            struct window_pane *wp, struct mouse_event *mouse);
1.18      nicm       32: void   server_client_handle_key(int, struct mouse_event *, void *);
1.17      nicm       33: void   server_client_repeat_timer(int, short, void *);
1.36      nicm       34: void   server_client_check_exit(struct client *);
1.1       nicm       35: void   server_client_check_redraw(struct client *);
                     36: void   server_client_set_title(struct client *);
1.18      nicm       37: void   server_client_reset_state(struct client *);
1.1       nicm       38:
                     39: int    server_client_msg_dispatch(struct client *);
                     40: void   server_client_msg_command(struct client *, struct msg_command_data *);
                     41: void   server_client_msg_identify(
1.25      nicm       42:            struct client *, struct msg_identify_data *, int);
1.1       nicm       43: void   server_client_msg_shell(struct client *);
                     44:
                     45: void printflike2 server_client_msg_error(struct cmd_ctx *, const char *, ...);
                     46: void printflike2 server_client_msg_print(struct cmd_ctx *, const char *, ...);
                     47: void printflike2 server_client_msg_info(struct cmd_ctx *, const char *, ...);
                     48:
                     49: /* Create a new client. */
                     50: void
                     51: server_client_create(int fd)
                     52: {
                     53:        struct client   *c;
                     54:        u_int            i;
                     55:
1.49      nicm       56:        setblocking(fd, 0);
1.1       nicm       57:
                     58:        c = xcalloc(1, sizeof *c);
                     59:        c->references = 0;
                     60:        imsg_init(&c->ibuf, fd);
1.14      nicm       61:        server_update_event(c);
1.25      nicm       62:
1.10      nicm       63:        if (gettimeofday(&c->creation_time, NULL) != 0)
1.1       nicm       64:                fatal("gettimeofday failed");
1.11      nicm       65:        memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.1       nicm       66:
1.73    ! nicm       67:        c->stdin_data = evbuffer_new ();
        !            68:        c->stdout_data = evbuffer_new ();
        !            69:        c->stderr_data = evbuffer_new ();
1.33      nicm       70:
1.1       nicm       71:        c->tty.fd = -1;
                     72:        c->title = NULL;
                     73:
                     74:        c->session = NULL;
1.45      nicm       75:        c->last_session = NULL;
1.1       nicm       76:        c->tty.sx = 80;
                     77:        c->tty.sy = 24;
                     78:
                     79:        screen_init(&c->status, c->tty.sx, 1, 0);
1.51      nicm       80:        RB_INIT(&c->status_new);
                     81:        RB_INIT(&c->status_old);
1.1       nicm       82:
                     83:        c->message_string = NULL;
1.21      nicm       84:        ARRAY_INIT(&c->message_log);
1.1       nicm       85:
                     86:        c->prompt_string = NULL;
                     87:        c->prompt_buffer = NULL;
                     88:        c->prompt_index = 0;
                     89:
1.57      nicm       90:        c->last_mouse.b = MOUSE_UP;
                     91:        c->last_mouse.x = c->last_mouse.y = -1;
                     92:
1.17      nicm       93:        evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
                     94:
1.1       nicm       95:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     96:                if (ARRAY_ITEM(&clients, i) == NULL) {
                     97:                        ARRAY_SET(&clients, i, c);
                     98:                        return;
                     99:                }
                    100:        }
                    101:        ARRAY_ADD(&clients, c);
                    102:        log_debug("new client %d", fd);
1.72      nicm      103: }
                    104:
                    105: /* Open client terminal if needed. */
                    106: int
                    107: server_client_open(struct client *c, struct session *s, char **cause)
                    108: {
                    109:        struct options  *oo = s != NULL ? &s->options : &global_s_options;
                    110:        char            *overrides;
                    111:
                    112:        if (!(c->flags & CLIENT_TERMINAL)) {
                    113:                *cause = xstrdup ("not a terminal");
                    114:                return (-1);
                    115:        }
                    116:
                    117:        overrides = options_get_string(oo, "terminal-overrides");
                    118:        if (tty_open(&c->tty, overrides, cause) != 0)
                    119:                return (-1);
                    120:
                    121:        return (0);
1.1       nicm      122: }
                    123:
                    124: /* Lost a client. */
                    125: void
                    126: server_client_lost(struct client *c)
                    127: {
1.21      nicm      128:        struct message_entry    *msg;
                    129:        u_int                    i;
1.1       nicm      130:
                    131:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    132:                if (ARRAY_ITEM(&clients, i) == c)
                    133:                        ARRAY_SET(&clients, i, NULL);
                    134:        }
                    135:        log_debug("lost client %d", c->ibuf.fd);
                    136:
                    137:        /*
                    138:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    139:         * and tty_free might close an unrelated fd.
                    140:         */
                    141:        if (c->flags & CLIENT_TERMINAL)
                    142:                tty_free(&c->tty);
                    143:
1.73    ! nicm      144:        evbuffer_free (c->stdin_data);
        !           145:        evbuffer_free (c->stdout_data);
        !           146:        evbuffer_free (c->stderr_data);
1.33      nicm      147:
1.51      nicm      148:        status_free_jobs(&c->status_new);
                    149:        status_free_jobs(&c->status_old);
1.1       nicm      150:        screen_free(&c->status);
                    151:
                    152:        if (c->title != NULL)
                    153:                xfree(c->title);
                    154:
1.17      nicm      155:        evtimer_del(&c->repeat_timer);
                    156:
1.69      nicm      157:        if (event_initialized(&c->identify_timer))
                    158:                evtimer_del(&c->identify_timer);
1.15      nicm      159:
1.1       nicm      160:        if (c->message_string != NULL)
                    161:                xfree(c->message_string);
1.69      nicm      162:        if (event_initialized (&c->message_timer))
                    163:                evtimer_del(&c->message_timer);
1.21      nicm      164:        for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
                    165:                msg = &ARRAY_ITEM(&c->message_log, i);
                    166:                xfree(msg->msg);
                    167:        }
                    168:        ARRAY_FREE(&c->message_log);
1.1       nicm      169:
                    170:        if (c->prompt_string != NULL)
                    171:                xfree(c->prompt_string);
                    172:        if (c->prompt_buffer != NULL)
                    173:                xfree(c->prompt_buffer);
                    174:
                    175:        if (c->cwd != NULL)
                    176:                xfree(c->cwd);
1.61      nicm      177:
                    178:        environ_free(&c->environ);
1.1       nicm      179:
                    180:        close(c->ibuf.fd);
                    181:        imsg_clear(&c->ibuf);
1.69      nicm      182:        if (event_initialized(&c->event))
                    183:                event_del(&c->event);
1.13      nicm      184:
1.1       nicm      185:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                    186:                if (ARRAY_ITEM(&dead_clients, i) == NULL) {
                    187:                        ARRAY_SET(&dead_clients, i, c);
                    188:                        break;
                    189:                }
                    190:        }
                    191:        if (i == ARRAY_LENGTH(&dead_clients))
                    192:                ARRAY_ADD(&dead_clients, c);
                    193:        c->flags |= CLIENT_DEAD;
1.71      nicm      194:
                    195:        server_add_accept(0); /* may be more file descriptors now */
1.1       nicm      196:
                    197:        recalculate_sizes();
1.41      nicm      198:        server_check_unattached();
1.19      nicm      199:        server_update_socket();
1.9       nicm      200: }
                    201:
1.1       nicm      202: /* Process a single client event. */
                    203: void
1.12      nicm      204: server_client_callback(int fd, short events, void *data)
1.1       nicm      205: {
                    206:        struct client   *c = data;
1.7       nicm      207:
                    208:        if (c->flags & CLIENT_DEAD)
                    209:                return;
1.1       nicm      210:
                    211:        if (fd == c->ibuf.fd) {
1.12      nicm      212:                if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
1.1       nicm      213:                        goto client_lost;
                    214:
                    215:                if (c->flags & CLIENT_BAD) {
                    216:                        if (c->ibuf.w.queued == 0)
                    217:                                goto client_lost;
                    218:                        return;
                    219:                }
                    220:
1.12      nicm      221:                if (events & EV_READ && server_client_msg_dispatch(c) != 0)
1.1       nicm      222:                        goto client_lost;
                    223:        }
1.14      nicm      224:
1.73    ! nicm      225:        server_push_stdout(c);
        !           226:        server_push_stderr(c);
        !           227:
1.25      nicm      228:        server_update_event(c);
1.1       nicm      229:        return;
                    230:
                    231: client_lost:
                    232:        server_client_lost(c);
                    233: }
                    234:
1.16      nicm      235: /* Handle client status timer. */
                    236: void
                    237: server_client_status_timer(void)
                    238: {
                    239:        struct client   *c;
                    240:        struct session  *s;
                    241:        struct timeval   tv;
1.20      nicm      242:        u_int            i;
                    243:        int              interval;
                    244:        time_t           difference;
1.16      nicm      245:
                    246:        if (gettimeofday(&tv, NULL) != 0)
                    247:                fatal("gettimeofday failed");
                    248:
                    249:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    250:                c = ARRAY_ITEM(&clients, i);
                    251:                if (c == NULL || c->session == NULL)
                    252:                        continue;
                    253:                if (c->message_string != NULL || c->prompt_string != NULL) {
                    254:                        /*
                    255:                         * Don't need timed redraw for messages/prompts so bail
                    256:                         * now. The status timer isn't reset when they are
                    257:                         * redrawn anyway.
                    258:                         */
                    259:                        continue;
                    260:                }
                    261:                s = c->session;
                    262:
                    263:                if (!options_get_number(&s->options, "status"))
                    264:                        continue;
                    265:                interval = options_get_number(&s->options, "status-interval");
                    266:
1.20      nicm      267:                difference = tv.tv_sec - c->status_timer.tv_sec;
                    268:                if (difference >= interval) {
1.51      nicm      269:                        status_update_jobs(c);
1.16      nicm      270:                        c->flags |= CLIENT_STATUS;
                    271:                }
                    272:        }
                    273: }
                    274:
1.66      nicm      275: /* Check for mouse keys. */
                    276: void
                    277: server_client_check_mouse(
                    278:     struct client *c, struct window_pane *wp, struct mouse_event *mouse)
                    279: {
                    280:        struct session  *s = c->session;
                    281:        struct options  *oo = &s->options;
                    282:        int              statusat;
                    283:
                    284:        statusat = status_at_line(c);
                    285:
                    286:        /* Is this a window selection click on the status line? */
                    287:        if (statusat != -1 && mouse->y == (u_int)statusat &&
                    288:            options_get_number(oo, "mouse-select-window")) {
                    289:                if (mouse->b == MOUSE_UP && c->last_mouse.b != MOUSE_UP) {
                    290:                        status_set_window_at(c, mouse->x);
1.70      nicm      291:                        recalculate_sizes();
1.66      nicm      292:                        return;
                    293:                }
                    294:                if (mouse->b & MOUSE_45) {
                    295:                        if ((mouse->b & MOUSE_BUTTON) == MOUSE_1) {
                    296:                                session_previous(c->session, 0);
                    297:                                server_redraw_session(s);
1.70      nicm      298:                                recalculate_sizes();
1.66      nicm      299:                        }
                    300:                        if ((mouse->b & MOUSE_BUTTON) == MOUSE_2) {
                    301:                                session_next(c->session, 0);
                    302:                                server_redraw_session(s);
1.70      nicm      303:                                recalculate_sizes();
1.66      nicm      304:                        }
                    305:                        return;
                    306:                }
1.67      nicm      307:                memcpy(&c->last_mouse, mouse, sizeof c->last_mouse);
                    308:                return;
1.66      nicm      309:        }
                    310:
                    311:        /*
                    312:         * Not on status line - adjust mouse position if status line is at the
                    313:         * top and limit if at the bottom. From here on a struct mouse
                    314:         * represents the offset onto the window itself.
                    315:         */
                    316:        if (statusat == 0 &&mouse->y > 0)
                    317:                mouse->y--;
                    318:        else if (statusat > 0 && mouse->y >= (u_int)statusat)
                    319:                mouse->y = statusat - 1;
                    320:
                    321:        /* Is this a pane selection? Allow down only in copy mode. */
                    322:        if (options_get_number(oo, "mouse-select-pane") &&
                    323:            ((!(mouse->b & MOUSE_DRAG) && mouse->b != MOUSE_UP) ||
                    324:            wp->mode != &window_copy_mode)) {
                    325:                window_set_active_at(wp->window, mouse->x, mouse->y);
                    326:                server_redraw_window_borders(wp->window);
                    327:                wp = wp->window->active; /* may have changed */
                    328:        }
                    329:
                    330:        /* Check if trying to resize pane. */
                    331:        if (options_get_number(oo, "mouse-resize-pane"))
                    332:                layout_resize_pane_mouse(c, mouse);
                    333:
                    334:        /* Update last and pass through to client. */
                    335:        memcpy(&c->last_mouse, mouse, sizeof c->last_mouse);
                    336:        window_pane_mouse(wp, c->session, mouse);
                    337: }
                    338:
1.18      nicm      339: /* Handle data key input from client. */
                    340: void
                    341: server_client_handle_key(int key, struct mouse_event *mouse, void *data)
                    342: {
                    343:        struct client           *c = data;
                    344:        struct session          *s;
                    345:        struct window           *w;
                    346:        struct window_pane      *wp;
                    347:        struct options          *oo;
                    348:        struct timeval           tv;
                    349:        struct key_binding      *bd;
                    350:        int                      xtimeout, isprefix;
                    351:
                    352:        /* Check the client is good to accept input. */
                    353:        if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
                    354:                return;
                    355:        if (c->session == NULL)
                    356:                return;
                    357:        s = c->session;
                    358:
                    359:        /* Update the activity timer. */
                    360:        if (gettimeofday(&c->activity_time, NULL) != 0)
                    361:                fatal("gettimeofday failed");
                    362:        memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
                    363:
                    364:        w = c->session->curw->window;
                    365:        wp = w->active;
                    366:        oo = &c->session->options;
                    367:
                    368:        /* Special case: number keys jump to pane in identify mode. */
1.25      nicm      369:        if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30      nicm      370:                if (c->flags & CLIENT_READONLY)
                    371:                        return;
1.18      nicm      372:                wp = window_pane_at_index(w, key - '0');
                    373:                if (wp != NULL && window_pane_visible(wp))
                    374:                        window_set_active_pane(w, wp);
                    375:                server_clear_identify(c);
                    376:                return;
                    377:        }
                    378:
                    379:        /* Handle status line. */
1.30      nicm      380:        if (!(c->flags & CLIENT_READONLY)) {
                    381:                status_message_clear(c);
                    382:                server_clear_identify(c);
                    383:        }
1.18      nicm      384:        if (c->prompt_string != NULL) {
1.30      nicm      385:                if (!(c->flags & CLIENT_READONLY))
                    386:                        status_prompt_key(c, key);
1.18      nicm      387:                return;
                    388:        }
                    389:
                    390:        /* Check for mouse keys. */
                    391:        if (key == KEYC_MOUSE) {
1.30      nicm      392:                if (c->flags & CLIENT_READONLY)
                    393:                        return;
1.66      nicm      394:                server_client_check_mouse(c, wp, mouse);
1.18      nicm      395:                return;
                    396:        }
                    397:
                    398:        /* Is this a prefix key? */
1.63      nicm      399:        if (key == options_get_number(&c->session->options, "prefix"))
                    400:                isprefix = 1;
                    401:        else if (key == options_get_number(&c->session->options, "prefix2"))
                    402:                isprefix = 1;
                    403:        else
                    404:                isprefix = 0;
1.18      nicm      405:
                    406:        /* No previous prefix key. */
                    407:        if (!(c->flags & CLIENT_PREFIX)) {
                    408:                if (isprefix)
                    409:                        c->flags |= CLIENT_PREFIX;
                    410:                else {
                    411:                        /* Try as a non-prefix key binding. */
1.30      nicm      412:                        if ((bd = key_bindings_lookup(key)) == NULL) {
                    413:                                if (!(c->flags & CLIENT_READONLY))
1.31      nicm      414:                                        window_pane_key(wp, c->session, key);
1.30      nicm      415:                        } else
1.18      nicm      416:                                key_bindings_dispatch(bd, c);
                    417:                }
                    418:                return;
                    419:        }
                    420:
                    421:        /* Prefix key already pressed. Reset prefix and lookup key. */
                    422:        c->flags &= ~CLIENT_PREFIX;
                    423:        if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
                    424:                /* If repeating, treat this as a key, else ignore. */
                    425:                if (c->flags & CLIENT_REPEAT) {
                    426:                        c->flags &= ~CLIENT_REPEAT;
                    427:                        if (isprefix)
                    428:                                c->flags |= CLIENT_PREFIX;
1.30      nicm      429:                        else if (!(c->flags & CLIENT_READONLY))
1.31      nicm      430:                                window_pane_key(wp, c->session, key);
1.18      nicm      431:                }
                    432:                return;
                    433:        }
                    434:
                    435:        /* If already repeating, but this key can't repeat, skip it. */
                    436:        if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
                    437:                c->flags &= ~CLIENT_REPEAT;
                    438:                if (isprefix)
                    439:                        c->flags |= CLIENT_PREFIX;
1.30      nicm      440:                else if (!(c->flags & CLIENT_READONLY))
1.31      nicm      441:                        window_pane_key(wp, c->session, key);
1.18      nicm      442:                return;
                    443:        }
                    444:
                    445:        /* If this key can repeat, reset the repeat flags and timer. */
                    446:        xtimeout = options_get_number(&c->session->options, "repeat-time");
                    447:        if (xtimeout != 0 && bd->can_repeat) {
                    448:                c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
1.25      nicm      449:
1.18      nicm      450:                tv.tv_sec = xtimeout / 1000;
                    451:                tv.tv_usec = (xtimeout % 1000) * 1000L;
                    452:                evtimer_del(&c->repeat_timer);
                    453:                evtimer_add(&c->repeat_timer, &tv);
                    454:        }
                    455:
                    456:        /* Dispatch the command. */
                    457:        key_bindings_dispatch(bd, c);
                    458: }
                    459:
1.2       nicm      460: /* Client functions that need to happen every loop. */
                    461: void
                    462: server_client_loop(void)
                    463: {
                    464:        struct client           *c;
                    465:        struct window           *w;
                    466:        struct window_pane      *wp;
                    467:        u_int                    i;
                    468:
                    469:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    470:                c = ARRAY_ITEM(&clients, i);
1.36      nicm      471:                if (c == NULL)
1.2       nicm      472:                        continue;
                    473:
1.36      nicm      474:                server_client_check_exit(c);
                    475:                if (c->session != NULL) {
                    476:                        server_client_check_redraw(c);
                    477:                        server_client_reset_state(c);
                    478:                }
1.2       nicm      479:        }
                    480:
                    481:        /*
                    482:         * Any windows will have been redrawn as part of clients, so clear
                    483:         * their flags now.
                    484:         */
                    485:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    486:                w = ARRAY_ITEM(&windows, i);
                    487:                if (w == NULL)
                    488:                        continue;
                    489:
                    490:                w->flags &= ~WINDOW_REDRAW;
                    491:                TAILQ_FOREACH(wp, &w->panes, entry)
                    492:                        wp->flags &= ~PANE_REDRAW;
                    493:        }
                    494: }
                    495:
1.18      nicm      496: /*
                    497:  * Update cursor position and mode settings. The scroll region and attributes
                    498:  * are cleared when idle (waiting for an event) as this is the most likely time
                    499:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                    500:  * compromise between excessive resets and likelihood of an interrupt.
                    501:  *
                    502:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                    503:  * things that are already in their default state.
                    504:  */
1.1       nicm      505: void
1.18      nicm      506: server_client_reset_state(struct client *c)
1.1       nicm      507: {
1.18      nicm      508:        struct window           *w = c->session->curw->window;
                    509:        struct window_pane      *wp = w->active;
                    510:        struct screen           *s = wp->screen;
                    511:        struct options          *oo = &c->session->options;
1.54      nicm      512:        struct options          *wo = &w->options;
1.66      nicm      513:        int                      status, mode, o;
1.60      nicm      514:
                    515:        if (c->flags & CLIENT_SUSPENDED)
                    516:                return;
1.1       nicm      517:
                    518:        tty_region(&c->tty, 0, c->tty.sy - 1);
                    519:
                    520:        status = options_get_number(oo, "status");
                    521:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                    522:                tty_cursor(&c->tty, 0, 0);
1.66      nicm      523:        else {
                    524:                o = status && options_get_number (oo, "status-position") == 0;
                    525:                tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
                    526:        }
1.1       nicm      527:
1.50      nicm      528:        /*
1.57      nicm      529:         * Resizing panes with the mouse requires at least button mode to give
                    530:         * a smooth appearance.
                    531:         */
                    532:        mode = s->mode;
                    533:        if ((c->last_mouse.b & MOUSE_RESIZE_PANE) &&
                    534:            !(mode & (MODE_MOUSE_BUTTON|MODE_MOUSE_ANY)))
                    535:                mode |= MODE_MOUSE_BUTTON;
                    536:
                    537:        /*
1.50      nicm      538:         * Any mode will do for mouse-select-pane, but set standard mode if
                    539:         * none.
                    540:         */
1.54      nicm      541:        if ((mode & ALL_MOUSE_MODES) == 0) {
                    542:                if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
1.55      nicm      543:                    options_get_number(oo, "mouse-select-pane"))
1.57      nicm      544:                        mode |= MODE_MOUSE_STANDARD;
                    545:                else if (options_get_number(oo, "mouse-resize-pane"))
1.54      nicm      546:                        mode |= MODE_MOUSE_STANDARD;
                    547:                else if (options_get_number(oo, "mouse-select-window"))
                    548:                        mode |= MODE_MOUSE_STANDARD;
                    549:                else if (options_get_number(wo, "mode-mouse"))
                    550:                        mode |= MODE_MOUSE_STANDARD;
                    551:        }
1.48      nicm      552:
                    553:        /*
                    554:         * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
                    555:         * user has set mouse-utf8 and any mouse mode is in effect, turn on
                    556:         * UTF-8 mouse input. If the receiving terminal hasn't requested it
                    557:         * (that is, it isn't in s->mode), then it'll be converted in
                    558:         * input_mouse.
                    559:         */
                    560:        if ((c->tty.flags & TTY_UTF8) &&
                    561:            (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
                    562:                mode |= MODE_MOUSE_UTF8;
                    563:        else
                    564:                mode &= ~MODE_MOUSE_UTF8;
                    565:
                    566:        /* Set the terminal mode and reset attributes. */
1.59      nicm      567:        tty_update_mode(&c->tty, mode, s);
1.1       nicm      568:        tty_reset(&c->tty);
1.17      nicm      569: }
                    570:
                    571: /* Repeat time callback. */
1.24      nicm      572: /* ARGSUSED */
1.17      nicm      573: void
                    574: server_client_repeat_timer(unused int fd, unused short events, void *data)
                    575: {
                    576:        struct client   *c = data;
                    577:
                    578:        if (c->flags & CLIENT_REPEAT)
                    579:                c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
1.1       nicm      580: }
                    581:
1.36      nicm      582: /* Check if client should be exited. */
                    583: void
                    584: server_client_check_exit(struct client *c)
                    585: {
                    586:        struct msg_exit_data    exitdata;
                    587:
                    588:        if (!(c->flags & CLIENT_EXIT))
                    589:                return;
                    590:
1.73    ! nicm      591:        if (EVBUFFER_LENGTH(c->stdin_data) != 0)
        !           592:                return;
        !           593:        if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36      nicm      594:                return;
1.73    ! nicm      595:        if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36      nicm      596:                return;
                    597:
                    598:        exitdata.retcode = c->retcode;
                    599:        server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
                    600:
                    601:        c->flags &= ~CLIENT_EXIT;
1.38      nicm      602: }
                    603:
1.1       nicm      604: /* Check for client redraws. */
                    605: void
                    606: server_client_check_redraw(struct client *c)
                    607: {
                    608:        struct session          *s = c->session;
                    609:        struct window_pane      *wp;
                    610:        int                      flags, redraw;
                    611:
                    612:        flags = c->tty.flags & TTY_FREEZE;
                    613:        c->tty.flags &= ~TTY_FREEZE;
                    614:
                    615:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
                    616:                if (options_get_number(&s->options, "set-titles"))
                    617:                        server_client_set_title(c);
1.12      nicm      618:
1.1       nicm      619:                if (c->message_string != NULL)
                    620:                        redraw = status_message_redraw(c);
                    621:                else if (c->prompt_string != NULL)
                    622:                        redraw = status_prompt_redraw(c);
                    623:                else
                    624:                        redraw = status_redraw(c);
                    625:                if (!redraw)
                    626:                        c->flags &= ~CLIENT_STATUS;
                    627:        }
                    628:
                    629:        if (c->flags & CLIENT_REDRAW) {
1.27      nicm      630:                screen_redraw_screen(c, 0, 0);
                    631:                c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38      nicm      632:        } else if (c->flags & CLIENT_REDRAWWINDOW) {
                    633:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
                    634:                        screen_redraw_pane(c, wp);
                    635:                c->flags &= ~CLIENT_REDRAWWINDOW;
1.1       nicm      636:        } else {
                    637:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
                    638:                        if (wp->flags & PANE_REDRAW)
                    639:                                screen_redraw_pane(c, wp);
                    640:                }
                    641:        }
                    642:
1.27      nicm      643:        if (c->flags & CLIENT_BORDERS)
                    644:                screen_redraw_screen(c, 0, 1);
                    645:
1.1       nicm      646:        if (c->flags & CLIENT_STATUS)
1.27      nicm      647:                screen_redraw_screen(c, 1, 0);
1.1       nicm      648:
                    649:        c->tty.flags |= flags;
                    650:
1.27      nicm      651:        c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
1.1       nicm      652: }
                    653:
                    654: /* Set client title. */
                    655: void
                    656: server_client_set_title(struct client *c)
                    657: {
                    658:        struct session  *s = c->session;
                    659:        const char      *template;
                    660:        char            *title;
                    661:
                    662:        template = options_get_string(&s->options, "set-titles-string");
1.25      nicm      663:
1.52      nicm      664:        title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
1.1       nicm      665:        if (c->title == NULL || strcmp(title, c->title) != 0) {
                    666:                if (c->title != NULL)
                    667:                        xfree(c->title);
                    668:                c->title = xstrdup(title);
                    669:                tty_set_title(&c->tty, c->title);
                    670:        }
                    671:        xfree(title);
                    672: }
                    673:
                    674: /* Dispatch message from client. */
                    675: int
                    676: server_client_msg_dispatch(struct client *c)
                    677: {
                    678:        struct imsg              imsg;
                    679:        struct msg_command_data  commanddata;
                    680:        struct msg_identify_data identifydata;
                    681:        struct msg_environ_data  environdata;
1.73    ! nicm      682:        struct msg_stdin_data    stdindata;
1.1       nicm      683:        ssize_t                  n, datalen;
                    684:
1.8       deraadt   685:        if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
                    686:                return (-1);
1.1       nicm      687:
                    688:        for (;;) {
                    689:                if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
                    690:                        return (-1);
                    691:                if (n == 0)
                    692:                        return (0);
                    693:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
                    694:
                    695:                if (imsg.hdr.peerid != PROTOCOL_VERSION) {
                    696:                        server_write_client(c, MSG_VERSION, NULL, 0);
                    697:                        c->flags |= CLIENT_BAD;
                    698:                        imsg_free(&imsg);
                    699:                        continue;
                    700:                }
                    701:
                    702:                log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
                    703:                switch (imsg.hdr.type) {
                    704:                case MSG_COMMAND:
                    705:                        if (datalen != sizeof commanddata)
                    706:                                fatalx("bad MSG_COMMAND size");
                    707:                        memcpy(&commanddata, imsg.data, sizeof commanddata);
                    708:
                    709:                        server_client_msg_command(c, &commanddata);
                    710:                        break;
                    711:                case MSG_IDENTIFY:
                    712:                        if (datalen != sizeof identifydata)
                    713:                                fatalx("bad MSG_IDENTIFY size");
                    714:                        if (imsg.fd == -1)
                    715:                                fatalx("MSG_IDENTIFY missing fd");
                    716:                        memcpy(&identifydata, imsg.data, sizeof identifydata);
                    717:
                    718:                        server_client_msg_identify(c, &identifydata, imsg.fd);
                    719:                        break;
1.73    ! nicm      720:                case MSG_STDIN:
        !           721:                        if (datalen != sizeof stdindata)
        !           722:                                fatalx("bad MSG_STDIN size");
        !           723:                        memcpy(&stdindata, imsg.data, sizeof stdindata);
1.36      nicm      724:
1.73    ! nicm      725:                        if (c->stdin_callback == NULL)
        !           726:                                break;
        !           727:                        if (stdindata.size <= 0)
        !           728:                                c->stdin_closed = 1;
        !           729:                        else {
        !           730:                                evbuffer_add(c->stdin_data, stdindata.data,
        !           731:                                    stdindata.size);
        !           732:                        }
        !           733:                        c->stdin_callback(c, c->stdin_closed,
        !           734:                            c->stdin_callback_data);
1.33      nicm      735:                        break;
1.1       nicm      736:                case MSG_RESIZE:
                    737:                        if (datalen != 0)
                    738:                                fatalx("bad MSG_RESIZE size");
                    739:
1.32      nicm      740:                        if (tty_resize(&c->tty)) {
                    741:                                recalculate_sizes();
                    742:                                server_redraw_client(c);
                    743:                        }
1.1       nicm      744:                        break;
                    745:                case MSG_EXITING:
                    746:                        if (datalen != 0)
                    747:                                fatalx("bad MSG_EXITING size");
                    748:
                    749:                        c->session = NULL;
                    750:                        tty_close(&c->tty);
                    751:                        server_write_client(c, MSG_EXITED, NULL, 0);
                    752:                        break;
                    753:                case MSG_WAKEUP:
                    754:                case MSG_UNLOCK:
                    755:                        if (datalen != 0)
                    756:                                fatalx("bad MSG_WAKEUP size");
                    757:
                    758:                        if (!(c->flags & CLIENT_SUSPENDED))
                    759:                                break;
                    760:                        c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm      761:
1.11      nicm      762:                        if (gettimeofday(&c->activity_time, NULL) != 0)
                    763:                                fatal("gettimeofday");
1.47      nicm      764:                        if (c->session != NULL)
                    765:                                session_update_activity(c->session);
1.10      nicm      766:
1.1       nicm      767:                        tty_start_tty(&c->tty);
                    768:                        server_redraw_client(c);
                    769:                        recalculate_sizes();
                    770:                        break;
                    771:                case MSG_ENVIRON:
                    772:                        if (datalen != sizeof environdata)
                    773:                                fatalx("bad MSG_ENVIRON size");
                    774:                        memcpy(&environdata, imsg.data, sizeof environdata);
                    775:
                    776:                        environdata.var[(sizeof environdata.var) - 1] = '\0';
                    777:                        if (strchr(environdata.var, '=') != NULL)
                    778:                                environ_put(&c->environ, environdata.var);
                    779:                        break;
                    780:                case MSG_SHELL:
                    781:                        if (datalen != 0)
                    782:                                fatalx("bad MSG_SHELL size");
                    783:
                    784:                        server_client_msg_shell(c);
                    785:                        break;
                    786:                default:
                    787:                        fatalx("unexpected message");
                    788:                }
                    789:
                    790:                imsg_free(&imsg);
                    791:        }
                    792: }
                    793:
                    794: /* Callback to send error message to client. */
                    795: void printflike2
                    796: server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
                    797: {
1.33      nicm      798:        va_list ap;
1.1       nicm      799:
                    800:        va_start(ap, fmt);
1.73    ! nicm      801:        evbuffer_add_vprintf(ctx->cmdclient->stderr_data, fmt, ap);
1.1       nicm      802:        va_end(ap);
                    803:
1.73    ! nicm      804:        evbuffer_add(ctx->cmdclient->stderr_data, "\n", 1);
        !           805:        server_push_stderr(ctx->cmdclient);
1.34      nicm      806:        ctx->cmdclient->retcode = 1;
1.1       nicm      807: }
                    808:
                    809: /* Callback to send print message to client. */
                    810: void printflike2
                    811: server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
                    812: {
1.33      nicm      813:        va_list ap;
1.1       nicm      814:
                    815:        va_start(ap, fmt);
1.73    ! nicm      816:        evbuffer_add_vprintf(ctx->cmdclient->stdout_data, fmt, ap);
1.1       nicm      817:        va_end(ap);
                    818:
1.73    ! nicm      819:        evbuffer_add(ctx->cmdclient->stdout_data, "\n", 1);
        !           820:        server_push_stdout(ctx->cmdclient);
1.1       nicm      821: }
                    822:
                    823: /* Callback to send print message to client, if not quiet. */
                    824: void printflike2
                    825: server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
                    826: {
1.33      nicm      827:        va_list ap;
1.1       nicm      828:
1.26      nicm      829:        if (options_get_number(&global_options, "quiet"))
1.1       nicm      830:                return;
                    831:
                    832:        va_start(ap, fmt);
1.73    ! nicm      833:        evbuffer_add_vprintf(ctx->cmdclient->stdout_data, fmt, ap);
1.1       nicm      834:        va_end(ap);
                    835:
1.73    ! nicm      836:        evbuffer_add(ctx->cmdclient->stdout_data, "\n", 1);
        !           837:        server_push_stdout(ctx->cmdclient);
1.1       nicm      838: }
                    839:
                    840: /* Handle command message. */
                    841: void
                    842: server_client_msg_command(struct client *c, struct msg_command_data *data)
                    843: {
1.36      nicm      844:        struct cmd_ctx   ctx;
                    845:        struct cmd_list *cmdlist = NULL;
                    846:        int              argc;
                    847:        char           **argv, *cause;
1.1       nicm      848:
                    849:        ctx.error = server_client_msg_error;
                    850:        ctx.print = server_client_msg_print;
                    851:        ctx.info = server_client_msg_info;
                    852:
                    853:        ctx.msgdata = data;
                    854:        ctx.curclient = NULL;
                    855:
                    856:        ctx.cmdclient = c;
                    857:
                    858:        argc = data->argc;
                    859:        data->argv[(sizeof data->argv) - 1] = '\0';
                    860:        if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
                    861:                server_client_msg_error(&ctx, "command too long");
                    862:                goto error;
                    863:        }
                    864:
                    865:        if (argc == 0) {
                    866:                argc = 1;
                    867:                argv = xcalloc(1, sizeof *argv);
                    868:                *argv = xstrdup("new-session");
                    869:        }
                    870:
                    871:        if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
                    872:                server_client_msg_error(&ctx, "%s", cause);
                    873:                cmd_free_argv(argc, argv);
                    874:                goto error;
                    875:        }
                    876:        cmd_free_argv(argc, argv);
                    877:
1.36      nicm      878:        if (cmd_list_exec(cmdlist, &ctx) != 1)
                    879:                c->flags |= CLIENT_EXIT;
1.1       nicm      880:        cmd_list_free(cmdlist);
                    881:        return;
                    882:
                    883: error:
                    884:        if (cmdlist != NULL)
                    885:                cmd_list_free(cmdlist);
1.36      nicm      886:        c->flags |= CLIENT_EXIT;
1.1       nicm      887: }
                    888:
                    889: /* Handle identify message. */
                    890: void
                    891: server_client_msg_identify(
                    892:     struct client *c, struct msg_identify_data *data, int fd)
                    893: {
                    894:        c->cwd = NULL;
                    895:        data->cwd[(sizeof data->cwd) - 1] = '\0';
                    896:        if (*data->cwd != '\0')
                    897:                c->cwd = xstrdup(data->cwd);
                    898:
1.33      nicm      899:        if (!isatty(fd))
                    900:            return;
1.1       nicm      901:        data->term[(sizeof data->term) - 1] = '\0';
1.73    ! nicm      902:        tty_init(&c->tty, fd, data->term);
1.1       nicm      903:        if (data->flags & IDENTIFY_UTF8)
                    904:                c->tty.flags |= TTY_UTF8;
                    905:        if (data->flags & IDENTIFY_256COLOURS)
                    906:                c->tty.term_flags |= TERM_256COLOURS;
                    907:        else if (data->flags & IDENTIFY_88COLOURS)
                    908:                c->tty.term_flags |= TERM_88COLOURS;
1.18      nicm      909:        c->tty.key_callback = server_client_handle_key;
                    910:        c->tty.key_data = c;
1.1       nicm      911:
                    912:        tty_resize(&c->tty);
                    913:
                    914:        c->flags |= CLIENT_TERMINAL;
                    915: }
                    916:
                    917: /* Handle shell message. */
                    918: void
                    919: server_client_msg_shell(struct client *c)
                    920: {
                    921:        struct msg_shell_data    data;
                    922:        const char              *shell;
1.25      nicm      923:
1.1       nicm      924:        shell = options_get_string(&global_s_options, "default-shell");
                    925:
                    926:        if (*shell == '\0' || areshell(shell))
                    927:                shell = _PATH_BSHELL;
                    928:        if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
                    929:                strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
1.25      nicm      930:
1.1       nicm      931:        server_write_client(c, MSG_SHELL, &data, sizeof data);
                    932:        c->flags |= CLIENT_BAD; /* it will die after exec */
                    933: }