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

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