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