Annotation of src/usr.bin/tmux/cmd-attach-session.c, Revision 1.38
1.38 ! nicm 1: /* $OpenBSD: cmd-attach-session.c,v 1.37 2015/06/09 07:07:06 nicm Exp $ */
1.1 nicm 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:
1.27 nicm 21: #include <errno.h>
22: #include <fcntl.h>
1.21 nicm 23: #include <stdlib.h>
1.27 nicm 24: #include <string.h>
25: #include <unistd.h>
1.21 nicm 26:
1.1 nicm 27: #include "tmux.h"
28:
29: /*
30: * Attach existing session to the current terminal.
31: */
32:
1.23 nicm 33: enum cmd_retval cmd_attach_session_exec(struct cmd *, struct cmd_q *);
1.1 nicm 34:
35: const struct cmd_entry cmd_attach_session_entry = {
36: "attach-session", "attach",
1.36 nicm 37: "c:dErt:", 0, 0,
38: "[-dEr] [-c working-directory] " CMD_TARGET_SESSION_USAGE,
1.35 nicm 39: CMD_STARTSERVER,
1.15 nicm 40: cmd_attach_session_exec
1.1 nicm 41: };
42:
1.22 nicm 43: enum cmd_retval
1.27 nicm 44: cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag,
1.36 nicm 45: const char *cflag, int Eflag)
1.1 nicm 46: {
1.27 nicm 47: struct session *s;
1.36 nicm 48: struct client *c = cmdq->client, *c_loop;
1.29 nicm 49: struct winlink *wl = NULL;
50: struct window *w = NULL;
51: struct window_pane *wp = NULL;
1.27 nicm 52: const char *update;
53: char *cause;
54: int fd;
55: struct format_tree *ft;
56: char *cp;
1.1 nicm 57:
1.13 nicm 58: if (RB_EMPTY(&sessions)) {
1.23 nicm 59: cmdq_error(cmdq, "no sessions");
1.22 nicm 60: return (CMD_RETURN_ERROR);
1.1 nicm 61: }
1.15 nicm 62:
1.29 nicm 63: if (tflag == NULL) {
64: if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL)
65: return (CMD_RETURN_ERROR);
66: } else if (tflag[strcspn(tflag, ":.")] != '\0') {
67: if ((wl = cmd_find_pane(cmdq, tflag, &s, &wp)) == NULL)
68: return (CMD_RETURN_ERROR);
69: } else {
70: if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL)
71: return (CMD_RETURN_ERROR);
1.34 nicm 72: w = window_find_by_id_str(tflag);
73: if (w == NULL) {
74: wp = window_pane_find_by_id_str(tflag);
75: if (wp != NULL)
76: w = wp->window;
77: }
1.29 nicm 78: if (w != NULL)
79: wl = winlink_find_by_window(&s->windows, w);
80: }
1.5 nicm 81:
1.36 nicm 82: if (c == NULL)
1.22 nicm 83: return (CMD_RETURN_NORMAL);
1.36 nicm 84: if (server_client_check_nested(c)) {
1.35 nicm 85: cmdq_error(cmdq, "sessions should be nested with care, "
86: "unset $TMUX to force");
87: return (CMD_RETURN_ERROR);
88: }
1.29 nicm 89:
90: if (wl != NULL) {
91: if (wp != NULL)
92: window_set_active_pane(wp->window, wp);
93: session_set_current(s, wl);
94: }
1.1 nicm 95:
1.36 nicm 96: if (c->session != NULL) {
1.24 nicm 97: if (dflag) {
1.11 nicm 98: /*
1.4 nicm 99: * Can't use server_write_session in case attaching to
100: * the same session as currently attached to.
101: */
1.36 nicm 102: TAILQ_FOREACH(c_loop, &clients, entry) {
1.37 nicm 103: if (c_loop->session != s || c == c_loop)
1.4 nicm 104: continue;
1.28 nicm 105: server_write_client(c, MSG_DETACH,
1.36 nicm 106: c_loop->session->name,
107: strlen(c_loop->session->name) + 1);
1.4 nicm 108: }
109: }
1.11 nicm 110:
1.27 nicm 111: if (cflag != NULL) {
112: ft = format_create();
1.32 nicm 113: format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s,
114: NULL, NULL);
1.27 nicm 115: cp = format_expand(ft, cflag);
116: format_free(ft);
117:
118: fd = open(cp, O_RDONLY|O_DIRECTORY);
119: free(cp);
120: if (fd == -1) {
121: cmdq_error(cmdq, "bad working directory: %s",
122: strerror(errno));
123: return (CMD_RETURN_ERROR);
124: }
125: close(s->cwd);
126: s->cwd = fd;
1.38 ! nicm 127: }
! 128:
! 129: if (!Eflag) {
! 130: update = options_get_string(&s->options,
! 131: "update-environment");
! 132: environ_update(update, &c->environ, &s->environ);
1.27 nicm 133: }
134:
1.36 nicm 135: c->session = s;
136: notify_attached_session_changed(c);
1.14 nicm 137: session_update_activity(s);
1.36 nicm 138: server_redraw_client(c);
1.17 nicm 139: s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.4 nicm 140: } else {
1.36 nicm 141: if (server_client_open(c, &cause) != 0) {
1.23 nicm 142: cmdq_error(cmdq, "open terminal failed: %s", cause);
1.21 nicm 143: free(cause);
1.22 nicm 144: return (CMD_RETURN_ERROR);
1.4 nicm 145: }
1.12 nicm 146:
1.27 nicm 147: if (cflag != NULL) {
148: ft = format_create();
1.32 nicm 149: format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s,
150: NULL, NULL);
1.27 nicm 151: cp = format_expand(ft, cflag);
152: format_free(ft);
153:
154: fd = open(cp, O_RDONLY|O_DIRECTORY);
155: free(cp);
156: if (fd == -1) {
157: cmdq_error(cmdq, "bad working directory: %s",
158: strerror(errno));
159: return (CMD_RETURN_ERROR);
160: }
161: close(s->cwd);
162: s->cwd = fd;
163: }
164:
1.24 nicm 165: if (rflag)
1.36 nicm 166: c->flags |= CLIENT_READONLY;
1.4 nicm 167:
1.28 nicm 168: if (dflag) {
169: server_write_session(s, MSG_DETACH, s->name,
170: strlen(s->name) + 1);
171: }
1.4 nicm 172:
1.36 nicm 173: if (!Eflag) {
174: update = options_get_string(&s->options,
175: "update-environment");
176: environ_update(update, &c->environ, &s->environ);
177: }
1.8 nicm 178:
1.36 nicm 179: c->session = s;
180: notify_attached_session_changed(c);
1.23 nicm 181: session_update_activity(s);
1.36 nicm 182: server_redraw_client(c);
1.17 nicm 183: s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.23 nicm 184:
1.36 nicm 185: server_write_ready(c);
1.23 nicm 186: cmdq->client_exit = 0;
1.1 nicm 187: }
188: recalculate_sizes();
1.9 nicm 189: server_update_socket();
1.1 nicm 190:
1.23 nicm 191: return (CMD_RETURN_NORMAL);
1.24 nicm 192: }
193:
194: enum cmd_retval
195: cmd_attach_session_exec(struct cmd *self, struct cmd_q *cmdq)
196: {
197: struct args *args = self->args;
198:
199: return (cmd_attach_session(cmdq, args_get(args, 't'),
1.36 nicm 200: args_has(args, 'd'), args_has(args, 'r'), args_get(args, 'c'),
201: args_has(args, 'E')));
1.1 nicm 202: }