Add a flag to cmd_find_session so that attach-session can prefer
[tmux-openbsd.git] / server-client.c
blob66e5caf05ef4e7b48d4c46393dbbec2262bc2c3b
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <event.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <time.h>
25 #include <paths.h>
26 #include <unistd.h>
28 #include "tmux.h"
30 void server_client_handle_key(int, struct mouse_event *, void *);
31 void server_client_repeat_timer(int, short, void *);
32 void server_client_check_exit(struct client *);
33 void server_client_check_backoff(struct client *);
34 void server_client_check_redraw(struct client *);
35 void server_client_set_title(struct client *);
36 void server_client_reset_state(struct client *);
37 void server_client_in_callback(struct bufferevent *, short, void *);
38 void server_client_out_callback(struct bufferevent *, short, void *);
39 void server_client_err_callback(struct bufferevent *, short, void *);
41 int server_client_msg_dispatch(struct client *);
42 void server_client_msg_command(struct client *, struct msg_command_data *);
43 void server_client_msg_identify(
44 struct client *, struct msg_identify_data *, int);
45 void server_client_msg_shell(struct client *);
47 void printflike2 server_client_msg_error(struct cmd_ctx *, const char *, ...);
48 void printflike2 server_client_msg_print(struct cmd_ctx *, const char *, ...);
49 void printflike2 server_client_msg_info(struct cmd_ctx *, const char *, ...);
51 /* Create a new client. */
52 void
53 server_client_create(int fd)
55 struct client *c;
56 u_int i;
58 setblocking(fd, 0);
60 c = xcalloc(1, sizeof *c);
61 c->references = 0;
62 imsg_init(&c->ibuf, fd);
63 server_update_event(c);
65 if (gettimeofday(&c->creation_time, NULL) != 0)
66 fatal("gettimeofday failed");
67 memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
69 c->stdin_event = NULL;
70 c->stdout_event = NULL;
71 c->stderr_event = NULL;
73 c->tty.fd = -1;
74 c->title = NULL;
76 c->session = NULL;
77 c->last_session = NULL;
78 c->tty.sx = 80;
79 c->tty.sy = 24;
81 screen_init(&c->status, c->tty.sx, 1, 0);
82 RB_INIT(&c->status_new);
83 RB_INIT(&c->status_old);
85 c->message_string = NULL;
86 ARRAY_INIT(&c->message_log);
88 c->prompt_string = NULL;
89 c->prompt_buffer = NULL;
90 c->prompt_index = 0;
92 evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
94 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
95 if (ARRAY_ITEM(&clients, i) == NULL) {
96 ARRAY_SET(&clients, i, c);
97 return;
100 ARRAY_ADD(&clients, c);
101 log_debug("new client %d", fd);
104 /* Lost a client. */
105 void
106 server_client_lost(struct client *c)
108 struct message_entry *msg;
109 u_int i;
111 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
112 if (ARRAY_ITEM(&clients, i) == c)
113 ARRAY_SET(&clients, i, NULL);
115 log_debug("lost client %d", c->ibuf.fd);
118 * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
119 * and tty_free might close an unrelated fd.
121 if (c->flags & CLIENT_TERMINAL)
122 tty_free(&c->tty);
124 if (c->stdin_fd != -1) {
125 setblocking(c->stdin_fd, 1);
126 close(c->stdin_fd);
128 if (c->stdin_event != NULL)
129 bufferevent_free(c->stdin_event);
130 if (c->stdout_fd != -1) {
131 setblocking(c->stdout_fd, 1);
132 close(c->stdout_fd);
134 if (c->stdout_event != NULL)
135 bufferevent_free(c->stdout_event);
136 if (c->stderr_fd != -1) {
137 setblocking(c->stderr_fd, 1);
138 close(c->stderr_fd);
140 if (c->stderr_event != NULL)
141 bufferevent_free(c->stderr_event);
143 status_free_jobs(&c->status_new);
144 status_free_jobs(&c->status_old);
145 screen_free(&c->status);
147 if (c->title != NULL)
148 xfree(c->title);
150 evtimer_del(&c->repeat_timer);
152 evtimer_del(&c->identify_timer);
154 if (c->message_string != NULL)
155 xfree(c->message_string);
156 evtimer_del(&c->message_timer);
157 for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
158 msg = &ARRAY_ITEM(&c->message_log, i);
159 xfree(msg->msg);
161 ARRAY_FREE(&c->message_log);
163 if (c->prompt_string != NULL)
164 xfree(c->prompt_string);
165 if (c->prompt_buffer != NULL)
166 xfree(c->prompt_buffer);
168 if (c->cwd != NULL)
169 xfree(c->cwd);
171 close(c->ibuf.fd);
172 imsg_clear(&c->ibuf);
173 event_del(&c->event);
175 for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
176 if (ARRAY_ITEM(&dead_clients, i) == NULL) {
177 ARRAY_SET(&dead_clients, i, c);
178 break;
181 if (i == ARRAY_LENGTH(&dead_clients))
182 ARRAY_ADD(&dead_clients, c);
183 c->flags |= CLIENT_DEAD;
185 recalculate_sizes();
186 server_check_unattached();
187 server_update_socket();
190 /* Process a single client event. */
191 void
192 server_client_callback(int fd, short events, void *data)
194 struct client *c = data;
196 if (c->flags & CLIENT_DEAD)
197 return;
199 if (fd == c->ibuf.fd) {
200 if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
201 goto client_lost;
203 if (c->flags & CLIENT_BAD) {
204 if (c->ibuf.w.queued == 0)
205 goto client_lost;
206 return;
209 if (events & EV_READ && server_client_msg_dispatch(c) != 0)
210 goto client_lost;
213 server_update_event(c);
214 return;
216 client_lost:
217 server_client_lost(c);
220 /* Handle client status timer. */
221 void
222 server_client_status_timer(void)
224 struct client *c;
225 struct session *s;
226 struct timeval tv;
227 u_int i;
228 int interval;
229 time_t difference;
231 if (gettimeofday(&tv, NULL) != 0)
232 fatal("gettimeofday failed");
234 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
235 c = ARRAY_ITEM(&clients, i);
236 if (c == NULL || c->session == NULL)
237 continue;
238 if (c->message_string != NULL || c->prompt_string != NULL) {
240 * Don't need timed redraw for messages/prompts so bail
241 * now. The status timer isn't reset when they are
242 * redrawn anyway.
244 continue;
246 s = c->session;
248 if (!options_get_number(&s->options, "status"))
249 continue;
250 interval = options_get_number(&s->options, "status-interval");
252 difference = tv.tv_sec - c->status_timer.tv_sec;
253 if (difference >= interval) {
254 status_update_jobs(c);
255 c->flags |= CLIENT_STATUS;
260 /* Handle data key input from client. */
261 void
262 server_client_handle_key(int key, struct mouse_event *mouse, void *data)
264 struct client *c = data;
265 struct session *s;
266 struct window *w;
267 struct window_pane *wp;
268 struct options *oo;
269 struct timeval tv;
270 struct key_binding *bd;
271 struct keylist *keylist;
272 int xtimeout, isprefix;
273 u_int i;
275 /* Check the client is good to accept input. */
276 if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
277 return;
278 if (c->session == NULL)
279 return;
280 s = c->session;
282 /* Update the activity timer. */
283 if (gettimeofday(&c->activity_time, NULL) != 0)
284 fatal("gettimeofday failed");
285 memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
287 w = c->session->curw->window;
288 wp = w->active;
289 oo = &c->session->options;
291 /* Special case: number keys jump to pane in identify mode. */
292 if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
293 if (c->flags & CLIENT_READONLY)
294 return;
295 wp = window_pane_at_index(w, key - '0');
296 if (wp != NULL && window_pane_visible(wp))
297 window_set_active_pane(w, wp);
298 server_clear_identify(c);
299 return;
302 /* Handle status line. */
303 if (!(c->flags & CLIENT_READONLY)) {
304 status_message_clear(c);
305 server_clear_identify(c);
307 if (c->prompt_string != NULL) {
308 if (!(c->flags & CLIENT_READONLY))
309 status_prompt_key(c, key);
310 return;
313 /* Check for mouse keys. */
314 if (key == KEYC_MOUSE) {
315 if (c->flags & CLIENT_READONLY)
316 return;
317 if (options_get_number(oo, "mouse-select-pane")) {
318 window_set_active_at(w, mouse->x, mouse->y);
319 server_redraw_window_borders(w);
320 wp = w->active;
322 window_pane_mouse(wp, c->session, mouse);
323 return;
326 /* Is this a prefix key? */
327 keylist = options_get_data(&c->session->options, "prefix");
328 isprefix = 0;
329 for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
330 if (key == ARRAY_ITEM(keylist, i)) {
331 isprefix = 1;
332 break;
336 /* No previous prefix key. */
337 if (!(c->flags & CLIENT_PREFIX)) {
338 if (isprefix)
339 c->flags |= CLIENT_PREFIX;
340 else {
341 /* Try as a non-prefix key binding. */
342 if ((bd = key_bindings_lookup(key)) == NULL) {
343 if (!(c->flags & CLIENT_READONLY))
344 window_pane_key(wp, c->session, key);
345 } else
346 key_bindings_dispatch(bd, c);
348 return;
351 /* Prefix key already pressed. Reset prefix and lookup key. */
352 c->flags &= ~CLIENT_PREFIX;
353 if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
354 /* If repeating, treat this as a key, else ignore. */
355 if (c->flags & CLIENT_REPEAT) {
356 c->flags &= ~CLIENT_REPEAT;
357 if (isprefix)
358 c->flags |= CLIENT_PREFIX;
359 else if (!(c->flags & CLIENT_READONLY))
360 window_pane_key(wp, c->session, key);
362 return;
365 /* If already repeating, but this key can't repeat, skip it. */
366 if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
367 c->flags &= ~CLIENT_REPEAT;
368 if (isprefix)
369 c->flags |= CLIENT_PREFIX;
370 else if (!(c->flags & CLIENT_READONLY))
371 window_pane_key(wp, c->session, key);
372 return;
375 /* If this key can repeat, reset the repeat flags and timer. */
376 xtimeout = options_get_number(&c->session->options, "repeat-time");
377 if (xtimeout != 0 && bd->can_repeat) {
378 c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
380 tv.tv_sec = xtimeout / 1000;
381 tv.tv_usec = (xtimeout % 1000) * 1000L;
382 evtimer_del(&c->repeat_timer);
383 evtimer_add(&c->repeat_timer, &tv);
386 /* Dispatch the command. */
387 key_bindings_dispatch(bd, c);
390 /* Client functions that need to happen every loop. */
391 void
392 server_client_loop(void)
394 struct client *c;
395 struct window *w;
396 struct window_pane *wp;
397 u_int i;
399 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
400 c = ARRAY_ITEM(&clients, i);
401 if (c == NULL)
402 continue;
404 server_client_check_exit(c);
405 if (c->session != NULL) {
406 server_client_check_redraw(c);
407 server_client_reset_state(c);
412 * Any windows will have been redrawn as part of clients, so clear
413 * their flags now.
415 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
416 w = ARRAY_ITEM(&windows, i);
417 if (w == NULL)
418 continue;
420 w->flags &= ~WINDOW_REDRAW;
421 TAILQ_FOREACH(wp, &w->panes, entry)
422 wp->flags &= ~PANE_REDRAW;
427 * Update cursor position and mode settings. The scroll region and attributes
428 * are cleared when idle (waiting for an event) as this is the most likely time
429 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
430 * compromise between excessive resets and likelihood of an interrupt.
432 * tty_region/tty_reset/tty_update_mode already take care of not resetting
433 * things that are already in their default state.
435 void
436 server_client_reset_state(struct client *c)
438 struct window *w = c->session->curw->window;
439 struct window_pane *wp = w->active;
440 struct screen *s = wp->screen;
441 struct options *oo = &c->session->options;
442 int status, mode;
444 tty_region(&c->tty, 0, c->tty.sy - 1);
446 status = options_get_number(oo, "status");
447 if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
448 tty_cursor(&c->tty, 0, 0);
449 else
450 tty_cursor(&c->tty, wp->xoff + s->cx, wp->yoff + s->cy);
453 * Any mode will do for mouse-select-pane, but set standard mode if
454 * none.
456 mode = s->mode;
457 if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
458 options_get_number(oo, "mouse-select-pane") &&
459 (mode & ALL_MOUSE_MODES) == 0)
460 mode |= MODE_MOUSE_STANDARD;
463 * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
464 * user has set mouse-utf8 and any mouse mode is in effect, turn on
465 * UTF-8 mouse input. If the receiving terminal hasn't requested it
466 * (that is, it isn't in s->mode), then it'll be converted in
467 * input_mouse.
469 if ((c->tty.flags & TTY_UTF8) &&
470 (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
471 mode |= MODE_MOUSE_UTF8;
472 else
473 mode &= ~MODE_MOUSE_UTF8;
475 /* Set the terminal mode and reset attributes. */
476 tty_update_mode(&c->tty, mode);
477 tty_reset(&c->tty);
480 /* Repeat time callback. */
481 /* ARGSUSED */
482 void
483 server_client_repeat_timer(unused int fd, unused short events, void *data)
485 struct client *c = data;
487 if (c->flags & CLIENT_REPEAT)
488 c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
491 /* Check if client should be exited. */
492 void
493 server_client_check_exit(struct client *c)
495 struct msg_exit_data exitdata;
497 if (!(c->flags & CLIENT_EXIT))
498 return;
500 if (c->stdout_fd != -1 && c->stdout_event != NULL &&
501 EVBUFFER_LENGTH(c->stdout_event->output) != 0)
502 return;
503 if (c->stderr_fd != -1 && c->stderr_event != NULL &&
504 EVBUFFER_LENGTH(c->stderr_event->output) != 0)
505 return;
507 exitdata.retcode = c->retcode;
508 server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
510 c->flags &= ~CLIENT_EXIT;
514 * Check if the client should backoff. During backoff, data from external
515 * programs is not written to the terminal. When the existing data drains, the
516 * client is redrawn.
518 * There are two backoff phases - both the tty and client have backoff flags -
519 * the first to allow existing data to drain and the latter to ensure backoff
520 * is disabled until the redraw has finished and prevent the redraw triggering
521 * another backoff.
523 void
524 server_client_check_backoff(struct client *c)
526 struct tty *tty = &c->tty;
527 size_t used;
529 used = EVBUFFER_LENGTH(tty->event->output);
532 * If in the second backoff phase (redrawing), don't check backoff
533 * until the redraw has completed (or enough of it to drop below the
534 * backoff threshold).
536 if (c->flags & CLIENT_BACKOFF) {
537 if (used > BACKOFF_THRESHOLD)
538 return;
539 c->flags &= ~CLIENT_BACKOFF;
540 return;
543 /* Once drained, allow data through again and schedule redraw. */
544 if (tty->flags & TTY_BACKOFF) {
545 if (used != 0)
546 return;
547 tty->flags &= ~TTY_BACKOFF;
548 c->flags |= (CLIENT_BACKOFF|CLIENT_REDRAWWINDOW|CLIENT_STATUS);
549 return;
552 /* If too much data, start backoff. */
553 if (used > BACKOFF_THRESHOLD)
554 tty->flags |= TTY_BACKOFF;
557 /* Check for client redraws. */
558 void
559 server_client_check_redraw(struct client *c)
561 struct session *s = c->session;
562 struct window_pane *wp;
563 int flags, redraw;
565 flags = c->tty.flags & TTY_FREEZE;
566 c->tty.flags &= ~TTY_FREEZE;
568 if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
569 if (options_get_number(&s->options, "set-titles"))
570 server_client_set_title(c);
572 if (c->message_string != NULL)
573 redraw = status_message_redraw(c);
574 else if (c->prompt_string != NULL)
575 redraw = status_prompt_redraw(c);
576 else
577 redraw = status_redraw(c);
578 if (!redraw)
579 c->flags &= ~CLIENT_STATUS;
582 if (c->flags & CLIENT_REDRAW) {
583 screen_redraw_screen(c, 0, 0);
584 c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
585 } else if (c->flags & CLIENT_REDRAWWINDOW) {
586 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
587 screen_redraw_pane(c, wp);
588 c->flags &= ~CLIENT_REDRAWWINDOW;
589 } else {
590 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
591 if (wp->flags & PANE_REDRAW)
592 screen_redraw_pane(c, wp);
596 if (c->flags & CLIENT_BORDERS)
597 screen_redraw_screen(c, 0, 1);
599 if (c->flags & CLIENT_STATUS)
600 screen_redraw_screen(c, 1, 0);
602 c->tty.flags |= flags;
604 c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
607 /* Set client title. */
608 void
609 server_client_set_title(struct client *c)
611 struct session *s = c->session;
612 const char *template;
613 char *title;
615 template = options_get_string(&s->options, "set-titles-string");
617 title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
618 if (c->title == NULL || strcmp(title, c->title) != 0) {
619 if (c->title != NULL)
620 xfree(c->title);
621 c->title = xstrdup(title);
622 tty_set_title(&c->tty, c->title);
624 xfree(title);
628 * Error callback for client stdin. Caller must increase reference count when
629 * enabling event!
631 void
632 server_client_in_callback(
633 unused struct bufferevent *bufev, unused short what, void *data)
635 struct client *c = data;
637 c->references--;
638 if (c->flags & CLIENT_DEAD)
639 return;
641 bufferevent_disable(c->stdin_event, EV_READ|EV_WRITE);
642 setblocking(c->stdin_fd, 1);
643 close(c->stdin_fd);
644 c->stdin_fd = -1;
646 if (c->stdin_callback != NULL)
647 c->stdin_callback(c, c->stdin_data);
650 /* Error callback for client stdout. */
651 void
652 server_client_out_callback(
653 unused struct bufferevent *bufev, unused short what, unused void *data)
655 struct client *c = data;
657 bufferevent_disable(c->stdout_event, EV_READ|EV_WRITE);
658 setblocking(c->stdout_fd, 1);
659 close(c->stdout_fd);
660 c->stdout_fd = -1;
663 /* Error callback for client stderr. */
664 void
665 server_client_err_callback(
666 unused struct bufferevent *bufev, unused short what, unused void *data)
668 struct client *c = data;
670 bufferevent_disable(c->stderr_event, EV_READ|EV_WRITE);
671 setblocking(c->stderr_fd, 1);
672 close(c->stderr_fd);
673 c->stderr_fd = -1;
676 /* Dispatch message from client. */
678 server_client_msg_dispatch(struct client *c)
680 struct imsg imsg;
681 struct msg_command_data commanddata;
682 struct msg_identify_data identifydata;
683 struct msg_environ_data environdata;
684 ssize_t n, datalen;
686 if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
687 return (-1);
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;
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;
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);
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);
719 c->stdin_fd = imsg.fd;
720 c->stdin_event = bufferevent_new(c->stdin_fd,
721 NULL, NULL, server_client_in_callback, c);
722 if (c->stdin_event == NULL)
723 fatalx("failed to create stdin event");
724 setblocking(c->stdin_fd, 0);
726 server_client_msg_identify(c, &identifydata, imsg.fd);
727 break;
728 case MSG_STDOUT:
729 if (datalen != 0)
730 fatalx("bad MSG_STDOUT size");
731 if (imsg.fd == -1)
732 fatalx("MSG_STDOUT missing fd");
734 c->stdout_fd = imsg.fd;
735 c->stdout_event = bufferevent_new(c->stdout_fd,
736 NULL, NULL, server_client_out_callback, c);
737 if (c->stdout_event == NULL)
738 fatalx("failed to create stdout event");
739 setblocking(c->stdout_fd, 0);
741 break;
742 case MSG_STDERR:
743 if (datalen != 0)
744 fatalx("bad MSG_STDERR size");
745 if (imsg.fd == -1)
746 fatalx("MSG_STDERR missing fd");
748 c->stderr_fd = imsg.fd;
749 c->stderr_event = bufferevent_new(c->stderr_fd,
750 NULL, NULL, server_client_err_callback, c);
751 if (c->stderr_event == NULL)
752 fatalx("failed to create stderr event");
753 setblocking(c->stderr_fd, 0);
755 break;
756 case MSG_RESIZE:
757 if (datalen != 0)
758 fatalx("bad MSG_RESIZE size");
760 if (tty_resize(&c->tty)) {
761 recalculate_sizes();
762 server_redraw_client(c);
764 break;
765 case MSG_EXITING:
766 if (datalen != 0)
767 fatalx("bad MSG_EXITING size");
769 c->session = NULL;
770 tty_close(&c->tty);
771 server_write_client(c, MSG_EXITED, NULL, 0);
772 break;
773 case MSG_WAKEUP:
774 case MSG_UNLOCK:
775 if (datalen != 0)
776 fatalx("bad MSG_WAKEUP size");
778 if (!(c->flags & CLIENT_SUSPENDED))
779 break;
780 c->flags &= ~CLIENT_SUSPENDED;
782 if (gettimeofday(&c->activity_time, NULL) != 0)
783 fatal("gettimeofday");
784 if (c->session != NULL)
785 session_update_activity(c->session);
787 tty_start_tty(&c->tty);
788 server_redraw_client(c);
789 recalculate_sizes();
790 break;
791 case MSG_ENVIRON:
792 if (datalen != sizeof environdata)
793 fatalx("bad MSG_ENVIRON size");
794 memcpy(&environdata, imsg.data, sizeof environdata);
796 environdata.var[(sizeof environdata.var) - 1] = '\0';
797 if (strchr(environdata.var, '=') != NULL)
798 environ_put(&c->environ, environdata.var);
799 break;
800 case MSG_SHELL:
801 if (datalen != 0)
802 fatalx("bad MSG_SHELL size");
804 server_client_msg_shell(c);
805 break;
806 default:
807 fatalx("unexpected message");
810 imsg_free(&imsg);
814 /* Callback to send error message to client. */
815 void printflike2
816 server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
818 va_list ap;
820 va_start(ap, fmt);
821 evbuffer_add_vprintf(ctx->cmdclient->stderr_event->output, fmt, ap);
822 va_end(ap);
824 bufferevent_write(ctx->cmdclient->stderr_event, "\n", 1);
825 ctx->cmdclient->retcode = 1;
828 /* Callback to send print message to client. */
829 void printflike2
830 server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
832 va_list ap;
834 va_start(ap, fmt);
835 evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
836 va_end(ap);
838 bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
841 /* Callback to send print message to client, if not quiet. */
842 void printflike2
843 server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
845 va_list ap;
847 if (options_get_number(&global_options, "quiet"))
848 return;
850 va_start(ap, fmt);
851 evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
852 va_end(ap);
854 bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
857 /* Handle command message. */
858 void
859 server_client_msg_command(struct client *c, struct msg_command_data *data)
861 struct cmd_ctx ctx;
862 struct cmd_list *cmdlist = NULL;
863 int argc;
864 char **argv, *cause;
866 ctx.error = server_client_msg_error;
867 ctx.print = server_client_msg_print;
868 ctx.info = server_client_msg_info;
870 ctx.msgdata = data;
871 ctx.curclient = NULL;
873 ctx.cmdclient = c;
875 argc = data->argc;
876 data->argv[(sizeof data->argv) - 1] = '\0';
877 if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
878 server_client_msg_error(&ctx, "command too long");
879 goto error;
882 if (argc == 0) {
883 argc = 1;
884 argv = xcalloc(1, sizeof *argv);
885 *argv = xstrdup("new-session");
888 if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
889 server_client_msg_error(&ctx, "%s", cause);
890 cmd_free_argv(argc, argv);
891 goto error;
893 cmd_free_argv(argc, argv);
895 if (cmd_list_exec(cmdlist, &ctx) != 1)
896 c->flags |= CLIENT_EXIT;
897 cmd_list_free(cmdlist);
898 return;
900 error:
901 if (cmdlist != NULL)
902 cmd_list_free(cmdlist);
903 c->flags |= CLIENT_EXIT;
906 /* Handle identify message. */
907 void
908 server_client_msg_identify(
909 struct client *c, struct msg_identify_data *data, int fd)
911 int tty_fd;
913 c->cwd = NULL;
914 data->cwd[(sizeof data->cwd) - 1] = '\0';
915 if (*data->cwd != '\0')
916 c->cwd = xstrdup(data->cwd);
918 if (!isatty(fd))
919 return;
920 if ((tty_fd = dup(fd)) == -1)
921 fatal("dup failed");
922 data->term[(sizeof data->term) - 1] = '\0';
923 tty_init(&c->tty, tty_fd, data->term);
924 if (data->flags & IDENTIFY_UTF8)
925 c->tty.flags |= TTY_UTF8;
926 if (data->flags & IDENTIFY_256COLOURS)
927 c->tty.term_flags |= TERM_256COLOURS;
928 else if (data->flags & IDENTIFY_88COLOURS)
929 c->tty.term_flags |= TERM_88COLOURS;
930 c->tty.key_callback = server_client_handle_key;
931 c->tty.key_data = c;
933 tty_resize(&c->tty);
935 c->flags |= CLIENT_TERMINAL;
938 /* Handle shell message. */
939 void
940 server_client_msg_shell(struct client *c)
942 struct msg_shell_data data;
943 const char *shell;
945 shell = options_get_string(&global_s_options, "default-shell");
947 if (*shell == '\0' || areshell(shell))
948 shell = _PATH_BSHELL;
949 if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
950 strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
952 server_write_client(c, MSG_SHELL, &data, sizeof data);
953 c->flags |= CLIENT_BAD; /* it will die after exec */