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

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