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

1.187   ! nicm        1: /* $OpenBSD: server-client.c,v 1.186 2016/04/30 18:59:02 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:
                    768: /* Check if pane should be resized. */
                    769: void
                    770: server_client_check_resize(struct window_pane *wp)
                    771: {
                    772:        struct winsize  ws;
                    773:
1.101     nicm      774:        if (!(wp->flags & PANE_RESIZE))
1.92      nicm      775:                return;
                    776:
                    777:        memset(&ws, 0, sizeof ws);
                    778:        ws.ws_col = wp->sx;
                    779:        ws.ws_row = wp->sy;
                    780:
1.100     nicm      781:        if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92      nicm      782:                fatal("ioctl failed");
                    783:
                    784:        wp->flags &= ~PANE_RESIZE;
1.91      nicm      785: }
                    786:
                    787: /* Check whether pane should be focused. */
                    788: void
                    789: server_client_check_focus(struct window_pane *wp)
                    790: {
1.93      nicm      791:        struct client   *c;
1.102     nicm      792:        int              push;
1.103     nicm      793:
                    794:        /* Are focus events off? */
1.163     nicm      795:        if (!options_get_number(global_options, "focus-events"))
1.103     nicm      796:                return;
1.102     nicm      797:
                    798:        /* Do we need to push the focus state? */
                    799:        push = wp->flags & PANE_FOCUSPUSH;
                    800:        wp->flags &= ~PANE_FOCUSPUSH;
1.91      nicm      801:
                    802:        /* If we don't care about focus, forget it. */
                    803:        if (!(wp->base.mode & MODE_FOCUSON))
                    804:                return;
                    805:
                    806:        /* If we're not the active pane in our window, we're not focused. */
                    807:        if (wp->window->active != wp)
                    808:                goto not_focused;
                    809:
                    810:        /* If we're in a mode, we're not focused. */
                    811:        if (wp->screen != &wp->base)
                    812:                goto not_focused;
                    813:
                    814:        /*
1.93      nicm      815:         * If our window is the current window in any focused clients with an
                    816:         * attached session, we're focused.
1.91      nicm      817:         */
1.135     nicm      818:        TAILQ_FOREACH(c, &clients, entry) {
                    819:                if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91      nicm      820:                        continue;
1.93      nicm      821:                if (c->session->flags & SESSION_UNATTACHED)
                    822:                        continue;
                    823:
                    824:                if (c->session->curw->window == wp->window)
1.91      nicm      825:                        goto focused;
                    826:        }
                    827:
                    828: not_focused:
1.102     nicm      829:        if (push || (wp->flags & PANE_FOCUSED))
1.91      nicm      830:                bufferevent_write(wp->event, "\033[O", 3);
                    831:        wp->flags &= ~PANE_FOCUSED;
                    832:        return;
                    833:
                    834: focused:
1.102     nicm      835:        if (push || !(wp->flags & PANE_FOCUSED))
1.91      nicm      836:                bufferevent_write(wp->event, "\033[I", 3);
                    837:        wp->flags |= PANE_FOCUSED;
1.2       nicm      838: }
                    839:
1.18      nicm      840: /*
                    841:  * Update cursor position and mode settings. The scroll region and attributes
                    842:  * are cleared when idle (waiting for an event) as this is the most likely time
                    843:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                    844:  * compromise between excessive resets and likelihood of an interrupt.
                    845:  *
                    846:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                    847:  * things that are already in their default state.
                    848:  */
1.1       nicm      849: void
1.18      nicm      850: server_client_reset_state(struct client *c)
1.1       nicm      851: {
1.18      nicm      852:        struct window           *w = c->session->curw->window;
                    853:        struct window_pane      *wp = w->active;
                    854:        struct screen           *s = wp->screen;
1.163     nicm      855:        struct options          *oo = c->session->options;
1.66      nicm      856:        int                      status, mode, o;
1.60      nicm      857:
1.186     nicm      858:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86      nicm      859:                return;
                    860:
1.1       nicm      861:        tty_region(&c->tty, 0, c->tty.sy - 1);
                    862:
                    863:        status = options_get_number(oo, "status");
                    864:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                    865:                tty_cursor(&c->tty, 0, 0);
1.66      nicm      866:        else {
1.126     nicm      867:                o = status && options_get_number(oo, "status-position") == 0;
1.66      nicm      868:                tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
                    869:        }
1.1       nicm      870:
1.50      nicm      871:        /*
1.131     nicm      872:         * Set mouse mode if requested. To support dragging, always use button
                    873:         * mode.
1.57      nicm      874:         */
                    875:        mode = s->mode;
1.131     nicm      876:        if (options_get_number(oo, "mouse"))
                    877:                mode = (mode & ~ALL_MOUSE_MODES) | MODE_MOUSE_BUTTON;
1.48      nicm      878:
                    879:        /* Set the terminal mode and reset attributes. */
1.59      nicm      880:        tty_update_mode(&c->tty, mode, s);
1.1       nicm      881:        tty_reset(&c->tty);
1.17      nicm      882: }
                    883:
                    884: /* Repeat time callback. */
                    885: void
1.170     nicm      886: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17      nicm      887: {
                    888:        struct client   *c = data;
                    889:
1.85      nicm      890:        if (c->flags & CLIENT_REPEAT) {
1.178     nicm      891:                server_client_set_key_table(c, NULL);
1.132     nicm      892:                c->flags &= ~CLIENT_REPEAT;
                    893:                server_status_client(c);
1.85      nicm      894:        }
1.1       nicm      895: }
                    896:
1.36      nicm      897: /* Check if client should be exited. */
                    898: void
                    899: server_client_check_exit(struct client *c)
                    900: {
                    901:        if (!(c->flags & CLIENT_EXIT))
                    902:                return;
                    903:
1.73      nicm      904:        if (EVBUFFER_LENGTH(c->stdin_data) != 0)
                    905:                return;
                    906:        if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36      nicm      907:                return;
1.73      nicm      908:        if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36      nicm      909:                return;
                    910:
1.162     nicm      911:        proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36      nicm      912:        c->flags &= ~CLIENT_EXIT;
1.38      nicm      913: }
                    914:
1.1       nicm      915: /* Check for client redraws. */
                    916: void
                    917: server_client_check_redraw(struct client *c)
                    918: {
                    919:        struct session          *s = c->session;
1.137     nicm      920:        struct tty              *tty = &c->tty;
1.1       nicm      921:        struct window_pane      *wp;
1.185     nicm      922:        int                      flags, masked, redraw;
1.1       nicm      923:
1.86      nicm      924:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79      nicm      925:                return;
                    926:
1.1       nicm      927:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.163     nicm      928:                if (options_get_number(s->options, "set-titles"))
1.1       nicm      929:                        server_client_set_title(c);
1.12      nicm      930:
1.1       nicm      931:                if (c->message_string != NULL)
                    932:                        redraw = status_message_redraw(c);
                    933:                else if (c->prompt_string != NULL)
                    934:                        redraw = status_prompt_redraw(c);
                    935:                else
                    936:                        redraw = status_redraw(c);
                    937:                if (!redraw)
                    938:                        c->flags &= ~CLIENT_STATUS;
                    939:        }
                    940:
1.137     nicm      941:        flags = tty->flags & (TTY_FREEZE|TTY_NOCURSOR);
                    942:        tty->flags = (tty->flags & ~TTY_FREEZE) | TTY_NOCURSOR;
                    943:
1.1       nicm      944:        if (c->flags & CLIENT_REDRAW) {
1.137     nicm      945:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      946:                screen_redraw_screen(c, 1, 1, 1);
1.27      nicm      947:                c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38      nicm      948:        } else if (c->flags & CLIENT_REDRAWWINDOW) {
1.137     nicm      949:                tty_update_mode(tty, tty->mode, NULL);
1.38      nicm      950:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
                    951:                        screen_redraw_pane(c, wp);
                    952:                c->flags &= ~CLIENT_REDRAWWINDOW;
1.1       nicm      953:        } else {
                    954:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137     nicm      955:                        if (wp->flags & PANE_REDRAW) {
                    956:                                tty_update_mode(tty, tty->mode, NULL);
1.1       nicm      957:                                screen_redraw_pane(c, wp);
1.137     nicm      958:                        }
1.1       nicm      959:                }
                    960:        }
                    961:
1.185     nicm      962:        masked = c->flags & (CLIENT_BORDERS|CLIENT_STATUS);
                    963:        if (masked != 0)
1.137     nicm      964:                tty_update_mode(tty, tty->mode, NULL);
1.185     nicm      965:        if (masked == CLIENT_BORDERS)
1.115     nicm      966:                screen_redraw_screen(c, 0, 0, 1);
1.185     nicm      967:        else if (masked == CLIENT_STATUS)
1.115     nicm      968:                screen_redraw_screen(c, 0, 1, 0);
1.185     nicm      969:        else if (masked != 0)
                    970:                screen_redraw_screen(c, 0, 1, 1);
1.1       nicm      971:
1.137     nicm      972:        tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
                    973:        tty_update_mode(tty, tty->mode, NULL);
1.1       nicm      974:
1.153     nicm      975:        c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS|
                    976:            CLIENT_STATUSFORCE);
1.1       nicm      977: }
                    978:
                    979: /* Set client title. */
                    980: void
                    981: server_client_set_title(struct client *c)
                    982: {
1.128     nicm      983:        struct session          *s = c->session;
                    984:        const char              *template;
                    985:        char                    *title;
                    986:        struct format_tree      *ft;
1.1       nicm      987:
1.163     nicm      988:        template = options_get_string(s->options, "set-titles-string");
1.25      nicm      989:
1.176     nicm      990:        ft = format_create(NULL, 0);
1.128     nicm      991:        format_defaults(ft, c, NULL, NULL, NULL);
                    992:
                    993:        title = format_expand_time(ft, template, time(NULL));
1.1       nicm      994:        if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77      nicm      995:                free(c->title);
1.1       nicm      996:                c->title = xstrdup(title);
                    997:                tty_set_title(&c->tty, c->title);
                    998:        }
1.77      nicm      999:        free(title);
1.128     nicm     1000:
                   1001:        format_free(ft);
1.1       nicm     1002: }
                   1003:
                   1004: /* Dispatch message from client. */
1.162     nicm     1005: void
                   1006: server_client_dispatch(struct imsg *imsg, void *arg)
1.1       nicm     1007: {
1.162     nicm     1008:        struct client           *c = arg;
1.73      nicm     1009:        struct msg_stdin_data    stdindata;
1.108     nicm     1010:        const char              *data;
1.162     nicm     1011:        ssize_t                  datalen;
1.149     nicm     1012:        struct session          *s;
1.1       nicm     1013:
1.162     nicm     1014:        if (c->flags & CLIENT_DEAD)
                   1015:                return;
                   1016:
                   1017:        if (imsg == NULL) {
                   1018:                server_client_lost(c);
                   1019:                return;
                   1020:        }
1.1       nicm     1021:
1.162     nicm     1022:        data = imsg->data;
                   1023:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1       nicm     1024:
1.162     nicm     1025:        switch (imsg->hdr.type) {
                   1026:        case MSG_IDENTIFY_FLAGS:
                   1027:        case MSG_IDENTIFY_TERM:
                   1028:        case MSG_IDENTIFY_TTYNAME:
                   1029:        case MSG_IDENTIFY_CWD:
                   1030:        case MSG_IDENTIFY_STDIN:
                   1031:        case MSG_IDENTIFY_ENVIRON:
                   1032:        case MSG_IDENTIFY_CLIENTPID:
                   1033:        case MSG_IDENTIFY_DONE:
                   1034:                server_client_dispatch_identify(c, imsg);
                   1035:                break;
                   1036:        case MSG_COMMAND:
                   1037:                server_client_dispatch_command(c, imsg);
                   1038:                break;
                   1039:        case MSG_STDIN:
                   1040:                if (datalen != sizeof stdindata)
                   1041:                        fatalx("bad MSG_STDIN size");
                   1042:                memcpy(&stdindata, data, sizeof stdindata);
1.36      nicm     1043:
1.162     nicm     1044:                if (c->stdin_callback == NULL)
1.33      nicm     1045:                        break;
1.162     nicm     1046:                if (stdindata.size <= 0)
                   1047:                        c->stdin_closed = 1;
                   1048:                else {
                   1049:                        evbuffer_add(c->stdin_data, stdindata.data,
                   1050:                            stdindata.size);
                   1051:                }
                   1052:                c->stdin_callback(c, c->stdin_closed,
                   1053:                    c->stdin_callback_data);
                   1054:                break;
                   1055:        case MSG_RESIZE:
                   1056:                if (datalen != 0)
                   1057:                        fatalx("bad MSG_RESIZE size");
1.1       nicm     1058:
1.162     nicm     1059:                if (c->flags & CLIENT_CONTROL)
1.1       nicm     1060:                        break;
1.162     nicm     1061:                if (tty_resize(&c->tty)) {
                   1062:                        recalculate_sizes();
                   1063:                        server_redraw_client(c);
                   1064:                }
1.174     nicm     1065:                if (c->session != NULL)
1.180     nicm     1066:                        hooks_run(c->session->hooks, c, NULL, "client-resized");
1.162     nicm     1067:                break;
                   1068:        case MSG_EXITING:
                   1069:                if (datalen != 0)
                   1070:                        fatalx("bad MSG_EXITING size");
1.1       nicm     1071:
1.162     nicm     1072:                c->session = NULL;
                   1073:                tty_close(&c->tty);
                   1074:                proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
                   1075:                break;
                   1076:        case MSG_WAKEUP:
                   1077:        case MSG_UNLOCK:
                   1078:                if (datalen != 0)
                   1079:                        fatalx("bad MSG_WAKEUP size");
1.121     nicm     1080:
1.162     nicm     1081:                if (!(c->flags & CLIENT_SUSPENDED))
                   1082:                        break;
                   1083:                c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm     1084:
1.162     nicm     1085:                if (c->tty.fd == -1) /* exited in the meantime */
1.1       nicm     1086:                        break;
1.162     nicm     1087:                s = c->session;
1.1       nicm     1088:
1.162     nicm     1089:                if (gettimeofday(&c->activity_time, NULL) != 0)
                   1090:                        fatal("gettimeofday failed");
                   1091:
                   1092:                tty_start_tty(&c->tty);
                   1093:                server_redraw_client(c);
                   1094:                recalculate_sizes();
1.184     nicm     1095:
                   1096:                if (s != NULL)
                   1097:                        session_update_activity(s, &c->activity_time);
1.162     nicm     1098:                break;
                   1099:        case MSG_SHELL:
                   1100:                if (datalen != 0)
                   1101:                        fatalx("bad MSG_SHELL size");
1.1       nicm     1102:
1.162     nicm     1103:                server_client_dispatch_shell(c);
                   1104:                break;
1.1       nicm     1105:        }
                   1106: }
                   1107:
                   1108: /* Handle command message. */
                   1109: void
1.162     nicm     1110: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1       nicm     1111: {
1.108     nicm     1112:        struct msg_command_data   data;
                   1113:        char                     *buf;
                   1114:        size_t                    len;
                   1115:        struct cmd_list          *cmdlist = NULL;
                   1116:        int                       argc;
                   1117:        char                    **argv, *cause;
                   1118:
                   1119:        if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
                   1120:                fatalx("bad MSG_COMMAND size");
                   1121:        memcpy(&data, imsg->data, sizeof data);
                   1122:
1.124     nicm     1123:        buf = (char *)imsg->data + sizeof data;
1.108     nicm     1124:        len = imsg->hdr.len  - IMSG_HEADER_SIZE - sizeof data;
                   1125:        if (len > 0 && buf[len - 1] != '\0')
                   1126:                fatalx("bad MSG_COMMAND string");
                   1127:
                   1128:        argc = data.argc;
                   1129:        if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.94      nicm     1130:                cmdq_error(c->cmdq, "command too long");
1.1       nicm     1131:                goto error;
                   1132:        }
                   1133:
                   1134:        if (argc == 0) {
                   1135:                argc = 1;
                   1136:                argv = xcalloc(1, sizeof *argv);
                   1137:                *argv = xstrdup("new-session");
                   1138:        }
                   1139:
1.94      nicm     1140:        if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
                   1141:                cmdq_error(c->cmdq, "%s", cause);
1.1       nicm     1142:                cmd_free_argv(argc, argv);
                   1143:                goto error;
                   1144:        }
                   1145:        cmd_free_argv(argc, argv);
                   1146:
1.113     nicm     1147:        if (c != cfg_client || cfg_finished)
1.131     nicm     1148:                cmdq_run(c->cmdq, cmdlist, NULL);
1.113     nicm     1149:        else
1.131     nicm     1150:                cmdq_append(c->cmdq, cmdlist, NULL);
1.1       nicm     1151:        cmd_list_free(cmdlist);
                   1152:        return;
                   1153:
                   1154: error:
                   1155:        if (cmdlist != NULL)
                   1156:                cmd_list_free(cmdlist);
1.88      nicm     1157:
1.36      nicm     1158:        c->flags |= CLIENT_EXIT;
1.1       nicm     1159: }
                   1160:
                   1161: /* Handle identify message. */
                   1162: void
1.162     nicm     1163: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1       nicm     1164: {
1.165     nicm     1165:        const char      *data, *home;
1.109     nicm     1166:        size_t           datalen;
                   1167:        int              flags;
                   1168:
                   1169:        if (c->flags & CLIENT_IDENTIFIED)
                   1170:                fatalx("out-of-order identify message");
                   1171:
                   1172:        data = imsg->data;
                   1173:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
                   1174:
                   1175:        switch (imsg->hdr.type) {
                   1176:        case MSG_IDENTIFY_FLAGS:
                   1177:                if (datalen != sizeof flags)
                   1178:                        fatalx("bad MSG_IDENTIFY_FLAGS size");
                   1179:                memcpy(&flags, data, sizeof flags);
                   1180:                c->flags |= flags;
1.158     nicm     1181:                log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109     nicm     1182:                break;
                   1183:        case MSG_IDENTIFY_TERM:
1.110     nicm     1184:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1185:                        fatalx("bad MSG_IDENTIFY_TERM string");
                   1186:                c->term = xstrdup(data);
1.158     nicm     1187:                log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109     nicm     1188:                break;
                   1189:        case MSG_IDENTIFY_TTYNAME:
1.110     nicm     1190:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1191:                        fatalx("bad MSG_IDENTIFY_TTYNAME string");
                   1192:                c->ttyname = xstrdup(data);
1.158     nicm     1193:                log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109     nicm     1194:                break;
                   1195:        case MSG_IDENTIFY_CWD:
1.155     nicm     1196:                if (datalen == 0 || data[datalen - 1] != '\0')
                   1197:                        fatalx("bad MSG_IDENTIFY_CWD string");
1.165     nicm     1198:                if (access(data, X_OK) == 0)
                   1199:                        c->cwd = xstrdup(data);
                   1200:                else if ((home = find_home()) != NULL)
                   1201:                        c->cwd = xstrdup(home);
                   1202:                else
                   1203:                        c->cwd = xstrdup("/");
1.158     nicm     1204:                log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109     nicm     1205:                break;
                   1206:        case MSG_IDENTIFY_STDIN:
                   1207:                if (datalen != 0)
                   1208:                        fatalx("bad MSG_IDENTIFY_STDIN size");
                   1209:                c->fd = imsg->fd;
1.158     nicm     1210:                log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109     nicm     1211:                break;
                   1212:        case MSG_IDENTIFY_ENVIRON:
1.110     nicm     1213:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1214:                        fatalx("bad MSG_IDENTIFY_ENVIRON string");
                   1215:                if (strchr(data, '=') != NULL)
1.164     nicm     1216:                        environ_put(c->environ, data);
1.158     nicm     1217:                log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143     nicm     1218:                break;
                   1219:        case MSG_IDENTIFY_CLIENTPID:
                   1220:                if (datalen != sizeof c->pid)
                   1221:                        fatalx("bad MSG_IDENTIFY_CLIENTPID size");
                   1222:                memcpy(&c->pid, data, sizeof c->pid);
1.158     nicm     1223:                log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109     nicm     1224:                break;
                   1225:        default:
                   1226:                break;
                   1227:        }
                   1228:
                   1229:        if (imsg->hdr.type != MSG_IDENTIFY_DONE)
                   1230:                return;
                   1231:        c->flags |= CLIENT_IDENTIFIED;
1.75      nicm     1232:
1.109     nicm     1233:        if (c->flags & CLIENT_CONTROL) {
1.75      nicm     1234:                c->stdin_callback = control_callback;
1.109     nicm     1235:
1.97      nicm     1236:                evbuffer_free(c->stderr_data);
                   1237:                c->stderr_data = c->stdout_data;
1.109     nicm     1238:
                   1239:                if (c->flags & CLIENT_CONTROLCONTROL)
1.96      nicm     1240:                        evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162     nicm     1241:                proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75      nicm     1242:
                   1243:                c->tty.fd = -1;
                   1244:
1.109     nicm     1245:                close(c->fd);
                   1246:                c->fd = -1;
                   1247:
1.75      nicm     1248:                return;
                   1249:        }
1.1       nicm     1250:
1.109     nicm     1251:        if (c->fd == -1)
1.104     nicm     1252:                return;
1.145     nicm     1253:        if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109     nicm     1254:                close(c->fd);
                   1255:                c->fd = -1;
1.80      nicm     1256:                return;
                   1257:        }
1.109     nicm     1258:        if (c->flags & CLIENT_UTF8)
1.1       nicm     1259:                c->tty.flags |= TTY_UTF8;
1.109     nicm     1260:        if (c->flags & CLIENT_256COLOURS)
1.1       nicm     1261:                c->tty.term_flags |= TERM_256COLOURS;
                   1262:
                   1263:        tty_resize(&c->tty);
                   1264:
1.109     nicm     1265:        if (!(c->flags & CLIENT_CONTROL))
1.86      nicm     1266:                c->flags |= CLIENT_TERMINAL;
1.1       nicm     1267: }
                   1268:
                   1269: /* Handle shell message. */
                   1270: void
1.162     nicm     1271: server_client_dispatch_shell(struct client *c)
1.1       nicm     1272: {
1.107     nicm     1273:        const char      *shell;
1.25      nicm     1274:
1.163     nicm     1275:        shell = options_get_string(global_s_options, "default-shell");
1.1       nicm     1276:        if (*shell == '\0' || areshell(shell))
                   1277:                shell = _PATH_BSHELL;
1.162     nicm     1278:        proc_send_s(c->peer, MSG_SHELL, shell);
1.25      nicm     1279:
1.162     nicm     1280:        proc_kill_peer(c->peer);
1.169     nicm     1281: }
                   1282:
                   1283: /* Event callback to push more stdout data if any left. */
                   1284: static void
1.170     nicm     1285: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1286: {
                   1287:        struct client   *c = arg;
                   1288:
                   1289:        if (~c->flags & CLIENT_DEAD)
                   1290:                server_client_push_stdout(c);
                   1291:        server_client_unref(c);
                   1292: }
                   1293:
                   1294: /* Push stdout to client if possible. */
                   1295: void
                   1296: server_client_push_stdout(struct client *c)
                   1297: {
                   1298:        struct msg_stdout_data data;
                   1299:        size_t                 sent, left;
                   1300:
                   1301:        left = EVBUFFER_LENGTH(c->stdout_data);
                   1302:        while (left != 0) {
                   1303:                sent = left;
                   1304:                if (sent > sizeof data.data)
                   1305:                        sent = sizeof data.data;
                   1306:                memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
                   1307:                data.size = sent;
                   1308:
                   1309:                if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
                   1310:                        break;
                   1311:                evbuffer_drain(c->stdout_data, sent);
                   1312:
                   1313:                left = EVBUFFER_LENGTH(c->stdout_data);
                   1314:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1315:                    sent, left);
                   1316:        }
                   1317:        if (left != 0) {
                   1318:                c->references++;
                   1319:                event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
                   1320:                log_debug("%s: client %p, queued", __func__, c);
                   1321:        }
                   1322: }
                   1323:
                   1324: /* Event callback to push more stderr data if any left. */
                   1325: static void
1.170     nicm     1326: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1327: {
                   1328:        struct client   *c = arg;
                   1329:
                   1330:        if (~c->flags & CLIENT_DEAD)
                   1331:                server_client_push_stderr(c);
                   1332:        server_client_unref(c);
                   1333: }
                   1334:
                   1335: /* Push stderr to client if possible. */
                   1336: void
                   1337: server_client_push_stderr(struct client *c)
                   1338: {
                   1339:        struct msg_stderr_data data;
                   1340:        size_t                 sent, left;
                   1341:
                   1342:        if (c->stderr_data == c->stdout_data) {
                   1343:                server_client_push_stdout(c);
                   1344:                return;
                   1345:        }
                   1346:
                   1347:        left = EVBUFFER_LENGTH(c->stderr_data);
                   1348:        while (left != 0) {
                   1349:                sent = left;
                   1350:                if (sent > sizeof data.data)
                   1351:                        sent = sizeof data.data;
                   1352:                memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
                   1353:                data.size = sent;
                   1354:
                   1355:                if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
                   1356:                        break;
                   1357:                evbuffer_drain(c->stderr_data, sent);
                   1358:
                   1359:                left = EVBUFFER_LENGTH(c->stderr_data);
                   1360:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1361:                    sent, left);
                   1362:        }
                   1363:        if (left != 0) {
                   1364:                c->references++;
                   1365:                event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
                   1366:                log_debug("%s: client %p, queued", __func__, c);
                   1367:        }
1.1       nicm     1368: }