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

1.146   ! nicm        1: /* $OpenBSD: server-client.c,v 1.145 2015/07/13 15:51:31 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>
1.92      nicm       20: #include <sys/ioctl.h>
1.1       nicm       21:
1.114     benno      22: #include <errno.h>
1.12      nicm       23: #include <event.h>
1.1       nicm       24: #include <fcntl.h>
1.98      nicm       25: #include <paths.h>
                     26: #include <stdlib.h>
1.1       nicm       27: #include <string.h>
1.4       nicm       28: #include <time.h>
1.1       nicm       29: #include <unistd.h>
                     30:
                     31: #include "tmux.h"
                     32:
1.132     nicm       33: void   server_client_key_table(struct client *, const char *);
1.141     nicm       34: void   server_client_free(int, short, void *);
1.91      nicm       35: void   server_client_check_focus(struct window_pane *);
1.92      nicm       36: void   server_client_check_resize(struct window_pane *);
1.131     nicm       37: int    server_client_check_mouse(struct client *);
1.17      nicm       38: void   server_client_repeat_timer(int, short, void *);
1.36      nicm       39: void   server_client_check_exit(struct client *);
1.1       nicm       40: void   server_client_check_redraw(struct client *);
                     41: void   server_client_set_title(struct client *);
1.18      nicm       42: void   server_client_reset_state(struct client *);
1.82      nicm       43: int    server_client_assume_paste(struct session *);
1.1       nicm       44:
                     45: int    server_client_msg_dispatch(struct client *);
1.108     nicm       46: void   server_client_msg_command(struct client *, struct imsg *);
1.109     nicm       47: void   server_client_msg_identify(struct client *, struct imsg *);
1.1       nicm       48: void   server_client_msg_shell(struct client *);
1.140     nicm       49:
                     50: /* Check if this client is inside this server. */
                     51: int
                     52: server_client_check_nested(struct client *c)
                     53: {
                     54:        struct environ_entry    *envent;
                     55:        struct window_pane      *wp;
                     56:
                     57:        if (c->tty.path == NULL)
                     58:                return (0);
                     59:
                     60:        envent = environ_find(&c->environ, "TMUX");
                     61:        if (envent == NULL || *envent->value == '\0')
                     62:                return (0);
                     63:
                     64:        RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
                     65:                if (strcmp(wp->tty, c->tty.path) == 0)
                     66:                        return (1);
                     67:        }
                     68:        return (0);
                     69: }
1.1       nicm       70:
1.132     nicm       71: /* Set client key table. */
                     72: void
                     73: server_client_key_table(struct client *c, const char *name)
                     74: {
                     75:        key_bindings_unref_table(c->keytable);
                     76:        c->keytable = key_bindings_get_table(name, 1);
                     77:        c->keytable->references++;
                     78: }
                     79:
1.1       nicm       80: /* Create a new client. */
                     81: void
                     82: server_client_create(int fd)
                     83: {
                     84:        struct client   *c;
                     85:
1.49      nicm       86:        setblocking(fd, 0);
1.1       nicm       87:
                     88:        c = xcalloc(1, sizeof *c);
1.141     nicm       89:        c->references = 1;
1.1       nicm       90:        imsg_init(&c->ibuf, fd);
1.14      nicm       91:        server_update_event(c);
1.25      nicm       92:
1.10      nicm       93:        if (gettimeofday(&c->creation_time, NULL) != 0)
1.1       nicm       94:                fatal("gettimeofday failed");
1.11      nicm       95:        memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111     nicm       96:
                     97:        environ_init(&c->environ);
1.1       nicm       98:
1.146   ! nicm       99:        c->fd = -1;
1.145     nicm      100:        c->cwd = -1;
                    101:
1.94      nicm      102:        c->cmdq = cmdq_new(c);
                    103:        c->cmdq->client_exit = 1;
                    104:
1.116     nicm      105:        c->stdin_data = evbuffer_new();
                    106:        c->stdout_data = evbuffer_new();
                    107:        c->stderr_data = evbuffer_new();
1.33      nicm      108:
1.1       nicm      109:        c->tty.fd = -1;
                    110:        c->title = NULL;
                    111:
                    112:        c->session = NULL;
1.45      nicm      113:        c->last_session = NULL;
1.1       nicm      114:        c->tty.sx = 80;
                    115:        c->tty.sy = 24;
                    116:
                    117:        screen_init(&c->status, c->tty.sx, 1, 0);
1.51      nicm      118:        RB_INIT(&c->status_new);
                    119:        RB_INIT(&c->status_old);
1.1       nicm      120:
                    121:        c->message_string = NULL;
1.136     nicm      122:        TAILQ_INIT(&c->message_log);
1.1       nicm      123:
                    124:        c->prompt_string = NULL;
                    125:        c->prompt_buffer = NULL;
                    126:        c->prompt_index = 0;
                    127:
1.93      nicm      128:        c->flags |= CLIENT_FOCUSED;
                    129:
1.132     nicm      130:        c->keytable = key_bindings_get_table("root", 1);
                    131:        c->keytable->references++;
                    132:
1.17      nicm      133:        evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
                    134:
1.135     nicm      135:        TAILQ_INSERT_TAIL(&clients, c, entry);
1.1       nicm      136:        log_debug("new client %d", fd);
1.72      nicm      137: }
                    138:
                    139: /* Open client terminal if needed. */
                    140: int
1.119     nicm      141: server_client_open(struct client *c, char **cause)
1.72      nicm      142: {
1.75      nicm      143:        if (c->flags & CLIENT_CONTROL)
                    144:                return (0);
1.120     nicm      145:
                    146:        if (strcmp(c->ttyname, "/dev/tty") == 0) {
                    147:                *cause = xstrdup("can't use /dev/tty");
                    148:                return (-1);
                    149:        }
1.75      nicm      150:
1.72      nicm      151:        if (!(c->flags & CLIENT_TERMINAL)) {
1.116     nicm      152:                *cause = xstrdup("not a terminal");
1.72      nicm      153:                return (-1);
                    154:        }
                    155:
1.119     nicm      156:        if (tty_open(&c->tty, cause) != 0)
1.72      nicm      157:                return (-1);
                    158:
                    159:        return (0);
1.1       nicm      160: }
                    161:
                    162: /* Lost a client. */
                    163: void
                    164: server_client_lost(struct client *c)
                    165: {
1.136     nicm      166:        struct message_entry    *msg, *msg1;
1.1       nicm      167:
1.141     nicm      168:        c->flags |= CLIENT_DEAD;
                    169:
                    170:        status_prompt_clear(c);
                    171:        status_message_clear(c);
                    172:
                    173:        if (c->stdin_callback != NULL)
                    174:                c->stdin_callback(c, 1, c->stdin_callback_data);
                    175:
1.135     nicm      176:        TAILQ_REMOVE(&clients, c, entry);
1.1       nicm      177:        log_debug("lost client %d", c->ibuf.fd);
                    178:
                    179:        /*
                    180:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    181:         * and tty_free might close an unrelated fd.
                    182:         */
                    183:        if (c->flags & CLIENT_TERMINAL)
                    184:                tty_free(&c->tty);
1.109     nicm      185:        free(c->ttyname);
                    186:        free(c->term);
1.1       nicm      187:
1.113     nicm      188:        evbuffer_free(c->stdin_data);
                    189:        evbuffer_free(c->stdout_data);
1.97      nicm      190:        if (c->stderr_data != c->stdout_data)
1.126     nicm      191:                evbuffer_free(c->stderr_data);
1.33      nicm      192:
1.1       nicm      193:        screen_free(&c->status);
                    194:
1.77      nicm      195:        free(c->title);
1.109     nicm      196:        close(c->cwd);
1.1       nicm      197:
1.17      nicm      198:        evtimer_del(&c->repeat_timer);
                    199:
1.132     nicm      200:        key_bindings_unref_table(c->keytable);
                    201:
1.69      nicm      202:        if (event_initialized(&c->identify_timer))
                    203:                evtimer_del(&c->identify_timer);
1.15      nicm      204:
1.77      nicm      205:        free(c->message_string);
1.116     nicm      206:        if (event_initialized(&c->message_timer))
1.69      nicm      207:                evtimer_del(&c->message_timer);
1.136     nicm      208:        TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1.77      nicm      209:                free(msg->msg);
1.136     nicm      210:                TAILQ_REMOVE(&c->message_log, msg, entry);
                    211:                free(msg);
1.21      nicm      212:        }
1.1       nicm      213:
1.77      nicm      214:        free(c->prompt_string);
                    215:        free(c->prompt_buffer);
1.61      nicm      216:
1.94      nicm      217:        c->cmdq->dead = 1;
                    218:        cmdq_free(c->cmdq);
                    219:        c->cmdq = NULL;
                    220:
1.61      nicm      221:        environ_free(&c->environ);
1.1       nicm      222:
                    223:        close(c->ibuf.fd);
                    224:        imsg_clear(&c->ibuf);
1.69      nicm      225:        if (event_initialized(&c->event))
                    226:                event_del(&c->event);
1.13      nicm      227:
1.142     nicm      228:        server_client_unref(c);
1.71      nicm      229:
                    230:        server_add_accept(0); /* may be more file descriptors now */
1.1       nicm      231:
                    232:        recalculate_sizes();
1.41      nicm      233:        server_check_unattached();
1.19      nicm      234:        server_update_socket();
1.141     nicm      235: }
                    236:
                    237: /* Remove reference from a client. */
                    238: void
1.142     nicm      239: server_client_unref(struct client *c)
1.141     nicm      240: {
1.142     nicm      241:        log_debug("unref client %d (%d references)", c->ibuf.fd, c->references);
1.141     nicm      242:
                    243:        c->references--;
                    244:        if (c->references == 0)
                    245:                event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
                    246: }
                    247:
                    248: /* Free dead client. */
                    249: void
                    250: server_client_free(unused int fd, unused short events, void *arg)
                    251: {
                    252:        struct client   *c = arg;
                    253:
                    254:        log_debug("free client %d (%d references)", c->ibuf.fd, c->references);
                    255:
                    256:        if (c->references == 0)
                    257:                free(c);
1.9       nicm      258: }
                    259:
1.1       nicm      260: /* Process a single client event. */
                    261: void
1.12      nicm      262: server_client_callback(int fd, short events, void *data)
1.1       nicm      263: {
                    264:        struct client   *c = data;
1.7       nicm      265:
                    266:        if (c->flags & CLIENT_DEAD)
                    267:                return;
1.1       nicm      268:
                    269:        if (fd == c->ibuf.fd) {
1.122     krw       270:                if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) <= 0 &&
1.114     benno     271:                    errno != EAGAIN)
1.1       nicm      272:                        goto client_lost;
                    273:
                    274:                if (c->flags & CLIENT_BAD) {
                    275:                        if (c->ibuf.w.queued == 0)
                    276:                                goto client_lost;
                    277:                        return;
                    278:                }
                    279:
1.12      nicm      280:                if (events & EV_READ && server_client_msg_dispatch(c) != 0)
1.1       nicm      281:                        goto client_lost;
                    282:        }
1.14      nicm      283:
1.73      nicm      284:        server_push_stdout(c);
                    285:        server_push_stderr(c);
                    286:
1.25      nicm      287:        server_update_event(c);
1.1       nicm      288:        return;
                    289:
                    290: client_lost:
                    291:        server_client_lost(c);
                    292: }
                    293:
1.16      nicm      294: /* Handle client status timer. */
                    295: void
                    296: server_client_status_timer(void)
                    297: {
                    298:        struct client   *c;
                    299:        struct session  *s;
                    300:        struct timeval   tv;
1.20      nicm      301:        int              interval;
                    302:        time_t           difference;
1.16      nicm      303:
                    304:        if (gettimeofday(&tv, NULL) != 0)
                    305:                fatal("gettimeofday failed");
                    306:
1.135     nicm      307:        TAILQ_FOREACH(c, &clients, entry) {
                    308:                if (c->session == NULL)
1.16      nicm      309:                        continue;
                    310:                if (c->message_string != NULL || c->prompt_string != NULL) {
                    311:                        /*
                    312:                         * Don't need timed redraw for messages/prompts so bail
                    313:                         * now. The status timer isn't reset when they are
                    314:                         * redrawn anyway.
                    315:                         */
                    316:                        continue;
                    317:                }
                    318:                s = c->session;
                    319:
                    320:                if (!options_get_number(&s->options, "status"))
                    321:                        continue;
                    322:                interval = options_get_number(&s->options, "status-interval");
                    323:
1.20      nicm      324:                difference = tv.tv_sec - c->status_timer.tv_sec;
1.139     nicm      325:                if (interval != 0 && difference >= interval)
1.16      nicm      326:                        c->flags |= CLIENT_STATUS;
                    327:        }
                    328: }
                    329:
1.66      nicm      330: /* Check for mouse keys. */
1.131     nicm      331: int
                    332: server_client_check_mouse(struct client *c)
1.66      nicm      333: {
1.131     nicm      334:        struct session                          *s = c->session;
                    335:        struct mouse_event                      *m = &c->tty.mouse;
                    336:        struct window                           *w;
                    337:        struct window_pane                      *wp;
                    338:        enum { NOTYPE, DOWN, UP, DRAG, WHEEL }   type = NOTYPE;
                    339:        enum { NOWHERE, PANE, STATUS, BORDER }   where = NOWHERE;
                    340:        u_int                                    x, y, b;
                    341:        int                                      key;
                    342:
                    343:        log_debug("mouse %02x at %u,%u (last %u,%u) (%d)", m->b, m->x, m->y,
                    344:            m->lx, m->ly, c->tty.mouse_drag_flag);
                    345:
                    346:        /* What type of event is this? */
                    347:        if (MOUSE_DRAG(m->b)) {
                    348:                type = DRAG;
                    349:                if (c->tty.mouse_drag_flag) {
                    350:                        x = m->x, y = m->y, b = m->b;
                    351:                        log_debug("drag update at %u,%u", x, y);
                    352:                } else {
                    353:                        x = m->lx, y = m->ly, b = m->lb;
                    354:                        log_debug("drag start at %u,%u", x, y);
                    355:                }
                    356:        } else if (MOUSE_WHEEL(m->b)) {
                    357:                type = WHEEL;
                    358:                x = m->x, y = m->y, b = m->b;
                    359:                log_debug("wheel at %u,%u", x, y);
                    360:        } else if (MOUSE_BUTTONS(m->b) == 3) {
                    361:                type = UP;
                    362:                x = m->x, y = m->y, b = m->lb;
                    363:                log_debug("up at %u,%u", x, y);
                    364:        } else {
                    365:                type = DOWN;
                    366:                x = m->x, y = m->y, b = m->b;
                    367:                log_debug("down at %u,%u", x, y);
                    368:        }
                    369:        if (type == NOTYPE)
                    370:                return (KEYC_NONE);
                    371:
                    372:        /* Always save the session. */
                    373:        m->s = s->id;
                    374:
                    375:        /* Is this on the status line? */
                    376:        m->statusat = status_at_line(c);
                    377:        if (m->statusat != -1 && y == (u_int)m->statusat) {
                    378:                w = status_get_window_at(c, x);
                    379:                if (w == NULL)
                    380:                        return (KEYC_NONE);
                    381:                m->w = w->id;
                    382:                where = STATUS;
                    383:        } else
                    384:                m->w = -1;
                    385:
                    386:        /* Not on status line. Adjust position and check for border or pane. */
                    387:        if (where == NOWHERE) {
                    388:                if (m->statusat == 0 && y > 0)
                    389:                        y--;
                    390:                else if (m->statusat > 0 && y >= (u_int)m->statusat)
                    391:                        y = m->statusat - 1;
                    392:
                    393:                TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
                    394:                        if ((wp->xoff + wp->sx == x &&
                    395:                            wp->yoff <= 1 + y &&
                    396:                            wp->yoff + wp->sy >= y) ||
                    397:                            (wp->yoff + wp->sy == y &&
                    398:                            wp->xoff <= 1 + x &&
                    399:                            wp->xoff + wp->sx >= x))
                    400:                                break;
                    401:                }
                    402:                if (wp != NULL)
                    403:                        where = BORDER;
                    404:                else {
                    405:                        wp = window_get_active_at(s->curw->window, x, y);
                    406:                        if (wp != NULL)
                    407:                                where = PANE;
                    408:                }
                    409:                if (where == NOWHERE)
                    410:                        return (KEYC_NONE);
                    411:                m->wp = wp->id;
                    412:                m->w = wp->window->id;
                    413:        } else
                    414:                m->wp = -1;
                    415:
                    416:        /* Stop dragging if needed. */
                    417:        if (type != DRAG && c->tty.mouse_drag_flag) {
                    418:                if (c->tty.mouse_drag_release != NULL)
                    419:                        c->tty.mouse_drag_release(c, m);
                    420:
                    421:                c->tty.mouse_drag_update = NULL;
                    422:                c->tty.mouse_drag_release = NULL;
                    423:
                    424:                c->tty.mouse_drag_flag = 0;
1.133     nicm      425:                return (KEYC_MOUSE); /* not a key, but still may want to pass */
1.131     nicm      426:        }
                    427:
                    428:        /* Convert to a key binding. */
                    429:        key = KEYC_NONE;
                    430:        switch (type) {
                    431:        case NOTYPE:
                    432:                break;
                    433:        case DRAG:
                    434:                if (c->tty.mouse_drag_update != NULL)
                    435:                        c->tty.mouse_drag_update(c, m);
                    436:                else {
                    437:                        switch (MOUSE_BUTTONS(b)) {
                    438:                        case 0:
                    439:                                if (where == PANE)
                    440:                                        key = KEYC_MOUSEDRAG1_PANE;
                    441:                                if (where == STATUS)
                    442:                                        key = KEYC_MOUSEDRAG1_STATUS;
                    443:                                if (where == BORDER)
                    444:                                        key = KEYC_MOUSEDRAG1_BORDER;
                    445:                                break;
                    446:                        case 1:
                    447:                                if (where == PANE)
                    448:                                        key = KEYC_MOUSEDRAG2_PANE;
                    449:                                if (where == STATUS)
                    450:                                        key = KEYC_MOUSEDRAG2_STATUS;
                    451:                                if (where == BORDER)
                    452:                                        key = KEYC_MOUSEDRAG2_BORDER;
                    453:                                break;
                    454:                        case 2:
                    455:                                if (where == PANE)
                    456:                                        key = KEYC_MOUSEDRAG3_PANE;
                    457:                                if (where == STATUS)
                    458:                                        key = KEYC_MOUSEDRAG3_STATUS;
                    459:                                if (where == BORDER)
                    460:                                        key = KEYC_MOUSEDRAG3_BORDER;
                    461:                                break;
                    462:                        }
                    463:                }
1.66      nicm      464:
1.131     nicm      465:                c->tty.mouse_drag_flag = 1;
                    466:                break;
                    467:        case WHEEL:
                    468:                if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
                    469:                        if (where == PANE)
                    470:                                key = KEYC_WHEELUP_PANE;
                    471:                        if (where == STATUS)
                    472:                                key = KEYC_WHEELUP_STATUS;
                    473:                        if (where == BORDER)
                    474:                                key = KEYC_WHEELUP_BORDER;
                    475:                } else {
                    476:                        if (where == PANE)
                    477:                                key = KEYC_WHEELDOWN_PANE;
                    478:                        if (where == STATUS)
                    479:                                key = KEYC_WHEELDOWN_STATUS;
                    480:                        if (where == BORDER)
                    481:                                key = KEYC_WHEELDOWN_BORDER;
                    482:                }
                    483:                break;
                    484:        case UP:
                    485:                switch (MOUSE_BUTTONS(b)) {
                    486:                case 0:
                    487:                        if (where == PANE)
                    488:                                key = KEYC_MOUSEUP1_PANE;
                    489:                        if (where == STATUS)
                    490:                                key = KEYC_MOUSEUP1_STATUS;
                    491:                        if (where == BORDER)
                    492:                                key = KEYC_MOUSEUP1_BORDER;
                    493:                        break;
                    494:                case 1:
                    495:                        if (where == PANE)
                    496:                                key = KEYC_MOUSEUP2_PANE;
                    497:                        if (where == STATUS)
                    498:                                key = KEYC_MOUSEUP2_STATUS;
                    499:                        if (where == BORDER)
                    500:                                key = KEYC_MOUSEUP2_BORDER;
                    501:                        break;
                    502:                case 2:
                    503:                        if (where == PANE)
                    504:                                key = KEYC_MOUSEUP3_PANE;
                    505:                        if (where == STATUS)
                    506:                                key = KEYC_MOUSEUP3_STATUS;
                    507:                        if (where == BORDER)
                    508:                                key = KEYC_MOUSEUP3_BORDER;
                    509:                        break;
                    510:                }
                    511:                break;
                    512:        case DOWN:
                    513:                switch (MOUSE_BUTTONS(b)) {
                    514:                case 0:
                    515:                        if (where == PANE)
                    516:                                key = KEYC_MOUSEDOWN1_PANE;
                    517:                        if (where == STATUS)
                    518:                                key = KEYC_MOUSEDOWN1_STATUS;
                    519:                        if (where == BORDER)
                    520:                                key = KEYC_MOUSEDOWN1_BORDER;
                    521:                        break;
                    522:                case 1:
                    523:                        if (where == PANE)
                    524:                                key = KEYC_MOUSEDOWN2_PANE;
                    525:                        if (where == STATUS)
                    526:                                key = KEYC_MOUSEDOWN2_STATUS;
                    527:                        if (where == BORDER)
                    528:                                key = KEYC_MOUSEDOWN2_BORDER;
                    529:                        break;
                    530:                case 2:
                    531:                        if (where == PANE)
                    532:                                key = KEYC_MOUSEDOWN3_PANE;
                    533:                        if (where == STATUS)
                    534:                                key = KEYC_MOUSEDOWN3_STATUS;
                    535:                        if (where == BORDER)
                    536:                                key = KEYC_MOUSEDOWN3_BORDER;
                    537:                        break;
1.66      nicm      538:                }
1.131     nicm      539:                break;
1.66      nicm      540:        }
1.131     nicm      541:        if (key == KEYC_NONE)
                    542:                return (KEYC_NONE);
1.66      nicm      543:
1.131     nicm      544:        /* Apply modifiers if any. */
                    545:        if (b & MOUSE_MASK_META)
                    546:                key |= KEYC_ESCAPE;
                    547:        if (b & MOUSE_MASK_CTRL)
                    548:                key |= KEYC_CTRL;
                    549:        if (b & MOUSE_MASK_SHIFT)
                    550:                key |= KEYC_SHIFT;
1.66      nicm      551:
1.131     nicm      552:        return (key);
1.66      nicm      553: }
                    554:
1.82      nicm      555: /* Is this fast enough to probably be a paste? */
                    556: int
                    557: server_client_assume_paste(struct session *s)
                    558: {
                    559:        struct timeval  tv;
1.84      nicm      560:        int             t;
1.82      nicm      561:
                    562:        if ((t = options_get_number(&s->options, "assume-paste-time")) == 0)
1.83      nicm      563:                return (0);
1.82      nicm      564:
                    565:        timersub(&s->activity_time, &s->last_activity_time, &tv);
                    566:        if (tv.tv_sec == 0 && tv.tv_usec < t * 1000)
1.83      nicm      567:                return (1);
                    568:        return (0);
1.82      nicm      569: }
                    570:
1.18      nicm      571: /* Handle data key input from client. */
                    572: void
1.74      nicm      573: server_client_handle_key(struct client *c, int key)
1.18      nicm      574: {
1.131     nicm      575:        struct mouse_event      *m = &c->tty.mouse;
1.132     nicm      576:        struct session          *s = c->session;
1.18      nicm      577:        struct window           *w;
                    578:        struct window_pane      *wp;
                    579:        struct timeval           tv;
1.132     nicm      580:        struct key_table        *table = c->keytable;
                    581:        struct key_binding       bd_find, *bd;
                    582:        int                      xtimeout;
1.18      nicm      583:
                    584:        /* Check the client is good to accept input. */
1.132     nicm      585:        if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.18      nicm      586:                return;
1.132     nicm      587:        w = s->curw->window;
                    588:        wp = w->active;
1.86      nicm      589:
1.131     nicm      590:        /* No session, do nothing. */
1.18      nicm      591:        if (c->session == NULL)
                    592:                return;
                    593:        s = c->session;
1.131     nicm      594:        w = c->session->curw->window;
                    595:        wp = w->active;
1.18      nicm      596:
                    597:        /* Update the activity timer. */
                    598:        if (gettimeofday(&c->activity_time, NULL) != 0)
                    599:                fatal("gettimeofday failed");
1.82      nicm      600:        memcpy(&s->last_activity_time, &s->activity_time,
                    601:            sizeof s->last_activity_time);
1.18      nicm      602:        memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
                    603:
1.132     nicm      604:        /* Number keys jump to pane in identify mode. */
1.25      nicm      605:        if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30      nicm      606:                if (c->flags & CLIENT_READONLY)
                    607:                        return;
1.95      nicm      608:                window_unzoom(w);
1.18      nicm      609:                wp = window_pane_at_index(w, key - '0');
                    610:                if (wp != NULL && window_pane_visible(wp))
                    611:                        window_set_active_pane(w, wp);
                    612:                server_clear_identify(c);
                    613:                return;
                    614:        }
                    615:
                    616:        /* Handle status line. */
1.30      nicm      617:        if (!(c->flags & CLIENT_READONLY)) {
                    618:                status_message_clear(c);
                    619:                server_clear_identify(c);
                    620:        }
1.18      nicm      621:        if (c->prompt_string != NULL) {
1.30      nicm      622:                if (!(c->flags & CLIENT_READONLY))
                    623:                        status_prompt_key(c, key);
1.18      nicm      624:                return;
                    625:        }
                    626:
                    627:        /* Check for mouse keys. */
                    628:        if (key == KEYC_MOUSE) {
1.30      nicm      629:                if (c->flags & CLIENT_READONLY)
                    630:                        return;
1.131     nicm      631:                key = server_client_check_mouse(c);
                    632:                if (key == KEYC_NONE)
                    633:                        return;
                    634:
                    635:                m->valid = 1;
                    636:                m->key = key;
                    637:
                    638:                if (!options_get_number(&s->options, "mouse")) {
                    639:                        window_pane_key(wp, c, s, key, m);
                    640:                        return;
                    641:                }
                    642:        } else
                    643:                m->valid = 0;
1.18      nicm      644:
1.132     nicm      645:        /* Treat everything as a regular key when pasting is detected. */
                    646:        if (server_client_assume_paste(s)) {
                    647:                if (!(c->flags & CLIENT_READONLY))
                    648:                        window_pane_key(wp, c, s, key, m);
                    649:                return;
                    650:        }
1.18      nicm      651:
1.132     nicm      652: retry:
                    653:        /* Try to see if there is a key binding in the current table. */
                    654:        bd_find.key = key;
                    655:        bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
                    656:        if (bd != NULL) {
                    657:                /*
                    658:                 * Key was matched in this table. If currently repeating but a
                    659:                 * non-repeating binding was found, stop repeating and try
                    660:                 * again in the root table.
                    661:                 */
                    662:                if ((c->flags & CLIENT_REPEAT) && !bd->can_repeat) {
                    663:                        server_client_key_table(c, "root");
                    664:                        c->flags &= ~CLIENT_REPEAT;
1.85      nicm      665:                        server_status_client(c);
1.132     nicm      666:                        goto retry;
1.18      nicm      667:                }
1.82      nicm      668:
1.132     nicm      669:                /*
                    670:                 * Take a reference to this table to make sure the key binding
                    671:                 * doesn't disappear.
                    672:                 */
                    673:                table->references++;
                    674:
                    675:                /*
                    676:                 * If this is a repeating key, start the timer. Otherwise reset
                    677:                 * the client back to the root table.
                    678:                 */
                    679:                xtimeout = options_get_number(&s->options, "repeat-time");
                    680:                if (xtimeout != 0 && bd->can_repeat) {
                    681:                        c->flags |= CLIENT_REPEAT;
                    682:
                    683:                        tv.tv_sec = xtimeout / 1000;
                    684:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                    685:                        evtimer_del(&c->repeat_timer);
                    686:                        evtimer_add(&c->repeat_timer, &tv);
                    687:                } else {
1.18      nicm      688:                        c->flags &= ~CLIENT_REPEAT;
1.132     nicm      689:                        server_client_key_table(c, "root");
1.18      nicm      690:                }
1.132     nicm      691:                server_status_client(c);
                    692:
                    693:                /* Dispatch the key binding. */
                    694:                key_bindings_dispatch(bd, c, m);
                    695:                key_bindings_unref_table(table);
1.18      nicm      696:                return;
                    697:        }
                    698:
1.132     nicm      699:        /*
                    700:         * No match in this table. If repeating, switch the client back to the
                    701:         * root table and try again.
                    702:         */
                    703:        if (c->flags & CLIENT_REPEAT) {
                    704:                server_client_key_table(c, "root");
1.18      nicm      705:                c->flags &= ~CLIENT_REPEAT;
1.132     nicm      706:                server_status_client(c);
                    707:                goto retry;
1.18      nicm      708:        }
                    709:
1.132     nicm      710:        /* If no match and we're not in the root table, that's it. */
                    711:        if (strcmp(c->keytable->name, "root") != 0) {
                    712:                server_client_key_table(c, "root");
                    713:                server_status_client(c);
                    714:                return;
1.18      nicm      715:        }
                    716:
1.132     nicm      717:        /*
                    718:         * No match, but in the root table. Prefix switches to the prefix table
                    719:         * and everything else is passed through.
                    720:         */
                    721:        if (key == options_get_number(&s->options, "prefix") ||
                    722:            key == options_get_number(&s->options, "prefix2")) {
                    723:                server_client_key_table(c, "prefix");
                    724:                server_status_client(c);
                    725:        } else if (!(c->flags & CLIENT_READONLY))
                    726:                window_pane_key(wp, c, s, key, m);
1.18      nicm      727: }
                    728:
1.2       nicm      729: /* Client functions that need to happen every loop. */
                    730: void
                    731: server_client_loop(void)
                    732: {
                    733:        struct client           *c;
                    734:        struct window           *w;
                    735:        struct window_pane      *wp;
                    736:
1.135     nicm      737:        TAILQ_FOREACH(c, &clients, entry) {
1.36      nicm      738:                server_client_check_exit(c);
                    739:                if (c->session != NULL) {
                    740:                        server_client_check_redraw(c);
                    741:                        server_client_reset_state(c);
                    742:                }
1.2       nicm      743:        }
                    744:
                    745:        /*
                    746:         * Any windows will have been redrawn as part of clients, so clear
1.92      nicm      747:         * their flags now. Also check pane focus and resize.
1.2       nicm      748:         */
1.134     nicm      749:        RB_FOREACH(w, windows, &windows) {
1.2       nicm      750:                w->flags &= ~WINDOW_REDRAW;
1.91      nicm      751:                TAILQ_FOREACH(wp, &w->panes, entry) {
1.101     nicm      752:                        if (wp->fd != -1) {
                    753:                                server_client_check_focus(wp);
                    754:                                server_client_check_resize(wp);
                    755:                        }
1.2       nicm      756:                        wp->flags &= ~PANE_REDRAW;
1.91      nicm      757:                }
1.2       nicm      758:        }
1.92      nicm      759: }
                    760:
                    761: /* Check if pane should be resized. */
                    762: void
                    763: server_client_check_resize(struct window_pane *wp)
                    764: {
                    765:        struct winsize  ws;
                    766:
1.101     nicm      767:        if (!(wp->flags & PANE_RESIZE))
1.92      nicm      768:                return;
                    769:
                    770:        memset(&ws, 0, sizeof ws);
                    771:        ws.ws_col = wp->sx;
                    772:        ws.ws_row = wp->sy;
                    773:
1.100     nicm      774:        if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92      nicm      775:                fatal("ioctl failed");
                    776:
                    777:        wp->flags &= ~PANE_RESIZE;
1.91      nicm      778: }
                    779:
                    780: /* Check whether pane should be focused. */
                    781: void
                    782: server_client_check_focus(struct window_pane *wp)
                    783: {
1.93      nicm      784:        struct client   *c;
1.102     nicm      785:        int              push;
1.103     nicm      786:
                    787:        /* Are focus events off? */
                    788:        if (!options_get_number(&global_options, "focus-events"))
                    789:                return;
1.102     nicm      790:
                    791:        /* Do we need to push the focus state? */
                    792:        push = wp->flags & PANE_FOCUSPUSH;
                    793:        wp->flags &= ~PANE_FOCUSPUSH;
1.91      nicm      794:
                    795:        /* If we don't care about focus, forget it. */
                    796:        if (!(wp->base.mode & MODE_FOCUSON))
                    797:                return;
                    798:
                    799:        /* If we're not the active pane in our window, we're not focused. */
                    800:        if (wp->window->active != wp)
                    801:                goto not_focused;
                    802:
                    803:        /* If we're in a mode, we're not focused. */
                    804:        if (wp->screen != &wp->base)
                    805:                goto not_focused;
                    806:
                    807:        /*
1.93      nicm      808:         * If our window is the current window in any focused clients with an
                    809:         * attached session, we're focused.
1.91      nicm      810:         */
1.135     nicm      811:        TAILQ_FOREACH(c, &clients, entry) {
                    812:                if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91      nicm      813:                        continue;
1.93      nicm      814:                if (c->session->flags & SESSION_UNATTACHED)
                    815:                        continue;
                    816:
                    817:                if (c->session->curw->window == wp->window)
1.91      nicm      818:                        goto focused;
                    819:        }
                    820:
                    821: not_focused:
1.102     nicm      822:        if (push || (wp->flags & PANE_FOCUSED))
1.91      nicm      823:                bufferevent_write(wp->event, "\033[O", 3);
                    824:        wp->flags &= ~PANE_FOCUSED;
                    825:        return;
                    826:
                    827: focused:
1.102     nicm      828:        if (push || !(wp->flags & PANE_FOCUSED))
1.91      nicm      829:                bufferevent_write(wp->event, "\033[I", 3);
                    830:        wp->flags |= PANE_FOCUSED;
1.2       nicm      831: }
                    832:
1.18      nicm      833: /*
                    834:  * Update cursor position and mode settings. The scroll region and attributes
                    835:  * are cleared when idle (waiting for an event) as this is the most likely time
                    836:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                    837:  * compromise between excessive resets and likelihood of an interrupt.
                    838:  *
                    839:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                    840:  * things that are already in their default state.
                    841:  */
1.1       nicm      842: void
1.18      nicm      843: server_client_reset_state(struct client *c)
1.1       nicm      844: {
1.18      nicm      845:        struct window           *w = c->session->curw->window;
                    846:        struct window_pane      *wp = w->active;
                    847:        struct screen           *s = wp->screen;
                    848:        struct options          *oo = &c->session->options;
1.66      nicm      849:        int                      status, mode, o;
1.60      nicm      850:
                    851:        if (c->flags & CLIENT_SUSPENDED)
                    852:                return;
1.1       nicm      853:
1.86      nicm      854:        if (c->flags & CLIENT_CONTROL)
                    855:                return;
                    856:
1.1       nicm      857:        tty_region(&c->tty, 0, c->tty.sy - 1);
                    858:
                    859:        status = options_get_number(oo, "status");
                    860:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                    861:                tty_cursor(&c->tty, 0, 0);
1.66      nicm      862:        else {
1.126     nicm      863:                o = status && options_get_number(oo, "status-position") == 0;
1.66      nicm      864:                tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
                    865:        }
1.1       nicm      866:
1.50      nicm      867:        /*
1.131     nicm      868:         * Set mouse mode if requested. To support dragging, always use button
                    869:         * mode.
1.57      nicm      870:         */
                    871:        mode = s->mode;
1.131     nicm      872:        if (options_get_number(oo, "mouse"))
                    873:                mode = (mode & ~ALL_MOUSE_MODES) | MODE_MOUSE_BUTTON;
1.48      nicm      874:
                    875:        /*
                    876:         * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
                    877:         * user has set mouse-utf8 and any mouse mode is in effect, turn on
                    878:         * UTF-8 mouse input. If the receiving terminal hasn't requested it
                    879:         * (that is, it isn't in s->mode), then it'll be converted in
                    880:         * input_mouse.
                    881:         */
                    882:        if ((c->tty.flags & TTY_UTF8) &&
                    883:            (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
                    884:                mode |= MODE_MOUSE_UTF8;
                    885:        else
                    886:                mode &= ~MODE_MOUSE_UTF8;
                    887:
                    888:        /* Set the terminal mode and reset attributes. */
1.59      nicm      889:        tty_update_mode(&c->tty, mode, s);
1.1       nicm      890:        tty_reset(&c->tty);
1.17      nicm      891: }
                    892:
                    893: /* Repeat time callback. */
                    894: void
                    895: server_client_repeat_timer(unused int fd, unused short events, void *data)
                    896: {
                    897:        struct client   *c = data;
                    898:
1.85      nicm      899:        if (c->flags & CLIENT_REPEAT) {
1.132     nicm      900:                server_client_key_table(c, "root");
                    901:                c->flags &= ~CLIENT_REPEAT;
                    902:                server_status_client(c);
1.85      nicm      903:        }
1.1       nicm      904: }
                    905:
1.36      nicm      906: /* Check if client should be exited. */
                    907: void
                    908: server_client_check_exit(struct client *c)
                    909: {
                    910:        if (!(c->flags & CLIENT_EXIT))
                    911:                return;
                    912:
1.73      nicm      913:        if (EVBUFFER_LENGTH(c->stdin_data) != 0)
                    914:                return;
                    915:        if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36      nicm      916:                return;
1.73      nicm      917:        if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36      nicm      918:                return;
                    919:
1.107     nicm      920:        server_write_client(c, MSG_EXIT, &c->retval, sizeof c->retval);
1.36      nicm      921:        c->flags &= ~CLIENT_EXIT;
1.38      nicm      922: }
                    923:
1.1       nicm      924: /* Check for client redraws. */
                    925: void
                    926: server_client_check_redraw(struct client *c)
                    927: {
                    928:        struct session          *s = c->session;
1.137     nicm      929:        struct tty              *tty = &c->tty;
1.1       nicm      930:        struct window_pane      *wp;
                    931:        int                      flags, redraw;
                    932:
1.86      nicm      933:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79      nicm      934:                return;
                    935:
1.1       nicm      936:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
                    937:                if (options_get_number(&s->options, "set-titles"))
                    938:                        server_client_set_title(c);
1.12      nicm      939:
1.1       nicm      940:                if (c->message_string != NULL)
                    941:                        redraw = status_message_redraw(c);
                    942:                else if (c->prompt_string != NULL)
                    943:                        redraw = status_prompt_redraw(c);
                    944:                else
                    945:                        redraw = status_redraw(c);
                    946:                if (!redraw)
                    947:                        c->flags &= ~CLIENT_STATUS;
                    948:        }
                    949:
1.137     nicm      950:        flags = tty->flags & (TTY_FREEZE|TTY_NOCURSOR);
                    951:        tty->flags = (tty->flags & ~TTY_FREEZE) | TTY_NOCURSOR;
                    952:
1.1       nicm      953:        if (c->flags & CLIENT_REDRAW) {
1.137     nicm      954:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      955:                screen_redraw_screen(c, 1, 1, 1);
1.27      nicm      956:                c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38      nicm      957:        } else if (c->flags & CLIENT_REDRAWWINDOW) {
1.137     nicm      958:                tty_update_mode(tty, tty->mode, NULL);
1.38      nicm      959:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
                    960:                        screen_redraw_pane(c, wp);
                    961:                c->flags &= ~CLIENT_REDRAWWINDOW;
1.1       nicm      962:        } else {
                    963:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137     nicm      964:                        if (wp->flags & PANE_REDRAW) {
                    965:                                tty_update_mode(tty, tty->mode, NULL);
1.1       nicm      966:                                screen_redraw_pane(c, wp);
1.137     nicm      967:                        }
1.1       nicm      968:                }
                    969:        }
                    970:
1.137     nicm      971:        if (c->flags & CLIENT_BORDERS) {
                    972:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      973:                screen_redraw_screen(c, 0, 0, 1);
1.137     nicm      974:        }
1.27      nicm      975:
1.137     nicm      976:        if (c->flags & CLIENT_STATUS) {
                    977:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      978:                screen_redraw_screen(c, 0, 1, 0);
1.137     nicm      979:        }
1.1       nicm      980:
1.137     nicm      981:        tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
                    982:        tty_update_mode(tty, tty->mode, NULL);
1.1       nicm      983:
1.27      nicm      984:        c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
1.1       nicm      985: }
                    986:
                    987: /* Set client title. */
                    988: void
                    989: server_client_set_title(struct client *c)
                    990: {
1.128     nicm      991:        struct session          *s = c->session;
                    992:        const char              *template;
                    993:        char                    *title;
                    994:        struct format_tree      *ft;
1.1       nicm      995:
                    996:        template = options_get_string(&s->options, "set-titles-string");
1.25      nicm      997:
1.128     nicm      998:        ft = format_create();
                    999:        format_defaults(ft, c, NULL, NULL, NULL);
                   1000:
                   1001:        title = format_expand_time(ft, template, time(NULL));
1.1       nicm     1002:        if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77      nicm     1003:                free(c->title);
1.1       nicm     1004:                c->title = xstrdup(title);
                   1005:                tty_set_title(&c->tty, c->title);
                   1006:        }
1.77      nicm     1007:        free(title);
1.128     nicm     1008:
                   1009:        format_free(ft);
1.1       nicm     1010: }
                   1011:
                   1012: /* Dispatch message from client. */
                   1013: int
                   1014: server_client_msg_dispatch(struct client *c)
                   1015: {
                   1016:        struct imsg              imsg;
1.73      nicm     1017:        struct msg_stdin_data    stdindata;
1.108     nicm     1018:        const char              *data;
1.1       nicm     1019:        ssize_t                  n, datalen;
                   1020:
1.8       deraadt  1021:        if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
                   1022:                return (-1);
1.1       nicm     1023:
                   1024:        for (;;) {
                   1025:                if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
                   1026:                        return (-1);
                   1027:                if (n == 0)
                   1028:                        return (0);
1.108     nicm     1029:
                   1030:                data = imsg.data;
1.1       nicm     1031:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
                   1032:
                   1033:                if (imsg.hdr.peerid != PROTOCOL_VERSION) {
                   1034:                        server_write_client(c, MSG_VERSION, NULL, 0);
                   1035:                        c->flags |= CLIENT_BAD;
1.112     nicm     1036:                        if (imsg.fd != -1)
                   1037:                                close(imsg.fd);
1.1       nicm     1038:                        imsg_free(&imsg);
                   1039:                        continue;
                   1040:                }
                   1041:
1.129     nicm     1042:                log_debug("got %u from client %d", imsg.hdr.type, c->ibuf.fd);
1.1       nicm     1043:                switch (imsg.hdr.type) {
1.109     nicm     1044:                case MSG_IDENTIFY_FLAGS:
                   1045:                case MSG_IDENTIFY_TERM:
                   1046:                case MSG_IDENTIFY_TTYNAME:
                   1047:                case MSG_IDENTIFY_CWD:
                   1048:                case MSG_IDENTIFY_STDIN:
                   1049:                case MSG_IDENTIFY_ENVIRON:
1.143     nicm     1050:                case MSG_IDENTIFY_CLIENTPID:
1.109     nicm     1051:                case MSG_IDENTIFY_DONE:
                   1052:                        server_client_msg_identify(c, &imsg);
1.1       nicm     1053:                        break;
1.108     nicm     1054:                case MSG_COMMAND:
                   1055:                        server_client_msg_command(c, &imsg);
                   1056:                        break;
1.73      nicm     1057:                case MSG_STDIN:
                   1058:                        if (datalen != sizeof stdindata)
                   1059:                                fatalx("bad MSG_STDIN size");
1.108     nicm     1060:                        memcpy(&stdindata, data, sizeof stdindata);
1.36      nicm     1061:
1.73      nicm     1062:                        if (c->stdin_callback == NULL)
                   1063:                                break;
                   1064:                        if (stdindata.size <= 0)
                   1065:                                c->stdin_closed = 1;
                   1066:                        else {
                   1067:                                evbuffer_add(c->stdin_data, stdindata.data,
                   1068:                                    stdindata.size);
                   1069:                        }
                   1070:                        c->stdin_callback(c, c->stdin_closed,
                   1071:                            c->stdin_callback_data);
1.33      nicm     1072:                        break;
1.1       nicm     1073:                case MSG_RESIZE:
                   1074:                        if (datalen != 0)
                   1075:                                fatalx("bad MSG_RESIZE size");
                   1076:
1.86      nicm     1077:                        if (c->flags & CLIENT_CONTROL)
                   1078:                                break;
1.32      nicm     1079:                        if (tty_resize(&c->tty)) {
                   1080:                                recalculate_sizes();
                   1081:                                server_redraw_client(c);
                   1082:                        }
1.1       nicm     1083:                        break;
                   1084:                case MSG_EXITING:
                   1085:                        if (datalen != 0)
                   1086:                                fatalx("bad MSG_EXITING size");
                   1087:
                   1088:                        c->session = NULL;
                   1089:                        tty_close(&c->tty);
                   1090:                        server_write_client(c, MSG_EXITED, NULL, 0);
                   1091:                        break;
                   1092:                case MSG_WAKEUP:
                   1093:                case MSG_UNLOCK:
                   1094:                        if (datalen != 0)
                   1095:                                fatalx("bad MSG_WAKEUP size");
                   1096:
                   1097:                        if (!(c->flags & CLIENT_SUSPENDED))
                   1098:                                break;
                   1099:                        c->flags &= ~CLIENT_SUSPENDED;
1.121     nicm     1100:
                   1101:                        if (c->tty.fd == -1) /* exited in the meantime */
                   1102:                                break;
1.10      nicm     1103:
1.11      nicm     1104:                        if (gettimeofday(&c->activity_time, NULL) != 0)
                   1105:                                fatal("gettimeofday");
1.47      nicm     1106:                        if (c->session != NULL)
                   1107:                                session_update_activity(c->session);
1.10      nicm     1108:
1.1       nicm     1109:                        tty_start_tty(&c->tty);
                   1110:                        server_redraw_client(c);
                   1111:                        recalculate_sizes();
                   1112:                        break;
                   1113:                case MSG_SHELL:
                   1114:                        if (datalen != 0)
                   1115:                                fatalx("bad MSG_SHELL size");
                   1116:
                   1117:                        server_client_msg_shell(c);
                   1118:                        break;
                   1119:                }
                   1120:
                   1121:                imsg_free(&imsg);
                   1122:        }
                   1123: }
                   1124:
                   1125: /* Handle command message. */
                   1126: void
1.108     nicm     1127: server_client_msg_command(struct client *c, struct imsg *imsg)
1.1       nicm     1128: {
1.108     nicm     1129:        struct msg_command_data   data;
                   1130:        char                     *buf;
                   1131:        size_t                    len;
                   1132:        struct cmd_list          *cmdlist = NULL;
                   1133:        int                       argc;
                   1134:        char                    **argv, *cause;
                   1135:
                   1136:        if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
                   1137:                fatalx("bad MSG_COMMAND size");
                   1138:        memcpy(&data, imsg->data, sizeof data);
                   1139:
1.124     nicm     1140:        buf = (char *)imsg->data + sizeof data;
1.108     nicm     1141:        len = imsg->hdr.len  - IMSG_HEADER_SIZE - sizeof data;
                   1142:        if (len > 0 && buf[len - 1] != '\0')
                   1143:                fatalx("bad MSG_COMMAND string");
                   1144:
                   1145:        argc = data.argc;
                   1146:        if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.94      nicm     1147:                cmdq_error(c->cmdq, "command too long");
1.1       nicm     1148:                goto error;
                   1149:        }
                   1150:
                   1151:        if (argc == 0) {
                   1152:                argc = 1;
                   1153:                argv = xcalloc(1, sizeof *argv);
                   1154:                *argv = xstrdup("new-session");
                   1155:        }
                   1156:
1.94      nicm     1157:        if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
                   1158:                cmdq_error(c->cmdq, "%s", cause);
1.1       nicm     1159:                cmd_free_argv(argc, argv);
                   1160:                goto error;
                   1161:        }
                   1162:        cmd_free_argv(argc, argv);
                   1163:
1.113     nicm     1164:        if (c != cfg_client || cfg_finished)
1.131     nicm     1165:                cmdq_run(c->cmdq, cmdlist, NULL);
1.113     nicm     1166:        else
1.131     nicm     1167:                cmdq_append(c->cmdq, cmdlist, NULL);
1.1       nicm     1168:        cmd_list_free(cmdlist);
                   1169:        return;
                   1170:
                   1171: error:
                   1172:        if (cmdlist != NULL)
                   1173:                cmd_list_free(cmdlist);
1.88      nicm     1174:
1.36      nicm     1175:        c->flags |= CLIENT_EXIT;
1.1       nicm     1176: }
                   1177:
                   1178: /* Handle identify message. */
                   1179: void
1.109     nicm     1180: server_client_msg_identify(struct client *c, struct imsg *imsg)
1.1       nicm     1181: {
1.109     nicm     1182:        const char      *data;
                   1183:        size_t           datalen;
                   1184:        int              flags;
                   1185:
                   1186:        if (c->flags & CLIENT_IDENTIFIED)
                   1187:                fatalx("out-of-order identify message");
                   1188:
                   1189:        data = imsg->data;
                   1190:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
                   1191:
                   1192:        switch (imsg->hdr.type) {
                   1193:        case MSG_IDENTIFY_FLAGS:
                   1194:                if (datalen != sizeof flags)
                   1195:                        fatalx("bad MSG_IDENTIFY_FLAGS size");
                   1196:                memcpy(&flags, data, sizeof flags);
                   1197:                c->flags |= flags;
                   1198:                break;
                   1199:        case MSG_IDENTIFY_TERM:
1.110     nicm     1200:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1201:                        fatalx("bad MSG_IDENTIFY_TERM string");
                   1202:                c->term = xstrdup(data);
                   1203:                break;
                   1204:        case MSG_IDENTIFY_TTYNAME:
1.110     nicm     1205:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1206:                        fatalx("bad MSG_IDENTIFY_TTYNAME string");
                   1207:                c->ttyname = xstrdup(data);
                   1208:                break;
                   1209:        case MSG_IDENTIFY_CWD:
                   1210:                if (datalen != 0)
                   1211:                        fatalx("bad MSG_IDENTIFY_CWD size");
                   1212:                c->cwd = imsg->fd;
                   1213:                break;
                   1214:        case MSG_IDENTIFY_STDIN:
                   1215:                if (datalen != 0)
                   1216:                        fatalx("bad MSG_IDENTIFY_STDIN size");
                   1217:                c->fd = imsg->fd;
                   1218:                break;
                   1219:        case MSG_IDENTIFY_ENVIRON:
1.110     nicm     1220:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1221:                        fatalx("bad MSG_IDENTIFY_ENVIRON string");
                   1222:                if (strchr(data, '=') != NULL)
                   1223:                        environ_put(&c->environ, data);
1.143     nicm     1224:                break;
                   1225:        case MSG_IDENTIFY_CLIENTPID:
                   1226:                if (datalen != sizeof c->pid)
                   1227:                        fatalx("bad MSG_IDENTIFY_CLIENTPID size");
                   1228:                memcpy(&c->pid, data, sizeof c->pid);
1.109     nicm     1229:                break;
                   1230:        default:
                   1231:                break;
                   1232:        }
                   1233:
                   1234:        if (imsg->hdr.type != MSG_IDENTIFY_DONE)
                   1235:                return;
                   1236:        c->flags |= CLIENT_IDENTIFIED;
1.75      nicm     1237:
1.109     nicm     1238:        if (c->flags & CLIENT_CONTROL) {
1.75      nicm     1239:                c->stdin_callback = control_callback;
1.109     nicm     1240:
1.97      nicm     1241:                evbuffer_free(c->stderr_data);
                   1242:                c->stderr_data = c->stdout_data;
1.109     nicm     1243:
                   1244:                if (c->flags & CLIENT_CONTROLCONTROL)
1.96      nicm     1245:                        evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.79      nicm     1246:                server_write_client(c, MSG_STDIN, NULL, 0);
1.75      nicm     1247:
                   1248:                c->tty.fd = -1;
                   1249:                c->tty.log_fd = -1;
                   1250:
1.109     nicm     1251:                close(c->fd);
                   1252:                c->fd = -1;
                   1253:
1.75      nicm     1254:                return;
                   1255:        }
1.1       nicm     1256:
1.109     nicm     1257:        if (c->fd == -1)
1.104     nicm     1258:                return;
1.145     nicm     1259:        if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109     nicm     1260:                close(c->fd);
                   1261:                c->fd = -1;
1.80      nicm     1262:                return;
                   1263:        }
1.109     nicm     1264:        if (c->flags & CLIENT_UTF8)
1.1       nicm     1265:                c->tty.flags |= TTY_UTF8;
1.109     nicm     1266:        if (c->flags & CLIENT_256COLOURS)
1.1       nicm     1267:                c->tty.term_flags |= TERM_256COLOURS;
                   1268:
                   1269:        tty_resize(&c->tty);
                   1270:
1.109     nicm     1271:        if (!(c->flags & CLIENT_CONTROL))
1.86      nicm     1272:                c->flags |= CLIENT_TERMINAL;
1.1       nicm     1273: }
                   1274:
                   1275: /* Handle shell message. */
                   1276: void
                   1277: server_client_msg_shell(struct client *c)
                   1278: {
1.107     nicm     1279:        const char      *shell;
1.25      nicm     1280:
1.1       nicm     1281:        shell = options_get_string(&global_s_options, "default-shell");
                   1282:        if (*shell == '\0' || areshell(shell))
                   1283:                shell = _PATH_BSHELL;
1.107     nicm     1284:        server_write_client(c, MSG_SHELL, shell, strlen(shell) + 1);
1.25      nicm     1285:
1.1       nicm     1286:        c->flags |= CLIENT_BAD; /* it will die after exec */
                   1287: }