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