4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
24 * Attach existing session to the current terminal.
27 int cmd_attach_session_exec(struct cmd
*, struct cmd_ctx
*);
29 const struct cmd_entry cmd_attach_session_entry
= {
30 "attach-session", "attach",
32 "[-dr] " CMD_TARGET_SESSION_USAGE
,
33 CMD_CANTNEST
|CMD_STARTSERVER
|CMD_SENDENVIRON
,
36 cmd_attach_session_exec
40 cmd_attach_session_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
42 struct args
*args
= self
->args
;
46 char *overrides
, *cause
;
49 if (RB_EMPTY(&sessions
)) {
50 ctx
->error(ctx
, "no sessions");
54 if ((s
= cmd_find_session(ctx
, args_get(args
, 't'))) == NULL
)
57 if (ctx
->cmdclient
== NULL
&& ctx
->curclient
== NULL
)
60 if (ctx
->cmdclient
== NULL
) {
61 if (args_has(self
->args
, 'd')) {
63 * Can't use server_write_session in case attaching to
64 * the same session as currently attached to.
66 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
67 c
= ARRAY_ITEM(&clients
, i
);
68 if (c
== NULL
|| c
->session
!= s
)
70 if (c
== ctx
->curclient
)
72 server_write_client(c
, MSG_DETACH
, NULL
, 0);
76 ctx
->curclient
->session
= s
;
77 session_update_activity(s
);
78 server_redraw_client(ctx
->curclient
);
80 if (!(ctx
->cmdclient
->flags
& CLIENT_TERMINAL
)) {
81 ctx
->error(ctx
, "not a terminal");
86 options_get_string(&s
->options
, "terminal-overrides");
87 if (tty_open(&ctx
->cmdclient
->tty
, overrides
, &cause
) != 0) {
88 ctx
->error(ctx
, "terminal open failed: %s", cause
);
93 if (args_has(self
->args
, 'r'))
94 ctx
->cmdclient
->flags
|= CLIENT_READONLY
;
96 if (args_has(self
->args
, 'd'))
97 server_write_session(s
, MSG_DETACH
, NULL
, 0);
99 ctx
->cmdclient
->session
= s
;
100 session_update_activity(s
);
101 server_write_client(ctx
->cmdclient
, MSG_READY
, NULL
, 0);
103 update
= options_get_string(&s
->options
, "update-environment");
104 environ_update(update
, &ctx
->cmdclient
->environ
, &s
->environ
);
106 server_redraw_client(ctx
->cmdclient
);
109 server_update_socket();
111 return (1); /* 1 means don't tell command client to exit */