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

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