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

1.27    ! nicm        1: /* $OpenBSD: cmd-attach-session.c,v 1.26 2013/10/10 12:09:34 nicm Exp $ */
1.1       nicm        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:
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.23      nicm       33: enum cmd_retval        cmd_attach_session_exec(struct cmd *, struct cmd_q *);
1.1       nicm       34:
                     35: const struct cmd_entry cmd_attach_session_entry = {
                     36:        "attach-session", "attach",
1.27    ! nicm       37:        "c:drt:", 0, 0,
        !            38:        "[-dr] [-c working-directory] " CMD_TARGET_SESSION_USAGE,
1.26      nicm       39:        CMD_CANTNEST|CMD_STARTSERVER,
1.15      nicm       40:        NULL,
                     41:        cmd_attach_session_exec
1.1       nicm       42: };
                     43:
1.22      nicm       44: enum cmd_retval
1.27    ! nicm       45: cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag,
        !            46:     const char *cflag)
1.1       nicm       47: {
1.27    ! nicm       48:        struct session          *s;
        !            49:        struct client           *c;
        !            50:        const char              *update;
        !            51:        char                    *cause;
        !            52:        u_int                    i;
        !            53:        int                      fd;
        !            54:        struct format_tree      *ft;
        !            55:        char                    *cp;
1.1       nicm       56:
1.13      nicm       57:        if (RB_EMPTY(&sessions)) {
1.23      nicm       58:                cmdq_error(cmdq, "no sessions");
1.22      nicm       59:                return (CMD_RETURN_ERROR);
1.1       nicm       60:        }
1.15      nicm       61:
1.24      nicm       62:        if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL)
1.22      nicm       63:                return (CMD_RETURN_ERROR);
1.5       nicm       64:
1.23      nicm       65:        if (cmdq->client == NULL)
1.22      nicm       66:                return (CMD_RETURN_NORMAL);
1.1       nicm       67:
1.23      nicm       68:        if (cmdq->client->session != NULL) {
1.24      nicm       69:                if (dflag) {
1.11      nicm       70:                        /*
1.4       nicm       71:                         * Can't use server_write_session in case attaching to
                     72:                         * the same session as currently attached to.
                     73:                         */
                     74:                        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     75:                                c = ARRAY_ITEM(&clients, i);
                     76:                                if (c == NULL || c->session != s)
                     77:                                        continue;
1.23      nicm       78:                                if (c == cmdq->client)
1.4       nicm       79:                                        continue;
                     80:                                server_write_client(c, MSG_DETACH, NULL, 0);
                     81:                        }
                     82:                }
1.11      nicm       83:
1.27    ! nicm       84:                if (cflag != NULL) {
        !            85:                        ft = format_create();
        !            86:                        if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
        !            87:                                format_client(ft, c);
        !            88:                        format_session(ft, s);
        !            89:                        format_winlink(ft, s, s->curw);
        !            90:                        format_window_pane(ft, s->curw->window->active);
        !            91:                        cp = format_expand(ft, cflag);
        !            92:                        format_free(ft);
        !            93:
        !            94:                        fd = open(cp, O_RDONLY|O_DIRECTORY);
        !            95:                        free(cp);
        !            96:                        if (fd == -1) {
        !            97:                                cmdq_error(cmdq, "bad working directory: %s",
        !            98:                                    strerror(errno));
        !            99:                                return (CMD_RETURN_ERROR);
        !           100:                        }
        !           101:                        close(s->cwd);
        !           102:                        s->cwd = fd;
        !           103:                }
        !           104:
1.23      nicm      105:                cmdq->client->session = s;
                    106:                notify_attached_session_changed(cmdq->client);
1.14      nicm      107:                session_update_activity(s);
1.23      nicm      108:                server_redraw_client(cmdq->client);
1.17      nicm      109:                s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.4       nicm      110:        } else {
1.23      nicm      111:                if (server_client_open(cmdq->client, s, &cause) != 0) {
                    112:                        cmdq_error(cmdq, "open terminal failed: %s", cause);
1.21      nicm      113:                        free(cause);
1.22      nicm      114:                        return (CMD_RETURN_ERROR);
1.4       nicm      115:                }
1.12      nicm      116:
1.27    ! nicm      117:                if (cflag != NULL) {
        !           118:                        ft = format_create();
        !           119:                        if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
        !           120:                                format_client(ft, c);
        !           121:                        format_session(ft, s);
        !           122:                        format_winlink(ft, s, s->curw);
        !           123:                        format_window_pane(ft, s->curw->window->active);
        !           124:                        cp = format_expand(ft, cflag);
        !           125:                        format_free(ft);
        !           126:
        !           127:                        fd = open(cp, O_RDONLY|O_DIRECTORY);
        !           128:                        free(cp);
        !           129:                        if (fd == -1) {
        !           130:                                cmdq_error(cmdq, "bad working directory: %s",
        !           131:                                    strerror(errno));
        !           132:                                return (CMD_RETURN_ERROR);
        !           133:                        }
        !           134:                        close(s->cwd);
        !           135:                        s->cwd = fd;
        !           136:                }
        !           137:
1.24      nicm      138:                if (rflag)
1.23      nicm      139:                        cmdq->client->flags |= CLIENT_READONLY;
1.4       nicm      140:
1.24      nicm      141:                if (dflag)
1.4       nicm      142:                        server_write_session(s, MSG_DETACH, NULL, 0);
                    143:
1.8       nicm      144:                update = options_get_string(&s->options, "update-environment");
1.23      nicm      145:                environ_update(update, &cmdq->client->environ, &s->environ);
1.8       nicm      146:
1.23      nicm      147:                cmdq->client->session = s;
                    148:                notify_attached_session_changed(cmdq->client);
                    149:                session_update_activity(s);
                    150:                server_redraw_client(cmdq->client);
1.17      nicm      151:                s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.23      nicm      152:
                    153:                server_write_ready(cmdq->client);
                    154:                cmdq->client_exit = 0;
1.1       nicm      155:        }
                    156:        recalculate_sizes();
1.9       nicm      157:        server_update_socket();
1.1       nicm      158:
1.23      nicm      159:        return (CMD_RETURN_NORMAL);
1.24      nicm      160: }
                    161:
                    162: enum cmd_retval
                    163: cmd_attach_session_exec(struct cmd *self, struct cmd_q *cmdq)
                    164: {
                    165:        struct args     *args = self->args;
                    166:
                    167:        return (cmd_attach_session(cmdq, args_get(args, 't'),
1.27    ! nicm      168:            args_has(args, 'd'), args_has(args, 'r'), args_get(args, 'c')));
1.1       nicm      169: }