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