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

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