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