[BACK]Return to cmd-attach-session.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-attach-session.c, Revision 1.72

1.72    ! nicm        1: /* $OpenBSD: cmd-attach-session.c,v 1.71 2017/04/21 14:01:19 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.61      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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:
1.27      nicm       21: #include <errno.h>
                     22: #include <fcntl.h>
1.21      nicm       23: #include <stdlib.h>
1.27      nicm       24: #include <string.h>
                     25: #include <unistd.h>
1.21      nicm       26:
1.1       nicm       27: #include "tmux.h"
                     28:
                     29: /*
                     30:  * Attach existing session to the current terminal.
                     31:  */
                     32:
1.64      nicm       33: static enum cmd_retval cmd_attach_session_exec(struct cmd *,
                     34:                            struct cmdq_item *);
1.1       nicm       35:
                     36: const struct cmd_entry cmd_attach_session_entry = {
1.56      nicm       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:
1.57      nicm       43:        .tflag = CMD_SESSION_WITHPANE,
                     44:
                     45:        .flags = CMD_STARTSERVER,
1.56      nicm       46:        .exec = cmd_attach_session_exec
1.1       nicm       47: };
                     48:
1.22      nicm       49: enum cmd_retval
1.64      nicm       50: cmd_attach_session(struct cmdq_item *item, int dflag, int rflag,
                     51:     const char *cflag, int Eflag)
1.1       nicm       52: {
1.72    ! nicm       53:        struct cmd_find_state   *current = &item->shared->current;
1.64      nicm       54:        struct session          *s = item->state.tflag.s;
                     55:        struct client           *c = item->client, *c_loop;
                     56:        struct winlink          *wl = item->state.tflag.wl;
                     57:        struct window_pane      *wp = item->state.tflag.wp;
1.70      nicm       58:        char                    *cause;
1.1       nicm       59:
1.13      nicm       60:        if (RB_EMPTY(&sessions)) {
1.64      nicm       61:                cmdq_error(item, "no sessions");
1.22      nicm       62:                return (CMD_RETURN_ERROR);
1.1       nicm       63:        }
1.15      nicm       64:
1.36      nicm       65:        if (c == NULL)
1.22      nicm       66:                return (CMD_RETURN_NORMAL);
1.36      nicm       67:        if (server_client_check_nested(c)) {
1.64      nicm       68:                cmdq_error(item, "sessions should be nested with care, "
1.35      nicm       69:                    "unset $TMUX to force");
                     70:                return (CMD_RETURN_ERROR);
                     71:        }
1.29      nicm       72:
                     73:        if (wl != NULL) {
                     74:                if (wp != NULL)
                     75:                        window_set_active_pane(wp->window, wp);
                     76:                session_set_current(s, wl);
1.72    ! nicm       77:                if (wp != NULL)
        !            78:                        cmd_find_from_winlink_pane(current, wl, wp);
        !            79:                else
        !            80:                        cmd_find_from_winlink(current, wl);
1.29      nicm       81:        }
1.1       nicm       82:
1.42      nicm       83:        if (cflag != NULL) {
1.46      nicm       84:                free((void *)s->cwd);
1.70      nicm       85:                s->cwd = format_single(item, cflag, c, s, wl, wp);
1.42      nicm       86:        }
                     87:
1.36      nicm       88:        if (c->session != NULL) {
1.24      nicm       89:                if (dflag) {
1.36      nicm       90:                        TAILQ_FOREACH(c_loop, &clients, entry) {
1.37      nicm       91:                                if (c_loop->session != s || c == c_loop)
1.4       nicm       92:                                        continue;
1.60      nicm       93:                                server_client_detach(c_loop, MSG_DETACH);
1.4       nicm       94:                        }
                     95:                }
1.66      nicm       96:                if (!Eflag)
                     97:                        environ_update(s->options, c->environ, s->environ);
1.27      nicm       98:
1.36      nicm       99:                c->session = s;
1.71      nicm      100:                if (~item->shared->flags & CMDQ_SHARED_REPEAT)
1.69      nicm      101:                        server_client_set_key_table(c, NULL);
1.39      nicm      102:                status_timer_start(c);
1.65      nicm      103:                notify_client("client-session-changed", c);
1.40      nicm      104:                session_update_activity(s, NULL);
1.41      nicm      105:                gettimeofday(&s->last_attached_time, NULL);
1.36      nicm      106:                server_redraw_client(c);
1.17      nicm      107:                s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.4       nicm      108:        } else {
1.36      nicm      109:                if (server_client_open(c, &cause) != 0) {
1.64      nicm      110:                        cmdq_error(item, "open terminal failed: %s", cause);
1.21      nicm      111:                        free(cause);
1.22      nicm      112:                        return (CMD_RETURN_ERROR);
1.27      nicm      113:                }
1.24      nicm      114:                if (rflag)
1.36      nicm      115:                        c->flags |= CLIENT_READONLY;
1.4       nicm      116:
1.28      nicm      117:                if (dflag) {
1.43      nicm      118:                        TAILQ_FOREACH(c_loop, &clients, entry) {
                    119:                                if (c_loop->session != s || c == c_loop)
                    120:                                        continue;
1.51      nicm      121:                                server_client_detach(c_loop, MSG_DETACH);
1.43      nicm      122:                        }
1.28      nicm      123:                }
1.66      nicm      124:                if (!Eflag)
                    125:                        environ_update(s->options, c->environ, s->environ);
1.8       nicm      126:
1.36      nicm      127:                c->session = s;
1.54      nicm      128:                server_client_set_key_table(c, NULL);
1.39      nicm      129:                status_timer_start(c);
1.65      nicm      130:                notify_client("client-session-changed", c);
1.40      nicm      131:                session_update_activity(s, NULL);
1.41      nicm      132:                gettimeofday(&s->last_attached_time, NULL);
1.36      nicm      133:                server_redraw_client(c);
1.17      nicm      134:                s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.23      nicm      135:
1.43      nicm      136:                if (~c->flags & CLIENT_CONTROL)
                    137:                        proc_send(c->peer, MSG_READY, -1, NULL, 0);
1.65      nicm      138:                notify_client("client-attached", c);
1.63      nicm      139:                c->flags |= CLIENT_ATTACHED;
1.1       nicm      140:        }
                    141:        recalculate_sizes();
1.50      nicm      142:        alerts_check_session(s);
1.9       nicm      143:        server_update_socket();
1.1       nicm      144:
1.23      nicm      145:        return (CMD_RETURN_NORMAL);
1.24      nicm      146: }
                    147:
1.62      nicm      148: static enum cmd_retval
1.64      nicm      149: cmd_attach_session_exec(struct cmd *self, struct cmdq_item *item)
1.24      nicm      150: {
                    151:        struct args     *args = self->args;
                    152:
1.64      nicm      153:        return (cmd_attach_session(item, args_has(args, 'd'),
1.55      nicm      154:            args_has(args, 'r'), args_get(args, 'c'), args_has(args, 'E')));
1.1       nicm      155: }