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