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

1.27    ! nicm        1: /* $OpenBSD: server-client.c,v 1.26 2009/12/10 09:16:52 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>
                     20:
1.12      nicm       21: #include <event.h>
1.1       nicm       22: #include <fcntl.h>
                     23: #include <string.h>
1.4       nicm       24: #include <time.h>
1.1       nicm       25: #include <paths.h>
                     26: #include <unistd.h>
                     27:
                     28: #include "tmux.h"
                     29:
1.18      nicm       30: void   server_client_handle_key(int, struct mouse_event *, void *);
1.17      nicm       31: void   server_client_repeat_timer(int, short, void *);
1.1       nicm       32: void   server_client_check_redraw(struct client *);
                     33: void   server_client_set_title(struct client *);
1.18      nicm       34: void   server_client_reset_state(struct client *);
1.1       nicm       35:
                     36: int    server_client_msg_dispatch(struct client *);
                     37: void   server_client_msg_command(struct client *, struct msg_command_data *);
                     38: void   server_client_msg_identify(
1.25      nicm       39:            struct client *, struct msg_identify_data *, int);
1.1       nicm       40: void   server_client_msg_shell(struct client *);
                     41:
                     42: void printflike2 server_client_msg_error(struct cmd_ctx *, const char *, ...);
                     43: void printflike2 server_client_msg_print(struct cmd_ctx *, const char *, ...);
                     44: void printflike2 server_client_msg_info(struct cmd_ctx *, const char *, ...);
                     45:
                     46: /* Create a new client. */
                     47: void
                     48: server_client_create(int fd)
                     49: {
                     50:        struct client   *c;
                     51:        int              mode;
                     52:        u_int            i;
                     53:
                     54:        if ((mode = fcntl(fd, F_GETFL)) == -1)
                     55:                fatal("fcntl failed");
                     56:        if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
                     57:                fatal("fcntl failed");
                     58:        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
                     59:                fatal("fcntl failed");
                     60:
                     61:        c = xcalloc(1, sizeof *c);
                     62:        c->references = 0;
                     63:        imsg_init(&c->ibuf, fd);
1.14      nicm       64:        server_update_event(c);
1.25      nicm       65:
1.10      nicm       66:        if (gettimeofday(&c->creation_time, NULL) != 0)
1.1       nicm       67:                fatal("gettimeofday failed");
1.11      nicm       68:        memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.1       nicm       69:
                     70:        ARRAY_INIT(&c->prompt_hdata);
                     71:
                     72:        c->tty.fd = -1;
                     73:        c->title = NULL;
                     74:
                     75:        c->session = NULL;
                     76:        c->tty.sx = 80;
                     77:        c->tty.sy = 24;
                     78:
                     79:        screen_init(&c->status, c->tty.sx, 1, 0);
                     80:        job_tree_init(&c->status_jobs);
                     81:
                     82:        c->message_string = NULL;
1.21      nicm       83:        ARRAY_INIT(&c->message_log);
1.1       nicm       84:
                     85:        c->prompt_string = NULL;
                     86:        c->prompt_buffer = NULL;
                     87:        c->prompt_index = 0;
                     88:
1.17      nicm       89:        evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
                     90:
1.1       nicm       91:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     92:                if (ARRAY_ITEM(&clients, i) == NULL) {
                     93:                        ARRAY_SET(&clients, i, c);
                     94:                        return;
                     95:                }
                     96:        }
                     97:        ARRAY_ADD(&clients, c);
                     98:        log_debug("new client %d", fd);
                     99: }
                    100:
                    101: /* Lost a client. */
                    102: void
                    103: server_client_lost(struct client *c)
                    104: {
1.21      nicm      105:        struct message_entry    *msg;
                    106:        u_int                    i;
1.1       nicm      107:
                    108:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    109:                if (ARRAY_ITEM(&clients, i) == c)
                    110:                        ARRAY_SET(&clients, i, NULL);
                    111:        }
                    112:        log_debug("lost client %d", c->ibuf.fd);
                    113:
                    114:        /*
                    115:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    116:         * and tty_free might close an unrelated fd.
                    117:         */
                    118:        if (c->flags & CLIENT_TERMINAL)
                    119:                tty_free(&c->tty);
                    120:
                    121:        screen_free(&c->status);
                    122:        job_tree_free(&c->status_jobs);
                    123:
                    124:        if (c->title != NULL)
                    125:                xfree(c->title);
                    126:
1.17      nicm      127:        evtimer_del(&c->repeat_timer);
                    128:
1.15      nicm      129:        evtimer_del(&c->identify_timer);
                    130:
1.1       nicm      131:        if (c->message_string != NULL)
                    132:                xfree(c->message_string);
1.15      nicm      133:        evtimer_del(&c->message_timer);
1.21      nicm      134:        for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
                    135:                msg = &ARRAY_ITEM(&c->message_log, i);
                    136:                xfree(msg->msg);
                    137:        }
                    138:        ARRAY_FREE(&c->message_log);
1.1       nicm      139:
                    140:        if (c->prompt_string != NULL)
                    141:                xfree(c->prompt_string);
                    142:        if (c->prompt_buffer != NULL)
                    143:                xfree(c->prompt_buffer);
                    144:        for (i = 0; i < ARRAY_LENGTH(&c->prompt_hdata); i++)
                    145:                xfree(ARRAY_ITEM(&c->prompt_hdata, i));
                    146:        ARRAY_FREE(&c->prompt_hdata);
                    147:
                    148:        if (c->cwd != NULL)
                    149:                xfree(c->cwd);
                    150:
                    151:        close(c->ibuf.fd);
                    152:        imsg_clear(&c->ibuf);
1.12      nicm      153:        event_del(&c->event);
1.13      nicm      154:
1.1       nicm      155:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                    156:                if (ARRAY_ITEM(&dead_clients, i) == NULL) {
                    157:                        ARRAY_SET(&dead_clients, i, c);
                    158:                        break;
                    159:                }
                    160:        }
                    161:        if (i == ARRAY_LENGTH(&dead_clients))
                    162:                ARRAY_ADD(&dead_clients, c);
                    163:        c->flags |= CLIENT_DEAD;
                    164:
                    165:        recalculate_sizes();
1.19      nicm      166:        server_update_socket();
1.9       nicm      167: }
                    168:
1.1       nicm      169: /* Process a single client event. */
                    170: void
1.12      nicm      171: server_client_callback(int fd, short events, void *data)
1.1       nicm      172: {
                    173:        struct client   *c = data;
1.7       nicm      174:
                    175:        if (c->flags & CLIENT_DEAD)
                    176:                return;
1.1       nicm      177:
                    178:        if (fd == c->ibuf.fd) {
1.12      nicm      179:                if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
1.1       nicm      180:                        goto client_lost;
                    181:
                    182:                if (c->flags & CLIENT_BAD) {
                    183:                        if (c->ibuf.w.queued == 0)
                    184:                                goto client_lost;
                    185:                        return;
                    186:                }
                    187:
1.12      nicm      188:                if (events & EV_READ && server_client_msg_dispatch(c) != 0)
1.1       nicm      189:                        goto client_lost;
                    190:        }
1.14      nicm      191:
1.25      nicm      192:        server_update_event(c);
1.1       nicm      193:        return;
                    194:
                    195: client_lost:
                    196:        server_client_lost(c);
                    197: }
                    198:
1.16      nicm      199: /* Handle client status timer. */
                    200: void
                    201: server_client_status_timer(void)
                    202: {
                    203:        struct client   *c;
                    204:        struct session  *s;
                    205:        struct job      *job;
                    206:        struct timeval   tv;
1.20      nicm      207:        u_int            i;
                    208:        int              interval;
                    209:        time_t           difference;
1.16      nicm      210:
                    211:        if (gettimeofday(&tv, NULL) != 0)
                    212:                fatal("gettimeofday failed");
                    213:
                    214:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    215:                c = ARRAY_ITEM(&clients, i);
                    216:                if (c == NULL || c->session == NULL)
                    217:                        continue;
                    218:                if (c->message_string != NULL || c->prompt_string != NULL) {
                    219:                        /*
                    220:                         * Don't need timed redraw for messages/prompts so bail
                    221:                         * now. The status timer isn't reset when they are
                    222:                         * redrawn anyway.
                    223:                         */
                    224:                        continue;
                    225:                }
                    226:                s = c->session;
                    227:
                    228:                if (!options_get_number(&s->options, "status"))
                    229:                        continue;
                    230:                interval = options_get_number(&s->options, "status-interval");
                    231:
1.20      nicm      232:                difference = tv.tv_sec - c->status_timer.tv_sec;
                    233:                if (difference >= interval) {
1.16      nicm      234:                        RB_FOREACH(job, jobs, &c->status_jobs)
1.20      nicm      235:                                job_run(job);
1.16      nicm      236:                        c->flags |= CLIENT_STATUS;
                    237:                }
                    238:        }
                    239: }
                    240:
1.18      nicm      241: /* Handle data key input from client. */
                    242: void
                    243: server_client_handle_key(int key, struct mouse_event *mouse, void *data)
                    244: {
                    245:        struct client           *c = data;
                    246:        struct session          *s;
                    247:        struct window           *w;
                    248:        struct window_pane      *wp;
                    249:        struct options          *oo;
                    250:        struct timeval           tv;
                    251:        struct key_binding      *bd;
                    252:        struct keylist          *keylist;
                    253:        int                      xtimeout, isprefix;
                    254:        u_int                    i;
                    255:
                    256:        /* Check the client is good to accept input. */
                    257:        if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
                    258:                return;
                    259:        if (c->session == NULL)
                    260:                return;
                    261:        s = c->session;
                    262:
                    263:        /* Update the activity timer. */
                    264:        if (gettimeofday(&c->activity_time, NULL) != 0)
                    265:                fatal("gettimeofday failed");
                    266:        memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
                    267:
                    268:        w = c->session->curw->window;
                    269:        wp = w->active;
                    270:        oo = &c->session->options;
                    271:
                    272:        /* Special case: number keys jump to pane in identify mode. */
1.25      nicm      273:        if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.18      nicm      274:                wp = window_pane_at_index(w, key - '0');
                    275:                if (wp != NULL && window_pane_visible(wp))
                    276:                        window_set_active_pane(w, wp);
                    277:                server_clear_identify(c);
                    278:                return;
                    279:        }
                    280:
                    281:        /* Handle status line. */
                    282:        status_message_clear(c);
                    283:        server_clear_identify(c);
                    284:        if (c->prompt_string != NULL) {
                    285:                status_prompt_key(c, key);
                    286:                return;
                    287:        }
                    288:
                    289:        /* Check for mouse keys. */
                    290:        if (key == KEYC_MOUSE) {
                    291:                if (options_get_number(oo, "mouse-select-pane")) {
                    292:                        window_set_active_at(w, mouse->x, mouse->y);
                    293:                        wp = w->active;
                    294:                }
                    295:                window_pane_mouse(wp, c, mouse);
                    296:                return;
                    297:        }
                    298:
                    299:        /* Is this a prefix key? */
                    300:        keylist = options_get_data(&c->session->options, "prefix");
                    301:        isprefix = 0;
                    302:        for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
                    303:                if (key == ARRAY_ITEM(keylist, i)) {
                    304:                        isprefix = 1;
                    305:                        break;
                    306:                }
                    307:        }
                    308:
                    309:        /* No previous prefix key. */
                    310:        if (!(c->flags & CLIENT_PREFIX)) {
                    311:                if (isprefix)
                    312:                        c->flags |= CLIENT_PREFIX;
                    313:                else {
                    314:                        /* Try as a non-prefix key binding. */
                    315:                        if ((bd = key_bindings_lookup(key)) == NULL)
                    316:                                window_pane_key(wp, c, key);
                    317:                        else
                    318:                                key_bindings_dispatch(bd, c);
                    319:                }
                    320:                return;
                    321:        }
                    322:
                    323:        /* Prefix key already pressed. Reset prefix and lookup key. */
                    324:        c->flags &= ~CLIENT_PREFIX;
                    325:        if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
                    326:                /* If repeating, treat this as a key, else ignore. */
                    327:                if (c->flags & CLIENT_REPEAT) {
                    328:                        c->flags &= ~CLIENT_REPEAT;
                    329:                        if (isprefix)
                    330:                                c->flags |= CLIENT_PREFIX;
                    331:                        else
                    332:                                window_pane_key(wp, c, key);
                    333:                }
                    334:                return;
                    335:        }
                    336:
                    337:        /* If already repeating, but this key can't repeat, skip it. */
                    338:        if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
                    339:                c->flags &= ~CLIENT_REPEAT;
                    340:                if (isprefix)
                    341:                        c->flags |= CLIENT_PREFIX;
                    342:                else
                    343:                        window_pane_key(wp, c, key);
                    344:                return;
                    345:        }
                    346:
                    347:        /* If this key can repeat, reset the repeat flags and timer. */
                    348:        xtimeout = options_get_number(&c->session->options, "repeat-time");
                    349:        if (xtimeout != 0 && bd->can_repeat) {
                    350:                c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
1.25      nicm      351:
1.18      nicm      352:                tv.tv_sec = xtimeout / 1000;
                    353:                tv.tv_usec = (xtimeout % 1000) * 1000L;
                    354:                evtimer_del(&c->repeat_timer);
                    355:                evtimer_add(&c->repeat_timer, &tv);
                    356:        }
                    357:
                    358:        /* Dispatch the command. */
                    359:        key_bindings_dispatch(bd, c);
                    360: }
                    361:
1.2       nicm      362: /* Client functions that need to happen every loop. */
                    363: void
                    364: server_client_loop(void)
                    365: {
                    366:        struct client           *c;
                    367:        struct window           *w;
                    368:        struct window_pane      *wp;
                    369:        u_int                    i;
                    370:
                    371:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    372:                c = ARRAY_ITEM(&clients, i);
                    373:                if (c == NULL || c->session == NULL)
                    374:                        continue;
                    375:
1.18      nicm      376:                server_client_check_redraw(c);
                    377:                server_client_reset_state(c);
1.2       nicm      378:        }
                    379:
                    380:        /*
                    381:         * Any windows will have been redrawn as part of clients, so clear
                    382:         * their flags now.
                    383:         */
                    384:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    385:                w = ARRAY_ITEM(&windows, i);
                    386:                if (w == NULL)
                    387:                        continue;
                    388:
                    389:                w->flags &= ~WINDOW_REDRAW;
                    390:                TAILQ_FOREACH(wp, &w->panes, entry)
                    391:                        wp->flags &= ~PANE_REDRAW;
                    392:        }
                    393: }
                    394:
1.18      nicm      395: /*
                    396:  * Update cursor position and mode settings. The scroll region and attributes
                    397:  * are cleared when idle (waiting for an event) as this is the most likely time
                    398:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                    399:  * compromise between excessive resets and likelihood of an interrupt.
                    400:  *
                    401:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                    402:  * things that are already in their default state.
                    403:  */
1.1       nicm      404: void
1.18      nicm      405: server_client_reset_state(struct client *c)
1.1       nicm      406: {
1.18      nicm      407:        struct window           *w = c->session->curw->window;
                    408:        struct window_pane      *wp = w->active;
                    409:        struct screen           *s = wp->screen;
                    410:        struct options          *oo = &c->session->options;
                    411:        int                      status, mode;
1.1       nicm      412:
                    413:        tty_region(&c->tty, 0, c->tty.sy - 1);
                    414:
                    415:        status = options_get_number(oo, "status");
                    416:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                    417:                tty_cursor(&c->tty, 0, 0);
                    418:        else
                    419:                tty_cursor(&c->tty, wp->xoff + s->cx, wp->yoff + s->cy);
                    420:
                    421:        mode = s->mode;
                    422:        if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
                    423:            options_get_number(oo, "mouse-select-pane"))
                    424:                mode |= MODE_MOUSE;
                    425:        tty_update_mode(&c->tty, mode);
                    426:        tty_reset(&c->tty);
1.17      nicm      427: }
                    428:
                    429: /* Repeat time callback. */
1.24      nicm      430: /* ARGSUSED */
1.17      nicm      431: void
                    432: server_client_repeat_timer(unused int fd, unused short events, void *data)
                    433: {
                    434:        struct client   *c = data;
                    435:
                    436:        if (c->flags & CLIENT_REPEAT)
                    437:                c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
1.1       nicm      438: }
                    439:
                    440: /* Check for client redraws. */
                    441: void
                    442: server_client_check_redraw(struct client *c)
                    443: {
                    444:        struct session          *s = c->session;
                    445:        struct window_pane      *wp;
                    446:        int                      flags, redraw;
                    447:
                    448:        flags = c->tty.flags & TTY_FREEZE;
                    449:        c->tty.flags &= ~TTY_FREEZE;
                    450:
                    451:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
                    452:                if (options_get_number(&s->options, "set-titles"))
                    453:                        server_client_set_title(c);
1.12      nicm      454:
1.1       nicm      455:                if (c->message_string != NULL)
                    456:                        redraw = status_message_redraw(c);
                    457:                else if (c->prompt_string != NULL)
                    458:                        redraw = status_prompt_redraw(c);
                    459:                else
                    460:                        redraw = status_redraw(c);
                    461:                if (!redraw)
                    462:                        c->flags &= ~CLIENT_STATUS;
                    463:        }
                    464:
                    465:        if (c->flags & CLIENT_REDRAW) {
1.27    ! nicm      466:                screen_redraw_screen(c, 0, 0);
        !           467:                c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.1       nicm      468:        } else {
                    469:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
                    470:                        if (wp->flags & PANE_REDRAW)
                    471:                                screen_redraw_pane(c, wp);
                    472:                }
                    473:        }
                    474:
1.27    ! nicm      475:        if (c->flags & CLIENT_BORDERS)
        !           476:                screen_redraw_screen(c, 0, 1);
        !           477:
1.1       nicm      478:        if (c->flags & CLIENT_STATUS)
1.27    ! nicm      479:                screen_redraw_screen(c, 1, 0);
1.1       nicm      480:
                    481:        c->tty.flags |= flags;
                    482:
1.27    ! nicm      483:        c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
1.1       nicm      484: }
                    485:
                    486: /* Set client title. */
                    487: void
                    488: server_client_set_title(struct client *c)
                    489: {
                    490:        struct session  *s = c->session;
                    491:        const char      *template;
                    492:        char            *title;
                    493:
                    494:        template = options_get_string(&s->options, "set-titles-string");
1.25      nicm      495:
1.23      nicm      496:        title = status_replace(c, NULL, template, time(NULL), 1);
1.1       nicm      497:        if (c->title == NULL || strcmp(title, c->title) != 0) {
                    498:                if (c->title != NULL)
                    499:                        xfree(c->title);
                    500:                c->title = xstrdup(title);
                    501:                tty_set_title(&c->tty, c->title);
                    502:        }
                    503:        xfree(title);
                    504: }
                    505:
                    506: /* Dispatch message from client. */
                    507: int
                    508: server_client_msg_dispatch(struct client *c)
                    509: {
                    510:        struct imsg              imsg;
                    511:        struct msg_command_data  commanddata;
                    512:        struct msg_identify_data identifydata;
                    513:        struct msg_environ_data  environdata;
                    514:        ssize_t                  n, datalen;
                    515:
1.8       deraadt   516:        if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
                    517:                return (-1);
1.1       nicm      518:
                    519:        for (;;) {
                    520:                if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
                    521:                        return (-1);
                    522:                if (n == 0)
                    523:                        return (0);
                    524:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
                    525:
                    526:                if (imsg.hdr.peerid != PROTOCOL_VERSION) {
                    527:                        server_write_client(c, MSG_VERSION, NULL, 0);
                    528:                        c->flags |= CLIENT_BAD;
                    529:                        imsg_free(&imsg);
                    530:                        continue;
                    531:                }
                    532:
                    533:                log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
                    534:                switch (imsg.hdr.type) {
                    535:                case MSG_COMMAND:
                    536:                        if (datalen != sizeof commanddata)
                    537:                                fatalx("bad MSG_COMMAND size");
                    538:                        memcpy(&commanddata, imsg.data, sizeof commanddata);
                    539:
                    540:                        server_client_msg_command(c, &commanddata);
                    541:                        break;
                    542:                case MSG_IDENTIFY:
                    543:                        if (datalen != sizeof identifydata)
                    544:                                fatalx("bad MSG_IDENTIFY size");
                    545:                        if (imsg.fd == -1)
                    546:                                fatalx("MSG_IDENTIFY missing fd");
                    547:                        memcpy(&identifydata, imsg.data, sizeof identifydata);
                    548:
                    549:                        server_client_msg_identify(c, &identifydata, imsg.fd);
                    550:                        break;
                    551:                case MSG_RESIZE:
                    552:                        if (datalen != 0)
                    553:                                fatalx("bad MSG_RESIZE size");
                    554:
                    555:                        tty_resize(&c->tty);
                    556:                        recalculate_sizes();
                    557:                        server_redraw_client(c);
                    558:                        break;
                    559:                case MSG_EXITING:
                    560:                        if (datalen != 0)
                    561:                                fatalx("bad MSG_EXITING size");
                    562:
                    563:                        c->session = NULL;
                    564:                        tty_close(&c->tty);
                    565:                        server_write_client(c, MSG_EXITED, NULL, 0);
                    566:                        break;
                    567:                case MSG_WAKEUP:
                    568:                case MSG_UNLOCK:
                    569:                        if (datalen != 0)
                    570:                                fatalx("bad MSG_WAKEUP size");
                    571:
                    572:                        if (!(c->flags & CLIENT_SUSPENDED))
                    573:                                break;
                    574:                        c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm      575:
1.11      nicm      576:                        if (gettimeofday(&c->activity_time, NULL) != 0)
                    577:                                fatal("gettimeofday");
                    578:                        if (c->session != NULL) {
                    579:                                memcpy(&c->session->activity_time,
                    580:                                    &c->activity_time,
                    581:                                    sizeof c->session->activity_time);
                    582:                        }
1.10      nicm      583:
1.1       nicm      584:                        tty_start_tty(&c->tty);
                    585:                        server_redraw_client(c);
                    586:                        recalculate_sizes();
                    587:                        break;
                    588:                case MSG_ENVIRON:
                    589:                        if (datalen != sizeof environdata)
                    590:                                fatalx("bad MSG_ENVIRON size");
                    591:                        memcpy(&environdata, imsg.data, sizeof environdata);
                    592:
                    593:                        environdata.var[(sizeof environdata.var) - 1] = '\0';
                    594:                        if (strchr(environdata.var, '=') != NULL)
                    595:                                environ_put(&c->environ, environdata.var);
                    596:                        break;
                    597:                case MSG_SHELL:
                    598:                        if (datalen != 0)
                    599:                                fatalx("bad MSG_SHELL size");
                    600:
                    601:                        server_client_msg_shell(c);
                    602:                        break;
                    603:                default:
                    604:                        fatalx("unexpected message");
                    605:                }
                    606:
                    607:                imsg_free(&imsg);
                    608:        }
                    609: }
                    610:
                    611: /* Callback to send error message to client. */
                    612: void printflike2
                    613: server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
                    614: {
                    615:        struct msg_print_data   data;
                    616:        va_list                 ap;
                    617:
                    618:        va_start(ap, fmt);
                    619:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
                    620:        va_end(ap);
                    621:
                    622:        server_write_client(ctx->cmdclient, MSG_ERROR, &data, sizeof data);
                    623: }
                    624:
                    625: /* Callback to send print message to client. */
                    626: void printflike2
                    627: server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
                    628: {
                    629:        struct msg_print_data   data;
                    630:        va_list                 ap;
                    631:
                    632:        va_start(ap, fmt);
                    633:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
                    634:        va_end(ap);
                    635:
                    636:        server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
                    637: }
                    638:
                    639: /* Callback to send print message to client, if not quiet. */
                    640: void printflike2
                    641: server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
                    642: {
                    643:        struct msg_print_data   data;
                    644:        va_list                 ap;
                    645:
1.26      nicm      646:        if (options_get_number(&global_options, "quiet"))
1.1       nicm      647:                return;
                    648:
                    649:        va_start(ap, fmt);
                    650:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
                    651:        va_end(ap);
                    652:
                    653:        server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
                    654: }
                    655:
                    656: /* Handle command message. */
                    657: void
                    658: server_client_msg_command(struct client *c, struct msg_command_data *data)
                    659: {
                    660:        struct cmd_ctx   ctx;
                    661:        struct cmd_list *cmdlist = NULL;
                    662:        struct cmd      *cmd;
                    663:        int              argc;
                    664:        char           **argv, *cause;
                    665:
                    666:        ctx.error = server_client_msg_error;
                    667:        ctx.print = server_client_msg_print;
                    668:        ctx.info = server_client_msg_info;
                    669:
                    670:        ctx.msgdata = data;
                    671:        ctx.curclient = NULL;
                    672:
                    673:        ctx.cmdclient = c;
                    674:
                    675:        argc = data->argc;
                    676:        data->argv[(sizeof data->argv) - 1] = '\0';
                    677:        if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
                    678:                server_client_msg_error(&ctx, "command too long");
                    679:                goto error;
                    680:        }
                    681:
                    682:        if (argc == 0) {
                    683:                argc = 1;
                    684:                argv = xcalloc(1, sizeof *argv);
                    685:                *argv = xstrdup("new-session");
                    686:        }
                    687:
                    688:        if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
                    689:                server_client_msg_error(&ctx, "%s", cause);
                    690:                cmd_free_argv(argc, argv);
                    691:                goto error;
                    692:        }
                    693:        cmd_free_argv(argc, argv);
                    694:
                    695:        if (data->pid != -1) {
                    696:                TAILQ_FOREACH(cmd, cmdlist, qentry) {
                    697:                        if (cmd->entry->flags & CMD_CANTNEST) {
                    698:                                server_client_msg_error(&ctx,
                    699:                                    "sessions should be nested with care. "
                    700:                                    "unset $TMUX to force");
                    701:                                goto error;
                    702:                        }
                    703:                }
                    704:        }
                    705:
                    706:        if (cmd_list_exec(cmdlist, &ctx) != 1)
                    707:                server_write_client(c, MSG_EXIT, NULL, 0);
                    708:        cmd_list_free(cmdlist);
                    709:        return;
                    710:
                    711: error:
                    712:        if (cmdlist != NULL)
                    713:                cmd_list_free(cmdlist);
                    714:        server_write_client(c, MSG_EXIT, NULL, 0);
                    715: }
                    716:
                    717: /* Handle identify message. */
                    718: void
                    719: server_client_msg_identify(
                    720:     struct client *c, struct msg_identify_data *data, int fd)
                    721: {
                    722:        c->cwd = NULL;
                    723:        data->cwd[(sizeof data->cwd) - 1] = '\0';
                    724:        if (*data->cwd != '\0')
                    725:                c->cwd = xstrdup(data->cwd);
                    726:
                    727:        data->term[(sizeof data->term) - 1] = '\0';
                    728:        tty_init(&c->tty, fd, data->term);
                    729:        if (data->flags & IDENTIFY_UTF8)
                    730:                c->tty.flags |= TTY_UTF8;
                    731:        if (data->flags & IDENTIFY_256COLOURS)
                    732:                c->tty.term_flags |= TERM_256COLOURS;
                    733:        else if (data->flags & IDENTIFY_88COLOURS)
                    734:                c->tty.term_flags |= TERM_88COLOURS;
1.18      nicm      735:        c->tty.key_callback = server_client_handle_key;
                    736:        c->tty.key_data = c;
1.1       nicm      737:
                    738:        tty_resize(&c->tty);
                    739:
                    740:        c->flags |= CLIENT_TERMINAL;
                    741: }
                    742:
                    743: /* Handle shell message. */
                    744: void
                    745: server_client_msg_shell(struct client *c)
                    746: {
                    747:        struct msg_shell_data    data;
                    748:        const char              *shell;
1.25      nicm      749:
1.1       nicm      750:        shell = options_get_string(&global_s_options, "default-shell");
                    751:
                    752:        if (*shell == '\0' || areshell(shell))
                    753:                shell = _PATH_BSHELL;
                    754:        if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
                    755:                strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
1.25      nicm      756:
1.1       nicm      757:        server_write_client(c, MSG_SHELL, &data, sizeof data);
                    758:        c->flags |= CLIENT_BAD; /* it will die after exec */
                    759: }