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