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

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