1 /* $OpenBSD: cmd-attach-session.c,v 1.65 2016/10/16 22:06:40 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> 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 <errno.h> 22 #include <fcntl.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <unistd.h> 26 27 #include "tmux.h" 28 29 /* 30 * Attach existing session to the current terminal. 31 */ 32 33 static enum cmd_retval cmd_attach_session_exec(struct cmd *, 34 struct cmdq_item *); 35 36 const struct cmd_entry cmd_attach_session_entry = { 37 .name = "attach-session", 38 .alias = "attach", 39 40 .args = { "c:dErt:", 0, 0 }, 41 .usage = "[-dEr] [-c working-directory] " CMD_TARGET_SESSION_USAGE, 42 43 .tflag = CMD_SESSION_WITHPANE, 44 45 .flags = CMD_STARTSERVER, 46 .exec = cmd_attach_session_exec 47 }; 48 49 enum cmd_retval 50 cmd_attach_session(struct cmdq_item *item, int dflag, int rflag, 51 const char *cflag, int Eflag) 52 { 53 struct session *s = item->state.tflag.s; 54 struct client *c = item->client, *c_loop; 55 struct winlink *wl = item->state.tflag.wl; 56 struct window_pane *wp = item->state.tflag.wp; 57 const char *update; 58 char *cause, *cwd; 59 struct format_tree *ft; 60 61 if (RB_EMPTY(&sessions)) { 62 cmdq_error(item, "no sessions"); 63 return (CMD_RETURN_ERROR); 64 } 65 66 if (c == NULL) 67 return (CMD_RETURN_NORMAL); 68 if (server_client_check_nested(c)) { 69 cmdq_error(item, "sessions should be nested with care, " 70 "unset $TMUX to force"); 71 return (CMD_RETURN_ERROR); 72 } 73 74 if (wl != NULL) { 75 if (wp != NULL) 76 window_set_active_pane(wp->window, wp); 77 session_set_current(s, wl); 78 } 79 80 if (cflag != NULL) { 81 ft = format_create(item, 0); 82 format_defaults(ft, c, s, wl, wp); 83 cwd = format_expand(ft, cflag); 84 format_free(ft); 85 86 free((void *)s->cwd); 87 s->cwd = cwd; 88 } 89 90 if (c->session != NULL) { 91 if (dflag) { 92 TAILQ_FOREACH(c_loop, &clients, entry) { 93 if (c_loop->session != s || c == c_loop) 94 continue; 95 server_client_detach(c_loop, MSG_DETACH); 96 } 97 } 98 99 if (!Eflag) { 100 update = options_get_string(s->options, 101 "update-environment"); 102 environ_update(update, c->environ, s->environ); 103 } 104 105 c->session = s; 106 server_client_set_key_table(c, NULL); 107 status_timer_start(c); 108 notify_client("client-session-changed", c); 109 session_update_activity(s, NULL); 110 gettimeofday(&s->last_attached_time, NULL); 111 server_redraw_client(c); 112 s->curw->flags &= ~WINLINK_ALERTFLAGS; 113 } else { 114 if (server_client_open(c, &cause) != 0) { 115 cmdq_error(item, "open terminal failed: %s", cause); 116 free(cause); 117 return (CMD_RETURN_ERROR); 118 } 119 120 if (rflag) 121 c->flags |= CLIENT_READONLY; 122 123 if (dflag) { 124 TAILQ_FOREACH(c_loop, &clients, entry) { 125 if (c_loop->session != s || c == c_loop) 126 continue; 127 server_client_detach(c_loop, MSG_DETACH); 128 } 129 } 130 131 if (!Eflag) { 132 update = options_get_string(s->options, 133 "update-environment"); 134 environ_update(update, c->environ, s->environ); 135 } 136 137 c->session = s; 138 server_client_set_key_table(c, NULL); 139 status_timer_start(c); 140 notify_client("client-session-changed", c); 141 session_update_activity(s, NULL); 142 gettimeofday(&s->last_attached_time, NULL); 143 server_redraw_client(c); 144 s->curw->flags &= ~WINLINK_ALERTFLAGS; 145 146 if (~c->flags & CLIENT_CONTROL) 147 proc_send(c->peer, MSG_READY, -1, NULL, 0); 148 notify_client("client-attached", c); 149 c->flags |= CLIENT_ATTACHED; 150 } 151 recalculate_sizes(); 152 alerts_check_session(s); 153 server_update_socket(); 154 155 return (CMD_RETURN_NORMAL); 156 } 157 158 static enum cmd_retval 159 cmd_attach_session_exec(struct cmd *self, struct cmdq_item *item) 160 { 161 struct args *args = self->args; 162 163 return (cmd_attach_session(item, args_has(args, 'd'), 164 args_has(args, 'r'), args_get(args, 'c'), args_has(args, 'E'))); 165 } 166