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

1.4     ! nicm        1: /* $OpenBSD: server-client.c,v 1.3 2009/10/22 21:01:52 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
                     21: #include <fcntl.h>
                     22: #include <string.h>
1.4     ! nicm       23: #include <time.h>
1.1       nicm       24: #include <paths.h>
                     25: #include <unistd.h>
                     26:
                     27: #include "tmux.h"
                     28:
                     29: void   server_client_handle_data(struct client *);
                     30: void   server_client_check_redraw(struct client *);
                     31: void   server_client_set_title(struct client *);
                     32: void   server_client_check_timers(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);
                     63:
                     64:        if (gettimeofday(&c->tv, NULL) != 0)
                     65:                fatal("gettimeofday failed");
                     66:
                     67:        ARRAY_INIT(&c->prompt_hdata);
                     68:
                     69:        c->tty.fd = -1;
                     70:        c->title = NULL;
                     71:
                     72:        c->session = NULL;
                     73:        c->tty.sx = 80;
                     74:        c->tty.sy = 24;
                     75:
                     76:        screen_init(&c->status, c->tty.sx, 1, 0);
                     77:        job_tree_init(&c->status_jobs);
                     78:
                     79:        c->message_string = NULL;
                     80:
                     81:        c->prompt_string = NULL;
                     82:        c->prompt_buffer = NULL;
                     83:        c->prompt_index = 0;
                     84:
                     85:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     86:                if (ARRAY_ITEM(&clients, i) == NULL) {
                     87:                        ARRAY_SET(&clients, i, c);
                     88:                        return;
                     89:                }
                     90:        }
                     91:        ARRAY_ADD(&clients, c);
                     92:        log_debug("new client %d", fd);
                     93: }
                     94:
                     95: /* Lost a client. */
                     96: void
                     97: server_client_lost(struct client *c)
                     98: {
                     99:        u_int   i;
                    100:
                    101:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    102:                if (ARRAY_ITEM(&clients, i) == c)
                    103:                        ARRAY_SET(&clients, i, NULL);
                    104:        }
                    105:        log_debug("lost client %d", c->ibuf.fd);
                    106:
                    107:        /*
                    108:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    109:         * and tty_free might close an unrelated fd.
                    110:         */
                    111:        if (c->flags & CLIENT_TERMINAL)
                    112:                tty_free(&c->tty);
                    113:
                    114:        screen_free(&c->status);
                    115:        job_tree_free(&c->status_jobs);
                    116:
                    117:        if (c->title != NULL)
                    118:                xfree(c->title);
                    119:
                    120:        if (c->message_string != NULL)
                    121:                xfree(c->message_string);
                    122:
                    123:        if (c->prompt_string != NULL)
                    124:                xfree(c->prompt_string);
                    125:        if (c->prompt_buffer != NULL)
                    126:                xfree(c->prompt_buffer);
                    127:        for (i = 0; i < ARRAY_LENGTH(&c->prompt_hdata); i++)
                    128:                xfree(ARRAY_ITEM(&c->prompt_hdata, i));
                    129:        ARRAY_FREE(&c->prompt_hdata);
                    130:
                    131:        if (c->cwd != NULL)
                    132:                xfree(c->cwd);
                    133:
                    134:        close(c->ibuf.fd);
                    135:        imsg_clear(&c->ibuf);
                    136:
                    137:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                    138:                if (ARRAY_ITEM(&dead_clients, i) == NULL) {
                    139:                        ARRAY_SET(&dead_clients, i, c);
                    140:                        break;
                    141:                }
                    142:        }
                    143:        if (i == ARRAY_LENGTH(&dead_clients))
                    144:                ARRAY_ADD(&dead_clients, c);
                    145:        c->flags |= CLIENT_DEAD;
                    146:
                    147:        recalculate_sizes();
                    148: }
                    149:
                    150: /* Process a single client event. */
                    151: void
                    152: server_client_callback(int fd, int events, void *data)
                    153: {
                    154:        struct client   *c = data;
                    155:
                    156:        if (fd == c->ibuf.fd) {
                    157:                if (events & (POLLERR|POLLNVAL|POLLHUP))
                    158:                        goto client_lost;
                    159:
                    160:                if (events & POLLOUT && msgbuf_write(&c->ibuf.w) < 0)
                    161:                        goto client_lost;
                    162:
                    163:                if (c->flags & CLIENT_BAD) {
                    164:                        if (c->ibuf.w.queued == 0)
                    165:                                goto client_lost;
                    166:                        return;
                    167:                }
                    168:
                    169:                if (events & POLLIN && server_client_msg_dispatch(c) != 0)
                    170:                        goto client_lost;
                    171:        }
                    172:
                    173:        if (c->tty.fd != -1 && fd == c->tty.fd) {
                    174:                if (c->flags & CLIENT_SUSPENDED || c->session == NULL)
                    175:                        return;
                    176:
                    177:                if (buffer_poll(fd, events, c->tty.in, c->tty.out) != 0)
                    178:                        goto client_lost;
                    179:        }
                    180:
                    181:        return;
                    182:
                    183: client_lost:
                    184:        server_client_lost(c);
                    185: }
                    186:
1.2       nicm      187: /* Client functions that need to happen every loop. */
                    188: void
                    189: server_client_loop(void)
                    190: {
                    191:        struct client           *c;
                    192:        struct window           *w;
                    193:        struct window_pane      *wp;
                    194:        u_int                    i;
                    195:
                    196:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    197:                c = ARRAY_ITEM(&clients, i);
                    198:                if (c == NULL || c->session == NULL)
                    199:                        continue;
                    200:
1.3       nicm      201:                server_client_handle_data(c);
                    202:
1.2       nicm      203:                server_client_check_timers(c);
                    204:                server_client_check_redraw(c);
                    205:        }
                    206:
                    207:        /*
                    208:         * Any windows will have been redrawn as part of clients, so clear
                    209:         * their flags now.
                    210:         */
                    211:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    212:                w = ARRAY_ITEM(&windows, i);
                    213:                if (w == NULL)
                    214:                        continue;
                    215:
                    216:                w->flags &= ~WINDOW_REDRAW;
                    217:                TAILQ_FOREACH(wp, &w->panes, entry)
                    218:                        wp->flags &= ~PANE_REDRAW;
                    219:        }
                    220: }
                    221:
                    222: /* Handle data input or output from client. */
1.1       nicm      223: void
                    224: server_client_handle_data(struct client *c)
                    225: {
                    226:        struct window           *w;
                    227:        struct window_pane      *wp;
                    228:        struct screen           *s;
                    229:        struct options          *oo;
                    230:        struct timeval           tv;
                    231:        struct key_binding      *bd;
                    232:        struct keylist          *keylist;
                    233:        struct mouse_event       mouse;
                    234:        int                      key, status, xtimeout, mode, isprefix;
                    235:        u_int                    i;
                    236:
                    237:        xtimeout = options_get_number(&c->session->options, "repeat-time");
                    238:        if (xtimeout != 0 && c->flags & CLIENT_REPEAT) {
                    239:                if (gettimeofday(&tv, NULL) != 0)
                    240:                        fatal("gettimeofday failed");
                    241:                if (timercmp(&tv, &c->repeat_timer, >))
                    242:                        c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
                    243:        }
                    244:
                    245:        /* Process keys. */
                    246:        keylist = options_get_data(&c->session->options, "prefix");
                    247:        while (tty_keys_next(&c->tty, &key, &mouse) == 0) {
                    248:                if (c->session == NULL)
                    249:                        return;
                    250:
                    251:                c->session->activity = time(NULL);
                    252:                w = c->session->curw->window;
                    253:                wp = w->active; /* could die */
                    254:                oo = &c->session->options;
                    255:
                    256:                /* Special case: number keys jump to pane in identify mode. */
                    257:                if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
                    258:                        wp = window_pane_at_index(w, key - '0');
                    259:                        if (wp != NULL && window_pane_visible(wp))
                    260:                                window_set_active_pane(w, wp);
                    261:                        server_clear_identify(c);
                    262:                        continue;
                    263:                }
                    264:
                    265:                status_message_clear(c);
                    266:                server_clear_identify(c);
                    267:                if (c->prompt_string != NULL) {
                    268:                        status_prompt_key(c, key);
                    269:                        continue;
                    270:                }
                    271:
                    272:                /* Check for mouse keys. */
                    273:                if (key == KEYC_MOUSE) {
                    274:                        if (options_get_number(oo, "mouse-select-pane")) {
                    275:                                window_set_active_at(w, mouse.x, mouse.y);
                    276:                                wp = w->active;
                    277:                        }
                    278:                        window_pane_mouse(wp, c, &mouse);
                    279:                        continue;
                    280:                }
                    281:
                    282:                /* Is this a prefix key? */
                    283:                isprefix = 0;
                    284:                for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
                    285:                        if (key == ARRAY_ITEM(keylist, i)) {
                    286:                                isprefix = 1;
                    287:                                break;
                    288:                        }
                    289:                }
                    290:
                    291:                /* No previous prefix key. */
                    292:                if (!(c->flags & CLIENT_PREFIX)) {
                    293:                        if (isprefix)
                    294:                                c->flags |= CLIENT_PREFIX;
                    295:                        else {
                    296:                                /* Try as a non-prefix key binding. */
                    297:                                if ((bd = key_bindings_lookup(key)) == NULL)
                    298:                                        window_pane_key(wp, c, key);
                    299:                                else
                    300:                                        key_bindings_dispatch(bd, c);
                    301:                        }
                    302:                        continue;
                    303:                }
                    304:
                    305:                /* Prefix key already pressed. Reset prefix and lookup key. */
                    306:                c->flags &= ~CLIENT_PREFIX;
                    307:                if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
                    308:                        /* If repeating, treat this as a key, else ignore. */
                    309:                        if (c->flags & CLIENT_REPEAT) {
                    310:                                c->flags &= ~CLIENT_REPEAT;
                    311:                                if (isprefix)
                    312:                                        c->flags |= CLIENT_PREFIX;
                    313:                                else
                    314:                                        window_pane_key(wp, c, key);
                    315:                        }
                    316:                        continue;
                    317:                }
                    318:
                    319:                /* If already repeating, but this key can't repeat, skip it. */
                    320:                if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
                    321:                        c->flags &= ~CLIENT_REPEAT;
                    322:                        if (isprefix)
                    323:                                c->flags |= CLIENT_PREFIX;
                    324:                        else
                    325:                                window_pane_key(wp, c, key);
                    326:                        continue;
                    327:                }
                    328:
                    329:                /* If this key can repeat, reset the repeat flags and timer. */
                    330:                if (xtimeout != 0 && bd->can_repeat) {
                    331:                        c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
                    332:
                    333:                        tv.tv_sec = xtimeout / 1000;
                    334:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                    335:                        if (gettimeofday(&c->repeat_timer, NULL) != 0)
                    336:                                fatal("gettimeofday failed");
                    337:                        timeradd(&c->repeat_timer, &tv, &c->repeat_timer);
                    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);
                    390:
                    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:
                    491:         if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
                    492:                 return (-1);
                    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;
                    550:                        tty_start_tty(&c->tty);
                    551:                        server_redraw_client(c);
                    552:                        recalculate_sizes();
                    553:                        if (c->session != NULL)
                    554:                                c->session->activity = time(NULL);
                    555:                        break;
                    556:                case MSG_ENVIRON:
                    557:                        if (datalen != sizeof environdata)
                    558:                                fatalx("bad MSG_ENVIRON size");
                    559:                        memcpy(&environdata, imsg.data, sizeof environdata);
                    560:
                    561:                        environdata.var[(sizeof environdata.var) - 1] = '\0';
                    562:                        if (strchr(environdata.var, '=') != NULL)
                    563:                                environ_put(&c->environ, environdata.var);
                    564:                        break;
                    565:                case MSG_SHELL:
                    566:                        if (datalen != 0)
                    567:                                fatalx("bad MSG_SHELL size");
                    568:
                    569:                        server_client_msg_shell(c);
                    570:                        break;
                    571:                default:
                    572:                        fatalx("unexpected message");
                    573:                }
                    574:
                    575:                imsg_free(&imsg);
                    576:        }
                    577: }
                    578:
                    579: /* Callback to send error message to client. */
                    580: void printflike2
                    581: server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
                    582: {
                    583:        struct msg_print_data   data;
                    584:        va_list                 ap;
                    585:
                    586:        va_start(ap, fmt);
                    587:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
                    588:        va_end(ap);
                    589:
                    590:        server_write_client(ctx->cmdclient, MSG_ERROR, &data, sizeof data);
                    591: }
                    592:
                    593: /* Callback to send print message to client. */
                    594: void printflike2
                    595: server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
                    596: {
                    597:        struct msg_print_data   data;
                    598:        va_list                 ap;
                    599:
                    600:        va_start(ap, fmt);
                    601:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
                    602:        va_end(ap);
                    603:
                    604:        server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
                    605: }
                    606:
                    607: /* Callback to send print message to client, if not quiet. */
                    608: void printflike2
                    609: server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
                    610: {
                    611:        struct msg_print_data   data;
                    612:        va_list                 ap;
                    613:
                    614:        if (be_quiet)
                    615:                return;
                    616:
                    617:        va_start(ap, fmt);
                    618:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
                    619:        va_end(ap);
                    620:
                    621:        server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
                    622: }
                    623:
                    624: /* Handle command message. */
                    625: void
                    626: server_client_msg_command(struct client *c, struct msg_command_data *data)
                    627: {
                    628:        struct cmd_ctx   ctx;
                    629:        struct cmd_list *cmdlist = NULL;
                    630:        struct cmd      *cmd;
                    631:        int              argc;
                    632:        char           **argv, *cause;
                    633:
                    634:        if (c->session != NULL)
                    635:                c->session->activity = time(NULL);
                    636:
                    637:        ctx.error = server_client_msg_error;
                    638:        ctx.print = server_client_msg_print;
                    639:        ctx.info = server_client_msg_info;
                    640:
                    641:        ctx.msgdata = data;
                    642:        ctx.curclient = NULL;
                    643:
                    644:        ctx.cmdclient = c;
                    645:
                    646:        argc = data->argc;
                    647:        data->argv[(sizeof data->argv) - 1] = '\0';
                    648:        if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
                    649:                server_client_msg_error(&ctx, "command too long");
                    650:                goto error;
                    651:        }
                    652:
                    653:        if (argc == 0) {
                    654:                argc = 1;
                    655:                argv = xcalloc(1, sizeof *argv);
                    656:                *argv = xstrdup("new-session");
                    657:        }
                    658:
                    659:        if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
                    660:                server_client_msg_error(&ctx, "%s", cause);
                    661:                cmd_free_argv(argc, argv);
                    662:                goto error;
                    663:        }
                    664:        cmd_free_argv(argc, argv);
                    665:
                    666:        if (data->pid != -1) {
                    667:                TAILQ_FOREACH(cmd, cmdlist, qentry) {
                    668:                        if (cmd->entry->flags & CMD_CANTNEST) {
                    669:                                server_client_msg_error(&ctx,
                    670:                                    "sessions should be nested with care. "
                    671:                                    "unset $TMUX to force");
                    672:                                goto error;
                    673:                        }
                    674:                }
                    675:        }
                    676:
                    677:        if (cmd_list_exec(cmdlist, &ctx) != 1)
                    678:                server_write_client(c, MSG_EXIT, NULL, 0);
                    679:        cmd_list_free(cmdlist);
                    680:        return;
                    681:
                    682: error:
                    683:        if (cmdlist != NULL)
                    684:                cmd_list_free(cmdlist);
                    685:        server_write_client(c, MSG_EXIT, NULL, 0);
                    686: }
                    687:
                    688: /* Handle identify message. */
                    689: void
                    690: server_client_msg_identify(
                    691:     struct client *c, struct msg_identify_data *data, int fd)
                    692: {
                    693:        c->cwd = NULL;
                    694:        data->cwd[(sizeof data->cwd) - 1] = '\0';
                    695:        if (*data->cwd != '\0')
                    696:                c->cwd = xstrdup(data->cwd);
                    697:
                    698:        data->term[(sizeof data->term) - 1] = '\0';
                    699:        tty_init(&c->tty, fd, data->term);
                    700:        if (data->flags & IDENTIFY_UTF8)
                    701:                c->tty.flags |= TTY_UTF8;
                    702:        if (data->flags & IDENTIFY_256COLOURS)
                    703:                c->tty.term_flags |= TERM_256COLOURS;
                    704:        else if (data->flags & IDENTIFY_88COLOURS)
                    705:                c->tty.term_flags |= TERM_88COLOURS;
                    706:        if (data->flags & IDENTIFY_HASDEFAULTS)
                    707:                c->tty.term_flags |= TERM_HASDEFAULTS;
                    708:
                    709:        tty_resize(&c->tty);
                    710:
                    711:        c->flags |= CLIENT_TERMINAL;
                    712: }
                    713:
                    714: /* Handle shell message. */
                    715: void
                    716: server_client_msg_shell(struct client *c)
                    717: {
                    718:        struct msg_shell_data    data;
                    719:        const char              *shell;
                    720:
                    721:        shell = options_get_string(&global_s_options, "default-shell");
                    722:
                    723:        if (*shell == '\0' || areshell(shell))
                    724:                shell = _PATH_BSHELL;
                    725:        if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
                    726:                strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
                    727:
                    728:        server_write_client(c, MSG_SHELL, &data, sizeof data);
                    729:        c->flags |= CLIENT_BAD; /* it will die after exec */
                    730: }