xref: /openbsd-src/usr.bin/tmux/cmd-attach-session.c (revision d89252e541ebb5e90a6b7e1089b8f396e19ca235)
1 /* $OpenBSD: cmd-attach-session.c,v 1.44 2015/10/27 15:58:42 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 enum cmd_retval	cmd_attach_session_exec(struct cmd *, struct cmd_q *);
34 
35 const struct cmd_entry cmd_attach_session_entry = {
36 	"attach-session", "attach",
37 	"c:dErt:", 0, 0,
38 	"[-dEr] [-c working-directory] " CMD_TARGET_SESSION_USAGE,
39 	CMD_STARTSERVER,
40 	cmd_attach_session_exec
41 };
42 
43 enum cmd_retval
44 cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag,
45     const char *cflag, int Eflag)
46 {
47 	struct session		*s;
48 	struct client		*c = cmdq->client, *c_loop;
49 	struct winlink		*wl = NULL;
50 	struct window		*w = NULL;
51 	struct window_pane	*wp = NULL;
52 	const char		*update;
53 	char			*cause;
54 	int			 fd;
55 	struct format_tree	*ft;
56 	char			*cp;
57 
58 	if (RB_EMPTY(&sessions)) {
59 		cmdq_error(cmdq, "no sessions");
60 		return (CMD_RETURN_ERROR);
61 	}
62 
63 	if (tflag == NULL) {
64 		if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL)
65 			return (CMD_RETURN_ERROR);
66 	} else if (tflag[strcspn(tflag, ":.")] != '\0') {
67 		if ((wl = cmd_find_pane(cmdq, tflag, &s, &wp)) == NULL)
68 			return (CMD_RETURN_ERROR);
69 	} else {
70 		if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL)
71 			return (CMD_RETURN_ERROR);
72 		w = window_find_by_id_str(tflag);
73 		if (w == NULL) {
74 			wp = window_pane_find_by_id_str(tflag);
75 			if (wp != NULL)
76 				w = wp->window;
77 		}
78 		if (w != NULL)
79 			wl = winlink_find_by_window(&s->windows, w);
80 	}
81 
82 	if (c == NULL)
83 		return (CMD_RETURN_NORMAL);
84 	if (server_client_check_nested(c)) {
85 		cmdq_error(cmdq, "sessions should be nested with care, "
86 		    "unset $TMUX to force");
87 		return (CMD_RETURN_ERROR);
88 	}
89 
90 	if (wl != NULL) {
91 		if (wp != NULL)
92 			window_set_active_pane(wp->window, wp);
93 		session_set_current(s, wl);
94 	}
95 
96 	if (cflag != NULL) {
97 		ft = format_create();
98 		format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s,
99 		    NULL, NULL);
100 		cp = format_expand(ft, cflag);
101 		format_free(ft);
102 
103 		fd = open(cp, O_RDONLY|O_DIRECTORY);
104 		free(cp);
105 		if (fd == -1) {
106 			cmdq_error(cmdq, "bad working directory: %s",
107 			    strerror(errno));
108 			return (CMD_RETURN_ERROR);
109 		}
110 		close(s->cwd);
111 		s->cwd = fd;
112 	}
113 
114 	if (c->session != NULL) {
115 		if (dflag) {
116 			TAILQ_FOREACH(c_loop, &clients, entry) {
117 				if (c_loop->session != s || c == c_loop)
118 					continue;
119 				proc_send_s(c->peer, MSG_DETACH, s->name);
120 			}
121 		}
122 
123 		if (!Eflag) {
124 			update = options_get_string(s->options,
125 			    "update-environment");
126 			environ_update(update, &c->environ, &s->environ);
127 		}
128 
129 		c->session = s;
130 		status_timer_start(c);
131 		notify_attached_session_changed(c);
132 		session_update_activity(s, NULL);
133 		gettimeofday(&s->last_attached_time, NULL);
134 		server_redraw_client(c);
135 		s->curw->flags &= ~WINLINK_ALERTFLAGS;
136 	} else {
137 		if (server_client_open(c, &cause) != 0) {
138 			cmdq_error(cmdq, "open terminal failed: %s", cause);
139 			free(cause);
140 			return (CMD_RETURN_ERROR);
141 		}
142 
143 		if (rflag)
144 			c->flags |= CLIENT_READONLY;
145 
146 		if (dflag) {
147 			TAILQ_FOREACH(c_loop, &clients, entry) {
148 				if (c_loop->session != s || c == c_loop)
149 					continue;
150 				proc_send_s(c->peer, MSG_DETACH, s->name);
151 			}
152 		}
153 
154 		if (!Eflag) {
155 			update = options_get_string(s->options,
156 			    "update-environment");
157 			environ_update(update, &c->environ, &s->environ);
158 		}
159 
160 		c->session = s;
161 		status_timer_start(c);
162 		notify_attached_session_changed(c);
163 		session_update_activity(s, NULL);
164 		gettimeofday(&s->last_attached_time, NULL);
165 		server_redraw_client(c);
166 		s->curw->flags &= ~WINLINK_ALERTFLAGS;
167 
168 		if (~c->flags & CLIENT_CONTROL)
169 			proc_send(c->peer, MSG_READY, -1, NULL, 0);
170 		cmdq->client_exit = 0;
171 	}
172 	recalculate_sizes();
173 	server_update_socket();
174 
175 	return (CMD_RETURN_NORMAL);
176 }
177 
178 enum cmd_retval
179 cmd_attach_session_exec(struct cmd *self, struct cmd_q *cmdq)
180 {
181 	struct args	*args = self->args;
182 
183 	return (cmd_attach_session(cmdq, args_get(args, 't'),
184 	    args_has(args, 'd'), args_has(args, 'r'), args_get(args, 'c'),
185 	    args_has(args, 'E')));
186 }
187