Check for the right return value from sscanf.
[tmux-openbsd.git] / server-client.c
bloba748d363ab241796ab41fed8f7fbb333d650c597
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_check_mouse(struct client *c,
31 struct window_pane *wp, struct mouse_event *mouse);
32 void server_client_handle_key(int, struct mouse_event *, void *);
33 void server_client_repeat_timer(int, short, void *);
34 void server_client_check_exit(struct client *);
35 void server_client_check_backoff(struct client *);
36 void server_client_check_redraw(struct client *);
37 void server_client_set_title(struct client *);
38 void server_client_reset_state(struct client *);
39 void server_client_in_callback(struct bufferevent *, short, void *);
40 void server_client_out_callback(struct bufferevent *, short, void *);
41 void server_client_err_callback(struct bufferevent *, short, void *);
43 int server_client_msg_dispatch(struct client *);
44 void server_client_msg_command(struct client *, struct msg_command_data *);
45 void server_client_msg_identify(
46 struct client *, struct msg_identify_data *, int);
47 void server_client_msg_shell(struct client *);
49 void printflike2 server_client_msg_error(struct cmd_ctx *, const char *, ...);
50 void printflike2 server_client_msg_print(struct cmd_ctx *, const char *, ...);
51 void printflike2 server_client_msg_info(struct cmd_ctx *, const char *, ...);
53 /* Create a new client. */
54 void
55 server_client_create(int fd)
57 struct client *c;
58 u_int i;
60 setblocking(fd, 0);
62 c = xcalloc(1, sizeof *c);
63 c->references = 0;
64 imsg_init(&c->ibuf, fd);
65 server_update_event(c);
67 if (gettimeofday(&c->creation_time, NULL) != 0)
68 fatal("gettimeofday failed");
69 memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
71 c->stdin_event = NULL;
72 c->stdout_event = NULL;
73 c->stderr_event = NULL;
75 c->tty.fd = -1;
76 c->title = NULL;
78 c->session = NULL;
79 c->last_session = NULL;
80 c->tty.sx = 80;
81 c->tty.sy = 24;
83 screen_init(&c->status, c->tty.sx, 1, 0);
84 RB_INIT(&c->status_new);
85 RB_INIT(&c->status_old);
87 c->message_string = NULL;
88 ARRAY_INIT(&c->message_log);
90 c->prompt_string = NULL;
91 c->prompt_buffer = NULL;
92 c->prompt_index = 0;
94 c->last_mouse.b = MOUSE_UP;
95 c->last_mouse.x = c->last_mouse.y = -1;
97 evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
99 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
100 if (ARRAY_ITEM(&clients, i) == NULL) {
101 ARRAY_SET(&clients, i, c);
102 return;
105 ARRAY_ADD(&clients, c);
106 log_debug("new client %d", fd);
109 /* Lost a client. */
110 void
111 server_client_lost(struct client *c)
113 struct message_entry *msg;
114 u_int i;
116 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
117 if (ARRAY_ITEM(&clients, i) == c)
118 ARRAY_SET(&clients, i, NULL);
120 log_debug("lost client %d", c->ibuf.fd);
123 * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
124 * and tty_free might close an unrelated fd.
126 if (c->flags & CLIENT_TERMINAL)
127 tty_free(&c->tty);
129 if (c->stdin_event != NULL)
130 bufferevent_free(c->stdin_event);
131 if (c->stdin_fd != -1) {
132 setblocking(c->stdin_fd, 1);
133 close(c->stdin_fd);
135 if (c->stdout_event != NULL)
136 bufferevent_free(c->stdout_event);
137 if (c->stdout_fd != -1) {
138 setblocking(c->stdout_fd, 1);
139 close(c->stdout_fd);
141 if (c->stderr_event != NULL)
142 bufferevent_free(c->stderr_event);
143 if (c->stderr_fd != -1) {
144 setblocking(c->stderr_fd, 1);
145 close(c->stderr_fd);
148 status_free_jobs(&c->status_new);
149 status_free_jobs(&c->status_old);
150 screen_free(&c->status);
152 if (c->title != NULL)
153 xfree(c->title);
155 evtimer_del(&c->repeat_timer);
157 evtimer_del(&c->identify_timer);
159 if (c->message_string != NULL)
160 xfree(c->message_string);
161 evtimer_del(&c->message_timer);
162 for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
163 msg = &ARRAY_ITEM(&c->message_log, i);
164 xfree(msg->msg);
166 ARRAY_FREE(&c->message_log);
168 if (c->prompt_string != NULL)
169 xfree(c->prompt_string);
170 if (c->prompt_buffer != NULL)
171 xfree(c->prompt_buffer);
173 if (c->cwd != NULL)
174 xfree(c->cwd);
176 environ_free(&c->environ);
178 close(c->ibuf.fd);
179 imsg_clear(&c->ibuf);
180 event_del(&c->event);
182 for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
183 if (ARRAY_ITEM(&dead_clients, i) == NULL) {
184 ARRAY_SET(&dead_clients, i, c);
185 break;
188 if (i == ARRAY_LENGTH(&dead_clients))
189 ARRAY_ADD(&dead_clients, c);
190 c->flags |= CLIENT_DEAD;
192 recalculate_sizes();
193 server_check_unattached();
194 server_update_socket();
197 /* Process a single client event. */
198 void
199 server_client_callback(int fd, short events, void *data)
201 struct client *c = data;
203 if (c->flags & CLIENT_DEAD)
204 return;
206 if (fd == c->ibuf.fd) {
207 if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
208 goto client_lost;
210 if (c->flags & CLIENT_BAD) {
211 if (c->ibuf.w.queued == 0)
212 goto client_lost;
213 return;
216 if (events & EV_READ && server_client_msg_dispatch(c) != 0)
217 goto client_lost;
220 server_update_event(c);
221 return;
223 client_lost:
224 server_client_lost(c);
227 /* Handle client status timer. */
228 void
229 server_client_status_timer(void)
231 struct client *c;
232 struct session *s;
233 struct timeval tv;
234 u_int i;
235 int interval;
236 time_t difference;
238 if (gettimeofday(&tv, NULL) != 0)
239 fatal("gettimeofday failed");
241 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
242 c = ARRAY_ITEM(&clients, i);
243 if (c == NULL || c->session == NULL)
244 continue;
245 if (c->message_string != NULL || c->prompt_string != NULL) {
247 * Don't need timed redraw for messages/prompts so bail
248 * now. The status timer isn't reset when they are
249 * redrawn anyway.
251 continue;
253 s = c->session;
255 if (!options_get_number(&s->options, "status"))
256 continue;
257 interval = options_get_number(&s->options, "status-interval");
259 difference = tv.tv_sec - c->status_timer.tv_sec;
260 if (difference >= interval) {
261 status_update_jobs(c);
262 c->flags |= CLIENT_STATUS;
267 /* Check for mouse keys. */
268 void
269 server_client_check_mouse(
270 struct client *c, struct window_pane *wp, struct mouse_event *mouse)
272 struct session *s = c->session;
273 struct options *oo = &s->options;
274 int statusat;
276 statusat = status_at_line(c);
278 /* Is this a window selection click on the status line? */
279 if (statusat != -1 && mouse->y == (u_int)statusat &&
280 options_get_number(oo, "mouse-select-window")) {
281 if (mouse->b == MOUSE_UP && c->last_mouse.b != MOUSE_UP) {
282 status_set_window_at(c, mouse->x);
283 return;
285 if (mouse->b & MOUSE_45) {
286 if ((mouse->b & MOUSE_BUTTON) == MOUSE_1) {
287 session_previous(c->session, 0);
288 server_redraw_session(s);
290 if ((mouse->b & MOUSE_BUTTON) == MOUSE_2) {
291 session_next(c->session, 0);
292 server_redraw_session(s);
294 return;
296 memcpy(&c->last_mouse, mouse, sizeof c->last_mouse);
297 return;
301 * Not on status line - adjust mouse position if status line is at the
302 * top and limit if at the bottom. From here on a struct mouse
303 * represents the offset onto the window itself.
305 if (statusat == 0 &&mouse->y > 0)
306 mouse->y--;
307 else if (statusat > 0 && mouse->y >= (u_int)statusat)
308 mouse->y = statusat - 1;
310 /* Is this a pane selection? Allow down only in copy mode. */
311 if (options_get_number(oo, "mouse-select-pane") &&
312 ((!(mouse->b & MOUSE_DRAG) && mouse->b != MOUSE_UP) ||
313 wp->mode != &window_copy_mode)) {
314 window_set_active_at(wp->window, mouse->x, mouse->y);
315 server_redraw_window_borders(wp->window);
316 wp = wp->window->active; /* may have changed */
319 /* Check if trying to resize pane. */
320 if (options_get_number(oo, "mouse-resize-pane"))
321 layout_resize_pane_mouse(c, mouse);
323 /* Update last and pass through to client. */
324 memcpy(&c->last_mouse, mouse, sizeof c->last_mouse);
325 window_pane_mouse(wp, c->session, mouse);
328 /* Handle data key input from client. */
329 void
330 server_client_handle_key(int key, struct mouse_event *mouse, void *data)
332 struct client *c = data;
333 struct session *s;
334 struct window *w;
335 struct window_pane *wp;
336 struct options *oo;
337 struct timeval tv;
338 struct key_binding *bd;
339 int xtimeout, isprefix;
341 /* Check the client is good to accept input. */
342 if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
343 return;
344 if (c->session == NULL)
345 return;
346 s = c->session;
348 /* Update the activity timer. */
349 if (gettimeofday(&c->activity_time, NULL) != 0)
350 fatal("gettimeofday failed");
351 memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
353 w = c->session->curw->window;
354 wp = w->active;
355 oo = &c->session->options;
357 /* Special case: number keys jump to pane in identify mode. */
358 if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
359 if (c->flags & CLIENT_READONLY)
360 return;
361 wp = window_pane_at_index(w, key - '0');
362 if (wp != NULL && window_pane_visible(wp))
363 window_set_active_pane(w, wp);
364 server_clear_identify(c);
365 return;
368 /* Handle status line. */
369 if (!(c->flags & CLIENT_READONLY)) {
370 status_message_clear(c);
371 server_clear_identify(c);
373 if (c->prompt_string != NULL) {
374 if (!(c->flags & CLIENT_READONLY))
375 status_prompt_key(c, key);
376 return;
379 /* Check for mouse keys. */
380 if (key == KEYC_MOUSE) {
381 if (c->flags & CLIENT_READONLY)
382 return;
383 server_client_check_mouse(c, wp, mouse);
384 return;
387 /* Is this a prefix key? */
388 if (key == options_get_number(&c->session->options, "prefix"))
389 isprefix = 1;
390 else if (key == options_get_number(&c->session->options, "prefix2"))
391 isprefix = 1;
392 else
393 isprefix = 0;
395 /* No previous prefix key. */
396 if (!(c->flags & CLIENT_PREFIX)) {
397 if (isprefix)
398 c->flags |= CLIENT_PREFIX;
399 else {
400 /* Try as a non-prefix key binding. */
401 if ((bd = key_bindings_lookup(key)) == NULL) {
402 if (!(c->flags & CLIENT_READONLY))
403 window_pane_key(wp, c->session, key);
404 } else
405 key_bindings_dispatch(bd, c);
407 return;
410 /* Prefix key already pressed. Reset prefix and lookup key. */
411 c->flags &= ~CLIENT_PREFIX;
412 if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
413 /* If repeating, treat this as a key, else ignore. */
414 if (c->flags & CLIENT_REPEAT) {
415 c->flags &= ~CLIENT_REPEAT;
416 if (isprefix)
417 c->flags |= CLIENT_PREFIX;
418 else if (!(c->flags & CLIENT_READONLY))
419 window_pane_key(wp, c->session, key);
421 return;
424 /* If already repeating, but this key can't repeat, skip it. */
425 if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
426 c->flags &= ~CLIENT_REPEAT;
427 if (isprefix)
428 c->flags |= CLIENT_PREFIX;
429 else if (!(c->flags & CLIENT_READONLY))
430 window_pane_key(wp, c->session, key);
431 return;
434 /* If this key can repeat, reset the repeat flags and timer. */
435 xtimeout = options_get_number(&c->session->options, "repeat-time");
436 if (xtimeout != 0 && bd->can_repeat) {
437 c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
439 tv.tv_sec = xtimeout / 1000;
440 tv.tv_usec = (xtimeout % 1000) * 1000L;
441 evtimer_del(&c->repeat_timer);
442 evtimer_add(&c->repeat_timer, &tv);
445 /* Dispatch the command. */
446 key_bindings_dispatch(bd, c);
449 /* Client functions that need to happen every loop. */
450 void
451 server_client_loop(void)
453 struct client *c;
454 struct window *w;
455 struct window_pane *wp;
456 u_int i;
458 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
459 c = ARRAY_ITEM(&clients, i);
460 if (c == NULL)
461 continue;
463 server_client_check_exit(c);
464 if (c->session != NULL) {
465 server_client_check_redraw(c);
466 server_client_reset_state(c);
471 * Any windows will have been redrawn as part of clients, so clear
472 * their flags now.
474 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
475 w = ARRAY_ITEM(&windows, i);
476 if (w == NULL)
477 continue;
479 w->flags &= ~WINDOW_REDRAW;
480 TAILQ_FOREACH(wp, &w->panes, entry)
481 wp->flags &= ~PANE_REDRAW;
486 * Update cursor position and mode settings. The scroll region and attributes
487 * are cleared when idle (waiting for an event) as this is the most likely time
488 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
489 * compromise between excessive resets and likelihood of an interrupt.
491 * tty_region/tty_reset/tty_update_mode already take care of not resetting
492 * things that are already in their default state.
494 void
495 server_client_reset_state(struct client *c)
497 struct window *w = c->session->curw->window;
498 struct window_pane *wp = w->active;
499 struct screen *s = wp->screen;
500 struct options *oo = &c->session->options;
501 struct options *wo = &w->options;
502 int status, mode, o;
504 if (c->flags & CLIENT_SUSPENDED)
505 return;
507 tty_region(&c->tty, 0, c->tty.sy - 1);
509 status = options_get_number(oo, "status");
510 if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
511 tty_cursor(&c->tty, 0, 0);
512 else {
513 o = status && options_get_number (oo, "status-position") == 0;
514 tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
518 * Resizing panes with the mouse requires at least button mode to give
519 * a smooth appearance.
521 mode = s->mode;
522 if ((c->last_mouse.b & MOUSE_RESIZE_PANE) &&
523 !(mode & (MODE_MOUSE_BUTTON|MODE_MOUSE_ANY)))
524 mode |= MODE_MOUSE_BUTTON;
527 * Any mode will do for mouse-select-pane, but set standard mode if
528 * none.
530 if ((mode & ALL_MOUSE_MODES) == 0) {
531 if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
532 options_get_number(oo, "mouse-select-pane"))
533 mode |= MODE_MOUSE_STANDARD;
534 else if (options_get_number(oo, "mouse-resize-pane"))
535 mode |= MODE_MOUSE_STANDARD;
536 else if (options_get_number(oo, "mouse-select-window"))
537 mode |= MODE_MOUSE_STANDARD;
538 else if (options_get_number(wo, "mode-mouse"))
539 mode |= MODE_MOUSE_STANDARD;
543 * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
544 * user has set mouse-utf8 and any mouse mode is in effect, turn on
545 * UTF-8 mouse input. If the receiving terminal hasn't requested it
546 * (that is, it isn't in s->mode), then it'll be converted in
547 * input_mouse.
549 if ((c->tty.flags & TTY_UTF8) &&
550 (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
551 mode |= MODE_MOUSE_UTF8;
552 else
553 mode &= ~MODE_MOUSE_UTF8;
555 /* Set the terminal mode and reset attributes. */
556 tty_update_mode(&c->tty, mode, s);
557 tty_reset(&c->tty);
560 /* Repeat time callback. */
561 /* ARGSUSED */
562 void
563 server_client_repeat_timer(unused int fd, unused short events, void *data)
565 struct client *c = data;
567 if (c->flags & CLIENT_REPEAT)
568 c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
571 /* Check if client should be exited. */
572 void
573 server_client_check_exit(struct client *c)
575 struct msg_exit_data exitdata;
577 if (!(c->flags & CLIENT_EXIT))
578 return;
580 if (c->stdout_fd != -1 && c->stdout_event != NULL &&
581 EVBUFFER_LENGTH(c->stdout_event->output) != 0)
582 return;
583 if (c->stderr_fd != -1 && c->stderr_event != NULL &&
584 EVBUFFER_LENGTH(c->stderr_event->output) != 0)
585 return;
587 exitdata.retcode = c->retcode;
588 server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
590 c->flags &= ~CLIENT_EXIT;
593 /* Check for client redraws. */
594 void
595 server_client_check_redraw(struct client *c)
597 struct session *s = c->session;
598 struct window_pane *wp;
599 int flags, redraw;
601 flags = c->tty.flags & TTY_FREEZE;
602 c->tty.flags &= ~TTY_FREEZE;
604 if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
605 if (options_get_number(&s->options, "set-titles"))
606 server_client_set_title(c);
608 if (c->message_string != NULL)
609 redraw = status_message_redraw(c);
610 else if (c->prompt_string != NULL)
611 redraw = status_prompt_redraw(c);
612 else
613 redraw = status_redraw(c);
614 if (!redraw)
615 c->flags &= ~CLIENT_STATUS;
618 if (c->flags & CLIENT_REDRAW) {
619 screen_redraw_screen(c, 0, 0);
620 c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
621 } else if (c->flags & CLIENT_REDRAWWINDOW) {
622 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
623 screen_redraw_pane(c, wp);
624 c->flags &= ~CLIENT_REDRAWWINDOW;
625 } else {
626 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
627 if (wp->flags & PANE_REDRAW)
628 screen_redraw_pane(c, wp);
632 if (c->flags & CLIENT_BORDERS)
633 screen_redraw_screen(c, 0, 1);
635 if (c->flags & CLIENT_STATUS)
636 screen_redraw_screen(c, 1, 0);
638 c->tty.flags |= flags;
640 c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
643 /* Set client title. */
644 void
645 server_client_set_title(struct client *c)
647 struct session *s = c->session;
648 const char *template;
649 char *title;
651 template = options_get_string(&s->options, "set-titles-string");
653 title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
654 if (c->title == NULL || strcmp(title, c->title) != 0) {
655 if (c->title != NULL)
656 xfree(c->title);
657 c->title = xstrdup(title);
658 tty_set_title(&c->tty, c->title);
660 xfree(title);
664 * Error callback for client stdin. Caller must increase reference count when
665 * enabling event!
667 void
668 server_client_in_callback(
669 unused struct bufferevent *bufev, unused short what, void *data)
671 struct client *c = data;
673 c->references--;
674 if (c->flags & CLIENT_DEAD)
675 return;
677 bufferevent_disable(c->stdin_event, EV_READ|EV_WRITE);
678 setblocking(c->stdin_fd, 1);
679 close(c->stdin_fd);
680 c->stdin_fd = -1;
682 if (c->stdin_callback != NULL)
683 c->stdin_callback(c, c->stdin_data);
686 /* Error callback for client stdout. */
687 void
688 server_client_out_callback(
689 unused struct bufferevent *bufev, unused short what, unused void *data)
691 struct client *c = data;
693 bufferevent_disable(c->stdout_event, EV_READ|EV_WRITE);
694 setblocking(c->stdout_fd, 1);
695 close(c->stdout_fd);
696 c->stdout_fd = -1;
699 /* Error callback for client stderr. */
700 void
701 server_client_err_callback(
702 unused struct bufferevent *bufev, unused short what, unused void *data)
704 struct client *c = data;
706 bufferevent_disable(c->stderr_event, EV_READ|EV_WRITE);
707 setblocking(c->stderr_fd, 1);
708 close(c->stderr_fd);
709 c->stderr_fd = -1;
712 /* Dispatch message from client. */
714 server_client_msg_dispatch(struct client *c)
716 struct imsg imsg;
717 struct msg_command_data commanddata;
718 struct msg_identify_data identifydata;
719 struct msg_environ_data environdata;
720 ssize_t n, datalen;
722 if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
723 return (-1);
725 for (;;) {
726 if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
727 return (-1);
728 if (n == 0)
729 return (0);
730 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
732 if (imsg.hdr.peerid != PROTOCOL_VERSION) {
733 server_write_client(c, MSG_VERSION, NULL, 0);
734 c->flags |= CLIENT_BAD;
735 imsg_free(&imsg);
736 continue;
739 log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
740 switch (imsg.hdr.type) {
741 case MSG_COMMAND:
742 if (datalen != sizeof commanddata)
743 fatalx("bad MSG_COMMAND size");
744 memcpy(&commanddata, imsg.data, sizeof commanddata);
746 server_client_msg_command(c, &commanddata);
747 break;
748 case MSG_IDENTIFY:
749 if (datalen != sizeof identifydata)
750 fatalx("bad MSG_IDENTIFY size");
751 if (imsg.fd == -1)
752 fatalx("MSG_IDENTIFY missing fd");
753 memcpy(&identifydata, imsg.data, sizeof identifydata);
755 c->stdin_fd = imsg.fd;
756 c->stdin_event = bufferevent_new(c->stdin_fd,
757 NULL, NULL, server_client_in_callback, c);
758 if (c->stdin_event == NULL)
759 fatalx("failed to create stdin event");
760 setblocking(c->stdin_fd, 0);
762 server_client_msg_identify(c, &identifydata, imsg.fd);
763 break;
764 case MSG_STDOUT:
765 if (datalen != 0)
766 fatalx("bad MSG_STDOUT size");
767 if (imsg.fd == -1)
768 fatalx("MSG_STDOUT missing fd");
770 c->stdout_fd = imsg.fd;
771 c->stdout_event = bufferevent_new(c->stdout_fd,
772 NULL, NULL, server_client_out_callback, c);
773 if (c->stdout_event == NULL)
774 fatalx("failed to create stdout event");
775 setblocking(c->stdout_fd, 0);
777 break;
778 case MSG_STDERR:
779 if (datalen != 0)
780 fatalx("bad MSG_STDERR size");
781 if (imsg.fd == -1)
782 fatalx("MSG_STDERR missing fd");
784 c->stderr_fd = imsg.fd;
785 c->stderr_event = bufferevent_new(c->stderr_fd,
786 NULL, NULL, server_client_err_callback, c);
787 if (c->stderr_event == NULL)
788 fatalx("failed to create stderr event");
789 setblocking(c->stderr_fd, 0);
791 break;
792 case MSG_RESIZE:
793 if (datalen != 0)
794 fatalx("bad MSG_RESIZE size");
796 if (tty_resize(&c->tty)) {
797 recalculate_sizes();
798 server_redraw_client(c);
800 break;
801 case MSG_EXITING:
802 if (datalen != 0)
803 fatalx("bad MSG_EXITING size");
805 c->session = NULL;
806 tty_close(&c->tty);
807 server_write_client(c, MSG_EXITED, NULL, 0);
808 break;
809 case MSG_WAKEUP:
810 case MSG_UNLOCK:
811 if (datalen != 0)
812 fatalx("bad MSG_WAKEUP size");
814 if (!(c->flags & CLIENT_SUSPENDED))
815 break;
816 c->flags &= ~CLIENT_SUSPENDED;
818 if (gettimeofday(&c->activity_time, NULL) != 0)
819 fatal("gettimeofday");
820 if (c->session != NULL)
821 session_update_activity(c->session);
823 tty_start_tty(&c->tty);
824 server_redraw_client(c);
825 recalculate_sizes();
826 break;
827 case MSG_ENVIRON:
828 if (datalen != sizeof environdata)
829 fatalx("bad MSG_ENVIRON size");
830 memcpy(&environdata, imsg.data, sizeof environdata);
832 environdata.var[(sizeof environdata.var) - 1] = '\0';
833 if (strchr(environdata.var, '=') != NULL)
834 environ_put(&c->environ, environdata.var);
835 break;
836 case MSG_SHELL:
837 if (datalen != 0)
838 fatalx("bad MSG_SHELL size");
840 server_client_msg_shell(c);
841 break;
842 default:
843 fatalx("unexpected message");
846 imsg_free(&imsg);
850 /* Callback to send error message to client. */
851 void printflike2
852 server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
854 va_list ap;
856 va_start(ap, fmt);
857 evbuffer_add_vprintf(ctx->cmdclient->stderr_event->output, fmt, ap);
858 va_end(ap);
860 bufferevent_write(ctx->cmdclient->stderr_event, "\n", 1);
861 ctx->cmdclient->retcode = 1;
864 /* Callback to send print message to client. */
865 void printflike2
866 server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
868 va_list ap;
870 va_start(ap, fmt);
871 evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
872 va_end(ap);
874 bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
877 /* Callback to send print message to client, if not quiet. */
878 void printflike2
879 server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
881 va_list ap;
883 if (options_get_number(&global_options, "quiet"))
884 return;
886 va_start(ap, fmt);
887 evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
888 va_end(ap);
890 bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
893 /* Handle command message. */
894 void
895 server_client_msg_command(struct client *c, struct msg_command_data *data)
897 struct cmd_ctx ctx;
898 struct cmd_list *cmdlist = NULL;
899 int argc;
900 char **argv, *cause;
902 ctx.error = server_client_msg_error;
903 ctx.print = server_client_msg_print;
904 ctx.info = server_client_msg_info;
906 ctx.msgdata = data;
907 ctx.curclient = NULL;
909 ctx.cmdclient = c;
911 argc = data->argc;
912 data->argv[(sizeof data->argv) - 1] = '\0';
913 if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
914 server_client_msg_error(&ctx, "command too long");
915 goto error;
918 if (argc == 0) {
919 argc = 1;
920 argv = xcalloc(1, sizeof *argv);
921 *argv = xstrdup("new-session");
924 if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
925 server_client_msg_error(&ctx, "%s", cause);
926 cmd_free_argv(argc, argv);
927 goto error;
929 cmd_free_argv(argc, argv);
931 if (cmd_list_exec(cmdlist, &ctx) != 1)
932 c->flags |= CLIENT_EXIT;
933 cmd_list_free(cmdlist);
934 return;
936 error:
937 if (cmdlist != NULL)
938 cmd_list_free(cmdlist);
939 c->flags |= CLIENT_EXIT;
942 /* Handle identify message. */
943 void
944 server_client_msg_identify(
945 struct client *c, struct msg_identify_data *data, int fd)
947 int tty_fd;
949 c->cwd = NULL;
950 data->cwd[(sizeof data->cwd) - 1] = '\0';
951 if (*data->cwd != '\0')
952 c->cwd = xstrdup(data->cwd);
954 if (!isatty(fd))
955 return;
956 if ((tty_fd = dup(fd)) == -1)
957 fatal("dup failed");
958 data->term[(sizeof data->term) - 1] = '\0';
959 tty_init(&c->tty, tty_fd, data->term);
960 if (data->flags & IDENTIFY_UTF8)
961 c->tty.flags |= TTY_UTF8;
962 if (data->flags & IDENTIFY_256COLOURS)
963 c->tty.term_flags |= TERM_256COLOURS;
964 else if (data->flags & IDENTIFY_88COLOURS)
965 c->tty.term_flags |= TERM_88COLOURS;
966 c->tty.key_callback = server_client_handle_key;
967 c->tty.key_data = c;
969 tty_resize(&c->tty);
971 c->flags |= CLIENT_TERMINAL;
974 /* Handle shell message. */
975 void
976 server_client_msg_shell(struct client *c)
978 struct msg_shell_data data;
979 const char *shell;
981 shell = options_get_string(&global_s_options, "default-shell");
983 if (*shell == '\0' || areshell(shell))
984 shell = _PATH_BSHELL;
985 if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
986 strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
988 server_write_client(c, MSG_SHELL, &data, sizeof data);
989 c->flags |= CLIENT_BAD; /* it will die after exec */