Annotation of src/usr.bin/tmux/server-client.c, Revision 1.179
1.179 ! nicm 1: /* $OpenBSD: server-client.c,v 1.178 2015/12/12 18:32:24 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
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.168 nicm 35: void server_client_free(int, short, void *);
36: void server_client_check_focus(struct window_pane *);
37: void server_client_check_resize(struct window_pane *);
38: key_code server_client_check_mouse(struct client *);
39: void server_client_repeat_timer(int, short, void *);
40: void server_client_check_exit(struct client *);
41: void server_client_check_redraw(struct client *);
42: void server_client_set_title(struct client *);
43: void server_client_reset_state(struct client *);
44: int server_client_assume_paste(struct session *);
45:
46: void server_client_dispatch(struct imsg *, void *);
47: void server_client_dispatch_command(struct client *, struct imsg *);
48: void server_client_dispatch_identify(struct client *, struct imsg *);
49: void server_client_dispatch_shell(struct client *);
1.140 nicm 50:
51: /* Check if this client is inside this server. */
52: int
53: server_client_check_nested(struct client *c)
54: {
55: struct environ_entry *envent;
56: struct window_pane *wp;
57:
58: if (c->tty.path == NULL)
59: return (0);
60:
1.164 nicm 61: envent = environ_find(c->environ, "TMUX");
1.140 nicm 62: if (envent == NULL || *envent->value == '\0')
63: return (0);
64:
65: RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
66: if (strcmp(wp->tty, c->tty.path) == 0)
67: return (1);
68: }
69: return (0);
70: }
1.1 nicm 71:
1.132 nicm 72: /* Set client key table. */
73: void
1.178 nicm 74: server_client_set_key_table(struct client *c, const char *name)
1.132 nicm 75: {
1.178 nicm 76: if (name == NULL)
77: name = server_client_get_key_table(c);
78:
1.132 nicm 79: key_bindings_unref_table(c->keytable);
80: c->keytable = key_bindings_get_table(name, 1);
81: c->keytable->references++;
82: }
83:
1.178 nicm 84: /* Get default key table. */
85: const char *
86: server_client_get_key_table(struct client *c)
87: {
88: struct session *s = c->session;
89: const char *name;
90:
91: if (s == NULL)
92: return ("root");
93:
94: name = options_get_string(s->options, "key-table");
95: if (*name == '\0')
96: return ("root");
97: return (name);
98: }
99:
1.1 nicm 100: /* Create a new client. */
101: void
102: server_client_create(int fd)
103: {
104: struct client *c;
105:
1.49 nicm 106: setblocking(fd, 0);
1.1 nicm 107:
108: c = xcalloc(1, sizeof *c);
1.141 nicm 109: c->references = 1;
1.162 nicm 110: c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25 nicm 111:
1.10 nicm 112: if (gettimeofday(&c->creation_time, NULL) != 0)
1.1 nicm 113: fatal("gettimeofday failed");
1.11 nicm 114: memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111 nicm 115:
1.164 nicm 116: c->environ = environ_create();
1.1 nicm 117:
1.146 nicm 118: c->fd = -1;
1.165 nicm 119: c->cwd = NULL;
1.145 nicm 120:
1.94 nicm 121: c->cmdq = cmdq_new(c);
122: c->cmdq->client_exit = 1;
123:
1.116 nicm 124: c->stdin_data = evbuffer_new();
125: c->stdout_data = evbuffer_new();
126: c->stderr_data = evbuffer_new();
1.33 nicm 127:
1.1 nicm 128: c->tty.fd = -1;
129: c->title = NULL;
130:
131: c->session = NULL;
1.45 nicm 132: c->last_session = NULL;
1.1 nicm 133: c->tty.sx = 80;
134: c->tty.sy = 24;
135:
136: screen_init(&c->status, c->tty.sx, 1, 0);
137:
138: c->message_string = NULL;
1.136 nicm 139: TAILQ_INIT(&c->message_log);
1.1 nicm 140:
141: c->prompt_string = NULL;
142: c->prompt_buffer = NULL;
143: c->prompt_index = 0;
144:
1.93 nicm 145: c->flags |= CLIENT_FOCUSED;
146:
1.132 nicm 147: c->keytable = key_bindings_get_table("root", 1);
148: c->keytable->references++;
149:
1.17 nicm 150: evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
151:
1.135 nicm 152: TAILQ_INSERT_TAIL(&clients, c, entry);
1.157 nicm 153: log_debug("new client %p", c);
1.72 nicm 154: }
155:
156: /* Open client terminal if needed. */
157: int
1.119 nicm 158: server_client_open(struct client *c, char **cause)
1.72 nicm 159: {
1.75 nicm 160: if (c->flags & CLIENT_CONTROL)
161: return (0);
1.120 nicm 162:
163: if (strcmp(c->ttyname, "/dev/tty") == 0) {
164: *cause = xstrdup("can't use /dev/tty");
165: return (-1);
166: }
1.75 nicm 167:
1.72 nicm 168: if (!(c->flags & CLIENT_TERMINAL)) {
1.116 nicm 169: *cause = xstrdup("not a terminal");
1.72 nicm 170: return (-1);
171: }
172:
1.119 nicm 173: if (tty_open(&c->tty, cause) != 0)
1.72 nicm 174: return (-1);
175:
176: return (0);
1.1 nicm 177: }
178:
179: /* Lost a client. */
180: void
181: server_client_lost(struct client *c)
182: {
1.136 nicm 183: struct message_entry *msg, *msg1;
1.1 nicm 184:
1.141 nicm 185: c->flags |= CLIENT_DEAD;
186:
187: status_prompt_clear(c);
188: status_message_clear(c);
189:
190: if (c->stdin_callback != NULL)
191: c->stdin_callback(c, 1, c->stdin_callback_data);
192:
1.135 nicm 193: TAILQ_REMOVE(&clients, c, entry);
1.157 nicm 194: log_debug("lost client %p", c);
1.1 nicm 195:
196: /*
197: * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
198: * and tty_free might close an unrelated fd.
199: */
200: if (c->flags & CLIENT_TERMINAL)
201: tty_free(&c->tty);
1.109 nicm 202: free(c->ttyname);
203: free(c->term);
1.1 nicm 204:
1.113 nicm 205: evbuffer_free(c->stdin_data);
206: evbuffer_free(c->stdout_data);
1.97 nicm 207: if (c->stderr_data != c->stdout_data)
1.126 nicm 208: evbuffer_free(c->stderr_data);
1.33 nicm 209:
1.148 nicm 210: if (event_initialized(&c->status_timer))
211: evtimer_del(&c->status_timer);
1.1 nicm 212: screen_free(&c->status);
213:
1.77 nicm 214: free(c->title);
1.165 nicm 215: free((void *)c->cwd);
1.1 nicm 216:
1.17 nicm 217: evtimer_del(&c->repeat_timer);
218:
1.132 nicm 219: key_bindings_unref_table(c->keytable);
220:
1.69 nicm 221: if (event_initialized(&c->identify_timer))
222: evtimer_del(&c->identify_timer);
1.15 nicm 223:
1.77 nicm 224: free(c->message_string);
1.116 nicm 225: if (event_initialized(&c->message_timer))
1.69 nicm 226: evtimer_del(&c->message_timer);
1.136 nicm 227: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1.77 nicm 228: free(msg->msg);
1.136 nicm 229: TAILQ_REMOVE(&c->message_log, msg, entry);
230: free(msg);
1.21 nicm 231: }
1.1 nicm 232:
1.77 nicm 233: free(c->prompt_string);
234: free(c->prompt_buffer);
1.61 nicm 235:
1.154 nicm 236: c->cmdq->flags |= CMD_Q_DEAD;
1.94 nicm 237: cmdq_free(c->cmdq);
238: c->cmdq = NULL;
239:
1.164 nicm 240: environ_free(c->environ);
1.1 nicm 241:
1.162 nicm 242: proc_remove_peer(c->peer);
243: c->peer = NULL;
1.13 nicm 244:
1.142 nicm 245: server_client_unref(c);
1.71 nicm 246:
247: server_add_accept(0); /* may be more file descriptors now */
1.1 nicm 248:
249: recalculate_sizes();
1.41 nicm 250: server_check_unattached();
1.19 nicm 251: server_update_socket();
1.141 nicm 252: }
253:
254: /* Remove reference from a client. */
255: void
1.142 nicm 256: server_client_unref(struct client *c)
1.141 nicm 257: {
1.157 nicm 258: log_debug("unref client %p (%d references)", c, c->references);
1.141 nicm 259:
260: c->references--;
261: if (c->references == 0)
262: event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
263: }
264:
265: /* Free dead client. */
266: void
1.170 nicm 267: server_client_free(__unused int fd, __unused short events, void *arg)
1.141 nicm 268: {
269: struct client *c = arg;
270:
1.157 nicm 271: log_debug("free client %p (%d references)", c, c->references);
1.141 nicm 272:
273: if (c->references == 0)
274: free(c);
1.9 nicm 275: }
276:
1.174 nicm 277: /* Detach a client. */
278: void
279: server_client_detach(struct client *c, enum msgtype msgtype)
280: {
281: struct session *s = c->session;
282:
283: if (s == NULL)
284: return;
285:
1.179 ! nicm 286: hooks_run(c->session->hooks, c, "client-detached");
1.174 nicm 287: proc_send_s(c->peer, msgtype, s->name);
288: }
289:
1.66 nicm 290: /* Check for mouse keys. */
1.168 nicm 291: key_code
1.131 nicm 292: server_client_check_mouse(struct client *c)
1.66 nicm 293: {
1.131 nicm 294: struct session *s = c->session;
295: struct mouse_event *m = &c->tty.mouse;
296: struct window *w;
297: struct window_pane *wp;
298: enum { NOTYPE, DOWN, UP, DRAG, WHEEL } type = NOTYPE;
299: enum { NOWHERE, PANE, STATUS, BORDER } where = NOWHERE;
300: u_int x, y, b;
1.168 nicm 301: key_code key;
1.131 nicm 302:
303: log_debug("mouse %02x at %u,%u (last %u,%u) (%d)", m->b, m->x, m->y,
304: m->lx, m->ly, c->tty.mouse_drag_flag);
305:
306: /* What type of event is this? */
307: if (MOUSE_DRAG(m->b)) {
308: type = DRAG;
309: if (c->tty.mouse_drag_flag) {
310: x = m->x, y = m->y, b = m->b;
311: log_debug("drag update at %u,%u", x, y);
312: } else {
313: x = m->lx, y = m->ly, b = m->lb;
314: log_debug("drag start at %u,%u", x, y);
315: }
316: } else if (MOUSE_WHEEL(m->b)) {
317: type = WHEEL;
318: x = m->x, y = m->y, b = m->b;
319: log_debug("wheel at %u,%u", x, y);
320: } else if (MOUSE_BUTTONS(m->b) == 3) {
321: type = UP;
322: x = m->x, y = m->y, b = m->lb;
323: log_debug("up at %u,%u", x, y);
324: } else {
325: type = DOWN;
326: x = m->x, y = m->y, b = m->b;
327: log_debug("down at %u,%u", x, y);
328: }
329: if (type == NOTYPE)
1.177 nicm 330: return (KEYC_UNKNOWN);
1.131 nicm 331:
332: /* Always save the session. */
333: m->s = s->id;
334:
335: /* Is this on the status line? */
336: m->statusat = status_at_line(c);
337: if (m->statusat != -1 && y == (u_int)m->statusat) {
338: w = status_get_window_at(c, x);
339: if (w == NULL)
1.177 nicm 340: return (KEYC_UNKNOWN);
1.131 nicm 341: m->w = w->id;
342: where = STATUS;
343: } else
344: m->w = -1;
345:
346: /* Not on status line. Adjust position and check for border or pane. */
347: if (where == NOWHERE) {
348: if (m->statusat == 0 && y > 0)
349: y--;
350: else if (m->statusat > 0 && y >= (u_int)m->statusat)
351: y = m->statusat - 1;
352:
353: TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
354: if ((wp->xoff + wp->sx == x &&
355: wp->yoff <= 1 + y &&
356: wp->yoff + wp->sy >= y) ||
357: (wp->yoff + wp->sy == y &&
358: wp->xoff <= 1 + x &&
359: wp->xoff + wp->sx >= x))
360: break;
361: }
362: if (wp != NULL)
363: where = BORDER;
364: else {
365: wp = window_get_active_at(s->curw->window, x, y);
1.173 nicm 366: if (wp != NULL) {
1.131 nicm 367: where = PANE;
1.173 nicm 368: log_debug("mouse at %u,%u is on pane %%%u",
369: x, y, wp->id);
370: }
1.131 nicm 371: }
372: if (where == NOWHERE)
1.177 nicm 373: return (KEYC_UNKNOWN);
1.131 nicm 374: m->wp = wp->id;
375: m->w = wp->window->id;
376: } else
377: m->wp = -1;
378:
379: /* Stop dragging if needed. */
380: if (type != DRAG && c->tty.mouse_drag_flag) {
381: if (c->tty.mouse_drag_release != NULL)
382: c->tty.mouse_drag_release(c, m);
383:
384: c->tty.mouse_drag_update = NULL;
385: c->tty.mouse_drag_release = NULL;
386:
387: c->tty.mouse_drag_flag = 0;
1.133 nicm 388: return (KEYC_MOUSE); /* not a key, but still may want to pass */
1.131 nicm 389: }
390:
391: /* Convert to a key binding. */
1.177 nicm 392: key = KEYC_UNKNOWN;
1.131 nicm 393: switch (type) {
394: case NOTYPE:
395: break;
396: case DRAG:
397: if (c->tty.mouse_drag_update != NULL)
398: c->tty.mouse_drag_update(c, m);
399: else {
400: switch (MOUSE_BUTTONS(b)) {
401: case 0:
402: if (where == PANE)
403: key = KEYC_MOUSEDRAG1_PANE;
404: if (where == STATUS)
405: key = KEYC_MOUSEDRAG1_STATUS;
406: if (where == BORDER)
407: key = KEYC_MOUSEDRAG1_BORDER;
408: break;
409: case 1:
410: if (where == PANE)
411: key = KEYC_MOUSEDRAG2_PANE;
412: if (where == STATUS)
413: key = KEYC_MOUSEDRAG2_STATUS;
414: if (where == BORDER)
415: key = KEYC_MOUSEDRAG2_BORDER;
416: break;
417: case 2:
418: if (where == PANE)
419: key = KEYC_MOUSEDRAG3_PANE;
420: if (where == STATUS)
421: key = KEYC_MOUSEDRAG3_STATUS;
422: if (where == BORDER)
423: key = KEYC_MOUSEDRAG3_BORDER;
424: break;
425: }
426: }
1.66 nicm 427:
1.131 nicm 428: c->tty.mouse_drag_flag = 1;
429: break;
430: case WHEEL:
431: if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
432: if (where == PANE)
433: key = KEYC_WHEELUP_PANE;
434: if (where == STATUS)
435: key = KEYC_WHEELUP_STATUS;
436: if (where == BORDER)
437: key = KEYC_WHEELUP_BORDER;
438: } else {
439: if (where == PANE)
440: key = KEYC_WHEELDOWN_PANE;
441: if (where == STATUS)
442: key = KEYC_WHEELDOWN_STATUS;
443: if (where == BORDER)
444: key = KEYC_WHEELDOWN_BORDER;
445: }
446: break;
447: case UP:
448: switch (MOUSE_BUTTONS(b)) {
449: case 0:
450: if (where == PANE)
451: key = KEYC_MOUSEUP1_PANE;
452: if (where == STATUS)
453: key = KEYC_MOUSEUP1_STATUS;
454: if (where == BORDER)
455: key = KEYC_MOUSEUP1_BORDER;
456: break;
457: case 1:
458: if (where == PANE)
459: key = KEYC_MOUSEUP2_PANE;
460: if (where == STATUS)
461: key = KEYC_MOUSEUP2_STATUS;
462: if (where == BORDER)
463: key = KEYC_MOUSEUP2_BORDER;
464: break;
465: case 2:
466: if (where == PANE)
467: key = KEYC_MOUSEUP3_PANE;
468: if (where == STATUS)
469: key = KEYC_MOUSEUP3_STATUS;
470: if (where == BORDER)
471: key = KEYC_MOUSEUP3_BORDER;
472: break;
473: }
474: break;
475: case DOWN:
476: switch (MOUSE_BUTTONS(b)) {
477: case 0:
478: if (where == PANE)
479: key = KEYC_MOUSEDOWN1_PANE;
480: if (where == STATUS)
481: key = KEYC_MOUSEDOWN1_STATUS;
482: if (where == BORDER)
483: key = KEYC_MOUSEDOWN1_BORDER;
484: break;
485: case 1:
486: if (where == PANE)
487: key = KEYC_MOUSEDOWN2_PANE;
488: if (where == STATUS)
489: key = KEYC_MOUSEDOWN2_STATUS;
490: if (where == BORDER)
491: key = KEYC_MOUSEDOWN2_BORDER;
492: break;
493: case 2:
494: if (where == PANE)
495: key = KEYC_MOUSEDOWN3_PANE;
496: if (where == STATUS)
497: key = KEYC_MOUSEDOWN3_STATUS;
498: if (where == BORDER)
499: key = KEYC_MOUSEDOWN3_BORDER;
500: break;
1.66 nicm 501: }
1.131 nicm 502: break;
1.66 nicm 503: }
1.177 nicm 504: if (key == KEYC_UNKNOWN)
505: return (KEYC_UNKNOWN);
1.66 nicm 506:
1.131 nicm 507: /* Apply modifiers if any. */
508: if (b & MOUSE_MASK_META)
509: key |= KEYC_ESCAPE;
510: if (b & MOUSE_MASK_CTRL)
511: key |= KEYC_CTRL;
512: if (b & MOUSE_MASK_SHIFT)
513: key |= KEYC_SHIFT;
1.66 nicm 514:
1.131 nicm 515: return (key);
1.66 nicm 516: }
517:
1.82 nicm 518: /* Is this fast enough to probably be a paste? */
519: int
520: server_client_assume_paste(struct session *s)
521: {
522: struct timeval tv;
1.84 nicm 523: int t;
1.82 nicm 524:
1.163 nicm 525: if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83 nicm 526: return (0);
1.82 nicm 527:
528: timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171 nicm 529: if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
530: log_debug("session %s pasting (flag %d)", s->name,
531: !!(s->flags & SESSION_PASTING));
532: if (s->flags & SESSION_PASTING)
533: return (1);
534: s->flags |= SESSION_PASTING;
535: return (0);
536: }
537: log_debug("session %s not pasting", s->name);
538: s->flags &= ~SESSION_PASTING;
1.83 nicm 539: return (0);
1.82 nicm 540: }
541:
1.18 nicm 542: /* Handle data key input from client. */
543: void
1.168 nicm 544: server_client_handle_key(struct client *c, key_code key)
1.18 nicm 545: {
1.131 nicm 546: struct mouse_event *m = &c->tty.mouse;
1.132 nicm 547: struct session *s = c->session;
1.18 nicm 548: struct window *w;
549: struct window_pane *wp;
550: struct timeval tv;
1.156 nicm 551: struct key_table *table;
1.132 nicm 552: struct key_binding bd_find, *bd;
553: int xtimeout;
1.18 nicm 554:
555: /* Check the client is good to accept input. */
1.132 nicm 556: if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.18 nicm 557: return;
1.132 nicm 558: w = s->curw->window;
1.18 nicm 559:
560: /* Update the activity timer. */
561: if (gettimeofday(&c->activity_time, NULL) != 0)
562: fatal("gettimeofday failed");
1.149 nicm 563: session_update_activity(s, &c->activity_time);
1.18 nicm 564:
1.132 nicm 565: /* Number keys jump to pane in identify mode. */
1.25 nicm 566: if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30 nicm 567: if (c->flags & CLIENT_READONLY)
568: return;
1.95 nicm 569: window_unzoom(w);
1.18 nicm 570: wp = window_pane_at_index(w, key - '0');
571: if (wp != NULL && window_pane_visible(wp))
572: window_set_active_pane(w, wp);
573: server_clear_identify(c);
574: return;
575: }
576:
577: /* Handle status line. */
1.30 nicm 578: if (!(c->flags & CLIENT_READONLY)) {
579: status_message_clear(c);
580: server_clear_identify(c);
581: }
1.18 nicm 582: if (c->prompt_string != NULL) {
1.30 nicm 583: if (!(c->flags & CLIENT_READONLY))
584: status_prompt_key(c, key);
1.18 nicm 585: return;
586: }
587:
588: /* Check for mouse keys. */
589: if (key == KEYC_MOUSE) {
1.30 nicm 590: if (c->flags & CLIENT_READONLY)
591: return;
1.131 nicm 592: key = server_client_check_mouse(c);
1.177 nicm 593: if (key == KEYC_UNKNOWN)
1.131 nicm 594: return;
595:
596: m->valid = 1;
597: m->key = key;
598:
1.163 nicm 599: if (!options_get_number(s->options, "mouse"))
1.161 nicm 600: goto forward;
1.131 nicm 601: } else
602: m->valid = 0;
1.18 nicm 603:
1.132 nicm 604: /* Treat everything as a regular key when pasting is detected. */
1.161 nicm 605: if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
606: goto forward;
1.18 nicm 607:
1.132 nicm 608: retry:
609: /* Try to see if there is a key binding in the current table. */
610: bd_find.key = key;
1.156 nicm 611: bd = RB_FIND(key_bindings, &c->keytable->key_bindings, &bd_find);
1.132 nicm 612: if (bd != NULL) {
613: /*
614: * Key was matched in this table. If currently repeating but a
615: * non-repeating binding was found, stop repeating and try
616: * again in the root table.
617: */
618: if ((c->flags & CLIENT_REPEAT) && !bd->can_repeat) {
1.178 nicm 619: server_client_set_key_table(c, NULL);
1.132 nicm 620: c->flags &= ~CLIENT_REPEAT;
1.85 nicm 621: server_status_client(c);
1.132 nicm 622: goto retry;
1.18 nicm 623: }
1.82 nicm 624:
1.132 nicm 625: /*
626: * Take a reference to this table to make sure the key binding
627: * doesn't disappear.
628: */
1.156 nicm 629: table = c->keytable;
1.132 nicm 630: table->references++;
631:
632: /*
633: * If this is a repeating key, start the timer. Otherwise reset
634: * the client back to the root table.
635: */
1.163 nicm 636: xtimeout = options_get_number(s->options, "repeat-time");
1.132 nicm 637: if (xtimeout != 0 && bd->can_repeat) {
638: c->flags |= CLIENT_REPEAT;
639:
640: tv.tv_sec = xtimeout / 1000;
641: tv.tv_usec = (xtimeout % 1000) * 1000L;
642: evtimer_del(&c->repeat_timer);
643: evtimer_add(&c->repeat_timer, &tv);
644: } else {
1.18 nicm 645: c->flags &= ~CLIENT_REPEAT;
1.178 nicm 646: server_client_set_key_table(c, NULL);
1.18 nicm 647: }
1.132 nicm 648: server_status_client(c);
649:
650: /* Dispatch the key binding. */
651: key_bindings_dispatch(bd, c, m);
652: key_bindings_unref_table(table);
1.18 nicm 653: return;
654: }
655:
1.132 nicm 656: /*
657: * No match in this table. If repeating, switch the client back to the
658: * root table and try again.
659: */
660: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 661: server_client_set_key_table(c, NULL);
1.18 nicm 662: c->flags &= ~CLIENT_REPEAT;
1.132 nicm 663: server_status_client(c);
664: goto retry;
1.18 nicm 665: }
666:
1.132 nicm 667: /* If no match and we're not in the root table, that's it. */
1.178 nicm 668: if (strcmp(c->keytable->name, server_client_get_key_table(c)) != 0) {
669: server_client_set_key_table(c, NULL);
1.132 nicm 670: server_status_client(c);
671: return;
1.18 nicm 672: }
673:
1.132 nicm 674: /*
675: * No match, but in the root table. Prefix switches to the prefix table
676: * and everything else is passed through.
677: */
1.168 nicm 678: if (key == (key_code)options_get_number(s->options, "prefix") ||
679: key == (key_code)options_get_number(s->options, "prefix2")) {
1.178 nicm 680: server_client_set_key_table(c, "prefix");
1.132 nicm 681: server_status_client(c);
1.161 nicm 682: return;
683: }
684:
685: forward:
686: if (c->flags & CLIENT_READONLY)
687: return;
688: if (KEYC_IS_MOUSE(key))
689: wp = cmd_mouse_pane(m, NULL, NULL);
690: else
691: wp = w->active;
692: if (wp != NULL)
1.132 nicm 693: window_pane_key(wp, c, s, key, m);
1.18 nicm 694: }
695:
1.2 nicm 696: /* Client functions that need to happen every loop. */
697: void
698: server_client_loop(void)
699: {
700: struct client *c;
701: struct window *w;
702: struct window_pane *wp;
703:
1.135 nicm 704: TAILQ_FOREACH(c, &clients, entry) {
1.36 nicm 705: server_client_check_exit(c);
706: if (c->session != NULL) {
707: server_client_check_redraw(c);
708: server_client_reset_state(c);
709: }
1.2 nicm 710: }
711:
712: /*
713: * Any windows will have been redrawn as part of clients, so clear
1.92 nicm 714: * their flags now. Also check pane focus and resize.
1.2 nicm 715: */
1.134 nicm 716: RB_FOREACH(w, windows, &windows) {
1.2 nicm 717: w->flags &= ~WINDOW_REDRAW;
1.91 nicm 718: TAILQ_FOREACH(wp, &w->panes, entry) {
1.101 nicm 719: if (wp->fd != -1) {
720: server_client_check_focus(wp);
721: server_client_check_resize(wp);
722: }
1.2 nicm 723: wp->flags &= ~PANE_REDRAW;
1.91 nicm 724: }
1.150 nicm 725: check_window_name(w);
1.2 nicm 726: }
1.92 nicm 727: }
728:
729: /* Check if pane should be resized. */
730: void
731: server_client_check_resize(struct window_pane *wp)
732: {
733: struct winsize ws;
734:
1.101 nicm 735: if (!(wp->flags & PANE_RESIZE))
1.92 nicm 736: return;
737:
738: memset(&ws, 0, sizeof ws);
739: ws.ws_col = wp->sx;
740: ws.ws_row = wp->sy;
741:
1.100 nicm 742: if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92 nicm 743: fatal("ioctl failed");
744:
745: wp->flags &= ~PANE_RESIZE;
1.91 nicm 746: }
747:
748: /* Check whether pane should be focused. */
749: void
750: server_client_check_focus(struct window_pane *wp)
751: {
1.93 nicm 752: struct client *c;
1.102 nicm 753: int push;
1.103 nicm 754:
755: /* Are focus events off? */
1.163 nicm 756: if (!options_get_number(global_options, "focus-events"))
1.103 nicm 757: return;
1.102 nicm 758:
759: /* Do we need to push the focus state? */
760: push = wp->flags & PANE_FOCUSPUSH;
761: wp->flags &= ~PANE_FOCUSPUSH;
1.91 nicm 762:
763: /* If we don't care about focus, forget it. */
764: if (!(wp->base.mode & MODE_FOCUSON))
765: return;
766:
767: /* If we're not the active pane in our window, we're not focused. */
768: if (wp->window->active != wp)
769: goto not_focused;
770:
771: /* If we're in a mode, we're not focused. */
772: if (wp->screen != &wp->base)
773: goto not_focused;
774:
775: /*
1.93 nicm 776: * If our window is the current window in any focused clients with an
777: * attached session, we're focused.
1.91 nicm 778: */
1.135 nicm 779: TAILQ_FOREACH(c, &clients, entry) {
780: if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91 nicm 781: continue;
1.93 nicm 782: if (c->session->flags & SESSION_UNATTACHED)
783: continue;
784:
785: if (c->session->curw->window == wp->window)
1.91 nicm 786: goto focused;
787: }
788:
789: not_focused:
1.102 nicm 790: if (push || (wp->flags & PANE_FOCUSED))
1.91 nicm 791: bufferevent_write(wp->event, "\033[O", 3);
792: wp->flags &= ~PANE_FOCUSED;
793: return;
794:
795: focused:
1.102 nicm 796: if (push || !(wp->flags & PANE_FOCUSED))
1.91 nicm 797: bufferevent_write(wp->event, "\033[I", 3);
798: wp->flags |= PANE_FOCUSED;
1.2 nicm 799: }
800:
1.18 nicm 801: /*
802: * Update cursor position and mode settings. The scroll region and attributes
803: * are cleared when idle (waiting for an event) as this is the most likely time
804: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
805: * compromise between excessive resets and likelihood of an interrupt.
806: *
807: * tty_region/tty_reset/tty_update_mode already take care of not resetting
808: * things that are already in their default state.
809: */
1.1 nicm 810: void
1.18 nicm 811: server_client_reset_state(struct client *c)
1.1 nicm 812: {
1.18 nicm 813: struct window *w = c->session->curw->window;
814: struct window_pane *wp = w->active;
815: struct screen *s = wp->screen;
1.163 nicm 816: struct options *oo = c->session->options;
1.66 nicm 817: int status, mode, o;
1.60 nicm 818:
819: if (c->flags & CLIENT_SUSPENDED)
820: return;
1.1 nicm 821:
1.86 nicm 822: if (c->flags & CLIENT_CONTROL)
823: return;
824:
1.1 nicm 825: tty_region(&c->tty, 0, c->tty.sy - 1);
826:
827: status = options_get_number(oo, "status");
828: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
829: tty_cursor(&c->tty, 0, 0);
1.66 nicm 830: else {
1.126 nicm 831: o = status && options_get_number(oo, "status-position") == 0;
1.66 nicm 832: tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
833: }
1.1 nicm 834:
1.50 nicm 835: /*
1.131 nicm 836: * Set mouse mode if requested. To support dragging, always use button
837: * mode.
1.57 nicm 838: */
839: mode = s->mode;
1.131 nicm 840: if (options_get_number(oo, "mouse"))
841: mode = (mode & ~ALL_MOUSE_MODES) | MODE_MOUSE_BUTTON;
1.48 nicm 842:
843: /* Set the terminal mode and reset attributes. */
1.59 nicm 844: tty_update_mode(&c->tty, mode, s);
1.1 nicm 845: tty_reset(&c->tty);
1.17 nicm 846: }
847:
848: /* Repeat time callback. */
849: void
1.170 nicm 850: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17 nicm 851: {
852: struct client *c = data;
853:
1.85 nicm 854: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 855: server_client_set_key_table(c, NULL);
1.132 nicm 856: c->flags &= ~CLIENT_REPEAT;
857: server_status_client(c);
1.85 nicm 858: }
1.1 nicm 859: }
860:
1.36 nicm 861: /* Check if client should be exited. */
862: void
863: server_client_check_exit(struct client *c)
864: {
865: if (!(c->flags & CLIENT_EXIT))
866: return;
867:
1.73 nicm 868: if (EVBUFFER_LENGTH(c->stdin_data) != 0)
869: return;
870: if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36 nicm 871: return;
1.73 nicm 872: if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36 nicm 873: return;
874:
1.162 nicm 875: proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36 nicm 876: c->flags &= ~CLIENT_EXIT;
1.38 nicm 877: }
878:
1.1 nicm 879: /* Check for client redraws. */
880: void
881: server_client_check_redraw(struct client *c)
882: {
883: struct session *s = c->session;
1.137 nicm 884: struct tty *tty = &c->tty;
1.1 nicm 885: struct window_pane *wp;
886: int flags, redraw;
887:
1.86 nicm 888: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 889: return;
890:
1.1 nicm 891: if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.163 nicm 892: if (options_get_number(s->options, "set-titles"))
1.1 nicm 893: server_client_set_title(c);
1.12 nicm 894:
1.1 nicm 895: if (c->message_string != NULL)
896: redraw = status_message_redraw(c);
897: else if (c->prompt_string != NULL)
898: redraw = status_prompt_redraw(c);
899: else
900: redraw = status_redraw(c);
901: if (!redraw)
902: c->flags &= ~CLIENT_STATUS;
903: }
904:
1.137 nicm 905: flags = tty->flags & (TTY_FREEZE|TTY_NOCURSOR);
906: tty->flags = (tty->flags & ~TTY_FREEZE) | TTY_NOCURSOR;
907:
1.1 nicm 908: if (c->flags & CLIENT_REDRAW) {
1.137 nicm 909: tty_update_mode(tty, tty->mode, NULL);
1.115 nicm 910: screen_redraw_screen(c, 1, 1, 1);
1.27 nicm 911: c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38 nicm 912: } else if (c->flags & CLIENT_REDRAWWINDOW) {
1.137 nicm 913: tty_update_mode(tty, tty->mode, NULL);
1.38 nicm 914: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
915: screen_redraw_pane(c, wp);
916: c->flags &= ~CLIENT_REDRAWWINDOW;
1.1 nicm 917: } else {
918: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137 nicm 919: if (wp->flags & PANE_REDRAW) {
920: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 921: screen_redraw_pane(c, wp);
1.137 nicm 922: }
1.1 nicm 923: }
924: }
925:
1.137 nicm 926: if (c->flags & CLIENT_BORDERS) {
927: tty_update_mode(tty, tty->mode, NULL);
1.115 nicm 928: screen_redraw_screen(c, 0, 0, 1);
1.137 nicm 929: }
1.27 nicm 930:
1.137 nicm 931: if (c->flags & CLIENT_STATUS) {
932: tty_update_mode(tty, tty->mode, NULL);
1.115 nicm 933: screen_redraw_screen(c, 0, 1, 0);
1.137 nicm 934: }
1.1 nicm 935:
1.137 nicm 936: tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
937: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 938:
1.153 nicm 939: c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS|
940: CLIENT_STATUSFORCE);
1.1 nicm 941: }
942:
943: /* Set client title. */
944: void
945: server_client_set_title(struct client *c)
946: {
1.128 nicm 947: struct session *s = c->session;
948: const char *template;
949: char *title;
950: struct format_tree *ft;
1.1 nicm 951:
1.163 nicm 952: template = options_get_string(s->options, "set-titles-string");
1.25 nicm 953:
1.176 nicm 954: ft = format_create(NULL, 0);
1.128 nicm 955: format_defaults(ft, c, NULL, NULL, NULL);
956:
957: title = format_expand_time(ft, template, time(NULL));
1.1 nicm 958: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 959: free(c->title);
1.1 nicm 960: c->title = xstrdup(title);
961: tty_set_title(&c->tty, c->title);
962: }
1.77 nicm 963: free(title);
1.128 nicm 964:
965: format_free(ft);
1.1 nicm 966: }
967:
968: /* Dispatch message from client. */
1.162 nicm 969: void
970: server_client_dispatch(struct imsg *imsg, void *arg)
1.1 nicm 971: {
1.162 nicm 972: struct client *c = arg;
1.73 nicm 973: struct msg_stdin_data stdindata;
1.108 nicm 974: const char *data;
1.162 nicm 975: ssize_t datalen;
1.149 nicm 976: struct session *s;
1.1 nicm 977:
1.162 nicm 978: if (c->flags & CLIENT_DEAD)
979: return;
980:
981: if (imsg == NULL) {
982: server_client_lost(c);
983: return;
984: }
1.1 nicm 985:
1.162 nicm 986: data = imsg->data;
987: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 988:
1.162 nicm 989: switch (imsg->hdr.type) {
990: case MSG_IDENTIFY_FLAGS:
991: case MSG_IDENTIFY_TERM:
992: case MSG_IDENTIFY_TTYNAME:
993: case MSG_IDENTIFY_CWD:
994: case MSG_IDENTIFY_STDIN:
995: case MSG_IDENTIFY_ENVIRON:
996: case MSG_IDENTIFY_CLIENTPID:
997: case MSG_IDENTIFY_DONE:
998: server_client_dispatch_identify(c, imsg);
999: break;
1000: case MSG_COMMAND:
1001: server_client_dispatch_command(c, imsg);
1002: break;
1003: case MSG_STDIN:
1004: if (datalen != sizeof stdindata)
1005: fatalx("bad MSG_STDIN size");
1006: memcpy(&stdindata, data, sizeof stdindata);
1.36 nicm 1007:
1.162 nicm 1008: if (c->stdin_callback == NULL)
1.33 nicm 1009: break;
1.162 nicm 1010: if (stdindata.size <= 0)
1011: c->stdin_closed = 1;
1012: else {
1013: evbuffer_add(c->stdin_data, stdindata.data,
1014: stdindata.size);
1015: }
1016: c->stdin_callback(c, c->stdin_closed,
1017: c->stdin_callback_data);
1018: break;
1019: case MSG_RESIZE:
1020: if (datalen != 0)
1021: fatalx("bad MSG_RESIZE size");
1.1 nicm 1022:
1.162 nicm 1023: if (c->flags & CLIENT_CONTROL)
1.1 nicm 1024: break;
1.162 nicm 1025: if (tty_resize(&c->tty)) {
1026: recalculate_sizes();
1027: server_redraw_client(c);
1028: }
1.174 nicm 1029: if (c->session != NULL)
1.179 ! nicm 1030: hooks_run(c->session->hooks, c, "client-resized");
1.162 nicm 1031: break;
1032: case MSG_EXITING:
1033: if (datalen != 0)
1034: fatalx("bad MSG_EXITING size");
1.1 nicm 1035:
1.162 nicm 1036: c->session = NULL;
1037: tty_close(&c->tty);
1038: proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
1039: break;
1040: case MSG_WAKEUP:
1041: case MSG_UNLOCK:
1042: if (datalen != 0)
1043: fatalx("bad MSG_WAKEUP size");
1.121 nicm 1044:
1.162 nicm 1045: if (!(c->flags & CLIENT_SUSPENDED))
1046: break;
1047: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 1048:
1.162 nicm 1049: if (c->tty.fd == -1) /* exited in the meantime */
1.1 nicm 1050: break;
1.162 nicm 1051: s = c->session;
1.1 nicm 1052:
1.162 nicm 1053: if (gettimeofday(&c->activity_time, NULL) != 0)
1054: fatal("gettimeofday failed");
1055: if (s != NULL)
1056: session_update_activity(s, &c->activity_time);
1057:
1058: tty_start_tty(&c->tty);
1059: server_redraw_client(c);
1060: recalculate_sizes();
1061: break;
1062: case MSG_SHELL:
1063: if (datalen != 0)
1064: fatalx("bad MSG_SHELL size");
1.1 nicm 1065:
1.162 nicm 1066: server_client_dispatch_shell(c);
1067: break;
1.1 nicm 1068: }
1069: }
1070:
1071: /* Handle command message. */
1072: void
1.162 nicm 1073: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1 nicm 1074: {
1.108 nicm 1075: struct msg_command_data data;
1076: char *buf;
1077: size_t len;
1078: struct cmd_list *cmdlist = NULL;
1079: int argc;
1080: char **argv, *cause;
1081:
1082: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
1083: fatalx("bad MSG_COMMAND size");
1084: memcpy(&data, imsg->data, sizeof data);
1085:
1.124 nicm 1086: buf = (char *)imsg->data + sizeof data;
1.108 nicm 1087: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
1088: if (len > 0 && buf[len - 1] != '\0')
1089: fatalx("bad MSG_COMMAND string");
1090:
1091: argc = data.argc;
1092: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.94 nicm 1093: cmdq_error(c->cmdq, "command too long");
1.1 nicm 1094: goto error;
1095: }
1096:
1097: if (argc == 0) {
1098: argc = 1;
1099: argv = xcalloc(1, sizeof *argv);
1100: *argv = xstrdup("new-session");
1101: }
1102:
1.94 nicm 1103: if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1104: cmdq_error(c->cmdq, "%s", cause);
1.1 nicm 1105: cmd_free_argv(argc, argv);
1106: goto error;
1107: }
1108: cmd_free_argv(argc, argv);
1109:
1.113 nicm 1110: if (c != cfg_client || cfg_finished)
1.131 nicm 1111: cmdq_run(c->cmdq, cmdlist, NULL);
1.113 nicm 1112: else
1.131 nicm 1113: cmdq_append(c->cmdq, cmdlist, NULL);
1.1 nicm 1114: cmd_list_free(cmdlist);
1115: return;
1116:
1117: error:
1118: if (cmdlist != NULL)
1119: cmd_list_free(cmdlist);
1.88 nicm 1120:
1.36 nicm 1121: c->flags |= CLIENT_EXIT;
1.1 nicm 1122: }
1123:
1124: /* Handle identify message. */
1125: void
1.162 nicm 1126: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1 nicm 1127: {
1.165 nicm 1128: const char *data, *home;
1.109 nicm 1129: size_t datalen;
1130: int flags;
1131:
1132: if (c->flags & CLIENT_IDENTIFIED)
1133: fatalx("out-of-order identify message");
1134:
1135: data = imsg->data;
1136: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1137:
1138: switch (imsg->hdr.type) {
1139: case MSG_IDENTIFY_FLAGS:
1140: if (datalen != sizeof flags)
1141: fatalx("bad MSG_IDENTIFY_FLAGS size");
1142: memcpy(&flags, data, sizeof flags);
1143: c->flags |= flags;
1.158 nicm 1144: log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109 nicm 1145: break;
1146: case MSG_IDENTIFY_TERM:
1.110 nicm 1147: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1148: fatalx("bad MSG_IDENTIFY_TERM string");
1149: c->term = xstrdup(data);
1.158 nicm 1150: log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109 nicm 1151: break;
1152: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 1153: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1154: fatalx("bad MSG_IDENTIFY_TTYNAME string");
1155: c->ttyname = xstrdup(data);
1.158 nicm 1156: log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109 nicm 1157: break;
1158: case MSG_IDENTIFY_CWD:
1.155 nicm 1159: if (datalen == 0 || data[datalen - 1] != '\0')
1160: fatalx("bad MSG_IDENTIFY_CWD string");
1.165 nicm 1161: if (access(data, X_OK) == 0)
1162: c->cwd = xstrdup(data);
1163: else if ((home = find_home()) != NULL)
1164: c->cwd = xstrdup(home);
1165: else
1166: c->cwd = xstrdup("/");
1.158 nicm 1167: log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109 nicm 1168: break;
1169: case MSG_IDENTIFY_STDIN:
1170: if (datalen != 0)
1171: fatalx("bad MSG_IDENTIFY_STDIN size");
1172: c->fd = imsg->fd;
1.158 nicm 1173: log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109 nicm 1174: break;
1175: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 1176: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1177: fatalx("bad MSG_IDENTIFY_ENVIRON string");
1178: if (strchr(data, '=') != NULL)
1.164 nicm 1179: environ_put(c->environ, data);
1.158 nicm 1180: log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143 nicm 1181: break;
1182: case MSG_IDENTIFY_CLIENTPID:
1183: if (datalen != sizeof c->pid)
1184: fatalx("bad MSG_IDENTIFY_CLIENTPID size");
1185: memcpy(&c->pid, data, sizeof c->pid);
1.158 nicm 1186: log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109 nicm 1187: break;
1188: default:
1189: break;
1190: }
1191:
1192: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
1193: return;
1194: c->flags |= CLIENT_IDENTIFIED;
1.75 nicm 1195:
1.109 nicm 1196: if (c->flags & CLIENT_CONTROL) {
1.75 nicm 1197: c->stdin_callback = control_callback;
1.109 nicm 1198:
1.97 nicm 1199: evbuffer_free(c->stderr_data);
1200: c->stderr_data = c->stdout_data;
1.109 nicm 1201:
1202: if (c->flags & CLIENT_CONTROLCONTROL)
1.96 nicm 1203: evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162 nicm 1204: proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75 nicm 1205:
1206: c->tty.fd = -1;
1207:
1.109 nicm 1208: close(c->fd);
1209: c->fd = -1;
1210:
1.75 nicm 1211: return;
1212: }
1.1 nicm 1213:
1.109 nicm 1214: if (c->fd == -1)
1.104 nicm 1215: return;
1.145 nicm 1216: if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109 nicm 1217: close(c->fd);
1218: c->fd = -1;
1.80 nicm 1219: return;
1220: }
1.109 nicm 1221: if (c->flags & CLIENT_UTF8)
1.1 nicm 1222: c->tty.flags |= TTY_UTF8;
1.109 nicm 1223: if (c->flags & CLIENT_256COLOURS)
1.1 nicm 1224: c->tty.term_flags |= TERM_256COLOURS;
1225:
1226: tty_resize(&c->tty);
1227:
1.109 nicm 1228: if (!(c->flags & CLIENT_CONTROL))
1.86 nicm 1229: c->flags |= CLIENT_TERMINAL;
1.1 nicm 1230: }
1231:
1232: /* Handle shell message. */
1233: void
1.162 nicm 1234: server_client_dispatch_shell(struct client *c)
1.1 nicm 1235: {
1.107 nicm 1236: const char *shell;
1.25 nicm 1237:
1.163 nicm 1238: shell = options_get_string(global_s_options, "default-shell");
1.1 nicm 1239: if (*shell == '\0' || areshell(shell))
1240: shell = _PATH_BSHELL;
1.162 nicm 1241: proc_send_s(c->peer, MSG_SHELL, shell);
1.25 nicm 1242:
1.162 nicm 1243: proc_kill_peer(c->peer);
1.169 nicm 1244: }
1245:
1246: /* Event callback to push more stdout data if any left. */
1247: static void
1.170 nicm 1248: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1249: {
1250: struct client *c = arg;
1251:
1252: if (~c->flags & CLIENT_DEAD)
1253: server_client_push_stdout(c);
1254: server_client_unref(c);
1255: }
1256:
1257: /* Push stdout to client if possible. */
1258: void
1259: server_client_push_stdout(struct client *c)
1260: {
1261: struct msg_stdout_data data;
1262: size_t sent, left;
1263:
1264: left = EVBUFFER_LENGTH(c->stdout_data);
1265: while (left != 0) {
1266: sent = left;
1267: if (sent > sizeof data.data)
1268: sent = sizeof data.data;
1269: memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
1270: data.size = sent;
1271:
1272: if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
1273: break;
1274: evbuffer_drain(c->stdout_data, sent);
1275:
1276: left = EVBUFFER_LENGTH(c->stdout_data);
1277: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1278: sent, left);
1279: }
1280: if (left != 0) {
1281: c->references++;
1282: event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
1283: log_debug("%s: client %p, queued", __func__, c);
1284: }
1285: }
1286:
1287: /* Event callback to push more stderr data if any left. */
1288: static void
1.170 nicm 1289: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1290: {
1291: struct client *c = arg;
1292:
1293: if (~c->flags & CLIENT_DEAD)
1294: server_client_push_stderr(c);
1295: server_client_unref(c);
1296: }
1297:
1298: /* Push stderr to client if possible. */
1299: void
1300: server_client_push_stderr(struct client *c)
1301: {
1302: struct msg_stderr_data data;
1303: size_t sent, left;
1304:
1305: if (c->stderr_data == c->stdout_data) {
1306: server_client_push_stdout(c);
1307: return;
1308: }
1309:
1310: left = EVBUFFER_LENGTH(c->stderr_data);
1311: while (left != 0) {
1312: sent = left;
1313: if (sent > sizeof data.data)
1314: sent = sizeof data.data;
1315: memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
1316: data.size = sent;
1317:
1318: if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
1319: break;
1320: evbuffer_drain(c->stderr_data, sent);
1321:
1322: left = EVBUFFER_LENGTH(c->stderr_data);
1323: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1324: sent, left);
1325: }
1326: if (left != 0) {
1327: c->references++;
1328: event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
1329: log_debug("%s: client %p, queued", __func__, c);
1330: }
1.1 nicm 1331: }