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

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