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

1.168   ! nicm        1: /* $OpenBSD: server-client.c,v 1.167 2015/11/11 23:23:33 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
                    249: server_client_free(unused int fd, unused short events, void *arg)
                    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);
                    497:        if (tv.tv_sec == 0 && tv.tv_usec < t * 1000)
1.83      nicm      498:                return (1);
                    499:        return (0);
1.82      nicm      500: }
                    501:
1.18      nicm      502: /* Handle data key input from client. */
                    503: void
1.168   ! nicm      504: server_client_handle_key(struct client *c, key_code key)
1.18      nicm      505: {
1.131     nicm      506:        struct mouse_event      *m = &c->tty.mouse;
1.132     nicm      507:        struct session          *s = c->session;
1.18      nicm      508:        struct window           *w;
                    509:        struct window_pane      *wp;
                    510:        struct timeval           tv;
1.156     nicm      511:        struct key_table        *table;
1.132     nicm      512:        struct key_binding       bd_find, *bd;
                    513:        int                      xtimeout;
1.18      nicm      514:
                    515:        /* Check the client is good to accept input. */
1.132     nicm      516:        if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.18      nicm      517:                return;
1.132     nicm      518:        w = s->curw->window;
1.18      nicm      519:
                    520:        /* Update the activity timer. */
                    521:        if (gettimeofday(&c->activity_time, NULL) != 0)
                    522:                fatal("gettimeofday failed");
1.149     nicm      523:        session_update_activity(s, &c->activity_time);
1.18      nicm      524:
1.132     nicm      525:        /* Number keys jump to pane in identify mode. */
1.25      nicm      526:        if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30      nicm      527:                if (c->flags & CLIENT_READONLY)
                    528:                        return;
1.95      nicm      529:                window_unzoom(w);
1.18      nicm      530:                wp = window_pane_at_index(w, key - '0');
                    531:                if (wp != NULL && window_pane_visible(wp))
                    532:                        window_set_active_pane(w, wp);
                    533:                server_clear_identify(c);
                    534:                return;
                    535:        }
                    536:
                    537:        /* Handle status line. */
1.30      nicm      538:        if (!(c->flags & CLIENT_READONLY)) {
                    539:                status_message_clear(c);
                    540:                server_clear_identify(c);
                    541:        }
1.18      nicm      542:        if (c->prompt_string != NULL) {
1.30      nicm      543:                if (!(c->flags & CLIENT_READONLY))
                    544:                        status_prompt_key(c, key);
1.18      nicm      545:                return;
                    546:        }
                    547:
                    548:        /* Check for mouse keys. */
                    549:        if (key == KEYC_MOUSE) {
1.30      nicm      550:                if (c->flags & CLIENT_READONLY)
                    551:                        return;
1.131     nicm      552:                key = server_client_check_mouse(c);
                    553:                if (key == KEYC_NONE)
                    554:                        return;
                    555:
                    556:                m->valid = 1;
                    557:                m->key = key;
                    558:
1.163     nicm      559:                if (!options_get_number(s->options, "mouse"))
1.161     nicm      560:                        goto forward;
1.131     nicm      561:        } else
                    562:                m->valid = 0;
1.18      nicm      563:
1.132     nicm      564:        /* Treat everything as a regular key when pasting is detected. */
1.161     nicm      565:        if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
                    566:                goto forward;
1.18      nicm      567:
1.132     nicm      568: retry:
                    569:        /* Try to see if there is a key binding in the current table. */
                    570:        bd_find.key = key;
1.156     nicm      571:        bd = RB_FIND(key_bindings, &c->keytable->key_bindings, &bd_find);
1.132     nicm      572:        if (bd != NULL) {
                    573:                /*
                    574:                 * Key was matched in this table. If currently repeating but a
                    575:                 * non-repeating binding was found, stop repeating and try
                    576:                 * again in the root table.
                    577:                 */
                    578:                if ((c->flags & CLIENT_REPEAT) && !bd->can_repeat) {
                    579:                        server_client_key_table(c, "root");
                    580:                        c->flags &= ~CLIENT_REPEAT;
1.85      nicm      581:                        server_status_client(c);
1.132     nicm      582:                        goto retry;
1.18      nicm      583:                }
1.82      nicm      584:
1.132     nicm      585:                /*
                    586:                 * Take a reference to this table to make sure the key binding
                    587:                 * doesn't disappear.
                    588:                 */
1.156     nicm      589:                table = c->keytable;
1.132     nicm      590:                table->references++;
                    591:
                    592:                /*
                    593:                 * If this is a repeating key, start the timer. Otherwise reset
                    594:                 * the client back to the root table.
                    595:                 */
1.163     nicm      596:                xtimeout = options_get_number(s->options, "repeat-time");
1.132     nicm      597:                if (xtimeout != 0 && bd->can_repeat) {
                    598:                        c->flags |= CLIENT_REPEAT;
                    599:
                    600:                        tv.tv_sec = xtimeout / 1000;
                    601:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                    602:                        evtimer_del(&c->repeat_timer);
                    603:                        evtimer_add(&c->repeat_timer, &tv);
                    604:                } else {
1.18      nicm      605:                        c->flags &= ~CLIENT_REPEAT;
1.132     nicm      606:                        server_client_key_table(c, "root");
1.18      nicm      607:                }
1.132     nicm      608:                server_status_client(c);
                    609:
                    610:                /* Dispatch the key binding. */
                    611:                key_bindings_dispatch(bd, c, m);
                    612:                key_bindings_unref_table(table);
1.18      nicm      613:                return;
                    614:        }
                    615:
1.132     nicm      616:        /*
                    617:         * No match in this table. If repeating, switch the client back to the
                    618:         * root table and try again.
                    619:         */
                    620:        if (c->flags & CLIENT_REPEAT) {
                    621:                server_client_key_table(c, "root");
1.18      nicm      622:                c->flags &= ~CLIENT_REPEAT;
1.132     nicm      623:                server_status_client(c);
                    624:                goto retry;
1.18      nicm      625:        }
                    626:
1.132     nicm      627:        /* If no match and we're not in the root table, that's it. */
                    628:        if (strcmp(c->keytable->name, "root") != 0) {
                    629:                server_client_key_table(c, "root");
                    630:                server_status_client(c);
                    631:                return;
1.18      nicm      632:        }
                    633:
1.132     nicm      634:        /*
                    635:         * No match, but in the root table. Prefix switches to the prefix table
                    636:         * and everything else is passed through.
                    637:         */
1.168   ! nicm      638:        if (key == (key_code)options_get_number(s->options, "prefix") ||
        !           639:            key == (key_code)options_get_number(s->options, "prefix2")) {
1.132     nicm      640:                server_client_key_table(c, "prefix");
                    641:                server_status_client(c);
1.161     nicm      642:                return;
                    643:        }
                    644:
                    645: forward:
                    646:        if (c->flags & CLIENT_READONLY)
                    647:                return;
                    648:        if (KEYC_IS_MOUSE(key))
                    649:                wp = cmd_mouse_pane(m, NULL, NULL);
                    650:        else
                    651:                wp = w->active;
                    652:        if (wp != NULL)
1.132     nicm      653:                window_pane_key(wp, c, s, key, m);
1.18      nicm      654: }
                    655:
1.2       nicm      656: /* Client functions that need to happen every loop. */
                    657: void
                    658: server_client_loop(void)
                    659: {
                    660:        struct client           *c;
                    661:        struct window           *w;
                    662:        struct window_pane      *wp;
                    663:
1.135     nicm      664:        TAILQ_FOREACH(c, &clients, entry) {
1.36      nicm      665:                server_client_check_exit(c);
                    666:                if (c->session != NULL) {
                    667:                        server_client_check_redraw(c);
                    668:                        server_client_reset_state(c);
                    669:                }
1.2       nicm      670:        }
                    671:
                    672:        /*
                    673:         * Any windows will have been redrawn as part of clients, so clear
1.92      nicm      674:         * their flags now. Also check pane focus and resize.
1.2       nicm      675:         */
1.134     nicm      676:        RB_FOREACH(w, windows, &windows) {
1.2       nicm      677:                w->flags &= ~WINDOW_REDRAW;
1.91      nicm      678:                TAILQ_FOREACH(wp, &w->panes, entry) {
1.101     nicm      679:                        if (wp->fd != -1) {
                    680:                                server_client_check_focus(wp);
                    681:                                server_client_check_resize(wp);
                    682:                        }
1.2       nicm      683:                        wp->flags &= ~PANE_REDRAW;
1.91      nicm      684:                }
1.150     nicm      685:                check_window_name(w);
1.2       nicm      686:        }
1.92      nicm      687: }
                    688:
                    689: /* Check if pane should be resized. */
                    690: void
                    691: server_client_check_resize(struct window_pane *wp)
                    692: {
                    693:        struct winsize  ws;
                    694:
1.101     nicm      695:        if (!(wp->flags & PANE_RESIZE))
1.92      nicm      696:                return;
                    697:
                    698:        memset(&ws, 0, sizeof ws);
                    699:        ws.ws_col = wp->sx;
                    700:        ws.ws_row = wp->sy;
                    701:
1.100     nicm      702:        if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92      nicm      703:                fatal("ioctl failed");
                    704:
                    705:        wp->flags &= ~PANE_RESIZE;
1.91      nicm      706: }
                    707:
                    708: /* Check whether pane should be focused. */
                    709: void
                    710: server_client_check_focus(struct window_pane *wp)
                    711: {
1.93      nicm      712:        struct client   *c;
1.102     nicm      713:        int              push;
1.103     nicm      714:
                    715:        /* Are focus events off? */
1.163     nicm      716:        if (!options_get_number(global_options, "focus-events"))
1.103     nicm      717:                return;
1.102     nicm      718:
                    719:        /* Do we need to push the focus state? */
                    720:        push = wp->flags & PANE_FOCUSPUSH;
                    721:        wp->flags &= ~PANE_FOCUSPUSH;
1.91      nicm      722:
                    723:        /* If we don't care about focus, forget it. */
                    724:        if (!(wp->base.mode & MODE_FOCUSON))
                    725:                return;
                    726:
                    727:        /* If we're not the active pane in our window, we're not focused. */
                    728:        if (wp->window->active != wp)
                    729:                goto not_focused;
                    730:
                    731:        /* If we're in a mode, we're not focused. */
                    732:        if (wp->screen != &wp->base)
                    733:                goto not_focused;
                    734:
                    735:        /*
1.93      nicm      736:         * If our window is the current window in any focused clients with an
                    737:         * attached session, we're focused.
1.91      nicm      738:         */
1.135     nicm      739:        TAILQ_FOREACH(c, &clients, entry) {
                    740:                if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91      nicm      741:                        continue;
1.93      nicm      742:                if (c->session->flags & SESSION_UNATTACHED)
                    743:                        continue;
                    744:
                    745:                if (c->session->curw->window == wp->window)
1.91      nicm      746:                        goto focused;
                    747:        }
                    748:
                    749: not_focused:
1.102     nicm      750:        if (push || (wp->flags & PANE_FOCUSED))
1.91      nicm      751:                bufferevent_write(wp->event, "\033[O", 3);
                    752:        wp->flags &= ~PANE_FOCUSED;
                    753:        return;
                    754:
                    755: focused:
1.102     nicm      756:        if (push || !(wp->flags & PANE_FOCUSED))
1.91      nicm      757:                bufferevent_write(wp->event, "\033[I", 3);
                    758:        wp->flags |= PANE_FOCUSED;
1.2       nicm      759: }
                    760:
1.18      nicm      761: /*
                    762:  * Update cursor position and mode settings. The scroll region and attributes
                    763:  * are cleared when idle (waiting for an event) as this is the most likely time
                    764:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                    765:  * compromise between excessive resets and likelihood of an interrupt.
                    766:  *
                    767:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                    768:  * things that are already in their default state.
                    769:  */
1.1       nicm      770: void
1.18      nicm      771: server_client_reset_state(struct client *c)
1.1       nicm      772: {
1.18      nicm      773:        struct window           *w = c->session->curw->window;
                    774:        struct window_pane      *wp = w->active;
                    775:        struct screen           *s = wp->screen;
1.163     nicm      776:        struct options          *oo = c->session->options;
1.66      nicm      777:        int                      status, mode, o;
1.60      nicm      778:
                    779:        if (c->flags & CLIENT_SUSPENDED)
                    780:                return;
1.1       nicm      781:
1.86      nicm      782:        if (c->flags & CLIENT_CONTROL)
                    783:                return;
                    784:
1.1       nicm      785:        tty_region(&c->tty, 0, c->tty.sy - 1);
                    786:
                    787:        status = options_get_number(oo, "status");
                    788:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                    789:                tty_cursor(&c->tty, 0, 0);
1.66      nicm      790:        else {
1.126     nicm      791:                o = status && options_get_number(oo, "status-position") == 0;
1.66      nicm      792:                tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
                    793:        }
1.1       nicm      794:
1.50      nicm      795:        /*
1.131     nicm      796:         * Set mouse mode if requested. To support dragging, always use button
                    797:         * mode.
1.57      nicm      798:         */
                    799:        mode = s->mode;
1.131     nicm      800:        if (options_get_number(oo, "mouse"))
                    801:                mode = (mode & ~ALL_MOUSE_MODES) | MODE_MOUSE_BUTTON;
1.48      nicm      802:
                    803:        /*
1.167     nicm      804:         * Set UTF-8 mouse input if required. If the terminal is UTF-8 and any
                    805:         * mouse mode is in effect, turn on UTF-8 mouse input. If the receiving
                    806:         * terminal hasn't requested it (that is, it isn't in s->mode), then
                    807:         * it'll be converted in input_mouse.
1.48      nicm      808:         */
1.167     nicm      809:        if ((c->tty.flags & TTY_UTF8) && (mode & ALL_MOUSE_MODES))
1.48      nicm      810:                mode |= MODE_MOUSE_UTF8;
                    811:        else
                    812:                mode &= ~MODE_MOUSE_UTF8;
                    813:
                    814:        /* Set the terminal mode and reset attributes. */
1.59      nicm      815:        tty_update_mode(&c->tty, mode, s);
1.1       nicm      816:        tty_reset(&c->tty);
1.17      nicm      817: }
                    818:
                    819: /* Repeat time callback. */
                    820: void
                    821: server_client_repeat_timer(unused int fd, unused short events, void *data)
                    822: {
                    823:        struct client   *c = data;
                    824:
1.85      nicm      825:        if (c->flags & CLIENT_REPEAT) {
1.132     nicm      826:                server_client_key_table(c, "root");
                    827:                c->flags &= ~CLIENT_REPEAT;
                    828:                server_status_client(c);
1.85      nicm      829:        }
1.1       nicm      830: }
                    831:
1.36      nicm      832: /* Check if client should be exited. */
                    833: void
                    834: server_client_check_exit(struct client *c)
                    835: {
                    836:        if (!(c->flags & CLIENT_EXIT))
                    837:                return;
                    838:
1.73      nicm      839:        if (EVBUFFER_LENGTH(c->stdin_data) != 0)
                    840:                return;
                    841:        if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36      nicm      842:                return;
1.73      nicm      843:        if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36      nicm      844:                return;
                    845:
1.162     nicm      846:        proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36      nicm      847:        c->flags &= ~CLIENT_EXIT;
1.38      nicm      848: }
                    849:
1.1       nicm      850: /* Check for client redraws. */
                    851: void
                    852: server_client_check_redraw(struct client *c)
                    853: {
                    854:        struct session          *s = c->session;
1.137     nicm      855:        struct tty              *tty = &c->tty;
1.1       nicm      856:        struct window_pane      *wp;
                    857:        int                      flags, redraw;
                    858:
1.86      nicm      859:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79      nicm      860:                return;
                    861:
1.1       nicm      862:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.163     nicm      863:                if (options_get_number(s->options, "set-titles"))
1.1       nicm      864:                        server_client_set_title(c);
1.12      nicm      865:
1.1       nicm      866:                if (c->message_string != NULL)
                    867:                        redraw = status_message_redraw(c);
                    868:                else if (c->prompt_string != NULL)
                    869:                        redraw = status_prompt_redraw(c);
                    870:                else
                    871:                        redraw = status_redraw(c);
                    872:                if (!redraw)
                    873:                        c->flags &= ~CLIENT_STATUS;
                    874:        }
                    875:
1.137     nicm      876:        flags = tty->flags & (TTY_FREEZE|TTY_NOCURSOR);
                    877:        tty->flags = (tty->flags & ~TTY_FREEZE) | TTY_NOCURSOR;
                    878:
1.1       nicm      879:        if (c->flags & CLIENT_REDRAW) {
1.137     nicm      880:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      881:                screen_redraw_screen(c, 1, 1, 1);
1.27      nicm      882:                c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38      nicm      883:        } else if (c->flags & CLIENT_REDRAWWINDOW) {
1.137     nicm      884:                tty_update_mode(tty, tty->mode, NULL);
1.38      nicm      885:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
                    886:                        screen_redraw_pane(c, wp);
                    887:                c->flags &= ~CLIENT_REDRAWWINDOW;
1.1       nicm      888:        } else {
                    889:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137     nicm      890:                        if (wp->flags & PANE_REDRAW) {
                    891:                                tty_update_mode(tty, tty->mode, NULL);
1.1       nicm      892:                                screen_redraw_pane(c, wp);
1.137     nicm      893:                        }
1.1       nicm      894:                }
                    895:        }
                    896:
1.137     nicm      897:        if (c->flags & CLIENT_BORDERS) {
                    898:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      899:                screen_redraw_screen(c, 0, 0, 1);
1.137     nicm      900:        }
1.27      nicm      901:
1.137     nicm      902:        if (c->flags & CLIENT_STATUS) {
                    903:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      904:                screen_redraw_screen(c, 0, 1, 0);
1.137     nicm      905:        }
1.1       nicm      906:
1.137     nicm      907:        tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
                    908:        tty_update_mode(tty, tty->mode, NULL);
1.1       nicm      909:
1.153     nicm      910:        c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS|
                    911:            CLIENT_STATUSFORCE);
1.1       nicm      912: }
                    913:
                    914: /* Set client title. */
                    915: void
                    916: server_client_set_title(struct client *c)
                    917: {
1.128     nicm      918:        struct session          *s = c->session;
                    919:        const char              *template;
                    920:        char                    *title;
                    921:        struct format_tree      *ft;
1.1       nicm      922:
1.163     nicm      923:        template = options_get_string(s->options, "set-titles-string");
1.25      nicm      924:
1.128     nicm      925:        ft = format_create();
                    926:        format_defaults(ft, c, NULL, NULL, NULL);
                    927:
                    928:        title = format_expand_time(ft, template, time(NULL));
1.1       nicm      929:        if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77      nicm      930:                free(c->title);
1.1       nicm      931:                c->title = xstrdup(title);
                    932:                tty_set_title(&c->tty, c->title);
                    933:        }
1.77      nicm      934:        free(title);
1.128     nicm      935:
                    936:        format_free(ft);
1.1       nicm      937: }
                    938:
                    939: /* Dispatch message from client. */
1.162     nicm      940: void
                    941: server_client_dispatch(struct imsg *imsg, void *arg)
1.1       nicm      942: {
1.162     nicm      943:        struct client           *c = arg;
1.73      nicm      944:        struct msg_stdin_data    stdindata;
1.108     nicm      945:        const char              *data;
1.162     nicm      946:        ssize_t                  datalen;
1.149     nicm      947:        struct session          *s;
1.1       nicm      948:
1.162     nicm      949:        if (c->flags & CLIENT_DEAD)
                    950:                return;
                    951:
                    952:        if (imsg == NULL) {
                    953:                server_client_lost(c);
                    954:                return;
                    955:        }
1.1       nicm      956:
1.162     nicm      957:        data = imsg->data;
                    958:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1       nicm      959:
1.162     nicm      960:        switch (imsg->hdr.type) {
                    961:        case MSG_IDENTIFY_FLAGS:
                    962:        case MSG_IDENTIFY_TERM:
                    963:        case MSG_IDENTIFY_TTYNAME:
                    964:        case MSG_IDENTIFY_CWD:
                    965:        case MSG_IDENTIFY_STDIN:
                    966:        case MSG_IDENTIFY_ENVIRON:
                    967:        case MSG_IDENTIFY_CLIENTPID:
                    968:        case MSG_IDENTIFY_DONE:
                    969:                server_client_dispatch_identify(c, imsg);
                    970:                break;
                    971:        case MSG_COMMAND:
                    972:                server_client_dispatch_command(c, imsg);
                    973:                break;
                    974:        case MSG_STDIN:
                    975:                if (datalen != sizeof stdindata)
                    976:                        fatalx("bad MSG_STDIN size");
                    977:                memcpy(&stdindata, data, sizeof stdindata);
1.36      nicm      978:
1.162     nicm      979:                if (c->stdin_callback == NULL)
1.33      nicm      980:                        break;
1.162     nicm      981:                if (stdindata.size <= 0)
                    982:                        c->stdin_closed = 1;
                    983:                else {
                    984:                        evbuffer_add(c->stdin_data, stdindata.data,
                    985:                            stdindata.size);
                    986:                }
                    987:                c->stdin_callback(c, c->stdin_closed,
                    988:                    c->stdin_callback_data);
                    989:                break;
                    990:        case MSG_RESIZE:
                    991:                if (datalen != 0)
                    992:                        fatalx("bad MSG_RESIZE size");
1.1       nicm      993:
1.162     nicm      994:                if (c->flags & CLIENT_CONTROL)
1.1       nicm      995:                        break;
1.162     nicm      996:                if (tty_resize(&c->tty)) {
                    997:                        recalculate_sizes();
                    998:                        server_redraw_client(c);
                    999:                }
                   1000:                break;
                   1001:        case MSG_EXITING:
                   1002:                if (datalen != 0)
                   1003:                        fatalx("bad MSG_EXITING size");
1.1       nicm     1004:
1.162     nicm     1005:                c->session = NULL;
                   1006:                tty_close(&c->tty);
                   1007:                proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
                   1008:                break;
                   1009:        case MSG_WAKEUP:
                   1010:        case MSG_UNLOCK:
                   1011:                if (datalen != 0)
                   1012:                        fatalx("bad MSG_WAKEUP size");
1.121     nicm     1013:
1.162     nicm     1014:                if (!(c->flags & CLIENT_SUSPENDED))
                   1015:                        break;
                   1016:                c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm     1017:
1.162     nicm     1018:                if (c->tty.fd == -1) /* exited in the meantime */
1.1       nicm     1019:                        break;
1.162     nicm     1020:                s = c->session;
1.1       nicm     1021:
1.162     nicm     1022:                if (gettimeofday(&c->activity_time, NULL) != 0)
                   1023:                        fatal("gettimeofday failed");
                   1024:                if (s != NULL)
                   1025:                        session_update_activity(s, &c->activity_time);
                   1026:
                   1027:                tty_start_tty(&c->tty);
                   1028:                server_redraw_client(c);
                   1029:                recalculate_sizes();
                   1030:                break;
                   1031:        case MSG_SHELL:
                   1032:                if (datalen != 0)
                   1033:                        fatalx("bad MSG_SHELL size");
1.1       nicm     1034:
1.162     nicm     1035:                server_client_dispatch_shell(c);
                   1036:                break;
1.1       nicm     1037:        }
1.162     nicm     1038:
                   1039:        server_push_stdout(c);
                   1040:        server_push_stderr(c);
1.1       nicm     1041: }
                   1042:
                   1043: /* Handle command message. */
                   1044: void
1.162     nicm     1045: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1       nicm     1046: {
1.108     nicm     1047:        struct msg_command_data   data;
                   1048:        char                     *buf;
                   1049:        size_t                    len;
                   1050:        struct cmd_list          *cmdlist = NULL;
                   1051:        int                       argc;
                   1052:        char                    **argv, *cause;
                   1053:
                   1054:        if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
                   1055:                fatalx("bad MSG_COMMAND size");
                   1056:        memcpy(&data, imsg->data, sizeof data);
                   1057:
1.124     nicm     1058:        buf = (char *)imsg->data + sizeof data;
1.108     nicm     1059:        len = imsg->hdr.len  - IMSG_HEADER_SIZE - sizeof data;
                   1060:        if (len > 0 && buf[len - 1] != '\0')
                   1061:                fatalx("bad MSG_COMMAND string");
                   1062:
                   1063:        argc = data.argc;
                   1064:        if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.94      nicm     1065:                cmdq_error(c->cmdq, "command too long");
1.1       nicm     1066:                goto error;
                   1067:        }
                   1068:
                   1069:        if (argc == 0) {
                   1070:                argc = 1;
                   1071:                argv = xcalloc(1, sizeof *argv);
                   1072:                *argv = xstrdup("new-session");
                   1073:        }
                   1074:
1.94      nicm     1075:        if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
                   1076:                cmdq_error(c->cmdq, "%s", cause);
1.1       nicm     1077:                cmd_free_argv(argc, argv);
                   1078:                goto error;
                   1079:        }
                   1080:        cmd_free_argv(argc, argv);
                   1081:
1.113     nicm     1082:        if (c != cfg_client || cfg_finished)
1.131     nicm     1083:                cmdq_run(c->cmdq, cmdlist, NULL);
1.113     nicm     1084:        else
1.131     nicm     1085:                cmdq_append(c->cmdq, cmdlist, NULL);
1.1       nicm     1086:        cmd_list_free(cmdlist);
                   1087:        return;
                   1088:
                   1089: error:
                   1090:        if (cmdlist != NULL)
                   1091:                cmd_list_free(cmdlist);
1.88      nicm     1092:
1.36      nicm     1093:        c->flags |= CLIENT_EXIT;
1.1       nicm     1094: }
                   1095:
                   1096: /* Handle identify message. */
                   1097: void
1.162     nicm     1098: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1       nicm     1099: {
1.165     nicm     1100:        const char      *data, *home;
1.109     nicm     1101:        size_t           datalen;
                   1102:        int              flags;
                   1103:
                   1104:        if (c->flags & CLIENT_IDENTIFIED)
                   1105:                fatalx("out-of-order identify message");
                   1106:
                   1107:        data = imsg->data;
                   1108:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
                   1109:
                   1110:        switch (imsg->hdr.type) {
                   1111:        case MSG_IDENTIFY_FLAGS:
                   1112:                if (datalen != sizeof flags)
                   1113:                        fatalx("bad MSG_IDENTIFY_FLAGS size");
                   1114:                memcpy(&flags, data, sizeof flags);
                   1115:                c->flags |= flags;
1.158     nicm     1116:                log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109     nicm     1117:                break;
                   1118:        case MSG_IDENTIFY_TERM:
1.110     nicm     1119:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1120:                        fatalx("bad MSG_IDENTIFY_TERM string");
                   1121:                c->term = xstrdup(data);
1.158     nicm     1122:                log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109     nicm     1123:                break;
                   1124:        case MSG_IDENTIFY_TTYNAME:
1.110     nicm     1125:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1126:                        fatalx("bad MSG_IDENTIFY_TTYNAME string");
                   1127:                c->ttyname = xstrdup(data);
1.158     nicm     1128:                log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109     nicm     1129:                break;
                   1130:        case MSG_IDENTIFY_CWD:
1.155     nicm     1131:                if (datalen == 0 || data[datalen - 1] != '\0')
                   1132:                        fatalx("bad MSG_IDENTIFY_CWD string");
1.165     nicm     1133:                if (access(data, X_OK) == 0)
                   1134:                        c->cwd = xstrdup(data);
                   1135:                else if ((home = find_home()) != NULL)
                   1136:                        c->cwd = xstrdup(home);
                   1137:                else
                   1138:                        c->cwd = xstrdup("/");
1.158     nicm     1139:                log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109     nicm     1140:                break;
                   1141:        case MSG_IDENTIFY_STDIN:
                   1142:                if (datalen != 0)
                   1143:                        fatalx("bad MSG_IDENTIFY_STDIN size");
                   1144:                c->fd = imsg->fd;
1.158     nicm     1145:                log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109     nicm     1146:                break;
                   1147:        case MSG_IDENTIFY_ENVIRON:
1.110     nicm     1148:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1149:                        fatalx("bad MSG_IDENTIFY_ENVIRON string");
                   1150:                if (strchr(data, '=') != NULL)
1.164     nicm     1151:                        environ_put(c->environ, data);
1.158     nicm     1152:                log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143     nicm     1153:                break;
                   1154:        case MSG_IDENTIFY_CLIENTPID:
                   1155:                if (datalen != sizeof c->pid)
                   1156:                        fatalx("bad MSG_IDENTIFY_CLIENTPID size");
                   1157:                memcpy(&c->pid, data, sizeof c->pid);
1.158     nicm     1158:                log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109     nicm     1159:                break;
                   1160:        default:
                   1161:                break;
                   1162:        }
                   1163:
                   1164:        if (imsg->hdr.type != MSG_IDENTIFY_DONE)
                   1165:                return;
                   1166:        c->flags |= CLIENT_IDENTIFIED;
1.75      nicm     1167:
1.109     nicm     1168:        if (c->flags & CLIENT_CONTROL) {
1.75      nicm     1169:                c->stdin_callback = control_callback;
1.109     nicm     1170:
1.97      nicm     1171:                evbuffer_free(c->stderr_data);
                   1172:                c->stderr_data = c->stdout_data;
1.109     nicm     1173:
                   1174:                if (c->flags & CLIENT_CONTROLCONTROL)
1.96      nicm     1175:                        evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162     nicm     1176:                proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75      nicm     1177:
                   1178:                c->tty.fd = -1;
                   1179:
1.109     nicm     1180:                close(c->fd);
                   1181:                c->fd = -1;
                   1182:
1.75      nicm     1183:                return;
                   1184:        }
1.1       nicm     1185:
1.109     nicm     1186:        if (c->fd == -1)
1.104     nicm     1187:                return;
1.145     nicm     1188:        if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109     nicm     1189:                close(c->fd);
                   1190:                c->fd = -1;
1.80      nicm     1191:                return;
                   1192:        }
1.109     nicm     1193:        if (c->flags & CLIENT_UTF8)
1.1       nicm     1194:                c->tty.flags |= TTY_UTF8;
1.109     nicm     1195:        if (c->flags & CLIENT_256COLOURS)
1.1       nicm     1196:                c->tty.term_flags |= TERM_256COLOURS;
                   1197:
                   1198:        tty_resize(&c->tty);
                   1199:
1.109     nicm     1200:        if (!(c->flags & CLIENT_CONTROL))
1.86      nicm     1201:                c->flags |= CLIENT_TERMINAL;
1.1       nicm     1202: }
                   1203:
                   1204: /* Handle shell message. */
                   1205: void
1.162     nicm     1206: server_client_dispatch_shell(struct client *c)
1.1       nicm     1207: {
1.107     nicm     1208:        const char      *shell;
1.25      nicm     1209:
1.163     nicm     1210:        shell = options_get_string(global_s_options, "default-shell");
1.1       nicm     1211:        if (*shell == '\0' || areshell(shell))
                   1212:                shell = _PATH_BSHELL;
1.162     nicm     1213:        proc_send_s(c->peer, MSG_SHELL, shell);
1.25      nicm     1214:
1.162     nicm     1215:        proc_kill_peer(c->peer);
1.1       nicm     1216: }