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

1.164   ! nicm        1: /* $OpenBSD: server-client.c,v 1.163 2015/10/27 15:58:42 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.132     nicm       35: void   server_client_key_table(struct client *, const char *);
1.141     nicm       36: void   server_client_free(int, short, void *);
1.91      nicm       37: void   server_client_check_focus(struct window_pane *);
1.92      nicm       38: void   server_client_check_resize(struct window_pane *);
1.131     nicm       39: int    server_client_check_mouse(struct client *);
1.17      nicm       40: void   server_client_repeat_timer(int, short, void *);
1.36      nicm       41: void   server_client_check_exit(struct client *);
1.1       nicm       42: void   server_client_check_redraw(struct client *);
                     43: void   server_client_set_title(struct client *);
1.18      nicm       44: void   server_client_reset_state(struct client *);
1.82      nicm       45: int    server_client_assume_paste(struct session *);
1.1       nicm       46:
1.162     nicm       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.145     nicm      101:        c->cwd = -1;
                    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.109     nicm      197:        close(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.131     nicm      260: int
                    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;
                    270:        int                                      key;
                    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.74      nicm      504: server_client_handle_key(struct client *c, int 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.163     nicm      638:        if (key == options_get_number(s->options, "prefix") ||
                    639:            key == 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:        /*
                    804:         * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
                    805:         * user has set mouse-utf8 and any mouse mode is in effect, turn on
                    806:         * UTF-8 mouse input. If the receiving terminal hasn't requested it
                    807:         * (that is, it isn't in s->mode), then it'll be converted in
                    808:         * input_mouse.
                    809:         */
                    810:        if ((c->tty.flags & TTY_UTF8) &&
                    811:            (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
                    812:                mode |= MODE_MOUSE_UTF8;
                    813:        else
                    814:                mode &= ~MODE_MOUSE_UTF8;
                    815:
                    816:        /* Set the terminal mode and reset attributes. */
1.59      nicm      817:        tty_update_mode(&c->tty, mode, s);
1.1       nicm      818:        tty_reset(&c->tty);
1.17      nicm      819: }
                    820:
                    821: /* Repeat time callback. */
                    822: void
                    823: server_client_repeat_timer(unused int fd, unused short events, void *data)
                    824: {
                    825:        struct client   *c = data;
                    826:
1.85      nicm      827:        if (c->flags & CLIENT_REPEAT) {
1.132     nicm      828:                server_client_key_table(c, "root");
                    829:                c->flags &= ~CLIENT_REPEAT;
                    830:                server_status_client(c);
1.85      nicm      831:        }
1.1       nicm      832: }
                    833:
1.36      nicm      834: /* Check if client should be exited. */
                    835: void
                    836: server_client_check_exit(struct client *c)
                    837: {
                    838:        if (!(c->flags & CLIENT_EXIT))
                    839:                return;
                    840:
1.73      nicm      841:        if (EVBUFFER_LENGTH(c->stdin_data) != 0)
                    842:                return;
                    843:        if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36      nicm      844:                return;
1.73      nicm      845:        if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36      nicm      846:                return;
                    847:
1.162     nicm      848:        proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36      nicm      849:        c->flags &= ~CLIENT_EXIT;
1.38      nicm      850: }
                    851:
1.1       nicm      852: /* Check for client redraws. */
                    853: void
                    854: server_client_check_redraw(struct client *c)
                    855: {
                    856:        struct session          *s = c->session;
1.137     nicm      857:        struct tty              *tty = &c->tty;
1.1       nicm      858:        struct window_pane      *wp;
                    859:        int                      flags, redraw;
                    860:
1.86      nicm      861:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79      nicm      862:                return;
                    863:
1.1       nicm      864:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.163     nicm      865:                if (options_get_number(s->options, "set-titles"))
1.1       nicm      866:                        server_client_set_title(c);
1.12      nicm      867:
1.1       nicm      868:                if (c->message_string != NULL)
                    869:                        redraw = status_message_redraw(c);
                    870:                else if (c->prompt_string != NULL)
                    871:                        redraw = status_prompt_redraw(c);
                    872:                else
                    873:                        redraw = status_redraw(c);
                    874:                if (!redraw)
                    875:                        c->flags &= ~CLIENT_STATUS;
                    876:        }
                    877:
1.137     nicm      878:        flags = tty->flags & (TTY_FREEZE|TTY_NOCURSOR);
                    879:        tty->flags = (tty->flags & ~TTY_FREEZE) | TTY_NOCURSOR;
                    880:
1.1       nicm      881:        if (c->flags & CLIENT_REDRAW) {
1.137     nicm      882:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      883:                screen_redraw_screen(c, 1, 1, 1);
1.27      nicm      884:                c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38      nicm      885:        } else if (c->flags & CLIENT_REDRAWWINDOW) {
1.137     nicm      886:                tty_update_mode(tty, tty->mode, NULL);
1.38      nicm      887:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
                    888:                        screen_redraw_pane(c, wp);
                    889:                c->flags &= ~CLIENT_REDRAWWINDOW;
1.1       nicm      890:        } else {
                    891:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137     nicm      892:                        if (wp->flags & PANE_REDRAW) {
                    893:                                tty_update_mode(tty, tty->mode, NULL);
1.1       nicm      894:                                screen_redraw_pane(c, wp);
1.137     nicm      895:                        }
1.1       nicm      896:                }
                    897:        }
                    898:
1.137     nicm      899:        if (c->flags & CLIENT_BORDERS) {
                    900:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      901:                screen_redraw_screen(c, 0, 0, 1);
1.137     nicm      902:        }
1.27      nicm      903:
1.137     nicm      904:        if (c->flags & CLIENT_STATUS) {
                    905:                tty_update_mode(tty, tty->mode, NULL);
1.115     nicm      906:                screen_redraw_screen(c, 0, 1, 0);
1.137     nicm      907:        }
1.1       nicm      908:
1.137     nicm      909:        tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
                    910:        tty_update_mode(tty, tty->mode, NULL);
1.1       nicm      911:
1.153     nicm      912:        c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS|
                    913:            CLIENT_STATUSFORCE);
1.1       nicm      914: }
                    915:
                    916: /* Set client title. */
                    917: void
                    918: server_client_set_title(struct client *c)
                    919: {
1.128     nicm      920:        struct session          *s = c->session;
                    921:        const char              *template;
                    922:        char                    *title;
                    923:        struct format_tree      *ft;
1.1       nicm      924:
1.163     nicm      925:        template = options_get_string(s->options, "set-titles-string");
1.25      nicm      926:
1.128     nicm      927:        ft = format_create();
                    928:        format_defaults(ft, c, NULL, NULL, NULL);
                    929:
                    930:        title = format_expand_time(ft, template, time(NULL));
1.1       nicm      931:        if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77      nicm      932:                free(c->title);
1.1       nicm      933:                c->title = xstrdup(title);
                    934:                tty_set_title(&c->tty, c->title);
                    935:        }
1.77      nicm      936:        free(title);
1.128     nicm      937:
                    938:        format_free(ft);
1.1       nicm      939: }
                    940:
                    941: /* Dispatch message from client. */
1.162     nicm      942: void
                    943: server_client_dispatch(struct imsg *imsg, void *arg)
1.1       nicm      944: {
1.162     nicm      945:        struct client           *c = arg;
1.73      nicm      946:        struct msg_stdin_data    stdindata;
1.108     nicm      947:        const char              *data;
1.162     nicm      948:        ssize_t                  datalen;
1.149     nicm      949:        struct session          *s;
1.1       nicm      950:
1.162     nicm      951:        if (c->flags & CLIENT_DEAD)
                    952:                return;
                    953:
                    954:        if (imsg == NULL) {
                    955:                server_client_lost(c);
                    956:                return;
                    957:        }
1.1       nicm      958:
1.162     nicm      959:        data = imsg->data;
                    960:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1       nicm      961:
1.162     nicm      962:        switch (imsg->hdr.type) {
                    963:        case MSG_IDENTIFY_FLAGS:
                    964:        case MSG_IDENTIFY_TERM:
                    965:        case MSG_IDENTIFY_TTYNAME:
                    966:        case MSG_IDENTIFY_CWD:
                    967:        case MSG_IDENTIFY_STDIN:
                    968:        case MSG_IDENTIFY_ENVIRON:
                    969:        case MSG_IDENTIFY_CLIENTPID:
                    970:        case MSG_IDENTIFY_DONE:
                    971:                server_client_dispatch_identify(c, imsg);
                    972:                break;
                    973:        case MSG_COMMAND:
                    974:                server_client_dispatch_command(c, imsg);
                    975:                break;
                    976:        case MSG_STDIN:
                    977:                if (datalen != sizeof stdindata)
                    978:                        fatalx("bad MSG_STDIN size");
                    979:                memcpy(&stdindata, data, sizeof stdindata);
1.36      nicm      980:
1.162     nicm      981:                if (c->stdin_callback == NULL)
1.33      nicm      982:                        break;
1.162     nicm      983:                if (stdindata.size <= 0)
                    984:                        c->stdin_closed = 1;
                    985:                else {
                    986:                        evbuffer_add(c->stdin_data, stdindata.data,
                    987:                            stdindata.size);
                    988:                }
                    989:                c->stdin_callback(c, c->stdin_closed,
                    990:                    c->stdin_callback_data);
                    991:                break;
                    992:        case MSG_RESIZE:
                    993:                if (datalen != 0)
                    994:                        fatalx("bad MSG_RESIZE size");
1.1       nicm      995:
1.162     nicm      996:                if (c->flags & CLIENT_CONTROL)
1.1       nicm      997:                        break;
1.162     nicm      998:                if (tty_resize(&c->tty)) {
                    999:                        recalculate_sizes();
                   1000:                        server_redraw_client(c);
                   1001:                }
                   1002:                break;
                   1003:        case MSG_EXITING:
                   1004:                if (datalen != 0)
                   1005:                        fatalx("bad MSG_EXITING size");
1.1       nicm     1006:
1.162     nicm     1007:                c->session = NULL;
                   1008:                tty_close(&c->tty);
                   1009:                proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
                   1010:                break;
                   1011:        case MSG_WAKEUP:
                   1012:        case MSG_UNLOCK:
                   1013:                if (datalen != 0)
                   1014:                        fatalx("bad MSG_WAKEUP size");
1.121     nicm     1015:
1.162     nicm     1016:                if (!(c->flags & CLIENT_SUSPENDED))
                   1017:                        break;
                   1018:                c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm     1019:
1.162     nicm     1020:                if (c->tty.fd == -1) /* exited in the meantime */
1.1       nicm     1021:                        break;
1.162     nicm     1022:                s = c->session;
1.1       nicm     1023:
1.162     nicm     1024:                if (gettimeofday(&c->activity_time, NULL) != 0)
                   1025:                        fatal("gettimeofday failed");
                   1026:                if (s != NULL)
                   1027:                        session_update_activity(s, &c->activity_time);
                   1028:
                   1029:                tty_start_tty(&c->tty);
                   1030:                server_redraw_client(c);
                   1031:                recalculate_sizes();
                   1032:                break;
                   1033:        case MSG_SHELL:
                   1034:                if (datalen != 0)
                   1035:                        fatalx("bad MSG_SHELL size");
1.1       nicm     1036:
1.162     nicm     1037:                server_client_dispatch_shell(c);
                   1038:                break;
1.1       nicm     1039:        }
1.162     nicm     1040:
                   1041:        server_push_stdout(c);
                   1042:        server_push_stderr(c);
1.1       nicm     1043: }
                   1044:
                   1045: /* Handle command message. */
                   1046: void
1.162     nicm     1047: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1       nicm     1048: {
1.108     nicm     1049:        struct msg_command_data   data;
                   1050:        char                     *buf;
                   1051:        size_t                    len;
                   1052:        struct cmd_list          *cmdlist = NULL;
                   1053:        int                       argc;
                   1054:        char                    **argv, *cause;
                   1055:
                   1056:        if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
                   1057:                fatalx("bad MSG_COMMAND size");
                   1058:        memcpy(&data, imsg->data, sizeof data);
                   1059:
1.124     nicm     1060:        buf = (char *)imsg->data + sizeof data;
1.108     nicm     1061:        len = imsg->hdr.len  - IMSG_HEADER_SIZE - sizeof data;
                   1062:        if (len > 0 && buf[len - 1] != '\0')
                   1063:                fatalx("bad MSG_COMMAND string");
                   1064:
                   1065:        argc = data.argc;
                   1066:        if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.94      nicm     1067:                cmdq_error(c->cmdq, "command too long");
1.1       nicm     1068:                goto error;
                   1069:        }
                   1070:
                   1071:        if (argc == 0) {
                   1072:                argc = 1;
                   1073:                argv = xcalloc(1, sizeof *argv);
                   1074:                *argv = xstrdup("new-session");
                   1075:        }
                   1076:
1.94      nicm     1077:        if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
                   1078:                cmdq_error(c->cmdq, "%s", cause);
1.1       nicm     1079:                cmd_free_argv(argc, argv);
                   1080:                goto error;
                   1081:        }
                   1082:        cmd_free_argv(argc, argv);
                   1083:
1.113     nicm     1084:        if (c != cfg_client || cfg_finished)
1.131     nicm     1085:                cmdq_run(c->cmdq, cmdlist, NULL);
1.113     nicm     1086:        else
1.131     nicm     1087:                cmdq_append(c->cmdq, cmdlist, NULL);
1.1       nicm     1088:        cmd_list_free(cmdlist);
                   1089:        return;
                   1090:
                   1091: error:
                   1092:        if (cmdlist != NULL)
                   1093:                cmd_list_free(cmdlist);
1.88      nicm     1094:
1.36      nicm     1095:        c->flags |= CLIENT_EXIT;
1.1       nicm     1096: }
                   1097:
                   1098: /* Handle identify message. */
                   1099: void
1.162     nicm     1100: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1       nicm     1101: {
1.109     nicm     1102:        const char      *data;
                   1103:        size_t           datalen;
                   1104:        int              flags;
                   1105:
                   1106:        if (c->flags & CLIENT_IDENTIFIED)
                   1107:                fatalx("out-of-order identify message");
                   1108:
                   1109:        data = imsg->data;
                   1110:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
                   1111:
                   1112:        switch (imsg->hdr.type) {
                   1113:        case MSG_IDENTIFY_FLAGS:
                   1114:                if (datalen != sizeof flags)
                   1115:                        fatalx("bad MSG_IDENTIFY_FLAGS size");
                   1116:                memcpy(&flags, data, sizeof flags);
                   1117:                c->flags |= flags;
1.158     nicm     1118:                log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109     nicm     1119:                break;
                   1120:        case MSG_IDENTIFY_TERM:
1.110     nicm     1121:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1122:                        fatalx("bad MSG_IDENTIFY_TERM string");
                   1123:                c->term = xstrdup(data);
1.158     nicm     1124:                log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109     nicm     1125:                break;
                   1126:        case MSG_IDENTIFY_TTYNAME:
1.110     nicm     1127:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1128:                        fatalx("bad MSG_IDENTIFY_TTYNAME string");
                   1129:                c->ttyname = xstrdup(data);
1.158     nicm     1130:                log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109     nicm     1131:                break;
                   1132:        case MSG_IDENTIFY_CWD:
1.155     nicm     1133:                if (datalen == 0 || data[datalen - 1] != '\0')
                   1134:                        fatalx("bad MSG_IDENTIFY_CWD string");
                   1135:                if ((c->cwd = open(data, O_RDONLY)) == -1)
                   1136:                        c->cwd = open("/", O_RDONLY);
1.158     nicm     1137:                log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109     nicm     1138:                break;
                   1139:        case MSG_IDENTIFY_STDIN:
                   1140:                if (datalen != 0)
                   1141:                        fatalx("bad MSG_IDENTIFY_STDIN size");
                   1142:                c->fd = imsg->fd;
1.158     nicm     1143:                log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109     nicm     1144:                break;
                   1145:        case MSG_IDENTIFY_ENVIRON:
1.110     nicm     1146:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1147:                        fatalx("bad MSG_IDENTIFY_ENVIRON string");
                   1148:                if (strchr(data, '=') != NULL)
1.164   ! nicm     1149:                        environ_put(c->environ, data);
1.158     nicm     1150:                log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143     nicm     1151:                break;
                   1152:        case MSG_IDENTIFY_CLIENTPID:
                   1153:                if (datalen != sizeof c->pid)
                   1154:                        fatalx("bad MSG_IDENTIFY_CLIENTPID size");
                   1155:                memcpy(&c->pid, data, sizeof c->pid);
1.158     nicm     1156:                log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109     nicm     1157:                break;
                   1158:        default:
                   1159:                break;
                   1160:        }
                   1161:
                   1162:        if (imsg->hdr.type != MSG_IDENTIFY_DONE)
                   1163:                return;
                   1164:        c->flags |= CLIENT_IDENTIFIED;
1.75      nicm     1165:
1.109     nicm     1166:        if (c->flags & CLIENT_CONTROL) {
1.75      nicm     1167:                c->stdin_callback = control_callback;
1.109     nicm     1168:
1.97      nicm     1169:                evbuffer_free(c->stderr_data);
                   1170:                c->stderr_data = c->stdout_data;
1.109     nicm     1171:
                   1172:                if (c->flags & CLIENT_CONTROLCONTROL)
1.96      nicm     1173:                        evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162     nicm     1174:                proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75      nicm     1175:
                   1176:                c->tty.fd = -1;
                   1177:                c->tty.log_fd = -1;
                   1178:
1.109     nicm     1179:                close(c->fd);
                   1180:                c->fd = -1;
                   1181:
1.75      nicm     1182:                return;
                   1183:        }
1.1       nicm     1184:
1.109     nicm     1185:        if (c->fd == -1)
1.104     nicm     1186:                return;
1.145     nicm     1187:        if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109     nicm     1188:                close(c->fd);
                   1189:                c->fd = -1;
1.80      nicm     1190:                return;
                   1191:        }
1.109     nicm     1192:        if (c->flags & CLIENT_UTF8)
1.1       nicm     1193:                c->tty.flags |= TTY_UTF8;
1.109     nicm     1194:        if (c->flags & CLIENT_256COLOURS)
1.1       nicm     1195:                c->tty.term_flags |= TERM_256COLOURS;
                   1196:
                   1197:        tty_resize(&c->tty);
                   1198:
1.109     nicm     1199:        if (!(c->flags & CLIENT_CONTROL))
1.86      nicm     1200:                c->flags |= CLIENT_TERMINAL;
1.1       nicm     1201: }
                   1202:
                   1203: /* Handle shell message. */
                   1204: void
1.162     nicm     1205: server_client_dispatch_shell(struct client *c)
1.1       nicm     1206: {
1.107     nicm     1207:        const char      *shell;
1.25      nicm     1208:
1.163     nicm     1209:        shell = options_get_string(global_s_options, "default-shell");
1.1       nicm     1210:        if (*shell == '\0' || areshell(shell))
                   1211:                shell = _PATH_BSHELL;
1.162     nicm     1212:        proc_send_s(c->peer, MSG_SHELL, shell);
1.25      nicm     1213:
1.162     nicm     1214:        proc_kill_peer(c->peer);
1.1       nicm     1215: }