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

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