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