xref: /openbsd-src/usr.bin/tmux/cmd-attach-session.c (revision 134e9c117fe89ae8d41181b1409e96985cbe0b1a)
1 /* $OpenBSD: cmd-attach-session.c,v 1.68 2017/02/03 11:57:27 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #include "tmux.h"
28 
29 /*
30  * Attach existing session to the current terminal.
31  */
32 
33 static enum cmd_retval	cmd_attach_session_exec(struct cmd *,
34 			    struct cmdq_item *);
35 
36 const struct cmd_entry cmd_attach_session_entry = {
37 	.name = "attach-session",
38 	.alias = "attach",
39 
40 	.args = { "c:dErt:", 0, 0 },
41 	.usage = "[-dEr] [-c working-directory] " CMD_TARGET_SESSION_USAGE,
42 
43 	.tflag = CMD_SESSION_WITHPANE,
44 
45 	.flags = CMD_STARTSERVER,
46 	.exec = cmd_attach_session_exec
47 };
48 
49 enum cmd_retval
50 cmd_attach_session(struct cmdq_item *item, int dflag, int rflag,
51     const char *cflag, int Eflag)
52 {
53 	struct session		*s = item->state.tflag.s;
54 	struct client		*c = item->client, *c_loop;
55 	struct winlink		*wl = item->state.tflag.wl;
56 	struct window_pane	*wp = item->state.tflag.wp;
57 	char			*cause, *cwd;
58 	struct format_tree	*ft;
59 
60 	if (RB_EMPTY(&sessions)) {
61 		cmdq_error(item, "no sessions");
62 		return (CMD_RETURN_ERROR);
63 	}
64 
65 	if (c == NULL)
66 		return (CMD_RETURN_NORMAL);
67 	if (server_client_check_nested(c)) {
68 		cmdq_error(item, "sessions should be nested with care, "
69 		    "unset $TMUX to force");
70 		return (CMD_RETURN_ERROR);
71 	}
72 
73 	if (wl != NULL) {
74 		if (wp != NULL)
75 			window_set_active_pane(wp->window, wp);
76 		session_set_current(s, wl);
77 	}
78 
79 	if (cflag != NULL) {
80 		ft = format_create(item, FORMAT_NONE, 0);
81 		format_defaults(ft, c, s, wl, wp);
82 		cwd = format_expand(ft, cflag);
83 		format_free(ft);
84 
85 		free((void *)s->cwd);
86 		s->cwd = cwd;
87 	}
88 
89 	if (c->session != NULL) {
90 		if (dflag) {
91 			TAILQ_FOREACH(c_loop, &clients, entry) {
92 				if (c_loop->session != s || c == c_loop)
93 					continue;
94 				server_client_detach(c_loop, MSG_DETACH);
95 			}
96 		}
97 		if (!Eflag)
98 			environ_update(s->options, c->environ, s->environ);
99 
100 		c->session = s;
101 		status_timer_start(c);
102 		notify_client("client-session-changed", c);
103 		session_update_activity(s, NULL);
104 		gettimeofday(&s->last_attached_time, NULL);
105 		server_redraw_client(c);
106 		s->curw->flags &= ~WINLINK_ALERTFLAGS;
107 	} else {
108 		if (server_client_open(c, &cause) != 0) {
109 			cmdq_error(item, "open terminal failed: %s", cause);
110 			free(cause);
111 			return (CMD_RETURN_ERROR);
112 		}
113 		if (rflag)
114 			c->flags |= CLIENT_READONLY;
115 
116 		if (dflag) {
117 			TAILQ_FOREACH(c_loop, &clients, entry) {
118 				if (c_loop->session != s || c == c_loop)
119 					continue;
120 				server_client_detach(c_loop, MSG_DETACH);
121 			}
122 		}
123 		if (!Eflag)
124 			environ_update(s->options, c->environ, s->environ);
125 
126 		c->session = s;
127 		server_client_set_key_table(c, NULL);
128 		status_timer_start(c);
129 		notify_client("client-session-changed", c);
130 		session_update_activity(s, NULL);
131 		gettimeofday(&s->last_attached_time, NULL);
132 		server_redraw_client(c);
133 		s->curw->flags &= ~WINLINK_ALERTFLAGS;
134 
135 		if (~c->flags & CLIENT_CONTROL)
136 			proc_send(c->peer, MSG_READY, -1, NULL, 0);
137 		notify_client("client-attached", c);
138 		c->flags |= CLIENT_ATTACHED;
139 	}
140 	recalculate_sizes();
141 	alerts_check_session(s);
142 	server_update_socket();
143 
144 	return (CMD_RETURN_NORMAL);
145 }
146 
147 static enum cmd_retval
148 cmd_attach_session_exec(struct cmd *self, struct cmdq_item *item)
149 {
150 	struct args	*args = self->args;
151 
152 	return (cmd_attach_session(item, args_has(args, 'd'),
153 	    args_has(args, 'r'), args_get(args, 'c'), args_has(args, 'E')));
154 }
155