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>
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. */
55 server_client_create(int fd
)
62 c
= xcalloc(1, sizeof *c
);
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
;
79 c
->last_session
= NULL
;
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
;
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
);
105 ARRAY_ADD(&clients
, c
);
106 log_debug("new client %d", fd
);
111 server_client_lost(struct client
*c
)
113 struct message_entry
*msg
;
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
)
129 if (c
->stdin_event
!= NULL
)
130 bufferevent_free(c
->stdin_event
);
131 if (c
->stdin_fd
!= -1) {
132 setblocking(c
->stdin_fd
, 1);
135 if (c
->stdout_event
!= NULL
)
136 bufferevent_free(c
->stdout_event
);
137 if (c
->stdout_fd
!= -1) {
138 setblocking(c
->stdout_fd
, 1);
141 if (c
->stderr_event
!= NULL
)
142 bufferevent_free(c
->stderr_event
);
143 if (c
->stderr_fd
!= -1) {
144 setblocking(c
->stderr_fd
, 1);
148 status_free_jobs(&c
->status_new
);
149 status_free_jobs(&c
->status_old
);
150 screen_free(&c
->status
);
152 if (c
->title
!= NULL
)
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
);
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
);
176 environ_free(&c
->environ
);
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
);
188 if (i
== ARRAY_LENGTH(&dead_clients
))
189 ARRAY_ADD(&dead_clients
, c
);
190 c
->flags
|= CLIENT_DEAD
;
193 server_check_unattached();
194 server_update_socket();
197 /* Process a single client event. */
199 server_client_callback(int fd
, short events
, void *data
)
201 struct client
*c
= data
;
203 if (c
->flags
& CLIENT_DEAD
)
206 if (fd
== c
->ibuf
.fd
) {
207 if (events
& EV_WRITE
&& msgbuf_write(&c
->ibuf
.w
) < 0)
210 if (c
->flags
& CLIENT_BAD
) {
211 if (c
->ibuf
.w
.queued
== 0)
216 if (events
& EV_READ
&& server_client_msg_dispatch(c
) != 0)
220 server_update_event(c
);
224 server_client_lost(c
);
227 /* Handle client status timer. */
229 server_client_status_timer(void)
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
)
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
255 if (!options_get_number(&s
->options
, "status"))
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. */
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
;
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
);
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
);
296 memcpy(&c
->last_mouse
, mouse
, sizeof c
->last_mouse
);
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)
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. */
330 server_client_handle_key(int key
, struct mouse_event
*mouse
, void *data
)
332 struct client
*c
= data
;
335 struct window_pane
*wp
;
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)
344 if (c
->session
== NULL
)
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
;
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
)
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
);
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
);
379 /* Check for mouse keys. */
380 if (key
== KEYC_MOUSE
) {
381 if (c
->flags
& CLIENT_READONLY
)
383 server_client_check_mouse(c
, wp
, mouse
);
387 /* Is this a prefix key? */
388 if (key
== options_get_number(&c
->session
->options
, "prefix"))
390 else if (key
== options_get_number(&c
->session
->options
, "prefix2"))
395 /* No previous prefix key. */
396 if (!(c
->flags
& CLIENT_PREFIX
)) {
398 c
->flags
|= CLIENT_PREFIX
;
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
);
405 key_bindings_dispatch(bd
, c
);
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
;
417 c
->flags
|= CLIENT_PREFIX
;
418 else if (!(c
->flags
& CLIENT_READONLY
))
419 window_pane_key(wp
, c
->session
, key
);
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
;
428 c
->flags
|= CLIENT_PREFIX
;
429 else if (!(c
->flags
& CLIENT_READONLY
))
430 window_pane_key(wp
, c
->session
, key
);
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. */
451 server_client_loop(void)
455 struct window_pane
*wp
;
458 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
459 c
= ARRAY_ITEM(&clients
, i
);
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
474 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
475 w
= ARRAY_ITEM(&windows
, i
);
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.
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
;
504 if (c
->flags
& CLIENT_SUSPENDED
)
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);
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.
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
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
549 if ((c
->tty
.flags
& TTY_UTF8
) &&
550 (mode
& ALL_MOUSE_MODES
) && options_get_number(oo
, "mouse-utf8"))
551 mode
|= MODE_MOUSE_UTF8
;
553 mode
&= ~MODE_MOUSE_UTF8
;
555 /* Set the terminal mode and reset attributes. */
556 tty_update_mode(&c
->tty
, mode
, s
);
560 /* Repeat time callback. */
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. */
573 server_client_check_exit(struct client
*c
)
575 struct msg_exit_data exitdata
;
577 if (!(c
->flags
& CLIENT_EXIT
))
580 if (c
->stdout_fd
!= -1 && c
->stdout_event
!= NULL
&&
581 EVBUFFER_LENGTH(c
->stdout_event
->output
) != 0)
583 if (c
->stderr_fd
!= -1 && c
->stderr_event
!= NULL
&&
584 EVBUFFER_LENGTH(c
->stderr_event
->output
) != 0)
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. */
595 server_client_check_redraw(struct client
*c
)
597 struct session
*s
= c
->session
;
598 struct window_pane
*wp
;
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
);
613 redraw
= status_redraw(c
);
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
;
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. */
645 server_client_set_title(struct client
*c
)
647 struct session
*s
= c
->session
;
648 const char *template;
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
)
657 c
->title
= xstrdup(title
);
658 tty_set_title(&c
->tty
, c
->title
);
664 * Error callback for client stdin. Caller must increase reference count when
668 server_client_in_callback(
669 unused
struct bufferevent
*bufev
, unused
short what
, void *data
)
671 struct client
*c
= data
;
674 if (c
->flags
& CLIENT_DEAD
)
677 bufferevent_disable(c
->stdin_event
, EV_READ
|EV_WRITE
);
678 setblocking(c
->stdin_fd
, 1);
682 if (c
->stdin_callback
!= NULL
)
683 c
->stdin_callback(c
, c
->stdin_data
);
686 /* Error callback for client stdout. */
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);
699 /* Error callback for client stderr. */
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);
712 /* Dispatch message from client. */
714 server_client_msg_dispatch(struct client
*c
)
717 struct msg_command_data commanddata
;
718 struct msg_identify_data identifydata
;
719 struct msg_environ_data environdata
;
722 if ((n
= imsg_read(&c
->ibuf
)) == -1 || n
== 0)
726 if ((n
= imsg_get(&c
->ibuf
, &imsg
)) == -1)
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
;
739 log_debug("got %d from client %d", imsg
.hdr
.type
, c
->ibuf
.fd
);
740 switch (imsg
.hdr
.type
) {
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
);
749 if (datalen
!= sizeof identifydata
)
750 fatalx("bad MSG_IDENTIFY size");
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
);
766 fatalx("bad MSG_STDOUT size");
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);
780 fatalx("bad MSG_STDERR size");
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);
794 fatalx("bad MSG_RESIZE size");
796 if (tty_resize(&c
->tty
)) {
798 server_redraw_client(c
);
803 fatalx("bad MSG_EXITING size");
807 server_write_client(c
, MSG_EXITED
, NULL
, 0);
812 fatalx("bad MSG_WAKEUP size");
814 if (!(c
->flags
& CLIENT_SUSPENDED
))
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
);
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
);
838 fatalx("bad MSG_SHELL size");
840 server_client_msg_shell(c
);
843 fatalx("unexpected message");
850 /* Callback to send error message to client. */
852 server_client_msg_error(struct cmd_ctx
*ctx
, const char *fmt
, ...)
857 evbuffer_add_vprintf(ctx
->cmdclient
->stderr_event
->output
, fmt
, ap
);
860 bufferevent_write(ctx
->cmdclient
->stderr_event
, "\n", 1);
861 ctx
->cmdclient
->retcode
= 1;
864 /* Callback to send print message to client. */
866 server_client_msg_print(struct cmd_ctx
*ctx
, const char *fmt
, ...)
871 evbuffer_add_vprintf(ctx
->cmdclient
->stdout_event
->output
, fmt
, ap
);
874 bufferevent_write(ctx
->cmdclient
->stdout_event
, "\n", 1);
877 /* Callback to send print message to client, if not quiet. */
879 server_client_msg_info(struct cmd_ctx
*ctx
, const char *fmt
, ...)
883 if (options_get_number(&global_options
, "quiet"))
887 evbuffer_add_vprintf(ctx
->cmdclient
->stdout_event
->output
, fmt
, ap
);
890 bufferevent_write(ctx
->cmdclient
->stdout_event
, "\n", 1);
893 /* Handle command message. */
895 server_client_msg_command(struct client
*c
, struct msg_command_data
*data
)
898 struct cmd_list
*cmdlist
= NULL
;
902 ctx
.error
= server_client_msg_error
;
903 ctx
.print
= server_client_msg_print
;
904 ctx
.info
= server_client_msg_info
;
907 ctx
.curclient
= NULL
;
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");
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
);
929 cmd_free_argv(argc
, argv
);
931 if (cmd_list_exec(cmdlist
, &ctx
) != 1)
932 c
->flags
|= CLIENT_EXIT
;
933 cmd_list_free(cmdlist
);
938 cmd_list_free(cmdlist
);
939 c
->flags
|= CLIENT_EXIT
;
942 /* Handle identify message. */
944 server_client_msg_identify(
945 struct client
*c
, struct msg_identify_data
*data
, int fd
)
950 data
->cwd
[(sizeof data
->cwd
) - 1] = '\0';
951 if (*data
->cwd
!= '\0')
952 c
->cwd
= xstrdup(data
->cwd
);
956 if ((tty_fd
= dup(fd
)) == -1)
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
;
971 c
->flags
|= CLIENT_TERMINAL
;
974 /* Handle shell message. */
976 server_client_msg_shell(struct client
*c
)
978 struct msg_shell_data data
;
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 */