Annotation of src/usr.bin/tmux/cmd-attach-session.c, Revision 1.7
1.7 ! nicm 1: /* $OpenBSD: cmd-attach-session.c,v 1.6 2009/07/26 12:58:44 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",
31: "[-d] " CMD_TARGET_SESSION_USAGE,
1.3 nicm 32: CMD_CANTNEST|CMD_STARTSERVER, CMD_CHFLAG('d'),
1.1 nicm 33: cmd_target_init,
34: cmd_target_parse,
35: cmd_attach_session_exec,
36: cmd_target_free,
37: cmd_target_print
38: };
39:
40: int
41: cmd_attach_session_exec(struct cmd *self, struct cmd_ctx *ctx)
42: {
43: struct cmd_target_data *data = self->data;
44: struct session *s;
1.4 nicm 45: struct client *c;
1.7 ! nicm 46: char *overrides, *cause;
1.4 nicm 47: u_int i;
1.1 nicm 48:
49: if (ARRAY_LENGTH(&sessions) == 0) {
50: ctx->error(ctx, "no sessions");
51: return (-1);
52: }
53: if ((s = cmd_find_session(ctx, data->target)) == NULL)
54: return (-1);
1.5 nicm 55:
56: if (ctx->cmdclient == NULL && ctx->curclient == NULL)
57: return (0);
1.1 nicm 58:
1.4 nicm 59: if (ctx->cmdclient == NULL) {
60: if (data->chflags & CMD_CHFLAG('d')) {
61: /*
62: * Can't use server_write_session in case attaching to
63: * the same session as currently attached to.
64: */
65: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
66: c = ARRAY_ITEM(&clients, i);
67: if (c == NULL || c->session != s)
68: continue;
69: if (c == ctx->curclient)
70: continue;
71: server_write_client(c, MSG_DETACH, NULL, 0);
72: }
73: }
74:
75: ctx->curclient->session = s;
76: server_redraw_client(ctx->curclient);
77: } else {
78: if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
79: ctx->error(ctx, "not a terminal");
80: return (-1);
81: }
82:
1.7 ! nicm 83: overrides =
! 84: options_get_string(&s->options, "terminal-overrides");
! 85: if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
1.4 nicm 86: ctx->error(ctx, "terminal open failed: %s", cause);
87: xfree(cause);
88: return (-1);
89: }
90:
91: if (data->chflags & CMD_CHFLAG('d'))
92: server_write_session(s, MSG_DETACH, NULL, 0);
93:
94: ctx->cmdclient->session = s;
95: server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
96: server_redraw_client(ctx->cmdclient);
1.1 nicm 97: }
98: recalculate_sizes();
99:
1.4 nicm 100: return (1); /* 1 means don't tell command client to exit */
1.1 nicm 101: }