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

1.14    ! nicm        1: /* $OpenBSD: cmd-new-session.c,v 1.13 2009/08/13 20:11:58 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.11      nicm       21: #include <string.h>
                     22: #include <termios.h>
                     23:
                     24: #define TTYDEFCHARS
                     25: #include <sys/ttydefaults.h>
                     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:
                     33: int    cmd_new_session_parse(struct cmd *, int, char **, char **);
                     34: int    cmd_new_session_exec(struct cmd *, struct cmd_ctx *);
                     35: void   cmd_new_session_free(struct cmd *);
                     36: void   cmd_new_session_init(struct cmd *, int);
                     37: size_t cmd_new_session_print(struct cmd *, char *, size_t);
                     38:
                     39: struct cmd_new_session_data {
                     40:        char    *newname;
                     41:        char    *winname;
                     42:        char    *cmd;
                     43:        int      flag_detached;
                     44: };
                     45:
                     46: const struct cmd_entry cmd_new_session_entry = {
                     47:        "new-session", "new",
                     48:        "[-d] [-n window-name] [-s session-name] [command]",
1.10      nicm       49:        CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON, 0,
1.1       nicm       50:        cmd_new_session_init,
                     51:        cmd_new_session_parse,
                     52:        cmd_new_session_exec,
                     53:        cmd_new_session_free,
                     54:        cmd_new_session_print
                     55: };
                     56:
                     57: void
                     58: cmd_new_session_init(struct cmd *self, unused int arg)
                     59: {
                     60:        struct cmd_new_session_data      *data;
                     61:
                     62:        self->data = data = xmalloc(sizeof *data);
                     63:        data->flag_detached = 0;
                     64:        data->newname = NULL;
                     65:        data->winname = NULL;
                     66:        data->cmd = NULL;
                     67: }
                     68:
                     69: int
                     70: cmd_new_session_parse(struct cmd *self, int argc, char **argv, char **cause)
                     71: {
                     72:        struct cmd_new_session_data     *data;
                     73:        int                              opt;
                     74:
                     75:        self->entry->init(self, 0);
                     76:        data = self->data;
                     77:
                     78:        while ((opt = getopt(argc, argv, "ds:n:")) != -1) {
                     79:                switch (opt) {
                     80:                case 'd':
                     81:                        data->flag_detached = 1;
                     82:                        break;
                     83:                case 's':
                     84:                        if (data->newname == NULL)
                     85:                                data->newname = xstrdup(optarg);
                     86:                        break;
                     87:                case 'n':
                     88:                        if (data->winname == NULL)
                     89:                                data->winname = xstrdup(optarg);
                     90:                        break;
                     91:                default:
                     92:                        goto usage;
                     93:                }
                     94:        }
                     95:        argc -= optind;
                     96:        argv += optind;
                     97:        if (argc != 0 && argc != 1)
                     98:                goto usage;
                     99:
                    100:        if (argc == 1)
                    101:                data->cmd = xstrdup(argv[0]);
                    102:
                    103:        return (0);
                    104:
                    105: usage:
                    106:        xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
                    107:
                    108:        self->entry->free(self);
                    109:        return (-1);
                    110: }
                    111:
                    112: int
                    113: cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
                    114: {
                    115:        struct cmd_new_session_data     *data = self->data;
                    116:        struct session                  *s;
1.10      nicm      117:        struct environ                   env;
1.11      nicm      118:        struct termios                   tio;
1.10      nicm      119:        const char                      *update;
1.9       nicm      120:        char                            *overrides, *cmd, *cwd, *cause;
1.13      nicm      121:        int                              detached, idx;
1.1       nicm      122:        u_int                            sx, sy;
                    123:
1.4       nicm      124:        if (data->newname != NULL && session_find(data->newname) != NULL) {
                    125:                ctx->error(ctx, "duplicate session: %s", data->newname);
                    126:                return (-1);
                    127:        }
1.1       nicm      128:
1.4       nicm      129:        /*
1.6       nicm      130:         * There are three cases:
1.4       nicm      131:         *
                    132:         * 1. If cmdclient is non-NULL, new-session has been called from the
                    133:         *    command-line - cmdclient is to become a new attached, interactive
                    134:         *    client. Unless -d is given, the terminal must be opened and then
                    135:         *    the client sent MSG_READY.
                    136:         *
                    137:         * 2. If cmdclient is NULL, new-session has been called from an
                    138:         *    existing client (such as a key binding).
                    139:         *
1.6       nicm      140:         * 3. Both are NULL, the command was in the configuration file. Treat
                    141:         *    this as if -d was given even if it was not.
                    142:         *
                    143:         * In all cases, a new additional session needs to be created and
1.4       nicm      144:         * (unless -d) set as the current session for the client.
                    145:         */
                    146:
1.6       nicm      147:        /* Set -d if no client. */
                    148:        detached = data->flag_detached;
                    149:        if (ctx->cmdclient == NULL && ctx->curclient == NULL)
                    150:                detached = 1;
                    151:
1.4       nicm      152:        /* Open the terminal if necessary. */
1.6       nicm      153:        if (!detached && ctx->cmdclient != NULL) {
1.4       nicm      154:                if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
                    155:                        ctx->error(ctx, "not a terminal");
1.1       nicm      156:                        return (-1);
                    157:                }
1.4       nicm      158:
1.9       nicm      159:                overrides =
                    160:                    options_get_string(&global_s_options, "terminal-overrides");
                    161:                if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
1.4       nicm      162:                        ctx->error(ctx, "open terminal failed: %s", cause);
                    163:                        xfree(cause);
1.1       nicm      164:                        return (-1);
                    165:                }
                    166:        }
                    167:
1.4       nicm      168:        /* Find new session size and options. */
1.6       nicm      169:        if (detached) {
1.4       nicm      170:                sx = 80;
                    171:                sy = 25;
                    172:        } else {
                    173:                if (ctx->cmdclient != NULL) {
                    174:                        sx = ctx->cmdclient->tty.sx;
                    175:                        sy = ctx->cmdclient->tty.sy;
                    176:                } else {
                    177:                        sx = ctx->curclient->tty.sx;
                    178:                        sy = ctx->curclient->tty.sy;
                    179:                }
1.1       nicm      180:        }
1.7       nicm      181:        if (sy > 0 && options_get_number(&global_s_options, "status"))
1.4       nicm      182:                sy--;
                    183:        if (sx == 0)
                    184:                sx = 1;
                    185:        if (sy == 0)
                    186:                sy = 1;
                    187:        if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
                    188:                cwd = ctx->cmdclient->cwd;
                    189:        else
1.7       nicm      190:                cwd = options_get_string(&global_s_options, "default-path");
1.4       nicm      191:        if (data->cmd != NULL)
                    192:                cmd = data->cmd;
1.1       nicm      193:        else
1.7       nicm      194:                cmd = options_get_string(&global_s_options, "default-command");
1.1       nicm      195:
1.10      nicm      196:        /* Construct the environment. */
                    197:        environ_init(&env);
                    198:        update = options_get_string(&global_s_options, "update-environment");
                    199:        if (ctx->cmdclient != NULL)
                    200:                environ_update(update, &ctx->cmdclient->environ, &env);
                    201:
1.11      nicm      202:        /*
                    203:         * Fill in the termios settings used for new windows in this session;
                    204:         * if there is a command client, use the control characters from it.
                    205:         */
                    206:        if (ctx->cmdclient != NULL && ctx->cmdclient->tty.fd != -1) {
                    207:                if (tcgetattr(ctx->cmdclient->tty.fd, &tio) != 0)
                    208:                        fatal("tcgetattr failed");
                    209:        } else
                    210:                memcpy(tio.c_cc, ttydefchars, sizeof tio.c_cc);
                    211:        tio.c_iflag = TTYDEF_IFLAG;
                    212:        tio.c_oflag = TTYDEF_OFLAG;
                    213:        tio.c_lflag = TTYDEF_LFLAG;
                    214:        tio.c_cflag = TTYDEF_CFLAG;
1.14    ! nicm      215:        cfsetispeed(&tio, TTYDEF_SPEED);
        !           216:        cfsetospeed(&tio, TTYDEF_SPEED);
1.11      nicm      217:
1.4       nicm      218:        /* Create the new session. */
1.13      nicm      219:        idx = -1 - options_get_number(&global_s_options, "base-index");
                    220:        s = session_create(
                    221:            data->newname, cmd, cwd, &env, &tio, idx, sx, sy, &cause);
1.1       nicm      222:        if (s == NULL) {
                    223:                ctx->error(ctx, "create session failed: %s", cause);
                    224:                xfree(cause);
                    225:                return (-1);
                    226:        }
1.10      nicm      227:        environ_free(&env);
1.4       nicm      228:
1.1       nicm      229:        if (data->winname != NULL) {
                    230:                xfree(s->curw->window->name);
                    231:                s->curw->window->name = xstrdup(data->winname);
                    232:                options_set_number(
                    233:                    &s->curw->window->options, "automatic-rename", 0);
                    234:        }
                    235:
1.4       nicm      236:        /*
                    237:         * If a command client exists, it is either taking this session (and
                    238:         * needs to get MSG_READY and stay around), or -d is given and it needs
                    239:         * to exit.
                    240:         */
                    241:        if (ctx->cmdclient != NULL) {
1.6       nicm      242:                if (!detached)
1.4       nicm      243:                        server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
                    244:        }
                    245:
                    246:        /* Set the client to the new session. */
1.6       nicm      247:        if (!detached) {
1.4       nicm      248:                if (ctx->cmdclient != NULL) {
                    249:                        ctx->cmdclient->session = s;
                    250:                        server_redraw_client(ctx->cmdclient);
                    251:                } else {
                    252:                        ctx->curclient->session = s;
                    253:                        server_redraw_client(ctx->curclient);
                    254:                }
1.1       nicm      255:        }
                    256:        recalculate_sizes();
                    257:
1.12      nicm      258:        return (!detached);     /* 1 means don't tell command client to exit */
1.1       nicm      259: }
                    260:
                    261: void
                    262: cmd_new_session_free(struct cmd *self)
                    263: {
                    264:        struct cmd_new_session_data     *data = self->data;
                    265:
                    266:        if (data->newname != NULL)
                    267:                xfree(data->newname);
                    268:        if (data->winname != NULL)
                    269:                xfree(data->winname);
                    270:        if (data->cmd != NULL)
                    271:                xfree(data->cmd);
                    272:        xfree(data);
                    273: }
                    274:
                    275: size_t
                    276: cmd_new_session_print(struct cmd *self, char *buf, size_t len)
                    277: {
                    278:        struct cmd_new_session_data     *data = self->data;
                    279:        size_t                           off = 0;
                    280:
                    281:        off += xsnprintf(buf, len, "%s", self->entry->name);
                    282:        if (data == NULL)
                    283:                return (off);
                    284:        if (off < len && data->flag_detached)
                    285:                off += xsnprintf(buf + off, len - off, " -d");
                    286:        if (off < len && data->newname != NULL)
                    287:                off += cmd_prarg(buf + off, len - off, " -s ", data->newname);
                    288:        if (off < len && data->winname != NULL)
                    289:                off += cmd_prarg(buf + off, len - off, " -n ", data->winname);
                    290:        if (off < len && data->cmd != NULL)
                    291:                off += cmd_prarg(buf + off, len - off, " ", data->cmd);
                    292:        return (off);
                    293: }