Support DECSCUSR sequence to set the cursor style with two new
[tmux-openbsd.git] / server-client.c
blobf4dd1fb28c9fb7de2240c9fff97891ca28ef713f
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 c->last_mouse.b = MOUSE_UP;
93 c->last_mouse.x = c->last_mouse.y = -1;
95 evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
97 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
98 if (ARRAY_ITEM(&clients, i) == NULL) {
99 ARRAY_SET(&clients, i, c);
100 return;
103 ARRAY_ADD(&clients, c);
104 log_debug("new client %d", fd);
107 /* Lost a client. */
108 void
109 server_client_lost(struct client *c)
111 struct message_entry *msg;
112 u_int i;
114 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
115 if (ARRAY_ITEM(&clients, i) == c)
116 ARRAY_SET(&clients, i, NULL);
118 log_debug("lost client %d", c->ibuf.fd);
121 * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
122 * and tty_free might close an unrelated fd.
124 if (c->flags & CLIENT_TERMINAL)
125 tty_free(&c->tty);
127 if (c->stdin_fd != -1) {
128 setblocking(c->stdin_fd, 1);
129 close(c->stdin_fd);
131 if (c->stdin_event != NULL)
132 bufferevent_free(c->stdin_event);
133 if (c->stdout_fd != -1) {
134 setblocking(c->stdout_fd, 1);
135 close(c->stdout_fd);
137 if (c->stdout_event != NULL)
138 bufferevent_free(c->stdout_event);
139 if (c->stderr_fd != -1) {
140 setblocking(c->stderr_fd, 1);
141 close(c->stderr_fd);
143 if (c->stderr_event != NULL)
144 bufferevent_free(c->stderr_event);
146 status_free_jobs(&c->status_new);
147 status_free_jobs(&c->status_old);
148 screen_free(&c->status);
150 if (c->title != NULL)
151 xfree(c->title);
153 evtimer_del(&c->repeat_timer);
155 evtimer_del(&c->identify_timer);
157 if (c->message_string != NULL)
158 xfree(c->message_string);
159 evtimer_del(&c->message_timer);
160 for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
161 msg = &ARRAY_ITEM(&c->message_log, i);
162 xfree(msg->msg);
164 ARRAY_FREE(&c->message_log);
166 if (c->prompt_string != NULL)
167 xfree(c->prompt_string);
168 if (c->prompt_buffer != NULL)
169 xfree(c->prompt_buffer);
171 if (c->cwd != NULL)
172 xfree(c->cwd);
174 close(c->ibuf.fd);
175 imsg_clear(&c->ibuf);
176 event_del(&c->event);
178 for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
179 if (ARRAY_ITEM(&dead_clients, i) == NULL) {
180 ARRAY_SET(&dead_clients, i, c);
181 break;
184 if (i == ARRAY_LENGTH(&dead_clients))
185 ARRAY_ADD(&dead_clients, c);
186 c->flags |= CLIENT_DEAD;
188 recalculate_sizes();
189 server_check_unattached();
190 server_update_socket();
193 /* Process a single client event. */
194 void
195 server_client_callback(int fd, short events, void *data)
197 struct client *c = data;
199 if (c->flags & CLIENT_DEAD)
200 return;
202 if (fd == c->ibuf.fd) {
203 if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
204 goto client_lost;
206 if (c->flags & CLIENT_BAD) {
207 if (c->ibuf.w.queued == 0)
208 goto client_lost;
209 return;
212 if (events & EV_READ && server_client_msg_dispatch(c) != 0)
213 goto client_lost;
216 server_update_event(c);
217 return;
219 client_lost:
220 server_client_lost(c);
223 /* Handle client status timer. */
224 void
225 server_client_status_timer(void)
227 struct client *c;
228 struct session *s;
229 struct timeval tv;
230 u_int i;
231 int interval;
232 time_t difference;
234 if (gettimeofday(&tv, NULL) != 0)
235 fatal("gettimeofday failed");
237 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
238 c = ARRAY_ITEM(&clients, i);
239 if (c == NULL || c->session == NULL)
240 continue;
241 if (c->message_string != NULL || c->prompt_string != NULL) {
243 * Don't need timed redraw for messages/prompts so bail
244 * now. The status timer isn't reset when they are
245 * redrawn anyway.
247 continue;
249 s = c->session;
251 if (!options_get_number(&s->options, "status"))
252 continue;
253 interval = options_get_number(&s->options, "status-interval");
255 difference = tv.tv_sec - c->status_timer.tv_sec;
256 if (difference >= interval) {
257 status_update_jobs(c);
258 c->flags |= CLIENT_STATUS;
263 /* Handle data key input from client. */
264 void
265 server_client_handle_key(int key, struct mouse_event *mouse, void *data)
267 struct client *c = data;
268 struct session *s;
269 struct window *w;
270 struct window_pane *wp;
271 struct options *oo;
272 struct timeval tv;
273 struct key_binding *bd;
274 struct keylist *keylist;
275 int xtimeout, isprefix;
276 u_int i;
278 /* Check the client is good to accept input. */
279 if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
280 return;
281 if (c->session == NULL)
282 return;
283 s = c->session;
285 /* Update the activity timer. */
286 if (gettimeofday(&c->activity_time, NULL) != 0)
287 fatal("gettimeofday failed");
288 memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
290 w = c->session->curw->window;
291 wp = w->active;
292 oo = &c->session->options;
294 /* Special case: number keys jump to pane in identify mode. */
295 if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
296 if (c->flags & CLIENT_READONLY)
297 return;
298 wp = window_pane_at_index(w, key - '0');
299 if (wp != NULL && window_pane_visible(wp))
300 window_set_active_pane(w, wp);
301 server_clear_identify(c);
302 return;
305 /* Handle status line. */
306 if (!(c->flags & CLIENT_READONLY)) {
307 status_message_clear(c);
308 server_clear_identify(c);
310 if (c->prompt_string != NULL) {
311 if (!(c->flags & CLIENT_READONLY))
312 status_prompt_key(c, key);
313 return;
316 /* Check for mouse keys. */
317 if (key == KEYC_MOUSE) {
318 if (c->flags & CLIENT_READONLY)
319 return;
320 if (options_get_number(oo, "mouse-select-pane") &&
321 ((!(mouse->b & MOUSE_DRAG) && mouse->b != MOUSE_UP) ||
322 wp->mode != &window_copy_mode)) {
324 * Allow pane switching in copy mode only by mouse down
325 * (click).
327 window_set_active_at(w, mouse->x, mouse->y);
328 server_redraw_window_borders(w);
329 wp = w->active;
331 if (mouse->y + 1 == c->tty.sy &&
332 options_get_number(oo, "mouse-select-window") &&
333 options_get_number(oo, "status")) {
334 if (mouse->b == MOUSE_UP &&
335 c->last_mouse.b != MOUSE_UP) {
336 status_set_window_at(c, mouse->x);
337 return;
339 if (mouse->b & MOUSE_45) {
340 if ((mouse->b & MOUSE_BUTTON) == MOUSE_1) {
341 session_previous(c->session, 0);
342 server_redraw_session(s);
344 if ((mouse->b & MOUSE_BUTTON) == MOUSE_2) {
345 session_next(c->session, 0);
346 server_redraw_session(s);
348 return;
351 if (options_get_number(oo, "mouse-resize-pane"))
352 layout_resize_pane_mouse(c, mouse);
353 memcpy(&c->last_mouse, mouse, sizeof c->last_mouse);
354 window_pane_mouse(wp, c->session, mouse);
355 return;
358 /* Is this a prefix key? */
359 keylist = options_get_data(&c->session->options, "prefix");
360 isprefix = 0;
361 for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
362 if (key == ARRAY_ITEM(keylist, i)) {
363 isprefix = 1;
364 break;
368 /* No previous prefix key. */
369 if (!(c->flags & CLIENT_PREFIX)) {
370 if (isprefix)
371 c->flags |= CLIENT_PREFIX;
372 else {
373 /* Try as a non-prefix key binding. */
374 if ((bd = key_bindings_lookup(key)) == NULL) {
375 if (!(c->flags & CLIENT_READONLY))
376 window_pane_key(wp, c->session, key);
377 } else
378 key_bindings_dispatch(bd, c);
380 return;
383 /* Prefix key already pressed. Reset prefix and lookup key. */
384 c->flags &= ~CLIENT_PREFIX;
385 if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
386 /* If repeating, treat this as a key, else ignore. */
387 if (c->flags & CLIENT_REPEAT) {
388 c->flags &= ~CLIENT_REPEAT;
389 if (isprefix)
390 c->flags |= CLIENT_PREFIX;
391 else if (!(c->flags & CLIENT_READONLY))
392 window_pane_key(wp, c->session, key);
394 return;
397 /* If already repeating, but this key can't repeat, skip it. */
398 if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
399 c->flags &= ~CLIENT_REPEAT;
400 if (isprefix)
401 c->flags |= CLIENT_PREFIX;
402 else if (!(c->flags & CLIENT_READONLY))
403 window_pane_key(wp, c->session, key);
404 return;
407 /* If this key can repeat, reset the repeat flags and timer. */
408 xtimeout = options_get_number(&c->session->options, "repeat-time");
409 if (xtimeout != 0 && bd->can_repeat) {
410 c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
412 tv.tv_sec = xtimeout / 1000;
413 tv.tv_usec = (xtimeout % 1000) * 1000L;
414 evtimer_del(&c->repeat_timer);
415 evtimer_add(&c->repeat_timer, &tv);
418 /* Dispatch the command. */
419 key_bindings_dispatch(bd, c);
422 /* Client functions that need to happen every loop. */
423 void
424 server_client_loop(void)
426 struct client *c;
427 struct window *w;
428 struct window_pane *wp;
429 u_int i;
431 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
432 c = ARRAY_ITEM(&clients, i);
433 if (c == NULL)
434 continue;
436 server_client_check_exit(c);
437 if (c->session != NULL) {
438 server_client_check_redraw(c);
439 server_client_reset_state(c);
444 * Any windows will have been redrawn as part of clients, so clear
445 * their flags now.
447 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
448 w = ARRAY_ITEM(&windows, i);
449 if (w == NULL)
450 continue;
452 w->flags &= ~WINDOW_REDRAW;
453 TAILQ_FOREACH(wp, &w->panes, entry)
454 wp->flags &= ~PANE_REDRAW;
459 * Update cursor position and mode settings. The scroll region and attributes
460 * are cleared when idle (waiting for an event) as this is the most likely time
461 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
462 * compromise between excessive resets and likelihood of an interrupt.
464 * tty_region/tty_reset/tty_update_mode already take care of not resetting
465 * things that are already in their default state.
467 void
468 server_client_reset_state(struct client *c)
470 struct window *w = c->session->curw->window;
471 struct window_pane *wp = w->active;
472 struct screen *s = wp->screen;
473 struct options *oo = &c->session->options;
474 struct options *wo = &w->options;
475 int status, mode;
477 tty_region(&c->tty, 0, c->tty.sy - 1);
479 status = options_get_number(oo, "status");
480 if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
481 tty_cursor(&c->tty, 0, 0);
482 else
483 tty_cursor(&c->tty, wp->xoff + s->cx, wp->yoff + s->cy);
486 * Resizing panes with the mouse requires at least button mode to give
487 * a smooth appearance.
489 mode = s->mode;
490 if ((c->last_mouse.b & MOUSE_RESIZE_PANE) &&
491 !(mode & (MODE_MOUSE_BUTTON|MODE_MOUSE_ANY)))
492 mode |= MODE_MOUSE_BUTTON;
495 * Any mode will do for mouse-select-pane, but set standard mode if
496 * none.
498 if ((mode & ALL_MOUSE_MODES) == 0) {
499 if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
500 options_get_number(oo, "mouse-select-pane"))
501 mode |= MODE_MOUSE_STANDARD;
502 else if (options_get_number(oo, "mouse-resize-pane"))
503 mode |= MODE_MOUSE_STANDARD;
504 else if (options_get_number(oo, "mouse-select-window"))
505 mode |= MODE_MOUSE_STANDARD;
506 else if (options_get_number(wo, "mode-mouse"))
507 mode |= MODE_MOUSE_STANDARD;
511 * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
512 * user has set mouse-utf8 and any mouse mode is in effect, turn on
513 * UTF-8 mouse input. If the receiving terminal hasn't requested it
514 * (that is, it isn't in s->mode), then it'll be converted in
515 * input_mouse.
517 if ((c->tty.flags & TTY_UTF8) &&
518 (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
519 mode |= MODE_MOUSE_UTF8;
520 else
521 mode &= ~MODE_MOUSE_UTF8;
523 /* Set the terminal mode and reset attributes. */
524 tty_update_mode(&c->tty, mode, s);
525 tty_reset(&c->tty);
528 /* Repeat time callback. */
529 /* ARGSUSED */
530 void
531 server_client_repeat_timer(unused int fd, unused short events, void *data)
533 struct client *c = data;
535 if (c->flags & CLIENT_REPEAT)
536 c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
539 /* Check if client should be exited. */
540 void
541 server_client_check_exit(struct client *c)
543 struct msg_exit_data exitdata;
545 if (!(c->flags & CLIENT_EXIT))
546 return;
548 if (c->stdout_fd != -1 && c->stdout_event != NULL &&
549 EVBUFFER_LENGTH(c->stdout_event->output) != 0)
550 return;
551 if (c->stderr_fd != -1 && c->stderr_event != NULL &&
552 EVBUFFER_LENGTH(c->stderr_event->output) != 0)
553 return;
555 exitdata.retcode = c->retcode;
556 server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
558 c->flags &= ~CLIENT_EXIT;
562 * Check if the client should backoff. During backoff, data from external
563 * programs is not written to the terminal. When the existing data drains, the
564 * client is redrawn.
566 * There are two backoff phases - both the tty and client have backoff flags -
567 * the first to allow existing data to drain and the latter to ensure backoff
568 * is disabled until the redraw has finished and prevent the redraw triggering
569 * another backoff.
571 void
572 server_client_check_backoff(struct client *c)
574 struct tty *tty = &c->tty;
575 size_t used;
577 used = EVBUFFER_LENGTH(tty->event->output);
580 * If in the second backoff phase (redrawing), don't check backoff
581 * until the redraw has completed (or enough of it to drop below the
582 * backoff threshold).
584 if (c->flags & CLIENT_BACKOFF) {
585 if (used > BACKOFF_THRESHOLD)
586 return;
587 c->flags &= ~CLIENT_BACKOFF;
588 return;
591 /* Once drained, allow data through again and schedule redraw. */
592 if (tty->flags & TTY_BACKOFF) {
593 if (used != 0)
594 return;
595 tty->flags &= ~TTY_BACKOFF;
596 c->flags |= (CLIENT_BACKOFF|CLIENT_REDRAWWINDOW|CLIENT_STATUS);
597 return;
600 /* If too much data, start backoff. */
601 if (used > BACKOFF_THRESHOLD)
602 tty->flags |= TTY_BACKOFF;
605 /* Check for client redraws. */
606 void
607 server_client_check_redraw(struct client *c)
609 struct session *s = c->session;
610 struct window_pane *wp;
611 int flags, redraw;
613 flags = c->tty.flags & TTY_FREEZE;
614 c->tty.flags &= ~TTY_FREEZE;
616 if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
617 if (options_get_number(&s->options, "set-titles"))
618 server_client_set_title(c);
620 if (c->message_string != NULL)
621 redraw = status_message_redraw(c);
622 else if (c->prompt_string != NULL)
623 redraw = status_prompt_redraw(c);
624 else
625 redraw = status_redraw(c);
626 if (!redraw)
627 c->flags &= ~CLIENT_STATUS;
630 if (c->flags & CLIENT_REDRAW) {
631 screen_redraw_screen(c, 0, 0);
632 c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
633 } else if (c->flags & CLIENT_REDRAWWINDOW) {
634 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
635 screen_redraw_pane(c, wp);
636 c->flags &= ~CLIENT_REDRAWWINDOW;
637 } else {
638 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
639 if (wp->flags & PANE_REDRAW)
640 screen_redraw_pane(c, wp);
644 if (c->flags & CLIENT_BORDERS)
645 screen_redraw_screen(c, 0, 1);
647 if (c->flags & CLIENT_STATUS)
648 screen_redraw_screen(c, 1, 0);
650 c->tty.flags |= flags;
652 c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
655 /* Set client title. */
656 void
657 server_client_set_title(struct client *c)
659 struct session *s = c->session;
660 const char *template;
661 char *title;
663 template = options_get_string(&s->options, "set-titles-string");
665 title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
666 if (c->title == NULL || strcmp(title, c->title) != 0) {
667 if (c->title != NULL)
668 xfree(c->title);
669 c->title = xstrdup(title);
670 tty_set_title(&c->tty, c->title);
672 xfree(title);
676 * Error callback for client stdin. Caller must increase reference count when
677 * enabling event!
679 void
680 server_client_in_callback(
681 unused struct bufferevent *bufev, unused short what, void *data)
683 struct client *c = data;
685 c->references--;
686 if (c->flags & CLIENT_DEAD)
687 return;
689 bufferevent_disable(c->stdin_event, EV_READ|EV_WRITE);
690 setblocking(c->stdin_fd, 1);
691 close(c->stdin_fd);
692 c->stdin_fd = -1;
694 if (c->stdin_callback != NULL)
695 c->stdin_callback(c, c->stdin_data);
698 /* Error callback for client stdout. */
699 void
700 server_client_out_callback(
701 unused struct bufferevent *bufev, unused short what, unused void *data)
703 struct client *c = data;
705 bufferevent_disable(c->stdout_event, EV_READ|EV_WRITE);
706 setblocking(c->stdout_fd, 1);
707 close(c->stdout_fd);
708 c->stdout_fd = -1;
711 /* Error callback for client stderr. */
712 void
713 server_client_err_callback(
714 unused struct bufferevent *bufev, unused short what, unused void *data)
716 struct client *c = data;
718 bufferevent_disable(c->stderr_event, EV_READ|EV_WRITE);
719 setblocking(c->stderr_fd, 1);
720 close(c->stderr_fd);
721 c->stderr_fd = -1;
724 /* Dispatch message from client. */
726 server_client_msg_dispatch(struct client *c)
728 struct imsg imsg;
729 struct msg_command_data commanddata;
730 struct msg_identify_data identifydata;
731 struct msg_environ_data environdata;
732 ssize_t n, datalen;
734 if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
735 return (-1);
737 for (;;) {
738 if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
739 return (-1);
740 if (n == 0)
741 return (0);
742 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
744 if (imsg.hdr.peerid != PROTOCOL_VERSION) {
745 server_write_client(c, MSG_VERSION, NULL, 0);
746 c->flags |= CLIENT_BAD;
747 imsg_free(&imsg);
748 continue;
751 log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
752 switch (imsg.hdr.type) {
753 case MSG_COMMAND:
754 if (datalen != sizeof commanddata)
755 fatalx("bad MSG_COMMAND size");
756 memcpy(&commanddata, imsg.data, sizeof commanddata);
758 server_client_msg_command(c, &commanddata);
759 break;
760 case MSG_IDENTIFY:
761 if (datalen != sizeof identifydata)
762 fatalx("bad MSG_IDENTIFY size");
763 if (imsg.fd == -1)
764 fatalx("MSG_IDENTIFY missing fd");
765 memcpy(&identifydata, imsg.data, sizeof identifydata);
767 c->stdin_fd = imsg.fd;
768 c->stdin_event = bufferevent_new(c->stdin_fd,
769 NULL, NULL, server_client_in_callback, c);
770 if (c->stdin_event == NULL)
771 fatalx("failed to create stdin event");
772 setblocking(c->stdin_fd, 0);
774 server_client_msg_identify(c, &identifydata, imsg.fd);
775 break;
776 case MSG_STDOUT:
777 if (datalen != 0)
778 fatalx("bad MSG_STDOUT size");
779 if (imsg.fd == -1)
780 fatalx("MSG_STDOUT missing fd");
782 c->stdout_fd = imsg.fd;
783 c->stdout_event = bufferevent_new(c->stdout_fd,
784 NULL, NULL, server_client_out_callback, c);
785 if (c->stdout_event == NULL)
786 fatalx("failed to create stdout event");
787 setblocking(c->stdout_fd, 0);
789 break;
790 case MSG_STDERR:
791 if (datalen != 0)
792 fatalx("bad MSG_STDERR size");
793 if (imsg.fd == -1)
794 fatalx("MSG_STDERR missing fd");
796 c->stderr_fd = imsg.fd;
797 c->stderr_event = bufferevent_new(c->stderr_fd,
798 NULL, NULL, server_client_err_callback, c);
799 if (c->stderr_event == NULL)
800 fatalx("failed to create stderr event");
801 setblocking(c->stderr_fd, 0);
803 break;
804 case MSG_RESIZE:
805 if (datalen != 0)
806 fatalx("bad MSG_RESIZE size");
808 if (tty_resize(&c->tty)) {
809 recalculate_sizes();
810 server_redraw_client(c);
812 break;
813 case MSG_EXITING:
814 if (datalen != 0)
815 fatalx("bad MSG_EXITING size");
817 c->session = NULL;
818 tty_close(&c->tty);
819 server_write_client(c, MSG_EXITED, NULL, 0);
820 break;
821 case MSG_WAKEUP:
822 case MSG_UNLOCK:
823 if (datalen != 0)
824 fatalx("bad MSG_WAKEUP size");
826 if (!(c->flags & CLIENT_SUSPENDED))
827 break;
828 c->flags &= ~CLIENT_SUSPENDED;
830 if (gettimeofday(&c->activity_time, NULL) != 0)
831 fatal("gettimeofday");
832 if (c->session != NULL)
833 session_update_activity(c->session);
835 tty_start_tty(&c->tty);
836 server_redraw_client(c);
837 recalculate_sizes();
838 break;
839 case MSG_ENVIRON:
840 if (datalen != sizeof environdata)
841 fatalx("bad MSG_ENVIRON size");
842 memcpy(&environdata, imsg.data, sizeof environdata);
844 environdata.var[(sizeof environdata.var) - 1] = '\0';
845 if (strchr(environdata.var, '=') != NULL)
846 environ_put(&c->environ, environdata.var);
847 break;
848 case MSG_SHELL:
849 if (datalen != 0)
850 fatalx("bad MSG_SHELL size");
852 server_client_msg_shell(c);
853 break;
854 default:
855 fatalx("unexpected message");
858 imsg_free(&imsg);
862 /* Callback to send error message to client. */
863 void printflike2
864 server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
866 va_list ap;
868 va_start(ap, fmt);
869 evbuffer_add_vprintf(ctx->cmdclient->stderr_event->output, fmt, ap);
870 va_end(ap);
872 bufferevent_write(ctx->cmdclient->stderr_event, "\n", 1);
873 ctx->cmdclient->retcode = 1;
876 /* Callback to send print message to client. */
877 void printflike2
878 server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
880 va_list ap;
882 va_start(ap, fmt);
883 evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
884 va_end(ap);
886 bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
889 /* Callback to send print message to client, if not quiet. */
890 void printflike2
891 server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
893 va_list ap;
895 if (options_get_number(&global_options, "quiet"))
896 return;
898 va_start(ap, fmt);
899 evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
900 va_end(ap);
902 bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
905 /* Handle command message. */
906 void
907 server_client_msg_command(struct client *c, struct msg_command_data *data)
909 struct cmd_ctx ctx;
910 struct cmd_list *cmdlist = NULL;
911 int argc;
912 char **argv, *cause;
914 ctx.error = server_client_msg_error;
915 ctx.print = server_client_msg_print;
916 ctx.info = server_client_msg_info;
918 ctx.msgdata = data;
919 ctx.curclient = NULL;
921 ctx.cmdclient = c;
923 argc = data->argc;
924 data->argv[(sizeof data->argv) - 1] = '\0';
925 if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
926 server_client_msg_error(&ctx, "command too long");
927 goto error;
930 if (argc == 0) {
931 argc = 1;
932 argv = xcalloc(1, sizeof *argv);
933 *argv = xstrdup("new-session");
936 if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
937 server_client_msg_error(&ctx, "%s", cause);
938 cmd_free_argv(argc, argv);
939 goto error;
941 cmd_free_argv(argc, argv);
943 if (cmd_list_exec(cmdlist, &ctx) != 1)
944 c->flags |= CLIENT_EXIT;
945 cmd_list_free(cmdlist);
946 return;
948 error:
949 if (cmdlist != NULL)
950 cmd_list_free(cmdlist);
951 c->flags |= CLIENT_EXIT;
954 /* Handle identify message. */
955 void
956 server_client_msg_identify(
957 struct client *c, struct msg_identify_data *data, int fd)
959 int tty_fd;
961 c->cwd = NULL;
962 data->cwd[(sizeof data->cwd) - 1] = '\0';
963 if (*data->cwd != '\0')
964 c->cwd = xstrdup(data->cwd);
966 if (!isatty(fd))
967 return;
968 if ((tty_fd = dup(fd)) == -1)
969 fatal("dup failed");
970 data->term[(sizeof data->term) - 1] = '\0';
971 tty_init(&c->tty, tty_fd, data->term);
972 if (data->flags & IDENTIFY_UTF8)
973 c->tty.flags |= TTY_UTF8;
974 if (data->flags & IDENTIFY_256COLOURS)
975 c->tty.term_flags |= TERM_256COLOURS;
976 else if (data->flags & IDENTIFY_88COLOURS)
977 c->tty.term_flags |= TERM_88COLOURS;
978 c->tty.key_callback = server_client_handle_key;
979 c->tty.key_data = c;
981 tty_resize(&c->tty);
983 c->flags |= CLIENT_TERMINAL;
986 /* Handle shell message. */
987 void
988 server_client_msg_shell(struct client *c)
990 struct msg_shell_data data;
991 const char *shell;
993 shell = options_get_string(&global_s_options, "default-shell");
995 if (*shell == '\0' || areshell(shell))
996 shell = _PATH_BSHELL;
997 if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
998 strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
1000 server_write_client(c, MSG_SHELL, &data, sizeof data);
1001 c->flags |= CLIENT_BAD; /* it will die after exec */