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

1.79    ! nicm        1: /* $OpenBSD: server-client.c,v 1.78 2012/07/11 07:10: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:
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>
1.77      nicm       26: #include <stdlib.h>
1.1       nicm       27: #include <unistd.h>
                     28:
                     29: #include "tmux.h"
                     30:
1.74      nicm       31: void   server_client_check_mouse(struct client *, struct window_pane *,
                     32:            struct mouse_event *);
1.17      nicm       33: void   server_client_repeat_timer(int, short, void *);
1.36      nicm       34: void   server_client_check_exit(struct client *);
1.1       nicm       35: void   server_client_check_redraw(struct client *);
                     36: void   server_client_set_title(struct client *);
1.18      nicm       37: void   server_client_reset_state(struct client *);
1.1       nicm       38:
                     39: int    server_client_msg_dispatch(struct client *);
                     40: void   server_client_msg_command(struct client *, struct msg_command_data *);
                     41: void   server_client_msg_identify(
1.25      nicm       42:            struct client *, struct msg_identify_data *, int);
1.1       nicm       43: void   server_client_msg_shell(struct client *);
                     44:
                     45: void printflike2 server_client_msg_error(struct cmd_ctx *, const char *, ...);
                     46: void printflike2 server_client_msg_print(struct cmd_ctx *, const char *, ...);
                     47: void printflike2 server_client_msg_info(struct cmd_ctx *, const char *, ...);
                     48:
                     49: /* Create a new client. */
                     50: void
                     51: server_client_create(int fd)
                     52: {
                     53:        struct client   *c;
                     54:        u_int            i;
                     55:
1.49      nicm       56:        setblocking(fd, 0);
1.1       nicm       57:
                     58:        c = xcalloc(1, sizeof *c);
                     59:        c->references = 0;
                     60:        imsg_init(&c->ibuf, fd);
1.14      nicm       61:        server_update_event(c);
1.25      nicm       62:
1.10      nicm       63:        if (gettimeofday(&c->creation_time, NULL) != 0)
1.1       nicm       64:                fatal("gettimeofday failed");
1.11      nicm       65:        memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.1       nicm       66:
1.73      nicm       67:        c->stdin_data = evbuffer_new ();
                     68:        c->stdout_data = evbuffer_new ();
                     69:        c->stderr_data = evbuffer_new ();
1.33      nicm       70:
1.1       nicm       71:        c->tty.fd = -1;
                     72:        c->title = NULL;
                     73:
                     74:        c->session = NULL;
1.45      nicm       75:        c->last_session = NULL;
1.1       nicm       76:        c->tty.sx = 80;
                     77:        c->tty.sy = 24;
                     78:
                     79:        screen_init(&c->status, c->tty.sx, 1, 0);
1.51      nicm       80:        RB_INIT(&c->status_new);
                     81:        RB_INIT(&c->status_old);
1.1       nicm       82:
                     83:        c->message_string = NULL;
1.21      nicm       84:        ARRAY_INIT(&c->message_log);
1.1       nicm       85:
                     86:        c->prompt_string = NULL;
                     87:        c->prompt_buffer = NULL;
                     88:        c->prompt_index = 0;
                     89:
1.57      nicm       90:        c->last_mouse.b = MOUSE_UP;
                     91:        c->last_mouse.x = c->last_mouse.y = -1;
                     92:
1.17      nicm       93:        evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
                     94:
1.1       nicm       95:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     96:                if (ARRAY_ITEM(&clients, i) == NULL) {
                     97:                        ARRAY_SET(&clients, i, c);
                     98:                        return;
                     99:                }
                    100:        }
                    101:        ARRAY_ADD(&clients, c);
                    102:        log_debug("new client %d", fd);
1.72      nicm      103: }
                    104:
                    105: /* Open client terminal if needed. */
                    106: int
                    107: server_client_open(struct client *c, struct session *s, char **cause)
                    108: {
                    109:        struct options  *oo = s != NULL ? &s->options : &global_s_options;
                    110:        char            *overrides;
                    111:
1.75      nicm      112:        if (c->flags & CLIENT_CONTROL)
                    113:                return (0);
                    114:
1.72      nicm      115:        if (!(c->flags & CLIENT_TERMINAL)) {
                    116:                *cause = xstrdup ("not a terminal");
                    117:                return (-1);
                    118:        }
                    119:
                    120:        overrides = options_get_string(oo, "terminal-overrides");
                    121:        if (tty_open(&c->tty, overrides, cause) != 0)
                    122:                return (-1);
                    123:
                    124:        return (0);
1.1       nicm      125: }
                    126:
                    127: /* Lost a client. */
                    128: void
                    129: server_client_lost(struct client *c)
                    130: {
1.21      nicm      131:        struct message_entry    *msg;
                    132:        u_int                    i;
1.1       nicm      133:
                    134:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    135:                if (ARRAY_ITEM(&clients, i) == c)
                    136:                        ARRAY_SET(&clients, i, NULL);
                    137:        }
                    138:        log_debug("lost client %d", c->ibuf.fd);
                    139:
                    140:        /*
                    141:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    142:         * and tty_free might close an unrelated fd.
                    143:         */
                    144:        if (c->flags & CLIENT_TERMINAL)
                    145:                tty_free(&c->tty);
                    146:
1.73      nicm      147:        evbuffer_free (c->stdin_data);
                    148:        evbuffer_free (c->stdout_data);
                    149:        evbuffer_free (c->stderr_data);
1.33      nicm      150:
1.51      nicm      151:        status_free_jobs(&c->status_new);
                    152:        status_free_jobs(&c->status_old);
1.1       nicm      153:        screen_free(&c->status);
                    154:
1.77      nicm      155:        free(c->title);
1.1       nicm      156:
1.17      nicm      157:        evtimer_del(&c->repeat_timer);
                    158:
1.69      nicm      159:        if (event_initialized(&c->identify_timer))
                    160:                evtimer_del(&c->identify_timer);
1.15      nicm      161:
1.77      nicm      162:        free(c->message_string);
1.69      nicm      163:        if (event_initialized (&c->message_timer))
                    164:                evtimer_del(&c->message_timer);
1.21      nicm      165:        for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
                    166:                msg = &ARRAY_ITEM(&c->message_log, i);
1.77      nicm      167:                free(msg->msg);
1.21      nicm      168:        }
                    169:        ARRAY_FREE(&c->message_log);
1.1       nicm      170:
1.77      nicm      171:        free(c->prompt_string);
                    172:        free(c->prompt_buffer);
                    173:        free(c->cwd);
1.61      nicm      174:
                    175:        environ_free(&c->environ);
1.1       nicm      176:
                    177:        close(c->ibuf.fd);
                    178:        imsg_clear(&c->ibuf);
1.69      nicm      179:        if (event_initialized(&c->event))
                    180:                event_del(&c->event);
1.13      nicm      181:
1.1       nicm      182:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                    183:                if (ARRAY_ITEM(&dead_clients, i) == NULL) {
                    184:                        ARRAY_SET(&dead_clients, i, c);
                    185:                        break;
                    186:                }
                    187:        }
                    188:        if (i == ARRAY_LENGTH(&dead_clients))
                    189:                ARRAY_ADD(&dead_clients, c);
                    190:        c->flags |= CLIENT_DEAD;
1.71      nicm      191:
                    192:        server_add_accept(0); /* may be more file descriptors now */
1.1       nicm      193:
                    194:        recalculate_sizes();
1.41      nicm      195:        server_check_unattached();
1.19      nicm      196:        server_update_socket();
1.9       nicm      197: }
                    198:
1.1       nicm      199: /* Process a single client event. */
                    200: void
1.12      nicm      201: server_client_callback(int fd, short events, void *data)
1.1       nicm      202: {
                    203:        struct client   *c = data;
1.7       nicm      204:
                    205:        if (c->flags & CLIENT_DEAD)
                    206:                return;
1.1       nicm      207:
                    208:        if (fd == c->ibuf.fd) {
1.12      nicm      209:                if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
1.1       nicm      210:                        goto client_lost;
                    211:
                    212:                if (c->flags & CLIENT_BAD) {
                    213:                        if (c->ibuf.w.queued == 0)
                    214:                                goto client_lost;
                    215:                        return;
                    216:                }
                    217:
1.12      nicm      218:                if (events & EV_READ && server_client_msg_dispatch(c) != 0)
1.1       nicm      219:                        goto client_lost;
                    220:        }
1.14      nicm      221:
1.73      nicm      222:        server_push_stdout(c);
                    223:        server_push_stderr(c);
                    224:
1.25      nicm      225:        server_update_event(c);
1.1       nicm      226:        return;
                    227:
                    228: client_lost:
                    229:        server_client_lost(c);
                    230: }
                    231:
1.16      nicm      232: /* Handle client status timer. */
                    233: void
                    234: server_client_status_timer(void)
                    235: {
                    236:        struct client   *c;
                    237:        struct session  *s;
                    238:        struct timeval   tv;
1.20      nicm      239:        u_int            i;
                    240:        int              interval;
                    241:        time_t           difference;
1.16      nicm      242:
                    243:        if (gettimeofday(&tv, NULL) != 0)
                    244:                fatal("gettimeofday failed");
                    245:
                    246:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    247:                c = ARRAY_ITEM(&clients, i);
                    248:                if (c == NULL || c->session == NULL)
                    249:                        continue;
                    250:                if (c->message_string != NULL || c->prompt_string != NULL) {
                    251:                        /*
                    252:                         * Don't need timed redraw for messages/prompts so bail
                    253:                         * now. The status timer isn't reset when they are
                    254:                         * redrawn anyway.
                    255:                         */
                    256:                        continue;
                    257:                }
                    258:                s = c->session;
                    259:
                    260:                if (!options_get_number(&s->options, "status"))
                    261:                        continue;
                    262:                interval = options_get_number(&s->options, "status-interval");
                    263:
1.20      nicm      264:                difference = tv.tv_sec - c->status_timer.tv_sec;
                    265:                if (difference >= interval) {
1.51      nicm      266:                        status_update_jobs(c);
1.16      nicm      267:                        c->flags |= CLIENT_STATUS;
                    268:                }
                    269:        }
                    270: }
                    271:
1.66      nicm      272: /* Check for mouse keys. */
                    273: void
                    274: server_client_check_mouse(
                    275:     struct client *c, struct window_pane *wp, struct mouse_event *mouse)
                    276: {
                    277:        struct session  *s = c->session;
                    278:        struct options  *oo = &s->options;
                    279:        int              statusat;
                    280:
                    281:        statusat = status_at_line(c);
                    282:
                    283:        /* Is this a window selection click on the status line? */
                    284:        if (statusat != -1 && mouse->y == (u_int)statusat &&
                    285:            options_get_number(oo, "mouse-select-window")) {
                    286:                if (mouse->b == MOUSE_UP && c->last_mouse.b != MOUSE_UP) {
                    287:                        status_set_window_at(c, mouse->x);
1.70      nicm      288:                        recalculate_sizes();
1.66      nicm      289:                        return;
                    290:                }
                    291:                if (mouse->b & MOUSE_45) {
                    292:                        if ((mouse->b & MOUSE_BUTTON) == MOUSE_1) {
                    293:                                session_previous(c->session, 0);
                    294:                                server_redraw_session(s);
1.70      nicm      295:                                recalculate_sizes();
1.66      nicm      296:                        }
                    297:                        if ((mouse->b & MOUSE_BUTTON) == MOUSE_2) {
                    298:                                session_next(c->session, 0);
                    299:                                server_redraw_session(s);
1.70      nicm      300:                                recalculate_sizes();
1.66      nicm      301:                        }
                    302:                        return;
                    303:                }
1.67      nicm      304:                memcpy(&c->last_mouse, mouse, sizeof c->last_mouse);
                    305:                return;
1.66      nicm      306:        }
                    307:
                    308:        /*
                    309:         * Not on status line - adjust mouse position if status line is at the
                    310:         * top and limit if at the bottom. From here on a struct mouse
                    311:         * represents the offset onto the window itself.
                    312:         */
                    313:        if (statusat == 0 &&mouse->y > 0)
                    314:                mouse->y--;
                    315:        else if (statusat > 0 && mouse->y >= (u_int)statusat)
                    316:                mouse->y = statusat - 1;
                    317:
                    318:        /* Is this a pane selection? Allow down only in copy mode. */
                    319:        if (options_get_number(oo, "mouse-select-pane") &&
                    320:            ((!(mouse->b & MOUSE_DRAG) && mouse->b != MOUSE_UP) ||
                    321:            wp->mode != &window_copy_mode)) {
                    322:                window_set_active_at(wp->window, mouse->x, mouse->y);
                    323:                server_redraw_window_borders(wp->window);
                    324:                wp = wp->window->active; /* may have changed */
                    325:        }
                    326:
                    327:        /* Check if trying to resize pane. */
                    328:        if (options_get_number(oo, "mouse-resize-pane"))
                    329:                layout_resize_pane_mouse(c, mouse);
                    330:
                    331:        /* Update last and pass through to client. */
                    332:        memcpy(&c->last_mouse, mouse, sizeof c->last_mouse);
                    333:        window_pane_mouse(wp, c->session, mouse);
                    334: }
                    335:
1.18      nicm      336: /* Handle data key input from client. */
                    337: void
1.74      nicm      338: server_client_handle_key(struct client *c, int key)
1.18      nicm      339: {
                    340:        struct session          *s;
                    341:        struct window           *w;
                    342:        struct window_pane      *wp;
                    343:        struct timeval           tv;
                    344:        struct key_binding      *bd;
                    345:        int                      xtimeout, isprefix;
                    346:
                    347:        /* Check the client is good to accept input. */
                    348:        if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
                    349:                return;
                    350:        if (c->session == NULL)
                    351:                return;
                    352:        s = c->session;
                    353:
                    354:        /* Update the activity timer. */
                    355:        if (gettimeofday(&c->activity_time, NULL) != 0)
                    356:                fatal("gettimeofday failed");
                    357:        memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
                    358:
                    359:        w = c->session->curw->window;
                    360:        wp = w->active;
                    361:
                    362:        /* Special case: number keys jump to pane in identify mode. */
1.25      nicm      363:        if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30      nicm      364:                if (c->flags & CLIENT_READONLY)
                    365:                        return;
1.18      nicm      366:                wp = window_pane_at_index(w, key - '0');
                    367:                if (wp != NULL && window_pane_visible(wp))
                    368:                        window_set_active_pane(w, wp);
                    369:                server_clear_identify(c);
                    370:                return;
                    371:        }
                    372:
                    373:        /* Handle status line. */
1.30      nicm      374:        if (!(c->flags & CLIENT_READONLY)) {
                    375:                status_message_clear(c);
                    376:                server_clear_identify(c);
                    377:        }
1.18      nicm      378:        if (c->prompt_string != NULL) {
1.30      nicm      379:                if (!(c->flags & CLIENT_READONLY))
                    380:                        status_prompt_key(c, key);
1.18      nicm      381:                return;
                    382:        }
                    383:
                    384:        /* Check for mouse keys. */
                    385:        if (key == KEYC_MOUSE) {
1.30      nicm      386:                if (c->flags & CLIENT_READONLY)
                    387:                        return;
1.74      nicm      388:                server_client_check_mouse(c, wp, &c->tty.mouse);
1.18      nicm      389:                return;
                    390:        }
                    391:
                    392:        /* Is this a prefix key? */
1.63      nicm      393:        if (key == options_get_number(&c->session->options, "prefix"))
                    394:                isprefix = 1;
                    395:        else if (key == options_get_number(&c->session->options, "prefix2"))
                    396:                isprefix = 1;
                    397:        else
                    398:                isprefix = 0;
1.18      nicm      399:
                    400:        /* No previous prefix key. */
                    401:        if (!(c->flags & CLIENT_PREFIX)) {
                    402:                if (isprefix)
                    403:                        c->flags |= CLIENT_PREFIX;
                    404:                else {
                    405:                        /* Try as a non-prefix key binding. */
1.30      nicm      406:                        if ((bd = key_bindings_lookup(key)) == NULL) {
                    407:                                if (!(c->flags & CLIENT_READONLY))
1.31      nicm      408:                                        window_pane_key(wp, c->session, key);
1.30      nicm      409:                        } else
1.18      nicm      410:                                key_bindings_dispatch(bd, c);
                    411:                }
                    412:                return;
                    413:        }
                    414:
                    415:        /* Prefix key already pressed. Reset prefix and lookup key. */
                    416:        c->flags &= ~CLIENT_PREFIX;
                    417:        if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
                    418:                /* If repeating, treat this as a key, else ignore. */
                    419:                if (c->flags & CLIENT_REPEAT) {
                    420:                        c->flags &= ~CLIENT_REPEAT;
                    421:                        if (isprefix)
                    422:                                c->flags |= CLIENT_PREFIX;
1.30      nicm      423:                        else if (!(c->flags & CLIENT_READONLY))
1.31      nicm      424:                                window_pane_key(wp, c->session, key);
1.18      nicm      425:                }
                    426:                return;
                    427:        }
                    428:
                    429:        /* If already repeating, but this key can't repeat, skip it. */
                    430:        if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
                    431:                c->flags &= ~CLIENT_REPEAT;
                    432:                if (isprefix)
                    433:                        c->flags |= CLIENT_PREFIX;
1.30      nicm      434:                else if (!(c->flags & CLIENT_READONLY))
1.31      nicm      435:                        window_pane_key(wp, c->session, key);
1.18      nicm      436:                return;
                    437:        }
                    438:
                    439:        /* If this key can repeat, reset the repeat flags and timer. */
                    440:        xtimeout = options_get_number(&c->session->options, "repeat-time");
                    441:        if (xtimeout != 0 && bd->can_repeat) {
                    442:                c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
1.25      nicm      443:
1.18      nicm      444:                tv.tv_sec = xtimeout / 1000;
                    445:                tv.tv_usec = (xtimeout % 1000) * 1000L;
                    446:                evtimer_del(&c->repeat_timer);
                    447:                evtimer_add(&c->repeat_timer, &tv);
                    448:        }
                    449:
                    450:        /* Dispatch the command. */
                    451:        key_bindings_dispatch(bd, c);
                    452: }
                    453:
1.2       nicm      454: /* Client functions that need to happen every loop. */
                    455: void
                    456: server_client_loop(void)
                    457: {
                    458:        struct client           *c;
                    459:        struct window           *w;
                    460:        struct window_pane      *wp;
                    461:        u_int                    i;
                    462:
                    463:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    464:                c = ARRAY_ITEM(&clients, i);
1.36      nicm      465:                if (c == NULL)
1.2       nicm      466:                        continue;
                    467:
1.36      nicm      468:                server_client_check_exit(c);
                    469:                if (c->session != NULL) {
                    470:                        server_client_check_redraw(c);
                    471:                        server_client_reset_state(c);
                    472:                }
1.2       nicm      473:        }
                    474:
                    475:        /*
                    476:         * Any windows will have been redrawn as part of clients, so clear
                    477:         * their flags now.
                    478:         */
                    479:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    480:                w = ARRAY_ITEM(&windows, i);
                    481:                if (w == NULL)
                    482:                        continue;
                    483:
                    484:                w->flags &= ~WINDOW_REDRAW;
                    485:                TAILQ_FOREACH(wp, &w->panes, entry)
                    486:                        wp->flags &= ~PANE_REDRAW;
                    487:        }
                    488: }
                    489:
1.18      nicm      490: /*
                    491:  * Update cursor position and mode settings. The scroll region and attributes
                    492:  * are cleared when idle (waiting for an event) as this is the most likely time
                    493:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                    494:  * compromise between excessive resets and likelihood of an interrupt.
                    495:  *
                    496:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                    497:  * things that are already in their default state.
                    498:  */
1.1       nicm      499: void
1.18      nicm      500: server_client_reset_state(struct client *c)
1.1       nicm      501: {
1.18      nicm      502:        struct window           *w = c->session->curw->window;
                    503:        struct window_pane      *wp = w->active;
                    504:        struct screen           *s = wp->screen;
                    505:        struct options          *oo = &c->session->options;
1.54      nicm      506:        struct options          *wo = &w->options;
1.66      nicm      507:        int                      status, mode, o;
1.60      nicm      508:
                    509:        if (c->flags & CLIENT_SUSPENDED)
                    510:                return;
1.1       nicm      511:
                    512:        tty_region(&c->tty, 0, c->tty.sy - 1);
                    513:
                    514:        status = options_get_number(oo, "status");
                    515:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                    516:                tty_cursor(&c->tty, 0, 0);
1.66      nicm      517:        else {
                    518:                o = status && options_get_number (oo, "status-position") == 0;
                    519:                tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
                    520:        }
1.1       nicm      521:
1.50      nicm      522:        /*
1.57      nicm      523:         * Resizing panes with the mouse requires at least button mode to give
                    524:         * a smooth appearance.
                    525:         */
                    526:        mode = s->mode;
                    527:        if ((c->last_mouse.b & MOUSE_RESIZE_PANE) &&
                    528:            !(mode & (MODE_MOUSE_BUTTON|MODE_MOUSE_ANY)))
                    529:                mode |= MODE_MOUSE_BUTTON;
                    530:
                    531:        /*
1.50      nicm      532:         * Any mode will do for mouse-select-pane, but set standard mode if
                    533:         * none.
                    534:         */
1.54      nicm      535:        if ((mode & ALL_MOUSE_MODES) == 0) {
                    536:                if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
1.55      nicm      537:                    options_get_number(oo, "mouse-select-pane"))
1.57      nicm      538:                        mode |= MODE_MOUSE_STANDARD;
                    539:                else if (options_get_number(oo, "mouse-resize-pane"))
1.54      nicm      540:                        mode |= MODE_MOUSE_STANDARD;
                    541:                else if (options_get_number(oo, "mouse-select-window"))
                    542:                        mode |= MODE_MOUSE_STANDARD;
                    543:                else if (options_get_number(wo, "mode-mouse"))
                    544:                        mode |= MODE_MOUSE_STANDARD;
                    545:        }
1.48      nicm      546:
                    547:        /*
                    548:         * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
                    549:         * user has set mouse-utf8 and any mouse mode is in effect, turn on
                    550:         * UTF-8 mouse input. If the receiving terminal hasn't requested it
                    551:         * (that is, it isn't in s->mode), then it'll be converted in
                    552:         * input_mouse.
                    553:         */
                    554:        if ((c->tty.flags & TTY_UTF8) &&
                    555:            (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
                    556:                mode |= MODE_MOUSE_UTF8;
                    557:        else
                    558:                mode &= ~MODE_MOUSE_UTF8;
                    559:
                    560:        /* Set the terminal mode and reset attributes. */
1.59      nicm      561:        tty_update_mode(&c->tty, mode, s);
1.1       nicm      562:        tty_reset(&c->tty);
1.17      nicm      563: }
                    564:
                    565: /* Repeat time callback. */
1.24      nicm      566: /* ARGSUSED */
1.17      nicm      567: void
                    568: server_client_repeat_timer(unused int fd, unused short events, void *data)
                    569: {
                    570:        struct client   *c = data;
                    571:
                    572:        if (c->flags & CLIENT_REPEAT)
                    573:                c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
1.1       nicm      574: }
                    575:
1.36      nicm      576: /* Check if client should be exited. */
                    577: void
                    578: server_client_check_exit(struct client *c)
                    579: {
                    580:        struct msg_exit_data    exitdata;
                    581:
                    582:        if (!(c->flags & CLIENT_EXIT))
                    583:                return;
                    584:
1.73      nicm      585:        if (EVBUFFER_LENGTH(c->stdin_data) != 0)
                    586:                return;
                    587:        if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36      nicm      588:                return;
1.73      nicm      589:        if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36      nicm      590:                return;
                    591:
                    592:        exitdata.retcode = c->retcode;
                    593:        server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
                    594:
                    595:        c->flags &= ~CLIENT_EXIT;
1.38      nicm      596: }
                    597:
1.1       nicm      598: /* Check for client redraws. */
                    599: void
                    600: server_client_check_redraw(struct client *c)
                    601: {
                    602:        struct session          *s = c->session;
                    603:        struct window_pane      *wp;
                    604:        int                      flags, redraw;
                    605:
1.79    ! nicm      606:        if (c->flags & CLIENT_SUSPENDED)
        !           607:                return;
        !           608:
1.1       nicm      609:        flags = c->tty.flags & TTY_FREEZE;
                    610:        c->tty.flags &= ~TTY_FREEZE;
                    611:
                    612:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
                    613:                if (options_get_number(&s->options, "set-titles"))
                    614:                        server_client_set_title(c);
1.12      nicm      615:
1.1       nicm      616:                if (c->message_string != NULL)
                    617:                        redraw = status_message_redraw(c);
                    618:                else if (c->prompt_string != NULL)
                    619:                        redraw = status_prompt_redraw(c);
                    620:                else
                    621:                        redraw = status_redraw(c);
                    622:                if (!redraw)
                    623:                        c->flags &= ~CLIENT_STATUS;
                    624:        }
                    625:
                    626:        if (c->flags & CLIENT_REDRAW) {
1.27      nicm      627:                screen_redraw_screen(c, 0, 0);
                    628:                c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38      nicm      629:        } else if (c->flags & CLIENT_REDRAWWINDOW) {
                    630:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
                    631:                        screen_redraw_pane(c, wp);
                    632:                c->flags &= ~CLIENT_REDRAWWINDOW;
1.1       nicm      633:        } else {
                    634:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
                    635:                        if (wp->flags & PANE_REDRAW)
                    636:                                screen_redraw_pane(c, wp);
                    637:                }
                    638:        }
                    639:
1.27      nicm      640:        if (c->flags & CLIENT_BORDERS)
                    641:                screen_redraw_screen(c, 0, 1);
                    642:
1.1       nicm      643:        if (c->flags & CLIENT_STATUS)
1.27      nicm      644:                screen_redraw_screen(c, 1, 0);
1.1       nicm      645:
                    646:        c->tty.flags |= flags;
                    647:
1.27      nicm      648:        c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
1.1       nicm      649: }
                    650:
                    651: /* Set client title. */
                    652: void
                    653: server_client_set_title(struct client *c)
                    654: {
                    655:        struct session  *s = c->session;
                    656:        const char      *template;
                    657:        char            *title;
                    658:
                    659:        template = options_get_string(&s->options, "set-titles-string");
1.25      nicm      660:
1.52      nicm      661:        title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
1.1       nicm      662:        if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77      nicm      663:                free(c->title);
1.1       nicm      664:                c->title = xstrdup(title);
                    665:                tty_set_title(&c->tty, c->title);
                    666:        }
1.77      nicm      667:        free(title);
1.1       nicm      668: }
                    669:
                    670: /* Dispatch message from client. */
                    671: int
                    672: server_client_msg_dispatch(struct client *c)
                    673: {
                    674:        struct imsg              imsg;
                    675:        struct msg_command_data  commanddata;
                    676:        struct msg_identify_data identifydata;
                    677:        struct msg_environ_data  environdata;
1.73      nicm      678:        struct msg_stdin_data    stdindata;
1.1       nicm      679:        ssize_t                  n, datalen;
                    680:
1.8       deraadt   681:        if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
                    682:                return (-1);
1.1       nicm      683:
                    684:        for (;;) {
                    685:                if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
                    686:                        return (-1);
                    687:                if (n == 0)
                    688:                        return (0);
                    689:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
                    690:
                    691:                if (imsg.hdr.peerid != PROTOCOL_VERSION) {
                    692:                        server_write_client(c, MSG_VERSION, NULL, 0);
                    693:                        c->flags |= CLIENT_BAD;
                    694:                        imsg_free(&imsg);
                    695:                        continue;
                    696:                }
                    697:
                    698:                log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
                    699:                switch (imsg.hdr.type) {
                    700:                case MSG_COMMAND:
                    701:                        if (datalen != sizeof commanddata)
                    702:                                fatalx("bad MSG_COMMAND size");
                    703:                        memcpy(&commanddata, imsg.data, sizeof commanddata);
                    704:
                    705:                        server_client_msg_command(c, &commanddata);
                    706:                        break;
                    707:                case MSG_IDENTIFY:
                    708:                        if (datalen != sizeof identifydata)
                    709:                                fatalx("bad MSG_IDENTIFY size");
                    710:                        if (imsg.fd == -1)
                    711:                                fatalx("MSG_IDENTIFY missing fd");
                    712:                        memcpy(&identifydata, imsg.data, sizeof identifydata);
                    713:
                    714:                        server_client_msg_identify(c, &identifydata, imsg.fd);
                    715:                        break;
1.73      nicm      716:                case MSG_STDIN:
                    717:                        if (datalen != sizeof stdindata)
                    718:                                fatalx("bad MSG_STDIN size");
                    719:                        memcpy(&stdindata, imsg.data, sizeof stdindata);
1.36      nicm      720:
1.73      nicm      721:                        if (c->stdin_callback == NULL)
                    722:                                break;
                    723:                        if (stdindata.size <= 0)
                    724:                                c->stdin_closed = 1;
                    725:                        else {
                    726:                                evbuffer_add(c->stdin_data, stdindata.data,
                    727:                                    stdindata.size);
                    728:                        }
                    729:                        c->stdin_callback(c, c->stdin_closed,
                    730:                            c->stdin_callback_data);
1.33      nicm      731:                        break;
1.1       nicm      732:                case MSG_RESIZE:
                    733:                        if (datalen != 0)
                    734:                                fatalx("bad MSG_RESIZE size");
                    735:
1.32      nicm      736:                        if (tty_resize(&c->tty)) {
                    737:                                recalculate_sizes();
                    738:                                server_redraw_client(c);
                    739:                        }
1.1       nicm      740:                        break;
                    741:                case MSG_EXITING:
                    742:                        if (datalen != 0)
                    743:                                fatalx("bad MSG_EXITING size");
                    744:
                    745:                        c->session = NULL;
                    746:                        tty_close(&c->tty);
                    747:                        server_write_client(c, MSG_EXITED, NULL, 0);
                    748:                        break;
                    749:                case MSG_WAKEUP:
                    750:                case MSG_UNLOCK:
                    751:                        if (datalen != 0)
                    752:                                fatalx("bad MSG_WAKEUP size");
                    753:
                    754:                        if (!(c->flags & CLIENT_SUSPENDED))
                    755:                                break;
                    756:                        c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm      757:
1.11      nicm      758:                        if (gettimeofday(&c->activity_time, NULL) != 0)
                    759:                                fatal("gettimeofday");
1.47      nicm      760:                        if (c->session != NULL)
                    761:                                session_update_activity(c->session);
1.10      nicm      762:
1.1       nicm      763:                        tty_start_tty(&c->tty);
                    764:                        server_redraw_client(c);
                    765:                        recalculate_sizes();
                    766:                        break;
                    767:                case MSG_ENVIRON:
                    768:                        if (datalen != sizeof environdata)
                    769:                                fatalx("bad MSG_ENVIRON size");
                    770:                        memcpy(&environdata, imsg.data, sizeof environdata);
                    771:
                    772:                        environdata.var[(sizeof environdata.var) - 1] = '\0';
                    773:                        if (strchr(environdata.var, '=') != NULL)
                    774:                                environ_put(&c->environ, environdata.var);
                    775:                        break;
                    776:                case MSG_SHELL:
                    777:                        if (datalen != 0)
                    778:                                fatalx("bad MSG_SHELL size");
                    779:
                    780:                        server_client_msg_shell(c);
                    781:                        break;
                    782:                default:
                    783:                        fatalx("unexpected message");
                    784:                }
                    785:
                    786:                imsg_free(&imsg);
                    787:        }
                    788: }
                    789:
                    790: /* Callback to send error message to client. */
                    791: void printflike2
                    792: server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
                    793: {
1.33      nicm      794:        va_list ap;
1.1       nicm      795:
                    796:        va_start(ap, fmt);
1.73      nicm      797:        evbuffer_add_vprintf(ctx->cmdclient->stderr_data, fmt, ap);
1.1       nicm      798:        va_end(ap);
                    799:
1.73      nicm      800:        evbuffer_add(ctx->cmdclient->stderr_data, "\n", 1);
                    801:        server_push_stderr(ctx->cmdclient);
1.34      nicm      802:        ctx->cmdclient->retcode = 1;
1.1       nicm      803: }
                    804:
                    805: /* Callback to send print message to client. */
                    806: void printflike2
                    807: server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
                    808: {
1.33      nicm      809:        va_list ap;
1.1       nicm      810:
                    811:        va_start(ap, fmt);
1.73      nicm      812:        evbuffer_add_vprintf(ctx->cmdclient->stdout_data, fmt, ap);
1.1       nicm      813:        va_end(ap);
                    814:
1.73      nicm      815:        evbuffer_add(ctx->cmdclient->stdout_data, "\n", 1);
                    816:        server_push_stdout(ctx->cmdclient);
1.1       nicm      817: }
                    818:
                    819: /* Callback to send print message to client, if not quiet. */
                    820: void printflike2
                    821: server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
                    822: {
1.33      nicm      823:        va_list ap;
1.1       nicm      824:
1.26      nicm      825:        if (options_get_number(&global_options, "quiet"))
1.1       nicm      826:                return;
                    827:
                    828:        va_start(ap, fmt);
1.73      nicm      829:        evbuffer_add_vprintf(ctx->cmdclient->stdout_data, fmt, ap);
1.1       nicm      830:        va_end(ap);
                    831:
1.73      nicm      832:        evbuffer_add(ctx->cmdclient->stdout_data, "\n", 1);
                    833:        server_push_stdout(ctx->cmdclient);
1.1       nicm      834: }
                    835:
                    836: /* Handle command message. */
                    837: void
                    838: server_client_msg_command(struct client *c, struct msg_command_data *data)
                    839: {
1.36      nicm      840:        struct cmd_ctx   ctx;
                    841:        struct cmd_list *cmdlist = NULL;
                    842:        int              argc;
                    843:        char           **argv, *cause;
1.1       nicm      844:
                    845:        ctx.error = server_client_msg_error;
                    846:        ctx.print = server_client_msg_print;
                    847:        ctx.info = server_client_msg_info;
                    848:
                    849:        ctx.msgdata = data;
                    850:        ctx.curclient = NULL;
                    851:
                    852:        ctx.cmdclient = c;
                    853:
                    854:        argc = data->argc;
                    855:        data->argv[(sizeof data->argv) - 1] = '\0';
                    856:        if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
                    857:                server_client_msg_error(&ctx, "command too long");
                    858:                goto error;
                    859:        }
                    860:
                    861:        if (argc == 0) {
                    862:                argc = 1;
                    863:                argv = xcalloc(1, sizeof *argv);
                    864:                *argv = xstrdup("new-session");
                    865:        }
                    866:
                    867:        if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
                    868:                server_client_msg_error(&ctx, "%s", cause);
                    869:                cmd_free_argv(argc, argv);
                    870:                goto error;
                    871:        }
                    872:        cmd_free_argv(argc, argv);
                    873:
1.78      nicm      874:        switch (cmd_list_exec(cmdlist, &ctx))
                    875:        {
                    876:        case CMD_RETURN_ERROR:
                    877:        case CMD_RETURN_NORMAL:
1.36      nicm      878:                c->flags |= CLIENT_EXIT;
1.78      nicm      879:                break;
                    880:        case CMD_RETURN_ATTACH:
                    881:        case CMD_RETURN_YIELD:
                    882:                break;
                    883:        }
1.1       nicm      884:        cmd_list_free(cmdlist);
                    885:        return;
                    886:
                    887: error:
                    888:        if (cmdlist != NULL)
                    889:                cmd_list_free(cmdlist);
1.36      nicm      890:        c->flags |= CLIENT_EXIT;
1.1       nicm      891: }
                    892:
                    893: /* Handle identify message. */
                    894: void
                    895: server_client_msg_identify(
                    896:     struct client *c, struct msg_identify_data *data, int fd)
                    897: {
                    898:        c->cwd = NULL;
                    899:        data->cwd[(sizeof data->cwd) - 1] = '\0';
                    900:        if (*data->cwd != '\0')
                    901:                c->cwd = xstrdup(data->cwd);
1.75      nicm      902:
                    903:        if (data->flags & IDENTIFY_CONTROL) {
                    904:                c->stdin_callback = control_callback;
                    905:                c->flags |= (CLIENT_CONTROL|CLIENT_SUSPENDED);
1.79    ! nicm      906:                server_write_client(c, MSG_STDIN, NULL, 0);
1.75      nicm      907:
                    908:                c->tty.fd = -1;
                    909:                c->tty.log_fd = -1;
                    910:
                    911:                close(fd);
                    912:                return;
                    913:        }
1.1       nicm      914:
1.33      nicm      915:        if (!isatty(fd))
                    916:            return;
1.1       nicm      917:        data->term[(sizeof data->term) - 1] = '\0';
1.74      nicm      918:        tty_init(&c->tty, c, fd, data->term);
1.1       nicm      919:        if (data->flags & IDENTIFY_UTF8)
                    920:                c->tty.flags |= TTY_UTF8;
                    921:        if (data->flags & IDENTIFY_256COLOURS)
                    922:                c->tty.term_flags |= TERM_256COLOURS;
                    923:        else if (data->flags & IDENTIFY_88COLOURS)
                    924:                c->tty.term_flags |= TERM_88COLOURS;
                    925:
                    926:        tty_resize(&c->tty);
                    927:
                    928:        c->flags |= CLIENT_TERMINAL;
                    929: }
                    930:
                    931: /* Handle shell message. */
                    932: void
                    933: server_client_msg_shell(struct client *c)
                    934: {
                    935:        struct msg_shell_data    data;
                    936:        const char              *shell;
1.25      nicm      937:
1.1       nicm      938:        shell = options_get_string(&global_s_options, "default-shell");
                    939:
                    940:        if (*shell == '\0' || areshell(shell))
                    941:                shell = _PATH_BSHELL;
                    942:        if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
                    943:                strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
1.25      nicm      944:
1.1       nicm      945:        server_write_client(c, MSG_SHELL, &data, sizeof data);
                    946:        c->flags |= CLIENT_BAD; /* it will die after exec */
                    947: }