Annotation of src/usr.bin/tmux/server-client.c, Revision 1.76
1.76 ! nicm 1: /* $OpenBSD: server-client.c,v 1.75 2012/06/18 13:16: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:
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 timeval tv;
349: struct key_binding *bd;
350: int xtimeout, isprefix;
351:
352: /* Check the client is good to accept input. */
353: if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
354: return;
355: if (c->session == NULL)
356: return;
357: s = c->session;
358:
359: /* Update the activity timer. */
360: if (gettimeofday(&c->activity_time, NULL) != 0)
361: fatal("gettimeofday failed");
362: memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
363:
364: w = c->session->curw->window;
365: wp = w->active;
366:
367: /* Special case: number keys jump to pane in identify mode. */
1.25 nicm 368: if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30 nicm 369: if (c->flags & CLIENT_READONLY)
370: return;
1.18 nicm 371: wp = window_pane_at_index(w, key - '0');
372: if (wp != NULL && window_pane_visible(wp))
373: window_set_active_pane(w, wp);
374: server_clear_identify(c);
375: return;
376: }
377:
378: /* Handle status line. */
1.30 nicm 379: if (!(c->flags & CLIENT_READONLY)) {
380: status_message_clear(c);
381: server_clear_identify(c);
382: }
1.18 nicm 383: if (c->prompt_string != NULL) {
1.30 nicm 384: if (!(c->flags & CLIENT_READONLY))
385: status_prompt_key(c, key);
1.18 nicm 386: return;
387: }
388:
389: /* Check for mouse keys. */
390: if (key == KEYC_MOUSE) {
1.30 nicm 391: if (c->flags & CLIENT_READONLY)
392: return;
1.74 nicm 393: server_client_check_mouse(c, wp, &c->tty.mouse);
1.18 nicm 394: return;
395: }
396:
397: /* Is this a prefix key? */
1.63 nicm 398: if (key == options_get_number(&c->session->options, "prefix"))
399: isprefix = 1;
400: else if (key == options_get_number(&c->session->options, "prefix2"))
401: isprefix = 1;
402: else
403: isprefix = 0;
1.18 nicm 404:
405: /* No previous prefix key. */
406: if (!(c->flags & CLIENT_PREFIX)) {
407: if (isprefix)
408: c->flags |= CLIENT_PREFIX;
409: else {
410: /* Try as a non-prefix key binding. */
1.30 nicm 411: if ((bd = key_bindings_lookup(key)) == NULL) {
412: if (!(c->flags & CLIENT_READONLY))
1.31 nicm 413: window_pane_key(wp, c->session, key);
1.30 nicm 414: } else
1.18 nicm 415: key_bindings_dispatch(bd, c);
416: }
417: return;
418: }
419:
420: /* Prefix key already pressed. Reset prefix and lookup key. */
421: c->flags &= ~CLIENT_PREFIX;
422: if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
423: /* If repeating, treat this as a key, else ignore. */
424: if (c->flags & CLIENT_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: }
431: return;
432: }
433:
434: /* If already repeating, but this key can't repeat, skip it. */
435: if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
436: c->flags &= ~CLIENT_REPEAT;
437: if (isprefix)
438: c->flags |= CLIENT_PREFIX;
1.30 nicm 439: else if (!(c->flags & CLIENT_READONLY))
1.31 nicm 440: window_pane_key(wp, c->session, key);
1.18 nicm 441: return;
442: }
443:
444: /* If this key can repeat, reset the repeat flags and timer. */
445: xtimeout = options_get_number(&c->session->options, "repeat-time");
446: if (xtimeout != 0 && bd->can_repeat) {
447: c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
1.25 nicm 448:
1.18 nicm 449: tv.tv_sec = xtimeout / 1000;
450: tv.tv_usec = (xtimeout % 1000) * 1000L;
451: evtimer_del(&c->repeat_timer);
452: evtimer_add(&c->repeat_timer, &tv);
453: }
454:
455: /* Dispatch the command. */
456: key_bindings_dispatch(bd, c);
457: }
458:
1.2 nicm 459: /* Client functions that need to happen every loop. */
460: void
461: server_client_loop(void)
462: {
463: struct client *c;
464: struct window *w;
465: struct window_pane *wp;
466: u_int i;
467:
468: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
469: c = ARRAY_ITEM(&clients, i);
1.36 nicm 470: if (c == NULL)
1.2 nicm 471: continue;
472:
1.36 nicm 473: server_client_check_exit(c);
474: if (c->session != NULL) {
475: server_client_check_redraw(c);
476: server_client_reset_state(c);
477: }
1.2 nicm 478: }
479:
480: /*
481: * Any windows will have been redrawn as part of clients, so clear
482: * their flags now.
483: */
484: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
485: w = ARRAY_ITEM(&windows, i);
486: if (w == NULL)
487: continue;
488:
489: w->flags &= ~WINDOW_REDRAW;
490: TAILQ_FOREACH(wp, &w->panes, entry)
491: wp->flags &= ~PANE_REDRAW;
492: }
493: }
494:
1.18 nicm 495: /*
496: * Update cursor position and mode settings. The scroll region and attributes
497: * are cleared when idle (waiting for an event) as this is the most likely time
498: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
499: * compromise between excessive resets and likelihood of an interrupt.
500: *
501: * tty_region/tty_reset/tty_update_mode already take care of not resetting
502: * things that are already in their default state.
503: */
1.1 nicm 504: void
1.18 nicm 505: server_client_reset_state(struct client *c)
1.1 nicm 506: {
1.18 nicm 507: struct window *w = c->session->curw->window;
508: struct window_pane *wp = w->active;
509: struct screen *s = wp->screen;
510: struct options *oo = &c->session->options;
1.54 nicm 511: struct options *wo = &w->options;
1.66 nicm 512: int status, mode, o;
1.60 nicm 513:
514: if (c->flags & CLIENT_SUSPENDED)
515: return;
1.1 nicm 516:
517: tty_region(&c->tty, 0, c->tty.sy - 1);
518:
519: status = options_get_number(oo, "status");
520: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
521: tty_cursor(&c->tty, 0, 0);
1.66 nicm 522: else {
523: o = status && options_get_number (oo, "status-position") == 0;
524: tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
525: }
1.1 nicm 526:
1.50 nicm 527: /*
1.57 nicm 528: * Resizing panes with the mouse requires at least button mode to give
529: * a smooth appearance.
530: */
531: mode = s->mode;
532: if ((c->last_mouse.b & MOUSE_RESIZE_PANE) &&
533: !(mode & (MODE_MOUSE_BUTTON|MODE_MOUSE_ANY)))
534: mode |= MODE_MOUSE_BUTTON;
535:
536: /*
1.50 nicm 537: * Any mode will do for mouse-select-pane, but set standard mode if
538: * none.
539: */
1.54 nicm 540: if ((mode & ALL_MOUSE_MODES) == 0) {
541: if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
1.55 nicm 542: options_get_number(oo, "mouse-select-pane"))
1.57 nicm 543: mode |= MODE_MOUSE_STANDARD;
544: else if (options_get_number(oo, "mouse-resize-pane"))
1.54 nicm 545: mode |= MODE_MOUSE_STANDARD;
546: else if (options_get_number(oo, "mouse-select-window"))
547: mode |= MODE_MOUSE_STANDARD;
548: else if (options_get_number(wo, "mode-mouse"))
549: mode |= MODE_MOUSE_STANDARD;
550: }
1.48 nicm 551:
552: /*
553: * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
554: * user has set mouse-utf8 and any mouse mode is in effect, turn on
555: * UTF-8 mouse input. If the receiving terminal hasn't requested it
556: * (that is, it isn't in s->mode), then it'll be converted in
557: * input_mouse.
558: */
559: if ((c->tty.flags & TTY_UTF8) &&
560: (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
561: mode |= MODE_MOUSE_UTF8;
562: else
563: mode &= ~MODE_MOUSE_UTF8;
564:
565: /* Set the terminal mode and reset attributes. */
1.59 nicm 566: tty_update_mode(&c->tty, mode, s);
1.1 nicm 567: tty_reset(&c->tty);
1.17 nicm 568: }
569:
570: /* Repeat time callback. */
1.24 nicm 571: /* ARGSUSED */
1.17 nicm 572: void
573: server_client_repeat_timer(unused int fd, unused short events, void *data)
574: {
575: struct client *c = data;
576:
577: if (c->flags & CLIENT_REPEAT)
578: c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
1.1 nicm 579: }
580:
1.36 nicm 581: /* Check if client should be exited. */
582: void
583: server_client_check_exit(struct client *c)
584: {
585: struct msg_exit_data exitdata;
586:
587: if (!(c->flags & CLIENT_EXIT))
588: return;
589:
1.73 nicm 590: if (EVBUFFER_LENGTH(c->stdin_data) != 0)
591: return;
592: if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36 nicm 593: return;
1.73 nicm 594: if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36 nicm 595: return;
596:
597: exitdata.retcode = c->retcode;
598: server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
599:
600: c->flags &= ~CLIENT_EXIT;
1.38 nicm 601: }
602:
1.1 nicm 603: /* Check for client redraws. */
604: void
605: server_client_check_redraw(struct client *c)
606: {
607: struct session *s = c->session;
608: struct window_pane *wp;
609: int flags, redraw;
610:
611: flags = c->tty.flags & TTY_FREEZE;
612: c->tty.flags &= ~TTY_FREEZE;
613:
614: if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
615: if (options_get_number(&s->options, "set-titles"))
616: server_client_set_title(c);
1.12 nicm 617:
1.1 nicm 618: if (c->message_string != NULL)
619: redraw = status_message_redraw(c);
620: else if (c->prompt_string != NULL)
621: redraw = status_prompt_redraw(c);
622: else
623: redraw = status_redraw(c);
624: if (!redraw)
625: c->flags &= ~CLIENT_STATUS;
626: }
627:
628: if (c->flags & CLIENT_REDRAW) {
1.27 nicm 629: screen_redraw_screen(c, 0, 0);
630: c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38 nicm 631: } else if (c->flags & CLIENT_REDRAWWINDOW) {
632: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
633: screen_redraw_pane(c, wp);
634: c->flags &= ~CLIENT_REDRAWWINDOW;
1.1 nicm 635: } else {
636: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
637: if (wp->flags & PANE_REDRAW)
638: screen_redraw_pane(c, wp);
639: }
640: }
641:
1.27 nicm 642: if (c->flags & CLIENT_BORDERS)
643: screen_redraw_screen(c, 0, 1);
644:
1.1 nicm 645: if (c->flags & CLIENT_STATUS)
1.27 nicm 646: screen_redraw_screen(c, 1, 0);
1.1 nicm 647:
648: c->tty.flags |= flags;
649:
1.27 nicm 650: c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
1.1 nicm 651: }
652:
653: /* Set client title. */
654: void
655: server_client_set_title(struct client *c)
656: {
657: struct session *s = c->session;
658: const char *template;
659: char *title;
660:
661: template = options_get_string(&s->options, "set-titles-string");
1.25 nicm 662:
1.52 nicm 663: title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
1.1 nicm 664: if (c->title == NULL || strcmp(title, c->title) != 0) {
665: if (c->title != NULL)
666: xfree(c->title);
667: c->title = xstrdup(title);
668: tty_set_title(&c->tty, c->title);
669: }
670: xfree(title);
671: }
672:
673: /* Dispatch message from client. */
674: int
675: server_client_msg_dispatch(struct client *c)
676: {
677: struct imsg imsg;
678: struct msg_command_data commanddata;
679: struct msg_identify_data identifydata;
680: struct msg_environ_data environdata;
1.73 nicm 681: struct msg_stdin_data stdindata;
1.1 nicm 682: ssize_t n, datalen;
683:
1.8 deraadt 684: if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
685: return (-1);
1.1 nicm 686:
687: for (;;) {
688: if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
689: return (-1);
690: if (n == 0)
691: return (0);
692: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
693:
694: if (imsg.hdr.peerid != PROTOCOL_VERSION) {
695: server_write_client(c, MSG_VERSION, NULL, 0);
696: c->flags |= CLIENT_BAD;
697: imsg_free(&imsg);
698: continue;
699: }
700:
701: log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
702: switch (imsg.hdr.type) {
703: case MSG_COMMAND:
704: if (datalen != sizeof commanddata)
705: fatalx("bad MSG_COMMAND size");
706: memcpy(&commanddata, imsg.data, sizeof commanddata);
707:
708: server_client_msg_command(c, &commanddata);
709: break;
710: case MSG_IDENTIFY:
711: if (datalen != sizeof identifydata)
712: fatalx("bad MSG_IDENTIFY size");
713: if (imsg.fd == -1)
714: fatalx("MSG_IDENTIFY missing fd");
715: memcpy(&identifydata, imsg.data, sizeof identifydata);
716:
717: server_client_msg_identify(c, &identifydata, imsg.fd);
718: break;
1.73 nicm 719: case MSG_STDIN:
720: if (datalen != sizeof stdindata)
721: fatalx("bad MSG_STDIN size");
722: memcpy(&stdindata, imsg.data, sizeof stdindata);
1.36 nicm 723:
1.73 nicm 724: if (c->stdin_callback == NULL)
725: break;
726: if (stdindata.size <= 0)
727: c->stdin_closed = 1;
728: else {
729: evbuffer_add(c->stdin_data, stdindata.data,
730: stdindata.size);
731: }
732: c->stdin_callback(c, c->stdin_closed,
733: c->stdin_callback_data);
1.33 nicm 734: break;
1.1 nicm 735: case MSG_RESIZE:
736: if (datalen != 0)
737: fatalx("bad MSG_RESIZE size");
738:
1.32 nicm 739: if (tty_resize(&c->tty)) {
740: recalculate_sizes();
741: server_redraw_client(c);
742: }
1.1 nicm 743: break;
744: case MSG_EXITING:
745: if (datalen != 0)
746: fatalx("bad MSG_EXITING size");
747:
748: c->session = NULL;
749: tty_close(&c->tty);
750: server_write_client(c, MSG_EXITED, NULL, 0);
751: break;
752: case MSG_WAKEUP:
753: case MSG_UNLOCK:
754: if (datalen != 0)
755: fatalx("bad MSG_WAKEUP size");
756:
757: if (!(c->flags & CLIENT_SUSPENDED))
758: break;
759: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 760:
1.11 nicm 761: if (gettimeofday(&c->activity_time, NULL) != 0)
762: fatal("gettimeofday");
1.47 nicm 763: if (c->session != NULL)
764: session_update_activity(c->session);
1.10 nicm 765:
1.1 nicm 766: tty_start_tty(&c->tty);
767: server_redraw_client(c);
768: recalculate_sizes();
769: break;
770: case MSG_ENVIRON:
771: if (datalen != sizeof environdata)
772: fatalx("bad MSG_ENVIRON size");
773: memcpy(&environdata, imsg.data, sizeof environdata);
774:
775: environdata.var[(sizeof environdata.var) - 1] = '\0';
776: if (strchr(environdata.var, '=') != NULL)
777: environ_put(&c->environ, environdata.var);
778: break;
779: case MSG_SHELL:
780: if (datalen != 0)
781: fatalx("bad MSG_SHELL size");
782:
783: server_client_msg_shell(c);
784: break;
785: default:
786: fatalx("unexpected message");
787: }
788:
789: imsg_free(&imsg);
790: }
791: }
792:
793: /* Callback to send error message to client. */
794: void printflike2
795: server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
796: {
1.33 nicm 797: va_list ap;
1.1 nicm 798:
799: va_start(ap, fmt);
1.73 nicm 800: evbuffer_add_vprintf(ctx->cmdclient->stderr_data, fmt, ap);
1.1 nicm 801: va_end(ap);
802:
1.73 nicm 803: evbuffer_add(ctx->cmdclient->stderr_data, "\n", 1);
804: server_push_stderr(ctx->cmdclient);
1.34 nicm 805: ctx->cmdclient->retcode = 1;
1.1 nicm 806: }
807:
808: /* Callback to send print message to client. */
809: void printflike2
810: server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
811: {
1.33 nicm 812: va_list ap;
1.1 nicm 813:
814: va_start(ap, fmt);
1.73 nicm 815: evbuffer_add_vprintf(ctx->cmdclient->stdout_data, fmt, ap);
1.1 nicm 816: va_end(ap);
817:
1.73 nicm 818: evbuffer_add(ctx->cmdclient->stdout_data, "\n", 1);
819: server_push_stdout(ctx->cmdclient);
1.1 nicm 820: }
821:
822: /* Callback to send print message to client, if not quiet. */
823: void printflike2
824: server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
825: {
1.33 nicm 826: va_list ap;
1.1 nicm 827:
1.26 nicm 828: if (options_get_number(&global_options, "quiet"))
1.1 nicm 829: return;
830:
831: va_start(ap, fmt);
1.73 nicm 832: evbuffer_add_vprintf(ctx->cmdclient->stdout_data, fmt, ap);
1.1 nicm 833: va_end(ap);
834:
1.73 nicm 835: evbuffer_add(ctx->cmdclient->stdout_data, "\n", 1);
836: server_push_stdout(ctx->cmdclient);
1.1 nicm 837: }
838:
839: /* Handle command message. */
840: void
841: server_client_msg_command(struct client *c, struct msg_command_data *data)
842: {
1.36 nicm 843: struct cmd_ctx ctx;
844: struct cmd_list *cmdlist = NULL;
845: int argc;
846: char **argv, *cause;
1.1 nicm 847:
848: ctx.error = server_client_msg_error;
849: ctx.print = server_client_msg_print;
850: ctx.info = server_client_msg_info;
851:
852: ctx.msgdata = data;
853: ctx.curclient = NULL;
854:
855: ctx.cmdclient = c;
856:
857: argc = data->argc;
858: data->argv[(sizeof data->argv) - 1] = '\0';
859: if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
860: server_client_msg_error(&ctx, "command too long");
861: goto error;
862: }
863:
864: if (argc == 0) {
865: argc = 1;
866: argv = xcalloc(1, sizeof *argv);
867: *argv = xstrdup("new-session");
868: }
869:
870: if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
871: server_client_msg_error(&ctx, "%s", cause);
872: cmd_free_argv(argc, argv);
873: goto error;
874: }
875: cmd_free_argv(argc, argv);
876:
1.36 nicm 877: if (cmd_list_exec(cmdlist, &ctx) != 1)
878: c->flags |= CLIENT_EXIT;
1.1 nicm 879: cmd_list_free(cmdlist);
880: return;
881:
882: error:
883: if (cmdlist != NULL)
884: cmd_list_free(cmdlist);
1.36 nicm 885: c->flags |= CLIENT_EXIT;
1.1 nicm 886: }
887:
888: /* Handle identify message. */
889: void
890: server_client_msg_identify(
891: struct client *c, struct msg_identify_data *data, int fd)
892: {
893: c->cwd = NULL;
894: data->cwd[(sizeof data->cwd) - 1] = '\0';
895: if (*data->cwd != '\0')
896: c->cwd = xstrdup(data->cwd);
1.75 nicm 897:
898: if (data->flags & IDENTIFY_CONTROL) {
899: c->stdin_callback = control_callback;
900: c->flags |= (CLIENT_CONTROL|CLIENT_SUSPENDED);
901:
902: c->tty.fd = -1;
903: c->tty.log_fd = -1;
904:
905: close(fd);
906: return;
907: }
1.1 nicm 908:
1.33 nicm 909: if (!isatty(fd))
910: return;
1.1 nicm 911: data->term[(sizeof data->term) - 1] = '\0';
1.74 nicm 912: tty_init(&c->tty, c, fd, data->term);
1.1 nicm 913: if (data->flags & IDENTIFY_UTF8)
914: c->tty.flags |= TTY_UTF8;
915: if (data->flags & IDENTIFY_256COLOURS)
916: c->tty.term_flags |= TERM_256COLOURS;
917: else if (data->flags & IDENTIFY_88COLOURS)
918: c->tty.term_flags |= TERM_88COLOURS;
919:
920: tty_resize(&c->tty);
921:
922: c->flags |= CLIENT_TERMINAL;
923: }
924:
925: /* Handle shell message. */
926: void
927: server_client_msg_shell(struct client *c)
928: {
929: struct msg_shell_data data;
930: const char *shell;
1.25 nicm 931:
1.1 nicm 932: shell = options_get_string(&global_s_options, "default-shell");
933:
934: if (*shell == '\0' || areshell(shell))
935: shell = _PATH_BSHELL;
936: if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
937: strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
1.25 nicm 938:
1.1 nicm 939: server_write_client(c, MSG_SHELL, &data, sizeof data);
940: c->flags |= CLIENT_BAD; /* it will die after exec */
941: }