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

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