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

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