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

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