1/* $OpenBSD$ */
2
3/*
4 * Copyright (c) 2007 Nicholas Marriott <[email protected]>
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
33static enum cmd_retval cmd_attach_session_exec(struct cmd *,
34 struct cmdq_item *);
35
36const struct cmd_entry cmd_attach_session_entry = {
37 .name = "attach-session",
38 .alias = "attach",
39
40 .args = { "c:dErt:x", 0, 0 },
41 .usage = "[-dErx] [-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
49enum cmd_retval
50cmd_attach_session(struct cmdq_item *item, const char *tflag, int dflag,
51 int xflag, 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 enum msgtype msgtype;
62
63 if (RB_EMPTY(&sessions)) {
64 cmdq_error(item, "no sessions");
65 return (CMD_RETURN_ERROR);
66 }
67
68 if (c == NULL)
69 return (CMD_RETURN_NORMAL);
70 if (server_client_check_nested(c)) {
71 cmdq_error(item, "sessions should be nested with care, "
72 "unset $TMUX to force");
73 return (CMD_RETURN_ERROR);
74 }
75
76 if (tflag != NULL && tflag[strcspn(tflag, ":.")] != '\0') {
77 type = CMD_FIND_PANE;
78 flags = 0;
79 } else {
80 type = CMD_FIND_SESSION;
81 flags = CMD_FIND_PREFER_UNATTACHED;
82 }
83 if (cmd_find_target(&item->target, item, tflag, type, flags) != 0)
84 return (CMD_RETURN_ERROR);
85 s = item->target.s;
86 wl = item->target.wl;
87 wp = item->target.wp;
88
89 if (wl != NULL) {
90 if (wp != NULL)
91 window_set_active_pane(wp->window, wp, 1);
92 session_set_current(s, wl);
93 if (wp != NULL)
94 cmd_find_from_winlink_pane(current, wl, wp, 0);
95 else
96 cmd_find_from_winlink(current, wl, 0);
97 }
98
99 if (cflag != NULL) {
100 free((void *)s->cwd);
101 s->cwd = format_single(item, cflag, c, s, wl, wp);
102 }
103
104 c->last_session = c->session;
105 if (c->session != NULL) {
106 if (dflag || xflag) {
107 if (xflag)
108 msgtype = MSG_DETACHKILL;
109 else
110 msgtype = MSG_DETACH;
111 TAILQ_FOREACH(c_loop, &clients, entry) {
112 if (c_loop->session != s || c == c_loop)
113 continue;
114 server_client_detach(c_loop, msgtype);
115 }
116 }
117 if (!Eflag)
118 environ_update(s->options, c->environ, s->environ);
119
120 c->session = s;
121 if (~item->shared->flags & CMDQ_SHARED_REPEAT)
122 server_client_set_key_table(c, NULL);
123 tty_update_client_offset(c);
124 status_timer_start(c);
125 notify_client("client-session-changed", c);
126 session_update_activity(s, NULL);
127 gettimeofday(&s->last_attached_time, NULL);
128 server_redraw_client(c);
129 s->curw->flags &= ~WINLINK_ALERTFLAGS;
130 s->curw->window->latest = c;
131 } else {
132 if (server_client_open(c, &cause) != 0) {
133 cmdq_error(item, "open terminal failed: %s", cause);
134 free(cause);
135 return (CMD_RETURN_ERROR);
136 }
137 if (rflag)
138 c->flags |= CLIENT_READONLY;
139
140 if (dflag || xflag) {
141 if (xflag)
142 msgtype = MSG_DETACHKILL;
143 else
144 msgtype = MSG_DETACH;
145 TAILQ_FOREACH(c_loop, &clients, entry) {
146 if (c_loop->session != s || c == c_loop)
147 continue;
148 server_client_detach(c_loop, msgtype);
149 }
150 }
151 if (!Eflag)
152 environ_update(s->options, c->environ, s->environ);
153
154 c->session = s;
155 server_client_set_key_table(c, NULL);
156 tty_update_client_offset(c);
157 status_timer_start(c);
158 notify_client("client-session-changed", c);
159 session_update_activity(s, NULL);
160 gettimeofday(&s->last_attached_time, NULL);
161 server_redraw_client(c);
162 s->curw->flags &= ~WINLINK_ALERTFLAGS;
163 s->curw->window->latest = c;
164
165 if (~c->flags & CLIENT_CONTROL)
166 proc_send(c->peer, MSG_READY, -1, NULL, 0);
167 notify_client("client-attached", c);
168 c->flags |= CLIENT_ATTACHED;
169 }
170 recalculate_sizes();
171 alerts_check_session(s);
172 server_update_socket();
173
174 return (CMD_RETURN_NORMAL);
175}
176
177static enum cmd_retval
178cmd_attach_session_exec(struct cmd *self, struct cmdq_item *item)
179{
180 struct args *args = self->args;
181
182 return (cmd_attach_session(item, args_get(args, 't'),
183 args_has(args, 'd'), args_has(args, 'x'), args_has(args, 'r'),
184 args_get(args, 'c'), args_has(args, 'E')));
185}
186