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

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