xref: /openbsd-src/usr.bin/tmux/cmd-attach-session.c (revision fcde59b201a29a2b4570b00b71e7aa25d61cb5c1)
1 /* $OpenBSD: cmd-attach-session.c,v 1.85 2020/09/03 12:47:33 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:dEf:rt:x", 0, 0 },
41 	.usage = "[-dErx] [-c working-directory] [-f flags] "
42 	         CMD_TARGET_SESSION_USAGE,
43 
44 	/* -t is special */
45 
46 	.flags = CMD_STARTSERVER,
47 	.exec = cmd_attach_session_exec
48 };
49 
50 enum cmd_retval
51 cmd_attach_session(struct cmdq_item *item, const char *tflag, int dflag,
52     int xflag, int rflag, const char *cflag, int Eflag, const char *fflag)
53 {
54 	struct cmd_find_state	*current = cmdq_get_current(item);
55 	struct cmd_find_state	 target;
56 	enum cmd_find_type	 type;
57 	int			 flags;
58 	struct client		*c = cmdq_get_client(item), *c_loop;
59 	struct session		*s;
60 	struct winlink		*wl;
61 	struct window_pane	*wp;
62 	char			*cwd, *cause;
63 	enum msgtype		 msgtype;
64 
65 	if (RB_EMPTY(&sessions)) {
66 		cmdq_error(item, "no sessions");
67 		return (CMD_RETURN_ERROR);
68 	}
69 
70 	if (c == NULL)
71 		return (CMD_RETURN_NORMAL);
72 	if (server_client_check_nested(c)) {
73 		cmdq_error(item, "sessions should be nested with care, "
74 		    "unset $TMUX to force");
75 		return (CMD_RETURN_ERROR);
76 	}
77 
78 	if (tflag != NULL && tflag[strcspn(tflag, ":.")] != '\0') {
79 		type = CMD_FIND_PANE;
80 		flags = 0;
81 	} else {
82 		type = CMD_FIND_SESSION;
83 		flags = CMD_FIND_PREFER_UNATTACHED;
84 	}
85 	if (cmd_find_target(&target, item, tflag, type, flags) != 0)
86 		return (CMD_RETURN_ERROR);
87 	s = target.s;
88 	wl = target.wl;
89 	wp = target.wp;
90 
91 	if (wl != NULL) {
92 		if (wp != NULL)
93 			window_set_active_pane(wp->window, wp, 1);
94 		session_set_current(s, wl);
95 		if (wp != NULL)
96 			cmd_find_from_winlink_pane(current, wl, wp, 0);
97 		else
98 			cmd_find_from_winlink(current, wl, 0);
99 	}
100 
101 	if (cflag != NULL) {
102 		cwd = format_single(item, cflag, c, s, wl, wp);
103 		free((void *)s->cwd);
104 		s->cwd = cwd;
105 	}
106 	if (fflag)
107 		server_client_set_flags(c, fflag);
108 	if (rflag)
109 		c->flags |= (CLIENT_READONLY|CLIENT_IGNORESIZE);
110 
111 	c->last_session = c->session;
112 	if (c->session != NULL) {
113 		if (dflag || xflag) {
114 			if (xflag)
115 				msgtype = MSG_DETACHKILL;
116 			else
117 				msgtype = MSG_DETACH;
118 			TAILQ_FOREACH(c_loop, &clients, entry) {
119 				if (c_loop->session != s || c == c_loop)
120 					continue;
121 				server_client_detach(c_loop, msgtype);
122 			}
123 		}
124 		if (!Eflag)
125 			environ_update(s->options, c->environ, s->environ);
126 
127 		c->session = s;
128 		if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
129 			server_client_set_key_table(c, NULL);
130 		tty_update_client_offset(c);
131 		status_timer_start(c);
132 		notify_client("client-session-changed", c);
133 		session_update_activity(s, NULL);
134 		gettimeofday(&s->last_attached_time, NULL);
135 		server_redraw_client(c);
136 		s->curw->flags &= ~WINLINK_ALERTFLAGS;
137 		s->curw->window->latest = c;
138 	} else {
139 		if (server_client_open(c, &cause) != 0) {
140 			cmdq_error(item, "open terminal failed: %s", cause);
141 			free(cause);
142 			return (CMD_RETURN_ERROR);
143 		}
144 
145 		if (dflag || xflag) {
146 			if (xflag)
147 				msgtype = MSG_DETACHKILL;
148 			else
149 				msgtype = MSG_DETACH;
150 			TAILQ_FOREACH(c_loop, &clients, entry) {
151 				if (c_loop->session != s || c == c_loop)
152 					continue;
153 				server_client_detach(c_loop, msgtype);
154 			}
155 		}
156 		if (!Eflag)
157 			environ_update(s->options, c->environ, s->environ);
158 
159 		c->session = s;
160 		server_client_set_key_table(c, NULL);
161 		tty_update_client_offset(c);
162 		status_timer_start(c);
163 		notify_client("client-session-changed", c);
164 		session_update_activity(s, NULL);
165 		gettimeofday(&s->last_attached_time, NULL);
166 		server_redraw_client(c);
167 		s->curw->flags &= ~WINLINK_ALERTFLAGS;
168 		s->curw->window->latest = c;
169 
170 		if (~c->flags & CLIENT_CONTROL)
171 			proc_send(c->peer, MSG_READY, -1, NULL, 0);
172 		notify_client("client-attached", c);
173 		c->flags |= CLIENT_ATTACHED;
174 	}
175 	recalculate_sizes();
176 	alerts_check_session(s);
177 	server_update_socket();
178 
179 	return (CMD_RETURN_NORMAL);
180 }
181 
182 static enum cmd_retval
183 cmd_attach_session_exec(struct cmd *self, struct cmdq_item *item)
184 {
185 	struct args	*args = cmd_get_args(self);
186 
187 	return (cmd_attach_session(item, args_get(args, 't'),
188 	    args_has(args, 'd'), args_has(args, 'x'), args_has(args, 'r'),
189 	    args_get(args, 'c'), args_has(args, 'E'), args_get(args, 'f')));
190 }
191