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

1.174   ! nicm        1: /* $OpenBSD: server-client.c,v 1.173 2015/12/01 09:41:03 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.174   ! nicm      259: /* Detach a client. */
        !           260: void
        !           261: server_client_detach(struct client *c, enum msgtype msgtype)
        !           262: {
        !           263:        struct session  *s = c->session;
        !           264:
        !           265:        if (s == NULL)
        !           266:                return;
        !           267:
        !           268:        hooks_run(c->session->hooks, "client-detached", c);
        !           269:        proc_send_s(c->peer, msgtype, s->name);
        !           270: }
        !           271:
1.66      nicm      272: /* Check for mouse keys. */
1.168     nicm      273: key_code
1.131     nicm      274: server_client_check_mouse(struct client *c)
1.66      nicm      275: {
1.131     nicm      276:        struct session                          *s = c->session;
                    277:        struct mouse_event                      *m = &c->tty.mouse;
                    278:        struct window                           *w;
                    279:        struct window_pane                      *wp;
                    280:        enum { NOTYPE, DOWN, UP, DRAG, WHEEL }   type = NOTYPE;
                    281:        enum { NOWHERE, PANE, STATUS, BORDER }   where = NOWHERE;
                    282:        u_int                                    x, y, b;
1.168     nicm      283:        key_code                                 key;
1.131     nicm      284:
                    285:        log_debug("mouse %02x at %u,%u (last %u,%u) (%d)", m->b, m->x, m->y,
                    286:            m->lx, m->ly, c->tty.mouse_drag_flag);
                    287:
                    288:        /* What type of event is this? */
                    289:        if (MOUSE_DRAG(m->b)) {
                    290:                type = DRAG;
                    291:                if (c->tty.mouse_drag_flag) {
                    292:                        x = m->x, y = m->y, b = m->b;
                    293:                        log_debug("drag update at %u,%u", x, y);
                    294:                } else {
                    295:                        x = m->lx, y = m->ly, b = m->lb;
                    296:                        log_debug("drag start at %u,%u", x, y);
                    297:                }
                    298:        } else if (MOUSE_WHEEL(m->b)) {
                    299:                type = WHEEL;
                    300:                x = m->x, y = m->y, b = m->b;
                    301:                log_debug("wheel at %u,%u", x, y);
                    302:        } else if (MOUSE_BUTTONS(m->b) == 3) {
                    303:                type = UP;
                    304:                x = m->x, y = m->y, b = m->lb;
                    305:                log_debug("up at %u,%u", x, y);
                    306:        } else {
                    307:                type = DOWN;
                    308:                x = m->x, y = m->y, b = m->b;
                    309:                log_debug("down at %u,%u", x, y);
                    310:        }
                    311:        if (type == NOTYPE)
                    312:                return (KEYC_NONE);
                    313:
                    314:        /* Always save the session. */
                    315:        m->s = s->id;
                    316:
                    317:        /* Is this on the status line? */
                    318:        m->statusat = status_at_line(c);
                    319:        if (m->statusat != -1 && y == (u_int)m->statusat) {
                    320:                w = status_get_window_at(c, x);
                    321:                if (w == NULL)
                    322:                        return (KEYC_NONE);
                    323:                m->w = w->id;
                    324:                where = STATUS;
                    325:        } else
                    326:                m->w = -1;
                    327:
                    328:        /* Not on status line. Adjust position and check for border or pane. */
                    329:        if (where == NOWHERE) {
                    330:                if (m->statusat == 0 && y > 0)
                    331:                        y--;
                    332:                else if (m->statusat > 0 && y >= (u_int)m->statusat)
                    333:                        y = m->statusat - 1;
                    334:
                    335:                TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
                    336:                        if ((wp->xoff + wp->sx == x &&
                    337:                            wp->yoff <= 1 + y &&
                    338:                            wp->yoff + wp->sy >= y) ||
                    339:                            (wp->yoff + wp->sy == y &&
                    340:                            wp->xoff <= 1 + x &&
                    341:                            wp->xoff + wp->sx >= x))
                    342:                                break;
                    343:                }
                    344:                if (wp != NULL)
                    345:                        where = BORDER;
                    346:                else {
                    347:                        wp = window_get_active_at(s->curw->window, x, y);
1.173     nicm      348:                        if (wp != NULL) {
1.131     nicm      349:                                where = PANE;
1.173     nicm      350:                                log_debug("mouse at %u,%u is on pane %%%u",
                    351:                                    x, y, wp->id);
                    352:                        }
1.131     nicm      353:                }
                    354:                if (where == NOWHERE)
                    355:                        return (KEYC_NONE);
                    356:                m->wp = wp->id;
                    357:                m->w = wp->window->id;
                    358:        } else
                    359:                m->wp = -1;
                    360:
                    361:        /* Stop dragging if needed. */
                    362:        if (type != DRAG && c->tty.mouse_drag_flag) {
                    363:                if (c->tty.mouse_drag_release != NULL)
                    364:                        c->tty.mouse_drag_release(c, m);
                    365:
                    366:                c->tty.mouse_drag_update = NULL;
                    367:                c->tty.mouse_drag_release = NULL;
                    368:
                    369:                c->tty.mouse_drag_flag = 0;
1.133     nicm      370:                return (KEYC_MOUSE); /* not a key, but still may want to pass */
1.131     nicm      371:        }
                    372:
                    373:        /* Convert to a key binding. */
                    374:        key = KEYC_NONE;
                    375:        switch (type) {
                    376:        case NOTYPE:
                    377:                break;
                    378:        case DRAG:
                    379:                if (c->tty.mouse_drag_update != NULL)
                    380:                        c->tty.mouse_drag_update(c, m);
                    381:                else {
                    382:                        switch (MOUSE_BUTTONS(b)) {
                    383:                        case 0:
                    384:                                if (where == PANE)
                    385:                                        key = KEYC_MOUSEDRAG1_PANE;
                    386:                                if (where == STATUS)
                    387:                                        key = KEYC_MOUSEDRAG1_STATUS;
                    388:                                if (where == BORDER)
                    389:                                        key = KEYC_MOUSEDRAG1_BORDER;
                    390:                                break;
                    391:                        case 1:
                    392:                                if (where == PANE)
                    393:                                        key = KEYC_MOUSEDRAG2_PANE;
                    394:                                if (where == STATUS)
                    395:                                        key = KEYC_MOUSEDRAG2_STATUS;
                    396:                                if (where == BORDER)
                    397:                                        key = KEYC_MOUSEDRAG2_BORDER;
                    398:                                break;
                    399:                        case 2:
                    400:                                if (where == PANE)
                    401:                                        key = KEYC_MOUSEDRAG3_PANE;
                    402:                                if (where == STATUS)
                    403:                                        key = KEYC_MOUSEDRAG3_STATUS;
                    404:                                if (where == BORDER)
                    405:                                        key = KEYC_MOUSEDRAG3_BORDER;
                    406:                                break;
                    407:                        }
                    408:                }
1.66      nicm      409:
1.131     nicm      410:                c->tty.mouse_drag_flag = 1;
                    411:                break;
                    412:        case WHEEL:
                    413:                if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
                    414:                        if (where == PANE)
                    415:                                key = KEYC_WHEELUP_PANE;
                    416:                        if (where == STATUS)
                    417:                                key = KEYC_WHEELUP_STATUS;
                    418:                        if (where == BORDER)
                    419:                                key = KEYC_WHEELUP_BORDER;
                    420:                } else {
                    421:                        if (where == PANE)
                    422:                                key = KEYC_WHEELDOWN_PANE;
                    423:                        if (where == STATUS)
                    424:                                key = KEYC_WHEELDOWN_STATUS;
                    425:                        if (where == BORDER)
                    426:                                key = KEYC_WHEELDOWN_BORDER;
                    427:                }
                    428:                break;
                    429:        case UP:
                    430:                switch (MOUSE_BUTTONS(b)) {
                    431:                case 0:
                    432:                        if (where == PANE)
                    433:                                key = KEYC_MOUSEUP1_PANE;
                    434:                        if (where == STATUS)
                    435:                                key = KEYC_MOUSEUP1_STATUS;
                    436:                        if (where == BORDER)
                    437:                                key = KEYC_MOUSEUP1_BORDER;
                    438:                        break;
                    439:                case 1:
                    440:                        if (where == PANE)
                    441:                                key = KEYC_MOUSEUP2_PANE;
                    442:                        if (where == STATUS)
                    443:                                key = KEYC_MOUSEUP2_STATUS;
                    444:                        if (where == BORDER)
                    445:                                key = KEYC_MOUSEUP2_BORDER;
                    446:                        break;
                    447:                case 2:
                    448:                        if (where == PANE)
                    449:                                key = KEYC_MOUSEUP3_PANE;
                    450:                        if (where == STATUS)
                    451:                                key = KEYC_MOUSEUP3_STATUS;
                    452:                        if (where == BORDER)
                    453:                                key = KEYC_MOUSEUP3_BORDER;
                    454:                        break;
                    455:                }
                    456:                break;
                    457:        case DOWN:
                    458:                switch (MOUSE_BUTTONS(b)) {
                    459:                case 0:
                    460:                        if (where == PANE)
                    461:                                key = KEYC_MOUSEDOWN1_PANE;
                    462:                        if (where == STATUS)
                    463:                                key = KEYC_MOUSEDOWN1_STATUS;
                    464:                        if (where == BORDER)
                    465:                                key = KEYC_MOUSEDOWN1_BORDER;
                    466:                        break;
                    467:                case 1:
                    468:                        if (where == PANE)
                    469:                                key = KEYC_MOUSEDOWN2_PANE;
                    470:                        if (where == STATUS)
                    471:                                key = KEYC_MOUSEDOWN2_STATUS;
                    472:                        if (where == BORDER)
                    473:                                key = KEYC_MOUSEDOWN2_BORDER;
                    474:                        break;
                    475:                case 2:
                    476:                        if (where == PANE)
                    477:                                key = KEYC_MOUSEDOWN3_PANE;
                    478:                        if (where == STATUS)
                    479:                                key = KEYC_MOUSEDOWN3_STATUS;
                    480:                        if (where == BORDER)
                    481:                                key = KEYC_MOUSEDOWN3_BORDER;
                    482:                        break;
1.66      nicm      483:                }
1.131     nicm      484:                break;
1.66      nicm      485:        }
1.131     nicm      486:        if (key == KEYC_NONE)
                    487:                return (KEYC_NONE);
1.66      nicm      488:
1.131     nicm      489:        /* Apply modifiers if any. */
                    490:        if (b & MOUSE_MASK_META)
                    491:                key |= KEYC_ESCAPE;
                    492:        if (b & MOUSE_MASK_CTRL)
                    493:                key |= KEYC_CTRL;
                    494:        if (b & MOUSE_MASK_SHIFT)
                    495:                key |= KEYC_SHIFT;
1.66      nicm      496:
1.131     nicm      497:        return (key);
1.66      nicm      498: }
                    499:
1.82      nicm      500: /* Is this fast enough to probably be a paste? */
                    501: int
                    502: server_client_assume_paste(struct session *s)
                    503: {
                    504:        struct timeval  tv;
1.84      nicm      505:        int             t;
1.82      nicm      506:
1.163     nicm      507:        if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83      nicm      508:                return (0);
1.82      nicm      509:
                    510:        timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171     nicm      511:        if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
                    512:                log_debug("session %s pasting (flag %d)", s->name,
                    513:                    !!(s->flags & SESSION_PASTING));
                    514:                if (s->flags & SESSION_PASTING)
                    515:                        return (1);
                    516:                s->flags |= SESSION_PASTING;
                    517:                return (0);
                    518:        }
                    519:        log_debug("session %s not pasting", s->name);
                    520:        s->flags &= ~SESSION_PASTING;
1.83      nicm      521:        return (0);
1.82      nicm      522: }
                    523:
1.18      nicm      524: /* Handle data key input from client. */
                    525: void
1.168     nicm      526: server_client_handle_key(struct client *c, key_code key)
1.18      nicm      527: {
1.131     nicm      528:        struct mouse_event      *m = &c->tty.mouse;
1.132     nicm      529:        struct session          *s = c->session;
1.18      nicm      530:        struct window           *w;
                    531:        struct window_pane      *wp;
                    532:        struct timeval           tv;
1.156     nicm      533:        struct key_table        *table;
1.132     nicm      534:        struct key_binding       bd_find, *bd;
                    535:        int                      xtimeout;
1.18      nicm      536:
                    537:        /* Check the client is good to accept input. */
1.132     nicm      538:        if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.18      nicm      539:                return;
1.132     nicm      540:        w = s->curw->window;
1.18      nicm      541:
                    542:        /* Update the activity timer. */
                    543:        if (gettimeofday(&c->activity_time, NULL) != 0)
                    544:                fatal("gettimeofday failed");
1.149     nicm      545:        session_update_activity(s, &c->activity_time);
1.18      nicm      546:
1.132     nicm      547:        /* Number keys jump to pane in identify mode. */
1.25      nicm      548:        if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30      nicm      549:                if (c->flags & CLIENT_READONLY)
                    550:                        return;
1.95      nicm      551:                window_unzoom(w);
1.18      nicm      552:                wp = window_pane_at_index(w, key - '0');
                    553:                if (wp != NULL && window_pane_visible(wp))
                    554:                        window_set_active_pane(w, wp);
                    555:                server_clear_identify(c);
                    556:                return;
                    557:        }
                    558:
                    559:        /* Handle status line. */
1.30      nicm      560:        if (!(c->flags & CLIENT_READONLY)) {
                    561:                status_message_clear(c);
                    562:                server_clear_identify(c);
                    563:        }
1.18      nicm      564:        if (c->prompt_string != NULL) {
1.30      nicm      565:                if (!(c->flags & CLIENT_READONLY))
                    566:                        status_prompt_key(c, key);
1.18      nicm      567:                return;
                    568:        }
                    569:
                    570:        /* Check for mouse keys. */
                    571:        if (key == KEYC_MOUSE) {
1.30      nicm      572:                if (c->flags & CLIENT_READONLY)
                    573:                        return;
1.131     nicm      574:                key = server_client_check_mouse(c);
                    575:                if (key == KEYC_NONE)
                    576:                        return;
                    577:
                    578:                m->valid = 1;
                    579:                m->key = key;
                    580:
1.163     nicm      581:                if (!options_get_number(s->options, "mouse"))
1.161     nicm      582:                        goto forward;
1.131     nicm      583:        } else
                    584:                m->valid = 0;
1.18      nicm      585:
1.132     nicm      586:        /* Treat everything as a regular key when pasting is detected. */
1.161     nicm      587:        if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
                    588:                goto forward;
1.18      nicm      589:
1.132     nicm      590: retry:
                    591:        /* Try to see if there is a key binding in the current table. */
                    592:        bd_find.key = key;
1.156     nicm      593:        bd = RB_FIND(key_bindings, &c->keytable->key_bindings, &bd_find);
1.132     nicm      594:        if (bd != NULL) {
                    595:                /*
                    596:                 * Key was matched in this table. If currently repeating but a
                    597:                 * non-repeating binding was found, stop repeating and try
                    598:                 * again in the root table.
                    599:                 */
                    600:                if ((c->flags & CLIENT_REPEAT) && !bd->can_repeat) {
                    601:                        server_client_key_table(c, "root");
                    602:                        c->flags &= ~CLIENT_REPEAT;
1.85      nicm      603:                        server_status_client(c);
1.132     nicm      604:                        goto retry;
1.18      nicm      605:                }
1.82      nicm      606:
1.132     nicm      607:                /*
                    608:                 * Take a reference to this table to make sure the key binding
                    609:                 * doesn't disappear.
                    610:                 */
1.156     nicm      611:                table = c->keytable;
1.132     nicm      612:                table->references++;
                    613:
                    614:                /*
                    615:                 * If this is a repeating key, start the timer. Otherwise reset
                    616:                 * the client back to the root table.
                    617:                 */
1.163     nicm      618:                xtimeout = options_get_number(s->options, "repeat-time");
1.132     nicm      619:                if (xtimeout != 0 && bd->can_repeat) {
                    620:                        c->flags |= CLIENT_REPEAT;
                    621:
                    622:                        tv.tv_sec = xtimeout / 1000;
                    623:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                    624:                        evtimer_del(&c->repeat_timer);
                    625:                        evtimer_add(&c->repeat_timer, &tv);
                    626:                } else {
1.18      nicm      627:                        c->flags &= ~CLIENT_REPEAT;
1.132     nicm      628:                        server_client_key_table(c, "root");
1.18      nicm      629:                }
1.132     nicm      630:                server_status_client(c);
                    631:
                    632:                /* Dispatch the key binding. */
                    633:                key_bindings_dispatch(bd, c, m);
                    634:                key_bindings_unref_table(table);
1.18      nicm      635:                return;
                    636:        }
                    637:
1.132     nicm      638:        /*
                    639:         * No match in this table. If repeating, switch the client back to the
                    640:         * root table and try again.
                    641:         */
                    642:        if (c->flags & CLIENT_REPEAT) {
                    643:                server_client_key_table(c, "root");
1.18      nicm      644:                c->flags &= ~CLIENT_REPEAT;
1.132     nicm      645:                server_status_client(c);
                    646:                goto retry;
1.18      nicm      647:        }
                    648:
1.132     nicm      649:        /* If no match and we're not in the root table, that's it. */
                    650:        if (strcmp(c->keytable->name, "root") != 0) {
                    651:                server_client_key_table(c, "root");
                    652:                server_status_client(c);
                    653:                return;
1.18      nicm      654:        }
                    655:
1.132     nicm      656:        /*
                    657:         * No match, but in the root table. Prefix switches to the prefix table
                    658:         * and everything else is passed through.
                    659:         */
1.168     nicm      660:        if (key == (key_code)options_get_number(s->options, "prefix") ||
                    661:            key == (key_code)options_get_number(s->options, "prefix2")) {
1.132     nicm      662:                server_client_key_table(c, "prefix");
                    663:                server_status_client(c);
1.161     nicm      664:                return;
                    665:        }
                    666:
                    667: forward:
                    668:        if (c->flags & CLIENT_READONLY)
                    669:                return;
                    670:        if (KEYC_IS_MOUSE(key))
                    671:                wp = cmd_mouse_pane(m, NULL, NULL);
                    672:        else
                    673:                wp = w->active;
                    674:        if (wp != NULL)
1.132     nicm      675:                window_pane_key(wp, c, s, key, m);
1.18      nicm      676: }
                    677:
1.2       nicm      678: /* Client functions that need to happen every loop. */
                    679: void
                    680: server_client_loop(void)
                    681: {
                    682:        struct client           *c;
                    683:        struct window           *w;
                    684:        struct window_pane      *wp;
                    685:
1.135     nicm      686:        TAILQ_FOREACH(c, &clients, entry) {
1.36      nicm      687:                server_client_check_exit(c);
                    688:                if (c->session != NULL) {
                    689:                        server_client_check_redraw(c);
                    690:                        server_client_reset_state(c);
                    691:                }
1.2       nicm      692:        }
                    693:
                    694:        /*
                    695:         * Any windows will have been redrawn as part of clients, so clear
1.92      nicm      696:         * their flags now. Also check pane focus and resize.
1.2       nicm      697:         */
1.134     nicm      698:        RB_FOREACH(w, windows, &windows) {
1.2       nicm      699:                w->flags &= ~WINDOW_REDRAW;
1.91      nicm      700:                TAILQ_FOREACH(wp, &w->panes, entry) {
1.101     nicm      701:                        if (wp->fd != -1) {
                    702:                                server_client_check_focus(wp);
                    703:                                server_client_check_resize(wp);
                    704:                        }
1.2       nicm      705:                        wp->flags &= ~PANE_REDRAW;
1.91      nicm      706:                }
1.150     nicm      707:                check_window_name(w);
1.2       nicm      708:        }
1.92      nicm      709: }
                    710:
                    711: /* Check if pane should be resized. */
                    712: void
                    713: server_client_check_resize(struct window_pane *wp)
                    714: {
                    715:        struct winsize  ws;
                    716:
1.101     nicm      717:        if (!(wp->flags & PANE_RESIZE))
1.92      nicm      718:                return;
                    719:
                    720:        memset(&ws, 0, sizeof ws);
                    721:        ws.ws_col = wp->sx;
                    722:        ws.ws_row = wp->sy;
                    723:
1.100     nicm      724:        if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92      nicm      725:                fatal("ioctl failed");
                    726:
                    727:        wp->flags &= ~PANE_RESIZE;
1.91      nicm      728: }
                    729:
                    730: /* Check whether pane should be focused. */
                    731: void
                    732: server_client_check_focus(struct window_pane *wp)
                    733: {
1.93      nicm      734:        struct client   *c;
1.102     nicm      735:        int              push;
1.103     nicm      736:
                    737:        /* Are focus events off? */
1.163     nicm      738:        if (!options_get_number(global_options, "focus-events"))
1.103     nicm      739:                return;
1.102     nicm      740:
                    741:        /* Do we need to push the focus state? */
                    742:        push = wp->flags & PANE_FOCUSPUSH;
                    743:        wp->flags &= ~PANE_FOCUSPUSH;
1.91      nicm      744:
                    745:        /* If we don't care about focus, forget it. */
                    746:        if (!(wp->base.mode & MODE_FOCUSON))
                    747:                return;
                    748:
                    749:        /* If we're not the active pane in our window, we're not focused. */
                    750:        if (wp->window->active != wp)
                    751:                goto not_focused;
                    752:
                    753:        /* If we're in a mode, we're not focused. */
                    754:        if (wp->screen != &wp->base)
                    755:                goto not_focused;
                    756:
                    757:        /*
1.93      nicm      758:         * If our window is the current window in any focused clients with an
                    759:         * attached session, we're focused.
1.91      nicm      760:         */
1.135     nicm      761:        TAILQ_FOREACH(c, &clients, entry) {
                    762:                if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91      nicm      763:                        continue;
1.93      nicm      764:                if (c->session->flags & SESSION_UNATTACHED)
                    765:                        continue;
                    766:
                    767:                if (c->session->curw->window == wp->window)
1.91      nicm      768:                        goto focused;
                    769:        }
                    770:
                    771: not_focused:
1.102     nicm      772:        if (push || (wp->flags & PANE_FOCUSED))
1.91      nicm      773:                bufferevent_write(wp->event, "\033[O", 3);
                    774:        wp->flags &= ~PANE_FOCUSED;
                    775:        return;
                    776:
                    777: focused:
1.102     nicm      778:        if (push || !(wp->flags & PANE_FOCUSED))
1.91      nicm      779:                bufferevent_write(wp->event, "\033[I", 3);
                    780:        wp->flags |= PANE_FOCUSED;
1.2       nicm      781: }
                    782:
1.18      nicm      783: /*
                    784:  * Update cursor position and mode settings. The scroll region and attributes
                    785:  * are cleared when idle (waiting for an event) as this is the most likely time
                    786:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                    787:  * compromise between excessive resets and likelihood of an interrupt.
                    788:  *
                    789:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                    790:  * things that are already in their default state.
                    791:  */
1.1       nicm      792: void
1.18      nicm      793: server_client_reset_state(struct client *c)
1.1       nicm      794: {
1.18      nicm      795:        struct window           *w = c->session->curw->window;
                    796:        struct window_pane      *wp = w->active;
                    797:        struct screen           *s = wp->screen;
1.163     nicm      798:        struct options          *oo = c->session->options;
1.66      nicm      799:        int                      status, mode, o;
1.60      nicm      800:
                    801:        if (c->flags & CLIENT_SUSPENDED)
                    802:                return;
1.1       nicm      803:
1.86      nicm      804:        if (c->flags & CLIENT_CONTROL)
                    805:                return;
                    806:
1.1       nicm      807:        tty_region(&c->tty, 0, c->tty.sy - 1);
                    808:
                    809:        status = options_get_number(oo, "status");
                    810:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                    811:                tty_cursor(&c->tty, 0, 0);
1.66      nicm      812:        else {
1.126     nicm      813:                o = status && options_get_number(oo, "status-position") == 0;
1.66      nicm      814:                tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
                    815:        }
1.1       nicm      816:
1.50      nicm      817:        /*
1.131     nicm      818:         * Set mouse mode if requested. To support dragging, always use button
                    819:         * mode.
1.57      nicm      820:         */
                    821:        mode = s->mode;
1.131     nicm      822:        if (options_get_number(oo, "mouse"))
                    823:                mode = (mode & ~ALL_MOUSE_MODES) | MODE_MOUSE_BUTTON;
1.48      nicm      824:
                    825:        /* Set the terminal mode and reset attributes. */
1.59      nicm      826:        tty_update_mode(&c->tty, mode, s);
1.1       nicm      827:        tty_reset(&c->tty);
1.17      nicm      828: }
                    829:
                    830: /* Repeat time callback. */
                    831: void
1.170     nicm      832: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17      nicm      833: {
                    834:        struct client   *c = data;
                    835:
1.85      nicm      836:        if (c->flags & CLIENT_REPEAT) {
1.132     nicm      837:                server_client_key_table(c, "root");
                    838:                c->flags &= ~CLIENT_REPEAT;
                    839:                server_status_client(c);
1.85      nicm      840:        }
1.1       nicm      841: }
                    842:
1.36      nicm      843: /* Check if client should be exited. */
                    844: void
                    845: server_client_check_exit(struct client *c)
                    846: {
                    847:        if (!(c->flags & CLIENT_EXIT))
                    848:                return;
                    849:
1.73      nicm      850:        if (EVBUFFER_LENGTH(c->stdin_data) != 0)
                    851:                return;
                    852:        if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36      nicm      853:                return;
1.73      nicm      854:        if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36      nicm      855:                return;
                    856:
1.162     nicm      857:        proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36      nicm      858:        c->flags &= ~CLIENT_EXIT;
1.38      nicm      859: }
                    860:
1.1       nicm      861: /* Check for client redraws. */
                    862: void
                    863: server_client_check_redraw(struct client *c)
                    864: {
                    865:        struct session          *s = c->session;
1.137     nicm      866:        struct tty              *tty = &c->tty;
1.1       nicm      867:        struct window_pane      *wp;
                    868:        int                      flags, redraw;
                    869:
1.86      nicm      870:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79      nicm      871:                return;
                    872:
1.1       nicm      873:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.163     nicm      874:                if (options_get_number(s->options, "set-titles"))
1.1       nicm      875:                        server_client_set_title(c);
1.12      nicm      876:
1.1       nicm      877:                if (c->message_string != NULL)
                    878:                        redraw = status_message_redraw(c);
                    879:                else if (c->prompt_string != NULL)
                    880:                        redraw = status_prompt_redraw(c);
                    881:                else
                    882:                        redraw = status_redraw(c);
                    883:                if (!redraw)
                    884:                        c->flags &= ~CLIENT_STATUS;
                    885:        }
                    886:
1.137     nicm      887:        flags = tty->flags & (TTY_FREEZE|TTY_NOCURSOR);
                    888:        tty->flags = (tty->flags & ~TTY_FREEZE) | TTY_NOCURSOR;
                    889:
1.1       nicm      890:        if (c->flags & CLIENT_REDRAW) {
1.137     nicm      891:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      892:                screen_redraw_screen(c, 1, 1, 1);
1.27      nicm      893:                c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38      nicm      894:        } else if (c->flags & CLIENT_REDRAWWINDOW) {
1.137     nicm      895:                tty_update_mode(tty, tty->mode, NULL);
1.38      nicm      896:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
                    897:                        screen_redraw_pane(c, wp);
                    898:                c->flags &= ~CLIENT_REDRAWWINDOW;
1.1       nicm      899:        } else {
                    900:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137     nicm      901:                        if (wp->flags & PANE_REDRAW) {
                    902:                                tty_update_mode(tty, tty->mode, NULL);
1.1       nicm      903:                                screen_redraw_pane(c, wp);
1.137     nicm      904:                        }
1.1       nicm      905:                }
                    906:        }
                    907:
1.137     nicm      908:        if (c->flags & CLIENT_BORDERS) {
                    909:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      910:                screen_redraw_screen(c, 0, 0, 1);
1.137     nicm      911:        }
1.27      nicm      912:
1.137     nicm      913:        if (c->flags & CLIENT_STATUS) {
                    914:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      915:                screen_redraw_screen(c, 0, 1, 0);
1.137     nicm      916:        }
1.1       nicm      917:
1.137     nicm      918:        tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
                    919:        tty_update_mode(tty, tty->mode, NULL);
1.1       nicm      920:
1.153     nicm      921:        c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS|
                    922:            CLIENT_STATUSFORCE);
1.1       nicm      923: }
                    924:
                    925: /* Set client title. */
                    926: void
                    927: server_client_set_title(struct client *c)
                    928: {
1.128     nicm      929:        struct session          *s = c->session;
                    930:        const char              *template;
                    931:        char                    *title;
                    932:        struct format_tree      *ft;
1.1       nicm      933:
1.163     nicm      934:        template = options_get_string(s->options, "set-titles-string");
1.25      nicm      935:
1.128     nicm      936:        ft = format_create();
                    937:        format_defaults(ft, c, NULL, NULL, NULL);
                    938:
                    939:        title = format_expand_time(ft, template, time(NULL));
1.1       nicm      940:        if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77      nicm      941:                free(c->title);
1.1       nicm      942:                c->title = xstrdup(title);
                    943:                tty_set_title(&c->tty, c->title);
                    944:        }
1.77      nicm      945:        free(title);
1.128     nicm      946:
                    947:        format_free(ft);
1.1       nicm      948: }
                    949:
                    950: /* Dispatch message from client. */
1.162     nicm      951: void
                    952: server_client_dispatch(struct imsg *imsg, void *arg)
1.1       nicm      953: {
1.162     nicm      954:        struct client           *c = arg;
1.73      nicm      955:        struct msg_stdin_data    stdindata;
1.108     nicm      956:        const char              *data;
1.162     nicm      957:        ssize_t                  datalen;
1.149     nicm      958:        struct session          *s;
1.1       nicm      959:
1.162     nicm      960:        if (c->flags & CLIENT_DEAD)
                    961:                return;
                    962:
                    963:        if (imsg == NULL) {
                    964:                server_client_lost(c);
                    965:                return;
                    966:        }
1.1       nicm      967:
1.162     nicm      968:        data = imsg->data;
                    969:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1       nicm      970:
1.162     nicm      971:        switch (imsg->hdr.type) {
                    972:        case MSG_IDENTIFY_FLAGS:
                    973:        case MSG_IDENTIFY_TERM:
                    974:        case MSG_IDENTIFY_TTYNAME:
                    975:        case MSG_IDENTIFY_CWD:
                    976:        case MSG_IDENTIFY_STDIN:
                    977:        case MSG_IDENTIFY_ENVIRON:
                    978:        case MSG_IDENTIFY_CLIENTPID:
                    979:        case MSG_IDENTIFY_DONE:
                    980:                server_client_dispatch_identify(c, imsg);
                    981:                break;
                    982:        case MSG_COMMAND:
                    983:                server_client_dispatch_command(c, imsg);
                    984:                break;
                    985:        case MSG_STDIN:
                    986:                if (datalen != sizeof stdindata)
                    987:                        fatalx("bad MSG_STDIN size");
                    988:                memcpy(&stdindata, data, sizeof stdindata);
1.36      nicm      989:
1.162     nicm      990:                if (c->stdin_callback == NULL)
1.33      nicm      991:                        break;
1.162     nicm      992:                if (stdindata.size <= 0)
                    993:                        c->stdin_closed = 1;
                    994:                else {
                    995:                        evbuffer_add(c->stdin_data, stdindata.data,
                    996:                            stdindata.size);
                    997:                }
                    998:                c->stdin_callback(c, c->stdin_closed,
                    999:                    c->stdin_callback_data);
                   1000:                break;
                   1001:        case MSG_RESIZE:
                   1002:                if (datalen != 0)
                   1003:                        fatalx("bad MSG_RESIZE size");
1.1       nicm     1004:
1.162     nicm     1005:                if (c->flags & CLIENT_CONTROL)
1.1       nicm     1006:                        break;
1.162     nicm     1007:                if (tty_resize(&c->tty)) {
                   1008:                        recalculate_sizes();
                   1009:                        server_redraw_client(c);
                   1010:                }
1.174   ! nicm     1011:                if (c->session != NULL)
        !          1012:                        hooks_run(c->session->hooks, "client-resized", c);
1.162     nicm     1013:                break;
                   1014:        case MSG_EXITING:
                   1015:                if (datalen != 0)
                   1016:                        fatalx("bad MSG_EXITING size");
1.1       nicm     1017:
1.162     nicm     1018:                c->session = NULL;
                   1019:                tty_close(&c->tty);
                   1020:                proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
                   1021:                break;
                   1022:        case MSG_WAKEUP:
                   1023:        case MSG_UNLOCK:
                   1024:                if (datalen != 0)
                   1025:                        fatalx("bad MSG_WAKEUP size");
1.121     nicm     1026:
1.162     nicm     1027:                if (!(c->flags & CLIENT_SUSPENDED))
                   1028:                        break;
                   1029:                c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm     1030:
1.162     nicm     1031:                if (c->tty.fd == -1) /* exited in the meantime */
1.1       nicm     1032:                        break;
1.162     nicm     1033:                s = c->session;
1.1       nicm     1034:
1.162     nicm     1035:                if (gettimeofday(&c->activity_time, NULL) != 0)
                   1036:                        fatal("gettimeofday failed");
                   1037:                if (s != NULL)
                   1038:                        session_update_activity(s, &c->activity_time);
                   1039:
                   1040:                tty_start_tty(&c->tty);
                   1041:                server_redraw_client(c);
                   1042:                recalculate_sizes();
                   1043:                break;
                   1044:        case MSG_SHELL:
                   1045:                if (datalen != 0)
                   1046:                        fatalx("bad MSG_SHELL size");
1.1       nicm     1047:
1.162     nicm     1048:                server_client_dispatch_shell(c);
                   1049:                break;
1.1       nicm     1050:        }
                   1051: }
                   1052:
                   1053: /* Handle command message. */
                   1054: void
1.162     nicm     1055: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1       nicm     1056: {
1.108     nicm     1057:        struct msg_command_data   data;
                   1058:        char                     *buf;
                   1059:        size_t                    len;
                   1060:        struct cmd_list          *cmdlist = NULL;
                   1061:        int                       argc;
                   1062:        char                    **argv, *cause;
                   1063:
                   1064:        if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
                   1065:                fatalx("bad MSG_COMMAND size");
                   1066:        memcpy(&data, imsg->data, sizeof data);
                   1067:
1.124     nicm     1068:        buf = (char *)imsg->data + sizeof data;
1.108     nicm     1069:        len = imsg->hdr.len  - IMSG_HEADER_SIZE - sizeof data;
                   1070:        if (len > 0 && buf[len - 1] != '\0')
                   1071:                fatalx("bad MSG_COMMAND string");
                   1072:
                   1073:        argc = data.argc;
                   1074:        if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.94      nicm     1075:                cmdq_error(c->cmdq, "command too long");
1.1       nicm     1076:                goto error;
                   1077:        }
                   1078:
                   1079:        if (argc == 0) {
                   1080:                argc = 1;
                   1081:                argv = xcalloc(1, sizeof *argv);
                   1082:                *argv = xstrdup("new-session");
                   1083:        }
                   1084:
1.94      nicm     1085:        if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
                   1086:                cmdq_error(c->cmdq, "%s", cause);
1.1       nicm     1087:                cmd_free_argv(argc, argv);
                   1088:                goto error;
                   1089:        }
                   1090:        cmd_free_argv(argc, argv);
                   1091:
1.113     nicm     1092:        if (c != cfg_client || cfg_finished)
1.131     nicm     1093:                cmdq_run(c->cmdq, cmdlist, NULL);
1.113     nicm     1094:        else
1.131     nicm     1095:                cmdq_append(c->cmdq, cmdlist, NULL);
1.1       nicm     1096:        cmd_list_free(cmdlist);
                   1097:        return;
                   1098:
                   1099: error:
                   1100:        if (cmdlist != NULL)
                   1101:                cmd_list_free(cmdlist);
1.88      nicm     1102:
1.36      nicm     1103:        c->flags |= CLIENT_EXIT;
1.1       nicm     1104: }
                   1105:
                   1106: /* Handle identify message. */
                   1107: void
1.162     nicm     1108: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1       nicm     1109: {
1.165     nicm     1110:        const char      *data, *home;
1.109     nicm     1111:        size_t           datalen;
                   1112:        int              flags;
                   1113:
                   1114:        if (c->flags & CLIENT_IDENTIFIED)
                   1115:                fatalx("out-of-order identify message");
                   1116:
                   1117:        data = imsg->data;
                   1118:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
                   1119:
                   1120:        switch (imsg->hdr.type) {
                   1121:        case MSG_IDENTIFY_FLAGS:
                   1122:                if (datalen != sizeof flags)
                   1123:                        fatalx("bad MSG_IDENTIFY_FLAGS size");
                   1124:                memcpy(&flags, data, sizeof flags);
                   1125:                c->flags |= flags;
1.158     nicm     1126:                log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109     nicm     1127:                break;
                   1128:        case MSG_IDENTIFY_TERM:
1.110     nicm     1129:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1130:                        fatalx("bad MSG_IDENTIFY_TERM string");
                   1131:                c->term = xstrdup(data);
1.158     nicm     1132:                log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109     nicm     1133:                break;
                   1134:        case MSG_IDENTIFY_TTYNAME:
1.110     nicm     1135:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1136:                        fatalx("bad MSG_IDENTIFY_TTYNAME string");
                   1137:                c->ttyname = xstrdup(data);
1.158     nicm     1138:                log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109     nicm     1139:                break;
                   1140:        case MSG_IDENTIFY_CWD:
1.155     nicm     1141:                if (datalen == 0 || data[datalen - 1] != '\0')
                   1142:                        fatalx("bad MSG_IDENTIFY_CWD string");
1.165     nicm     1143:                if (access(data, X_OK) == 0)
                   1144:                        c->cwd = xstrdup(data);
                   1145:                else if ((home = find_home()) != NULL)
                   1146:                        c->cwd = xstrdup(home);
                   1147:                else
                   1148:                        c->cwd = xstrdup("/");
1.158     nicm     1149:                log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109     nicm     1150:                break;
                   1151:        case MSG_IDENTIFY_STDIN:
                   1152:                if (datalen != 0)
                   1153:                        fatalx("bad MSG_IDENTIFY_STDIN size");
                   1154:                c->fd = imsg->fd;
1.158     nicm     1155:                log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109     nicm     1156:                break;
                   1157:        case MSG_IDENTIFY_ENVIRON:
1.110     nicm     1158:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1159:                        fatalx("bad MSG_IDENTIFY_ENVIRON string");
                   1160:                if (strchr(data, '=') != NULL)
1.164     nicm     1161:                        environ_put(c->environ, data);
1.158     nicm     1162:                log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143     nicm     1163:                break;
                   1164:        case MSG_IDENTIFY_CLIENTPID:
                   1165:                if (datalen != sizeof c->pid)
                   1166:                        fatalx("bad MSG_IDENTIFY_CLIENTPID size");
                   1167:                memcpy(&c->pid, data, sizeof c->pid);
1.158     nicm     1168:                log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109     nicm     1169:                break;
                   1170:        default:
                   1171:                break;
                   1172:        }
                   1173:
                   1174:        if (imsg->hdr.type != MSG_IDENTIFY_DONE)
                   1175:                return;
                   1176:        c->flags |= CLIENT_IDENTIFIED;
1.75      nicm     1177:
1.109     nicm     1178:        if (c->flags & CLIENT_CONTROL) {
1.75      nicm     1179:                c->stdin_callback = control_callback;
1.109     nicm     1180:
1.97      nicm     1181:                evbuffer_free(c->stderr_data);
                   1182:                c->stderr_data = c->stdout_data;
1.109     nicm     1183:
                   1184:                if (c->flags & CLIENT_CONTROLCONTROL)
1.96      nicm     1185:                        evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162     nicm     1186:                proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75      nicm     1187:
                   1188:                c->tty.fd = -1;
                   1189:
1.109     nicm     1190:                close(c->fd);
                   1191:                c->fd = -1;
                   1192:
1.75      nicm     1193:                return;
                   1194:        }
1.1       nicm     1195:
1.109     nicm     1196:        if (c->fd == -1)
1.104     nicm     1197:                return;
1.145     nicm     1198:        if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109     nicm     1199:                close(c->fd);
                   1200:                c->fd = -1;
1.80      nicm     1201:                return;
                   1202:        }
1.109     nicm     1203:        if (c->flags & CLIENT_UTF8)
1.1       nicm     1204:                c->tty.flags |= TTY_UTF8;
1.109     nicm     1205:        if (c->flags & CLIENT_256COLOURS)
1.1       nicm     1206:                c->tty.term_flags |= TERM_256COLOURS;
                   1207:
                   1208:        tty_resize(&c->tty);
                   1209:
1.109     nicm     1210:        if (!(c->flags & CLIENT_CONTROL))
1.86      nicm     1211:                c->flags |= CLIENT_TERMINAL;
1.1       nicm     1212: }
                   1213:
                   1214: /* Handle shell message. */
                   1215: void
1.162     nicm     1216: server_client_dispatch_shell(struct client *c)
1.1       nicm     1217: {
1.107     nicm     1218:        const char      *shell;
1.25      nicm     1219:
1.163     nicm     1220:        shell = options_get_string(global_s_options, "default-shell");
1.1       nicm     1221:        if (*shell == '\0' || areshell(shell))
                   1222:                shell = _PATH_BSHELL;
1.162     nicm     1223:        proc_send_s(c->peer, MSG_SHELL, shell);
1.25      nicm     1224:
1.162     nicm     1225:        proc_kill_peer(c->peer);
1.169     nicm     1226: }
                   1227:
                   1228: /* Event callback to push more stdout data if any left. */
                   1229: static void
1.170     nicm     1230: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1231: {
                   1232:        struct client   *c = arg;
                   1233:
                   1234:        if (~c->flags & CLIENT_DEAD)
                   1235:                server_client_push_stdout(c);
                   1236:        server_client_unref(c);
                   1237: }
                   1238:
                   1239: /* Push stdout to client if possible. */
                   1240: void
                   1241: server_client_push_stdout(struct client *c)
                   1242: {
                   1243:        struct msg_stdout_data data;
                   1244:        size_t                 sent, left;
                   1245:
                   1246:        left = EVBUFFER_LENGTH(c->stdout_data);
                   1247:        while (left != 0) {
                   1248:                sent = left;
                   1249:                if (sent > sizeof data.data)
                   1250:                        sent = sizeof data.data;
                   1251:                memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
                   1252:                data.size = sent;
                   1253:
                   1254:                if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
                   1255:                        break;
                   1256:                evbuffer_drain(c->stdout_data, sent);
                   1257:
                   1258:                left = EVBUFFER_LENGTH(c->stdout_data);
                   1259:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1260:                    sent, left);
                   1261:        }
                   1262:        if (left != 0) {
                   1263:                c->references++;
                   1264:                event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
                   1265:                log_debug("%s: client %p, queued", __func__, c);
                   1266:        }
                   1267: }
                   1268:
                   1269: /* Event callback to push more stderr data if any left. */
                   1270: static void
1.170     nicm     1271: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1272: {
                   1273:        struct client   *c = arg;
                   1274:
                   1275:        if (~c->flags & CLIENT_DEAD)
                   1276:                server_client_push_stderr(c);
                   1277:        server_client_unref(c);
                   1278: }
                   1279:
                   1280: /* Push stderr to client if possible. */
                   1281: void
                   1282: server_client_push_stderr(struct client *c)
                   1283: {
                   1284:        struct msg_stderr_data data;
                   1285:        size_t                 sent, left;
                   1286:
                   1287:        if (c->stderr_data == c->stdout_data) {
                   1288:                server_client_push_stdout(c);
                   1289:                return;
                   1290:        }
                   1291:
                   1292:        left = EVBUFFER_LENGTH(c->stderr_data);
                   1293:        while (left != 0) {
                   1294:                sent = left;
                   1295:                if (sent > sizeof data.data)
                   1296:                        sent = sizeof data.data;
                   1297:                memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
                   1298:                data.size = sent;
                   1299:
                   1300:                if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
                   1301:                        break;
                   1302:                evbuffer_drain(c->stderr_data, sent);
                   1303:
                   1304:                left = EVBUFFER_LENGTH(c->stderr_data);
                   1305:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1306:                    sent, left);
                   1307:        }
                   1308:        if (left != 0) {
                   1309:                c->references++;
                   1310:                event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
                   1311:                log_debug("%s: client %p, queued", __func__, c);
                   1312:        }
1.1       nicm     1313: }