1 /* $OpenBSD: cmd-attach-session.c,v 1.49 2015/11/05 23:32:21 nicm Exp $ */ 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 <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 enum cmd_retval cmd_attach_session_exec(struct cmd *, struct cmd_q *); 34 35 const struct cmd_entry cmd_attach_session_entry = { 36 "attach-session", "attach", 37 "c:dErt:", 0, 0, 38 "[-dEr] [-c working-directory] " CMD_TARGET_SESSION_USAGE, 39 CMD_STARTSERVER, 40 cmd_attach_session_exec 41 }; 42 43 enum cmd_retval 44 cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag, 45 const char *cflag, int Eflag) 46 { 47 struct session *s; 48 struct client *c = cmdq->client, *c_loop; 49 struct winlink *wl = NULL; 50 struct window *w = NULL; 51 struct window_pane *wp = NULL; 52 const char *update; 53 char *cause; 54 struct format_tree *ft; 55 char *cwd; 56 57 if (RB_EMPTY(&sessions)) { 58 cmdq_error(cmdq, "no sessions"); 59 return (CMD_RETURN_ERROR); 60 } 61 62 if (tflag == NULL) { 63 if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) 64 return (CMD_RETURN_ERROR); 65 } else if (tflag[strcspn(tflag, ":.")] != '\0') { 66 if ((wl = cmd_find_pane(cmdq, tflag, &s, &wp)) == NULL) 67 return (CMD_RETURN_ERROR); 68 } else { 69 if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) 70 return (CMD_RETURN_ERROR); 71 w = window_find_by_id_str(tflag); 72 if (w == NULL) { 73 wp = window_pane_find_by_id_str(tflag); 74 if (wp != NULL) 75 w = wp->window; 76 } 77 if (w != NULL) 78 wl = winlink_find_by_window(&s->windows, w); 79 } 80 81 if (c == NULL) 82 return (CMD_RETURN_NORMAL); 83 if (server_client_check_nested(c)) { 84 cmdq_error(cmdq, "sessions should be nested with care, " 85 "unset $TMUX to force"); 86 return (CMD_RETURN_ERROR); 87 } 88 89 if (wl != NULL) { 90 if (wp != NULL) 91 window_set_active_pane(wp->window, wp); 92 session_set_current(s, wl); 93 } 94 95 if (cflag != NULL) { 96 ft = format_create(); 97 format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, 98 NULL, NULL); 99 cwd = format_expand(ft, cflag); 100 format_free(ft); 101 102 free((void *)s->cwd); 103 s->cwd = cwd; 104 } 105 106 if (c->session != NULL) { 107 if (dflag) { 108 TAILQ_FOREACH(c_loop, &clients, entry) { 109 if (c_loop->session != s || c == c_loop) 110 continue; 111 proc_send_s(c_loop->peer, MSG_DETACH, s->name); 112 } 113 } 114 115 if (!Eflag) { 116 update = options_get_string(s->options, 117 "update-environment"); 118 environ_update(update, c->environ, s->environ); 119 } 120 121 c->session = s; 122 status_timer_start(c); 123 notify_attached_session_changed(c); 124 session_update_activity(s, NULL); 125 gettimeofday(&s->last_attached_time, NULL); 126 server_redraw_client(c); 127 s->curw->flags &= ~WINLINK_ALERTFLAGS; 128 } else { 129 if (server_client_open(c, &cause) != 0) { 130 cmdq_error(cmdq, "open terminal failed: %s", cause); 131 free(cause); 132 return (CMD_RETURN_ERROR); 133 } 134 135 if (rflag) 136 c->flags |= CLIENT_READONLY; 137 138 if (dflag) { 139 TAILQ_FOREACH(c_loop, &clients, entry) { 140 if (c_loop->session != s || c == c_loop) 141 continue; 142 proc_send_s(c_loop->peer, MSG_DETACH, s->name); 143 } 144 } 145 146 if (!Eflag) { 147 update = options_get_string(s->options, 148 "update-environment"); 149 environ_update(update, c->environ, s->environ); 150 } 151 152 c->session = s; 153 status_timer_start(c); 154 notify_attached_session_changed(c); 155 session_update_activity(s, NULL); 156 gettimeofday(&s->last_attached_time, NULL); 157 server_redraw_client(c); 158 s->curw->flags &= ~WINLINK_ALERTFLAGS; 159 160 if (~c->flags & CLIENT_CONTROL) 161 proc_send(c->peer, MSG_READY, -1, NULL, 0); 162 cmdq->client_exit = 0; 163 } 164 recalculate_sizes(); 165 server_update_socket(); 166 167 return (CMD_RETURN_NORMAL); 168 } 169 170 enum cmd_retval 171 cmd_attach_session_exec(struct cmd *self, struct cmd_q *cmdq) 172 { 173 struct args *args = self->args; 174 175 return (cmd_attach_session(cmdq, args_get(args, 't'), 176 args_has(args, 'd'), args_has(args, 'r'), args_get(args, 'c'), 177 args_has(args, 'E'))); 178 } 179