Accept colours of the hex form #ffffff and translate to the nearest from
[tmux-openbsd.git] / server-client.c
blob24a85b56c12a16757b6f728f5f592707a65d4f38
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 int mode;
57 u_int i;
59 if ((mode = fcntl(fd, F_GETFL)) == -1)
60 fatal("fcntl failed");
61 if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
62 fatal("fcntl failed");
64 c = xcalloc(1, sizeof *c);
65 c->references = 0;
66 imsg_init(&c->ibuf, fd);
67 server_update_event(c);
69 if (gettimeofday(&c->creation_time, NULL) != 0)
70 fatal("gettimeofday failed");
71 memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
73 c->stdin_event = NULL;
74 c->stdout_event = NULL;
75 c->stderr_event = NULL;
77 c->tty.fd = -1;
78 c->title = NULL;
80 c->session = NULL;
81 c->last_session = NULL;
82 c->tty.sx = 80;
83 c->tty.sy = 24;
85 screen_init(&c->status, c->tty.sx, 1, 0);
86 job_tree_init(&c->status_jobs);
88 c->message_string = NULL;
89 ARRAY_INIT(&c->message_log);
91 c->prompt_string = NULL;
92 c->prompt_buffer = NULL;
93 c->prompt_index = 0;
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 close(c->stdin_fd);
129 if (c->stdin_event != NULL)
130 bufferevent_free(c->stdin_event);
131 if (c->stdout_fd != -1)
132 close(c->stdout_fd);
133 if (c->stdout_event != NULL)
134 bufferevent_free(c->stdout_event);
135 if (c->stderr_fd != -1)
136 close(c->stderr_fd);
137 if (c->stderr_event != NULL)
138 bufferevent_free(c->stderr_event);
140 screen_free(&c->status);
141 job_tree_free(&c->status_jobs);
143 if (c->title != NULL)
144 xfree(c->title);
146 evtimer_del(&c->repeat_timer);
148 evtimer_del(&c->identify_timer);
150 if (c->message_string != NULL)
151 xfree(c->message_string);
152 evtimer_del(&c->message_timer);
153 for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
154 msg = &ARRAY_ITEM(&c->message_log, i);
155 xfree(msg->msg);
157 ARRAY_FREE(&c->message_log);
159 if (c->prompt_string != NULL)
160 xfree(c->prompt_string);
161 if (c->prompt_buffer != NULL)
162 xfree(c->prompt_buffer);
164 if (c->cwd != NULL)
165 xfree(c->cwd);
167 close(c->ibuf.fd);
168 imsg_clear(&c->ibuf);
169 event_del(&c->event);
171 for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
172 if (ARRAY_ITEM(&dead_clients, i) == NULL) {
173 ARRAY_SET(&dead_clients, i, c);
174 break;
177 if (i == ARRAY_LENGTH(&dead_clients))
178 ARRAY_ADD(&dead_clients, c);
179 c->flags |= CLIENT_DEAD;
181 recalculate_sizes();
182 server_check_unattached();
183 server_update_socket();
186 /* Process a single client event. */
187 void
188 server_client_callback(int fd, short events, void *data)
190 struct client *c = data;
192 if (c->flags & CLIENT_DEAD)
193 return;
195 if (fd == c->ibuf.fd) {
196 if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
197 goto client_lost;
199 if (c->flags & CLIENT_BAD) {
200 if (c->ibuf.w.queued == 0)
201 goto client_lost;
202 return;
205 if (events & EV_READ && server_client_msg_dispatch(c) != 0)
206 goto client_lost;
209 server_update_event(c);
210 return;
212 client_lost:
213 server_client_lost(c);
216 /* Handle client status timer. */
217 void
218 server_client_status_timer(void)
220 struct client *c;
221 struct session *s;
222 struct job *job;
223 struct timeval tv;
224 u_int i;
225 int interval;
226 time_t difference;
228 if (gettimeofday(&tv, NULL) != 0)
229 fatal("gettimeofday failed");
231 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
232 c = ARRAY_ITEM(&clients, i);
233 if (c == NULL || c->session == NULL)
234 continue;
235 if (c->message_string != NULL || c->prompt_string != NULL) {
237 * Don't need timed redraw for messages/prompts so bail
238 * now. The status timer isn't reset when they are
239 * redrawn anyway.
241 continue;
243 s = c->session;
245 if (!options_get_number(&s->options, "status"))
246 continue;
247 interval = options_get_number(&s->options, "status-interval");
249 difference = tv.tv_sec - c->status_timer.tv_sec;
250 if (difference >= interval) {
251 RB_FOREACH(job, jobs, &c->status_jobs)
252 job_run(job);
253 c->flags |= CLIENT_STATUS;
258 /* Handle data key input from client. */
259 void
260 server_client_handle_key(int key, struct mouse_event *mouse, void *data)
262 struct client *c = data;
263 struct session *s;
264 struct window *w;
265 struct window_pane *wp;
266 struct options *oo;
267 struct timeval tv;
268 struct key_binding *bd;
269 struct keylist *keylist;
270 int xtimeout, isprefix;
271 u_int i;
273 /* Check the client is good to accept input. */
274 if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
275 return;
276 if (c->session == NULL)
277 return;
278 s = c->session;
280 /* Update the activity timer. */
281 if (gettimeofday(&c->activity_time, NULL) != 0)
282 fatal("gettimeofday failed");
283 memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
285 w = c->session->curw->window;
286 wp = w->active;
287 oo = &c->session->options;
289 /* Special case: number keys jump to pane in identify mode. */
290 if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
291 if (c->flags & CLIENT_READONLY)
292 return;
293 wp = window_pane_at_index(w, key - '0');
294 if (wp != NULL && window_pane_visible(wp))
295 window_set_active_pane(w, wp);
296 server_clear_identify(c);
297 return;
300 /* Handle status line. */
301 if (!(c->flags & CLIENT_READONLY)) {
302 status_message_clear(c);
303 server_clear_identify(c);
305 if (c->prompt_string != NULL) {
306 if (!(c->flags & CLIENT_READONLY))
307 status_prompt_key(c, key);
308 return;
311 /* Check for mouse keys. */
312 if (key == KEYC_MOUSE) {
313 if (c->flags & CLIENT_READONLY)
314 return;
315 if (options_get_number(oo, "mouse-select-pane")) {
316 window_set_active_at(w, mouse->x, mouse->y);
317 server_redraw_window_borders(w);
318 wp = w->active;
320 window_pane_mouse(wp, c->session, mouse);
321 return;
324 /* Is this a prefix key? */
325 keylist = options_get_data(&c->session->options, "prefix");
326 isprefix = 0;
327 for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
328 if (key == ARRAY_ITEM(keylist, i)) {
329 isprefix = 1;
330 break;
334 /* No previous prefix key. */
335 if (!(c->flags & CLIENT_PREFIX)) {
336 if (isprefix)
337 c->flags |= CLIENT_PREFIX;
338 else {
339 /* Try as a non-prefix key binding. */
340 if ((bd = key_bindings_lookup(key)) == NULL) {
341 if (!(c->flags & CLIENT_READONLY))
342 window_pane_key(wp, c->session, key);
343 } else
344 key_bindings_dispatch(bd, c);
346 return;
349 /* Prefix key already pressed. Reset prefix and lookup key. */
350 c->flags &= ~CLIENT_PREFIX;
351 if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
352 /* If repeating, treat this as a key, else ignore. */
353 if (c->flags & CLIENT_REPEAT) {
354 c->flags &= ~CLIENT_REPEAT;
355 if (isprefix)
356 c->flags |= CLIENT_PREFIX;
357 else if (!(c->flags & CLIENT_READONLY))
358 window_pane_key(wp, c->session, key);
360 return;
363 /* If already repeating, but this key can't repeat, skip it. */
364 if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
365 c->flags &= ~CLIENT_REPEAT;
366 if (isprefix)
367 c->flags |= CLIENT_PREFIX;
368 else if (!(c->flags & CLIENT_READONLY))
369 window_pane_key(wp, c->session, key);
370 return;
373 /* If this key can repeat, reset the repeat flags and timer. */
374 xtimeout = options_get_number(&c->session->options, "repeat-time");
375 if (xtimeout != 0 && bd->can_repeat) {
376 c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
378 tv.tv_sec = xtimeout / 1000;
379 tv.tv_usec = (xtimeout % 1000) * 1000L;
380 evtimer_del(&c->repeat_timer);
381 evtimer_add(&c->repeat_timer, &tv);
384 /* Dispatch the command. */
385 key_bindings_dispatch(bd, c);
388 /* Client functions that need to happen every loop. */
389 void
390 server_client_loop(void)
392 struct client *c;
393 struct window *w;
394 struct window_pane *wp;
395 u_int i;
397 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
398 c = ARRAY_ITEM(&clients, i);
399 if (c == NULL)
400 continue;
402 server_client_check_exit(c);
403 if (c->session != NULL) {
404 server_client_check_redraw(c);
405 server_client_reset_state(c);
410 * Any windows will have been redrawn as part of clients, so clear
411 * their flags now.
413 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
414 w = ARRAY_ITEM(&windows, i);
415 if (w == NULL)
416 continue;
418 w->flags &= ~WINDOW_REDRAW;
419 TAILQ_FOREACH(wp, &w->panes, entry)
420 wp->flags &= ~PANE_REDRAW;
425 * Update cursor position and mode settings. The scroll region and attributes
426 * are cleared when idle (waiting for an event) as this is the most likely time
427 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
428 * compromise between excessive resets and likelihood of an interrupt.
430 * tty_region/tty_reset/tty_update_mode already take care of not resetting
431 * things that are already in their default state.
433 void
434 server_client_reset_state(struct client *c)
436 struct window *w = c->session->curw->window;
437 struct window_pane *wp = w->active;
438 struct screen *s = wp->screen;
439 struct options *oo = &c->session->options;
440 int status, mode;
442 tty_region(&c->tty, 0, c->tty.sy - 1);
444 status = options_get_number(oo, "status");
445 if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
446 tty_cursor(&c->tty, 0, 0);
447 else
448 tty_cursor(&c->tty, wp->xoff + s->cx, wp->yoff + s->cy);
450 mode = s->mode;
451 if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
452 options_get_number(oo, "mouse-select-pane"))
453 mode |= MODE_MOUSE_STANDARD;
456 * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
457 * user has set mouse-utf8 and any mouse mode is in effect, turn on
458 * UTF-8 mouse input. If the receiving terminal hasn't requested it
459 * (that is, it isn't in s->mode), then it'll be converted in
460 * input_mouse.
462 if ((c->tty.flags & TTY_UTF8) &&
463 (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
464 mode |= MODE_MOUSE_UTF8;
465 else
466 mode &= ~MODE_MOUSE_UTF8;
468 /* Set the terminal mode and reset attributes. */
469 tty_update_mode(&c->tty, mode);
470 tty_reset(&c->tty);
473 /* Repeat time callback. */
474 /* ARGSUSED */
475 void
476 server_client_repeat_timer(unused int fd, unused short events, void *data)
478 struct client *c = data;
480 if (c->flags & CLIENT_REPEAT)
481 c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
484 /* Check if client should be exited. */
485 void
486 server_client_check_exit(struct client *c)
488 struct msg_exit_data exitdata;
490 if (!(c->flags & CLIENT_EXIT))
491 return;
493 if (c->stdout_fd != -1 && c->stdout_event != NULL &&
494 EVBUFFER_LENGTH(c->stdout_event->output) != 0)
495 return;
496 if (c->stderr_fd != -1 && c->stderr_event != NULL &&
497 EVBUFFER_LENGTH(c->stderr_event->output) != 0)
498 return;
500 exitdata.retcode = c->retcode;
501 server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
503 c->flags &= ~CLIENT_EXIT;
507 * Check if the client should backoff. During backoff, data from external
508 * programs is not written to the terminal. When the existing data drains, the
509 * client is redrawn.
511 * There are two backoff phases - both the tty and client have backoff flags -
512 * the first to allow existing data to drain and the latter to ensure backoff
513 * is disabled until the redraw has finished and prevent the redraw triggering
514 * another backoff.
516 void
517 server_client_check_backoff(struct client *c)
519 struct tty *tty = &c->tty;
520 size_t used;
522 used = EVBUFFER_LENGTH(tty->event->output);
525 * If in the second backoff phase (redrawing), don't check backoff
526 * until the redraw has completed (or enough of it to drop below the
527 * backoff threshold).
529 if (c->flags & CLIENT_BACKOFF) {
530 if (used > BACKOFF_THRESHOLD)
531 return;
532 c->flags &= ~CLIENT_BACKOFF;
533 return;
536 /* Once drained, allow data through again and schedule redraw. */
537 if (tty->flags & TTY_BACKOFF) {
538 if (used != 0)
539 return;
540 tty->flags &= ~TTY_BACKOFF;
541 c->flags |= (CLIENT_BACKOFF|CLIENT_REDRAWWINDOW|CLIENT_STATUS);
542 return;
545 /* If too much data, start backoff. */
546 if (used > BACKOFF_THRESHOLD)
547 tty->flags |= TTY_BACKOFF;
550 /* Check for client redraws. */
551 void
552 server_client_check_redraw(struct client *c)
554 struct session *s = c->session;
555 struct window_pane *wp;
556 int flags, redraw;
558 flags = c->tty.flags & TTY_FREEZE;
559 c->tty.flags &= ~TTY_FREEZE;
561 if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
562 if (options_get_number(&s->options, "set-titles"))
563 server_client_set_title(c);
565 if (c->message_string != NULL)
566 redraw = status_message_redraw(c);
567 else if (c->prompt_string != NULL)
568 redraw = status_prompt_redraw(c);
569 else
570 redraw = status_redraw(c);
571 if (!redraw)
572 c->flags &= ~CLIENT_STATUS;
575 if (c->flags & CLIENT_REDRAW) {
576 screen_redraw_screen(c, 0, 0);
577 c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
578 } else if (c->flags & CLIENT_REDRAWWINDOW) {
579 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
580 screen_redraw_pane(c, wp);
581 c->flags &= ~CLIENT_REDRAWWINDOW;
582 } else {
583 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
584 if (wp->flags & PANE_REDRAW)
585 screen_redraw_pane(c, wp);
589 if (c->flags & CLIENT_BORDERS)
590 screen_redraw_screen(c, 0, 1);
592 if (c->flags & CLIENT_STATUS)
593 screen_redraw_screen(c, 1, 0);
595 c->tty.flags |= flags;
597 c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
600 /* Set client title. */
601 void
602 server_client_set_title(struct client *c)
604 struct session *s = c->session;
605 const char *template;
606 char *title;
608 template = options_get_string(&s->options, "set-titles-string");
610 title = status_replace(c, NULL, template, time(NULL), 1);
611 if (c->title == NULL || strcmp(title, c->title) != 0) {
612 if (c->title != NULL)
613 xfree(c->title);
614 c->title = xstrdup(title);
615 tty_set_title(&c->tty, c->title);
617 xfree(title);
621 * Error callback for client stdin. Caller must increase reference count when
622 * enabling event!
624 void
625 server_client_in_callback(
626 unused struct bufferevent *bufev, unused short what, void *data)
628 struct client *c = data;
630 c->references--;
631 if (c->flags & CLIENT_DEAD)
632 return;
634 bufferevent_disable(c->stdin_event, EV_READ|EV_WRITE);
635 close(c->stdin_fd);
636 c->stdin_fd = -1;
638 if (c->stdin_callback != NULL)
639 c->stdin_callback(c, c->stdin_data);
642 /* Error callback for client stdout. */
643 void
644 server_client_out_callback(
645 unused struct bufferevent *bufev, unused short what, unused void *data)
647 struct client *c = data;
649 bufferevent_disable(c->stdout_event, EV_READ|EV_WRITE);
650 close(c->stdout_fd);
651 c->stdout_fd = -1;
654 /* Error callback for client stderr. */
655 void
656 server_client_err_callback(
657 unused struct bufferevent *bufev, unused short what, unused void *data)
659 struct client *c = data;
661 bufferevent_disable(c->stderr_event, EV_READ|EV_WRITE);
662 close(c->stderr_fd);
663 c->stderr_fd = -1;
666 /* Dispatch message from client. */
668 server_client_msg_dispatch(struct client *c)
670 struct imsg imsg;
671 struct msg_command_data commanddata;
672 struct msg_identify_data identifydata;
673 struct msg_environ_data environdata;
674 ssize_t n, datalen;
675 int mode;
677 if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
678 return (-1);
680 for (;;) {
681 if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
682 return (-1);
683 if (n == 0)
684 return (0);
685 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
687 if (imsg.hdr.peerid != PROTOCOL_VERSION) {
688 server_write_client(c, MSG_VERSION, NULL, 0);
689 c->flags |= CLIENT_BAD;
690 imsg_free(&imsg);
691 continue;
694 log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
695 switch (imsg.hdr.type) {
696 case MSG_COMMAND:
697 if (datalen != sizeof commanddata)
698 fatalx("bad MSG_COMMAND size");
699 memcpy(&commanddata, imsg.data, sizeof commanddata);
701 server_client_msg_command(c, &commanddata);
702 break;
703 case MSG_IDENTIFY:
704 if (datalen != sizeof identifydata)
705 fatalx("bad MSG_IDENTIFY size");
706 if (imsg.fd == -1)
707 fatalx("MSG_IDENTIFY missing fd");
708 memcpy(&identifydata, imsg.data, sizeof identifydata);
710 c->stdin_fd = imsg.fd;
711 c->stdin_event = bufferevent_new(c->stdin_fd,
712 NULL, NULL, server_client_in_callback, c);
713 if (c->stdin_event == NULL)
714 fatalx("failed to create stdin event");
716 if ((mode = fcntl(c->stdin_fd, F_GETFL)) != -1)
717 fcntl(c->stdin_fd, F_SETFL, mode|O_NONBLOCK);
719 server_client_msg_identify(c, &identifydata, imsg.fd);
720 break;
721 case MSG_STDOUT:
722 if (datalen != 0)
723 fatalx("bad MSG_STDOUT size");
724 if (imsg.fd == -1)
725 fatalx("MSG_STDOUT missing fd");
727 c->stdout_fd = imsg.fd;
728 c->stdout_event = bufferevent_new(c->stdout_fd,
729 NULL, NULL, server_client_out_callback, c);
730 if (c->stdout_event == NULL)
731 fatalx("failed to create stdout event");
733 if ((mode = fcntl(c->stdout_fd, F_GETFL)) != -1)
734 fcntl(c->stdout_fd, F_SETFL, mode|O_NONBLOCK);
735 break;
736 case MSG_STDERR:
737 if (datalen != 0)
738 fatalx("bad MSG_STDERR size");
739 if (imsg.fd == -1)
740 fatalx("MSG_STDERR missing fd");
742 c->stderr_fd = imsg.fd;
743 c->stderr_event = bufferevent_new(c->stderr_fd,
744 NULL, NULL, server_client_err_callback, c);
745 if (c->stderr_event == NULL)
746 fatalx("failed to create stderr event");
748 if ((mode = fcntl(c->stderr_fd, F_GETFL)) != -1)
749 fcntl(c->stderr_fd, F_SETFL, mode|O_NONBLOCK);
750 break;
751 case MSG_RESIZE:
752 if (datalen != 0)
753 fatalx("bad MSG_RESIZE size");
755 if (tty_resize(&c->tty)) {
756 recalculate_sizes();
757 server_redraw_client(c);
759 break;
760 case MSG_EXITING:
761 if (datalen != 0)
762 fatalx("bad MSG_EXITING size");
764 c->session = NULL;
765 tty_close(&c->tty);
766 server_write_client(c, MSG_EXITED, NULL, 0);
767 break;
768 case MSG_WAKEUP:
769 case MSG_UNLOCK:
770 if (datalen != 0)
771 fatalx("bad MSG_WAKEUP size");
773 if (!(c->flags & CLIENT_SUSPENDED))
774 break;
775 c->flags &= ~CLIENT_SUSPENDED;
777 if (gettimeofday(&c->activity_time, NULL) != 0)
778 fatal("gettimeofday");
779 if (c->session != NULL)
780 session_update_activity(c->session);
782 tty_start_tty(&c->tty);
783 server_redraw_client(c);
784 recalculate_sizes();
785 break;
786 case MSG_ENVIRON:
787 if (datalen != sizeof environdata)
788 fatalx("bad MSG_ENVIRON size");
789 memcpy(&environdata, imsg.data, sizeof environdata);
791 environdata.var[(sizeof environdata.var) - 1] = '\0';
792 if (strchr(environdata.var, '=') != NULL)
793 environ_put(&c->environ, environdata.var);
794 break;
795 case MSG_SHELL:
796 if (datalen != 0)
797 fatalx("bad MSG_SHELL size");
799 server_client_msg_shell(c);
800 break;
801 default:
802 fatalx("unexpected message");
805 imsg_free(&imsg);
809 /* Callback to send error message to client. */
810 void printflike2
811 server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
813 va_list ap;
815 va_start(ap, fmt);
816 evbuffer_add_vprintf(ctx->cmdclient->stderr_event->output, fmt, ap);
817 va_end(ap);
819 bufferevent_write(ctx->cmdclient->stderr_event, "\n", 1);
820 ctx->cmdclient->retcode = 1;
823 /* Callback to send print message to client. */
824 void printflike2
825 server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
827 va_list ap;
829 va_start(ap, fmt);
830 evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
831 va_end(ap);
833 bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
836 /* Callback to send print message to client, if not quiet. */
837 void printflike2
838 server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
840 va_list ap;
842 if (options_get_number(&global_options, "quiet"))
843 return;
845 va_start(ap, fmt);
846 evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
847 va_end(ap);
849 bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
852 /* Handle command message. */
853 void
854 server_client_msg_command(struct client *c, struct msg_command_data *data)
856 struct cmd_ctx ctx;
857 struct cmd_list *cmdlist = NULL;
858 int argc;
859 char **argv, *cause;
861 ctx.error = server_client_msg_error;
862 ctx.print = server_client_msg_print;
863 ctx.info = server_client_msg_info;
865 ctx.msgdata = data;
866 ctx.curclient = NULL;
868 ctx.cmdclient = c;
870 argc = data->argc;
871 data->argv[(sizeof data->argv) - 1] = '\0';
872 if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
873 server_client_msg_error(&ctx, "command too long");
874 goto error;
877 if (argc == 0) {
878 argc = 1;
879 argv = xcalloc(1, sizeof *argv);
880 *argv = xstrdup("new-session");
883 if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
884 server_client_msg_error(&ctx, "%s", cause);
885 cmd_free_argv(argc, argv);
886 goto error;
888 cmd_free_argv(argc, argv);
890 if (cmd_list_exec(cmdlist, &ctx) != 1)
891 c->flags |= CLIENT_EXIT;
892 cmd_list_free(cmdlist);
893 return;
895 error:
896 if (cmdlist != NULL)
897 cmd_list_free(cmdlist);
898 c->flags |= CLIENT_EXIT;
901 /* Handle identify message. */
902 void
903 server_client_msg_identify(
904 struct client *c, struct msg_identify_data *data, int fd)
906 int tty_fd;
908 c->cwd = NULL;
909 data->cwd[(sizeof data->cwd) - 1] = '\0';
910 if (*data->cwd != '\0')
911 c->cwd = xstrdup(data->cwd);
913 if (!isatty(fd))
914 return;
915 if ((tty_fd = dup(fd)) == -1)
916 fatal("dup failed");
917 data->term[(sizeof data->term) - 1] = '\0';
918 tty_init(&c->tty, tty_fd, data->term);
919 if (data->flags & IDENTIFY_UTF8)
920 c->tty.flags |= TTY_UTF8;
921 if (data->flags & IDENTIFY_256COLOURS)
922 c->tty.term_flags |= TERM_256COLOURS;
923 else if (data->flags & IDENTIFY_88COLOURS)
924 c->tty.term_flags |= TERM_88COLOURS;
925 c->tty.key_callback = server_client_handle_key;
926 c->tty.key_data = c;
928 tty_resize(&c->tty);
930 c->flags |= CLIENT_TERMINAL;
933 /* Handle shell message. */
934 void
935 server_client_msg_shell(struct client *c)
937 struct msg_shell_data data;
938 const char *shell;
940 shell = options_get_string(&global_s_options, "default-shell");
942 if (*shell == '\0' || areshell(shell))
943 shell = _PATH_BSHELL;
944 if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
945 strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
947 server_write_client(c, MSG_SHELL, &data, sizeof data);
948 c->flags |= CLIENT_BAD; /* it will die after exec */