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

Annotation of src/usr.bin/tmux/cmd-new-session.c, Revision 1.38

1.38    ! nicm        1: /* $OpenBSD: cmd-new-session.c,v 1.37 2011/04/05 19:37:01 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.30      nicm       21: #include <pwd.h>
1.35      nicm       22: #include <stdlib.h>
1.11      nicm       23: #include <string.h>
                     24: #include <termios.h>
1.30      nicm       25: #include <unistd.h>
1.11      nicm       26:
1.1       nicm       27: #include "tmux.h"
                     28:
                     29: /*
                     30:  * Create a new session and attach to the current terminal unless -d is given.
                     31:  */
                     32:
1.34      nicm       33: int    cmd_new_session_check(struct args *);
1.1       nicm       34: int    cmd_new_session_exec(struct cmd *, struct cmd_ctx *);
                     35:
                     36: const struct cmd_entry cmd_new_session_entry = {
                     37:        "new-session", "new",
1.35      nicm       38:        "dn:s:t:x:y:", 0, 1,
                     39:        "[-d] [-n window-name] [-s session-name] [-t target-session] "
                     40:        "[-x width] [-y height] [command]",
1.34      nicm       41:        CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON,
                     42:        NULL,
                     43:        cmd_new_session_check,
                     44:        cmd_new_session_exec
1.1       nicm       45: };
                     46:
                     47: int
1.34      nicm       48: cmd_new_session_check(struct args *args)
1.1       nicm       49: {
1.34      nicm       50:        if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n')))
1.35      nicm       51:                return (-1);
1.1       nicm       52:        return (0);
                     53: }
                     54:
                     55: int
                     56: cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
                     57: {
1.34      nicm       58:        struct args             *args = self->args;
                     59:        struct session          *s, *old_s, *groupwith;
                     60:        struct window           *w;
                     61:        struct window_pane      *wp;
                     62:        struct environ           env;
                     63:        struct termios           tio, *tiop;
                     64:        struct passwd           *pw;
1.35      nicm       65:        const char              *newname, *target, *update, *cwd, *errstr;
1.34      nicm       66:        char                    *overrides, *cmd, *cause;
                     67:        int                      detached, idx;
                     68:        u_int                    sx, sy, i;
                     69:
                     70:        newname = args_get(args, 's');
1.38    ! nicm       71:        if (newname != NULL) {
        !            72:                if (!session_check_name(newname)) {
        !            73:                        ctx->error(ctx, "bad session name: %s", newname);
        !            74:                        return (-1);
        !            75:                }
        !            76:                if (session_find(newname) != NULL) {
        !            77:                        ctx->error(ctx, "duplicate session: %s", newname);
        !            78:                        return (-1);
        !            79:                }
1.4       nicm       80:        }
1.1       nicm       81:
1.34      nicm       82:        target = args_get(args, 't');
                     83:        if (target != NULL) {
1.37      nicm       84:                groupwith = cmd_find_session(ctx, target, 0);
1.34      nicm       85:                if (groupwith == NULL)
                     86:                        return (-1);
                     87:        } else
                     88:                groupwith = NULL;
1.21      nicm       89:
1.4       nicm       90:        /*
1.6       nicm       91:         * There are three cases:
1.4       nicm       92:         *
                     93:         * 1. If cmdclient is non-NULL, new-session has been called from the
                     94:         *    command-line - cmdclient is to become a new attached, interactive
                     95:         *    client. Unless -d is given, the terminal must be opened and then
                     96:         *    the client sent MSG_READY.
                     97:         *
                     98:         * 2. If cmdclient is NULL, new-session has been called from an
                     99:         *    existing client (such as a key binding).
                    100:         *
1.6       nicm      101:         * 3. Both are NULL, the command was in the configuration file. Treat
                    102:         *    this as if -d was given even if it was not.
                    103:         *
                    104:         * In all cases, a new additional session needs to be created and
1.4       nicm      105:         * (unless -d) set as the current session for the client.
                    106:         */
                    107:
1.6       nicm      108:        /* Set -d if no client. */
1.34      nicm      109:        detached = args_has(args, 'd');
1.6       nicm      110:        if (ctx->cmdclient == NULL && ctx->curclient == NULL)
                    111:                detached = 1;
                    112:
1.15      nicm      113:        /*
1.19      nicm      114:         * Save the termios settings, part of which is used for new windows in
                    115:         * this session.
1.15      nicm      116:         *
                    117:         * This is read again with tcgetattr() rather than using tty.tio as if
                    118:         * detached, tty_open won't be called. Because of this, it must be done
                    119:         * before opening the terminal as that calls tcsetattr() to prepare for
                    120:         * tmux taking over.
                    121:         */
                    122:        if (ctx->cmdclient != NULL && ctx->cmdclient->tty.fd != -1) {
                    123:                if (tcgetattr(ctx->cmdclient->tty.fd, &tio) != 0)
                    124:                        fatal("tcgetattr failed");
1.19      nicm      125:                tiop = &tio;
1.15      nicm      126:        } else
1.19      nicm      127:                tiop = NULL;
1.15      nicm      128:
1.4       nicm      129:        /* Open the terminal if necessary. */
1.6       nicm      130:        if (!detached && ctx->cmdclient != NULL) {
1.4       nicm      131:                if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
                    132:                        ctx->error(ctx, "not a terminal");
1.1       nicm      133:                        return (-1);
                    134:                }
1.25      nicm      135:
                    136:                overrides =
1.9       nicm      137:                    options_get_string(&global_s_options, "terminal-overrides");
                    138:                if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
1.4       nicm      139:                        ctx->error(ctx, "open terminal failed: %s", cause);
                    140:                        xfree(cause);
1.1       nicm      141:                        return (-1);
                    142:                }
                    143:        }
                    144:
1.16      nicm      145:        /* Get the new session working directory. */
                    146:        if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
                    147:                cwd = ctx->cmdclient->cwd;
1.30      nicm      148:        else {
                    149:                pw = getpwuid(getuid());
                    150:                if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
                    151:                        cwd = pw->pw_dir;
                    152:                else
                    153:                        cwd = "/";
                    154:        }
1.16      nicm      155:
                    156:        /* Find new session size. */
1.6       nicm      157:        if (detached) {
1.4       nicm      158:                sx = 80;
1.18      nicm      159:                sy = 24;
1.35      nicm      160:                if (args_has(args, 'x')) {
                    161:                        sx = strtonum(
                    162:                            args_get(args, 'x'), 1, USHRT_MAX, &errstr);
                    163:                        if (errstr != NULL) {
                    164:                                ctx->error(ctx, "width %s", errstr);
                    165:                                return (-1);
                    166:                        }
                    167:                }
                    168:                if (args_has(args, 'y')) {
                    169:                        sy = strtonum(
                    170:                            args_get(args, 'y'), 1, USHRT_MAX, &errstr);
                    171:                        if (errstr != NULL) {
                    172:                                ctx->error(ctx, "height %s", errstr);
                    173:                                return (-1);
                    174:                        }
                    175:                }
1.16      nicm      176:        } else if (ctx->cmdclient != NULL) {
                    177:                sx = ctx->cmdclient->tty.sx;
                    178:                sy = ctx->cmdclient->tty.sy;
1.4       nicm      179:        } else {
1.16      nicm      180:                sx = ctx->curclient->tty.sx;
                    181:                sy = ctx->curclient->tty.sy;
1.1       nicm      182:        }
1.7       nicm      183:        if (sy > 0 && options_get_number(&global_s_options, "status"))
1.4       nicm      184:                sy--;
                    185:        if (sx == 0)
                    186:                sx = 1;
                    187:        if (sy == 0)
                    188:                sy = 1;
1.16      nicm      189:
                    190:        /* Figure out the command for the new window. */
1.34      nicm      191:        if (target != NULL)
1.21      nicm      192:                cmd = NULL;
1.34      nicm      193:        else if (args->argc != 0)
                    194:                cmd = args->argv[0];
1.1       nicm      195:        else
1.7       nicm      196:                cmd = options_get_string(&global_s_options, "default-command");
1.1       nicm      197:
1.10      nicm      198:        /* Construct the environment. */
                    199:        environ_init(&env);
                    200:        update = options_get_string(&global_s_options, "update-environment");
                    201:        if (ctx->cmdclient != NULL)
                    202:                environ_update(update, &ctx->cmdclient->environ, &env);
1.11      nicm      203:
1.4       nicm      204:        /* Create the new session. */
1.13      nicm      205:        idx = -1 - options_get_number(&global_s_options, "base-index");
1.34      nicm      206:        s = session_create(newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause);
1.1       nicm      207:        if (s == NULL) {
                    208:                ctx->error(ctx, "create session failed: %s", cause);
                    209:                xfree(cause);
                    210:                return (-1);
                    211:        }
1.10      nicm      212:        environ_free(&env);
1.4       nicm      213:
1.16      nicm      214:        /* Set the initial window name if one given. */
1.34      nicm      215:        if (cmd != NULL && args_has(args, 'n')) {
1.16      nicm      216:                w = s->curw->window;
                    217:
                    218:                xfree(w->name);
1.34      nicm      219:                w->name = xstrdup(args_get(args, 'n'));
1.16      nicm      220:
                    221:                options_set_number(&w->options, "automatic-rename", 0);
1.21      nicm      222:        }
                    223:
                    224:        /*
                    225:         * If a target session is given, this is to be part of a session group,
                    226:         * so add it to the group and synchronize.
                    227:         */
                    228:        if (groupwith != NULL) {
                    229:                session_group_add(groupwith, s);
                    230:                session_group_synchronize_to(s);
                    231:                session_select(s, RB_ROOT(&s->windows)->idx);
1.1       nicm      232:        }
                    233:
1.16      nicm      234:        /*
                    235:         * Set the client to the new session. If a command client exists, it is
                    236:         * taking this session and needs to get MSG_READY and stay around.
1.4       nicm      237:         */
1.25      nicm      238:        if (!detached) {
1.4       nicm      239:                if (ctx->cmdclient != NULL) {
1.16      nicm      240:                        server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
1.32      nicm      241:
                    242:                        old_s = ctx->cmdclient->session;
                    243:                        if (old_s != NULL)
                    244:                                ctx->cmdclient->last_session = old_s;
1.25      nicm      245:                        ctx->cmdclient->session = s;
1.33      nicm      246:                        session_update_activity(s);
1.4       nicm      247:                        server_redraw_client(ctx->cmdclient);
                    248:                } else {
1.32      nicm      249:                        old_s = ctx->curclient->session;
                    250:                        if (old_s != NULL)
                    251:                                ctx->curclient->last_session = old_s;
1.25      nicm      252:                        ctx->curclient->session = s;
1.33      nicm      253:                        session_update_activity(s);
1.4       nicm      254:                        server_redraw_client(ctx->curclient);
                    255:                }
1.1       nicm      256:        }
                    257:        recalculate_sizes();
1.22      nicm      258:        server_update_socket();
1.26      nicm      259:
                    260:        /*
                    261:         * If there are still configuration file errors to display, put the new
                    262:         * session's current window into more mode and display them now.
                    263:         */
1.27      nicm      264:        if (cfg_finished && !ARRAY_EMPTY(&cfg_causes)) {
1.26      nicm      265:                wp = s->curw->window->active;
1.29      nicm      266:                window_pane_set_mode(wp, &window_copy_mode);
                    267:                window_copy_init_for_output(wp);
1.27      nicm      268:                for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
                    269:                        cause = ARRAY_ITEM(&cfg_causes, i);
1.29      nicm      270:                        window_copy_add(wp, "%s", cause);
1.27      nicm      271:                        xfree(cause);
1.26      nicm      272:                }
1.27      nicm      273:                ARRAY_FREE(&cfg_causes);
1.26      nicm      274:        }
1.1       nicm      275:
1.12      nicm      276:        return (!detached);     /* 1 means don't tell command client to exit */
1.1       nicm      277: }