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

1.132   ! nicm        1: /* $OpenBSD: cmd-new-session.c,v 1.131 2020/05/16 14:49:50 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.92      nicm       36: static enum cmd_retval cmd_new_session_exec(struct cmd *, struct cmdq_item *);
1.1       nicm       37:
                     38: const struct cmd_entry cmd_new_session_entry = {
1.82      nicm       39:        .name = "new-session",
                     40:        .alias = "new",
                     41:
1.132   ! nicm       42:        .args = { "Ac:dDe:EF:n:Ps:t:x:Xy:", 0, -1 },
        !            43:        .usage = "[-AdDEPX] [-c start-directory] [-e environment] [-F format] "
        !            44:                 "[-n window-name] [-s session-name] "
        !            45:                 CMD_TARGET_SESSION_USAGE " [-x width] [-y height] [command]",
1.82      nicm       46:
1.105     nicm       47:        .target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL },
1.83      nicm       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.105     nicm       60:        .target = { 't', CMD_FIND_SESSION, 0 },
1.83      nicm       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.92      nicm       67: cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       68: {
1.125     nicm       69:        struct args             *args = cmd_get_args(self);
1.128     nicm       70:        struct cmd_find_state   *current = cmdq_get_current(item);
1.126     nicm       71:        struct cmd_find_state   *target = cmdq_get_target(item);
                     72:        struct client           *c = cmdq_get_client(item);
1.131     nicm       73:        struct session          *s, *as, *groupwith = NULL;
1.75      nicm       74:        struct environ          *env;
1.114     nicm       75:        struct options          *oo;
1.50      nicm       76:        struct termios           tio, *tiop;
1.131     nicm       77:        struct session_group    *sg = NULL;
1.132   ! nicm       78:        const char              *errstr, *template, *group, *tmp, *add;
1.116     nicm       79:        char                    *cause, *cwd = NULL, *cp, *newname = NULL;
1.131     nicm       80:        char                    *name, *prefix = NULL;
1.116     nicm       81:        int                      detached, already_attached, is_control = 0;
                     82:        u_int                    sx, sy, dsx, dsy;
                     83:        struct spawn_context     sc;
1.110     nicm       84:        enum cmd_retval          retval;
1.116     nicm       85:        struct cmd_find_state    fs;
1.132   ! nicm       86:        struct args_value       *value;
1.62      nicm       87:
1.125     nicm       88:        if (cmd_get_entry(self) == &cmd_has_session_entry) {
1.81      nicm       89:                /*
1.106     nicm       90:                 * cmd_find_target() will fail if the session cannot be found,
                     91:                 * so always return success here.
1.81      nicm       92:                 */
1.62      nicm       93:                return (CMD_RETURN_NORMAL);
                     94:        }
1.51      nicm       95:
                     96:        if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n'))) {
1.92      nicm       97:                cmdq_error(item, "command or window name given with target");
1.51      nicm       98:                return (CMD_RETURN_ERROR);
                     99:        }
1.34      nicm      100:
1.122     nicm      101:        tmp = args_get(args, 's');
                    102:        if (tmp != NULL) {
1.131     nicm      103:                name = format_single(item, tmp, c, NULL, NULL, NULL);
                    104:                newname = session_check_name(name);
                    105:                free(name);
1.122     nicm      106:        }
                    107:        if (args_has(args, 'A')) {
                    108:                if (newname != NULL)
                    109:                        as = session_find(newname);
                    110:                else
1.126     nicm      111:                        as = target->s;
1.122     nicm      112:                if (as != NULL) {
                    113:                        retval = cmd_attach_session(item, as->name,
                    114:                            args_has(args, 'D'), args_has(args, 'X'), 0, NULL,
                    115:                            args_has(args, 'E'));
                    116:                        free(newname);
                    117:                        return (retval);
1.38      nicm      118:                }
1.122     nicm      119:        }
                    120:        if (newname != NULL && session_find(newname) != NULL) {
                    121:                cmdq_error(item, "duplicate session: %s", newname);
                    122:                goto fail;
1.4       nicm      123:        }
1.1       nicm      124:
1.101     nicm      125:        /* Is this going to be part of a session group? */
                    126:        group = args_get(args, 't');
                    127:        if (group != NULL) {
1.126     nicm      128:                groupwith = target->s;
1.131     nicm      129:                if (groupwith == NULL)
1.101     nicm      130:                        sg = session_group_find(group);
1.131     nicm      131:                else
1.101     nicm      132:                        sg = session_group_contains(groupwith);
                    133:                if (sg != NULL)
1.131     nicm      134:                        prefix = xstrdup(sg->name);
1.101     nicm      135:                else if (groupwith != NULL)
1.131     nicm      136:                        prefix = xstrdup(groupwith->name);
1.101     nicm      137:                else
1.131     nicm      138:                        prefix = session_check_name(group);
1.101     nicm      139:        }
1.21      nicm      140:
1.6       nicm      141:        /* Set -d if no client. */
1.34      nicm      142:        detached = args_has(args, 'd');
1.48      nicm      143:        if (c == NULL)
1.6       nicm      144:                detached = 1;
1.107     nicm      145:        else if (c->flags & CLIENT_CONTROL)
                    146:                is_control = 1;
1.6       nicm      147:
1.48      nicm      148:        /* Is this client already attached? */
                    149:        already_attached = 0;
                    150:        if (c != NULL && c->session != NULL)
                    151:                already_attached = 1;
                    152:
1.54      nicm      153:        /* Get the new session working directory. */
1.110     nicm      154:        if ((tmp = args_get(args, 'c')) != NULL)
                    155:                cwd = format_single(item, tmp, c, NULL, NULL, NULL);
1.76      nicm      156:        else
1.111     nicm      157:                cwd = xstrdup(server_client_get_cwd(c, NULL));
1.54      nicm      158:
1.15      nicm      159:        /*
1.67      nicm      160:         * If this is a new client, check for nesting and save the termios
                    161:         * settings (part of which is used for new windows in this session).
1.15      nicm      162:         *
1.67      nicm      163:         * tcgetattr() is used rather than using tty.tio since if the client is
                    164:         * detached, tty_open won't be called. It must be done before opening
                    165:         * the terminal as that calls tcsetattr() to prepare for tmux taking
                    166:         * over.
1.15      nicm      167:         */
1.48      nicm      168:        if (!detached && !already_attached && c->tty.fd != -1) {
1.126     nicm      169:                if (server_client_check_nested(cmdq_get_client(item))) {
1.92      nicm      170:                        cmdq_error(item, "sessions should be nested with care, "
1.67      nicm      171:                            "unset $TMUX to force");
1.116     nicm      172:                        goto fail;
1.67      nicm      173:                }
1.48      nicm      174:                if (tcgetattr(c->tty.fd, &tio) != 0)
1.15      nicm      175:                        fatal("tcgetattr failed");
1.19      nicm      176:                tiop = &tio;
1.15      nicm      177:        } else
1.19      nicm      178:                tiop = NULL;
1.15      nicm      179:
1.4       nicm      180:        /* Open the terminal if necessary. */
1.48      nicm      181:        if (!detached && !already_attached) {
1.57      nicm      182:                if (server_client_open(c, &cause) != 0) {
1.92      nicm      183:                        cmdq_error(item, "open terminal failed: %s", cause);
1.44      nicm      184:                        free(cause);
1.116     nicm      185:                        goto fail;
1.1       nicm      186:                }
                    187:        }
                    188:
1.114     nicm      189:        /* Get default session size. */
                    190:        if (args_has(args, 'x')) {
1.112     nicm      191:                tmp = args_get(args, 'x');
1.113     nicm      192:                if (strcmp(tmp, "-") == 0) {
                    193:                        if (c != NULL)
1.114     nicm      194:                                dsx = c->tty.sx;
1.118     nicm      195:                        else
                    196:                                dsx = 80;
1.113     nicm      197:                } else {
1.114     nicm      198:                        dsx = strtonum(tmp, 1, USHRT_MAX, &errstr);
1.112     nicm      199:                        if (errstr != NULL) {
                    200:                                cmdq_error(item, "width %s", errstr);
1.116     nicm      201:                                goto fail;
1.112     nicm      202:                        }
1.35      nicm      203:                }
1.124     nicm      204:        } else
                    205:                dsx = 80;
1.114     nicm      206:        if (args_has(args, 'y')) {
1.112     nicm      207:                tmp = args_get(args, 'y');
1.113     nicm      208:                if (strcmp(tmp, "-") == 0) {
                    209:                        if (c != NULL)
1.114     nicm      210:                                dsy = c->tty.sy;
1.118     nicm      211:                        else
                    212:                                dsy = 24;
1.113     nicm      213:                } else {
1.114     nicm      214:                        dsy = strtonum(tmp, 1, USHRT_MAX, &errstr);
1.112     nicm      215:                        if (errstr != NULL) {
                    216:                                cmdq_error(item, "height %s", errstr);
1.116     nicm      217:                                goto fail;
1.112     nicm      218:                        }
1.35      nicm      219:                }
1.124     nicm      220:        } else
                    221:                dsy = 24;
1.114     nicm      222:
                    223:        /* Find new session size. */
                    224:        if (!detached && !is_control) {
                    225:                sx = c->tty.sx;
                    226:                sy = c->tty.sy;
1.115     nicm      227:                if (sy > 0 && options_get_number(global_s_options, "status"))
1.114     nicm      228:                        sy--;
                    229:        } else {
1.116     nicm      230:                tmp = options_get_string(global_s_options, "default-size");
                    231:                if (sscanf(tmp, "%ux%u", &sx, &sy) != 2) {
1.114     nicm      232:                        sx = dsx;
                    233:                        sy = dsy;
1.124     nicm      234:                } else {
                    235:                        if (args_has(args, 'x'))
                    236:                                sx = dsx;
                    237:                        if (args_has(args, 'y'))
                    238:                                sy = dsy;
                    239:                }
1.114     nicm      240:        }
1.4       nicm      241:        if (sx == 0)
                    242:                sx = 1;
                    243:        if (sy == 0)
                    244:                sy = 1;
1.16      nicm      245:
1.116     nicm      246:        /* Create the new session. */
                    247:        oo = options_create(global_s_options);
1.119     nicm      248:        if (args_has(args, 'x') || args_has(args, 'y')) {
                    249:                if (!args_has(args, 'x'))
                    250:                        dsx = sx;
                    251:                if (!args_has(args, 'y'))
                    252:                        dsy = sy;
1.116     nicm      253:                options_set_string(oo, "default-size", 0, "%ux%u", dsx, dsy);
1.119     nicm      254:        }
1.75      nicm      255:        env = environ_create();
1.97      nicm      256:        if (c != NULL && !args_has(args, 'E'))
                    257:                environ_update(global_s_options, c->environ, env);
1.132   ! nicm      258:        add = args_first_value(args, 'e', &value);
        !           259:        while (add != NULL) {
        !           260:                environ_put(env, add, 0);
        !           261:                add = args_next_value(&value);
        !           262:        }
1.116     nicm      263:        s = session_create(prefix, newname, cwd, env, oo, tiop);
1.11      nicm      264:
1.116     nicm      265:        /* Spawn the initial window. */
                    266:        memset(&sc, 0, sizeof sc);
                    267:        sc.item = item;
                    268:        sc.s = s;
                    269:
                    270:        sc.name = args_get(args, 'n');
                    271:        sc.argc = args->argc;
                    272:        sc.argv = args->argv;
                    273:
                    274:        sc.idx = -1;
                    275:        sc.cwd = args_get(args, 'c');
                    276:
                    277:        sc.flags = 0;
1.114     nicm      278:
1.116     nicm      279:        if (spawn_window(&sc, &cause) == NULL) {
                    280:                session_destroy(s, 0, __func__);
                    281:                cmdq_error(item, "create window failed: %s", cause);
1.44      nicm      282:                free(cause);
1.116     nicm      283:                goto fail;
1.21      nicm      284:        }
                    285:
                    286:        /*
                    287:         * If a target session is given, this is to be part of a session group,
                    288:         * so add it to the group and synchronize.
                    289:         */
1.101     nicm      290:        if (group != NULL) {
                    291:                if (sg == NULL) {
                    292:                        if (groupwith != NULL) {
                    293:                                sg = session_group_new(groupwith->name);
                    294:                                session_group_add(sg, groupwith);
                    295:                        } else
                    296:                                sg = session_group_new(group);
                    297:                }
                    298:                session_group_add(sg, s);
1.21      nicm      299:                session_group_synchronize_to(s);
1.66      nicm      300:                session_select(s, RB_MIN(winlinks, &s->windows)->idx);
1.1       nicm      301:        }
1.94      nicm      302:        notify_session("session-created", s);
1.1       nicm      303:
1.16      nicm      304:        /*
                    305:         * Set the client to the new session. If a command client exists, it is
                    306:         * taking this session and needs to get MSG_READY and stay around.
1.4       nicm      307:         */
1.25      nicm      308:        if (!detached) {
1.73      nicm      309:                if (!already_attached) {
                    310:                        if (~c->flags & CLIENT_CONTROL)
                    311:                                proc_send(c->peer, MSG_READY, -1, NULL, 0);
                    312:                } else if (c->session != NULL)
1.48      nicm      313:                        c->last_session = c->session;
                    314:                c->session = s;
1.128     nicm      315:                if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
1.100     nicm      316:                        server_client_set_key_table(c, NULL);
1.114     nicm      317:                tty_update_client_offset(c);
1.69      nicm      318:                status_timer_start(c);
1.93      nicm      319:                notify_client("client-session-changed", c);
1.70      nicm      320:                session_update_activity(s, NULL);
1.71      nicm      321:                gettimeofday(&s->last_attached_time, NULL);
1.48      nicm      322:                server_redraw_client(c);
1.1       nicm      323:        }
                    324:        recalculate_sizes();
1.22      nicm      325:        server_update_socket();
1.26      nicm      326:
                    327:        /*
                    328:         * If there are still configuration file errors to display, put the new
                    329:         * session's current window into more mode and display them now.
                    330:         */
1.46      nicm      331:        if (cfg_finished)
1.48      nicm      332:                cfg_show_causes(s);
1.50      nicm      333:
                    334:        /* Print if requested. */
                    335:        if (args_has(args, 'P')) {
                    336:                if ((template = args_get(args, 'F')) == NULL)
                    337:                        template = NEW_SESSION_TEMPLATE;
1.123     nicm      338:                cp = format_single(item, template, c, s, s->curw, NULL);
1.92      nicm      339:                cmdq_print(item, "%s", cp);
1.50      nicm      340:                free(cp);
                    341:        }
1.89      nicm      342:
1.129     nicm      343:        if (!detached)
1.91      nicm      344:                c->flags |= CLIENT_ATTACHED;
1.129     nicm      345:        if (!args_has(args, 'd'))
1.126     nicm      346:                cmd_find_from_session(current, s, 0);
1.54      nicm      347:
1.109     nicm      348:        cmd_find_from_session(&fs, s, 0);
1.117     nicm      349:        cmdq_insert_hook(s, item, &fs, "after-new-session");
1.91      nicm      350:
1.110     nicm      351:        free(cwd);
                    352:        free(newname);
1.131     nicm      353:        free(prefix);
1.48      nicm      354:        return (CMD_RETURN_NORMAL);
1.54      nicm      355:
1.116     nicm      356: fail:
1.110     nicm      357:        free(cwd);
                    358:        free(newname);
1.131     nicm      359:        free(prefix);
1.54      nicm      360:        return (CMD_RETURN_ERROR);
1.1       nicm      361: }