Annotation of src/usr.bin/tmux/cmd-attach-session.c, Revision 1.28
1.28 ! nicm 1: /* $OpenBSD: cmd-attach-session.c,v 1.27 2013/10/10 12:26:34 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.27 nicm 37: "c:drt:", 0, 0,
38: "[-dr] [-c working-directory] " CMD_TARGET_SESSION_USAGE,
1.26 nicm 39: CMD_CANTNEST|CMD_STARTSERVER,
1.15 nicm 40: NULL,
41: cmd_attach_session_exec
1.1 nicm 42: };
43:
1.22 nicm 44: enum cmd_retval
1.27 nicm 45: cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag,
46: const char *cflag)
1.1 nicm 47: {
1.27 nicm 48: struct session *s;
49: struct client *c;
50: const char *update;
51: char *cause;
52: u_int i;
53: int fd;
54: struct format_tree *ft;
55: char *cp;
1.1 nicm 56:
1.13 nicm 57: if (RB_EMPTY(&sessions)) {
1.23 nicm 58: cmdq_error(cmdq, "no sessions");
1.22 nicm 59: return (CMD_RETURN_ERROR);
1.1 nicm 60: }
1.15 nicm 61:
1.24 nicm 62: if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL)
1.22 nicm 63: return (CMD_RETURN_ERROR);
1.5 nicm 64:
1.23 nicm 65: if (cmdq->client == NULL)
1.22 nicm 66: return (CMD_RETURN_NORMAL);
1.1 nicm 67:
1.23 nicm 68: if (cmdq->client->session != NULL) {
1.24 nicm 69: if (dflag) {
1.11 nicm 70: /*
1.4 nicm 71: * Can't use server_write_session in case attaching to
72: * the same session as currently attached to.
73: */
74: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
75: c = ARRAY_ITEM(&clients, i);
76: if (c == NULL || c->session != s)
77: continue;
1.23 nicm 78: if (c == cmdq->client)
1.4 nicm 79: continue;
1.28 ! nicm 80: server_write_client(c, MSG_DETACH,
! 81: c->session->name,
! 82: strlen(c->session->name) + 1);
1.4 nicm 83: }
84: }
1.11 nicm 85:
1.27 nicm 86: if (cflag != NULL) {
87: ft = format_create();
88: if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
89: format_client(ft, c);
90: format_session(ft, s);
91: format_winlink(ft, s, s->curw);
92: format_window_pane(ft, s->curw->window->active);
93: cp = format_expand(ft, cflag);
94: format_free(ft);
95:
96: fd = open(cp, O_RDONLY|O_DIRECTORY);
97: free(cp);
98: if (fd == -1) {
99: cmdq_error(cmdq, "bad working directory: %s",
100: strerror(errno));
101: return (CMD_RETURN_ERROR);
102: }
103: close(s->cwd);
104: s->cwd = fd;
105: }
106:
1.23 nicm 107: cmdq->client->session = s;
108: notify_attached_session_changed(cmdq->client);
1.14 nicm 109: session_update_activity(s);
1.23 nicm 110: server_redraw_client(cmdq->client);
1.17 nicm 111: s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.4 nicm 112: } else {
1.23 nicm 113: if (server_client_open(cmdq->client, s, &cause) != 0) {
114: cmdq_error(cmdq, "open terminal failed: %s", cause);
1.21 nicm 115: free(cause);
1.22 nicm 116: return (CMD_RETURN_ERROR);
1.4 nicm 117: }
1.12 nicm 118:
1.27 nicm 119: if (cflag != NULL) {
120: ft = format_create();
121: if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
122: format_client(ft, c);
123: format_session(ft, s);
124: format_winlink(ft, s, s->curw);
125: format_window_pane(ft, s->curw->window->active);
126: cp = format_expand(ft, cflag);
127: format_free(ft);
128:
129: fd = open(cp, O_RDONLY|O_DIRECTORY);
130: free(cp);
131: if (fd == -1) {
132: cmdq_error(cmdq, "bad working directory: %s",
133: strerror(errno));
134: return (CMD_RETURN_ERROR);
135: }
136: close(s->cwd);
137: s->cwd = fd;
138: }
139:
1.24 nicm 140: if (rflag)
1.23 nicm 141: cmdq->client->flags |= CLIENT_READONLY;
1.4 nicm 142:
1.28 ! nicm 143: if (dflag) {
! 144: server_write_session(s, MSG_DETACH, s->name,
! 145: strlen(s->name) + 1);
! 146: }
1.4 nicm 147:
1.8 nicm 148: update = options_get_string(&s->options, "update-environment");
1.23 nicm 149: environ_update(update, &cmdq->client->environ, &s->environ);
1.8 nicm 150:
1.23 nicm 151: cmdq->client->session = s;
152: notify_attached_session_changed(cmdq->client);
153: session_update_activity(s);
154: server_redraw_client(cmdq->client);
1.17 nicm 155: s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.23 nicm 156:
157: server_write_ready(cmdq->client);
158: cmdq->client_exit = 0;
1.1 nicm 159: }
160: recalculate_sizes();
1.9 nicm 161: server_update_socket();
1.1 nicm 162:
1.23 nicm 163: return (CMD_RETURN_NORMAL);
1.24 nicm 164: }
165:
166: enum cmd_retval
167: cmd_attach_session_exec(struct cmd *self, struct cmd_q *cmdq)
168: {
169: struct args *args = self->args;
170:
171: return (cmd_attach_session(cmdq, args_get(args, 't'),
1.27 nicm 172: args_has(args, 'd'), args_has(args, 'r'), args_get(args, 'c')));
1.1 nicm 173: }