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

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