Alter how tmux handles the working directory to internally use file
[tmux-openbsd.git] / server-client.c
bloba39f56d8fd1d052aa9fb99578b253617104c0e8a
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>
20 #include <sys/ioctl.h>
22 #include <event.h>
23 #include <fcntl.h>
24 #include <paths.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <unistd.h>
30 #include "tmux.h"
32 void server_client_check_focus(struct window_pane *);
33 void server_client_check_resize(struct window_pane *);
34 void server_client_check_mouse(struct client *, struct window_pane *);
35 void server_client_repeat_timer(int, short, void *);
36 void server_client_check_exit(struct client *);
37 void server_client_check_redraw(struct client *);
38 void server_client_set_title(struct client *);
39 void server_client_reset_state(struct client *);
40 int server_client_assume_paste(struct session *);
42 int server_client_msg_dispatch(struct client *);
43 void server_client_msg_command(struct client *, struct imsg *);
44 void server_client_msg_identify(struct client *, struct imsg *);
45 void server_client_msg_shell(struct client *);
47 /* Create a new client. */
48 void
49 server_client_create(int fd)
51 struct client *c;
52 u_int i;
54 setblocking(fd, 0);
56 c = xcalloc(1, sizeof *c);
57 c->references = 0;
58 imsg_init(&c->ibuf, fd);
59 server_update_event(c);
61 if (gettimeofday(&c->creation_time, NULL) != 0)
62 fatal("gettimeofday failed");
63 memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
65 c->cmdq = cmdq_new(c);
66 c->cmdq->client_exit = 1;
68 c->stdin_data = evbuffer_new ();
69 c->stdout_data = evbuffer_new ();
70 c->stderr_data = evbuffer_new ();
72 c->tty.fd = -1;
73 c->title = NULL;
75 c->session = NULL;
76 c->last_session = NULL;
77 c->tty.sx = 80;
78 c->tty.sy = 24;
80 screen_init(&c->status, c->tty.sx, 1, 0);
81 RB_INIT(&c->status_new);
82 RB_INIT(&c->status_old);
84 c->message_string = NULL;
85 ARRAY_INIT(&c->message_log);
87 c->prompt_string = NULL;
88 c->prompt_buffer = NULL;
89 c->prompt_index = 0;
91 c->tty.mouse.xb = c->tty.mouse.button = 3;
92 c->tty.mouse.x = c->tty.mouse.y = -1;
93 c->tty.mouse.lx = c->tty.mouse.ly = -1;
94 c->tty.mouse.sx = c->tty.mouse.sy = -1;
95 c->tty.mouse.event = MOUSE_EVENT_UP;
96 c->tty.mouse.flags = 0;
98 c->flags |= CLIENT_FOCUSED;
100 evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
102 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
103 if (ARRAY_ITEM(&clients, i) == NULL) {
104 ARRAY_SET(&clients, i, c);
105 return;
108 ARRAY_ADD(&clients, c);
109 log_debug("new client %d", fd);
112 /* Open client terminal if needed. */
114 server_client_open(struct client *c, struct session *s, char **cause)
116 struct options *oo = s != NULL ? &s->options : &global_s_options;
117 char *overrides;
119 if (c->flags & CLIENT_CONTROL)
120 return (0);
122 if (!(c->flags & CLIENT_TERMINAL)) {
123 *cause = xstrdup ("not a terminal");
124 return (-1);
127 overrides = options_get_string(oo, "terminal-overrides");
128 if (tty_open(&c->tty, overrides, cause) != 0)
129 return (-1);
131 return (0);
134 /* Lost a client. */
135 void
136 server_client_lost(struct client *c)
138 struct message_entry *msg;
139 u_int i;
141 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
142 if (ARRAY_ITEM(&clients, i) == c)
143 ARRAY_SET(&clients, i, NULL);
145 log_debug("lost client %d", c->ibuf.fd);
148 * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
149 * and tty_free might close an unrelated fd.
151 if (c->flags & CLIENT_TERMINAL)
152 tty_free(&c->tty);
153 free(c->ttyname);
154 free(c->term);
156 evbuffer_free (c->stdin_data);
157 evbuffer_free (c->stdout_data);
158 if (c->stderr_data != c->stdout_data)
159 evbuffer_free (c->stderr_data);
161 status_free_jobs(&c->status_new);
162 status_free_jobs(&c->status_old);
163 screen_free(&c->status);
165 free(c->title);
166 close(c->cwd);
168 evtimer_del(&c->repeat_timer);
170 if (event_initialized(&c->identify_timer))
171 evtimer_del(&c->identify_timer);
173 free(c->message_string);
174 if (event_initialized (&c->message_timer))
175 evtimer_del(&c->message_timer);
176 for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
177 msg = &ARRAY_ITEM(&c->message_log, i);
178 free(msg->msg);
180 ARRAY_FREE(&c->message_log);
182 free(c->prompt_string);
183 free(c->prompt_buffer);
185 c->cmdq->dead = 1;
186 cmdq_free(c->cmdq);
187 c->cmdq = NULL;
189 environ_free(&c->environ);
191 close(c->ibuf.fd);
192 imsg_clear(&c->ibuf);
193 if (event_initialized(&c->event))
194 event_del(&c->event);
196 for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
197 if (ARRAY_ITEM(&dead_clients, i) == NULL) {
198 ARRAY_SET(&dead_clients, i, c);
199 break;
202 if (i == ARRAY_LENGTH(&dead_clients))
203 ARRAY_ADD(&dead_clients, c);
204 c->flags |= CLIENT_DEAD;
206 server_add_accept(0); /* may be more file descriptors now */
208 recalculate_sizes();
209 server_check_unattached();
210 server_update_socket();
213 /* Process a single client event. */
214 void
215 server_client_callback(int fd, short events, void *data)
217 struct client *c = data;
219 if (c->flags & CLIENT_DEAD)
220 return;
222 if (fd == c->ibuf.fd) {
223 if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
224 goto client_lost;
226 if (c->flags & CLIENT_BAD) {
227 if (c->ibuf.w.queued == 0)
228 goto client_lost;
229 return;
232 if (events & EV_READ && server_client_msg_dispatch(c) != 0)
233 goto client_lost;
236 server_push_stdout(c);
237 server_push_stderr(c);
239 server_update_event(c);
240 return;
242 client_lost:
243 server_client_lost(c);
246 /* Handle client status timer. */
247 void
248 server_client_status_timer(void)
250 struct client *c;
251 struct session *s;
252 struct timeval tv;
253 u_int i;
254 int interval;
255 time_t difference;
257 if (gettimeofday(&tv, NULL) != 0)
258 fatal("gettimeofday failed");
260 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
261 c = ARRAY_ITEM(&clients, i);
262 if (c == NULL || c->session == NULL)
263 continue;
264 if (c->message_string != NULL || c->prompt_string != NULL) {
266 * Don't need timed redraw for messages/prompts so bail
267 * now. The status timer isn't reset when they are
268 * redrawn anyway.
270 continue;
272 s = c->session;
274 if (!options_get_number(&s->options, "status"))
275 continue;
276 interval = options_get_number(&s->options, "status-interval");
278 difference = tv.tv_sec - c->status_timer.tv_sec;
279 if (difference >= interval) {
280 status_update_jobs(c);
281 c->flags |= CLIENT_STATUS;
286 /* Check for mouse keys. */
287 void
288 server_client_check_mouse(struct client *c, struct window_pane *wp)
290 struct session *s = c->session;
291 struct options *oo = &s->options;
292 struct mouse_event *m = &c->tty.mouse;
293 int statusat;
295 statusat = status_at_line(c);
297 /* Is this a window selection click on the status line? */
298 if (statusat != -1 && m->y == (u_int)statusat &&
299 options_get_number(oo, "mouse-select-window")) {
300 if (m->event & MOUSE_EVENT_CLICK) {
301 status_set_window_at(c, m->x);
302 } else if (m->event == MOUSE_EVENT_WHEEL) {
303 if (m->wheel == MOUSE_WHEEL_UP)
304 session_previous(c->session, 0);
305 else if (m->wheel == MOUSE_WHEEL_DOWN)
306 session_next(c->session, 0);
307 server_redraw_session(s);
309 recalculate_sizes();
310 return;
314 * Not on status line - adjust mouse position if status line is at the
315 * top and limit if at the bottom. From here on a struct mouse
316 * represents the offset onto the window itself.
318 if (statusat == 0 && m->y > 0)
319 m->y--;
320 else if (statusat > 0 && m->y >= (u_int)statusat)
321 m->y = statusat - 1;
323 /* Is this a pane selection? Allow down only in copy mode. */
324 if (options_get_number(oo, "mouse-select-pane") &&
325 (m->event == MOUSE_EVENT_DOWN || wp->mode != &window_copy_mode)) {
326 window_set_active_at(wp->window, m->x, m->y);
327 server_redraw_window_borders(wp->window);
328 wp = wp->window->active; /* may have changed */
331 /* Check if trying to resize pane. */
332 if (options_get_number(oo, "mouse-resize-pane"))
333 layout_resize_pane_mouse(c);
335 /* Update last and pass through to client. */
336 window_pane_mouse(wp, c->session, m);
339 /* Is this fast enough to probably be a paste? */
341 server_client_assume_paste(struct session *s)
343 struct timeval tv;
344 int t;
346 if ((t = options_get_number(&s->options, "assume-paste-time")) == 0)
347 return (0);
349 timersub(&s->activity_time, &s->last_activity_time, &tv);
350 if (tv.tv_sec == 0 && tv.tv_usec < t * 1000)
351 return (1);
352 return (0);
355 /* Handle data key input from client. */
356 void
357 server_client_handle_key(struct client *c, int key)
359 struct session *s;
360 struct window *w;
361 struct window_pane *wp;
362 struct timeval tv;
363 struct key_binding *bd;
364 int xtimeout, isprefix, ispaste;
366 /* Check the client is good to accept input. */
367 if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
368 return;
370 if (c->session == NULL)
371 return;
372 s = c->session;
374 /* Update the activity timer. */
375 if (gettimeofday(&c->activity_time, NULL) != 0)
376 fatal("gettimeofday failed");
378 memcpy(&s->last_activity_time, &s->activity_time,
379 sizeof s->last_activity_time);
380 memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
382 w = c->session->curw->window;
383 wp = w->active;
385 /* Special case: number keys jump to pane in identify mode. */
386 if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
387 if (c->flags & CLIENT_READONLY)
388 return;
389 window_unzoom(w);
390 wp = window_pane_at_index(w, key - '0');
391 if (wp != NULL && window_pane_visible(wp))
392 window_set_active_pane(w, wp);
393 server_clear_identify(c);
394 return;
397 /* Handle status line. */
398 if (!(c->flags & CLIENT_READONLY)) {
399 status_message_clear(c);
400 server_clear_identify(c);
402 if (c->prompt_string != NULL) {
403 if (!(c->flags & CLIENT_READONLY))
404 status_prompt_key(c, key);
405 return;
408 /* Check for mouse keys. */
409 if (key == KEYC_MOUSE) {
410 if (c->flags & CLIENT_READONLY)
411 return;
412 server_client_check_mouse(c, wp);
413 return;
416 /* Is this a prefix key? */
417 if (key == options_get_number(&s->options, "prefix"))
418 isprefix = 1;
419 else if (key == options_get_number(&s->options, "prefix2"))
420 isprefix = 1;
421 else
422 isprefix = 0;
424 /* Treat prefix as a regular key when pasting is detected. */
425 ispaste = server_client_assume_paste(s);
426 if (ispaste)
427 isprefix = 0;
429 /* No previous prefix key. */
430 if (!(c->flags & CLIENT_PREFIX)) {
431 if (isprefix) {
432 c->flags |= CLIENT_PREFIX;
433 server_status_client(c);
434 return;
437 /* Try as a non-prefix key binding. */
438 if (ispaste || (bd = key_bindings_lookup(key)) == NULL) {
439 if (!(c->flags & CLIENT_READONLY))
440 window_pane_key(wp, s, key);
441 } else
442 key_bindings_dispatch(bd, c);
443 return;
446 /* Prefix key already pressed. Reset prefix and lookup key. */
447 c->flags &= ~CLIENT_PREFIX;
448 server_status_client(c);
449 if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
450 /* If repeating, treat this as a key, else ignore. */
451 if (c->flags & CLIENT_REPEAT) {
452 c->flags &= ~CLIENT_REPEAT;
453 if (isprefix)
454 c->flags |= CLIENT_PREFIX;
455 else if (!(c->flags & CLIENT_READONLY))
456 window_pane_key(wp, s, key);
458 return;
461 /* If already repeating, but this key can't repeat, skip it. */
462 if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
463 c->flags &= ~CLIENT_REPEAT;
464 if (isprefix)
465 c->flags |= CLIENT_PREFIX;
466 else if (!(c->flags & CLIENT_READONLY))
467 window_pane_key(wp, s, key);
468 return;
471 /* If this key can repeat, reset the repeat flags and timer. */
472 xtimeout = options_get_number(&s->options, "repeat-time");
473 if (xtimeout != 0 && bd->can_repeat) {
474 c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
476 tv.tv_sec = xtimeout / 1000;
477 tv.tv_usec = (xtimeout % 1000) * 1000L;
478 evtimer_del(&c->repeat_timer);
479 evtimer_add(&c->repeat_timer, &tv);
482 /* Dispatch the command. */
483 key_bindings_dispatch(bd, c);
486 /* Client functions that need to happen every loop. */
487 void
488 server_client_loop(void)
490 struct client *c;
491 struct window *w;
492 struct window_pane *wp;
493 u_int i;
495 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
496 c = ARRAY_ITEM(&clients, i);
497 if (c == NULL)
498 continue;
500 server_client_check_exit(c);
501 if (c->session != NULL) {
502 server_client_check_redraw(c);
503 server_client_reset_state(c);
508 * Any windows will have been redrawn as part of clients, so clear
509 * their flags now. Also check pane focus and resize.
511 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
512 w = ARRAY_ITEM(&windows, i);
513 if (w == NULL)
514 continue;
516 w->flags &= ~WINDOW_REDRAW;
517 TAILQ_FOREACH(wp, &w->panes, entry) {
518 if (wp->fd != -1) {
519 server_client_check_focus(wp);
520 server_client_check_resize(wp);
522 wp->flags &= ~PANE_REDRAW;
527 /* Check if pane should be resized. */
528 void
529 server_client_check_resize(struct window_pane *wp)
531 struct winsize ws;
533 if (!(wp->flags & PANE_RESIZE))
534 return;
536 memset(&ws, 0, sizeof ws);
537 ws.ws_col = wp->sx;
538 ws.ws_row = wp->sy;
540 if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
541 fatal("ioctl failed");
543 wp->flags &= ~PANE_RESIZE;
546 /* Check whether pane should be focused. */
547 void
548 server_client_check_focus(struct window_pane *wp)
550 u_int i;
551 struct client *c;
552 int push;
554 /* Are focus events off? */
555 if (!options_get_number(&global_options, "focus-events"))
556 return;
558 /* Do we need to push the focus state? */
559 push = wp->flags & PANE_FOCUSPUSH;
560 wp->flags &= ~PANE_FOCUSPUSH;
562 /* If we don't care about focus, forget it. */
563 if (!(wp->base.mode & MODE_FOCUSON))
564 return;
566 /* If we're not the active pane in our window, we're not focused. */
567 if (wp->window->active != wp)
568 goto not_focused;
570 /* If we're in a mode, we're not focused. */
571 if (wp->screen != &wp->base)
572 goto not_focused;
575 * If our window is the current window in any focused clients with an
576 * attached session, we're focused.
578 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
579 c = ARRAY_ITEM(&clients, i);
580 if (c == NULL || c->session == NULL)
581 continue;
583 if (!(c->flags & CLIENT_FOCUSED))
584 continue;
585 if (c->session->flags & SESSION_UNATTACHED)
586 continue;
588 if (c->session->curw->window == wp->window)
589 goto focused;
592 not_focused:
593 if (push || (wp->flags & PANE_FOCUSED))
594 bufferevent_write(wp->event, "\033[O", 3);
595 wp->flags &= ~PANE_FOCUSED;
596 return;
598 focused:
599 if (push || !(wp->flags & PANE_FOCUSED))
600 bufferevent_write(wp->event, "\033[I", 3);
601 wp->flags |= PANE_FOCUSED;
605 * Update cursor position and mode settings. The scroll region and attributes
606 * are cleared when idle (waiting for an event) as this is the most likely time
607 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
608 * compromise between excessive resets and likelihood of an interrupt.
610 * tty_region/tty_reset/tty_update_mode already take care of not resetting
611 * things that are already in their default state.
613 void
614 server_client_reset_state(struct client *c)
616 struct window *w = c->session->curw->window;
617 struct window_pane *wp = w->active;
618 struct screen *s = wp->screen;
619 struct options *oo = &c->session->options;
620 struct options *wo = &w->options;
621 int status, mode, o;
623 if (c->flags & CLIENT_SUSPENDED)
624 return;
626 if (c->flags & CLIENT_CONTROL)
627 return;
629 tty_region(&c->tty, 0, c->tty.sy - 1);
631 status = options_get_number(oo, "status");
632 if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
633 tty_cursor(&c->tty, 0, 0);
634 else {
635 o = status && options_get_number (oo, "status-position") == 0;
636 tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
640 * Resizing panes with the mouse requires at least button mode to give
641 * a smooth appearance.
643 mode = s->mode;
644 if ((c->tty.mouse.flags & MOUSE_RESIZE_PANE) &&
645 !(mode & (MODE_MOUSE_BUTTON|MODE_MOUSE_ANY)))
646 mode |= MODE_MOUSE_BUTTON;
649 * Any mode will do for mouse-select-pane, but set standard mode if
650 * none.
652 if ((mode & ALL_MOUSE_MODES) == 0) {
653 if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
654 options_get_number(oo, "mouse-select-pane"))
655 mode |= MODE_MOUSE_STANDARD;
656 else if (options_get_number(oo, "mouse-resize-pane"))
657 mode |= MODE_MOUSE_STANDARD;
658 else if (options_get_number(oo, "mouse-select-window"))
659 mode |= MODE_MOUSE_STANDARD;
660 else if (options_get_number(wo, "mode-mouse"))
661 mode |= MODE_MOUSE_STANDARD;
665 * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
666 * user has set mouse-utf8 and any mouse mode is in effect, turn on
667 * UTF-8 mouse input. If the receiving terminal hasn't requested it
668 * (that is, it isn't in s->mode), then it'll be converted in
669 * input_mouse.
671 if ((c->tty.flags & TTY_UTF8) &&
672 (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
673 mode |= MODE_MOUSE_UTF8;
674 else
675 mode &= ~MODE_MOUSE_UTF8;
677 /* Set the terminal mode and reset attributes. */
678 tty_update_mode(&c->tty, mode, s);
679 tty_reset(&c->tty);
682 /* Repeat time callback. */
683 void
684 server_client_repeat_timer(unused int fd, unused short events, void *data)
686 struct client *c = data;
688 if (c->flags & CLIENT_REPEAT) {
689 if (c->flags & CLIENT_PREFIX)
690 server_status_client(c);
691 c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
695 /* Check if client should be exited. */
696 void
697 server_client_check_exit(struct client *c)
699 if (!(c->flags & CLIENT_EXIT))
700 return;
702 if (EVBUFFER_LENGTH(c->stdin_data) != 0)
703 return;
704 if (EVBUFFER_LENGTH(c->stdout_data) != 0)
705 return;
706 if (EVBUFFER_LENGTH(c->stderr_data) != 0)
707 return;
709 server_write_client(c, MSG_EXIT, &c->retval, sizeof c->retval);
710 c->flags &= ~CLIENT_EXIT;
713 /* Check for client redraws. */
714 void
715 server_client_check_redraw(struct client *c)
717 struct session *s = c->session;
718 struct window_pane *wp;
719 int flags, redraw;
721 if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
722 return;
724 flags = c->tty.flags & TTY_FREEZE;
725 c->tty.flags &= ~TTY_FREEZE;
727 if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
728 if (options_get_number(&s->options, "set-titles"))
729 server_client_set_title(c);
731 if (c->message_string != NULL)
732 redraw = status_message_redraw(c);
733 else if (c->prompt_string != NULL)
734 redraw = status_prompt_redraw(c);
735 else
736 redraw = status_redraw(c);
737 if (!redraw)
738 c->flags &= ~CLIENT_STATUS;
741 if (c->flags & CLIENT_REDRAW) {
742 screen_redraw_screen(c, 0, 0);
743 c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
744 } else if (c->flags & CLIENT_REDRAWWINDOW) {
745 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
746 screen_redraw_pane(c, wp);
747 c->flags &= ~CLIENT_REDRAWWINDOW;
748 } else {
749 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
750 if (wp->flags & PANE_REDRAW)
751 screen_redraw_pane(c, wp);
755 if (c->flags & CLIENT_BORDERS)
756 screen_redraw_screen(c, 0, 1);
758 if (c->flags & CLIENT_STATUS)
759 screen_redraw_screen(c, 1, 0);
761 c->tty.flags |= flags;
763 c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
766 /* Set client title. */
767 void
768 server_client_set_title(struct client *c)
770 struct session *s = c->session;
771 const char *template;
772 char *title;
774 template = options_get_string(&s->options, "set-titles-string");
776 title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
777 if (c->title == NULL || strcmp(title, c->title) != 0) {
778 free(c->title);
779 c->title = xstrdup(title);
780 tty_set_title(&c->tty, c->title);
782 free(title);
785 /* Dispatch message from client. */
787 server_client_msg_dispatch(struct client *c)
789 struct imsg imsg;
790 struct msg_stdin_data stdindata;
791 const char *data;
792 ssize_t n, datalen;
794 if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
795 return (-1);
797 for (;;) {
798 if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
799 return (-1);
800 if (n == 0)
801 return (0);
803 data = imsg.data;
804 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
806 if (imsg.hdr.peerid != PROTOCOL_VERSION) {
807 server_write_client(c, MSG_VERSION, NULL, 0);
808 c->flags |= CLIENT_BAD;
809 imsg_free(&imsg);
810 continue;
813 log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
814 switch (imsg.hdr.type) {
815 case MSG_IDENTIFY_FLAGS:
816 case MSG_IDENTIFY_TERM:
817 case MSG_IDENTIFY_TTYNAME:
818 case MSG_IDENTIFY_CWD:
819 case MSG_IDENTIFY_STDIN:
820 case MSG_IDENTIFY_ENVIRON:
821 case MSG_IDENTIFY_DONE:
822 server_client_msg_identify(c, &imsg);
823 break;
824 case MSG_COMMAND:
825 server_client_msg_command(c, &imsg);
826 break;
827 case MSG_STDIN:
828 if (datalen != sizeof stdindata)
829 fatalx("bad MSG_STDIN size");
830 memcpy(&stdindata, data, sizeof stdindata);
832 if (c->stdin_callback == NULL)
833 break;
834 if (stdindata.size <= 0)
835 c->stdin_closed = 1;
836 else {
837 evbuffer_add(c->stdin_data, stdindata.data,
838 stdindata.size);
840 c->stdin_callback(c, c->stdin_closed,
841 c->stdin_callback_data);
842 break;
843 case MSG_RESIZE:
844 if (datalen != 0)
845 fatalx("bad MSG_RESIZE size");
847 if (c->flags & CLIENT_CONTROL)
848 break;
849 if (tty_resize(&c->tty)) {
850 recalculate_sizes();
851 server_redraw_client(c);
853 break;
854 case MSG_EXITING:
855 if (datalen != 0)
856 fatalx("bad MSG_EXITING size");
858 c->session = NULL;
859 tty_close(&c->tty);
860 server_write_client(c, MSG_EXITED, NULL, 0);
861 break;
862 case MSG_WAKEUP:
863 case MSG_UNLOCK:
864 if (datalen != 0)
865 fatalx("bad MSG_WAKEUP size");
867 if (!(c->flags & CLIENT_SUSPENDED))
868 break;
869 c->flags &= ~CLIENT_SUSPENDED;
871 if (gettimeofday(&c->activity_time, NULL) != 0)
872 fatal("gettimeofday");
873 if (c->session != NULL)
874 session_update_activity(c->session);
876 tty_start_tty(&c->tty);
877 server_redraw_client(c);
878 recalculate_sizes();
879 break;
880 case MSG_SHELL:
881 if (datalen != 0)
882 fatalx("bad MSG_SHELL size");
884 server_client_msg_shell(c);
885 break;
888 imsg_free(&imsg);
892 /* Handle command message. */
893 void
894 server_client_msg_command(struct client *c, struct imsg *imsg)
896 struct msg_command_data data;
897 char *buf;
898 size_t len;
899 struct cmd_list *cmdlist = NULL;
900 int argc;
901 char **argv, *cause;
903 if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
904 fatalx("bad MSG_COMMAND size");
905 memcpy(&data, imsg->data, sizeof data);
907 buf = (char*)imsg->data + sizeof data;
908 len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
909 if (len > 0 && buf[len - 1] != '\0')
910 fatalx("bad MSG_COMMAND string");
912 argc = data.argc;
913 if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
914 cmdq_error(c->cmdq, "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, NULL, 0, &cause)) == NULL) {
925 cmdq_error(c->cmdq, "%s", cause);
926 cmd_free_argv(argc, argv);
927 goto error;
929 cmd_free_argv(argc, argv);
931 cmdq_run(c->cmdq, cmdlist);
932 cmd_list_free(cmdlist);
933 return;
935 error:
936 if (cmdlist != NULL)
937 cmd_list_free(cmdlist);
939 c->flags |= CLIENT_EXIT;
942 /* Handle identify message. */
943 void
944 server_client_msg_identify(struct client *c, struct imsg *imsg)
946 const char *data;
947 size_t datalen;
948 int flags;
950 if (c->flags & CLIENT_IDENTIFIED)
951 fatalx("out-of-order identify message");
953 data = imsg->data;
954 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
956 switch (imsg->hdr.type) {
957 case MSG_IDENTIFY_FLAGS:
958 if (datalen != sizeof flags)
959 fatalx("bad MSG_IDENTIFY_FLAGS size");
960 memcpy(&flags, data, sizeof flags);
961 c->flags |= flags;
962 break;
963 case MSG_IDENTIFY_TERM:
964 if (data[datalen - 1] != '\0')
965 fatalx("bad MSG_IDENTIFY_TERM string");
966 c->term = xstrdup(data);
967 break;
968 case MSG_IDENTIFY_TTYNAME:
969 if (data[datalen - 1] != '\0')
970 fatalx("bad MSG_IDENTIFY_TTYNAME string");
971 c->ttyname = xstrdup(data);
972 break;
973 case MSG_IDENTIFY_CWD:
974 if (datalen != 0)
975 fatalx("bad MSG_IDENTIFY_CWD size");
976 c->cwd = imsg->fd;
977 break;
978 case MSG_IDENTIFY_STDIN:
979 if (datalen != 0)
980 fatalx("bad MSG_IDENTIFY_STDIN size");
981 c->fd = imsg->fd;
982 break;
983 case MSG_IDENTIFY_ENVIRON:
984 if (data[datalen - 1] != '\0')
985 fatalx("bad MSG_IDENTIFY_ENVIRON string");
986 if (strchr(data, '=') != NULL)
987 environ_put(&c->environ, data);
988 break;
989 default:
990 break;
993 if (imsg->hdr.type != MSG_IDENTIFY_DONE)
994 return;
995 c->flags |= CLIENT_IDENTIFIED;
997 #ifdef __CYGWIN__
998 c->fd = open(c->ttyname, O_RDWR|O_NOCTTY);
999 c->cwd = open(".", O_RDONLY);
1000 #endif
1002 if (c->flags & CLIENT_CONTROL) {
1003 c->stdin_callback = control_callback;
1005 evbuffer_free(c->stderr_data);
1006 c->stderr_data = c->stdout_data;
1008 if (c->flags & CLIENT_CONTROLCONTROL)
1009 evbuffer_add_printf(c->stdout_data, "\033P1000p");
1010 server_write_client(c, MSG_STDIN, NULL, 0);
1012 c->tty.fd = -1;
1013 c->tty.log_fd = -1;
1015 close(c->fd);
1016 c->fd = -1;
1018 return;
1021 if (c->fd == -1)
1022 return;
1023 if (!isatty(c->fd)) {
1024 close(c->fd);
1025 c->fd = -1;
1026 return;
1028 tty_init(&c->tty, c, c->fd, c->term);
1029 if (c->flags & CLIENT_UTF8)
1030 c->tty.flags |= TTY_UTF8;
1031 if (c->flags & CLIENT_256COLOURS)
1032 c->tty.term_flags |= TERM_256COLOURS;
1034 tty_resize(&c->tty);
1036 if (!(c->flags & CLIENT_CONTROL))
1037 c->flags |= CLIENT_TERMINAL;
1040 /* Handle shell message. */
1041 void
1042 server_client_msg_shell(struct client *c)
1044 const char *shell;
1046 shell = options_get_string(&global_s_options, "default-shell");
1047 if (*shell == '\0' || areshell(shell))
1048 shell = _PATH_BSHELL;
1049 server_write_client(c, MSG_SHELL, shell, strlen(shell) + 1);
1051 c->flags |= CLIENT_BAD; /* it will die after exec */