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