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