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