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