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

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