/src/tmux/cmd-attach-session.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD$ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> |
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 | | static enum cmd_retval cmd_attach_session_exec(struct cmd *, |
34 | | struct cmdq_item *); |
35 | | |
36 | | const struct cmd_entry cmd_attach_session_entry = { |
37 | | .name = "attach-session", |
38 | | .alias = "attach", |
39 | | |
40 | | .args = { "c:dEf:rt:x", 0, 0, NULL }, |
41 | | .usage = "[-dErx] [-c working-directory] [-f flags] " |
42 | | CMD_TARGET_SESSION_USAGE, |
43 | | |
44 | | /* -t is special */ |
45 | | |
46 | | .flags = CMD_STARTSERVER|CMD_READONLY, |
47 | | .exec = cmd_attach_session_exec |
48 | | }; |
49 | | |
50 | | enum cmd_retval |
51 | | cmd_attach_session(struct cmdq_item *item, const char *tflag, int dflag, |
52 | | int xflag, int rflag, const char *cflag, int Eflag, const char *fflag) |
53 | 0 | { |
54 | 0 | struct cmd_find_state *current = cmdq_get_current(item); |
55 | 0 | struct cmd_find_state target; |
56 | 0 | enum cmd_find_type type; |
57 | 0 | int flags; |
58 | 0 | struct client *c = cmdq_get_client(item), *c_loop; |
59 | 0 | struct session *s; |
60 | 0 | struct winlink *wl; |
61 | 0 | struct window_pane *wp; |
62 | 0 | char *cwd, *cause; |
63 | 0 | enum msgtype msgtype; |
64 | |
|
65 | 0 | if (RB_EMPTY(&sessions)) { |
66 | 0 | cmdq_error(item, "no sessions"); |
67 | 0 | return (CMD_RETURN_ERROR); |
68 | 0 | } |
69 | | |
70 | 0 | if (c == NULL) |
71 | 0 | return (CMD_RETURN_NORMAL); |
72 | | |
73 | 0 | if (server_client_check_nested(c)) { |
74 | 0 | cmdq_error(item, "sessions should be nested with care, " |
75 | 0 | "unset $TMUX to force"); |
76 | 0 | return (CMD_RETURN_ERROR); |
77 | 0 | } |
78 | | |
79 | 0 | if (tflag != NULL && tflag[strcspn(tflag, ":.")] != '\0') { |
80 | 0 | type = CMD_FIND_PANE; |
81 | 0 | flags = 0; |
82 | 0 | } else { |
83 | 0 | type = CMD_FIND_SESSION; |
84 | 0 | flags = CMD_FIND_PREFER_UNATTACHED; |
85 | 0 | } |
86 | 0 | if (cmd_find_target(&target, item, tflag, type, flags) != 0) |
87 | 0 | return (CMD_RETURN_ERROR); |
88 | 0 | s = target.s; |
89 | 0 | wl = target.wl; |
90 | 0 | wp = target.wp; |
91 | |
|
92 | 0 | if (wl != NULL) { |
93 | 0 | if (wp != NULL) |
94 | 0 | window_set_active_pane(wp->window, wp, 1); |
95 | 0 | session_set_current(s, wl); |
96 | 0 | if (wp != NULL) |
97 | 0 | cmd_find_from_winlink_pane(current, wl, wp, 0); |
98 | 0 | else |
99 | 0 | cmd_find_from_winlink(current, wl, 0); |
100 | 0 | } |
101 | |
|
102 | 0 | if (cflag != NULL) { |
103 | 0 | cwd = format_single(item, cflag, c, s, wl, wp); |
104 | 0 | free((void *)s->cwd); |
105 | 0 | s->cwd = cwd; |
106 | 0 | } |
107 | 0 | if (fflag) |
108 | 0 | server_client_set_flags(c, fflag); |
109 | 0 | if (rflag) |
110 | 0 | c->flags |= (CLIENT_READONLY|CLIENT_IGNORESIZE); |
111 | |
|
112 | 0 | c->last_session = c->session; |
113 | 0 | if (c->session != NULL) { |
114 | 0 | if (dflag || xflag) { |
115 | 0 | if (xflag) |
116 | 0 | msgtype = MSG_DETACHKILL; |
117 | 0 | else |
118 | 0 | msgtype = MSG_DETACH; |
119 | 0 | TAILQ_FOREACH(c_loop, &clients, entry) { |
120 | 0 | if (c_loop->session != s || c == c_loop) |
121 | 0 | continue; |
122 | 0 | server_client_detach(c_loop, msgtype); |
123 | 0 | } |
124 | 0 | } |
125 | 0 | if (!Eflag) |
126 | 0 | environ_update(s->options, c->environ, s->environ); |
127 | |
|
128 | 0 | server_client_set_session(c, s); |
129 | 0 | if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT) |
130 | 0 | server_client_set_key_table(c, NULL); |
131 | 0 | } else { |
132 | 0 | if (server_client_open(c, &cause) != 0) { |
133 | 0 | cmdq_error(item, "open terminal failed: %s", cause); |
134 | 0 | free(cause); |
135 | 0 | return (CMD_RETURN_ERROR); |
136 | 0 | } |
137 | | |
138 | 0 | if (dflag || xflag) { |
139 | 0 | if (xflag) |
140 | 0 | msgtype = MSG_DETACHKILL; |
141 | 0 | else |
142 | 0 | msgtype = MSG_DETACH; |
143 | 0 | TAILQ_FOREACH(c_loop, &clients, entry) { |
144 | 0 | if (c_loop->session != s || c == c_loop) |
145 | 0 | continue; |
146 | 0 | server_client_detach(c_loop, msgtype); |
147 | 0 | } |
148 | 0 | } |
149 | 0 | if (!Eflag) |
150 | 0 | environ_update(s->options, c->environ, s->environ); |
151 | |
|
152 | 0 | server_client_set_session(c, s); |
153 | 0 | server_client_set_key_table(c, NULL); |
154 | |
|
155 | 0 | if (~c->flags & CLIENT_CONTROL) |
156 | 0 | proc_send(c->peer, MSG_READY, -1, NULL, 0); |
157 | 0 | notify_client("client-attached", c); |
158 | 0 | c->flags |= CLIENT_ATTACHED; |
159 | 0 | } |
160 | | |
161 | 0 | if (cfg_finished) |
162 | 0 | cfg_show_causes(s); |
163 | |
|
164 | 0 | return (CMD_RETURN_NORMAL); |
165 | 0 | } |
166 | | |
167 | | static enum cmd_retval |
168 | | cmd_attach_session_exec(struct cmd *self, struct cmdq_item *item) |
169 | 0 | { |
170 | 0 | struct args *args = cmd_get_args(self); |
171 | |
|
172 | 0 | return (cmd_attach_session(item, args_get(args, 't'), |
173 | 0 | args_has(args, 'd'), args_has(args, 'x'), args_has(args, 'r'), |
174 | 0 | args_get(args, 'c'), args_has(args, 'E'), args_get(args, 'f'))); |
175 | 0 | } |