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

1.58    ! nicm        1: /* $OpenBSD: cmd-new-session.c,v 1.57 2014/02/23 00:53:06 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.54      nicm       21: #include <errno.h>
                     22: #include <fcntl.h>
1.35      nicm       23: #include <stdlib.h>
1.11      nicm       24: #include <string.h>
                     25: #include <termios.h>
1.30      nicm       26: #include <unistd.h>
1.11      nicm       27:
1.1       nicm       28: #include "tmux.h"
                     29:
                     30: /*
                     31:  * Create a new session and attach to the current terminal unless -d is given.
                     32:  */
                     33:
1.48      nicm       34: enum cmd_retval         cmd_new_session_exec(struct cmd *, struct cmd_q *);
1.1       nicm       35:
                     36: const struct cmd_entry cmd_new_session_entry = {
                     37:        "new-session", "new",
1.52      nicm       38:        "Ac:dDF:n:Ps:t:x:y:", 0, 1,
                     39:        "[-AdDP] [-c start-directory] [-F format] [-n window-name] "
                     40:        "[-s session-name] " CMD_TARGET_SESSION_USAGE " [-x width] [-y height] "
                     41:        "[command]",
1.53      nicm       42:        CMD_STARTSERVER|CMD_CANTNEST,
1.34      nicm       43:        NULL,
                     44:        cmd_new_session_exec
1.1       nicm       45: };
                     46:
1.45      nicm       47: enum cmd_retval
1.48      nicm       48: cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       49: {
1.50      nicm       50:        struct args             *args = self->args;
1.54      nicm       51:        struct client           *c = cmdq->client, *c0;
1.50      nicm       52:        struct session          *s, *groupwith;
                     53:        struct window           *w;
                     54:        struct environ           env;
                     55:        struct termios           tio, *tiop;
1.54      nicm       56:        const char              *newname, *target, *update, *errstr, *template;
1.50      nicm       57:        char                    *cmd, *cause, *cp;
1.54      nicm       58:        int                      detached, already_attached, idx, cwd, fd = -1;
1.50      nicm       59:        u_int                    sx, sy;
                     60:        struct format_tree      *ft;
1.51      nicm       61:
                     62:        if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n'))) {
                     63:                cmdq_error(cmdq, "command or window name given with target");
                     64:                return (CMD_RETURN_ERROR);
                     65:        }
1.34      nicm       66:
                     67:        newname = args_get(args, 's');
1.38      nicm       68:        if (newname != NULL) {
                     69:                if (!session_check_name(newname)) {
1.48      nicm       70:                        cmdq_error(cmdq, "bad session name: %s", newname);
1.45      nicm       71:                        return (CMD_RETURN_ERROR);
1.38      nicm       72:                }
                     73:                if (session_find(newname) != NULL) {
1.49      nicm       74:                        if (args_has(args, 'A')) {
                     75:                                return (cmd_attach_session(cmdq, newname,
1.54      nicm       76:                                    args_has(args, 'D'), 0, NULL));
1.49      nicm       77:                        }
1.48      nicm       78:                        cmdq_error(cmdq, "duplicate session: %s", newname);
1.45      nicm       79:                        return (CMD_RETURN_ERROR);
1.38      nicm       80:                }
1.4       nicm       81:        }
1.1       nicm       82:
1.34      nicm       83:        target = args_get(args, 't');
                     84:        if (target != NULL) {
1.48      nicm       85:                groupwith = cmd_find_session(cmdq, target, 0);
1.34      nicm       86:                if (groupwith == NULL)
1.45      nicm       87:                        return (CMD_RETURN_ERROR);
1.34      nicm       88:        } else
                     89:                groupwith = NULL;
1.21      nicm       90:
1.6       nicm       91:        /* Set -d if no client. */
1.34      nicm       92:        detached = args_has(args, 'd');
1.48      nicm       93:        if (c == NULL)
1.6       nicm       94:                detached = 1;
                     95:
1.48      nicm       96:        /* Is this client already attached? */
                     97:        already_attached = 0;
                     98:        if (c != NULL && c->session != NULL)
                     99:                already_attached = 1;
                    100:
1.54      nicm      101:        /* Get the new session working directory. */
                    102:        if (args_has(args, 'c')) {
                    103:                ft = format_create();
                    104:                if ((c0 = cmd_find_client(cmdq, NULL, 1)) != NULL)
                    105:                        format_client(ft, c0);
                    106:                cp = format_expand(ft, args_get(args, 'c'));
                    107:                format_free(ft);
                    108:
1.56      nicm      109:                if (cp != NULL && *cp != '\0') {
                    110:                        fd = open(cp, O_RDONLY|O_DIRECTORY);
                    111:                        free(cp);
                    112:                        if (fd == -1) {
                    113:                                cmdq_error(cmdq, "bad working directory: %s",
                    114:                                    strerror(errno));
                    115:                                return (CMD_RETURN_ERROR);
                    116:                        }
                    117:                } else if (cp != NULL)
                    118:                        free(cp);
1.54      nicm      119:                cwd = fd;
1.55      nicm      120:        } else if (c != NULL && c->session == NULL)
1.54      nicm      121:                cwd = c->cwd;
                    122:        else if ((c0 = cmd_current_client(cmdq)) != NULL)
                    123:                cwd = c0->session->cwd;
                    124:        else {
                    125:                fd = open(".", O_RDONLY);
                    126:                cwd = fd;
                    127:        }
                    128:
1.15      nicm      129:        /*
1.19      nicm      130:         * Save the termios settings, part of which is used for new windows in
                    131:         * this session.
1.15      nicm      132:         *
                    133:         * This is read again with tcgetattr() rather than using tty.tio as if
                    134:         * detached, tty_open won't be called. Because of this, it must be done
                    135:         * before opening the terminal as that calls tcsetattr() to prepare for
                    136:         * tmux taking over.
                    137:         */
1.48      nicm      138:        if (!detached && !already_attached && c->tty.fd != -1) {
                    139:                if (tcgetattr(c->tty.fd, &tio) != 0)
1.15      nicm      140:                        fatal("tcgetattr failed");
1.19      nicm      141:                tiop = &tio;
1.15      nicm      142:        } else
1.19      nicm      143:                tiop = NULL;
1.15      nicm      144:
1.4       nicm      145:        /* Open the terminal if necessary. */
1.48      nicm      146:        if (!detached && !already_attached) {
1.57      nicm      147:                if (server_client_open(c, &cause) != 0) {
1.48      nicm      148:                        cmdq_error(cmdq, "open terminal failed: %s", cause);
1.44      nicm      149:                        free(cause);
1.54      nicm      150:                        goto error;
1.1       nicm      151:                }
                    152:        }
                    153:
1.16      nicm      154:        /* Find new session size. */
1.48      nicm      155:        if (c != NULL) {
                    156:                sx = c->tty.sx;
                    157:                sy = c->tty.sy;
1.39      nicm      158:        } else {
1.4       nicm      159:                sx = 80;
1.18      nicm      160:                sy = 24;
1.39      nicm      161:        }
1.48      nicm      162:        if (detached && args_has(args, 'x')) {
                    163:                sx = strtonum(args_get(args, 'x'), 1, USHRT_MAX, &errstr);
                    164:                if (errstr != NULL) {
                    165:                        cmdq_error(cmdq, "width %s", errstr);
1.54      nicm      166:                        goto error;
1.35      nicm      167:                }
1.48      nicm      168:        }
                    169:        if (detached && args_has(args, 'y')) {
                    170:                sy = strtonum(args_get(args, 'y'), 1, USHRT_MAX, &errstr);
                    171:                if (errstr != NULL) {
                    172:                        cmdq_error(cmdq, "height %s", errstr);
1.54      nicm      173:                        goto error;
1.35      nicm      174:                }
1.1       nicm      175:        }
1.7       nicm      176:        if (sy > 0 && options_get_number(&global_s_options, "status"))
1.4       nicm      177:                sy--;
                    178:        if (sx == 0)
                    179:                sx = 1;
                    180:        if (sy == 0)
                    181:                sy = 1;
1.16      nicm      182:
                    183:        /* Figure out the command for the new window. */
1.34      nicm      184:        if (target != NULL)
1.21      nicm      185:                cmd = NULL;
1.34      nicm      186:        else if (args->argc != 0)
                    187:                cmd = args->argv[0];
1.1       nicm      188:        else
1.7       nicm      189:                cmd = options_get_string(&global_s_options, "default-command");
1.1       nicm      190:
1.10      nicm      191:        /* Construct the environment. */
                    192:        environ_init(&env);
                    193:        update = options_get_string(&global_s_options, "update-environment");
1.48      nicm      194:        if (c != NULL)
                    195:                environ_update(update, &c->environ, &env);
1.11      nicm      196:
1.4       nicm      197:        /* Create the new session. */
1.13      nicm      198:        idx = -1 - options_get_number(&global_s_options, "base-index");
1.34      nicm      199:        s = session_create(newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause);
1.1       nicm      200:        if (s == NULL) {
1.48      nicm      201:                cmdq_error(cmdq, "create session failed: %s", cause);
1.44      nicm      202:                free(cause);
1.54      nicm      203:                goto error;
1.1       nicm      204:        }
1.10      nicm      205:        environ_free(&env);
1.4       nicm      206:
1.16      nicm      207:        /* Set the initial window name if one given. */
1.34      nicm      208:        if (cmd != NULL && args_has(args, 'n')) {
1.16      nicm      209:                w = s->curw->window;
1.40      nicm      210:                window_set_name(w, args_get(args, 'n'));
1.16      nicm      211:                options_set_number(&w->options, "automatic-rename", 0);
1.21      nicm      212:        }
                    213:
                    214:        /*
                    215:         * If a target session is given, this is to be part of a session group,
                    216:         * so add it to the group and synchronize.
                    217:         */
                    218:        if (groupwith != NULL) {
                    219:                session_group_add(groupwith, s);
                    220:                session_group_synchronize_to(s);
                    221:                session_select(s, RB_ROOT(&s->windows)->idx);
1.1       nicm      222:        }
                    223:
1.16      nicm      224:        /*
                    225:         * Set the client to the new session. If a command client exists, it is
                    226:         * taking this session and needs to get MSG_READY and stay around.
1.4       nicm      227:         */
1.25      nicm      228:        if (!detached) {
1.48      nicm      229:                if (!already_attached)
                    230:                        server_write_ready(c);
                    231:                else if (c->session != NULL)
                    232:                        c->last_session = c->session;
                    233:                c->session = s;
                    234:                notify_attached_session_changed(c);
                    235:                session_update_activity(s);
                    236:                server_redraw_client(c);
1.1       nicm      237:        }
                    238:        recalculate_sizes();
1.22      nicm      239:        server_update_socket();
1.26      nicm      240:
                    241:        /*
                    242:         * If there are still configuration file errors to display, put the new
                    243:         * session's current window into more mode and display them now.
                    244:         */
1.46      nicm      245:        if (cfg_finished)
1.48      nicm      246:                cfg_show_causes(s);
1.50      nicm      247:
                    248:        /* Print if requested. */
                    249:        if (args_has(args, 'P')) {
                    250:                if ((template = args_get(args, 'F')) == NULL)
                    251:                        template = NEW_SESSION_TEMPLATE;
                    252:
                    253:                ft = format_create();
1.54      nicm      254:                if ((c0 = cmd_find_client(cmdq, NULL, 1)) != NULL)
                    255:                        format_client(ft, c0);
1.50      nicm      256:                format_session(ft, s);
                    257:
                    258:                cp = format_expand(ft, template);
                    259:                cmdq_print(cmdq, "%s", cp);
                    260:                free(cp);
                    261:
                    262:                format_free(ft);
                    263:        }
1.1       nicm      264:
1.48      nicm      265:        if (!detached)
                    266:                cmdq->client_exit = 0;
1.54      nicm      267:
                    268:        if (fd != -1)
                    269:                close(fd);
1.48      nicm      270:        return (CMD_RETURN_NORMAL);
1.54      nicm      271:
                    272: error:
                    273:        if (fd != -1)
                    274:                close(fd);
                    275:        return (CMD_RETURN_ERROR);
1.1       nicm      276: }