Annotation of src/usr.bin/tmux/server-client.c, Revision 1.254
1.254 ! nicm 1: /* $OpenBSD: server-client.c,v 1.253 2018/07/17 18:02:40 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.93 nicm 1176: if (c->session->flags & SESSION_UNATTACHED)
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.232 nicm 1315: int needed, flags, masked;
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.218 nicm 1322:
1323: /*
1324: * If there is outstanding data, defer the redraw until it has been
1325: * consumed. We can just add a timer to get out of the event loop and
1326: * end up back here.
1327: */
1328: needed = 0;
1329: if (c->flags & CLIENT_REDRAW)
1330: needed = 1;
1331: else {
1332: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1333: if (wp->flags & PANE_REDRAW) {
1334: needed = 1;
1335: break;
1336: }
1337: }
1338: }
1.241 nicm 1339: if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
1340: log_debug("%s: redraw deferred (%zu left)", c->name, left);
1341: if (!evtimer_initialized(&ev))
1342: evtimer_set(&ev, server_client_redraw_timer, NULL);
1343: if (!evtimer_pending(&ev, NULL)) {
1.218 nicm 1344: log_debug("redraw timer started");
1345: evtimer_add(&ev, &tv);
1.241 nicm 1346: }
1.218 nicm 1347:
1.241 nicm 1348: /*
1349: * We may have got here for a single pane redraw, but force a
1350: * full redraw next time in case other panes have been updated.
1351: */
1352: c->flags |= CLIENT_REDRAW;
1353: return;
1354: } else if (needed)
1.218 nicm 1355: log_debug("%s: redraw needed", c->name);
1.79 nicm 1356:
1.1 nicm 1357: if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.163 nicm 1358: if (options_get_number(s->options, "set-titles"))
1.1 nicm 1359: server_client_set_title(c);
1.189 nicm 1360: screen_redraw_update(c); /* will adjust flags */
1.1 nicm 1361: }
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.1 nicm 1366: if (c->flags & CLIENT_REDRAW) {
1.137 nicm 1367: tty_update_mode(tty, tty->mode, NULL);
1.115 nicm 1368: screen_redraw_screen(c, 1, 1, 1);
1.27 nicm 1369: c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.1 nicm 1370: } else {
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.185 nicm 1379: masked = c->flags & (CLIENT_BORDERS|CLIENT_STATUS);
1380: if (masked != 0)
1.137 nicm 1381: tty_update_mode(tty, tty->mode, NULL);
1.185 nicm 1382: if (masked == CLIENT_BORDERS)
1.115 nicm 1383: screen_redraw_screen(c, 0, 0, 1);
1.185 nicm 1384: else if (masked == CLIENT_STATUS)
1.115 nicm 1385: screen_redraw_screen(c, 0, 1, 0);
1.185 nicm 1386: else if (masked != 0)
1387: screen_redraw_screen(c, 0, 1, 1);
1.1 nicm 1388:
1.137 nicm 1389: tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
1390: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1391:
1.153 nicm 1392: c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS|
1393: CLIENT_STATUSFORCE);
1.230 nicm 1394:
1395: if (needed) {
1396: /*
1397: * We would have deferred the redraw unless the output buffer
1398: * was empty, so we can record how many bytes the redraw
1399: * generated.
1400: */
1401: c->redraw = EVBUFFER_LENGTH(tty->out);
1402: log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
1403: }
1.1 nicm 1404: }
1405:
1406: /* Set client title. */
1.190 nicm 1407: static void
1.1 nicm 1408: server_client_set_title(struct client *c)
1409: {
1.128 nicm 1410: struct session *s = c->session;
1411: const char *template;
1412: char *title;
1413: struct format_tree *ft;
1.1 nicm 1414:
1.163 nicm 1415: template = options_get_string(s->options, "set-titles-string");
1.25 nicm 1416:
1.228 nicm 1417: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.128 nicm 1418: format_defaults(ft, c, NULL, NULL, NULL);
1419:
1420: title = format_expand_time(ft, template, time(NULL));
1.1 nicm 1421: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 1422: free(c->title);
1.1 nicm 1423: c->title = xstrdup(title);
1424: tty_set_title(&c->tty, c->title);
1425: }
1.77 nicm 1426: free(title);
1.128 nicm 1427:
1428: format_free(ft);
1.1 nicm 1429: }
1430:
1431: /* Dispatch message from client. */
1.190 nicm 1432: static void
1.162 nicm 1433: server_client_dispatch(struct imsg *imsg, void *arg)
1.1 nicm 1434: {
1.162 nicm 1435: struct client *c = arg;
1.73 nicm 1436: struct msg_stdin_data stdindata;
1.108 nicm 1437: const char *data;
1.162 nicm 1438: ssize_t datalen;
1.149 nicm 1439: struct session *s;
1.1 nicm 1440:
1.162 nicm 1441: if (c->flags & CLIENT_DEAD)
1442: return;
1443:
1444: if (imsg == NULL) {
1445: server_client_lost(c);
1446: return;
1447: }
1.1 nicm 1448:
1.162 nicm 1449: data = imsg->data;
1450: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 1451:
1.162 nicm 1452: switch (imsg->hdr.type) {
1453: case MSG_IDENTIFY_FLAGS:
1454: case MSG_IDENTIFY_TERM:
1455: case MSG_IDENTIFY_TTYNAME:
1456: case MSG_IDENTIFY_CWD:
1457: case MSG_IDENTIFY_STDIN:
1458: case MSG_IDENTIFY_ENVIRON:
1459: case MSG_IDENTIFY_CLIENTPID:
1460: case MSG_IDENTIFY_DONE:
1461: server_client_dispatch_identify(c, imsg);
1462: break;
1463: case MSG_COMMAND:
1464: server_client_dispatch_command(c, imsg);
1465: break;
1466: case MSG_STDIN:
1467: if (datalen != sizeof stdindata)
1468: fatalx("bad MSG_STDIN size");
1469: memcpy(&stdindata, data, sizeof stdindata);
1.36 nicm 1470:
1.162 nicm 1471: if (c->stdin_callback == NULL)
1.33 nicm 1472: break;
1.162 nicm 1473: if (stdindata.size <= 0)
1474: c->stdin_closed = 1;
1475: else {
1476: evbuffer_add(c->stdin_data, stdindata.data,
1477: stdindata.size);
1478: }
1479: c->stdin_callback(c, c->stdin_closed,
1480: c->stdin_callback_data);
1481: break;
1482: case MSG_RESIZE:
1483: if (datalen != 0)
1484: fatalx("bad MSG_RESIZE size");
1.1 nicm 1485:
1.162 nicm 1486: if (c->flags & CLIENT_CONTROL)
1.1 nicm 1487: break;
1.236 nicm 1488: tty_resize(&c->tty);
1489: recalculate_sizes();
1490: server_redraw_client(c);
1.174 nicm 1491: if (c->session != NULL)
1.196 nicm 1492: notify_client("client-resized", c);
1.162 nicm 1493: break;
1494: case MSG_EXITING:
1495: if (datalen != 0)
1496: fatalx("bad MSG_EXITING size");
1.1 nicm 1497:
1.162 nicm 1498: c->session = NULL;
1499: tty_close(&c->tty);
1500: proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
1501: break;
1502: case MSG_WAKEUP:
1503: case MSG_UNLOCK:
1504: if (datalen != 0)
1505: fatalx("bad MSG_WAKEUP size");
1.121 nicm 1506:
1.162 nicm 1507: if (!(c->flags & CLIENT_SUSPENDED))
1508: break;
1509: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 1510:
1.162 nicm 1511: if (c->tty.fd == -1) /* exited in the meantime */
1.1 nicm 1512: break;
1.162 nicm 1513: s = c->session;
1.1 nicm 1514:
1.162 nicm 1515: if (gettimeofday(&c->activity_time, NULL) != 0)
1516: fatal("gettimeofday failed");
1517:
1518: tty_start_tty(&c->tty);
1519: server_redraw_client(c);
1520: recalculate_sizes();
1.184 nicm 1521:
1522: if (s != NULL)
1523: session_update_activity(s, &c->activity_time);
1.162 nicm 1524: break;
1525: case MSG_SHELL:
1526: if (datalen != 0)
1527: fatalx("bad MSG_SHELL size");
1.1 nicm 1528:
1.162 nicm 1529: server_client_dispatch_shell(c);
1530: break;
1.1 nicm 1531: }
1532: }
1533:
1.194 nicm 1534: /* Callback when command is done. */
1535: static enum cmd_retval
1.195 nicm 1536: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194 nicm 1537: {
1.195 nicm 1538: struct client *c = item->client;
1.194 nicm 1539:
1540: if (~c->flags & CLIENT_ATTACHED)
1541: c->flags |= CLIENT_EXIT;
1542: return (CMD_RETURN_NORMAL);
1543: }
1544:
1545: /* Show an error message. */
1546: static enum cmd_retval
1.195 nicm 1547: server_client_command_error(struct cmdq_item *item, void *data)
1.194 nicm 1548: {
1549: char *error = data;
1550:
1.195 nicm 1551: cmdq_error(item, "%s", error);
1.194 nicm 1552: free(error);
1553:
1554: return (CMD_RETURN_NORMAL);
1555: }
1556:
1.1 nicm 1557: /* Handle command message. */
1.190 nicm 1558: static void
1.162 nicm 1559: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1 nicm 1560: {
1.108 nicm 1561: struct msg_command_data data;
1562: char *buf;
1563: size_t len;
1564: struct cmd_list *cmdlist = NULL;
1565: int argc;
1566: char **argv, *cause;
1.246 nicm 1567:
1568: if (c->flags & CLIENT_EXIT)
1569: return;
1.108 nicm 1570:
1571: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
1572: fatalx("bad MSG_COMMAND size");
1573: memcpy(&data, imsg->data, sizeof data);
1574:
1.124 nicm 1575: buf = (char *)imsg->data + sizeof data;
1.108 nicm 1576: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
1577: if (len > 0 && buf[len - 1] != '\0')
1578: fatalx("bad MSG_COMMAND string");
1579:
1580: argc = data.argc;
1581: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194 nicm 1582: cause = xstrdup("command too long");
1.1 nicm 1583: goto error;
1584: }
1585:
1586: if (argc == 0) {
1587: argc = 1;
1588: argv = xcalloc(1, sizeof *argv);
1589: *argv = xstrdup("new-session");
1590: }
1591:
1.94 nicm 1592: if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1.1 nicm 1593: cmd_free_argv(argc, argv);
1594: goto error;
1595: }
1596: cmd_free_argv(argc, argv);
1597:
1.194 nicm 1598: cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
1599: cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.1 nicm 1600: cmd_list_free(cmdlist);
1601: return;
1602:
1603: error:
1.194 nicm 1604: cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
1605:
1.1 nicm 1606: if (cmdlist != NULL)
1607: cmd_list_free(cmdlist);
1.88 nicm 1608:
1.36 nicm 1609: c->flags |= CLIENT_EXIT;
1.1 nicm 1610: }
1611:
1612: /* Handle identify message. */
1.190 nicm 1613: static void
1.162 nicm 1614: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1 nicm 1615: {
1.165 nicm 1616: const char *data, *home;
1.232 nicm 1617: size_t datalen;
1.109 nicm 1618: int flags;
1.216 nicm 1619: char *name;
1.109 nicm 1620:
1621: if (c->flags & CLIENT_IDENTIFIED)
1622: fatalx("out-of-order identify message");
1623:
1624: data = imsg->data;
1625: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1626:
1627: switch (imsg->hdr.type) {
1628: case MSG_IDENTIFY_FLAGS:
1629: if (datalen != sizeof flags)
1630: fatalx("bad MSG_IDENTIFY_FLAGS size");
1631: memcpy(&flags, data, sizeof flags);
1632: c->flags |= flags;
1.158 nicm 1633: log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109 nicm 1634: break;
1635: case MSG_IDENTIFY_TERM:
1.110 nicm 1636: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1637: fatalx("bad MSG_IDENTIFY_TERM string");
1638: c->term = xstrdup(data);
1.158 nicm 1639: log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109 nicm 1640: break;
1641: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 1642: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1643: fatalx("bad MSG_IDENTIFY_TTYNAME string");
1644: c->ttyname = xstrdup(data);
1.158 nicm 1645: log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109 nicm 1646: break;
1647: case MSG_IDENTIFY_CWD:
1.155 nicm 1648: if (datalen == 0 || data[datalen - 1] != '\0')
1649: fatalx("bad MSG_IDENTIFY_CWD string");
1.165 nicm 1650: if (access(data, X_OK) == 0)
1651: c->cwd = xstrdup(data);
1652: else if ((home = find_home()) != NULL)
1653: c->cwd = xstrdup(home);
1654: else
1655: c->cwd = xstrdup("/");
1.158 nicm 1656: log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109 nicm 1657: break;
1658: case MSG_IDENTIFY_STDIN:
1659: if (datalen != 0)
1660: fatalx("bad MSG_IDENTIFY_STDIN size");
1661: c->fd = imsg->fd;
1.158 nicm 1662: log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109 nicm 1663: break;
1664: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 1665: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1666: fatalx("bad MSG_IDENTIFY_ENVIRON string");
1667: if (strchr(data, '=') != NULL)
1.164 nicm 1668: environ_put(c->environ, data);
1.158 nicm 1669: log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143 nicm 1670: break;
1671: case MSG_IDENTIFY_CLIENTPID:
1672: if (datalen != sizeof c->pid)
1673: fatalx("bad MSG_IDENTIFY_CLIENTPID size");
1674: memcpy(&c->pid, data, sizeof c->pid);
1.158 nicm 1675: log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109 nicm 1676: break;
1677: default:
1678: break;
1679: }
1680:
1681: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
1682: return;
1683: c->flags |= CLIENT_IDENTIFIED;
1.75 nicm 1684:
1.216 nicm 1685: if (*c->ttyname != '\0')
1686: name = xstrdup(c->ttyname);
1687: else
1688: xasprintf(&name, "client-%ld", (long)c->pid);
1689: c->name = name;
1690: log_debug("client %p name is %s", c, c->name);
1691:
1.109 nicm 1692: if (c->flags & CLIENT_CONTROL) {
1.75 nicm 1693: c->stdin_callback = control_callback;
1.109 nicm 1694:
1.97 nicm 1695: evbuffer_free(c->stderr_data);
1696: c->stderr_data = c->stdout_data;
1.109 nicm 1697:
1698: if (c->flags & CLIENT_CONTROLCONTROL)
1.96 nicm 1699: evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162 nicm 1700: proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75 nicm 1701:
1702: c->tty.fd = -1;
1703:
1.109 nicm 1704: close(c->fd);
1705: c->fd = -1;
1706:
1.75 nicm 1707: return;
1708: }
1.1 nicm 1709:
1.109 nicm 1710: if (c->fd == -1)
1.104 nicm 1711: return;
1.145 nicm 1712: if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109 nicm 1713: close(c->fd);
1714: c->fd = -1;
1.80 nicm 1715: return;
1716: }
1.109 nicm 1717: if (c->flags & CLIENT_UTF8)
1.1 nicm 1718: c->tty.flags |= TTY_UTF8;
1.109 nicm 1719: if (c->flags & CLIENT_256COLOURS)
1.1 nicm 1720: c->tty.term_flags |= TERM_256COLOURS;
1721:
1722: tty_resize(&c->tty);
1723:
1.109 nicm 1724: if (!(c->flags & CLIENT_CONTROL))
1.86 nicm 1725: c->flags |= CLIENT_TERMINAL;
1.1 nicm 1726: }
1727:
1728: /* Handle shell message. */
1.190 nicm 1729: static void
1.162 nicm 1730: server_client_dispatch_shell(struct client *c)
1.1 nicm 1731: {
1.107 nicm 1732: const char *shell;
1.25 nicm 1733:
1.163 nicm 1734: shell = options_get_string(global_s_options, "default-shell");
1.1 nicm 1735: if (*shell == '\0' || areshell(shell))
1736: shell = _PATH_BSHELL;
1.240 nicm 1737: proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
1.25 nicm 1738:
1.162 nicm 1739: proc_kill_peer(c->peer);
1.169 nicm 1740: }
1741:
1742: /* Event callback to push more stdout data if any left. */
1743: static void
1.170 nicm 1744: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1745: {
1746: struct client *c = arg;
1747:
1748: if (~c->flags & CLIENT_DEAD)
1749: server_client_push_stdout(c);
1750: server_client_unref(c);
1751: }
1752:
1753: /* Push stdout to client if possible. */
1754: void
1755: server_client_push_stdout(struct client *c)
1756: {
1757: struct msg_stdout_data data;
1.232 nicm 1758: size_t sent, left;
1.169 nicm 1759:
1760: left = EVBUFFER_LENGTH(c->stdout_data);
1761: while (left != 0) {
1762: sent = left;
1763: if (sent > sizeof data.data)
1764: sent = sizeof data.data;
1765: memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
1766: data.size = sent;
1767:
1768: if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
1769: break;
1770: evbuffer_drain(c->stdout_data, sent);
1771:
1772: left = EVBUFFER_LENGTH(c->stdout_data);
1773: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1774: sent, left);
1775: }
1776: if (left != 0) {
1777: c->references++;
1778: event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
1779: log_debug("%s: client %p, queued", __func__, c);
1780: }
1781: }
1782:
1783: /* Event callback to push more stderr data if any left. */
1784: static void
1.170 nicm 1785: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1786: {
1787: struct client *c = arg;
1788:
1789: if (~c->flags & CLIENT_DEAD)
1790: server_client_push_stderr(c);
1791: server_client_unref(c);
1792: }
1793:
1794: /* Push stderr to client if possible. */
1795: void
1796: server_client_push_stderr(struct client *c)
1797: {
1798: struct msg_stderr_data data;
1.232 nicm 1799: size_t sent, left;
1.169 nicm 1800:
1801: if (c->stderr_data == c->stdout_data) {
1802: server_client_push_stdout(c);
1803: return;
1804: }
1805:
1806: left = EVBUFFER_LENGTH(c->stderr_data);
1807: while (left != 0) {
1808: sent = left;
1809: if (sent > sizeof data.data)
1810: sent = sizeof data.data;
1811: memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
1812: data.size = sent;
1813:
1814: if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
1815: break;
1816: evbuffer_drain(c->stderr_data, sent);
1817:
1818: left = EVBUFFER_LENGTH(c->stderr_data);
1819: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1820: sent, left);
1821: }
1822: if (left != 0) {
1823: c->references++;
1824: event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
1825: log_debug("%s: client %p, queued", __func__, c);
1.212 nicm 1826: }
1827: }
1828:
1829: /* Add to client message log. */
1830: void
1831: server_client_add_message(struct client *c, const char *fmt, ...)
1832: {
1833: struct message_entry *msg, *msg1;
1834: char *s;
1835: va_list ap;
1836: u_int limit;
1837:
1838: va_start(ap, fmt);
1839: xvasprintf(&s, fmt, ap);
1840: va_end(ap);
1841:
1.216 nicm 1842: log_debug("message %s (client %p)", s, c);
1.212 nicm 1843:
1844: msg = xcalloc(1, sizeof *msg);
1845: msg->msg_time = time(NULL);
1846: msg->msg_num = c->message_next++;
1847: msg->msg = s;
1848: TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
1849:
1850: limit = options_get_number(global_options, "message-limit");
1851: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1852: if (msg->msg_num + limit >= c->message_next)
1853: break;
1854: free(msg->msg);
1855: TAILQ_REMOVE(&c->message_log, msg, entry);
1856: free(msg);
1.169 nicm 1857: }
1.213 nicm 1858: }
1859:
1860: /* Get client working directory. */
1861: const char *
1.250 nicm 1862: server_client_get_cwd(struct client *c, struct session *s)
1.213 nicm 1863: {
1.250 nicm 1864: const char *home;
1.213 nicm 1865:
1866: if (c != NULL && c->session == NULL && c->cwd != NULL)
1867: return (c->cwd);
1.250 nicm 1868: if (s != NULL && s->cwd != NULL)
1869: return (s->cwd);
1.213 nicm 1870: if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
1871: return (s->cwd);
1.250 nicm 1872: if ((home = find_home()) != NULL)
1873: return (home);
1874: return ("/");
1.213 nicm 1875: }
1876:
1877: /* Resolve an absolute path or relative to client working directory. */
1878: char *
1879: server_client_get_path(struct client *c, const char *file)
1880: {
1881: char *path, resolved[PATH_MAX];
1882:
1883: if (*file == '/')
1884: path = xstrdup(file);
1885: else
1.250 nicm 1886: xasprintf(&path, "%s/%s", server_client_get_cwd(c, NULL), file);
1.213 nicm 1887: if (realpath(path, resolved) == NULL)
1888: return (path);
1889: free(path);
1890: return (xstrdup(resolved));
1.1 nicm 1891: }