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