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