xref: /openbsd-src/usr.bin/tmux/cmd-attach-session.c (revision ac9b4aacc1da35008afea06a5d23c2f2dea9b93e)
1 /* $OpenBSD: cmd-attach-session.c,v 1.22 2012/07/11 07:10:15 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_ctx *);
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_exec(struct cmd *self, struct cmd_ctx *ctx)
43 {
44 	struct args	*args = self->args;
45 	struct session	*s;
46 	struct client	*c;
47 	const char	*update;
48 	char		*cause;
49 	u_int		 i;
50 
51 	if (RB_EMPTY(&sessions)) {
52 		ctx->error(ctx, "no sessions");
53 		return (CMD_RETURN_ERROR);
54 	}
55 
56 	if ((s = cmd_find_session(ctx, args_get(args, 't'), 1)) == NULL)
57 		return (CMD_RETURN_ERROR);
58 
59 	if (ctx->cmdclient == NULL && ctx->curclient == NULL)
60 		return (CMD_RETURN_NORMAL);
61 
62 	if (ctx->cmdclient == NULL) {
63 		if (args_has(self->args, 'd')) {
64 			/*
65 			 * Can't use server_write_session in case attaching to
66 			 * the same session as currently attached to.
67 			 */
68 			for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
69 				c = ARRAY_ITEM(&clients, i);
70 				if (c == NULL || c->session != s)
71 					continue;
72 				if (c == ctx->curclient)
73 					continue;
74 				server_write_client(c, MSG_DETACH, NULL, 0);
75 			}
76 		}
77 
78 		ctx->curclient->session = s;
79 		notify_attached_session_changed(ctx->curclient);
80 		session_update_activity(s);
81 		server_redraw_client(ctx->curclient);
82 		s->curw->flags &= ~WINLINK_ALERTFLAGS;
83 	} else {
84 		if (server_client_open(ctx->cmdclient, s, &cause) != 0) {
85 			ctx->error(ctx, "open terminal failed: %s", cause);
86 			free(cause);
87 			return (CMD_RETURN_ERROR);
88 		}
89 
90 		if (args_has(self->args, 'r'))
91 			ctx->cmdclient->flags |= CLIENT_READONLY;
92 
93 		if (args_has(self->args, 'd'))
94 			server_write_session(s, MSG_DETACH, NULL, 0);
95 
96 		ctx->cmdclient->session = s;
97 		notify_attached_session_changed(ctx->cmdclient);
98 		session_update_activity(s);
99 		server_write_ready(ctx->cmdclient);
100 
101 		update = options_get_string(&s->options, "update-environment");
102 		environ_update(update, &ctx->cmdclient->environ, &s->environ);
103 
104 		server_redraw_client(ctx->cmdclient);
105 		s->curw->flags &= ~WINLINK_ALERTFLAGS;
106 	}
107 	recalculate_sizes();
108 	server_update_socket();
109 
110 	return (CMD_RETURN_ATTACH);
111 }
112