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"))
454 tty_update_mode(&c
->tty
, mode
);
458 /* Repeat time callback. */
461 server_client_repeat_timer(unused
int fd
, unused
short events
, void *data
)
463 struct client
*c
= data
;
465 if (c
->flags
& CLIENT_REPEAT
)
466 c
->flags
&= ~(CLIENT_PREFIX
|CLIENT_REPEAT
);
469 /* Check if client should be exited. */
471 server_client_check_exit(struct client
*c
)
473 struct msg_exit_data exitdata
;
475 if (!(c
->flags
& CLIENT_EXIT
))
478 if (c
->stdout_fd
!= -1 && c
->stdout_event
!= NULL
&&
479 EVBUFFER_LENGTH(c
->stdout_event
->output
) != 0)
481 if (c
->stderr_fd
!= -1 && c
->stderr_event
!= NULL
&&
482 EVBUFFER_LENGTH(c
->stderr_event
->output
) != 0)
485 exitdata
.retcode
= c
->retcode
;
486 server_write_client(c
, MSG_EXIT
, &exitdata
, sizeof exitdata
);
488 c
->flags
&= ~CLIENT_EXIT
;
492 * Check if the client should backoff. During backoff, data from external
493 * programs is not written to the terminal. When the existing data drains, the
496 * There are two backoff phases - both the tty and client have backoff flags -
497 * the first to allow existing data to drain and the latter to ensure backoff
498 * is disabled until the redraw has finished and prevent the redraw triggering
502 server_client_check_backoff(struct client
*c
)
504 struct tty
*tty
= &c
->tty
;
507 used
= EVBUFFER_LENGTH(tty
->event
->output
);
510 * If in the second backoff phase (redrawing), don't check backoff
511 * until the redraw has completed (or enough of it to drop below the
512 * backoff threshold).
514 if (c
->flags
& CLIENT_BACKOFF
) {
515 if (used
> BACKOFF_THRESHOLD
)
517 c
->flags
&= ~CLIENT_BACKOFF
;
521 /* Once drained, allow data through again and schedule redraw. */
522 if (tty
->flags
& TTY_BACKOFF
) {
525 tty
->flags
&= ~TTY_BACKOFF
;
526 c
->flags
|= (CLIENT_BACKOFF
|CLIENT_REDRAWWINDOW
|CLIENT_STATUS
);
530 /* If too much data, start backoff. */
531 if (used
> BACKOFF_THRESHOLD
)
532 tty
->flags
|= TTY_BACKOFF
;
535 /* Check for client redraws. */
537 server_client_check_redraw(struct client
*c
)
539 struct session
*s
= c
->session
;
540 struct window_pane
*wp
;
543 flags
= c
->tty
.flags
& TTY_FREEZE
;
544 c
->tty
.flags
&= ~TTY_FREEZE
;
546 if (c
->flags
& (CLIENT_REDRAW
|CLIENT_STATUS
)) {
547 if (options_get_number(&s
->options
, "set-titles"))
548 server_client_set_title(c
);
550 if (c
->message_string
!= NULL
)
551 redraw
= status_message_redraw(c
);
552 else if (c
->prompt_string
!= NULL
)
553 redraw
= status_prompt_redraw(c
);
555 redraw
= status_redraw(c
);
557 c
->flags
&= ~CLIENT_STATUS
;
560 if (c
->flags
& CLIENT_REDRAW
) {
561 screen_redraw_screen(c
, 0, 0);
562 c
->flags
&= ~(CLIENT_STATUS
|CLIENT_BORDERS
);
563 } else if (c
->flags
& CLIENT_REDRAWWINDOW
) {
564 TAILQ_FOREACH(wp
, &c
->session
->curw
->window
->panes
, entry
)
565 screen_redraw_pane(c
, wp
);
566 c
->flags
&= ~CLIENT_REDRAWWINDOW
;
568 TAILQ_FOREACH(wp
, &c
->session
->curw
->window
->panes
, entry
) {
569 if (wp
->flags
& PANE_REDRAW
)
570 screen_redraw_pane(c
, wp
);
574 if (c
->flags
& CLIENT_BORDERS
)
575 screen_redraw_screen(c
, 0, 1);
577 if (c
->flags
& CLIENT_STATUS
)
578 screen_redraw_screen(c
, 1, 0);
580 c
->tty
.flags
|= flags
;
582 c
->flags
&= ~(CLIENT_REDRAW
|CLIENT_STATUS
|CLIENT_BORDERS
);
585 /* Set client title. */
587 server_client_set_title(struct client
*c
)
589 struct session
*s
= c
->session
;
590 const char *template;
593 template = options_get_string(&s
->options
, "set-titles-string");
595 title
= status_replace(c
, NULL
, template, time(NULL
), 1);
596 if (c
->title
== NULL
|| strcmp(title
, c
->title
) != 0) {
597 if (c
->title
!= NULL
)
599 c
->title
= xstrdup(title
);
600 tty_set_title(&c
->tty
, c
->title
);
606 * Error callback for client stdin. Caller must increase reference count when
610 server_client_in_callback(
611 unused
struct bufferevent
*bufev
, unused
short what
, void *data
)
613 struct client
*c
= data
;
616 if (c
->flags
& CLIENT_DEAD
)
619 bufferevent_disable(c
->stdin_event
, EV_READ
|EV_WRITE
);
623 if (c
->stdin_callback
!= NULL
)
624 c
->stdin_callback(c
, c
->stdin_data
);
627 /* Error callback for client stdout. */
629 server_client_out_callback(
630 unused
struct bufferevent
*bufev
, unused
short what
, unused
void *data
)
632 struct client
*c
= data
;
634 bufferevent_disable(c
->stdout_event
, EV_READ
|EV_WRITE
);
639 /* Error callback for client stderr. */
641 server_client_err_callback(
642 unused
struct bufferevent
*bufev
, unused
short what
, unused
void *data
)
644 struct client
*c
= data
;
646 bufferevent_disable(c
->stderr_event
, EV_READ
|EV_WRITE
);
651 /* Dispatch message from client. */
653 server_client_msg_dispatch(struct client
*c
)
656 struct msg_command_data commanddata
;
657 struct msg_identify_data identifydata
;
658 struct msg_environ_data environdata
;
662 if ((n
= imsg_read(&c
->ibuf
)) == -1 || n
== 0)
666 if ((n
= imsg_get(&c
->ibuf
, &imsg
)) == -1)
670 datalen
= imsg
.hdr
.len
- IMSG_HEADER_SIZE
;
672 if (imsg
.hdr
.peerid
!= PROTOCOL_VERSION
) {
673 server_write_client(c
, MSG_VERSION
, NULL
, 0);
674 c
->flags
|= CLIENT_BAD
;
679 log_debug("got %d from client %d", imsg
.hdr
.type
, c
->ibuf
.fd
);
680 switch (imsg
.hdr
.type
) {
682 if (datalen
!= sizeof commanddata
)
683 fatalx("bad MSG_COMMAND size");
684 memcpy(&commanddata
, imsg
.data
, sizeof commanddata
);
686 server_client_msg_command(c
, &commanddata
);
689 if (datalen
!= sizeof identifydata
)
690 fatalx("bad MSG_IDENTIFY size");
692 fatalx("MSG_IDENTIFY missing fd");
693 memcpy(&identifydata
, imsg
.data
, sizeof identifydata
);
695 c
->stdin_fd
= imsg
.fd
;
696 c
->stdin_event
= bufferevent_new(c
->stdin_fd
,
697 NULL
, NULL
, server_client_in_callback
, c
);
698 if (c
->stdin_event
== NULL
)
699 fatalx("failed to create stdin event");
701 if ((mode
= fcntl(c
->stdin_fd
, F_GETFL
)) != -1)
702 fcntl(c
->stdin_fd
, F_SETFL
, mode
|O_NONBLOCK
);
704 server_client_msg_identify(c
, &identifydata
, imsg
.fd
);
708 fatalx("bad MSG_STDOUT size");
710 fatalx("MSG_STDOUT missing fd");
712 c
->stdout_fd
= imsg
.fd
;
713 c
->stdout_event
= bufferevent_new(c
->stdout_fd
,
714 NULL
, NULL
, server_client_out_callback
, c
);
715 if (c
->stdout_event
== NULL
)
716 fatalx("failed to create stdout event");
718 if ((mode
= fcntl(c
->stdout_fd
, F_GETFL
)) != -1)
719 fcntl(c
->stdout_fd
, F_SETFL
, mode
|O_NONBLOCK
);
723 fatalx("bad MSG_STDERR size");
725 fatalx("MSG_STDERR missing fd");
727 c
->stderr_fd
= imsg
.fd
;
728 c
->stderr_event
= bufferevent_new(c
->stderr_fd
,
729 NULL
, NULL
, server_client_err_callback
, c
);
730 if (c
->stderr_event
== NULL
)
731 fatalx("failed to create stderr event");
733 if ((mode
= fcntl(c
->stderr_fd
, F_GETFL
)) != -1)
734 fcntl(c
->stderr_fd
, F_SETFL
, mode
|O_NONBLOCK
);
738 fatalx("bad MSG_RESIZE size");
740 if (tty_resize(&c
->tty
)) {
742 server_redraw_client(c
);
747 fatalx("bad MSG_EXITING size");
751 server_write_client(c
, MSG_EXITED
, NULL
, 0);
756 fatalx("bad MSG_WAKEUP size");
758 if (!(c
->flags
& CLIENT_SUSPENDED
))
760 c
->flags
&= ~CLIENT_SUSPENDED
;
762 if (gettimeofday(&c
->activity_time
, NULL
) != 0)
763 fatal("gettimeofday");
764 if (c
->session
!= NULL
) {
765 memcpy(&c
->session
->activity_time
,
767 sizeof c
->session
->activity_time
);
770 tty_start_tty(&c
->tty
);
771 server_redraw_client(c
);
775 if (datalen
!= sizeof environdata
)
776 fatalx("bad MSG_ENVIRON size");
777 memcpy(&environdata
, imsg
.data
, sizeof environdata
);
779 environdata
.var
[(sizeof environdata
.var
) - 1] = '\0';
780 if (strchr(environdata
.var
, '=') != NULL
)
781 environ_put(&c
->environ
, environdata
.var
);
785 fatalx("bad MSG_SHELL size");
787 server_client_msg_shell(c
);
790 fatalx("unexpected message");
797 /* Callback to send error message to client. */
799 server_client_msg_error(struct cmd_ctx
*ctx
, const char *fmt
, ...)
804 evbuffer_add_vprintf(ctx
->cmdclient
->stderr_event
->output
, fmt
, ap
);
807 bufferevent_write(ctx
->cmdclient
->stderr_event
, "\n", 1);
808 ctx
->cmdclient
->retcode
= 1;
811 /* Callback to send print message to client. */
813 server_client_msg_print(struct cmd_ctx
*ctx
, const char *fmt
, ...)
818 evbuffer_add_vprintf(ctx
->cmdclient
->stdout_event
->output
, fmt
, ap
);
821 bufferevent_write(ctx
->cmdclient
->stdout_event
, "\n", 1);
824 /* Callback to send print message to client, if not quiet. */
826 server_client_msg_info(struct cmd_ctx
*ctx
, const char *fmt
, ...)
830 if (options_get_number(&global_options
, "quiet"))
834 evbuffer_add_vprintf(ctx
->cmdclient
->stdout_event
->output
, fmt
, ap
);
837 bufferevent_write(ctx
->cmdclient
->stdout_event
, "\n", 1);
840 /* Handle command message. */
842 server_client_msg_command(struct client
*c
, struct msg_command_data
*data
)
845 struct cmd_list
*cmdlist
= NULL
;
849 ctx
.error
= server_client_msg_error
;
850 ctx
.print
= server_client_msg_print
;
851 ctx
.info
= server_client_msg_info
;
854 ctx
.curclient
= NULL
;
859 data
->argv
[(sizeof data
->argv
) - 1] = '\0';
860 if (cmd_unpack_argv(data
->argv
, sizeof data
->argv
, argc
, &argv
) != 0) {
861 server_client_msg_error(&ctx
, "command too long");
867 argv
= xcalloc(1, sizeof *argv
);
868 *argv
= xstrdup("new-session");
871 if ((cmdlist
= cmd_list_parse(argc
, argv
, &cause
)) == NULL
) {
872 server_client_msg_error(&ctx
, "%s", cause
);
873 cmd_free_argv(argc
, argv
);
876 cmd_free_argv(argc
, argv
);
878 if (cmd_list_exec(cmdlist
, &ctx
) != 1)
879 c
->flags
|= CLIENT_EXIT
;
880 cmd_list_free(cmdlist
);
885 cmd_list_free(cmdlist
);
886 c
->flags
|= CLIENT_EXIT
;
889 /* Handle identify message. */
891 server_client_msg_identify(
892 struct client
*c
, struct msg_identify_data
*data
, int fd
)
897 data
->cwd
[(sizeof data
->cwd
) - 1] = '\0';
898 if (*data
->cwd
!= '\0')
899 c
->cwd
= xstrdup(data
->cwd
);
903 if ((tty_fd
= dup(fd
)) == -1)
905 data
->term
[(sizeof data
->term
) - 1] = '\0';
906 tty_init(&c
->tty
, tty_fd
, data
->term
);
907 if (data
->flags
& IDENTIFY_UTF8
)
908 c
->tty
.flags
|= TTY_UTF8
;
909 if (data
->flags
& IDENTIFY_256COLOURS
)
910 c
->tty
.term_flags
|= TERM_256COLOURS
;
911 else if (data
->flags
& IDENTIFY_88COLOURS
)
912 c
->tty
.term_flags
|= TERM_88COLOURS
;
913 c
->tty
.key_callback
= server_client_handle_key
;
918 c
->flags
|= CLIENT_TERMINAL
;
921 /* Handle shell message. */
923 server_client_msg_shell(struct client
*c
)
925 struct msg_shell_data data
;
928 shell
= options_get_string(&global_s_options
, "default-shell");
930 if (*shell
== '\0' || areshell(shell
))
931 shell
= _PATH_BSHELL
;
932 if (strlcpy(data
.shell
, shell
, sizeof data
.shell
) >= sizeof data
.shell
)
933 strlcpy(data
.shell
, _PATH_BSHELL
, sizeof data
.shell
);
935 server_write_client(c
, MSG_SHELL
, &data
, sizeof data
);
936 c
->flags
|= CLIENT_BAD
; /* it will die after exec */