Annotation of src/usr.bin/tmux/server-client.c, Revision 1.69
1.69 ! nicm 1: /* $OpenBSD: server-client.c,v 1.68 2012/03/09 21:42:13 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>
20:
1.12 nicm 21: #include <event.h>
1.1 nicm 22: #include <fcntl.h>
23: #include <string.h>
1.4 nicm 24: #include <time.h>
1.1 nicm 25: #include <paths.h>
26: #include <unistd.h>
27:
28: #include "tmux.h"
29:
1.66 nicm 30: void server_client_check_mouse(struct client *c,
31: struct window_pane *wp, struct mouse_event *mouse);
1.18 nicm 32: void server_client_handle_key(int, struct mouse_event *, void *);
1.17 nicm 33: void server_client_repeat_timer(int, short, void *);
1.36 nicm 34: void server_client_check_exit(struct client *);
1.1 nicm 35: void server_client_check_redraw(struct client *);
36: void server_client_set_title(struct client *);
1.18 nicm 37: void server_client_reset_state(struct client *);
1.36 nicm 38: void server_client_in_callback(struct bufferevent *, short, void *);
39: void server_client_out_callback(struct bufferevent *, short, void *);
40: void server_client_err_callback(struct bufferevent *, short, void *);
1.1 nicm 41:
42: int server_client_msg_dispatch(struct client *);
43: void server_client_msg_command(struct client *, struct msg_command_data *);
44: void server_client_msg_identify(
1.25 nicm 45: struct client *, struct msg_identify_data *, int);
1.1 nicm 46: void server_client_msg_shell(struct client *);
47:
48: void printflike2 server_client_msg_error(struct cmd_ctx *, const char *, ...);
49: void printflike2 server_client_msg_print(struct cmd_ctx *, const char *, ...);
50: void printflike2 server_client_msg_info(struct cmd_ctx *, const char *, ...);
51:
52: /* Create a new client. */
53: void
54: server_client_create(int fd)
55: {
56: struct client *c;
57: u_int i;
58:
1.49 nicm 59: setblocking(fd, 0);
1.1 nicm 60:
61: c = xcalloc(1, sizeof *c);
62: c->references = 0;
63: imsg_init(&c->ibuf, fd);
1.14 nicm 64: server_update_event(c);
1.25 nicm 65:
1.10 nicm 66: if (gettimeofday(&c->creation_time, NULL) != 0)
1.1 nicm 67: fatal("gettimeofday failed");
1.11 nicm 68: memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.1 nicm 69:
1.36 nicm 70: c->stdin_event = NULL;
71: c->stdout_event = NULL;
72: c->stderr_event = NULL;
1.33 nicm 73:
1.1 nicm 74: c->tty.fd = -1;
75: c->title = NULL;
76:
77: c->session = NULL;
1.45 nicm 78: c->last_session = NULL;
1.1 nicm 79: c->tty.sx = 80;
80: c->tty.sy = 24;
81:
82: screen_init(&c->status, c->tty.sx, 1, 0);
1.51 nicm 83: RB_INIT(&c->status_new);
84: RB_INIT(&c->status_old);
1.1 nicm 85:
86: c->message_string = NULL;
1.21 nicm 87: ARRAY_INIT(&c->message_log);
1.1 nicm 88:
89: c->prompt_string = NULL;
90: c->prompt_buffer = NULL;
91: c->prompt_index = 0;
92:
1.57 nicm 93: c->last_mouse.b = MOUSE_UP;
94: c->last_mouse.x = c->last_mouse.y = -1;
95:
1.17 nicm 96: evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
97:
1.1 nicm 98: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
99: if (ARRAY_ITEM(&clients, i) == NULL) {
100: ARRAY_SET(&clients, i, c);
101: return;
102: }
103: }
104: ARRAY_ADD(&clients, c);
105: log_debug("new client %d", fd);
106: }
107:
108: /* Lost a client. */
109: void
110: server_client_lost(struct client *c)
111: {
1.21 nicm 112: struct message_entry *msg;
113: u_int i;
1.1 nicm 114:
115: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
116: if (ARRAY_ITEM(&clients, i) == c)
117: ARRAY_SET(&clients, i, NULL);
118: }
119: log_debug("lost client %d", c->ibuf.fd);
120:
121: /*
122: * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
123: * and tty_free might close an unrelated fd.
124: */
125: if (c->flags & CLIENT_TERMINAL)
126: tty_free(&c->tty);
127:
1.65 nicm 128: if (c->stdin_event != NULL)
129: bufferevent_free(c->stdin_event);
1.49 nicm 130: if (c->stdin_fd != -1) {
131: setblocking(c->stdin_fd, 1);
1.36 nicm 132: close(c->stdin_fd);
1.49 nicm 133: }
1.65 nicm 134: if (c->stdout_event != NULL)
135: bufferevent_free(c->stdout_event);
1.49 nicm 136: if (c->stdout_fd != -1) {
137: setblocking(c->stdout_fd, 1);
1.36 nicm 138: close(c->stdout_fd);
1.49 nicm 139: }
1.65 nicm 140: if (c->stderr_event != NULL)
141: bufferevent_free(c->stderr_event);
1.49 nicm 142: if (c->stderr_fd != -1) {
143: setblocking(c->stderr_fd, 1);
1.36 nicm 144: close(c->stderr_fd);
1.49 nicm 145: }
1.33 nicm 146:
1.51 nicm 147: status_free_jobs(&c->status_new);
148: status_free_jobs(&c->status_old);
1.1 nicm 149: screen_free(&c->status);
150:
151: if (c->title != NULL)
152: xfree(c->title);
153:
1.17 nicm 154: evtimer_del(&c->repeat_timer);
155:
1.69 ! nicm 156: if (event_initialized(&c->identify_timer))
! 157: evtimer_del(&c->identify_timer);
1.15 nicm 158:
1.1 nicm 159: if (c->message_string != NULL)
160: xfree(c->message_string);
1.69 ! nicm 161: if (event_initialized (&c->message_timer))
! 162: evtimer_del(&c->message_timer);
1.21 nicm 163: for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
164: msg = &ARRAY_ITEM(&c->message_log, i);
165: xfree(msg->msg);
166: }
167: ARRAY_FREE(&c->message_log);
1.1 nicm 168:
169: if (c->prompt_string != NULL)
170: xfree(c->prompt_string);
171: if (c->prompt_buffer != NULL)
172: xfree(c->prompt_buffer);
173:
174: if (c->cwd != NULL)
175: xfree(c->cwd);
1.61 nicm 176:
177: environ_free(&c->environ);
1.1 nicm 178:
179: close(c->ibuf.fd);
180: imsg_clear(&c->ibuf);
1.69 ! nicm 181: if (event_initialized(&c->event))
! 182: event_del(&c->event);
1.13 nicm 183:
1.1 nicm 184: for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
185: if (ARRAY_ITEM(&dead_clients, i) == NULL) {
186: ARRAY_SET(&dead_clients, i, c);
187: break;
188: }
189: }
190: if (i == ARRAY_LENGTH(&dead_clients))
191: ARRAY_ADD(&dead_clients, c);
192: c->flags |= CLIENT_DEAD;
193:
194: recalculate_sizes();
1.41 nicm 195: server_check_unattached();
1.19 nicm 196: server_update_socket();
1.9 nicm 197: }
198:
1.1 nicm 199: /* Process a single client event. */
200: void
1.12 nicm 201: server_client_callback(int fd, short events, void *data)
1.1 nicm 202: {
203: struct client *c = data;
1.7 nicm 204:
205: if (c->flags & CLIENT_DEAD)
206: return;
1.1 nicm 207:
208: if (fd == c->ibuf.fd) {
1.12 nicm 209: if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
1.1 nicm 210: goto client_lost;
211:
212: if (c->flags & CLIENT_BAD) {
213: if (c->ibuf.w.queued == 0)
214: goto client_lost;
215: return;
216: }
217:
1.12 nicm 218: if (events & EV_READ && server_client_msg_dispatch(c) != 0)
1.1 nicm 219: goto client_lost;
220: }
1.14 nicm 221:
1.25 nicm 222: server_update_event(c);
1.1 nicm 223: return;
224:
225: client_lost:
226: server_client_lost(c);
227: }
228:
1.16 nicm 229: /* Handle client status timer. */
230: void
231: server_client_status_timer(void)
232: {
233: struct client *c;
234: struct session *s;
235: struct timeval tv;
1.20 nicm 236: u_int i;
237: int interval;
238: time_t difference;
1.16 nicm 239:
240: if (gettimeofday(&tv, NULL) != 0)
241: fatal("gettimeofday failed");
242:
243: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
244: c = ARRAY_ITEM(&clients, i);
245: if (c == NULL || c->session == NULL)
246: continue;
247: if (c->message_string != NULL || c->prompt_string != NULL) {
248: /*
249: * Don't need timed redraw for messages/prompts so bail
250: * now. The status timer isn't reset when they are
251: * redrawn anyway.
252: */
253: continue;
254: }
255: s = c->session;
256:
257: if (!options_get_number(&s->options, "status"))
258: continue;
259: interval = options_get_number(&s->options, "status-interval");
260:
1.20 nicm 261: difference = tv.tv_sec - c->status_timer.tv_sec;
262: if (difference >= interval) {
1.51 nicm 263: status_update_jobs(c);
1.16 nicm 264: c->flags |= CLIENT_STATUS;
265: }
266: }
267: }
268:
1.66 nicm 269: /* Check for mouse keys. */
270: void
271: server_client_check_mouse(
272: struct client *c, struct window_pane *wp, struct mouse_event *mouse)
273: {
274: struct session *s = c->session;
275: struct options *oo = &s->options;
276: int statusat;
277:
278: statusat = status_at_line(c);
279:
280: /* Is this a window selection click on the status line? */
281: if (statusat != -1 && mouse->y == (u_int)statusat &&
282: options_get_number(oo, "mouse-select-window")) {
283: if (mouse->b == MOUSE_UP && c->last_mouse.b != MOUSE_UP) {
284: status_set_window_at(c, mouse->x);
285: return;
286: }
287: if (mouse->b & MOUSE_45) {
288: if ((mouse->b & MOUSE_BUTTON) == MOUSE_1) {
289: session_previous(c->session, 0);
290: server_redraw_session(s);
291: }
292: if ((mouse->b & MOUSE_BUTTON) == MOUSE_2) {
293: session_next(c->session, 0);
294: server_redraw_session(s);
295: }
296: return;
297: }
1.67 nicm 298: memcpy(&c->last_mouse, mouse, sizeof c->last_mouse);
299: return;
1.66 nicm 300: }
301:
302: /*
303: * Not on status line - adjust mouse position if status line is at the
304: * top and limit if at the bottom. From here on a struct mouse
305: * represents the offset onto the window itself.
306: */
307: if (statusat == 0 &&mouse->y > 0)
308: mouse->y--;
309: else if (statusat > 0 && mouse->y >= (u_int)statusat)
310: mouse->y = statusat - 1;
311:
312: /* Is this a pane selection? Allow down only in copy mode. */
313: if (options_get_number(oo, "mouse-select-pane") &&
314: ((!(mouse->b & MOUSE_DRAG) && mouse->b != MOUSE_UP) ||
315: wp->mode != &window_copy_mode)) {
316: window_set_active_at(wp->window, mouse->x, mouse->y);
317: server_redraw_window_borders(wp->window);
318: wp = wp->window->active; /* may have changed */
319: }
320:
321: /* Check if trying to resize pane. */
322: if (options_get_number(oo, "mouse-resize-pane"))
323: layout_resize_pane_mouse(c, mouse);
324:
325: /* Update last and pass through to client. */
326: memcpy(&c->last_mouse, mouse, sizeof c->last_mouse);
327: window_pane_mouse(wp, c->session, mouse);
328: }
329:
1.18 nicm 330: /* Handle data key input from client. */
331: void
332: server_client_handle_key(int key, struct mouse_event *mouse, void *data)
333: {
334: struct client *c = data;
335: struct session *s;
336: struct window *w;
337: struct window_pane *wp;
338: struct options *oo;
339: struct timeval tv;
340: struct key_binding *bd;
341: int xtimeout, isprefix;
342:
343: /* Check the client is good to accept input. */
344: if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
345: return;
346: if (c->session == NULL)
347: return;
348: s = c->session;
349:
350: /* Update the activity timer. */
351: if (gettimeofday(&c->activity_time, NULL) != 0)
352: fatal("gettimeofday failed");
353: memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
354:
355: w = c->session->curw->window;
356: wp = w->active;
357: oo = &c->session->options;
358:
359: /* Special case: number keys jump to pane in identify mode. */
1.25 nicm 360: if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30 nicm 361: if (c->flags & CLIENT_READONLY)
362: return;
1.18 nicm 363: wp = window_pane_at_index(w, key - '0');
364: if (wp != NULL && window_pane_visible(wp))
365: window_set_active_pane(w, wp);
366: server_clear_identify(c);
367: return;
368: }
369:
370: /* Handle status line. */
1.30 nicm 371: if (!(c->flags & CLIENT_READONLY)) {
372: status_message_clear(c);
373: server_clear_identify(c);
374: }
1.18 nicm 375: if (c->prompt_string != NULL) {
1.30 nicm 376: if (!(c->flags & CLIENT_READONLY))
377: status_prompt_key(c, key);
1.18 nicm 378: return;
379: }
380:
381: /* Check for mouse keys. */
382: if (key == KEYC_MOUSE) {
1.30 nicm 383: if (c->flags & CLIENT_READONLY)
384: return;
1.66 nicm 385: server_client_check_mouse(c, wp, mouse);
1.18 nicm 386: return;
387: }
388:
389: /* Is this a prefix key? */
1.63 nicm 390: if (key == options_get_number(&c->session->options, "prefix"))
391: isprefix = 1;
392: else if (key == options_get_number(&c->session->options, "prefix2"))
393: isprefix = 1;
394: else
395: isprefix = 0;
1.18 nicm 396:
397: /* No previous prefix key. */
398: if (!(c->flags & CLIENT_PREFIX)) {
399: if (isprefix)
400: c->flags |= CLIENT_PREFIX;
401: else {
402: /* Try as a non-prefix key binding. */
1.30 nicm 403: if ((bd = key_bindings_lookup(key)) == NULL) {
404: if (!(c->flags & CLIENT_READONLY))
1.31 nicm 405: window_pane_key(wp, c->session, key);
1.30 nicm 406: } else
1.18 nicm 407: key_bindings_dispatch(bd, c);
408: }
409: return;
410: }
411:
412: /* Prefix key already pressed. Reset prefix and lookup key. */
413: c->flags &= ~CLIENT_PREFIX;
414: if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
415: /* If repeating, treat this as a key, else ignore. */
416: if (c->flags & CLIENT_REPEAT) {
417: c->flags &= ~CLIENT_REPEAT;
418: if (isprefix)
419: c->flags |= CLIENT_PREFIX;
1.30 nicm 420: else if (!(c->flags & CLIENT_READONLY))
1.31 nicm 421: window_pane_key(wp, c->session, key);
1.18 nicm 422: }
423: return;
424: }
425:
426: /* If already repeating, but this key can't repeat, skip it. */
427: if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
428: c->flags &= ~CLIENT_REPEAT;
429: if (isprefix)
430: c->flags |= CLIENT_PREFIX;
1.30 nicm 431: else if (!(c->flags & CLIENT_READONLY))
1.31 nicm 432: window_pane_key(wp, c->session, key);
1.18 nicm 433: return;
434: }
435:
436: /* If this key can repeat, reset the repeat flags and timer. */
437: xtimeout = options_get_number(&c->session->options, "repeat-time");
438: if (xtimeout != 0 && bd->can_repeat) {
439: c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
1.25 nicm 440:
1.18 nicm 441: tv.tv_sec = xtimeout / 1000;
442: tv.tv_usec = (xtimeout % 1000) * 1000L;
443: evtimer_del(&c->repeat_timer);
444: evtimer_add(&c->repeat_timer, &tv);
445: }
446:
447: /* Dispatch the command. */
448: key_bindings_dispatch(bd, c);
449: }
450:
1.2 nicm 451: /* Client functions that need to happen every loop. */
452: void
453: server_client_loop(void)
454: {
455: struct client *c;
456: struct window *w;
457: struct window_pane *wp;
458: u_int i;
459:
460: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
461: c = ARRAY_ITEM(&clients, i);
1.36 nicm 462: if (c == NULL)
1.2 nicm 463: continue;
464:
1.36 nicm 465: server_client_check_exit(c);
466: if (c->session != NULL) {
467: server_client_check_redraw(c);
468: server_client_reset_state(c);
469: }
1.2 nicm 470: }
471:
472: /*
473: * Any windows will have been redrawn as part of clients, so clear
474: * their flags now.
475: */
476: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
477: w = ARRAY_ITEM(&windows, i);
478: if (w == NULL)
479: continue;
480:
481: w->flags &= ~WINDOW_REDRAW;
482: TAILQ_FOREACH(wp, &w->panes, entry)
483: wp->flags &= ~PANE_REDRAW;
484: }
485: }
486:
1.18 nicm 487: /*
488: * Update cursor position and mode settings. The scroll region and attributes
489: * are cleared when idle (waiting for an event) as this is the most likely time
490: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
491: * compromise between excessive resets and likelihood of an interrupt.
492: *
493: * tty_region/tty_reset/tty_update_mode already take care of not resetting
494: * things that are already in their default state.
495: */
1.1 nicm 496: void
1.18 nicm 497: server_client_reset_state(struct client *c)
1.1 nicm 498: {
1.18 nicm 499: struct window *w = c->session->curw->window;
500: struct window_pane *wp = w->active;
501: struct screen *s = wp->screen;
502: struct options *oo = &c->session->options;
1.54 nicm 503: struct options *wo = &w->options;
1.66 nicm 504: int status, mode, o;
1.60 nicm 505:
506: if (c->flags & CLIENT_SUSPENDED)
507: return;
1.1 nicm 508:
509: tty_region(&c->tty, 0, c->tty.sy - 1);
510:
511: status = options_get_number(oo, "status");
512: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
513: tty_cursor(&c->tty, 0, 0);
1.66 nicm 514: else {
515: o = status && options_get_number (oo, "status-position") == 0;
516: tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
517: }
1.1 nicm 518:
1.50 nicm 519: /*
1.57 nicm 520: * Resizing panes with the mouse requires at least button mode to give
521: * a smooth appearance.
522: */
523: mode = s->mode;
524: if ((c->last_mouse.b & MOUSE_RESIZE_PANE) &&
525: !(mode & (MODE_MOUSE_BUTTON|MODE_MOUSE_ANY)))
526: mode |= MODE_MOUSE_BUTTON;
527:
528: /*
1.50 nicm 529: * Any mode will do for mouse-select-pane, but set standard mode if
530: * none.
531: */
1.54 nicm 532: if ((mode & ALL_MOUSE_MODES) == 0) {
533: if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
1.55 nicm 534: options_get_number(oo, "mouse-select-pane"))
1.57 nicm 535: mode |= MODE_MOUSE_STANDARD;
536: else if (options_get_number(oo, "mouse-resize-pane"))
1.54 nicm 537: mode |= MODE_MOUSE_STANDARD;
538: else if (options_get_number(oo, "mouse-select-window"))
539: mode |= MODE_MOUSE_STANDARD;
540: else if (options_get_number(wo, "mode-mouse"))
541: mode |= MODE_MOUSE_STANDARD;
542: }
1.48 nicm 543:
544: /*
545: * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
546: * user has set mouse-utf8 and any mouse mode is in effect, turn on
547: * UTF-8 mouse input. If the receiving terminal hasn't requested it
548: * (that is, it isn't in s->mode), then it'll be converted in
549: * input_mouse.
550: */
551: if ((c->tty.flags & TTY_UTF8) &&
552: (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
553: mode |= MODE_MOUSE_UTF8;
554: else
555: mode &= ~MODE_MOUSE_UTF8;
556:
557: /* Set the terminal mode and reset attributes. */
1.59 nicm 558: tty_update_mode(&c->tty, mode, s);
1.1 nicm 559: tty_reset(&c->tty);
1.17 nicm 560: }
561:
562: /* Repeat time callback. */
1.24 nicm 563: /* ARGSUSED */
1.17 nicm 564: void
565: server_client_repeat_timer(unused int fd, unused short events, void *data)
566: {
567: struct client *c = data;
568:
569: if (c->flags & CLIENT_REPEAT)
570: c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
1.1 nicm 571: }
572:
1.36 nicm 573: /* Check if client should be exited. */
574: void
575: server_client_check_exit(struct client *c)
576: {
577: struct msg_exit_data exitdata;
578:
579: if (!(c->flags & CLIENT_EXIT))
580: return;
581:
582: if (c->stdout_fd != -1 && c->stdout_event != NULL &&
583: EVBUFFER_LENGTH(c->stdout_event->output) != 0)
584: return;
585: if (c->stderr_fd != -1 && c->stderr_event != NULL &&
586: EVBUFFER_LENGTH(c->stderr_event->output) != 0)
587: return;
588:
589: exitdata.retcode = c->retcode;
590: server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
591:
592: c->flags &= ~CLIENT_EXIT;
1.38 nicm 593: }
594:
1.1 nicm 595: /* Check for client redraws. */
596: void
597: server_client_check_redraw(struct client *c)
598: {
599: struct session *s = c->session;
600: struct window_pane *wp;
601: int flags, redraw;
602:
603: flags = c->tty.flags & TTY_FREEZE;
604: c->tty.flags &= ~TTY_FREEZE;
605:
606: if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
607: if (options_get_number(&s->options, "set-titles"))
608: server_client_set_title(c);
1.12 nicm 609:
1.1 nicm 610: if (c->message_string != NULL)
611: redraw = status_message_redraw(c);
612: else if (c->prompt_string != NULL)
613: redraw = status_prompt_redraw(c);
614: else
615: redraw = status_redraw(c);
616: if (!redraw)
617: c->flags &= ~CLIENT_STATUS;
618: }
619:
620: if (c->flags & CLIENT_REDRAW) {
1.27 nicm 621: screen_redraw_screen(c, 0, 0);
622: c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38 nicm 623: } else if (c->flags & CLIENT_REDRAWWINDOW) {
624: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
625: screen_redraw_pane(c, wp);
626: c->flags &= ~CLIENT_REDRAWWINDOW;
1.1 nicm 627: } else {
628: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
629: if (wp->flags & PANE_REDRAW)
630: screen_redraw_pane(c, wp);
631: }
632: }
633:
1.27 nicm 634: if (c->flags & CLIENT_BORDERS)
635: screen_redraw_screen(c, 0, 1);
636:
1.1 nicm 637: if (c->flags & CLIENT_STATUS)
1.27 nicm 638: screen_redraw_screen(c, 1, 0);
1.1 nicm 639:
640: c->tty.flags |= flags;
641:
1.27 nicm 642: c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
1.1 nicm 643: }
644:
645: /* Set client title. */
646: void
647: server_client_set_title(struct client *c)
648: {
649: struct session *s = c->session;
650: const char *template;
651: char *title;
652:
653: template = options_get_string(&s->options, "set-titles-string");
1.25 nicm 654:
1.52 nicm 655: title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
1.1 nicm 656: if (c->title == NULL || strcmp(title, c->title) != 0) {
657: if (c->title != NULL)
658: xfree(c->title);
659: c->title = xstrdup(title);
660: tty_set_title(&c->tty, c->title);
661: }
662: xfree(title);
663: }
664:
1.36 nicm 665: /*
666: * Error callback for client stdin. Caller must increase reference count when
667: * enabling event!
668: */
669: void
670: server_client_in_callback(
671: unused struct bufferevent *bufev, unused short what, void *data)
672: {
673: struct client *c = data;
674:
675: c->references--;
676: if (c->flags & CLIENT_DEAD)
677: return;
678:
679: bufferevent_disable(c->stdin_event, EV_READ|EV_WRITE);
1.49 nicm 680: setblocking(c->stdin_fd, 1);
1.36 nicm 681: close(c->stdin_fd);
682: c->stdin_fd = -1;
683:
684: if (c->stdin_callback != NULL)
685: c->stdin_callback(c, c->stdin_data);
686: }
687:
688: /* Error callback for client stdout. */
689: void
690: server_client_out_callback(
691: unused struct bufferevent *bufev, unused short what, unused void *data)
692: {
693: struct client *c = data;
694:
695: bufferevent_disable(c->stdout_event, EV_READ|EV_WRITE);
1.49 nicm 696: setblocking(c->stdout_fd, 1);
1.36 nicm 697: close(c->stdout_fd);
698: c->stdout_fd = -1;
699: }
700:
701: /* Error callback for client stderr. */
702: void
703: server_client_err_callback(
704: unused struct bufferevent *bufev, unused short what, unused void *data)
705: {
706: struct client *c = data;
707:
708: bufferevent_disable(c->stderr_event, EV_READ|EV_WRITE);
1.49 nicm 709: setblocking(c->stderr_fd, 1);
1.36 nicm 710: close(c->stderr_fd);
711: c->stderr_fd = -1;
712: }
713:
1.1 nicm 714: /* Dispatch message from client. */
715: int
716: server_client_msg_dispatch(struct client *c)
717: {
718: struct imsg imsg;
719: struct msg_command_data commanddata;
720: struct msg_identify_data identifydata;
721: struct msg_environ_data environdata;
722: ssize_t n, datalen;
723:
1.8 deraadt 724: if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
725: return (-1);
1.1 nicm 726:
727: for (;;) {
728: if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
729: return (-1);
730: if (n == 0)
731: return (0);
732: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
733:
734: if (imsg.hdr.peerid != PROTOCOL_VERSION) {
735: server_write_client(c, MSG_VERSION, NULL, 0);
736: c->flags |= CLIENT_BAD;
737: imsg_free(&imsg);
738: continue;
739: }
740:
741: log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
742: switch (imsg.hdr.type) {
743: case MSG_COMMAND:
744: if (datalen != sizeof commanddata)
745: fatalx("bad MSG_COMMAND size");
746: memcpy(&commanddata, imsg.data, sizeof commanddata);
747:
748: server_client_msg_command(c, &commanddata);
749: break;
750: case MSG_IDENTIFY:
751: if (datalen != sizeof identifydata)
752: fatalx("bad MSG_IDENTIFY size");
753: if (imsg.fd == -1)
754: fatalx("MSG_IDENTIFY missing fd");
755: memcpy(&identifydata, imsg.data, sizeof identifydata);
756:
1.39 nicm 757: c->stdin_fd = imsg.fd;
1.37 nicm 758: c->stdin_event = bufferevent_new(c->stdin_fd,
759: NULL, NULL, server_client_in_callback, c);
1.36 nicm 760: if (c->stdin_event == NULL)
761: fatalx("failed to create stdin event");
1.49 nicm 762: setblocking(c->stdin_fd, 0);
1.36 nicm 763:
1.1 nicm 764: server_client_msg_identify(c, &identifydata, imsg.fd);
765: break;
1.33 nicm 766: case MSG_STDOUT:
767: if (datalen != 0)
768: fatalx("bad MSG_STDOUT size");
769: if (imsg.fd == -1)
770: fatalx("MSG_STDOUT missing fd");
771:
1.36 nicm 772: c->stdout_fd = imsg.fd;
1.37 nicm 773: c->stdout_event = bufferevent_new(c->stdout_fd,
774: NULL, NULL, server_client_out_callback, c);
1.36 nicm 775: if (c->stdout_event == NULL)
776: fatalx("failed to create stdout event");
1.49 nicm 777: setblocking(c->stdout_fd, 0);
1.36 nicm 778:
1.33 nicm 779: break;
780: case MSG_STDERR:
781: if (datalen != 0)
782: fatalx("bad MSG_STDERR size");
783: if (imsg.fd == -1)
784: fatalx("MSG_STDERR missing fd");
785:
1.36 nicm 786: c->stderr_fd = imsg.fd;
1.37 nicm 787: c->stderr_event = bufferevent_new(c->stderr_fd,
788: NULL, NULL, server_client_err_callback, c);
1.36 nicm 789: if (c->stderr_event == NULL)
790: fatalx("failed to create stderr event");
1.49 nicm 791: setblocking(c->stderr_fd, 0);
1.36 nicm 792:
1.33 nicm 793: break;
1.1 nicm 794: case MSG_RESIZE:
795: if (datalen != 0)
796: fatalx("bad MSG_RESIZE size");
797:
1.32 nicm 798: if (tty_resize(&c->tty)) {
799: recalculate_sizes();
800: server_redraw_client(c);
801: }
1.1 nicm 802: break;
803: case MSG_EXITING:
804: if (datalen != 0)
805: fatalx("bad MSG_EXITING size");
806:
807: c->session = NULL;
808: tty_close(&c->tty);
809: server_write_client(c, MSG_EXITED, NULL, 0);
810: break;
811: case MSG_WAKEUP:
812: case MSG_UNLOCK:
813: if (datalen != 0)
814: fatalx("bad MSG_WAKEUP size");
815:
816: if (!(c->flags & CLIENT_SUSPENDED))
817: break;
818: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 819:
1.11 nicm 820: if (gettimeofday(&c->activity_time, NULL) != 0)
821: fatal("gettimeofday");
1.47 nicm 822: if (c->session != NULL)
823: session_update_activity(c->session);
1.10 nicm 824:
1.1 nicm 825: tty_start_tty(&c->tty);
826: server_redraw_client(c);
827: recalculate_sizes();
828: break;
829: case MSG_ENVIRON:
830: if (datalen != sizeof environdata)
831: fatalx("bad MSG_ENVIRON size");
832: memcpy(&environdata, imsg.data, sizeof environdata);
833:
834: environdata.var[(sizeof environdata.var) - 1] = '\0';
835: if (strchr(environdata.var, '=') != NULL)
836: environ_put(&c->environ, environdata.var);
837: break;
838: case MSG_SHELL:
839: if (datalen != 0)
840: fatalx("bad MSG_SHELL size");
841:
842: server_client_msg_shell(c);
843: break;
844: default:
845: fatalx("unexpected message");
846: }
847:
848: imsg_free(&imsg);
849: }
850: }
851:
852: /* Callback to send error message to client. */
853: void printflike2
854: server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
855: {
1.33 nicm 856: va_list ap;
1.1 nicm 857:
858: va_start(ap, fmt);
1.36 nicm 859: evbuffer_add_vprintf(ctx->cmdclient->stderr_event->output, fmt, ap);
1.1 nicm 860: va_end(ap);
861:
1.36 nicm 862: bufferevent_write(ctx->cmdclient->stderr_event, "\n", 1);
1.34 nicm 863: ctx->cmdclient->retcode = 1;
1.1 nicm 864: }
865:
866: /* Callback to send print message to client. */
867: void printflike2
868: server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
869: {
1.33 nicm 870: va_list ap;
1.1 nicm 871:
872: va_start(ap, fmt);
1.36 nicm 873: evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
1.1 nicm 874: va_end(ap);
875:
1.36 nicm 876: bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
1.1 nicm 877: }
878:
879: /* Callback to send print message to client, if not quiet. */
880: void printflike2
881: server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
882: {
1.33 nicm 883: va_list ap;
1.1 nicm 884:
1.26 nicm 885: if (options_get_number(&global_options, "quiet"))
1.1 nicm 886: return;
887:
888: va_start(ap, fmt);
1.36 nicm 889: evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
1.1 nicm 890: va_end(ap);
891:
1.36 nicm 892: bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
1.1 nicm 893: }
894:
895: /* Handle command message. */
896: void
897: server_client_msg_command(struct client *c, struct msg_command_data *data)
898: {
1.36 nicm 899: struct cmd_ctx ctx;
900: struct cmd_list *cmdlist = NULL;
901: int argc;
902: char **argv, *cause;
1.1 nicm 903:
904: ctx.error = server_client_msg_error;
905: ctx.print = server_client_msg_print;
906: ctx.info = server_client_msg_info;
907:
908: ctx.msgdata = data;
909: ctx.curclient = NULL;
910:
911: ctx.cmdclient = c;
912:
913: argc = data->argc;
914: data->argv[(sizeof data->argv) - 1] = '\0';
915: if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
916: server_client_msg_error(&ctx, "command too long");
917: goto error;
918: }
919:
920: if (argc == 0) {
921: argc = 1;
922: argv = xcalloc(1, sizeof *argv);
923: *argv = xstrdup("new-session");
924: }
925:
926: if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
927: server_client_msg_error(&ctx, "%s", cause);
928: cmd_free_argv(argc, argv);
929: goto error;
930: }
931: cmd_free_argv(argc, argv);
932:
1.36 nicm 933: if (cmd_list_exec(cmdlist, &ctx) != 1)
934: c->flags |= CLIENT_EXIT;
1.1 nicm 935: cmd_list_free(cmdlist);
936: return;
937:
938: error:
939: if (cmdlist != NULL)
940: cmd_list_free(cmdlist);
1.36 nicm 941: c->flags |= CLIENT_EXIT;
1.1 nicm 942: }
943:
944: /* Handle identify message. */
945: void
946: server_client_msg_identify(
947: struct client *c, struct msg_identify_data *data, int fd)
948: {
1.33 nicm 949: int tty_fd;
950:
1.1 nicm 951: c->cwd = NULL;
952: data->cwd[(sizeof data->cwd) - 1] = '\0';
953: if (*data->cwd != '\0')
954: c->cwd = xstrdup(data->cwd);
955:
1.33 nicm 956: if (!isatty(fd))
957: return;
958: if ((tty_fd = dup(fd)) == -1)
959: fatal("dup failed");
1.1 nicm 960: data->term[(sizeof data->term) - 1] = '\0';
1.33 nicm 961: tty_init(&c->tty, tty_fd, data->term);
1.1 nicm 962: if (data->flags & IDENTIFY_UTF8)
963: c->tty.flags |= TTY_UTF8;
964: if (data->flags & IDENTIFY_256COLOURS)
965: c->tty.term_flags |= TERM_256COLOURS;
966: else if (data->flags & IDENTIFY_88COLOURS)
967: c->tty.term_flags |= TERM_88COLOURS;
1.18 nicm 968: c->tty.key_callback = server_client_handle_key;
969: c->tty.key_data = c;
1.1 nicm 970:
971: tty_resize(&c->tty);
972:
973: c->flags |= CLIENT_TERMINAL;
974: }
975:
976: /* Handle shell message. */
977: void
978: server_client_msg_shell(struct client *c)
979: {
980: struct msg_shell_data data;
981: const char *shell;
1.25 nicm 982:
1.1 nicm 983: shell = options_get_string(&global_s_options, "default-shell");
984:
985: if (*shell == '\0' || areshell(shell))
986: shell = _PATH_BSHELL;
987: if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
988: strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
1.25 nicm 989:
1.1 nicm 990: server_write_client(c, MSG_SHELL, &data, sizeof data);
991: c->flags |= CLIENT_BAD; /* it will die after exec */
992: }