Home | History | Annotate | Line # | Download | only in dist
cmd-attach-session.c revision 1.1
      1  1.1  jmmv /* $Id: cmd-attach-session.c,v 1.1 2011/03/10 09:15:36 jmmv Exp $ */
      2  1.1  jmmv 
      3  1.1  jmmv /*
      4  1.1  jmmv  * Copyright (c) 2007 Nicholas Marriott <nicm (at) users.sourceforge.net>
      5  1.1  jmmv  *
      6  1.1  jmmv  * Permission to use, copy, modify, and distribute this software for any
      7  1.1  jmmv  * purpose with or without fee is hereby granted, provided that the above
      8  1.1  jmmv  * copyright notice and this permission notice appear in all copies.
      9  1.1  jmmv  *
     10  1.1  jmmv  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  1.1  jmmv  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  1.1  jmmv  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  1.1  jmmv  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  1.1  jmmv  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
     15  1.1  jmmv  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     16  1.1  jmmv  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  1.1  jmmv  */
     18  1.1  jmmv 
     19  1.1  jmmv #include <sys/types.h>
     20  1.1  jmmv 
     21  1.1  jmmv #include "tmux.h"
     22  1.1  jmmv 
     23  1.1  jmmv /*
     24  1.1  jmmv  * Attach existing session to the current terminal.
     25  1.1  jmmv  */
     26  1.1  jmmv 
     27  1.1  jmmv int	cmd_attach_session_exec(struct cmd *, struct cmd_ctx *);
     28  1.1  jmmv 
     29  1.1  jmmv const struct cmd_entry cmd_attach_session_entry = {
     30  1.1  jmmv 	"attach-session", "attach",
     31  1.1  jmmv 	"[-dr] " CMD_TARGET_SESSION_USAGE,
     32  1.1  jmmv 	CMD_CANTNEST|CMD_STARTSERVER|CMD_SENDENVIRON, "dr",
     33  1.1  jmmv 	cmd_target_init,
     34  1.1  jmmv 	cmd_target_parse,
     35  1.1  jmmv 	cmd_attach_session_exec,
     36  1.1  jmmv 	cmd_target_free,
     37  1.1  jmmv 	cmd_target_print
     38  1.1  jmmv };
     39  1.1  jmmv 
     40  1.1  jmmv int
     41  1.1  jmmv cmd_attach_session_exec(struct cmd *self, struct cmd_ctx *ctx)
     42  1.1  jmmv {
     43  1.1  jmmv 	struct cmd_target_data	*data = self->data;
     44  1.1  jmmv 	struct session		*s;
     45  1.1  jmmv 	struct client		*c;
     46  1.1  jmmv 	const char		*update;
     47  1.1  jmmv 	char			*overrides, *cause;
     48  1.1  jmmv 	u_int			 i;
     49  1.1  jmmv 
     50  1.1  jmmv 	if (RB_EMPTY(&sessions)) {
     51  1.1  jmmv 		ctx->error(ctx, "no sessions");
     52  1.1  jmmv 		return (-1);
     53  1.1  jmmv 	}
     54  1.1  jmmv 	if ((s = cmd_find_session(ctx, data->target)) == NULL)
     55  1.1  jmmv 		return (-1);
     56  1.1  jmmv 
     57  1.1  jmmv 	if (ctx->cmdclient == NULL && ctx->curclient == NULL)
     58  1.1  jmmv 		return (0);
     59  1.1  jmmv 
     60  1.1  jmmv 	if (ctx->cmdclient == NULL) {
     61  1.1  jmmv 		if (cmd_check_flag(data->chflags, 'd')) {
     62  1.1  jmmv 			/*
     63  1.1  jmmv 			 * Can't use server_write_session in case attaching to
     64  1.1  jmmv 			 * the same session as currently attached to.
     65  1.1  jmmv 			 */
     66  1.1  jmmv 			for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
     67  1.1  jmmv 				c = ARRAY_ITEM(&clients, i);
     68  1.1  jmmv 				if (c == NULL || c->session != s)
     69  1.1  jmmv 					continue;
     70  1.1  jmmv 				if (c == ctx->curclient)
     71  1.1  jmmv 					continue;
     72  1.1  jmmv 				server_write_client(c, MSG_DETACH, NULL, 0);
     73  1.1  jmmv 			}
     74  1.1  jmmv 		}
     75  1.1  jmmv 
     76  1.1  jmmv 		ctx->curclient->session = s;
     77  1.1  jmmv 		server_redraw_client(ctx->curclient);
     78  1.1  jmmv 	} else {
     79  1.1  jmmv 		if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
     80  1.1  jmmv 			ctx->error(ctx, "not a terminal");
     81  1.1  jmmv 			return (-1);
     82  1.1  jmmv 		}
     83  1.1  jmmv 
     84  1.1  jmmv 		overrides =
     85  1.1  jmmv 		    options_get_string(&s->options, "terminal-overrides");
     86  1.1  jmmv 		if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
     87  1.1  jmmv 			ctx->error(ctx, "terminal open failed: %s", cause);
     88  1.1  jmmv 			xfree(cause);
     89  1.1  jmmv 			return (-1);
     90  1.1  jmmv 		}
     91  1.1  jmmv 
     92  1.1  jmmv 		if (cmd_check_flag(data->chflags, 'r'))
     93  1.1  jmmv 			ctx->cmdclient->flags |= CLIENT_READONLY;
     94  1.1  jmmv 
     95  1.1  jmmv 		if (cmd_check_flag(data->chflags, 'd'))
     96  1.1  jmmv 			server_write_session(s, MSG_DETACH, NULL, 0);
     97  1.1  jmmv 
     98  1.1  jmmv 		ctx->cmdclient->session = s;
     99  1.1  jmmv 		server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
    100  1.1  jmmv 
    101  1.1  jmmv 		update = options_get_string(&s->options, "update-environment");
    102  1.1  jmmv 		environ_update(update, &ctx->cmdclient->environ, &s->environ);
    103  1.1  jmmv 
    104  1.1  jmmv 		server_redraw_client(ctx->cmdclient);
    105  1.1  jmmv 	}
    106  1.1  jmmv 	recalculate_sizes();
    107  1.1  jmmv 	server_update_socket();
    108  1.1  jmmv 
    109  1.1  jmmv 	return (1);	/* 1 means don't tell command client to exit */
    110  1.1  jmmv }
    111