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

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