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

1.274   ! nicm        1: /* $OpenBSD: server-client.c,v 1.273 2019/03/25 09:22:09 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.181     nicm        4:  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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>
1.92      nicm       20: #include <sys/ioctl.h>
1.162     nicm       21: #include <sys/uio.h>
1.1       nicm       22:
1.114     benno      23: #include <errno.h>
1.12      nicm       24: #include <event.h>
1.1       nicm       25: #include <fcntl.h>
1.162     nicm       26: #include <imsg.h>
1.98      nicm       27: #include <paths.h>
                     28: #include <stdlib.h>
1.1       nicm       29: #include <string.h>
1.4       nicm       30: #include <time.h>
1.1       nicm       31: #include <unistd.h>
                     32:
                     33: #include "tmux.h"
                     34:
1.190     nicm       35: static void    server_client_free(int, short, void *);
                     36: static void    server_client_check_focus(struct window_pane *);
                     37: static void    server_client_check_resize(struct window_pane *);
                     38: static key_code        server_client_check_mouse(struct client *);
                     39: static void    server_client_repeat_timer(int, short, void *);
1.192     nicm       40: static void    server_client_click_timer(int, short, void *);
1.190     nicm       41: static void    server_client_check_exit(struct client *);
                     42: static void    server_client_check_redraw(struct client *);
                     43: static void    server_client_set_title(struct client *);
                     44: static void    server_client_reset_state(struct client *);
                     45: static int     server_client_assume_paste(struct session *);
1.254     nicm       46: static void    server_client_clear_identify(struct client *,
                     47:                    struct window_pane *);
1.190     nicm       48:
                     49: static void    server_client_dispatch(struct imsg *, void *);
                     50: static void    server_client_dispatch_command(struct client *, struct imsg *);
                     51: static void    server_client_dispatch_identify(struct client *, struct imsg *);
                     52: static void    server_client_dispatch_shell(struct client *);
1.233     nicm       53:
                     54: /* Number of attached clients. */
                     55: u_int
                     56: server_client_how_many(void)
                     57: {
                     58:        struct client   *c;
                     59:        u_int            n;
                     60:
                     61:        n = 0;
                     62:        TAILQ_FOREACH(c, &clients, entry) {
                     63:                if (c->session != NULL && (~c->flags & CLIENT_DETACHING))
                     64:                        n++;
                     65:        }
                     66:        return (n);
                     67: }
1.140     nicm       68:
1.220     nicm       69: /* Identify mode callback. */
1.214     nicm       70: static void
1.232     nicm       71: server_client_callback_identify(__unused int fd, __unused short events,
                     72:     void *data)
1.214     nicm       73: {
                     74:        server_client_clear_identify(data, NULL);
                     75: }
                     76:
                     77: /* Set identify mode on client. */
                     78: void
1.242     nicm       79: server_client_set_identify(struct client *c, u_int delay)
1.214     nicm       80: {
                     81:        struct timeval  tv;
                     82:
                     83:        tv.tv_sec = delay / 1000;
                     84:        tv.tv_usec = (delay % 1000) * 1000L;
                     85:
                     86:        if (event_initialized(&c->identify_timer))
                     87:                evtimer_del(&c->identify_timer);
                     88:        evtimer_set(&c->identify_timer, server_client_callback_identify, c);
1.242     nicm       89:        if (delay != 0)
                     90:                evtimer_add(&c->identify_timer, &tv);
1.214     nicm       91:
                     92:        c->flags |= CLIENT_IDENTIFY;
                     93:        c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
                     94:        server_redraw_client(c);
                     95: }
                     96:
                     97: /* Clear identify mode on client. */
1.254     nicm       98: static void
1.214     nicm       99: server_client_clear_identify(struct client *c, struct window_pane *wp)
                    100: {
                    101:        if (~c->flags & CLIENT_IDENTIFY)
                    102:                return;
                    103:        c->flags &= ~CLIENT_IDENTIFY;
                    104:
                    105:        if (c->identify_callback != NULL)
                    106:                c->identify_callback(c, wp);
                    107:
                    108:        c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
                    109:        server_redraw_client(c);
                    110: }
                    111:
1.140     nicm      112: /* Check if this client is inside this server. */
                    113: int
                    114: server_client_check_nested(struct client *c)
                    115: {
                    116:        struct environ_entry    *envent;
                    117:        struct window_pane      *wp;
                    118:
1.164     nicm      119:        envent = environ_find(c->environ, "TMUX");
1.140     nicm      120:        if (envent == NULL || *envent->value == '\0')
                    121:                return (0);
                    122:
                    123:        RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
1.216     nicm      124:                if (strcmp(wp->tty, c->ttyname) == 0)
1.140     nicm      125:                        return (1);
                    126:        }
                    127:        return (0);
                    128: }
1.1       nicm      129:
1.132     nicm      130: /* Set client key table. */
                    131: void
1.178     nicm      132: server_client_set_key_table(struct client *c, const char *name)
1.132     nicm      133: {
1.178     nicm      134:        if (name == NULL)
                    135:                name = server_client_get_key_table(c);
                    136:
1.132     nicm      137:        key_bindings_unref_table(c->keytable);
                    138:        c->keytable = key_bindings_get_table(name, 1);
                    139:        c->keytable->references++;
                    140: }
                    141:
1.178     nicm      142: /* Get default key table. */
                    143: const char *
                    144: server_client_get_key_table(struct client *c)
                    145: {
                    146:        struct session  *s = c->session;
                    147:        const char      *name;
                    148:
                    149:        if (s == NULL)
                    150:                return ("root");
                    151:
                    152:        name = options_get_string(s->options, "key-table");
                    153:        if (*name == '\0')
                    154:                return ("root");
                    155:        return (name);
                    156: }
                    157:
1.223     nicm      158: /* Is this table the default key table? */
                    159: static int
                    160: server_client_is_default_key_table(struct client *c, struct key_table *table)
1.191     nicm      161: {
1.223     nicm      162:        return (strcmp(table->name, server_client_get_key_table(c)) == 0);
1.191     nicm      163: }
                    164:
1.1       nicm      165: /* Create a new client. */
1.246     nicm      166: struct client *
1.1       nicm      167: server_client_create(int fd)
                    168: {
                    169:        struct client   *c;
                    170:
1.49      nicm      171:        setblocking(fd, 0);
1.1       nicm      172:
                    173:        c = xcalloc(1, sizeof *c);
1.141     nicm      174:        c->references = 1;
1.162     nicm      175:        c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25      nicm      176:
1.10      nicm      177:        if (gettimeofday(&c->creation_time, NULL) != 0)
1.1       nicm      178:                fatal("gettimeofday failed");
1.11      nicm      179:        memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111     nicm      180:
1.164     nicm      181:        c->environ = environ_create();
1.1       nicm      182:
1.146     nicm      183:        c->fd = -1;
1.165     nicm      184:        c->cwd = NULL;
1.145     nicm      185:
1.194     nicm      186:        TAILQ_INIT(&c->queue);
1.94      nicm      187:
1.116     nicm      188:        c->stdin_data = evbuffer_new();
1.263     nicm      189:        if (c->stdin_data == NULL)
                    190:                fatalx("out of memory");
1.116     nicm      191:        c->stdout_data = evbuffer_new();
1.263     nicm      192:        if (c->stdout_data == NULL)
                    193:                fatalx("out of memory");
1.116     nicm      194:        c->stderr_data = evbuffer_new();
1.263     nicm      195:        if (c->stderr_data == NULL)
                    196:                fatalx("out of memory");
1.33      nicm      197:
1.1       nicm      198:        c->tty.fd = -1;
                    199:        c->title = NULL;
                    200:
                    201:        c->session = NULL;
1.45      nicm      202:        c->last_session = NULL;
1.261     nicm      203:
1.1       nicm      204:        c->tty.sx = 80;
                    205:        c->tty.sy = 24;
                    206:
1.271     nicm      207:        status_init(c);
1.1       nicm      208:
                    209:        c->message_string = NULL;
1.136     nicm      210:        TAILQ_INIT(&c->message_log);
1.1       nicm      211:
                    212:        c->prompt_string = NULL;
                    213:        c->prompt_buffer = NULL;
                    214:        c->prompt_index = 0;
                    215:
1.93      nicm      216:        c->flags |= CLIENT_FOCUSED;
                    217:
1.132     nicm      218:        c->keytable = key_bindings_get_table("root", 1);
                    219:        c->keytable->references++;
                    220:
1.17      nicm      221:        evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
1.192     nicm      222:        evtimer_set(&c->click_timer, server_client_click_timer, c);
1.17      nicm      223:
1.135     nicm      224:        TAILQ_INSERT_TAIL(&clients, c, entry);
1.157     nicm      225:        log_debug("new client %p", c);
1.246     nicm      226:        return (c);
1.72      nicm      227: }
                    228:
                    229: /* Open client terminal if needed. */
                    230: int
1.119     nicm      231: server_client_open(struct client *c, char **cause)
1.72      nicm      232: {
1.75      nicm      233:        if (c->flags & CLIENT_CONTROL)
                    234:                return (0);
1.120     nicm      235:
                    236:        if (strcmp(c->ttyname, "/dev/tty") == 0) {
                    237:                *cause = xstrdup("can't use /dev/tty");
                    238:                return (-1);
                    239:        }
1.75      nicm      240:
1.72      nicm      241:        if (!(c->flags & CLIENT_TERMINAL)) {
1.116     nicm      242:                *cause = xstrdup("not a terminal");
1.72      nicm      243:                return (-1);
                    244:        }
                    245:
1.119     nicm      246:        if (tty_open(&c->tty, cause) != 0)
1.72      nicm      247:                return (-1);
                    248:
                    249:        return (0);
1.1       nicm      250: }
                    251:
                    252: /* Lost a client. */
                    253: void
                    254: server_client_lost(struct client *c)
                    255: {
1.136     nicm      256:        struct message_entry    *msg, *msg1;
1.1       nicm      257:
1.141     nicm      258:        c->flags |= CLIENT_DEAD;
                    259:
1.214     nicm      260:        server_client_clear_identify(c, NULL);
1.141     nicm      261:        status_prompt_clear(c);
                    262:        status_message_clear(c);
                    263:
                    264:        if (c->stdin_callback != NULL)
                    265:                c->stdin_callback(c, 1, c->stdin_callback_data);
                    266:
1.135     nicm      267:        TAILQ_REMOVE(&clients, c, entry);
1.157     nicm      268:        log_debug("lost client %p", c);
1.1       nicm      269:
                    270:        /*
                    271:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    272:         * and tty_free might close an unrelated fd.
                    273:         */
                    274:        if (c->flags & CLIENT_TERMINAL)
                    275:                tty_free(&c->tty);
1.109     nicm      276:        free(c->ttyname);
                    277:        free(c->term);
1.1       nicm      278:
1.113     nicm      279:        evbuffer_free(c->stdin_data);
                    280:        evbuffer_free(c->stdout_data);
1.97      nicm      281:        if (c->stderr_data != c->stdout_data)
1.126     nicm      282:                evbuffer_free(c->stderr_data);
1.33      nicm      283:
1.270     nicm      284:        status_free(c);
1.1       nicm      285:
1.77      nicm      286:        free(c->title);
1.165     nicm      287:        free((void *)c->cwd);
1.1       nicm      288:
1.17      nicm      289:        evtimer_del(&c->repeat_timer);
1.192     nicm      290:        evtimer_del(&c->click_timer);
1.17      nicm      291:
1.132     nicm      292:        key_bindings_unref_table(c->keytable);
                    293:
1.69      nicm      294:        if (event_initialized(&c->identify_timer))
                    295:                evtimer_del(&c->identify_timer);
1.15      nicm      296:
1.77      nicm      297:        free(c->message_string);
1.116     nicm      298:        if (event_initialized(&c->message_timer))
1.69      nicm      299:                evtimer_del(&c->message_timer);
1.136     nicm      300:        TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1.77      nicm      301:                free(msg->msg);
1.136     nicm      302:                TAILQ_REMOVE(&c->message_log, msg, entry);
                    303:                free(msg);
1.21      nicm      304:        }
1.1       nicm      305:
1.259     nicm      306:        free(c->prompt_saved);
1.77      nicm      307:        free(c->prompt_string);
                    308:        free(c->prompt_buffer);
1.61      nicm      309:
1.228     nicm      310:        format_lost_client(c);
1.164     nicm      311:        environ_free(c->environ);
1.1       nicm      312:
1.162     nicm      313:        proc_remove_peer(c->peer);
                    314:        c->peer = NULL;
1.13      nicm      315:
1.142     nicm      316:        server_client_unref(c);
1.71      nicm      317:
                    318:        server_add_accept(0); /* may be more file descriptors now */
1.1       nicm      319:
                    320:        recalculate_sizes();
1.41      nicm      321:        server_check_unattached();
1.19      nicm      322:        server_update_socket();
1.141     nicm      323: }
                    324:
                    325: /* Remove reference from a client. */
                    326: void
1.142     nicm      327: server_client_unref(struct client *c)
1.141     nicm      328: {
1.157     nicm      329:        log_debug("unref client %p (%d references)", c, c->references);
1.141     nicm      330:
                    331:        c->references--;
                    332:        if (c->references == 0)
                    333:                event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
                    334: }
                    335:
                    336: /* Free dead client. */
1.190     nicm      337: static void
1.170     nicm      338: server_client_free(__unused int fd, __unused short events, void *arg)
1.141     nicm      339: {
                    340:        struct client   *c = arg;
                    341:
1.157     nicm      342:        log_debug("free client %p (%d references)", c, c->references);
1.141     nicm      343:
1.194     nicm      344:        if (!TAILQ_EMPTY(&c->queue))
                    345:                fatalx("queue not empty");
                    346:
1.216     nicm      347:        if (c->references == 0) {
                    348:                free((void *)c->name);
1.141     nicm      349:                free(c);
1.216     nicm      350:        }
1.9       nicm      351: }
                    352:
1.220     nicm      353: /* Suspend a client. */
                    354: void
                    355: server_client_suspend(struct client *c)
                    356: {
1.232     nicm      357:        struct session  *s = c->session;
1.220     nicm      358:
                    359:        if (s == NULL || (c->flags & CLIENT_DETACHING))
                    360:                return;
                    361:
                    362:        tty_stop_tty(&c->tty);
                    363:        c->flags |= CLIENT_SUSPENDED;
                    364:        proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0);
                    365: }
                    366:
1.174     nicm      367: /* Detach a client. */
                    368: void
                    369: server_client_detach(struct client *c, enum msgtype msgtype)
                    370: {
1.232     nicm      371:        struct session  *s = c->session;
1.174     nicm      372:
1.220     nicm      373:        if (s == NULL || (c->flags & CLIENT_DETACHING))
1.174     nicm      374:                return;
                    375:
1.220     nicm      376:        c->flags |= CLIENT_DETACHING;
1.196     nicm      377:        notify_client("client-detached", c);
1.240     nicm      378:        proc_send(c->peer, msgtype, -1, s->name, strlen(s->name) + 1);
1.207     nicm      379: }
                    380:
1.208     nicm      381: /* Execute command to replace a client. */
1.207     nicm      382: void
                    383: server_client_exec(struct client *c, const char *cmd)
                    384: {
                    385:        struct session  *s = c->session;
1.208     nicm      386:        char            *msg;
                    387:        const char      *shell;
1.207     nicm      388:        size_t           cmdsize, shellsize;
                    389:
                    390:        if (*cmd == '\0')
                    391:                return;
                    392:        cmdsize = strlen(cmd) + 1;
                    393:
                    394:        if (s != NULL)
                    395:                shell = options_get_string(s->options, "default-shell");
                    396:        else
                    397:                shell = options_get_string(global_s_options, "default-shell");
                    398:        shellsize = strlen(shell) + 1;
                    399:
                    400:        msg = xmalloc(cmdsize + shellsize);
                    401:        memcpy(msg, cmd, cmdsize);
                    402:        memcpy(msg + cmdsize, shell, shellsize);
                    403:
                    404:        proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize);
                    405:        free(msg);
1.174     nicm      406: }
                    407:
1.66      nicm      408: /* Check for mouse keys. */
1.190     nicm      409: static key_code
1.131     nicm      410: server_client_check_mouse(struct client *c)
1.66      nicm      411: {
1.192     nicm      412:        struct session          *s = c->session;
                    413:        struct mouse_event      *m = &c->tty.mouse;
1.272     nicm      414:        struct winlink          *wl;
1.192     nicm      415:        struct window_pane      *wp;
1.261     nicm      416:        u_int                    x, y, b, sx, sy, px, py;
1.192     nicm      417:        int                      flag;
                    418:        key_code                 key;
                    419:        struct timeval           tv;
1.272     nicm      420:        struct style_range      *sr;
1.209     nicm      421:        enum { NOTYPE, MOVE, DOWN, UP, DRAG, WHEEL, DOUBLE, TRIPLE } type;
1.274   ! nicm      422:        enum { NOWHERE, PANE, STATUS, STATUS_LEFT, STATUS_RIGHT, STATUS_DEFAULT, BORDER } where;
1.209     nicm      423:
                    424:        type = NOTYPE;
                    425:        where = NOWHERE;
1.131     nicm      426:
1.261     nicm      427:        log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c->name, m->b,
                    428:            m->x, m->y, m->lx, m->ly, c->tty.mouse_drag_flag);
1.131     nicm      429:
                    430:        /* What type of event is this? */
1.209     nicm      431:        if ((m->sgr_type != ' ' &&
                    432:            MOUSE_DRAG(m->sgr_b) &&
                    433:            MOUSE_BUTTONS(m->sgr_b) == 3) ||
                    434:            (m->sgr_type == ' ' &&
                    435:            MOUSE_DRAG(m->b) &&
                    436:            MOUSE_BUTTONS(m->b) == 3 &&
                    437:            MOUSE_BUTTONS(m->lb) == 3)) {
                    438:                type = MOVE;
                    439:                x = m->x, y = m->y, b = 0;
                    440:                log_debug("move at %u,%u", x, y);
                    441:        } else if (MOUSE_DRAG(m->b)) {
1.131     nicm      442:                type = DRAG;
                    443:                if (c->tty.mouse_drag_flag) {
                    444:                        x = m->x, y = m->y, b = m->b;
                    445:                        log_debug("drag update at %u,%u", x, y);
                    446:                } else {
1.261     nicm      447:                        x = m->lx - m->ox, y = m->ly - m->oy, b = m->lb;
1.131     nicm      448:                        log_debug("drag start at %u,%u", x, y);
                    449:                }
                    450:        } else if (MOUSE_WHEEL(m->b)) {
                    451:                type = WHEEL;
                    452:                x = m->x, y = m->y, b = m->b;
                    453:                log_debug("wheel at %u,%u", x, y);
1.200     nicm      454:        } else if (MOUSE_RELEASE(m->b)) {
1.131     nicm      455:                type = UP;
                    456:                x = m->x, y = m->y, b = m->lb;
                    457:                log_debug("up at %u,%u", x, y);
                    458:        } else {
1.192     nicm      459:                if (c->flags & CLIENT_DOUBLECLICK) {
                    460:                        evtimer_del(&c->click_timer);
                    461:                        c->flags &= ~CLIENT_DOUBLECLICK;
                    462:                        if (m->b == c->click_button) {
                    463:                                type = DOUBLE;
                    464:                                x = m->x, y = m->y, b = m->b;
                    465:                                log_debug("double-click at %u,%u", x, y);
                    466:                                flag = CLIENT_TRIPLECLICK;
                    467:                                goto add_timer;
                    468:                        }
                    469:                } else if (c->flags & CLIENT_TRIPLECLICK) {
                    470:                        evtimer_del(&c->click_timer);
                    471:                        c->flags &= ~CLIENT_TRIPLECLICK;
                    472:                        if (m->b == c->click_button) {
                    473:                                type = TRIPLE;
                    474:                                x = m->x, y = m->y, b = m->b;
                    475:                                log_debug("triple-click at %u,%u", x, y);
                    476:                                goto have_event;
                    477:                        }
                    478:                }
                    479:
1.131     nicm      480:                type = DOWN;
                    481:                x = m->x, y = m->y, b = m->b;
                    482:                log_debug("down at %u,%u", x, y);
1.192     nicm      483:                flag = CLIENT_DOUBLECLICK;
                    484:
                    485:        add_timer:
                    486:                if (KEYC_CLICK_TIMEOUT != 0) {
                    487:                        c->flags |= flag;
                    488:                        c->click_button = m->b;
                    489:
                    490:                        tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
                    491:                        tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
                    492:                        evtimer_del(&c->click_timer);
                    493:                        evtimer_add(&c->click_timer, &tv);
                    494:                }
1.131     nicm      495:        }
1.192     nicm      496:
                    497: have_event:
1.131     nicm      498:        if (type == NOTYPE)
1.177     nicm      499:                return (KEYC_UNKNOWN);
1.131     nicm      500:
1.258     nicm      501:        /* Save the session. */
1.131     nicm      502:        m->s = s->id;
1.258     nicm      503:        m->w = -1;
1.131     nicm      504:
                    505:        /* Is this on the status line? */
                    506:        m->statusat = status_at_line(c);
1.272     nicm      507:        if (m->statusat != -1 &&
                    508:            y >= (u_int)m->statusat &&
1.273     nicm      509:            y < m->statusat + status_line_size(c)) {
1.272     nicm      510:                sr = status_get_range(c, x, y - m->statusat);
1.274   ! nicm      511:                if (sr == NULL) {
        !           512:                        where = STATUS_DEFAULT;
        !           513:                } else {
        !           514:                        switch (sr->type) {
        !           515:                        case STYLE_RANGE_NONE:
1.273     nicm      516:                                return (KEYC_UNKNOWN);
1.274   ! nicm      517:                        case STYLE_RANGE_LEFT:
        !           518:                                where = STATUS_LEFT;
        !           519:                                break;
        !           520:                        case STYLE_RANGE_RIGHT:
        !           521:                                where = STATUS_RIGHT;
        !           522:                                break;
        !           523:                        case STYLE_RANGE_WINDOW:
        !           524:                                wl = winlink_find_by_index(&s->windows, sr->argument);
        !           525:                                if (wl == NULL)
        !           526:                                        return (KEYC_UNKNOWN);
        !           527:                                m->w = wl->window->id;
1.273     nicm      528:
1.274   ! nicm      529:                                where = STATUS;
        !           530:                                break;
        !           531:                        }
1.258     nicm      532:                }
                    533:        }
1.131     nicm      534:
                    535:        /* Not on status line. Adjust position and check for border or pane. */
                    536:        if (where == NOWHERE) {
1.261     nicm      537:                px = x;
1.131     nicm      538:                if (m->statusat == 0 && y > 0)
1.261     nicm      539:                        py = y - 1;
1.131     nicm      540:                else if (m->statusat > 0 && y >= (u_int)m->statusat)
1.261     nicm      541:                        py = m->statusat - 1;
                    542:                else
                    543:                        py = y;
                    544:
                    545:                tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy);
                    546:                log_debug("mouse window @%u at %u,%u (%ux%u)",
                    547:                    s->curw->window->id, m->ox, m->oy, sx, sy);
                    548:                if (px > sx || py > sy)
                    549:                        return (KEYC_UNKNOWN);
                    550:                px = px + m->ox;
                    551:                py = py + m->oy;
                    552:                m->x = x + m->ox;
                    553:                m->y = y + m->oy;
1.131     nicm      554:
1.260     nicm      555:                /* Try the pane borders if not zoomed. */
                    556:                if (~s->curw->window->flags & WINDOW_ZOOMED) {
                    557:                        TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
1.261     nicm      558:                                if ((wp->xoff + wp->sx == px &&
                    559:                                    wp->yoff <= 1 + py &&
                    560:                                    wp->yoff + wp->sy >= py) ||
                    561:                                    (wp->yoff + wp->sy == py &&
                    562:                                    wp->xoff <= 1 + px &&
                    563:                                    wp->xoff + wp->sx >= px))
1.260     nicm      564:                                        break;
                    565:                        }
                    566:                        if (wp != NULL)
                    567:                                where = BORDER;
1.131     nicm      568:                }
1.260     nicm      569:
                    570:                /* Otherwise try inside the pane. */
                    571:                if (where == NOWHERE) {
1.261     nicm      572:                        wp = window_get_active_at(s->curw->window, px, py);
1.260     nicm      573:                        if (wp != NULL)
1.131     nicm      574:                                where = PANE;
                    575:                }
1.260     nicm      576:
1.131     nicm      577:                if (where == NOWHERE)
1.177     nicm      578:                        return (KEYC_UNKNOWN);
1.260     nicm      579:                if (where == PANE)
                    580:                        log_debug("mouse %u,%u on pane %%%u", x, y, wp->id);
                    581:                else if (where == BORDER)
                    582:                        log_debug("mouse on pane %%%u border", wp->id);
1.131     nicm      583:                m->wp = wp->id;
                    584:                m->w = wp->window->id;
                    585:        } else
                    586:                m->wp = -1;
                    587:
                    588:        /* Stop dragging if needed. */
1.200     nicm      589:        if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) {
1.131     nicm      590:                if (c->tty.mouse_drag_release != NULL)
                    591:                        c->tty.mouse_drag_release(c, m);
                    592:
                    593:                c->tty.mouse_drag_update = NULL;
                    594:                c->tty.mouse_drag_release = NULL;
                    595:
1.182     nicm      596:                /*
1.183     nicm      597:                 * End a mouse drag by passing a MouseDragEnd key corresponding
                    598:                 * to the button that started the drag.
1.182     nicm      599:                 */
                    600:                switch (c->tty.mouse_drag_flag) {
                    601:                case 1:
                    602:                        if (where == PANE)
1.183     nicm      603:                                key = KEYC_MOUSEDRAGEND1_PANE;
1.182     nicm      604:                        if (where == STATUS)
1.183     nicm      605:                                key = KEYC_MOUSEDRAGEND1_STATUS;
1.258     nicm      606:                        if (where == STATUS_LEFT)
                    607:                                key = KEYC_MOUSEDRAGEND1_STATUS_LEFT;
                    608:                        if (where == STATUS_RIGHT)
                    609:                                key = KEYC_MOUSEDRAGEND1_STATUS_RIGHT;
1.274   ! nicm      610:                        if (where == STATUS_DEFAULT)
        !           611:                                key = KEYC_MOUSEDRAGEND1_STATUS_DEFAULT;
1.182     nicm      612:                        if (where == BORDER)
1.183     nicm      613:                                key = KEYC_MOUSEDRAGEND1_BORDER;
1.182     nicm      614:                        break;
                    615:                case 2:
                    616:                        if (where == PANE)
1.183     nicm      617:                                key = KEYC_MOUSEDRAGEND2_PANE;
1.182     nicm      618:                        if (where == STATUS)
1.183     nicm      619:                                key = KEYC_MOUSEDRAGEND2_STATUS;
1.258     nicm      620:                        if (where == STATUS_LEFT)
                    621:                                key = KEYC_MOUSEDRAGEND2_STATUS_LEFT;
                    622:                        if (where == STATUS_RIGHT)
                    623:                                key = KEYC_MOUSEDRAGEND2_STATUS_RIGHT;
1.274   ! nicm      624:                        if (where == STATUS_DEFAULT)
        !           625:                                key = KEYC_MOUSEDRAGEND2_STATUS_DEFAULT;
1.182     nicm      626:                        if (where == BORDER)
1.183     nicm      627:                                key = KEYC_MOUSEDRAGEND2_BORDER;
1.182     nicm      628:                        break;
                    629:                case 3:
                    630:                        if (where == PANE)
1.183     nicm      631:                                key = KEYC_MOUSEDRAGEND3_PANE;
1.182     nicm      632:                        if (where == STATUS)
1.183     nicm      633:                                key = KEYC_MOUSEDRAGEND3_STATUS;
1.258     nicm      634:                        if (where == STATUS_LEFT)
                    635:                                key = KEYC_MOUSEDRAGEND3_STATUS_LEFT;
                    636:                        if (where == STATUS_RIGHT)
                    637:                                key = KEYC_MOUSEDRAGEND3_STATUS_RIGHT;
1.274   ! nicm      638:                        if (where == STATUS_DEFAULT)
        !           639:                                key = KEYC_MOUSEDRAGEND3_STATUS_DEFAULT;
1.182     nicm      640:                        if (where == BORDER)
1.183     nicm      641:                                key = KEYC_MOUSEDRAGEND3_BORDER;
1.182     nicm      642:                        break;
                    643:                default:
                    644:                        key = KEYC_MOUSE;
                    645:                        break;
                    646:                }
1.131     nicm      647:                c->tty.mouse_drag_flag = 0;
1.182     nicm      648:
                    649:                return (key);
1.131     nicm      650:        }
                    651:
                    652:        /* Convert to a key binding. */
1.177     nicm      653:        key = KEYC_UNKNOWN;
1.131     nicm      654:        switch (type) {
                    655:        case NOTYPE:
                    656:                break;
1.209     nicm      657:        case MOVE:
                    658:                if (where == PANE)
                    659:                        key = KEYC_MOUSEMOVE_PANE;
                    660:                if (where == STATUS)
                    661:                        key = KEYC_MOUSEMOVE_STATUS;
1.274   ! nicm      662:                if (where == STATUS_LEFT)
        !           663:                        key = KEYC_MOUSEMOVE_STATUS_LEFT;
        !           664:                if (where == STATUS_RIGHT)
        !           665:                        key = KEYC_MOUSEMOVE_STATUS_RIGHT;
        !           666:                if (where == STATUS_DEFAULT)
        !           667:                        key = KEYC_MOUSEMOVE_STATUS_DEFAULT;
1.209     nicm      668:                if (where == BORDER)
                    669:                        key = KEYC_MOUSEMOVE_BORDER;
                    670:                break;
1.131     nicm      671:        case DRAG:
1.204     nicm      672:                if (c->tty.mouse_drag_update != NULL)
                    673:                        key = KEYC_DRAGGING;
                    674:                else {
1.131     nicm      675:                        switch (MOUSE_BUTTONS(b)) {
                    676:                        case 0:
                    677:                                if (where == PANE)
                    678:                                        key = KEYC_MOUSEDRAG1_PANE;
                    679:                                if (where == STATUS)
                    680:                                        key = KEYC_MOUSEDRAG1_STATUS;
1.258     nicm      681:                                if (where == STATUS_LEFT)
                    682:                                        key = KEYC_MOUSEDRAG1_STATUS_LEFT;
                    683:                                if (where == STATUS_RIGHT)
                    684:                                        key = KEYC_MOUSEDRAG1_STATUS_RIGHT;
1.274   ! nicm      685:                                if (where == STATUS_DEFAULT)
        !           686:                                        key = KEYC_MOUSEDRAG1_STATUS_DEFAULT;
1.131     nicm      687:                                if (where == BORDER)
                    688:                                        key = KEYC_MOUSEDRAG1_BORDER;
                    689:                                break;
                    690:                        case 1:
                    691:                                if (where == PANE)
                    692:                                        key = KEYC_MOUSEDRAG2_PANE;
                    693:                                if (where == STATUS)
                    694:                                        key = KEYC_MOUSEDRAG2_STATUS;
1.258     nicm      695:                                if (where == STATUS_LEFT)
                    696:                                        key = KEYC_MOUSEDRAG2_STATUS_LEFT;
                    697:                                if (where == STATUS_RIGHT)
                    698:                                        key = KEYC_MOUSEDRAG2_STATUS_RIGHT;
1.274   ! nicm      699:                                if (where == STATUS_DEFAULT)
        !           700:                                        key = KEYC_MOUSEDRAG2_STATUS_DEFAULT;
1.131     nicm      701:                                if (where == BORDER)
                    702:                                        key = KEYC_MOUSEDRAG2_BORDER;
                    703:                                break;
                    704:                        case 2:
                    705:                                if (where == PANE)
                    706:                                        key = KEYC_MOUSEDRAG3_PANE;
                    707:                                if (where == STATUS)
                    708:                                        key = KEYC_MOUSEDRAG3_STATUS;
1.258     nicm      709:                                if (where == STATUS_LEFT)
                    710:                                        key = KEYC_MOUSEDRAG3_STATUS_LEFT;
                    711:                                if (where == STATUS_RIGHT)
                    712:                                        key = KEYC_MOUSEDRAG3_STATUS_RIGHT;
1.274   ! nicm      713:                                if (where == STATUS_DEFAULT)
        !           714:                                        key = KEYC_MOUSEDRAG3_STATUS_DEFAULT;
1.131     nicm      715:                                if (where == BORDER)
                    716:                                        key = KEYC_MOUSEDRAG3_BORDER;
                    717:                                break;
                    718:                        }
                    719:                }
1.66      nicm      720:
1.182     nicm      721:                /*
                    722:                 * Begin a drag by setting the flag to a non-zero value that
                    723:                 * corresponds to the mouse button in use.
                    724:                 */
                    725:                c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131     nicm      726:                break;
                    727:        case WHEEL:
                    728:                if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
                    729:                        if (where == PANE)
                    730:                                key = KEYC_WHEELUP_PANE;
                    731:                        if (where == STATUS)
                    732:                                key = KEYC_WHEELUP_STATUS;
1.258     nicm      733:                        if (where == STATUS_LEFT)
                    734:                                key = KEYC_WHEELUP_STATUS_LEFT;
                    735:                        if (where == STATUS_RIGHT)
                    736:                                key = KEYC_WHEELUP_STATUS_RIGHT;
1.274   ! nicm      737:                        if (where == STATUS_DEFAULT)
        !           738:                                key = KEYC_WHEELUP_STATUS_DEFAULT;
1.131     nicm      739:                        if (where == BORDER)
                    740:                                key = KEYC_WHEELUP_BORDER;
                    741:                } else {
                    742:                        if (where == PANE)
                    743:                                key = KEYC_WHEELDOWN_PANE;
                    744:                        if (where == STATUS)
                    745:                                key = KEYC_WHEELDOWN_STATUS;
1.274   ! nicm      746:                        if (where == STATUS_LEFT)
        !           747:                                key = KEYC_WHEELDOWN_STATUS_LEFT;
        !           748:                        if (where == STATUS_RIGHT)
        !           749:                                key = KEYC_WHEELDOWN_STATUS_RIGHT;
        !           750:                        if (where == STATUS_DEFAULT)
        !           751:                                key = KEYC_WHEELDOWN_STATUS_DEFAULT;
1.131     nicm      752:                        if (where == BORDER)
                    753:                                key = KEYC_WHEELDOWN_BORDER;
                    754:                }
                    755:                break;
                    756:        case UP:
                    757:                switch (MOUSE_BUTTONS(b)) {
                    758:                case 0:
                    759:                        if (where == PANE)
                    760:                                key = KEYC_MOUSEUP1_PANE;
                    761:                        if (where == STATUS)
                    762:                                key = KEYC_MOUSEUP1_STATUS;
1.258     nicm      763:                        if (where == STATUS_LEFT)
                    764:                                key = KEYC_MOUSEUP1_STATUS_LEFT;
                    765:                        if (where == STATUS_RIGHT)
                    766:                                key = KEYC_MOUSEUP1_STATUS_RIGHT;
1.274   ! nicm      767:                        if (where == STATUS_DEFAULT)
        !           768:                                key = KEYC_MOUSEUP1_STATUS_DEFAULT;
1.131     nicm      769:                        if (where == BORDER)
                    770:                                key = KEYC_MOUSEUP1_BORDER;
                    771:                        break;
                    772:                case 1:
                    773:                        if (where == PANE)
                    774:                                key = KEYC_MOUSEUP2_PANE;
                    775:                        if (where == STATUS)
                    776:                                key = KEYC_MOUSEUP2_STATUS;
1.258     nicm      777:                        if (where == STATUS_LEFT)
                    778:                                key = KEYC_MOUSEUP2_STATUS_LEFT;
                    779:                        if (where == STATUS_RIGHT)
                    780:                                key = KEYC_MOUSEUP2_STATUS_RIGHT;
1.274   ! nicm      781:                        if (where == STATUS_DEFAULT)
        !           782:                                key = KEYC_MOUSEUP2_STATUS_DEFAULT;
1.131     nicm      783:                        if (where == BORDER)
                    784:                                key = KEYC_MOUSEUP2_BORDER;
                    785:                        break;
                    786:                case 2:
                    787:                        if (where == PANE)
                    788:                                key = KEYC_MOUSEUP3_PANE;
                    789:                        if (where == STATUS)
                    790:                                key = KEYC_MOUSEUP3_STATUS;
1.258     nicm      791:                        if (where == STATUS_LEFT)
                    792:                                key = KEYC_MOUSEUP3_STATUS_LEFT;
                    793:                        if (where == STATUS_RIGHT)
                    794:                                key = KEYC_MOUSEUP3_STATUS_RIGHT;
1.274   ! nicm      795:                        if (where == STATUS_DEFAULT)
        !           796:                                key = KEYC_MOUSEUP3_STATUS_DEFAULT;
1.131     nicm      797:                        if (where == BORDER)
                    798:                                key = KEYC_MOUSEUP3_BORDER;
                    799:                        break;
                    800:                }
                    801:                break;
                    802:        case DOWN:
                    803:                switch (MOUSE_BUTTONS(b)) {
                    804:                case 0:
                    805:                        if (where == PANE)
                    806:                                key = KEYC_MOUSEDOWN1_PANE;
                    807:                        if (where == STATUS)
                    808:                                key = KEYC_MOUSEDOWN1_STATUS;
1.258     nicm      809:                        if (where == STATUS_LEFT)
                    810:                                key = KEYC_MOUSEDOWN1_STATUS_LEFT;
                    811:                        if (where == STATUS_RIGHT)
                    812:                                key = KEYC_MOUSEDOWN1_STATUS_RIGHT;
1.274   ! nicm      813:                        if (where == STATUS_DEFAULT)
        !           814:                                key = KEYC_MOUSEDOWN1_STATUS_DEFAULT;
1.131     nicm      815:                        if (where == BORDER)
                    816:                                key = KEYC_MOUSEDOWN1_BORDER;
                    817:                        break;
                    818:                case 1:
                    819:                        if (where == PANE)
                    820:                                key = KEYC_MOUSEDOWN2_PANE;
                    821:                        if (where == STATUS)
                    822:                                key = KEYC_MOUSEDOWN2_STATUS;
1.258     nicm      823:                        if (where == STATUS_LEFT)
                    824:                                key = KEYC_MOUSEDOWN2_STATUS_LEFT;
                    825:                        if (where == STATUS_RIGHT)
                    826:                                key = KEYC_MOUSEDOWN2_STATUS_RIGHT;
1.274   ! nicm      827:                        if (where == STATUS_DEFAULT)
        !           828:                                key = KEYC_MOUSEDOWN2_STATUS_DEFAULT;
1.131     nicm      829:                        if (where == BORDER)
                    830:                                key = KEYC_MOUSEDOWN2_BORDER;
                    831:                        break;
                    832:                case 2:
                    833:                        if (where == PANE)
                    834:                                key = KEYC_MOUSEDOWN3_PANE;
                    835:                        if (where == STATUS)
                    836:                                key = KEYC_MOUSEDOWN3_STATUS;
1.258     nicm      837:                        if (where == STATUS_LEFT)
                    838:                                key = KEYC_MOUSEDOWN3_STATUS_LEFT;
                    839:                        if (where == STATUS_RIGHT)
                    840:                                key = KEYC_MOUSEDOWN3_STATUS_RIGHT;
1.274   ! nicm      841:                        if (where == STATUS_DEFAULT)
        !           842:                                key = KEYC_MOUSEDOWN3_STATUS_DEFAULT;
1.131     nicm      843:                        if (where == BORDER)
                    844:                                key = KEYC_MOUSEDOWN3_BORDER;
                    845:                        break;
1.66      nicm      846:                }
1.131     nicm      847:                break;
1.192     nicm      848:        case DOUBLE:
                    849:                switch (MOUSE_BUTTONS(b)) {
                    850:                case 0:
                    851:                        if (where == PANE)
                    852:                                key = KEYC_DOUBLECLICK1_PANE;
                    853:                        if (where == STATUS)
                    854:                                key = KEYC_DOUBLECLICK1_STATUS;
1.258     nicm      855:                        if (where == STATUS_LEFT)
                    856:                                key = KEYC_DOUBLECLICK1_STATUS_LEFT;
                    857:                        if (where == STATUS_RIGHT)
                    858:                                key = KEYC_DOUBLECLICK1_STATUS_RIGHT;
1.274   ! nicm      859:                        if (where == STATUS_DEFAULT)
        !           860:                                key = KEYC_DOUBLECLICK1_STATUS_DEFAULT;
1.192     nicm      861:                        if (where == BORDER)
                    862:                                key = KEYC_DOUBLECLICK1_BORDER;
                    863:                        break;
                    864:                case 1:
                    865:                        if (where == PANE)
                    866:                                key = KEYC_DOUBLECLICK2_PANE;
                    867:                        if (where == STATUS)
                    868:                                key = KEYC_DOUBLECLICK2_STATUS;
1.258     nicm      869:                        if (where == STATUS_LEFT)
                    870:                                key = KEYC_DOUBLECLICK2_STATUS_LEFT;
                    871:                        if (where == STATUS_RIGHT)
                    872:                                key = KEYC_DOUBLECLICK2_STATUS_RIGHT;
1.274   ! nicm      873:                        if (where == STATUS_DEFAULT)
        !           874:                                key = KEYC_DOUBLECLICK2_STATUS_DEFAULT;
1.192     nicm      875:                        if (where == BORDER)
                    876:                                key = KEYC_DOUBLECLICK2_BORDER;
                    877:                        break;
                    878:                case 2:
                    879:                        if (where == PANE)
                    880:                                key = KEYC_DOUBLECLICK3_PANE;
                    881:                        if (where == STATUS)
                    882:                                key = KEYC_DOUBLECLICK3_STATUS;
1.258     nicm      883:                        if (where == STATUS_LEFT)
                    884:                                key = KEYC_DOUBLECLICK3_STATUS_LEFT;
                    885:                        if (where == STATUS_RIGHT)
                    886:                                key = KEYC_DOUBLECLICK3_STATUS_RIGHT;
1.274   ! nicm      887:                        if (where == STATUS_DEFAULT)
        !           888:                                key = KEYC_DOUBLECLICK3_STATUS_DEFAULT;
1.192     nicm      889:                        if (where == BORDER)
                    890:                                key = KEYC_DOUBLECLICK3_BORDER;
                    891:                        break;
                    892:                }
                    893:                break;
                    894:        case TRIPLE:
                    895:                switch (MOUSE_BUTTONS(b)) {
                    896:                case 0:
                    897:                        if (where == PANE)
                    898:                                key = KEYC_TRIPLECLICK1_PANE;
                    899:                        if (where == STATUS)
                    900:                                key = KEYC_TRIPLECLICK1_STATUS;
1.258     nicm      901:                        if (where == STATUS_LEFT)
                    902:                                key = KEYC_TRIPLECLICK1_STATUS_LEFT;
                    903:                        if (where == STATUS_RIGHT)
                    904:                                key = KEYC_TRIPLECLICK1_STATUS_RIGHT;
1.274   ! nicm      905:                        if (where == STATUS_DEFAULT)
        !           906:                                key = KEYC_TRIPLECLICK1_STATUS_DEFAULT;
1.192     nicm      907:                        if (where == BORDER)
                    908:                                key = KEYC_TRIPLECLICK1_BORDER;
                    909:                        break;
                    910:                case 1:
                    911:                        if (where == PANE)
                    912:                                key = KEYC_TRIPLECLICK2_PANE;
                    913:                        if (where == STATUS)
                    914:                                key = KEYC_TRIPLECLICK2_STATUS;
1.258     nicm      915:                        if (where == STATUS_LEFT)
                    916:                                key = KEYC_TRIPLECLICK2_STATUS_LEFT;
                    917:                        if (where == STATUS_RIGHT)
                    918:                                key = KEYC_TRIPLECLICK2_STATUS_RIGHT;
1.274   ! nicm      919:                        if (where == STATUS_DEFAULT)
        !           920:                                key = KEYC_TRIPLECLICK2_STATUS_DEFAULT;
1.192     nicm      921:                        if (where == BORDER)
                    922:                                key = KEYC_TRIPLECLICK2_BORDER;
                    923:                        break;
                    924:                case 2:
                    925:                        if (where == PANE)
                    926:                                key = KEYC_TRIPLECLICK3_PANE;
                    927:                        if (where == STATUS)
                    928:                                key = KEYC_TRIPLECLICK3_STATUS;
1.258     nicm      929:                        if (where == STATUS_LEFT)
                    930:                                key = KEYC_TRIPLECLICK3_STATUS_LEFT;
                    931:                        if (where == STATUS_RIGHT)
                    932:                                key = KEYC_TRIPLECLICK3_STATUS_RIGHT;
1.274   ! nicm      933:                        if (where == STATUS_DEFAULT)
        !           934:                                key = KEYC_TRIPLECLICK3_STATUS_DEFAULT;
1.192     nicm      935:                        if (where == BORDER)
                    936:                                key = KEYC_TRIPLECLICK3_BORDER;
                    937:                        break;
                    938:                }
                    939:                break;
1.66      nicm      940:        }
1.177     nicm      941:        if (key == KEYC_UNKNOWN)
                    942:                return (KEYC_UNKNOWN);
1.66      nicm      943:
1.131     nicm      944:        /* Apply modifiers if any. */
                    945:        if (b & MOUSE_MASK_META)
                    946:                key |= KEYC_ESCAPE;
                    947:        if (b & MOUSE_MASK_CTRL)
                    948:                key |= KEYC_CTRL;
                    949:        if (b & MOUSE_MASK_SHIFT)
                    950:                key |= KEYC_SHIFT;
1.66      nicm      951:
1.131     nicm      952:        return (key);
1.66      nicm      953: }
                    954:
1.82      nicm      955: /* Is this fast enough to probably be a paste? */
1.190     nicm      956: static int
1.82      nicm      957: server_client_assume_paste(struct session *s)
                    958: {
                    959:        struct timeval  tv;
1.84      nicm      960:        int             t;
1.82      nicm      961:
1.163     nicm      962:        if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83      nicm      963:                return (0);
1.82      nicm      964:
                    965:        timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171     nicm      966:        if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
                    967:                log_debug("session %s pasting (flag %d)", s->name,
                    968:                    !!(s->flags & SESSION_PASTING));
                    969:                if (s->flags & SESSION_PASTING)
                    970:                        return (1);
                    971:                s->flags |= SESSION_PASTING;
                    972:                return (0);
                    973:        }
                    974:        log_debug("session %s not pasting", s->name);
                    975:        s->flags &= ~SESSION_PASTING;
1.83      nicm      976:        return (0);
1.82      nicm      977: }
                    978:
1.18      nicm      979: /* Handle data key input from client. */
                    980: void
1.168     nicm      981: server_client_handle_key(struct client *c, key_code key)
1.18      nicm      982: {
1.267     nicm      983:        struct mouse_event              *m = &c->tty.mouse;
                    984:        struct session                  *s = c->session;
                    985:        struct winlink                  *wl;
                    986:        struct window                   *w;
                    987:        struct window_pane              *wp;
                    988:        struct window_mode_entry        *wme;
                    989:        struct timeval                   tv;
                    990:        struct key_table                *table, *first;
                    991:        struct key_binding              *bd;
                    992:        int                              xtimeout, flags;
                    993:        struct cmd_find_state            fs;
                    994:        key_code                         key0;
1.18      nicm      995:
                    996:        /* Check the client is good to accept input. */
1.132     nicm      997:        if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.18      nicm      998:                return;
1.264     nicm      999:        wl = s->curw;
                   1000:        w = wl->window;
1.18      nicm     1001:
                   1002:        /* Update the activity timer. */
                   1003:        if (gettimeofday(&c->activity_time, NULL) != 0)
                   1004:                fatal("gettimeofday failed");
1.149     nicm     1005:        session_update_activity(s, &c->activity_time);
1.18      nicm     1006:
1.132     nicm     1007:        /* Number keys jump to pane in identify mode. */
1.25      nicm     1008:        if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30      nicm     1009:                if (c->flags & CLIENT_READONLY)
                   1010:                        return;
1.95      nicm     1011:                window_unzoom(w);
1.18      nicm     1012:                wp = window_pane_at_index(w, key - '0');
1.214     nicm     1013:                server_client_clear_identify(c, wp);
1.18      nicm     1014:                return;
                   1015:        }
                   1016:
                   1017:        /* Handle status line. */
1.30      nicm     1018:        if (!(c->flags & CLIENT_READONLY)) {
                   1019:                status_message_clear(c);
1.214     nicm     1020:                server_client_clear_identify(c, NULL);
1.30      nicm     1021:        }
1.18      nicm     1022:        if (c->prompt_string != NULL) {
1.193     nicm     1023:                if (c->flags & CLIENT_READONLY)
                   1024:                        return;
                   1025:                if (status_prompt_key(c, key) == 0)
                   1026:                        return;
1.18      nicm     1027:        }
                   1028:
                   1029:        /* Check for mouse keys. */
1.204     nicm     1030:        m->valid = 0;
1.18      nicm     1031:        if (key == KEYC_MOUSE) {
1.30      nicm     1032:                if (c->flags & CLIENT_READONLY)
                   1033:                        return;
1.131     nicm     1034:                key = server_client_check_mouse(c);
1.177     nicm     1035:                if (key == KEYC_UNKNOWN)
1.131     nicm     1036:                        return;
                   1037:
                   1038:                m->valid = 1;
                   1039:                m->key = key;
1.203     nicm     1040:
                   1041:                /*
1.204     nicm     1042:                 * Mouse drag is in progress, so fire the callback (now that
                   1043:                 * the mouse event is valid).
1.203     nicm     1044:                 */
1.204     nicm     1045:                if (key == KEYC_DRAGGING) {
                   1046:                        c->tty.mouse_drag_update(c, m);
1.203     nicm     1047:                        return;
1.204     nicm     1048:                }
1.131     nicm     1049:        } else
                   1050:                m->valid = 0;
1.18      nicm     1051:
1.202     nicm     1052:        /* Find affected pane. */
1.243     nicm     1053:        if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0)
                   1054:                cmd_find_from_session(&fs, s, 0);
1.227     nicm     1055:        wp = fs.wp;
1.202     nicm     1056:
                   1057:        /* Forward mouse keys if disabled. */
1.206     nicm     1058:        if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1.253     nicm     1059:                goto forward_key;
1.202     nicm     1060:
1.132     nicm     1061:        /* Treat everything as a regular key when pasting is detected. */
1.161     nicm     1062:        if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
1.253     nicm     1063:                goto forward_key;
1.18      nicm     1064:
1.191     nicm     1065:        /*
                   1066:         * Work out the current key table. If the pane is in a mode, use
                   1067:         * the mode table instead of the default key table.
                   1068:         */
1.223     nicm     1069:        if (server_client_is_default_key_table(c, c->keytable) &&
                   1070:            wp != NULL &&
1.267     nicm     1071:            (wme = TAILQ_FIRST(&wp->modes)) != NULL &&
                   1072:            wme->mode->key_table != NULL)
                   1073:                table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1.223     nicm     1074:        else
1.191     nicm     1075:                table = c->keytable;
1.223     nicm     1076:        first = table;
1.191     nicm     1077:
1.253     nicm     1078: table_changed:
1.205     nicm     1079:        /*
                   1080:         * The prefix always takes precedence and forces a switch to the prefix
                   1081:         * table, unless we are already there.
                   1082:         */
1.252     nicm     1083:        key0 = (key & ~KEYC_XTERM);
1.239     nicm     1084:        if ((key0 == (key_code)options_get_number(s->options, "prefix") ||
                   1085:            key0 == (key_code)options_get_number(s->options, "prefix2")) &&
1.205     nicm     1086:            strcmp(table->name, "prefix") != 0) {
                   1087:                server_client_set_key_table(c, "prefix");
                   1088:                server_status_client(c);
                   1089:                return;
                   1090:        }
1.238     nicm     1091:        flags = c->flags;
1.205     nicm     1092:
1.262     nicm     1093: try_again:
1.223     nicm     1094:        /* Log key table. */
                   1095:        if (wp == NULL)
                   1096:                log_debug("key table %s (no pane)", table->name);
                   1097:        else
                   1098:                log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.225     nicm     1099:        if (c->flags & CLIENT_REPEAT)
                   1100:                log_debug("currently repeating");
1.223     nicm     1101:
1.132     nicm     1102:        /* Try to see if there is a key binding in the current table. */
1.254     nicm     1103:        bd = key_bindings_get(table, key0);
1.132     nicm     1104:        if (bd != NULL) {
                   1105:                /*
                   1106:                 * Key was matched in this table. If currently repeating but a
                   1107:                 * non-repeating binding was found, stop repeating and try
                   1108:                 * again in the root table.
                   1109:                 */
1.222     nicm     1110:                if ((c->flags & CLIENT_REPEAT) &&
                   1111:                    (~bd->flags & KEY_BINDING_REPEAT)) {
1.262     nicm     1112:                        log_debug("found in key table %s (not repeating)",
                   1113:                            table->name);
1.178     nicm     1114:                        server_client_set_key_table(c, NULL);
1.262     nicm     1115:                        first = table = c->keytable;
1.132     nicm     1116:                        c->flags &= ~CLIENT_REPEAT;
1.85      nicm     1117:                        server_status_client(c);
1.253     nicm     1118:                        goto table_changed;
1.18      nicm     1119:                }
1.223     nicm     1120:                log_debug("found in key table %s", table->name);
1.82      nicm     1121:
1.132     nicm     1122:                /*
                   1123:                 * Take a reference to this table to make sure the key binding
                   1124:                 * doesn't disappear.
                   1125:                 */
                   1126:                table->references++;
                   1127:
                   1128:                /*
                   1129:                 * If this is a repeating key, start the timer. Otherwise reset
                   1130:                 * the client back to the root table.
                   1131:                 */
1.163     nicm     1132:                xtimeout = options_get_number(s->options, "repeat-time");
1.222     nicm     1133:                if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) {
1.132     nicm     1134:                        c->flags |= CLIENT_REPEAT;
                   1135:
                   1136:                        tv.tv_sec = xtimeout / 1000;
                   1137:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                   1138:                        evtimer_del(&c->repeat_timer);
                   1139:                        evtimer_add(&c->repeat_timer, &tv);
                   1140:                } else {
1.18      nicm     1141:                        c->flags &= ~CLIENT_REPEAT;
1.178     nicm     1142:                        server_client_set_key_table(c, NULL);
1.18      nicm     1143:                }
1.132     nicm     1144:                server_status_client(c);
                   1145:
1.227     nicm     1146:                /* Execute the key binding. */
1.231     nicm     1147:                key_bindings_dispatch(bd, NULL, c, m, &fs);
1.132     nicm     1148:                key_bindings_unref_table(table);
1.18      nicm     1149:                return;
                   1150:        }
                   1151:
1.132     nicm     1152:        /*
1.253     nicm     1153:         * No match, try the ANY key.
                   1154:         */
                   1155:        if (key0 != KEYC_ANY) {
                   1156:                key0 = KEYC_ANY;
                   1157:                goto try_again;
                   1158:        }
                   1159:
                   1160:        /*
1.223     nicm     1161:         * No match in this table. If not in the root table or if repeating,
                   1162:         * switch the client back to the root table and try again.
1.132     nicm     1163:         */
1.223     nicm     1164:        log_debug("not found in key table %s", table->name);
                   1165:        if (!server_client_is_default_key_table(c, table) ||
                   1166:            (c->flags & CLIENT_REPEAT)) {
1.262     nicm     1167:                log_debug("trying in root table");
1.178     nicm     1168:                server_client_set_key_table(c, NULL);
1.262     nicm     1169:                table = c->keytable;
                   1170:                if (c->flags & CLIENT_REPEAT)
                   1171:                        first = table;
1.18      nicm     1172:                c->flags &= ~CLIENT_REPEAT;
1.132     nicm     1173:                server_status_client(c);
1.253     nicm     1174:                goto table_changed;
1.18      nicm     1175:        }
                   1176:
1.223     nicm     1177:        /*
                   1178:         * No match in the root table either. If this wasn't the first table
                   1179:         * tried, don't pass the key to the pane.
                   1180:         */
1.238     nicm     1181:        if (first != table && (~flags & CLIENT_REPEAT)) {
1.178     nicm     1182:                server_client_set_key_table(c, NULL);
1.132     nicm     1183:                server_status_client(c);
1.161     nicm     1184:                return;
                   1185:        }
                   1186:
1.253     nicm     1187: forward_key:
1.161     nicm     1188:        if (c->flags & CLIENT_READONLY)
                   1189:                return;
                   1190:        if (wp != NULL)
1.264     nicm     1191:                window_pane_key(wp, c, s, wl, key, m);
1.18      nicm     1192: }
                   1193:
1.2       nicm     1194: /* Client functions that need to happen every loop. */
                   1195: void
                   1196: server_client_loop(void)
                   1197: {
                   1198:        struct client           *c;
                   1199:        struct window           *w;
                   1200:        struct window_pane      *wp;
1.197     nicm     1201:        int                      focus;
1.2       nicm     1202:
1.135     nicm     1203:        TAILQ_FOREACH(c, &clients, entry) {
1.36      nicm     1204:                server_client_check_exit(c);
                   1205:                if (c->session != NULL) {
                   1206:                        server_client_check_redraw(c);
                   1207:                        server_client_reset_state(c);
                   1208:                }
1.2       nicm     1209:        }
                   1210:
                   1211:        /*
                   1212:         * Any windows will have been redrawn as part of clients, so clear
1.92      nicm     1213:         * their flags now. Also check pane focus and resize.
1.2       nicm     1214:         */
1.197     nicm     1215:        focus = options_get_number(global_options, "focus-events");
1.134     nicm     1216:        RB_FOREACH(w, windows, &windows) {
1.91      nicm     1217:                TAILQ_FOREACH(wp, &w->panes, entry) {
1.101     nicm     1218:                        if (wp->fd != -1) {
1.197     nicm     1219:                                if (focus)
                   1220:                                        server_client_check_focus(wp);
1.101     nicm     1221:                                server_client_check_resize(wp);
                   1222:                        }
1.2       nicm     1223:                        wp->flags &= ~PANE_REDRAW;
1.91      nicm     1224:                }
1.150     nicm     1225:                check_window_name(w);
1.2       nicm     1226:        }
1.92      nicm     1227: }
                   1228:
1.237     nicm     1229: /* Check if we need to force a resize. */
                   1230: static int
                   1231: server_client_resize_force(struct window_pane *wp)
                   1232: {
                   1233:        struct timeval  tv = { .tv_usec = 100000 };
                   1234:        struct winsize  ws;
                   1235:
                   1236:        /*
                   1237:         * If we are resizing to the same size as when we entered the loop
                   1238:         * (that is, to the same size the application currently thinks it is),
                   1239:         * tmux may have gone through several resizes internally and thrown
                   1240:         * away parts of the screen. So we need the application to actually
                   1241:         * redraw even though its final size has not changed.
                   1242:         */
                   1243:
                   1244:        if (wp->flags & PANE_RESIZEFORCE) {
                   1245:                wp->flags &= ~PANE_RESIZEFORCE;
                   1246:                return (0);
                   1247:        }
                   1248:
                   1249:        if (wp->sx != wp->osx ||
                   1250:            wp->sy != wp->osy ||
                   1251:            wp->sx <= 1 ||
                   1252:            wp->sy <= 1)
                   1253:                return (0);
                   1254:
                   1255:        memset(&ws, 0, sizeof ws);
                   1256:        ws.ws_col = wp->sx;
                   1257:        ws.ws_row = wp->sy - 1;
1.244     nicm     1258:        if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.237     nicm     1259:                fatal("ioctl failed");
                   1260:        log_debug("%s: %%%u forcing resize", __func__, wp->id);
                   1261:
                   1262:        evtimer_add(&wp->resize_timer, &tv);
                   1263:        wp->flags |= PANE_RESIZEFORCE;
                   1264:        return (1);
                   1265: }
                   1266:
1.211     nicm     1267: /* Resize timer event. */
1.188     nicm     1268: static void
                   1269: server_client_resize_event(__unused int fd, __unused short events, void *data)
1.92      nicm     1270: {
1.188     nicm     1271:        struct window_pane      *wp = data;
                   1272:        struct winsize           ws;
                   1273:
                   1274:        evtimer_del(&wp->resize_timer);
1.92      nicm     1275:
1.101     nicm     1276:        if (!(wp->flags & PANE_RESIZE))
1.92      nicm     1277:                return;
1.237     nicm     1278:        if (server_client_resize_force(wp))
                   1279:                return;
1.235     nicm     1280:
1.92      nicm     1281:        memset(&ws, 0, sizeof ws);
                   1282:        ws.ws_col = wp->sx;
                   1283:        ws.ws_row = wp->sy;
1.244     nicm     1284:        if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92      nicm     1285:                fatal("ioctl failed");
1.235     nicm     1286:        log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, wp->sy);
1.92      nicm     1287:
                   1288:        wp->flags &= ~PANE_RESIZE;
1.235     nicm     1289:
                   1290:        wp->osx = wp->sx;
                   1291:        wp->osy = wp->sy;
1.188     nicm     1292: }
                   1293:
                   1294: /* Check if pane should be resized. */
1.190     nicm     1295: static void
1.188     nicm     1296: server_client_check_resize(struct window_pane *wp)
                   1297: {
                   1298:        struct timeval   tv = { .tv_usec = 250000 };
                   1299:
                   1300:        if (!(wp->flags & PANE_RESIZE))
                   1301:                return;
1.235     nicm     1302:        log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, wp->sy);
1.188     nicm     1303:
                   1304:        if (!event_initialized(&wp->resize_timer))
                   1305:                evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
                   1306:
                   1307:        /*
                   1308:         * The first resize should happen immediately, so if the timer is not
                   1309:         * running, do it now.
                   1310:         */
                   1311:        if (!evtimer_pending(&wp->resize_timer, NULL))
                   1312:                server_client_resize_event(-1, 0, wp);
                   1313:
                   1314:        /*
                   1315:         * If the pane is in the alternate screen, let the timer expire and
                   1316:         * resize to give the application a chance to redraw. If not, keep
                   1317:         * pushing the timer back.
                   1318:         */
                   1319:        if (wp->saved_grid != NULL && evtimer_pending(&wp->resize_timer, NULL))
                   1320:                return;
                   1321:        evtimer_del(&wp->resize_timer);
                   1322:        evtimer_add(&wp->resize_timer, &tv);
1.91      nicm     1323: }
                   1324:
                   1325: /* Check whether pane should be focused. */
1.190     nicm     1326: static void
1.91      nicm     1327: server_client_check_focus(struct window_pane *wp)
                   1328: {
1.93      nicm     1329:        struct client   *c;
1.102     nicm     1330:        int              push;
                   1331:
                   1332:        /* Do we need to push the focus state? */
                   1333:        push = wp->flags & PANE_FOCUSPUSH;
                   1334:        wp->flags &= ~PANE_FOCUSPUSH;
1.91      nicm     1335:
                   1336:        /* If we're not the active pane in our window, we're not focused. */
                   1337:        if (wp->window->active != wp)
                   1338:                goto not_focused;
                   1339:
                   1340:        /* If we're in a mode, we're not focused. */
                   1341:        if (wp->screen != &wp->base)
                   1342:                goto not_focused;
                   1343:
                   1344:        /*
1.93      nicm     1345:         * If our window is the current window in any focused clients with an
                   1346:         * attached session, we're focused.
1.91      nicm     1347:         */
1.135     nicm     1348:        TAILQ_FOREACH(c, &clients, entry) {
                   1349:                if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91      nicm     1350:                        continue;
1.255     nicm     1351:                if (c->session->attached == 0)
1.93      nicm     1352:                        continue;
                   1353:
                   1354:                if (c->session->curw->window == wp->window)
1.91      nicm     1355:                        goto focused;
                   1356:        }
                   1357:
                   1358: not_focused:
1.251     nicm     1359:        if (push || (wp->flags & PANE_FOCUSED)) {
                   1360:                if (wp->base.mode & MODE_FOCUSON)
                   1361:                        bufferevent_write(wp->event, "\033[O", 3);
                   1362:                notify_pane("pane-focus-out", wp);
                   1363:        }
1.91      nicm     1364:        wp->flags &= ~PANE_FOCUSED;
                   1365:        return;
                   1366:
                   1367: focused:
1.251     nicm     1368:        if (push || !(wp->flags & PANE_FOCUSED)) {
                   1369:                if (wp->base.mode & MODE_FOCUSON)
                   1370:                        bufferevent_write(wp->event, "\033[I", 3);
                   1371:                notify_pane("pane-focus-in", wp);
                   1372:        }
1.91      nicm     1373:        wp->flags |= PANE_FOCUSED;
1.2       nicm     1374: }
                   1375:
1.18      nicm     1376: /*
                   1377:  * Update cursor position and mode settings. The scroll region and attributes
                   1378:  * are cleared when idle (waiting for an event) as this is the most likely time
                   1379:  * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
                   1380:  * compromise between excessive resets and likelihood of an interrupt.
                   1381:  *
                   1382:  * tty_region/tty_reset/tty_update_mode already take care of not resetting
                   1383:  * things that are already in their default state.
                   1384:  */
1.190     nicm     1385: static void
1.18      nicm     1386: server_client_reset_state(struct client *c)
1.1       nicm     1387: {
1.18      nicm     1388:        struct window           *w = c->session->curw->window;
1.209     nicm     1389:        struct window_pane      *wp = w->active, *loop;
1.18      nicm     1390:        struct screen           *s = wp->screen;
1.163     nicm     1391:        struct options          *oo = c->session->options;
1.261     nicm     1392:        int                      mode, cursor = 0;
                   1393:        u_int                    cx = 0, cy = 0, ox, oy, sx, sy;
1.60      nicm     1394:
1.186     nicm     1395:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86      nicm     1396:                return;
1.261     nicm     1397:        mode = s->mode;
1.86      nicm     1398:
1.199     nicm     1399:        tty_region_off(&c->tty);
                   1400:        tty_margin_off(&c->tty);
1.1       nicm     1401:
1.261     nicm     1402:        /* Move cursor to pane cursor and offset. */
                   1403:        cursor = 0;
                   1404:        tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
                   1405:        if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx &&
                   1406:            wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) {
                   1407:                cursor = 1;
                   1408:
                   1409:                cx = wp->xoff + s->cx - ox;
                   1410:                cy = wp->yoff + s->cy - oy;
                   1411:
                   1412:                if (status_at_line(c) == 0)
                   1413:                        cy += status_line_size(c);
                   1414:        }
                   1415:        if (!cursor)
                   1416:                mode &= ~MODE_CURSOR;
                   1417:        tty_cursor(&c->tty, cx, cy);
1.1       nicm     1418:
1.50      nicm     1419:        /*
1.131     nicm     1420:         * Set mouse mode if requested. To support dragging, always use button
                   1421:         * mode.
1.57      nicm     1422:         */
1.209     nicm     1423:        if (options_get_number(oo, "mouse")) {
                   1424:                mode &= ~ALL_MOUSE_MODES;
                   1425:                TAILQ_FOREACH(loop, &w->panes, entry) {
                   1426:                        if (loop->screen->mode & MODE_MOUSE_ALL)
                   1427:                                mode |= MODE_MOUSE_ALL;
                   1428:                }
                   1429:                if (~mode & MODE_MOUSE_ALL)
                   1430:                        mode |= MODE_MOUSE_BUTTON;
                   1431:        }
1.215     nicm     1432:
                   1433:        /* Clear bracketed paste mode if at the prompt. */
                   1434:        if (c->prompt_string != NULL)
                   1435:                mode &= ~MODE_BRACKETPASTE;
1.48      nicm     1436:
                   1437:        /* Set the terminal mode and reset attributes. */
1.59      nicm     1438:        tty_update_mode(&c->tty, mode, s);
1.1       nicm     1439:        tty_reset(&c->tty);
1.17      nicm     1440: }
                   1441:
                   1442: /* Repeat time callback. */
1.190     nicm     1443: static void
1.170     nicm     1444: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17      nicm     1445: {
                   1446:        struct client   *c = data;
                   1447:
1.85      nicm     1448:        if (c->flags & CLIENT_REPEAT) {
1.178     nicm     1449:                server_client_set_key_table(c, NULL);
1.132     nicm     1450:                c->flags &= ~CLIENT_REPEAT;
                   1451:                server_status_client(c);
1.85      nicm     1452:        }
1.192     nicm     1453: }
                   1454:
                   1455: /* Double-click callback. */
                   1456: static void
                   1457: server_client_click_timer(__unused int fd, __unused short events, void *data)
                   1458: {
                   1459:        struct client   *c = data;
                   1460:
                   1461:        c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1       nicm     1462: }
                   1463:
1.36      nicm     1464: /* Check if client should be exited. */
1.190     nicm     1465: static void
1.36      nicm     1466: server_client_check_exit(struct client *c)
                   1467: {
                   1468:        if (!(c->flags & CLIENT_EXIT))
                   1469:                return;
                   1470:
1.73      nicm     1471:        if (EVBUFFER_LENGTH(c->stdin_data) != 0)
                   1472:                return;
                   1473:        if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36      nicm     1474:                return;
1.73      nicm     1475:        if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36      nicm     1476:                return;
                   1477:
1.249     nicm     1478:        if (c->flags & CLIENT_ATTACHED)
                   1479:                notify_client("client-detached", c);
1.162     nicm     1480:        proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36      nicm     1481:        c->flags &= ~CLIENT_EXIT;
1.38      nicm     1482: }
                   1483:
1.218     nicm     1484: /* Redraw timer callback. */
                   1485: static void
                   1486: server_client_redraw_timer(__unused int fd, __unused short events,
                   1487:     __unused void* data)
                   1488: {
                   1489:        log_debug("redraw timer fired");
                   1490: }
                   1491:
1.1       nicm     1492: /* Check for client redraws. */
1.190     nicm     1493: static void
1.1       nicm     1494: server_client_check_redraw(struct client *c)
                   1495: {
                   1496:        struct session          *s = c->session;
1.137     nicm     1497:        struct tty              *tty = &c->tty;
1.1       nicm     1498:        struct window_pane      *wp;
1.256     nicm     1499:        int                      needed, flags;
1.218     nicm     1500:        struct timeval           tv = { .tv_usec = 1000 };
                   1501:        static struct event      ev;
                   1502:        size_t                   left;
1.1       nicm     1503:
1.86      nicm     1504:        if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79      nicm     1505:                return;
1.257     nicm     1506:        if (c->flags & CLIENT_ALLREDRAWFLAGS) {
                   1507:                log_debug("%s: redraw%s%s%s", c->name,
                   1508:                    (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
                   1509:                    (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
                   1510:                    (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "");
                   1511:        }
1.218     nicm     1512:
                   1513:        /*
                   1514:         * If there is outstanding data, defer the redraw until it has been
                   1515:         * consumed. We can just add a timer to get out of the event loop and
                   1516:         * end up back here.
                   1517:         */
                   1518:        needed = 0;
1.256     nicm     1519:        if (c->flags & CLIENT_ALLREDRAWFLAGS)
1.218     nicm     1520:                needed = 1;
                   1521:        else {
                   1522:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
                   1523:                        if (wp->flags & PANE_REDRAW) {
                   1524:                                needed = 1;
                   1525:                                break;
                   1526:                        }
                   1527:                }
                   1528:        }
1.241     nicm     1529:        if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
                   1530:                log_debug("%s: redraw deferred (%zu left)", c->name, left);
                   1531:                if (!evtimer_initialized(&ev))
                   1532:                        evtimer_set(&ev, server_client_redraw_timer, NULL);
                   1533:                if (!evtimer_pending(&ev, NULL)) {
1.218     nicm     1534:                        log_debug("redraw timer started");
                   1535:                        evtimer_add(&ev, &tv);
1.241     nicm     1536:                }
1.218     nicm     1537:
1.241     nicm     1538:                /*
                   1539:                 * We may have got here for a single pane redraw, but force a
                   1540:                 * full redraw next time in case other panes have been updated.
                   1541:                 */
1.256     nicm     1542:                c->flags |= CLIENT_ALLREDRAWFLAGS;
1.241     nicm     1543:                return;
                   1544:        } else if (needed)
1.218     nicm     1545:                log_debug("%s: redraw needed", c->name);
1.79      nicm     1546:
1.219     nicm     1547:        flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
                   1548:        tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE)) | TTY_NOCURSOR;
1.137     nicm     1549:
1.256     nicm     1550:        if (~c->flags & CLIENT_REDRAWWINDOW) {
                   1551:                /*
                   1552:                 * If not redrawing the entire window, check whether each pane
                   1553:                 * needs to be redrawn.
                   1554:                 */
1.1       nicm     1555:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137     nicm     1556:                        if (wp->flags & PANE_REDRAW) {
                   1557:                                tty_update_mode(tty, tty->mode, NULL);
1.1       nicm     1558:                                screen_redraw_pane(c, wp);
1.137     nicm     1559:                        }
1.1       nicm     1560:                }
                   1561:        }
                   1562:
1.256     nicm     1563:        if (c->flags & CLIENT_ALLREDRAWFLAGS) {
                   1564:                if (options_get_number(s->options, "set-titles"))
                   1565:                        server_client_set_title(c);
                   1566:                screen_redraw_screen(c);
                   1567:        }
1.1       nicm     1568:
1.137     nicm     1569:        tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
                   1570:        tty_update_mode(tty, tty->mode, NULL);
1.1       nicm     1571:
1.256     nicm     1572:        c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE);
1.230     nicm     1573:
                   1574:        if (needed) {
                   1575:                /*
                   1576:                 * We would have deferred the redraw unless the output buffer
                   1577:                 * was empty, so we can record how many bytes the redraw
                   1578:                 * generated.
                   1579:                 */
                   1580:                c->redraw = EVBUFFER_LENGTH(tty->out);
                   1581:                log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
                   1582:        }
1.1       nicm     1583: }
                   1584:
                   1585: /* Set client title. */
1.190     nicm     1586: static void
1.1       nicm     1587: server_client_set_title(struct client *c)
                   1588: {
1.128     nicm     1589:        struct session          *s = c->session;
                   1590:        const char              *template;
                   1591:        char                    *title;
                   1592:        struct format_tree      *ft;
1.1       nicm     1593:
1.163     nicm     1594:        template = options_get_string(s->options, "set-titles-string");
1.25      nicm     1595:
1.228     nicm     1596:        ft = format_create(c, NULL, FORMAT_NONE, 0);
1.128     nicm     1597:        format_defaults(ft, c, NULL, NULL, NULL);
                   1598:
1.269     nicm     1599:        title = format_expand_time(ft, template);
1.1       nicm     1600:        if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77      nicm     1601:                free(c->title);
1.1       nicm     1602:                c->title = xstrdup(title);
                   1603:                tty_set_title(&c->tty, c->title);
                   1604:        }
1.77      nicm     1605:        free(title);
1.128     nicm     1606:
                   1607:        format_free(ft);
1.1       nicm     1608: }
                   1609:
                   1610: /* Dispatch message from client. */
1.190     nicm     1611: static void
1.162     nicm     1612: server_client_dispatch(struct imsg *imsg, void *arg)
1.1       nicm     1613: {
1.162     nicm     1614:        struct client           *c = arg;
1.73      nicm     1615:        struct msg_stdin_data    stdindata;
1.108     nicm     1616:        const char              *data;
1.162     nicm     1617:        ssize_t                  datalen;
1.149     nicm     1618:        struct session          *s;
1.1       nicm     1619:
1.162     nicm     1620:        if (c->flags & CLIENT_DEAD)
                   1621:                return;
                   1622:
                   1623:        if (imsg == NULL) {
                   1624:                server_client_lost(c);
                   1625:                return;
                   1626:        }
1.1       nicm     1627:
1.162     nicm     1628:        data = imsg->data;
                   1629:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1       nicm     1630:
1.162     nicm     1631:        switch (imsg->hdr.type) {
                   1632:        case MSG_IDENTIFY_FLAGS:
                   1633:        case MSG_IDENTIFY_TERM:
                   1634:        case MSG_IDENTIFY_TTYNAME:
                   1635:        case MSG_IDENTIFY_CWD:
                   1636:        case MSG_IDENTIFY_STDIN:
                   1637:        case MSG_IDENTIFY_ENVIRON:
                   1638:        case MSG_IDENTIFY_CLIENTPID:
                   1639:        case MSG_IDENTIFY_DONE:
                   1640:                server_client_dispatch_identify(c, imsg);
                   1641:                break;
                   1642:        case MSG_COMMAND:
                   1643:                server_client_dispatch_command(c, imsg);
                   1644:                break;
                   1645:        case MSG_STDIN:
                   1646:                if (datalen != sizeof stdindata)
                   1647:                        fatalx("bad MSG_STDIN size");
                   1648:                memcpy(&stdindata, data, sizeof stdindata);
1.36      nicm     1649:
1.162     nicm     1650:                if (c->stdin_callback == NULL)
1.33      nicm     1651:                        break;
1.162     nicm     1652:                if (stdindata.size <= 0)
                   1653:                        c->stdin_closed = 1;
                   1654:                else {
                   1655:                        evbuffer_add(c->stdin_data, stdindata.data,
                   1656:                            stdindata.size);
                   1657:                }
                   1658:                c->stdin_callback(c, c->stdin_closed,
                   1659:                    c->stdin_callback_data);
                   1660:                break;
                   1661:        case MSG_RESIZE:
                   1662:                if (datalen != 0)
                   1663:                        fatalx("bad MSG_RESIZE size");
1.1       nicm     1664:
1.162     nicm     1665:                if (c->flags & CLIENT_CONTROL)
1.1       nicm     1666:                        break;
1.236     nicm     1667:                tty_resize(&c->tty);
                   1668:                recalculate_sizes();
                   1669:                server_redraw_client(c);
1.174     nicm     1670:                if (c->session != NULL)
1.196     nicm     1671:                        notify_client("client-resized", c);
1.162     nicm     1672:                break;
                   1673:        case MSG_EXITING:
                   1674:                if (datalen != 0)
                   1675:                        fatalx("bad MSG_EXITING size");
1.1       nicm     1676:
1.162     nicm     1677:                c->session = NULL;
                   1678:                tty_close(&c->tty);
                   1679:                proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
                   1680:                break;
                   1681:        case MSG_WAKEUP:
                   1682:        case MSG_UNLOCK:
                   1683:                if (datalen != 0)
                   1684:                        fatalx("bad MSG_WAKEUP size");
1.121     nicm     1685:
1.162     nicm     1686:                if (!(c->flags & CLIENT_SUSPENDED))
                   1687:                        break;
                   1688:                c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm     1689:
1.162     nicm     1690:                if (c->tty.fd == -1) /* exited in the meantime */
1.1       nicm     1691:                        break;
1.162     nicm     1692:                s = c->session;
1.1       nicm     1693:
1.162     nicm     1694:                if (gettimeofday(&c->activity_time, NULL) != 0)
                   1695:                        fatal("gettimeofday failed");
                   1696:
                   1697:                tty_start_tty(&c->tty);
                   1698:                server_redraw_client(c);
                   1699:                recalculate_sizes();
1.184     nicm     1700:
                   1701:                if (s != NULL)
                   1702:                        session_update_activity(s, &c->activity_time);
1.162     nicm     1703:                break;
                   1704:        case MSG_SHELL:
                   1705:                if (datalen != 0)
                   1706:                        fatalx("bad MSG_SHELL size");
1.1       nicm     1707:
1.162     nicm     1708:                server_client_dispatch_shell(c);
                   1709:                break;
1.1       nicm     1710:        }
                   1711: }
                   1712:
1.194     nicm     1713: /* Callback when command is done. */
                   1714: static enum cmd_retval
1.195     nicm     1715: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194     nicm     1716: {
1.195     nicm     1717:        struct client   *c = item->client;
1.194     nicm     1718:
                   1719:        if (~c->flags & CLIENT_ATTACHED)
                   1720:                c->flags |= CLIENT_EXIT;
                   1721:        return (CMD_RETURN_NORMAL);
                   1722: }
                   1723:
                   1724: /* Show an error message. */
                   1725: static enum cmd_retval
1.195     nicm     1726: server_client_command_error(struct cmdq_item *item, void *data)
1.194     nicm     1727: {
                   1728:        char    *error = data;
                   1729:
1.195     nicm     1730:        cmdq_error(item, "%s", error);
1.194     nicm     1731:        free(error);
                   1732:
                   1733:        return (CMD_RETURN_NORMAL);
                   1734: }
                   1735:
1.1       nicm     1736: /* Handle command message. */
1.190     nicm     1737: static void
1.162     nicm     1738: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1       nicm     1739: {
1.108     nicm     1740:        struct msg_command_data   data;
                   1741:        char                     *buf;
                   1742:        size_t                    len;
                   1743:        struct cmd_list          *cmdlist = NULL;
                   1744:        int                       argc;
                   1745:        char                    **argv, *cause;
1.246     nicm     1746:
                   1747:        if (c->flags & CLIENT_EXIT)
                   1748:                return;
1.108     nicm     1749:
                   1750:        if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
                   1751:                fatalx("bad MSG_COMMAND size");
                   1752:        memcpy(&data, imsg->data, sizeof data);
                   1753:
1.124     nicm     1754:        buf = (char *)imsg->data + sizeof data;
1.108     nicm     1755:        len = imsg->hdr.len  - IMSG_HEADER_SIZE - sizeof data;
                   1756:        if (len > 0 && buf[len - 1] != '\0')
                   1757:                fatalx("bad MSG_COMMAND string");
                   1758:
                   1759:        argc = data.argc;
                   1760:        if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194     nicm     1761:                cause = xstrdup("command too long");
1.1       nicm     1762:                goto error;
                   1763:        }
                   1764:
                   1765:        if (argc == 0) {
                   1766:                argc = 1;
                   1767:                argv = xcalloc(1, sizeof *argv);
                   1768:                *argv = xstrdup("new-session");
                   1769:        }
                   1770:
1.94      nicm     1771:        if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1.1       nicm     1772:                cmd_free_argv(argc, argv);
                   1773:                goto error;
                   1774:        }
                   1775:        cmd_free_argv(argc, argv);
                   1776:
1.194     nicm     1777:        cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
                   1778:        cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.1       nicm     1779:        cmd_list_free(cmdlist);
                   1780:        return;
                   1781:
                   1782: error:
1.194     nicm     1783:        cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
                   1784:
1.1       nicm     1785:        if (cmdlist != NULL)
                   1786:                cmd_list_free(cmdlist);
1.88      nicm     1787:
1.36      nicm     1788:        c->flags |= CLIENT_EXIT;
1.1       nicm     1789: }
                   1790:
                   1791: /* Handle identify message. */
1.190     nicm     1792: static void
1.162     nicm     1793: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1       nicm     1794: {
1.165     nicm     1795:        const char      *data, *home;
1.232     nicm     1796:        size_t           datalen;
1.109     nicm     1797:        int              flags;
1.216     nicm     1798:        char            *name;
1.109     nicm     1799:
                   1800:        if (c->flags & CLIENT_IDENTIFIED)
                   1801:                fatalx("out-of-order identify message");
                   1802:
                   1803:        data = imsg->data;
                   1804:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
                   1805:
                   1806:        switch (imsg->hdr.type) {
                   1807:        case MSG_IDENTIFY_FLAGS:
                   1808:                if (datalen != sizeof flags)
                   1809:                        fatalx("bad MSG_IDENTIFY_FLAGS size");
                   1810:                memcpy(&flags, data, sizeof flags);
                   1811:                c->flags |= flags;
1.158     nicm     1812:                log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109     nicm     1813:                break;
                   1814:        case MSG_IDENTIFY_TERM:
1.110     nicm     1815:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1816:                        fatalx("bad MSG_IDENTIFY_TERM string");
                   1817:                c->term = xstrdup(data);
1.158     nicm     1818:                log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109     nicm     1819:                break;
                   1820:        case MSG_IDENTIFY_TTYNAME:
1.110     nicm     1821:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1822:                        fatalx("bad MSG_IDENTIFY_TTYNAME string");
                   1823:                c->ttyname = xstrdup(data);
1.158     nicm     1824:                log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109     nicm     1825:                break;
                   1826:        case MSG_IDENTIFY_CWD:
1.155     nicm     1827:                if (datalen == 0 || data[datalen - 1] != '\0')
                   1828:                        fatalx("bad MSG_IDENTIFY_CWD string");
1.165     nicm     1829:                if (access(data, X_OK) == 0)
                   1830:                        c->cwd = xstrdup(data);
                   1831:                else if ((home = find_home()) != NULL)
                   1832:                        c->cwd = xstrdup(home);
                   1833:                else
                   1834:                        c->cwd = xstrdup("/");
1.158     nicm     1835:                log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109     nicm     1836:                break;
                   1837:        case MSG_IDENTIFY_STDIN:
                   1838:                if (datalen != 0)
                   1839:                        fatalx("bad MSG_IDENTIFY_STDIN size");
                   1840:                c->fd = imsg->fd;
1.158     nicm     1841:                log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109     nicm     1842:                break;
                   1843:        case MSG_IDENTIFY_ENVIRON:
1.110     nicm     1844:                if (datalen == 0 || data[datalen - 1] != '\0')
1.109     nicm     1845:                        fatalx("bad MSG_IDENTIFY_ENVIRON string");
                   1846:                if (strchr(data, '=') != NULL)
1.164     nicm     1847:                        environ_put(c->environ, data);
1.158     nicm     1848:                log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143     nicm     1849:                break;
                   1850:        case MSG_IDENTIFY_CLIENTPID:
                   1851:                if (datalen != sizeof c->pid)
                   1852:                        fatalx("bad MSG_IDENTIFY_CLIENTPID size");
                   1853:                memcpy(&c->pid, data, sizeof c->pid);
1.158     nicm     1854:                log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109     nicm     1855:                break;
                   1856:        default:
                   1857:                break;
                   1858:        }
                   1859:
                   1860:        if (imsg->hdr.type != MSG_IDENTIFY_DONE)
                   1861:                return;
                   1862:        c->flags |= CLIENT_IDENTIFIED;
1.75      nicm     1863:
1.216     nicm     1864:        if (*c->ttyname != '\0')
                   1865:                name = xstrdup(c->ttyname);
                   1866:        else
                   1867:                xasprintf(&name, "client-%ld", (long)c->pid);
                   1868:        c->name = name;
                   1869:        log_debug("client %p name is %s", c, c->name);
                   1870:
1.109     nicm     1871:        if (c->flags & CLIENT_CONTROL) {
1.75      nicm     1872:                c->stdin_callback = control_callback;
1.109     nicm     1873:
1.97      nicm     1874:                evbuffer_free(c->stderr_data);
                   1875:                c->stderr_data = c->stdout_data;
1.109     nicm     1876:
                   1877:                if (c->flags & CLIENT_CONTROLCONTROL)
1.96      nicm     1878:                        evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162     nicm     1879:                proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75      nicm     1880:
                   1881:                c->tty.fd = -1;
                   1882:
1.109     nicm     1883:                close(c->fd);
                   1884:                c->fd = -1;
                   1885:
1.75      nicm     1886:                return;
                   1887:        }
1.1       nicm     1888:
1.109     nicm     1889:        if (c->fd == -1)
1.104     nicm     1890:                return;
1.145     nicm     1891:        if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109     nicm     1892:                close(c->fd);
                   1893:                c->fd = -1;
1.80      nicm     1894:                return;
                   1895:        }
1.109     nicm     1896:        if (c->flags & CLIENT_UTF8)
1.1       nicm     1897:                c->tty.flags |= TTY_UTF8;
1.109     nicm     1898:        if (c->flags & CLIENT_256COLOURS)
1.1       nicm     1899:                c->tty.term_flags |= TERM_256COLOURS;
                   1900:
                   1901:        tty_resize(&c->tty);
                   1902:
1.109     nicm     1903:        if (!(c->flags & CLIENT_CONTROL))
1.86      nicm     1904:                c->flags |= CLIENT_TERMINAL;
1.1       nicm     1905: }
                   1906:
                   1907: /* Handle shell message. */
1.190     nicm     1908: static void
1.162     nicm     1909: server_client_dispatch_shell(struct client *c)
1.1       nicm     1910: {
1.107     nicm     1911:        const char      *shell;
1.25      nicm     1912:
1.163     nicm     1913:        shell = options_get_string(global_s_options, "default-shell");
1.1       nicm     1914:        if (*shell == '\0' || areshell(shell))
                   1915:                shell = _PATH_BSHELL;
1.240     nicm     1916:        proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
1.25      nicm     1917:
1.162     nicm     1918:        proc_kill_peer(c->peer);
1.169     nicm     1919: }
                   1920:
                   1921: /* Event callback to push more stdout data if any left. */
                   1922: static void
1.170     nicm     1923: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1924: {
                   1925:        struct client   *c = arg;
                   1926:
                   1927:        if (~c->flags & CLIENT_DEAD)
                   1928:                server_client_push_stdout(c);
                   1929:        server_client_unref(c);
                   1930: }
                   1931:
                   1932: /* Push stdout to client if possible. */
                   1933: void
                   1934: server_client_push_stdout(struct client *c)
                   1935: {
                   1936:        struct msg_stdout_data data;
1.232     nicm     1937:        size_t                 sent, left;
1.169     nicm     1938:
                   1939:        left = EVBUFFER_LENGTH(c->stdout_data);
                   1940:        while (left != 0) {
                   1941:                sent = left;
                   1942:                if (sent > sizeof data.data)
                   1943:                        sent = sizeof data.data;
                   1944:                memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
                   1945:                data.size = sent;
                   1946:
                   1947:                if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
                   1948:                        break;
                   1949:                evbuffer_drain(c->stdout_data, sent);
                   1950:
                   1951:                left = EVBUFFER_LENGTH(c->stdout_data);
                   1952:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1953:                    sent, left);
                   1954:        }
                   1955:        if (left != 0) {
                   1956:                c->references++;
                   1957:                event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
                   1958:                log_debug("%s: client %p, queued", __func__, c);
                   1959:        }
                   1960: }
                   1961:
                   1962: /* Event callback to push more stderr data if any left. */
                   1963: static void
1.170     nicm     1964: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169     nicm     1965: {
                   1966:        struct client   *c = arg;
                   1967:
                   1968:        if (~c->flags & CLIENT_DEAD)
                   1969:                server_client_push_stderr(c);
                   1970:        server_client_unref(c);
                   1971: }
                   1972:
                   1973: /* Push stderr to client if possible. */
                   1974: void
                   1975: server_client_push_stderr(struct client *c)
                   1976: {
                   1977:        struct msg_stderr_data data;
1.232     nicm     1978:        size_t                 sent, left;
1.169     nicm     1979:
                   1980:        if (c->stderr_data == c->stdout_data) {
                   1981:                server_client_push_stdout(c);
                   1982:                return;
                   1983:        }
                   1984:
                   1985:        left = EVBUFFER_LENGTH(c->stderr_data);
                   1986:        while (left != 0) {
                   1987:                sent = left;
                   1988:                if (sent > sizeof data.data)
                   1989:                        sent = sizeof data.data;
                   1990:                memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
                   1991:                data.size = sent;
                   1992:
                   1993:                if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
                   1994:                        break;
                   1995:                evbuffer_drain(c->stderr_data, sent);
                   1996:
                   1997:                left = EVBUFFER_LENGTH(c->stderr_data);
                   1998:                log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
                   1999:                    sent, left);
                   2000:        }
                   2001:        if (left != 0) {
                   2002:                c->references++;
                   2003:                event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
                   2004:                log_debug("%s: client %p, queued", __func__, c);
1.212     nicm     2005:        }
                   2006: }
                   2007:
                   2008: /* Add to client message log. */
                   2009: void
                   2010: server_client_add_message(struct client *c, const char *fmt, ...)
                   2011: {
                   2012:        struct message_entry    *msg, *msg1;
                   2013:        char                    *s;
                   2014:        va_list                  ap;
                   2015:        u_int                    limit;
                   2016:
                   2017:        va_start(ap, fmt);
                   2018:        xvasprintf(&s, fmt, ap);
                   2019:        va_end(ap);
                   2020:
1.216     nicm     2021:        log_debug("message %s (client %p)", s, c);
1.212     nicm     2022:
                   2023:        msg = xcalloc(1, sizeof *msg);
                   2024:        msg->msg_time = time(NULL);
                   2025:        msg->msg_num = c->message_next++;
                   2026:        msg->msg = s;
                   2027:        TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
                   2028:
                   2029:        limit = options_get_number(global_options, "message-limit");
                   2030:        TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
                   2031:                if (msg->msg_num + limit >= c->message_next)
                   2032:                        break;
                   2033:                free(msg->msg);
                   2034:                TAILQ_REMOVE(&c->message_log, msg, entry);
                   2035:                free(msg);
1.169     nicm     2036:        }
1.213     nicm     2037: }
                   2038:
                   2039: /* Get client working directory. */
                   2040: const char *
1.250     nicm     2041: server_client_get_cwd(struct client *c, struct session *s)
1.213     nicm     2042: {
1.250     nicm     2043:        const char      *home;
1.213     nicm     2044:
1.265     nicm     2045:        if (!cfg_finished && cfg_client != NULL)
                   2046:                return (cfg_client->cwd);
1.213     nicm     2047:        if (c != NULL && c->session == NULL && c->cwd != NULL)
                   2048:                return (c->cwd);
1.250     nicm     2049:        if (s != NULL && s->cwd != NULL)
                   2050:                return (s->cwd);
1.213     nicm     2051:        if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
                   2052:                return (s->cwd);
1.250     nicm     2053:        if ((home = find_home()) != NULL)
                   2054:                return (home);
                   2055:        return ("/");
1.213     nicm     2056: }
                   2057:
                   2058: /* Resolve an absolute path or relative to client working directory. */
                   2059: char *
                   2060: server_client_get_path(struct client *c, const char *file)
                   2061: {
                   2062:        char    *path, resolved[PATH_MAX];
                   2063:
                   2064:        if (*file == '/')
                   2065:                path = xstrdup(file);
                   2066:        else
1.250     nicm     2067:                xasprintf(&path, "%s/%s", server_client_get_cwd(c, NULL), file);
1.213     nicm     2068:        if (realpath(path, resolved) == NULL)
                   2069:                return (path);
                   2070:        free(path);
                   2071:        return (xstrdup(resolved));
1.1       nicm     2072: }