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

1.7     ! nicm        1: /* $OpenBSD: server-client.c,v 1.6 2009/10/25 22:00:15 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;
1.7     ! nicm      155:
        !           156:        if (c->flags & CLIENT_DEAD)
        !           157:                return;
1.1       nicm      158:
                    159:        if (fd == c->ibuf.fd) {
                    160:                if (events & (POLLERR|POLLNVAL|POLLHUP))
                    161:                        goto client_lost;
                    162:
                    163:                if (events & POLLOUT && msgbuf_write(&c->ibuf.w) < 0)
                    164:                        goto client_lost;
                    165:
                    166:                if (c->flags & CLIENT_BAD) {
                    167:                        if (c->ibuf.w.queued == 0)
                    168:                                goto client_lost;
                    169:                        return;
                    170:                }
                    171:
                    172:                if (events & POLLIN && server_client_msg_dispatch(c) != 0)
                    173:                        goto client_lost;
                    174:        }
                    175:
                    176:        if (c->tty.fd != -1 && fd == c->tty.fd) {
                    177:                if (c->flags & CLIENT_SUSPENDED || c->session == NULL)
                    178:                        return;
                    179:
                    180:                if (buffer_poll(fd, events, c->tty.in, c->tty.out) != 0)
                    181:                        goto client_lost;
                    182:        }
                    183:
                    184:        return;
                    185:
                    186: client_lost:
                    187:        server_client_lost(c);
                    188: }
                    189:
1.2       nicm      190: /* Client functions that need to happen every loop. */
                    191: void
                    192: server_client_loop(void)
                    193: {
                    194:        struct client           *c;
                    195:        struct window           *w;
                    196:        struct window_pane      *wp;
                    197:        u_int                    i;
                    198:
                    199:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    200:                c = ARRAY_ITEM(&clients, i);
                    201:                if (c == NULL || c->session == NULL)
                    202:                        continue;
                    203:
1.3       nicm      204:                server_client_handle_data(c);
1.6       nicm      205:                if (c->session != NULL) {
                    206:                        server_client_check_timers(c);
                    207:                        server_client_check_redraw(c);
                    208:                }
1.2       nicm      209:        }
                    210:
                    211:        /*
                    212:         * Any windows will have been redrawn as part of clients, so clear
                    213:         * their flags now.
                    214:         */
                    215:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    216:                w = ARRAY_ITEM(&windows, i);
                    217:                if (w == NULL)
                    218:                        continue;
                    219:
                    220:                w->flags &= ~WINDOW_REDRAW;
                    221:                TAILQ_FOREACH(wp, &w->panes, entry)
                    222:                        wp->flags &= ~PANE_REDRAW;
                    223:        }
                    224: }
                    225:
                    226: /* Handle data input or output from client. */
1.1       nicm      227: void
                    228: server_client_handle_data(struct client *c)
                    229: {
                    230:        struct window           *w;
                    231:        struct window_pane      *wp;
                    232:        struct screen           *s;
                    233:        struct options          *oo;
                    234:        struct timeval           tv;
                    235:        struct key_binding      *bd;
                    236:        struct keylist          *keylist;
                    237:        struct mouse_event       mouse;
                    238:        int                      key, status, xtimeout, mode, isprefix;
                    239:        u_int                    i;
                    240:
                    241:        xtimeout = options_get_number(&c->session->options, "repeat-time");
                    242:        if (xtimeout != 0 && c->flags & CLIENT_REPEAT) {
                    243:                if (gettimeofday(&tv, NULL) != 0)
                    244:                        fatal("gettimeofday failed");
                    245:                if (timercmp(&tv, &c->repeat_timer, >))
                    246:                        c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
                    247:        }
                    248:
                    249:        /* Process keys. */
                    250:        keylist = options_get_data(&c->session->options, "prefix");
                    251:        while (tty_keys_next(&c->tty, &key, &mouse) == 0) {
                    252:                if (c->session == NULL)
                    253:                        return;
                    254:
                    255:                c->session->activity = time(NULL);
                    256:                w = c->session->curw->window;
                    257:                wp = w->active; /* could die */
                    258:                oo = &c->session->options;
                    259:
                    260:                /* Special case: number keys jump to pane in identify mode. */
                    261:                if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
                    262:                        wp = window_pane_at_index(w, key - '0');
                    263:                        if (wp != NULL && window_pane_visible(wp))
                    264:                                window_set_active_pane(w, wp);
                    265:                        server_clear_identify(c);
                    266:                        continue;
                    267:                }
                    268:
                    269:                status_message_clear(c);
                    270:                server_clear_identify(c);
                    271:                if (c->prompt_string != NULL) {
                    272:                        status_prompt_key(c, key);
                    273:                        continue;
                    274:                }
                    275:
                    276:                /* Check for mouse keys. */
                    277:                if (key == KEYC_MOUSE) {
                    278:                        if (options_get_number(oo, "mouse-select-pane")) {
                    279:                                window_set_active_at(w, mouse.x, mouse.y);
                    280:                                wp = w->active;
                    281:                        }
                    282:                        window_pane_mouse(wp, c, &mouse);
                    283:                        continue;
                    284:                }
                    285:
                    286:                /* Is this a prefix key? */
                    287:                isprefix = 0;
                    288:                for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
                    289:                        if (key == ARRAY_ITEM(keylist, i)) {
                    290:                                isprefix = 1;
                    291:                                break;
                    292:                        }
                    293:                }
                    294:
                    295:                /* No previous prefix key. */
                    296:                if (!(c->flags & CLIENT_PREFIX)) {
                    297:                        if (isprefix)
                    298:                                c->flags |= CLIENT_PREFIX;
                    299:                        else {
                    300:                                /* Try as a non-prefix key binding. */
                    301:                                if ((bd = key_bindings_lookup(key)) == NULL)
                    302:                                        window_pane_key(wp, c, key);
                    303:                                else
                    304:                                        key_bindings_dispatch(bd, c);
                    305:                        }
                    306:                        continue;
                    307:                }
                    308:
                    309:                /* Prefix key already pressed. Reset prefix and lookup key. */
                    310:                c->flags &= ~CLIENT_PREFIX;
                    311:                if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
                    312:                        /* If repeating, treat this as a key, else ignore. */
                    313:                        if (c->flags & CLIENT_REPEAT) {
                    314:                                c->flags &= ~CLIENT_REPEAT;
                    315:                                if (isprefix)
                    316:                                        c->flags |= CLIENT_PREFIX;
                    317:                                else
                    318:                                        window_pane_key(wp, c, key);
                    319:                        }
                    320:                        continue;
                    321:                }
                    322:
                    323:                /* If already repeating, but this key can't repeat, skip it. */
                    324:                if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
                    325:                        c->flags &= ~CLIENT_REPEAT;
                    326:                        if (isprefix)
                    327:                                c->flags |= CLIENT_PREFIX;
                    328:                        else
                    329:                                window_pane_key(wp, c, key);
                    330:                        continue;
                    331:                }
                    332:
                    333:                /* If this key can repeat, reset the repeat flags and timer. */
                    334:                if (xtimeout != 0 && bd->can_repeat) {
                    335:                        c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
                    336:
                    337:                        tv.tv_sec = xtimeout / 1000;
                    338:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                    339:                        if (gettimeofday(&c->repeat_timer, NULL) != 0)
                    340:                                fatal("gettimeofday failed");
                    341:                        timeradd(&c->repeat_timer, &tv, &c->repeat_timer);
                    342:                }
                    343:
                    344:                /* Dispatch the command. */
                    345:                key_bindings_dispatch(bd, c);
                    346:        }
                    347:        if (c->session == NULL)
                    348:                return;
                    349:        w = c->session->curw->window;
                    350:        wp = w->active;
                    351:        oo = &c->session->options;
                    352:        s = wp->screen;
                    353:
                    354:        /*
                    355:         * Update cursor position and mode settings. The scroll region and
                    356:         * attributes are cleared across poll(2) as this is the most likely
                    357:         * time a user may interrupt tmux, for example with ~^Z in ssh(1). This
                    358:         * is a compromise between excessive resets and likelihood of an
                    359:         * interrupt.
                    360:         *
                    361:         * tty_region/tty_reset/tty_update_mode already take care of not
                    362:         * resetting things that are already in their default state.
                    363:         */
                    364:        tty_region(&c->tty, 0, c->tty.sy - 1);
                    365:
                    366:        status = options_get_number(oo, "status");
                    367:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                    368:                tty_cursor(&c->tty, 0, 0);
                    369:        else
                    370:                tty_cursor(&c->tty, wp->xoff + s->cx, wp->yoff + s->cy);
                    371:
                    372:        mode = s->mode;
                    373:        if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
                    374:            options_get_number(oo, "mouse-select-pane"))
                    375:                mode |= MODE_MOUSE;
                    376:        tty_update_mode(&c->tty, mode);
                    377:        tty_reset(&c->tty);
                    378: }
                    379:
                    380: /* Check for client redraws. */
                    381: void
                    382: server_client_check_redraw(struct client *c)
                    383: {
                    384:        struct session          *s = c->session;
                    385:        struct window_pane      *wp;
                    386:        int                      flags, redraw;
                    387:
                    388:        flags = c->tty.flags & TTY_FREEZE;
                    389:        c->tty.flags &= ~TTY_FREEZE;
                    390:
                    391:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
                    392:                if (options_get_number(&s->options, "set-titles"))
                    393:                        server_client_set_title(c);
                    394:
                    395:                if (c->message_string != NULL)
                    396:                        redraw = status_message_redraw(c);
                    397:                else if (c->prompt_string != NULL)
                    398:                        redraw = status_prompt_redraw(c);
                    399:                else
                    400:                        redraw = status_redraw(c);
                    401:                if (!redraw)
                    402:                        c->flags &= ~CLIENT_STATUS;
                    403:        }
                    404:
                    405:        if (c->flags & CLIENT_REDRAW) {
                    406:                screen_redraw_screen(c, 0);
                    407:                c->flags &= ~CLIENT_STATUS;
                    408:        } else {
                    409:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
                    410:                        if (wp->flags & PANE_REDRAW)
                    411:                                screen_redraw_pane(c, wp);
                    412:                }
                    413:        }
                    414:
                    415:        if (c->flags & CLIENT_STATUS)
                    416:                screen_redraw_screen(c, 1);
                    417:
                    418:        c->tty.flags |= flags;
                    419:
                    420:        c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS);
                    421: }
                    422:
                    423: /* Set client title. */
                    424: void
                    425: server_client_set_title(struct client *c)
                    426: {
                    427:        struct session  *s = c->session;
                    428:        const char      *template;
                    429:        char            *title;
                    430:
                    431:        template = options_get_string(&s->options, "set-titles-string");
                    432:
                    433:        title = status_replace(c, template, time(NULL));
                    434:        if (c->title == NULL || strcmp(title, c->title) != 0) {
                    435:                if (c->title != NULL)
                    436:                        xfree(c->title);
                    437:                c->title = xstrdup(title);
                    438:                tty_set_title(&c->tty, c->title);
                    439:        }
                    440:        xfree(title);
                    441: }
                    442:
                    443: /* Check client timers. */
                    444: void
                    445: server_client_check_timers(struct client *c)
                    446: {
                    447:        struct session  *s = c->session;
                    448:        struct job      *job;
                    449:        struct timeval   tv;
                    450:        u_int            interval;
                    451:
                    452:        if (gettimeofday(&tv, NULL) != 0)
                    453:                fatal("gettimeofday failed");
                    454:
                    455:        if (c->flags & CLIENT_IDENTIFY && timercmp(&tv, &c->identify_timer, >))
                    456:                server_clear_identify(c);
                    457:
                    458:        if (c->message_string != NULL && timercmp(&tv, &c->message_timer, >))
                    459:                status_message_clear(c);
                    460:
                    461:        if (c->message_string != NULL || c->prompt_string != NULL) {
                    462:                /*
                    463:                 * Don't need timed redraw for messages/prompts so bail now.
                    464:                 * The status timer isn't reset when they are redrawn anyway.
                    465:                 */
                    466:                return;
                    467:
                    468:        }
                    469:        if (!options_get_number(&s->options, "status"))
                    470:                return;
                    471:
                    472:        /* Check timer; resolution is only a second so don't be too clever. */
                    473:        interval = options_get_number(&s->options, "status-interval");
                    474:        if (interval == 0)
                    475:                return;
                    476:        if (tv.tv_sec < c->status_timer.tv_sec ||
                    477:            ((u_int) tv.tv_sec) - c->status_timer.tv_sec >= interval) {
                    478:                /* Run the jobs for this client and schedule for redraw. */
                    479:                RB_FOREACH(job, jobs, &c->status_jobs)
                    480:                        job_run(job);
                    481:                c->flags |= CLIENT_STATUS;
                    482:        }
                    483: }
                    484:
                    485: /* Dispatch message from client. */
                    486: int
                    487: server_client_msg_dispatch(struct client *c)
                    488: {
                    489:        struct imsg              imsg;
                    490:        struct msg_command_data  commanddata;
                    491:        struct msg_identify_data identifydata;
                    492:        struct msg_environ_data  environdata;
                    493:        ssize_t                  n, datalen;
                    494:
                    495:         if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
                    496:                 return (-1);
                    497:
                    498:        for (;;) {
                    499:                if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
                    500:                        return (-1);
                    501:                if (n == 0)
                    502:                        return (0);
                    503:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
                    504:
                    505:                if (imsg.hdr.peerid != PROTOCOL_VERSION) {
                    506:                        server_write_client(c, MSG_VERSION, NULL, 0);
                    507:                        c->flags |= CLIENT_BAD;
                    508:                        imsg_free(&imsg);
                    509:                        continue;
                    510:                }
                    511:
                    512:                log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
                    513:                switch (imsg.hdr.type) {
                    514:                case MSG_COMMAND:
                    515:                        if (datalen != sizeof commanddata)
                    516:                                fatalx("bad MSG_COMMAND size");
                    517:                        memcpy(&commanddata, imsg.data, sizeof commanddata);
                    518:
                    519:                        server_client_msg_command(c, &commanddata);
                    520:                        break;
                    521:                case MSG_IDENTIFY:
                    522:                        if (datalen != sizeof identifydata)
                    523:                                fatalx("bad MSG_IDENTIFY size");
                    524:                        if (imsg.fd == -1)
                    525:                                fatalx("MSG_IDENTIFY missing fd");
                    526:                        memcpy(&identifydata, imsg.data, sizeof identifydata);
                    527:
                    528:                        server_client_msg_identify(c, &identifydata, imsg.fd);
                    529:                        break;
                    530:                case MSG_RESIZE:
                    531:                        if (datalen != 0)
                    532:                                fatalx("bad MSG_RESIZE size");
                    533:
                    534:                        tty_resize(&c->tty);
                    535:                        recalculate_sizes();
                    536:                        server_redraw_client(c);
                    537:                        break;
                    538:                case MSG_EXITING:
                    539:                        if (datalen != 0)
                    540:                                fatalx("bad MSG_EXITING size");
                    541:
                    542:                        c->session = NULL;
                    543:                        tty_close(&c->tty);
                    544:                        server_write_client(c, MSG_EXITED, NULL, 0);
                    545:                        break;
                    546:                case MSG_WAKEUP:
                    547:                case MSG_UNLOCK:
                    548:                        if (datalen != 0)
                    549:                                fatalx("bad MSG_WAKEUP size");
                    550:
                    551:                        if (!(c->flags & CLIENT_SUSPENDED))
                    552:                                break;
                    553:                        c->flags &= ~CLIENT_SUSPENDED;
                    554:                        tty_start_tty(&c->tty);
                    555:                        server_redraw_client(c);
                    556:                        recalculate_sizes();
                    557:                        if (c->session != NULL)
                    558:                                c->session->activity = time(NULL);
                    559:                        break;
                    560:                case MSG_ENVIRON:
                    561:                        if (datalen != sizeof environdata)
                    562:                                fatalx("bad MSG_ENVIRON size");
                    563:                        memcpy(&environdata, imsg.data, sizeof environdata);
                    564:
                    565:                        environdata.var[(sizeof environdata.var) - 1] = '\0';
                    566:                        if (strchr(environdata.var, '=') != NULL)
                    567:                                environ_put(&c->environ, environdata.var);
                    568:                        break;
                    569:                case MSG_SHELL:
                    570:                        if (datalen != 0)
                    571:                                fatalx("bad MSG_SHELL size");
                    572:
                    573:                        server_client_msg_shell(c);
                    574:                        break;
                    575:                default:
                    576:                        fatalx("unexpected message");
                    577:                }
                    578:
                    579:                imsg_free(&imsg);
                    580:        }
                    581: }
                    582:
                    583: /* Callback to send error message to client. */
                    584: void printflike2
                    585: server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
                    586: {
                    587:        struct msg_print_data   data;
                    588:        va_list                 ap;
                    589:
                    590:        va_start(ap, fmt);
                    591:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
                    592:        va_end(ap);
                    593:
                    594:        server_write_client(ctx->cmdclient, MSG_ERROR, &data, sizeof data);
                    595: }
                    596:
                    597: /* Callback to send print message to client. */
                    598: void printflike2
                    599: server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
                    600: {
                    601:        struct msg_print_data   data;
                    602:        va_list                 ap;
                    603:
                    604:        va_start(ap, fmt);
                    605:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
                    606:        va_end(ap);
                    607:
                    608:        server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
                    609: }
                    610:
                    611: /* Callback to send print message to client, if not quiet. */
                    612: void printflike2
                    613: server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
                    614: {
                    615:        struct msg_print_data   data;
                    616:        va_list                 ap;
                    617:
                    618:        if (be_quiet)
                    619:                return;
                    620:
                    621:        va_start(ap, fmt);
                    622:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
                    623:        va_end(ap);
                    624:
                    625:        server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
                    626: }
                    627:
                    628: /* Handle command message. */
                    629: void
                    630: server_client_msg_command(struct client *c, struct msg_command_data *data)
                    631: {
                    632:        struct cmd_ctx   ctx;
                    633:        struct cmd_list *cmdlist = NULL;
                    634:        struct cmd      *cmd;
                    635:        int              argc;
                    636:        char           **argv, *cause;
                    637:
                    638:        if (c->session != NULL)
                    639:                c->session->activity = time(NULL);
                    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: }