1 /* $OpenBSD: cmd-attach-session.c,v 1.55 2015/12/13 14:32:38 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|CMD_SESSION_T|CMD_PANE_T|CMD_PREFERUNATTACHED, 40 cmd_attach_session_exec 41 }; 42 43 enum cmd_retval 44 cmd_attach_session(struct cmd_q *cmdq, int dflag, int rflag, const char *cflag, 45 int Eflag) 46 { 47 struct session *s = cmdq->state.tflag.s; 48 struct client *c = cmdq->client, *c_loop; 49 struct winlink *wl = cmdq->state.tflag.wl; 50 struct window_pane *wp = cmdq->state.tflag.wp; 51 const char *update; 52 char *cause, *cwd; 53 struct format_tree *ft; 54 55 if (RB_EMPTY(&sessions)) { 56 cmdq_error(cmdq, "no sessions"); 57 return (CMD_RETURN_ERROR); 58 } 59 60 if (c == NULL) 61 return (CMD_RETURN_NORMAL); 62 if (server_client_check_nested(c)) { 63 cmdq_error(cmdq, "sessions should be nested with care, " 64 "unset $TMUX to force"); 65 return (CMD_RETURN_ERROR); 66 } 67 68 if (wl != NULL) { 69 if (wp != NULL) 70 window_set_active_pane(wp->window, wp); 71 session_set_current(s, wl); 72 } 73 74 if (cflag != NULL) { 75 ft = format_create(cmdq, 0); 76 format_defaults(ft, c, s, wl, wp); 77 cwd = format_expand(ft, cflag); 78 format_free(ft); 79 80 free((void *)s->cwd); 81 s->cwd = cwd; 82 } 83 84 if (c->session != NULL) { 85 if (dflag) { 86 TAILQ_FOREACH(c_loop, &clients, entry) { 87 if (c_loop->session != s || c == c_loop) 88 continue; 89 server_client_detach(c, MSG_DETACH); 90 } 91 } 92 93 if (!Eflag) { 94 update = options_get_string(s->options, 95 "update-environment"); 96 environ_update(update, c->environ, s->environ); 97 } 98 99 c->session = s; 100 server_client_set_key_table(c, NULL); 101 status_timer_start(c); 102 notify_attached_session_changed(c); 103 session_update_activity(s, NULL); 104 gettimeofday(&s->last_attached_time, NULL); 105 server_redraw_client(c); 106 s->curw->flags &= ~WINLINK_ALERTFLAGS; 107 } else { 108 if (server_client_open(c, &cause) != 0) { 109 cmdq_error(cmdq, "open terminal failed: %s", cause); 110 free(cause); 111 return (CMD_RETURN_ERROR); 112 } 113 114 if (rflag) 115 c->flags |= CLIENT_READONLY; 116 117 if (dflag) { 118 TAILQ_FOREACH(c_loop, &clients, entry) { 119 if (c_loop->session != s || c == c_loop) 120 continue; 121 server_client_detach(c_loop, MSG_DETACH); 122 } 123 } 124 125 if (!Eflag) { 126 update = options_get_string(s->options, 127 "update-environment"); 128 environ_update(update, c->environ, s->environ); 129 } 130 131 c->session = s; 132 server_client_set_key_table(c, NULL); 133 status_timer_start(c); 134 notify_attached_session_changed(c); 135 session_update_activity(s, NULL); 136 gettimeofday(&s->last_attached_time, NULL); 137 server_redraw_client(c); 138 s->curw->flags &= ~WINLINK_ALERTFLAGS; 139 140 if (~c->flags & CLIENT_CONTROL) 141 proc_send(c->peer, MSG_READY, -1, NULL, 0); 142 hooks_run(c->session->hooks, "client-attached", c); 143 cmdq->client_exit = 0; 144 } 145 recalculate_sizes(); 146 alerts_check_session(s); 147 server_update_socket(); 148 149 return (CMD_RETURN_NORMAL); 150 } 151 152 enum cmd_retval 153 cmd_attach_session_exec(struct cmd *self, struct cmd_q *cmdq) 154 { 155 struct args *args = self->args; 156 157 return (cmd_attach_session(cmdq, args_has(args, 'd'), 158 args_has(args, 'r'), args_get(args, 'c'), args_has(args, 'E'))); 159 } 160