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

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