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

1.91    ! nicm        1: /* $OpenBSD: cmd-new-session.c,v 1.90 2016/10/13 22:48:51 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.84      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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.88      nicm       36: static 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 = {
1.82      nicm       39:        .name = "new-session",
                     40:        .alias = "new",
                     41:
                     42:        .args = { "Ac:dDEF:n:Ps:t:x:y:", 0, -1 },
                     43:        .usage = "[-AdDEP] [-c start-directory] [-F format] [-n window-name] "
                     44:                 "[-s session-name] " CMD_TARGET_SESSION_USAGE " [-x width] "
                     45:                 "[-y height] [command]",
                     46:
1.83      nicm       47:        .tflag = CMD_SESSION_CANFAIL,
                     48:
                     49:        .flags = CMD_STARTSERVER,
1.82      nicm       50:        .exec = cmd_new_session_exec
1.1       nicm       51: };
                     52:
1.62      nicm       53: const struct cmd_entry cmd_has_session_entry = {
1.82      nicm       54:        .name = "has-session",
                     55:        .alias = "has",
                     56:
                     57:        .args = { "t:", 0, 0 },
                     58:        .usage = CMD_TARGET_SESSION_USAGE,
                     59:
1.83      nicm       60:        .tflag = CMD_SESSION,
                     61:
                     62:        .flags = 0,
1.82      nicm       63:        .exec = cmd_new_session_exec
1.62      nicm       64: };
                     65:
1.88      nicm       66: static enum cmd_retval
1.48      nicm       67: cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       68: {
1.50      nicm       69:        struct args             *args = self->args;
1.81      nicm       70:        struct client           *c = cmdq->client;
1.86      nicm       71:        struct session          *s, *as;
1.81      nicm       72:        struct session          *groupwith = cmdq->state.tflag.s;
1.50      nicm       73:        struct window           *w;
1.75      nicm       74:        struct environ          *env;
1.50      nicm       75:        struct termios           tio, *tiop;
1.54      nicm       76:        const char              *newname, *target, *update, *errstr, *template;
1.85      nicm       77:        const char              *path, *cwd, *to_free = NULL;
1.60      nicm       78:        char                   **argv, *cmd, *cause, *cp;
1.76      nicm       79:        int                      detached, already_attached, idx, argc;
1.50      nicm       80:        u_int                    sx, sy;
                     81:        struct format_tree      *ft;
1.59      nicm       82:        struct environ_entry    *envent;
1.90      nicm       83:        struct cmd_find_state    fs;
1.62      nicm       84:
                     85:        if (self->entry == &cmd_has_session_entry) {
1.81      nicm       86:                /*
                     87:                 * cmd_prepare() will fail if the session cannot be found,
                     88:                 * hence always return success here.
                     89:                 */
1.62      nicm       90:                return (CMD_RETURN_NORMAL);
                     91:        }
1.51      nicm       92:
                     93:        if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n'))) {
                     94:                cmdq_error(cmdq, "command or window name given with target");
                     95:                return (CMD_RETURN_ERROR);
                     96:        }
1.34      nicm       97:
                     98:        newname = args_get(args, 's');
1.38      nicm       99:        if (newname != NULL) {
                    100:                if (!session_check_name(newname)) {
1.48      nicm      101:                        cmdq_error(cmdq, "bad session name: %s", newname);
1.45      nicm      102:                        return (CMD_RETURN_ERROR);
1.38      nicm      103:                }
1.86      nicm      104:                if ((as = session_find(newname)) != NULL) {
1.49      nicm      105:                        if (args_has(args, 'A')) {
1.81      nicm      106:                                /*
                    107:                                 * This cmdq is now destined for
                    108:                                 * attach-session.  Because attach-session
                    109:                                 * will have already been prepared, copy this
                    110:                                 * session into its tflag so it can be used.
                    111:                                 */
1.86      nicm      112:                                cmd_find_from_session(&cmdq->state.tflag, as);
1.81      nicm      113:                                return (cmd_attach_session(cmdq,
1.68      nicm      114:                                    args_has(args, 'D'), 0, NULL,
                    115:                                    args_has(args, 'E')));
1.49      nicm      116:                        }
1.48      nicm      117:                        cmdq_error(cmdq, "duplicate session: %s", newname);
1.45      nicm      118:                        return (CMD_RETURN_ERROR);
1.38      nicm      119:                }
1.4       nicm      120:        }
1.1       nicm      121:
1.85      nicm      122:        if ((target = args_get(args, 't')) != NULL) {
                    123:                if (groupwith == NULL) {
                    124:                        cmdq_error(cmdq, "no such session: %s", target);
                    125:                        goto error;
                    126:                }
                    127:        } else
1.34      nicm      128:                groupwith = NULL;
1.21      nicm      129:
1.6       nicm      130:        /* Set -d if no client. */
1.34      nicm      131:        detached = args_has(args, 'd');
1.48      nicm      132:        if (c == NULL)
1.6       nicm      133:                detached = 1;
                    134:
1.48      nicm      135:        /* Is this client already attached? */
                    136:        already_attached = 0;
                    137:        if (c != NULL && c->session != NULL)
                    138:                already_attached = 1;
                    139:
1.54      nicm      140:        /* Get the new session working directory. */
                    141:        if (args_has(args, 'c')) {
1.79      nicm      142:                ft = format_create(cmdq, 0);
1.81      nicm      143:                format_defaults(ft, c, NULL, NULL, NULL);
1.76      nicm      144:                to_free = cwd = format_expand(ft, args_get(args, 'c'));
1.54      nicm      145:                format_free(ft);
1.87      nicm      146:        } else if (c != NULL && c->session == NULL && c->cwd != NULL)
1.54      nicm      147:                cwd = c->cwd;
1.76      nicm      148:        else
                    149:                cwd = ".";
1.54      nicm      150:
1.15      nicm      151:        /*
1.67      nicm      152:         * If this is a new client, check for nesting and save the termios
                    153:         * settings (part of which is used for new windows in this session).
1.15      nicm      154:         *
1.67      nicm      155:         * tcgetattr() is used rather than using tty.tio since if the client is
                    156:         * detached, tty_open won't be called. It must be done before opening
                    157:         * the terminal as that calls tcsetattr() to prepare for tmux taking
                    158:         * over.
1.15      nicm      159:         */
1.48      nicm      160:        if (!detached && !already_attached && c->tty.fd != -1) {
1.67      nicm      161:                if (server_client_check_nested(cmdq->client)) {
                    162:                        cmdq_error(cmdq, "sessions should be nested with care, "
                    163:                            "unset $TMUX to force");
                    164:                        return (CMD_RETURN_ERROR);
                    165:                }
1.48      nicm      166:                if (tcgetattr(c->tty.fd, &tio) != 0)
1.15      nicm      167:                        fatal("tcgetattr failed");
1.19      nicm      168:                tiop = &tio;
1.15      nicm      169:        } else
1.19      nicm      170:                tiop = NULL;
1.15      nicm      171:
1.4       nicm      172:        /* Open the terminal if necessary. */
1.48      nicm      173:        if (!detached && !already_attached) {
1.57      nicm      174:                if (server_client_open(c, &cause) != 0) {
1.48      nicm      175:                        cmdq_error(cmdq, "open terminal failed: %s", cause);
1.44      nicm      176:                        free(cause);
1.54      nicm      177:                        goto error;
1.1       nicm      178:                }
                    179:        }
                    180:
1.16      nicm      181:        /* Find new session size. */
1.48      nicm      182:        if (c != NULL) {
                    183:                sx = c->tty.sx;
                    184:                sy = c->tty.sy;
1.39      nicm      185:        } else {
1.4       nicm      186:                sx = 80;
1.18      nicm      187:                sy = 24;
1.39      nicm      188:        }
1.48      nicm      189:        if (detached && args_has(args, 'x')) {
                    190:                sx = strtonum(args_get(args, 'x'), 1, USHRT_MAX, &errstr);
                    191:                if (errstr != NULL) {
                    192:                        cmdq_error(cmdq, "width %s", errstr);
1.54      nicm      193:                        goto error;
1.35      nicm      194:                }
1.48      nicm      195:        }
                    196:        if (detached && args_has(args, 'y')) {
                    197:                sy = strtonum(args_get(args, 'y'), 1, USHRT_MAX, &errstr);
                    198:                if (errstr != NULL) {
                    199:                        cmdq_error(cmdq, "height %s", errstr);
1.54      nicm      200:                        goto error;
1.35      nicm      201:                }
1.1       nicm      202:        }
1.74      nicm      203:        if (sy > 0 && options_get_number(global_s_options, "status"))
1.4       nicm      204:                sy--;
                    205:        if (sx == 0)
                    206:                sx = 1;
                    207:        if (sy == 0)
                    208:                sy = 1;
1.16      nicm      209:
                    210:        /* Figure out the command for the new window. */
1.60      nicm      211:        argc = -1;
                    212:        argv = NULL;
1.81      nicm      213:        if (!args_has(args, 't') && args->argc != 0) {
1.60      nicm      214:                argc = args->argc;
                    215:                argv = args->argv;
1.85      nicm      216:        } else if (groupwith == NULL) {
1.74      nicm      217:                cmd = options_get_string(global_s_options, "default-command");
1.60      nicm      218:                if (cmd != NULL && *cmd != '\0') {
                    219:                        argc = 1;
                    220:                        argv = &cmd;
                    221:                } else {
                    222:                        argc = 0;
                    223:                        argv = NULL;
                    224:                }
                    225:        }
1.1       nicm      226:
1.59      nicm      227:        path = NULL;
                    228:        if (c != NULL && c->session == NULL)
1.75      nicm      229:                envent = environ_find(c->environ, "PATH");
1.59      nicm      230:        else
1.75      nicm      231:                envent = environ_find(global_environ, "PATH");
1.59      nicm      232:        if (envent != NULL)
                    233:                path = envent->value;
                    234:
1.10      nicm      235:        /* Construct the environment. */
1.75      nicm      236:        env = environ_create();
1.68      nicm      237:        if (c != NULL && !args_has(args, 'E')) {
1.74      nicm      238:                update = options_get_string(global_s_options,
1.68      nicm      239:                    "update-environment");
1.75      nicm      240:                environ_update(update, c->environ, env);
1.68      nicm      241:        }
1.11      nicm      242:
1.4       nicm      243:        /* Create the new session. */
1.74      nicm      244:        idx = -1 - options_get_number(global_s_options, "base-index");
1.75      nicm      245:        s = session_create(newname, argc, argv, path, cwd, env, tiop, idx, sx,
1.60      nicm      246:            sy, &cause);
1.75      nicm      247:        environ_free(env);
1.1       nicm      248:        if (s == NULL) {
1.48      nicm      249:                cmdq_error(cmdq, "create session failed: %s", cause);
1.44      nicm      250:                free(cause);
1.54      nicm      251:                goto error;
1.1       nicm      252:        }
1.4       nicm      253:
1.16      nicm      254:        /* Set the initial window name if one given. */
1.60      nicm      255:        if (argc >= 0 && args_has(args, 'n')) {
1.16      nicm      256:                w = s->curw->window;
1.40      nicm      257:                window_set_name(w, args_get(args, 'n'));
1.74      nicm      258:                options_set_number(w->options, "automatic-rename", 0);
1.21      nicm      259:        }
                    260:
                    261:        /*
                    262:         * If a target session is given, this is to be part of a session group,
                    263:         * so add it to the group and synchronize.
                    264:         */
1.85      nicm      265:        if (groupwith != NULL) {
1.21      nicm      266:                session_group_add(groupwith, s);
                    267:                session_group_synchronize_to(s);
1.66      nicm      268:                session_select(s, RB_MIN(winlinks, &s->windows)->idx);
1.1       nicm      269:        }
                    270:
1.16      nicm      271:        /*
                    272:         * Set the client to the new session. If a command client exists, it is
                    273:         * taking this session and needs to get MSG_READY and stay around.
1.4       nicm      274:         */
1.25      nicm      275:        if (!detached) {
1.73      nicm      276:                if (!already_attached) {
                    277:                        if (~c->flags & CLIENT_CONTROL)
                    278:                                proc_send(c->peer, MSG_READY, -1, NULL, 0);
                    279:                } else if (c->session != NULL)
1.48      nicm      280:                        c->last_session = c->session;
                    281:                c->session = s;
1.80      nicm      282:                server_client_set_key_table(c, NULL);
1.69      nicm      283:                status_timer_start(c);
1.48      nicm      284:                notify_attached_session_changed(c);
1.70      nicm      285:                session_update_activity(s, NULL);
1.71      nicm      286:                gettimeofday(&s->last_attached_time, NULL);
1.48      nicm      287:                server_redraw_client(c);
1.1       nicm      288:        }
                    289:        recalculate_sizes();
1.22      nicm      290:        server_update_socket();
1.26      nicm      291:
                    292:        /*
                    293:         * If there are still configuration file errors to display, put the new
                    294:         * session's current window into more mode and display them now.
                    295:         */
1.46      nicm      296:        if (cfg_finished)
1.48      nicm      297:                cfg_show_causes(s);
1.50      nicm      298:
                    299:        /* Print if requested. */
                    300:        if (args_has(args, 'P')) {
                    301:                if ((template = args_get(args, 'F')) == NULL)
                    302:                        template = NEW_SESSION_TEMPLATE;
                    303:
1.79      nicm      304:                ft = format_create(cmdq, 0);
1.81      nicm      305:                format_defaults(ft, c, s, NULL, NULL);
1.50      nicm      306:
                    307:                cp = format_expand(ft, template);
                    308:                cmdq_print(cmdq, "%s", cp);
                    309:                free(cp);
                    310:
                    311:                format_free(ft);
                    312:        }
1.89      nicm      313:
1.48      nicm      314:        if (!detached)
1.91    ! nicm      315:                c->flags |= CLIENT_ATTACHED;
1.54      nicm      316:
1.76      nicm      317:        if (to_free != NULL)
                    318:                free((void *)to_free);
1.90      nicm      319:
                    320:        cmd_find_from_session(&fs, s);
1.91    ! nicm      321:        hooks_insert(s->hooks, cmdq, &fs, "after-new-session");
        !           322:
1.48      nicm      323:        return (CMD_RETURN_NORMAL);
1.54      nicm      324:
                    325: error:
1.76      nicm      326:        if (to_free != NULL)
                    327:                free((void *)to_free);
1.54      nicm      328:        return (CMD_RETURN_ERROR);
1.1       nicm      329: }