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

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