[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.84

1.84    ! nicm        1: /* $OpenBSD: cmd-attach-session.c,v 1.83 2020/04/13 14:46:04 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:
1.84    ! nicm       40:        .args = { "c:dEf:rt:x", 0, 0 },
        !            41:        .usage = "[-dErx] [-c working-directory] [-f flags] "
        !            42:                 CMD_TARGET_SESSION_USAGE,
1.56      nicm       43:
1.73      nicm       44:        /* -t is special */
1.57      nicm       45:
                     46:        .flags = CMD_STARTSERVER,
1.56      nicm       47:        .exec = cmd_attach_session_exec
1.1       nicm       48: };
                     49:
1.22      nicm       50: enum cmd_retval
1.73      nicm       51: cmd_attach_session(struct cmdq_item *item, const char *tflag, int dflag,
1.84    ! nicm       52:     int xflag, int rflag, const char *cflag, int Eflag, const char *fflag)
1.1       nicm       53: {
1.83      nicm       54:        struct cmd_find_state   *current = cmdq_get_current(item);
1.81      nicm       55:        struct cmd_find_state    target;
1.73      nicm       56:        enum cmd_find_type       type;
                     57:        int                      flags;
1.81      nicm       58:        struct client           *c = cmdq_get_client(item), *c_loop;
1.73      nicm       59:        struct session          *s;
                     60:        struct winlink          *wl;
                     61:        struct window_pane      *wp;
1.70      nicm       62:        char                    *cause;
1.78      nicm       63:        enum msgtype             msgtype;
1.1       nicm       64:
1.13      nicm       65:        if (RB_EMPTY(&sessions)) {
1.64      nicm       66:                cmdq_error(item, "no sessions");
1.22      nicm       67:                return (CMD_RETURN_ERROR);
1.1       nicm       68:        }
1.15      nicm       69:
1.36      nicm       70:        if (c == NULL)
1.22      nicm       71:                return (CMD_RETURN_NORMAL);
1.36      nicm       72:        if (server_client_check_nested(c)) {
1.64      nicm       73:                cmdq_error(item, "sessions should be nested with care, "
1.35      nicm       74:                    "unset $TMUX to force");
                     75:                return (CMD_RETURN_ERROR);
                     76:        }
1.29      nicm       77:
1.73      nicm       78:        if (tflag != NULL && tflag[strcspn(tflag, ":.")] != '\0') {
                     79:                type = CMD_FIND_PANE;
                     80:                flags = 0;
                     81:        } else {
                     82:                type = CMD_FIND_SESSION;
                     83:                flags = CMD_FIND_PREFER_UNATTACHED;
                     84:        }
1.81      nicm       85:        if (cmd_find_target(&target, item, tflag, type, flags) != 0)
1.73      nicm       86:                return (CMD_RETURN_ERROR);
1.81      nicm       87:        s = target.s;
                     88:        wl = target.wl;
                     89:        wp = target.wp;
1.73      nicm       90:
1.29      nicm       91:        if (wl != NULL) {
                     92:                if (wp != NULL)
1.77      nicm       93:                        window_set_active_pane(wp->window, wp, 1);
1.29      nicm       94:                session_set_current(s, wl);
1.72      nicm       95:                if (wp != NULL)
1.74      nicm       96:                        cmd_find_from_winlink_pane(current, wl, wp, 0);
1.72      nicm       97:                else
1.74      nicm       98:                        cmd_find_from_winlink(current, wl, 0);
1.29      nicm       99:        }
1.1       nicm      100:
1.42      nicm      101:        if (cflag != NULL) {
1.46      nicm      102:                free((void *)s->cwd);
1.70      nicm      103:                s->cwd = format_single(item, cflag, c, s, wl, wp);
1.42      nicm      104:        }
1.84    ! nicm      105:        if (fflag)
        !           106:                server_client_set_flags(c, fflag);
        !           107:        if (rflag)
        !           108:                c->flags |= (CLIENT_READONLY|CLIENT_IGNORESIZE);
1.42      nicm      109:
1.75      nicm      110:        c->last_session = c->session;
1.36      nicm      111:        if (c->session != NULL) {
1.78      nicm      112:                if (dflag || xflag) {
                    113:                        if (xflag)
                    114:                                msgtype = MSG_DETACHKILL;
                    115:                        else
                    116:                                msgtype = MSG_DETACH;
1.36      nicm      117:                        TAILQ_FOREACH(c_loop, &clients, entry) {
1.37      nicm      118:                                if (c_loop->session != s || c == c_loop)
1.4       nicm      119:                                        continue;
1.78      nicm      120:                                server_client_detach(c_loop, msgtype);
1.4       nicm      121:                        }
                    122:                }
1.66      nicm      123:                if (!Eflag)
                    124:                        environ_update(s->options, c->environ, s->environ);
1.27      nicm      125:
1.36      nicm      126:                c->session = s;
1.83      nicm      127:                if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
1.69      nicm      128:                        server_client_set_key_table(c, NULL);
1.76      nicm      129:                tty_update_client_offset(c);
1.39      nicm      130:                status_timer_start(c);
1.65      nicm      131:                notify_client("client-session-changed", c);
1.40      nicm      132:                session_update_activity(s, NULL);
1.41      nicm      133:                gettimeofday(&s->last_attached_time, NULL);
1.36      nicm      134:                server_redraw_client(c);
1.17      nicm      135:                s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.79      nicm      136:                s->curw->window->latest = c;
1.4       nicm      137:        } else {
1.36      nicm      138:                if (server_client_open(c, &cause) != 0) {
1.64      nicm      139:                        cmdq_error(item, "open terminal failed: %s", cause);
1.21      nicm      140:                        free(cause);
1.22      nicm      141:                        return (CMD_RETURN_ERROR);
1.27      nicm      142:                }
1.4       nicm      143:
1.78      nicm      144:                if (dflag || xflag) {
                    145:                        if (xflag)
                    146:                                msgtype = MSG_DETACHKILL;
                    147:                        else
                    148:                                msgtype = MSG_DETACH;
1.43      nicm      149:                        TAILQ_FOREACH(c_loop, &clients, entry) {
                    150:                                if (c_loop->session != s || c == c_loop)
                    151:                                        continue;
1.78      nicm      152:                                server_client_detach(c_loop, msgtype);
1.43      nicm      153:                        }
1.28      nicm      154:                }
1.66      nicm      155:                if (!Eflag)
                    156:                        environ_update(s->options, c->environ, s->environ);
1.8       nicm      157:
1.36      nicm      158:                c->session = s;
1.54      nicm      159:                server_client_set_key_table(c, NULL);
1.76      nicm      160:                tty_update_client_offset(c);
1.39      nicm      161:                status_timer_start(c);
1.65      nicm      162:                notify_client("client-session-changed", c);
1.40      nicm      163:                session_update_activity(s, NULL);
1.41      nicm      164:                gettimeofday(&s->last_attached_time, NULL);
1.36      nicm      165:                server_redraw_client(c);
1.17      nicm      166:                s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.79      nicm      167:                s->curw->window->latest = c;
1.23      nicm      168:
1.43      nicm      169:                if (~c->flags & CLIENT_CONTROL)
                    170:                        proc_send(c->peer, MSG_READY, -1, NULL, 0);
1.65      nicm      171:                notify_client("client-attached", c);
1.63      nicm      172:                c->flags |= CLIENT_ATTACHED;
1.1       nicm      173:        }
                    174:        recalculate_sizes();
1.50      nicm      175:        alerts_check_session(s);
1.9       nicm      176:        server_update_socket();
1.1       nicm      177:
1.23      nicm      178:        return (CMD_RETURN_NORMAL);
1.24      nicm      179: }
                    180:
1.62      nicm      181: static enum cmd_retval
1.64      nicm      182: cmd_attach_session_exec(struct cmd *self, struct cmdq_item *item)
1.24      nicm      183: {
1.80      nicm      184:        struct args     *args = cmd_get_args(self);
1.24      nicm      185:
1.73      nicm      186:        return (cmd_attach_session(item, args_get(args, 't'),
1.78      nicm      187:            args_has(args, 'd'), args_has(args, 'x'), args_has(args, 'r'),
1.84    ! nicm      188:            args_get(args, 'c'), args_has(args, 'E'), args_get(args, 'f')));
1.1       nicm      189: }