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

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