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

1.46    ! nicm        1: /* $OpenBSD: cmd-new-session.c,v 1.45 2012/07/11 07:10:15 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.45      nicm       33: enum cmd_retval         cmd_new_session_check(struct args *);
                     34: enum cmd_retval         cmd_new_session_exec(struct cmd *, struct cmd_ctx *);
1.1       nicm       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:
1.45      nicm       47: enum cmd_retval
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.45      nicm       51:                return (CMD_RETURN_ERROR);
                     52:        return (CMD_RETURN_NORMAL);
1.1       nicm       53: }
                     54:
1.45      nicm       55: enum cmd_retval
1.1       nicm       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 environ           env;
                     62:        struct termios           tio, *tiop;
                     63:        struct passwd           *pw;
1.35      nicm       64:        const char              *newname, *target, *update, *cwd, *errstr;
1.42      nicm       65:        char                    *cmd, *cause;
1.34      nicm       66:        int                      detached, idx;
1.46    ! nicm       67:        u_int                    sx, sy;
1.34      nicm       68:
                     69:        newname = args_get(args, 's');
1.38      nicm       70:        if (newname != NULL) {
                     71:                if (!session_check_name(newname)) {
                     72:                        ctx->error(ctx, "bad session name: %s", newname);
1.45      nicm       73:                        return (CMD_RETURN_ERROR);
1.38      nicm       74:                }
                     75:                if (session_find(newname) != NULL) {
                     76:                        ctx->error(ctx, "duplicate session: %s", newname);
1.45      nicm       77:                        return (CMD_RETURN_ERROR);
1.38      nicm       78:                }
1.4       nicm       79:        }
1.1       nicm       80:
1.34      nicm       81:        target = args_get(args, 't');
                     82:        if (target != NULL) {
1.37      nicm       83:                groupwith = cmd_find_session(ctx, target, 0);
1.34      nicm       84:                if (groupwith == NULL)
1.45      nicm       85:                        return (CMD_RETURN_ERROR);
1.34      nicm       86:        } else
                     87:                groupwith = NULL;
1.21      nicm       88:
1.4       nicm       89:        /*
1.6       nicm       90:         * There are three cases:
1.4       nicm       91:         *
                     92:         * 1. If cmdclient is non-NULL, new-session has been called from the
                     93:         *    command-line - cmdclient is to become a new attached, interactive
                     94:         *    client. Unless -d is given, the terminal must be opened and then
                     95:         *    the client sent MSG_READY.
                     96:         *
                     97:         * 2. If cmdclient is NULL, new-session has been called from an
                     98:         *    existing client (such as a key binding).
                     99:         *
1.6       nicm      100:         * 3. Both are NULL, the command was in the configuration file. Treat
                    101:         *    this as if -d was given even if it was not.
                    102:         *
                    103:         * In all cases, a new additional session needs to be created and
1.4       nicm      104:         * (unless -d) set as the current session for the client.
                    105:         */
                    106:
1.6       nicm      107:        /* Set -d if no client. */
1.34      nicm      108:        detached = args_has(args, 'd');
1.6       nicm      109:        if (ctx->cmdclient == NULL && ctx->curclient == NULL)
                    110:                detached = 1;
                    111:
1.15      nicm      112:        /*
1.19      nicm      113:         * Save the termios settings, part of which is used for new windows in
                    114:         * this session.
1.15      nicm      115:         *
                    116:         * This is read again with tcgetattr() rather than using tty.tio as if
                    117:         * detached, tty_open won't be called. Because of this, it must be done
                    118:         * before opening the terminal as that calls tcsetattr() to prepare for
                    119:         * tmux taking over.
                    120:         */
                    121:        if (ctx->cmdclient != NULL && ctx->cmdclient->tty.fd != -1) {
                    122:                if (tcgetattr(ctx->cmdclient->tty.fd, &tio) != 0)
                    123:                        fatal("tcgetattr failed");
1.19      nicm      124:                tiop = &tio;
1.15      nicm      125:        } else
1.19      nicm      126:                tiop = NULL;
1.15      nicm      127:
1.4       nicm      128:        /* Open the terminal if necessary. */
1.6       nicm      129:        if (!detached && ctx->cmdclient != NULL) {
1.42      nicm      130:                if (server_client_open(ctx->cmdclient, NULL, &cause) != 0) {
1.4       nicm      131:                        ctx->error(ctx, "open terminal failed: %s", cause);
1.44      nicm      132:                        free(cause);
1.45      nicm      133:                        return (CMD_RETURN_ERROR);
1.1       nicm      134:                }
                    135:        }
                    136:
1.16      nicm      137:        /* Get the new session working directory. */
                    138:        if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
                    139:                cwd = ctx->cmdclient->cwd;
1.30      nicm      140:        else {
                    141:                pw = getpwuid(getuid());
                    142:                if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
                    143:                        cwd = pw->pw_dir;
                    144:                else
                    145:                        cwd = "/";
                    146:        }
1.16      nicm      147:
                    148:        /* Find new session size. */
1.39      nicm      149:        if (ctx->cmdclient != NULL) {
                    150:                sx = ctx->cmdclient->tty.sx;
                    151:                sy = ctx->cmdclient->tty.sy;
                    152:        } else if (ctx->curclient != NULL) {
                    153:                sx = ctx->curclient->tty.sx;
                    154:                sy = ctx->curclient->tty.sy;
                    155:        } else {
1.4       nicm      156:                sx = 80;
1.18      nicm      157:                sy = 24;
1.39      nicm      158:        }
                    159:        if (detached) {
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);
1.45      nicm      165:                                return (CMD_RETURN_ERROR);
1.35      nicm      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);
1.45      nicm      173:                                return (CMD_RETURN_ERROR);
1.35      nicm      174:                        }
                    175:                }
1.1       nicm      176:        }
1.7       nicm      177:        if (sy > 0 && options_get_number(&global_s_options, "status"))
1.4       nicm      178:                sy--;
                    179:        if (sx == 0)
                    180:                sx = 1;
                    181:        if (sy == 0)
                    182:                sy = 1;
1.16      nicm      183:
                    184:        /* Figure out the command for the new window. */
1.34      nicm      185:        if (target != NULL)
1.21      nicm      186:                cmd = NULL;
1.34      nicm      187:        else if (args->argc != 0)
                    188:                cmd = args->argv[0];
1.1       nicm      189:        else
1.7       nicm      190:                cmd = options_get_string(&global_s_options, "default-command");
1.1       nicm      191:
1.10      nicm      192:        /* Construct the environment. */
                    193:        environ_init(&env);
                    194:        update = options_get_string(&global_s_options, "update-environment");
                    195:        if (ctx->cmdclient != NULL)
                    196:                environ_update(update, &ctx->cmdclient->environ, &env);
1.11      nicm      197:
1.4       nicm      198:        /* Create the new session. */
1.13      nicm      199:        idx = -1 - options_get_number(&global_s_options, "base-index");
1.34      nicm      200:        s = session_create(newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause);
1.1       nicm      201:        if (s == NULL) {
                    202:                ctx->error(ctx, "create session failed: %s", cause);
1.44      nicm      203:                free(cause);
1.45      nicm      204:                return (CMD_RETURN_ERROR);
1.1       nicm      205:        }
1.10      nicm      206:        environ_free(&env);
1.4       nicm      207:
1.16      nicm      208:        /* Set the initial window name if one given. */
1.34      nicm      209:        if (cmd != NULL && args_has(args, 'n')) {
1.16      nicm      210:                w = s->curw->window;
                    211:
1.40      nicm      212:                window_set_name(w, args_get(args, 'n'));
1.16      nicm      213:
                    214:                options_set_number(&w->options, "automatic-rename", 0);
1.21      nicm      215:        }
                    216:
                    217:        /*
                    218:         * If a target session is given, this is to be part of a session group,
                    219:         * so add it to the group and synchronize.
                    220:         */
                    221:        if (groupwith != NULL) {
                    222:                session_group_add(groupwith, s);
                    223:                session_group_synchronize_to(s);
                    224:                session_select(s, RB_ROOT(&s->windows)->idx);
1.1       nicm      225:        }
                    226:
1.16      nicm      227:        /*
                    228:         * Set the client to the new session. If a command client exists, it is
                    229:         * taking this session and needs to get MSG_READY and stay around.
1.4       nicm      230:         */
1.25      nicm      231:        if (!detached) {
1.4       nicm      232:                if (ctx->cmdclient != NULL) {
1.43      nicm      233:                        server_write_ready(ctx->cmdclient);
1.32      nicm      234:
                    235:                        old_s = ctx->cmdclient->session;
                    236:                        if (old_s != NULL)
                    237:                                ctx->cmdclient->last_session = old_s;
1.25      nicm      238:                        ctx->cmdclient->session = s;
1.41      nicm      239:                        notify_attached_session_changed(ctx->cmdclient);
1.33      nicm      240:                        session_update_activity(s);
1.4       nicm      241:                        server_redraw_client(ctx->cmdclient);
                    242:                } else {
1.32      nicm      243:                        old_s = ctx->curclient->session;
                    244:                        if (old_s != NULL)
                    245:                                ctx->curclient->last_session = old_s;
1.25      nicm      246:                        ctx->curclient->session = s;
1.41      nicm      247:                        notify_attached_session_changed(ctx->curclient);
1.33      nicm      248:                        session_update_activity(s);
1.4       nicm      249:                        server_redraw_client(ctx->curclient);
                    250:                }
1.1       nicm      251:        }
                    252:        recalculate_sizes();
1.22      nicm      253:        server_update_socket();
1.26      nicm      254:
                    255:        /*
                    256:         * If there are still configuration file errors to display, put the new
                    257:         * session's current window into more mode and display them now.
                    258:         */
1.46    ! nicm      259:        if (cfg_finished)
        !           260:                show_cfg_causes(s);
1.1       nicm      261:
1.45      nicm      262:        return (detached ? CMD_RETURN_NORMAL : CMD_RETURN_ATTACH);
1.1       nicm      263: }