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

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