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_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. */
53 server_client_create(int fd
)
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
);
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
;
81 c
->last_session
= NULL
;
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
;
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
);
103 ARRAY_ADD(&clients
, c
);
104 log_debug("new client %d", fd
);
109 server_client_lost(struct client
*c
)
111 struct message_entry
*msg
;
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
)
127 if (c
->stdin_fd
!= -1)
129 if (c
->stdin_event
!= NULL
)
130 bufferevent_free(c
->stdin_event
);
131 if (c
->stdout_fd
!= -1)
133 if (c
->stdout_event
!= NULL
)
134 bufferevent_free(c
->stdout_event
);
135 if (c
->stderr_fd
!= -1)
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
)
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
);
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
);
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
);
177 if (i
== ARRAY_LENGTH(&dead_clients
))
178 ARRAY_ADD(&dead_clients
, c
);
179 c
->flags
|= CLIENT_DEAD
;
182 server_check_unattached();
183 server_update_socket();
186 /* Process a single client event. */
188 server_client_callback(int fd
, short events
, void *data
)
190 struct client
*c
= data
;
192 if (c
->flags
& CLIENT_DEAD
)
195 if (fd
== c
->ibuf
.fd
) {
196 if (events
& EV_WRITE
&& msgbuf_write(&c
->ibuf
.w
) < 0)
199 if (c
->flags
& CLIENT_BAD
) {
200 if (c
->ibuf
.w
.queued
== 0)
205 if (events
& EV_READ
&& server_client_msg_dispatch(c
) != 0)
209 server_update_event(c
);
213 server_client_lost(c
);
216 /* Handle client status timer. */
218 server_client_status_timer(void)
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
)
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
245 if (!options_get_number(&s
->options
, "status"))
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
)
253 c
->flags
|= CLIENT_STATUS
;
258 /* Handle data key input from client. */
260 server_client_handle_key(int key
, struct mouse_event
*mouse
, void *data
)
262 struct client
*c
= data
;
265 struct window_pane
*wp
;
268 struct key_binding
*bd
;
269 struct keylist
*keylist
;
270 int xtimeout
, isprefix
;
273 /* Check the client is good to accept input. */
274 if ((c
->flags
& (CLIENT_DEAD
|CLIENT_SUSPENDED
)) != 0)
276 if (c
->session
== NULL
)
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
;
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
)
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
);
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
);
311 /* Check for mouse keys. */
312 if (key
== KEYC_MOUSE
) {
313 if (c
->flags
& CLIENT_READONLY
)
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
);
320 window_pane_mouse(wp
, c
->session
, mouse
);
324 /* Is this a prefix key? */
325 keylist
= options_get_data(&c
->session
->options
, "prefix");
327 for (i
= 0; i
< ARRAY_LENGTH(keylist
); i
++) {
328 if (key
== ARRAY_ITEM(keylist
, i
)) {
334 /* No previous prefix key. */
335 if (!(c
->flags
& CLIENT_PREFIX
)) {
337 c
->flags
|= CLIENT_PREFIX
;
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
);
344 key_bindings_dispatch(bd
, c
);
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
;
356 c
->flags
|= CLIENT_PREFIX
;
357 else if (!(c
->flags
& CLIENT_READONLY
))
358 window_pane_key(wp
, c
->session
, key
);
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
;
367 c
->flags
|= CLIENT_PREFIX
;
368 else if (!(c
->flags
& CLIENT_READONLY
))
369 window_pane_key(wp
, c
->session
, key
);
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. */
390 server_client_loop(void)
394 struct window_pane
*wp
;
397 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
398 c
= ARRAY_ITEM(&clients
, i
);
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
413 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
414 w
= ARRAY_ITEM(&windows
, i
);
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.
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
;
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);
448 tty_cursor(&c
->tty
, wp
->xoff
+ s
->cx
, wp
->yoff
+ s
->cy
);
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
462 if ((c
->tty
.flags
& TTY_UTF8
) &&
463 (mode
& ALL_MOUSE_MODES
) && options_get_number(oo
, "mouse-utf8"))
464 mode
|= MODE_MOUSE_UTF8
;
466 mode
&= ~MODE_MOUSE_UTF8
;
468 /* Set the terminal mode and reset attributes. */
469 tty_update_mode(&c
->tty
, mode
);
473 /* Repeat time callback. */
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. */
486 server_client_check_exit(struct client
*c
)
488 struct msg_exit_data exitdata
;
490 if (!(c
->flags
& CLIENT_EXIT
))
493 if (c
->stdout_fd
!= -1 && c
->stdout_event
!= NULL
&&
494 EVBUFFER_LENGTH(c
->stdout_event
->output
) != 0)
496 if (c
->stderr_fd
!= -1 && c
->stderr_event
!= NULL
&&
497 EVBUFFER_LENGTH(c
->stderr_event
->output
) != 0)
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
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
517 server_client_check_backoff(struct client
*c
)
519 struct tty
*tty
= &c
->tty
;
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
)
532 c
->flags
&= ~CLIENT_BACKOFF
;
536 /* Once drained, allow data through again and schedule redraw. */
537 if (tty
->flags
& TTY_BACKOFF
) {
540 tty
->flags
&= ~TTY_BACKOFF
;
541 c
->flags
|= (CLIENT_BACKOFF
|CLIENT_REDRAWWINDOW
|CLIENT_STATUS
);
545 /* If too much data, start backoff. */
546 if (used
> BACKOFF_THRESHOLD
)
547 tty
->flags
|= TTY_BACKOFF
;
550 /* Check for client redraws. */
552 server_client_check_redraw(struct client
*c
)
554 struct session
*s
= c
->session
;
555 struct window_pane
*wp
;
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
);
570 redraw
= status_redraw(c
);
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
;
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. */
602 server_client_set_title(struct client
*c
)
604 struct session
*s
= c
->session
;
605 const char *template;
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
)
614 c
->title
= xstrdup(title
);
615 tty_set_title(&c
->tty
, c
->title
);
621 * Error callback for client stdin. Caller must increase reference count when
625 server_client_in_callback(
626 unused
struct bufferevent
*bufev
, unused
short what
, void *data
)
628 struct client
*c
= data
;
631 if (c
->flags
& CLIENT_DEAD
)
634 bufferevent_disable(c
->stdin_event
, EV_READ
|EV_WRITE
);
638 if (c
->stdin_callback
!= NULL
)
639 c
->stdin_callback(c
, c
->stdin_data
);
642 /* Error callback for client stdout. */
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
);
654 /* Error callback for client stderr. */
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
);
666 /* Dispatch message from client. */
668 server_client_msg_dispatch(struct client
*c
)
671 struct msg_command_data commanddata
;
672 struct msg_identify_data identifydata
;
673 struct msg_environ_data environdata
;
677 if ((n
= imsg_read(&c
->ibuf
)) == -1 || n
== 0)
681 if ((n
= imsg_get(&c
->ibuf
, &imsg
)) == -1)
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
;
694 log_debug("got %d from client %d", imsg
.hdr
.type
, c
->ibuf
.fd
);
695 switch (imsg
.hdr
.type
) {
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
);
704 if (datalen
!= sizeof identifydata
)
705 fatalx("bad MSG_IDENTIFY size");
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
);
723 fatalx("bad MSG_STDOUT size");
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
);
738 fatalx("bad MSG_STDERR size");
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
);
753 fatalx("bad MSG_RESIZE size");
755 if (tty_resize(&c
->tty
)) {
757 server_redraw_client(c
);
762 fatalx("bad MSG_EXITING size");
766 server_write_client(c
, MSG_EXITED
, NULL
, 0);
771 fatalx("bad MSG_WAKEUP size");
773 if (!(c
->flags
& CLIENT_SUSPENDED
))
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
);
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
);
797 fatalx("bad MSG_SHELL size");
799 server_client_msg_shell(c
);
802 fatalx("unexpected message");
809 /* Callback to send error message to client. */
811 server_client_msg_error(struct cmd_ctx
*ctx
, const char *fmt
, ...)
816 evbuffer_add_vprintf(ctx
->cmdclient
->stderr_event
->output
, fmt
, ap
);
819 bufferevent_write(ctx
->cmdclient
->stderr_event
, "\n", 1);
820 ctx
->cmdclient
->retcode
= 1;
823 /* Callback to send print message to client. */
825 server_client_msg_print(struct cmd_ctx
*ctx
, const char *fmt
, ...)
830 evbuffer_add_vprintf(ctx
->cmdclient
->stdout_event
->output
, fmt
, ap
);
833 bufferevent_write(ctx
->cmdclient
->stdout_event
, "\n", 1);
836 /* Callback to send print message to client, if not quiet. */
838 server_client_msg_info(struct cmd_ctx
*ctx
, const char *fmt
, ...)
842 if (options_get_number(&global_options
, "quiet"))
846 evbuffer_add_vprintf(ctx
->cmdclient
->stdout_event
->output
, fmt
, ap
);
849 bufferevent_write(ctx
->cmdclient
->stdout_event
, "\n", 1);
852 /* Handle command message. */
854 server_client_msg_command(struct client
*c
, struct msg_command_data
*data
)
857 struct cmd_list
*cmdlist
= NULL
;
861 ctx
.error
= server_client_msg_error
;
862 ctx
.print
= server_client_msg_print
;
863 ctx
.info
= server_client_msg_info
;
866 ctx
.curclient
= NULL
;
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");
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
);
888 cmd_free_argv(argc
, argv
);
890 if (cmd_list_exec(cmdlist
, &ctx
) != 1)
891 c
->flags
|= CLIENT_EXIT
;
892 cmd_list_free(cmdlist
);
897 cmd_list_free(cmdlist
);
898 c
->flags
|= CLIENT_EXIT
;
901 /* Handle identify message. */
903 server_client_msg_identify(
904 struct client
*c
, struct msg_identify_data
*data
, int fd
)
909 data
->cwd
[(sizeof data
->cwd
) - 1] = '\0';
910 if (*data
->cwd
!= '\0')
911 c
->cwd
= xstrdup(data
->cwd
);
915 if ((tty_fd
= dup(fd
)) == -1)
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
;
930 c
->flags
|= CLIENT_TERMINAL
;
933 /* Handle shell message. */
935 server_client_msg_shell(struct client
*c
)
937 struct msg_shell_data data
;
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 */