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

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