Annotation of src/usr.bin/tmux/cmd-attach-session.c, Revision 1.18
1.18 ! nicm 1: /* $OpenBSD: cmd-attach-session.c,v 1.17 2012/01/21 06:13:16 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:
21: #include "tmux.h"
22:
23: /*
24: * Attach existing session to the current terminal.
25: */
26:
27: int cmd_attach_session_exec(struct cmd *, struct cmd_ctx *);
28:
29: const struct cmd_entry cmd_attach_session_entry = {
30: "attach-session", "attach",
1.15 nicm 31: "drt:", 0, 0,
1.12 nicm 32: "[-dr] " CMD_TARGET_SESSION_USAGE,
1.15 nicm 33: CMD_CANTNEST|CMD_STARTSERVER|CMD_SENDENVIRON,
34: NULL,
35: NULL,
36: cmd_attach_session_exec
1.1 nicm 37: };
38:
39: int
40: cmd_attach_session_exec(struct cmd *self, struct cmd_ctx *ctx)
41: {
1.15 nicm 42: struct args *args = self->args;
43: struct session *s;
44: struct client *c;
45: const char *update;
46: char *overrides, *cause;
47: u_int i;
1.1 nicm 48:
1.13 nicm 49: if (RB_EMPTY(&sessions)) {
1.1 nicm 50: ctx->error(ctx, "no sessions");
51: return (-1);
52: }
1.15 nicm 53:
1.16 nicm 54: if ((s = cmd_find_session(ctx, args_get(args, 't'), 1)) == NULL)
1.1 nicm 55: return (-1);
1.5 nicm 56:
57: if (ctx->cmdclient == NULL && ctx->curclient == NULL)
58: return (0);
1.1 nicm 59:
1.4 nicm 60: if (ctx->cmdclient == NULL) {
1.15 nicm 61: if (args_has(self->args, 'd')) {
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;
70: if (c == ctx->curclient)
71: continue;
72: server_write_client(c, MSG_DETACH, NULL, 0);
73: }
74: }
1.11 nicm 75:
1.4 nicm 76: ctx->curclient->session = s;
1.18 ! nicm 77: notify_attached_session_changed(ctx->curclient);
1.14 nicm 78: session_update_activity(s);
1.4 nicm 79: server_redraw_client(ctx->curclient);
1.17 nicm 80: s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.4 nicm 81: } else {
82: if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
83: ctx->error(ctx, "not a terminal");
84: return (-1);
85: }
86:
1.7 nicm 87: overrides =
88: options_get_string(&s->options, "terminal-overrides");
89: if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
1.4 nicm 90: ctx->error(ctx, "terminal open failed: %s", cause);
91: xfree(cause);
92: return (-1);
93: }
1.12 nicm 94:
1.15 nicm 95: if (args_has(self->args, 'r'))
1.12 nicm 96: ctx->cmdclient->flags |= CLIENT_READONLY;
1.4 nicm 97:
1.15 nicm 98: if (args_has(self->args, 'd'))
1.4 nicm 99: server_write_session(s, MSG_DETACH, NULL, 0);
100:
101: ctx->cmdclient->session = s;
1.18 ! nicm 102: notify_attached_session_changed(ctx->cmdclient);
1.14 nicm 103: session_update_activity(s);
1.4 nicm 104: server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
1.8 nicm 105:
106: update = options_get_string(&s->options, "update-environment");
107: environ_update(update, &ctx->cmdclient->environ, &s->environ);
108:
1.4 nicm 109: server_redraw_client(ctx->cmdclient);
1.17 nicm 110: s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.1 nicm 111: }
112: recalculate_sizes();
1.9 nicm 113: server_update_socket();
1.1 nicm 114:
1.4 nicm 115: return (1); /* 1 means don't tell command client to exit */
1.1 nicm 116: }