Use TMPDIR if set, from Han Boetes.
[tmux-openbsd.git] / server-client.c
blob055f192c5ef62edc9ff34d1191529440fa945a6d
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 job_tree_init(&c->status_jobs);
84 c->message_string = NULL;
85 ARRAY_INIT(&c->message_log);
87 c->prompt_string = NULL;
88 c->prompt_buffer = NULL;
89 c->prompt_index = 0;
91 evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
93 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
94 if (ARRAY_ITEM(&clients, i) == NULL) {
95 ARRAY_SET(&clients, i, c);
96 return;
99 ARRAY_ADD(&clients, c);
100 log_debug("new client %d", fd);
103 /* Lost a client. */
104 void
105 server_client_lost(struct client *c)
107 struct message_entry *msg;
108 u_int i;
110 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
111 if (ARRAY_ITEM(&clients, i) == c)
112 ARRAY_SET(&clients, i, NULL);
114 log_debug("lost client %d", c->ibuf.fd);
117 * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
118 * and tty_free might close an unrelated fd.
120 if (c->flags & CLIENT_TERMINAL)
121 tty_free(&c->tty);
123 if (c->stdin_fd != -1) {
124 setblocking(c->stdin_fd, 1);
125 close(c->stdin_fd);
127 if (c->stdin_event != NULL)
128 bufferevent_free(c->stdin_event);
129 if (c->stdout_fd != -1) {
130 setblocking(c->stdout_fd, 1);
131 close(c->stdout_fd);
133 if (c->stdout_event != NULL)
134 bufferevent_free(c->stdout_event);
135 if (c->stderr_fd != -1) {
136 setblocking(c->stderr_fd, 1);
137 close(c->stderr_fd);
139 if (c->stderr_event != NULL)
140 bufferevent_free(c->stderr_event);
142 screen_free(&c->status);
143 job_tree_free(&c->status_jobs);
145 if (c->title != NULL)
146 xfree(c->title);
148 evtimer_del(&c->repeat_timer);
150 evtimer_del(&c->identify_timer);
152 if (c->message_string != NULL)
153 xfree(c->message_string);
154 evtimer_del(&c->message_timer);
155 for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
156 msg = &ARRAY_ITEM(&c->message_log, i);
157 xfree(msg->msg);
159 ARRAY_FREE(&c->message_log);
161 if (c->prompt_string != NULL)
162 xfree(c->prompt_string);
163 if (c->prompt_buffer != NULL)
164 xfree(c->prompt_buffer);
166 if (c->cwd != NULL)
167 xfree(c->cwd);
169 close(c->ibuf.fd);
170 imsg_clear(&c->ibuf);
171 event_del(&c->event);
173 for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
174 if (ARRAY_ITEM(&dead_clients, i) == NULL) {
175 ARRAY_SET(&dead_clients, i, c);
176 break;
179 if (i == ARRAY_LENGTH(&dead_clients))
180 ARRAY_ADD(&dead_clients, c);
181 c->flags |= CLIENT_DEAD;
183 recalculate_sizes();
184 server_check_unattached();
185 server_update_socket();
188 /* Process a single client event. */
189 void
190 server_client_callback(int fd, short events, void *data)
192 struct client *c = data;
194 if (c->flags & CLIENT_DEAD)
195 return;
197 if (fd == c->ibuf.fd) {
198 if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
199 goto client_lost;
201 if (c->flags & CLIENT_BAD) {
202 if (c->ibuf.w.queued == 0)
203 goto client_lost;
204 return;
207 if (events & EV_READ && server_client_msg_dispatch(c) != 0)
208 goto client_lost;
211 server_update_event(c);
212 return;
214 client_lost:
215 server_client_lost(c);
218 /* Handle client status timer. */
219 void
220 server_client_status_timer(void)
222 struct client *c;
223 struct session *s;
224 struct job *job;
225 struct timeval tv;
226 u_int i;
227 int interval;
228 time_t difference;
230 if (gettimeofday(&tv, NULL) != 0)
231 fatal("gettimeofday failed");
233 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
234 c = ARRAY_ITEM(&clients, i);
235 if (c == NULL || c->session == NULL)
236 continue;
237 if (c->message_string != NULL || c->prompt_string != NULL) {
239 * Don't need timed redraw for messages/prompts so bail
240 * now. The status timer isn't reset when they are
241 * redrawn anyway.
243 continue;
245 s = c->session;
247 if (!options_get_number(&s->options, "status"))
248 continue;
249 interval = options_get_number(&s->options, "status-interval");
251 difference = tv.tv_sec - c->status_timer.tv_sec;
252 if (difference >= interval) {
253 RB_FOREACH(job, jobs, &c->status_jobs)
254 job_run(job);
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);
452 mode = s->mode;
453 if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
454 options_get_number(oo, "mouse-select-pane"))
455 mode |= MODE_MOUSE_STANDARD;
458 * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
459 * user has set mouse-utf8 and any mouse mode is in effect, turn on
460 * UTF-8 mouse input. If the receiving terminal hasn't requested it
461 * (that is, it isn't in s->mode), then it'll be converted in
462 * input_mouse.
464 if ((c->tty.flags & TTY_UTF8) &&
465 (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
466 mode |= MODE_MOUSE_UTF8;
467 else
468 mode &= ~MODE_MOUSE_UTF8;
470 /* Set the terminal mode and reset attributes. */
471 tty_update_mode(&c->tty, mode);
472 tty_reset(&c->tty);
475 /* Repeat time callback. */
476 /* ARGSUSED */
477 void
478 server_client_repeat_timer(unused int fd, unused short events, void *data)
480 struct client *c = data;
482 if (c->flags & CLIENT_REPEAT)
483 c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
486 /* Check if client should be exited. */
487 void
488 server_client_check_exit(struct client *c)
490 struct msg_exit_data exitdata;
492 if (!(c->flags & CLIENT_EXIT))
493 return;
495 if (c->stdout_fd != -1 && c->stdout_event != NULL &&
496 EVBUFFER_LENGTH(c->stdout_event->output) != 0)
497 return;
498 if (c->stderr_fd != -1 && c->stderr_event != NULL &&
499 EVBUFFER_LENGTH(c->stderr_event->output) != 0)
500 return;
502 exitdata.retcode = c->retcode;
503 server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
505 c->flags &= ~CLIENT_EXIT;
509 * Check if the client should backoff. During backoff, data from external
510 * programs is not written to the terminal. When the existing data drains, the
511 * client is redrawn.
513 * There are two backoff phases - both the tty and client have backoff flags -
514 * the first to allow existing data to drain and the latter to ensure backoff
515 * is disabled until the redraw has finished and prevent the redraw triggering
516 * another backoff.
518 void
519 server_client_check_backoff(struct client *c)
521 struct tty *tty = &c->tty;
522 size_t used;
524 used = EVBUFFER_LENGTH(tty->event->output);
527 * If in the second backoff phase (redrawing), don't check backoff
528 * until the redraw has completed (or enough of it to drop below the
529 * backoff threshold).
531 if (c->flags & CLIENT_BACKOFF) {
532 if (used > BACKOFF_THRESHOLD)
533 return;
534 c->flags &= ~CLIENT_BACKOFF;
535 return;
538 /* Once drained, allow data through again and schedule redraw. */
539 if (tty->flags & TTY_BACKOFF) {
540 if (used != 0)
541 return;
542 tty->flags &= ~TTY_BACKOFF;
543 c->flags |= (CLIENT_BACKOFF|CLIENT_REDRAWWINDOW|CLIENT_STATUS);
544 return;
547 /* If too much data, start backoff. */
548 if (used > BACKOFF_THRESHOLD)
549 tty->flags |= TTY_BACKOFF;
552 /* Check for client redraws. */
553 void
554 server_client_check_redraw(struct client *c)
556 struct session *s = c->session;
557 struct window_pane *wp;
558 int flags, redraw;
560 flags = c->tty.flags & TTY_FREEZE;
561 c->tty.flags &= ~TTY_FREEZE;
563 if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
564 if (options_get_number(&s->options, "set-titles"))
565 server_client_set_title(c);
567 if (c->message_string != NULL)
568 redraw = status_message_redraw(c);
569 else if (c->prompt_string != NULL)
570 redraw = status_prompt_redraw(c);
571 else
572 redraw = status_redraw(c);
573 if (!redraw)
574 c->flags &= ~CLIENT_STATUS;
577 if (c->flags & CLIENT_REDRAW) {
578 screen_redraw_screen(c, 0, 0);
579 c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
580 } else if (c->flags & CLIENT_REDRAWWINDOW) {
581 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
582 screen_redraw_pane(c, wp);
583 c->flags &= ~CLIENT_REDRAWWINDOW;
584 } else {
585 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
586 if (wp->flags & PANE_REDRAW)
587 screen_redraw_pane(c, wp);
591 if (c->flags & CLIENT_BORDERS)
592 screen_redraw_screen(c, 0, 1);
594 if (c->flags & CLIENT_STATUS)
595 screen_redraw_screen(c, 1, 0);
597 c->tty.flags |= flags;
599 c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
602 /* Set client title. */
603 void
604 server_client_set_title(struct client *c)
606 struct session *s = c->session;
607 const char *template;
608 char *title;
610 template = options_get_string(&s->options, "set-titles-string");
612 title = status_replace(c, NULL, template, time(NULL), 1);
613 if (c->title == NULL || strcmp(title, c->title) != 0) {
614 if (c->title != NULL)
615 xfree(c->title);
616 c->title = xstrdup(title);
617 tty_set_title(&c->tty, c->title);
619 xfree(title);
623 * Error callback for client stdin. Caller must increase reference count when
624 * enabling event!
626 void
627 server_client_in_callback(
628 unused struct bufferevent *bufev, unused short what, void *data)
630 struct client *c = data;
632 c->references--;
633 if (c->flags & CLIENT_DEAD)
634 return;
636 bufferevent_disable(c->stdin_event, EV_READ|EV_WRITE);
637 setblocking(c->stdin_fd, 1);
638 close(c->stdin_fd);
639 c->stdin_fd = -1;
641 if (c->stdin_callback != NULL)
642 c->stdin_callback(c, c->stdin_data);
645 /* Error callback for client stdout. */
646 void
647 server_client_out_callback(
648 unused struct bufferevent *bufev, unused short what, unused void *data)
650 struct client *c = data;
652 bufferevent_disable(c->stdout_event, EV_READ|EV_WRITE);
653 setblocking(c->stdout_fd, 1);
654 close(c->stdout_fd);
655 c->stdout_fd = -1;
658 /* Error callback for client stderr. */
659 void
660 server_client_err_callback(
661 unused struct bufferevent *bufev, unused short what, unused void *data)
663 struct client *c = data;
665 bufferevent_disable(c->stderr_event, EV_READ|EV_WRITE);
666 setblocking(c->stderr_fd, 1);
667 close(c->stderr_fd);
668 c->stderr_fd = -1;
671 /* Dispatch message from client. */
673 server_client_msg_dispatch(struct client *c)
675 struct imsg imsg;
676 struct msg_command_data commanddata;
677 struct msg_identify_data identifydata;
678 struct msg_environ_data environdata;
679 ssize_t n, datalen;
681 if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
682 return (-1);
684 for (;;) {
685 if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
686 return (-1);
687 if (n == 0)
688 return (0);
689 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
691 if (imsg.hdr.peerid != PROTOCOL_VERSION) {
692 server_write_client(c, MSG_VERSION, NULL, 0);
693 c->flags |= CLIENT_BAD;
694 imsg_free(&imsg);
695 continue;
698 log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
699 switch (imsg.hdr.type) {
700 case MSG_COMMAND:
701 if (datalen != sizeof commanddata)
702 fatalx("bad MSG_COMMAND size");
703 memcpy(&commanddata, imsg.data, sizeof commanddata);
705 server_client_msg_command(c, &commanddata);
706 break;
707 case MSG_IDENTIFY:
708 if (datalen != sizeof identifydata)
709 fatalx("bad MSG_IDENTIFY size");
710 if (imsg.fd == -1)
711 fatalx("MSG_IDENTIFY missing fd");
712 memcpy(&identifydata, imsg.data, sizeof identifydata);
714 c->stdin_fd = imsg.fd;
715 c->stdin_event = bufferevent_new(c->stdin_fd,
716 NULL, NULL, server_client_in_callback, c);
717 if (c->stdin_event == NULL)
718 fatalx("failed to create stdin event");
719 setblocking(c->stdin_fd, 0);
721 server_client_msg_identify(c, &identifydata, imsg.fd);
722 break;
723 case MSG_STDOUT:
724 if (datalen != 0)
725 fatalx("bad MSG_STDOUT size");
726 if (imsg.fd == -1)
727 fatalx("MSG_STDOUT missing fd");
729 c->stdout_fd = imsg.fd;
730 c->stdout_event = bufferevent_new(c->stdout_fd,
731 NULL, NULL, server_client_out_callback, c);
732 if (c->stdout_event == NULL)
733 fatalx("failed to create stdout event");
734 setblocking(c->stdout_fd, 0);
736 break;
737 case MSG_STDERR:
738 if (datalen != 0)
739 fatalx("bad MSG_STDERR size");
740 if (imsg.fd == -1)
741 fatalx("MSG_STDERR missing fd");
743 c->stderr_fd = imsg.fd;
744 c->stderr_event = bufferevent_new(c->stderr_fd,
745 NULL, NULL, server_client_err_callback, c);
746 if (c->stderr_event == NULL)
747 fatalx("failed to create stderr event");
748 setblocking(c->stderr_fd, 0);
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 */