xref: /openbsd-src/usr.bin/tmux/cmd-attach-session.c (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1 /* $OpenBSD: cmd-attach-session.c,v 1.24 2013/03/24 09:58:40 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 <stdlib.h>
22 
23 #include "tmux.h"
24 
25 /*
26  * Attach existing session to the current terminal.
27  */
28 
29 enum cmd_retval	cmd_attach_session_exec(struct cmd *, struct cmd_q *);
30 
31 const struct cmd_entry cmd_attach_session_entry = {
32 	"attach-session", "attach",
33 	"drt:", 0, 0,
34 	"[-dr] " CMD_TARGET_SESSION_USAGE,
35 	CMD_CANTNEST|CMD_STARTSERVER|CMD_SENDENVIRON,
36 	NULL,
37 	NULL,
38 	cmd_attach_session_exec
39 };
40 
41 enum cmd_retval
42 cmd_attach_session(struct cmd_q *cmdq, const char* tflag, int dflag, int rflag)
43 {
44 	struct session	*s;
45 	struct client	*c;
46 	const char	*update;
47 	char		*cause;
48 	u_int		 i;
49 
50 	if (RB_EMPTY(&sessions)) {
51 		cmdq_error(cmdq, "no sessions");
52 		return (CMD_RETURN_ERROR);
53 	}
54 
55 	if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL)
56 		return (CMD_RETURN_ERROR);
57 
58 	if (cmdq->client == NULL)
59 		return (CMD_RETURN_NORMAL);
60 
61 	if (cmdq->client->session != NULL) {
62 		if (dflag) {
63 			/*
64 			 * Can't use server_write_session in case attaching to
65 			 * the same session as currently attached to.
66 			 */
67 			for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
68 				c = ARRAY_ITEM(&clients, i);
69 				if (c == NULL || c->session != s)
70 					continue;
71 				if (c == cmdq->client)
72 					continue;
73 				server_write_client(c, MSG_DETACH, NULL, 0);
74 			}
75 		}
76 
77 		cmdq->client->session = s;
78 		notify_attached_session_changed(cmdq->client);
79 		session_update_activity(s);
80 		server_redraw_client(cmdq->client);
81 		s->curw->flags &= ~WINLINK_ALERTFLAGS;
82 	} else {
83 		if (server_client_open(cmdq->client, s, &cause) != 0) {
84 			cmdq_error(cmdq, "open terminal failed: %s", cause);
85 			free(cause);
86 			return (CMD_RETURN_ERROR);
87 		}
88 
89 		if (rflag)
90 			cmdq->client->flags |= CLIENT_READONLY;
91 
92 		if (dflag)
93 			server_write_session(s, MSG_DETACH, NULL, 0);
94 
95 		update = options_get_string(&s->options, "update-environment");
96 		environ_update(update, &cmdq->client->environ, &s->environ);
97 
98 		cmdq->client->session = s;
99 		notify_attached_session_changed(cmdq->client);
100 		session_update_activity(s);
101 		server_redraw_client(cmdq->client);
102 		s->curw->flags &= ~WINLINK_ALERTFLAGS;
103 
104 		server_write_ready(cmdq->client);
105 		cmdq->client_exit = 0;
106 	}
107 	recalculate_sizes();
108 	server_update_socket();
109 
110 	return (CMD_RETURN_NORMAL);
111 }
112 
113 enum cmd_retval
114 cmd_attach_session_exec(struct cmd *self, struct cmd_q *cmdq)
115 {
116 	struct args	*args = self->args;
117 
118 	return (cmd_attach_session(cmdq, args_get(args, 't'),
119 	    args_has(args, 'd'), args_has(args, 'r')));
120 }
121