Annotation of src/usr.bin/tmux/cmd-attach-session.c, Revision 1.25
1.25 ! nicm 1: /* $OpenBSD: cmd-attach-session.c,v 1.24 2013/03/24 09:58:40 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.21 nicm 21: #include <stdlib.h>
22:
1.1 nicm 23: #include "tmux.h"
24:
25: /*
26: * Attach existing session to the current terminal.
27: */
28:
1.23 nicm 29: enum cmd_retval cmd_attach_session_exec(struct cmd *, struct cmd_q *);
1.1 nicm 30:
31: const struct cmd_entry cmd_attach_session_entry = {
32: "attach-session", "attach",
1.15 nicm 33: "drt:", 0, 0,
1.12 nicm 34: "[-dr] " CMD_TARGET_SESSION_USAGE,
1.15 nicm 35: CMD_CANTNEST|CMD_STARTSERVER|CMD_SENDENVIRON,
36: NULL,
37: cmd_attach_session_exec
1.1 nicm 38: };
39:
1.22 nicm 40: enum cmd_retval
1.24 nicm 41: cmd_attach_session(struct cmd_q *cmdq, const char* tflag, int dflag, int rflag)
1.1 nicm 42: {
1.15 nicm 43: struct session *s;
44: struct client *c;
45: const char *update;
1.19 nicm 46: char *cause;
1.15 nicm 47: u_int i;
1.1 nicm 48:
1.13 nicm 49: if (RB_EMPTY(&sessions)) {
1.23 nicm 50: cmdq_error(cmdq, "no sessions");
1.22 nicm 51: return (CMD_RETURN_ERROR);
1.1 nicm 52: }
1.15 nicm 53:
1.24 nicm 54: if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL)
1.22 nicm 55: return (CMD_RETURN_ERROR);
1.5 nicm 56:
1.23 nicm 57: if (cmdq->client == NULL)
1.22 nicm 58: return (CMD_RETURN_NORMAL);
1.1 nicm 59:
1.23 nicm 60: if (cmdq->client->session != NULL) {
1.24 nicm 61: if (dflag) {
1.11 nicm 62: /*
1.4 nicm 63: * Can't use server_write_session in case attaching to
64: * the same session as currently attached to.
65: */
66: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
67: c = ARRAY_ITEM(&clients, i);
68: if (c == NULL || c->session != s)
69: continue;
1.23 nicm 70: if (c == cmdq->client)
1.4 nicm 71: continue;
72: server_write_client(c, MSG_DETACH, NULL, 0);
73: }
74: }
1.11 nicm 75:
1.23 nicm 76: cmdq->client->session = s;
77: notify_attached_session_changed(cmdq->client);
1.14 nicm 78: session_update_activity(s);
1.23 nicm 79: server_redraw_client(cmdq->client);
1.17 nicm 80: s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.4 nicm 81: } else {
1.23 nicm 82: if (server_client_open(cmdq->client, s, &cause) != 0) {
83: cmdq_error(cmdq, "open terminal failed: %s", cause);
1.21 nicm 84: free(cause);
1.22 nicm 85: return (CMD_RETURN_ERROR);
1.4 nicm 86: }
1.12 nicm 87:
1.24 nicm 88: if (rflag)
1.23 nicm 89: cmdq->client->flags |= CLIENT_READONLY;
1.4 nicm 90:
1.24 nicm 91: if (dflag)
1.4 nicm 92: server_write_session(s, MSG_DETACH, NULL, 0);
93:
1.8 nicm 94: update = options_get_string(&s->options, "update-environment");
1.23 nicm 95: environ_update(update, &cmdq->client->environ, &s->environ);
1.8 nicm 96:
1.23 nicm 97: cmdq->client->session = s;
98: notify_attached_session_changed(cmdq->client);
99: session_update_activity(s);
100: server_redraw_client(cmdq->client);
1.17 nicm 101: s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.23 nicm 102:
103: server_write_ready(cmdq->client);
104: cmdq->client_exit = 0;
1.1 nicm 105: }
106: recalculate_sizes();
1.9 nicm 107: server_update_socket();
1.1 nicm 108:
1.23 nicm 109: return (CMD_RETURN_NORMAL);
1.24 nicm 110: }
111:
112: enum cmd_retval
113: cmd_attach_session_exec(struct cmd *self, struct cmd_q *cmdq)
114: {
115: struct args *args = self->args;
116:
117: return (cmd_attach_session(cmdq, args_get(args, 't'),
118: args_has(args, 'd'), args_has(args, 'r')));
1.1 nicm 119: }