Annotation of src/usr.bin/tmux/cmd-new-session.c, Revision 1.111
1.111 ! nicm 1: /* $OpenBSD: cmd-new-session.c,v 1.110 2018/03/01 12:53:08 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:
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.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.50 nicm 69: struct args *args = self->args;
1.92 nicm 70: struct client *c = item->client;
1.101 nicm 71: struct session *s, *as, *groupwith;
1.50 nicm 72: struct window *w;
1.75 nicm 73: struct environ *env;
1.50 nicm 74: struct termios tio, *tiop;
1.101 nicm 75: struct session_group *sg;
1.110 nicm 76: const char *errstr, *template, *group, *prefix;
77: const char *path, *cmd, *tmp;
78: char **argv, *cause, *cp, *newname, *cwd = NULL;
1.76 nicm 79: int detached, already_attached, idx, argc;
1.107 nicm 80: int is_control = 0;
1.50 nicm 81: u_int sx, sy;
1.59 nicm 82: struct environ_entry *envent;
1.90 nicm 83: struct cmd_find_state fs;
1.110 nicm 84: enum cmd_retval retval;
1.62 nicm 85:
86: if (self->entry == &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.110 nicm 99: newname = NULL;
100: if (args_has(args, 's')) {
101: newname = format_single(item, args_get(args, 's'), c, NULL,
102: NULL, NULL);
1.38 nicm 103: if (!session_check_name(newname)) {
1.92 nicm 104: cmdq_error(item, "bad session name: %s", newname);
1.110 nicm 105: goto error;
1.38 nicm 106: }
1.86 nicm 107: if ((as = session_find(newname)) != NULL) {
1.49 nicm 108: if (args_has(args, 'A')) {
1.110 nicm 109: retval = cmd_attach_session(item,
1.106 nicm 110: newname, args_has(args, 'D'),
1.110 nicm 111: 0, NULL, args_has(args, 'E'));
112: free(newname);
113: return (retval);
1.49 nicm 114: }
1.92 nicm 115: cmdq_error(item, "duplicate session: %s", newname);
1.110 nicm 116: goto error;
1.38 nicm 117: }
1.4 nicm 118: }
1.1 nicm 119:
1.101 nicm 120: /* Is this going to be part of a session group? */
121: group = args_get(args, 't');
122: if (group != NULL) {
1.105 nicm 123: groupwith = item->target.s;
1.85 nicm 124: if (groupwith == NULL) {
1.101 nicm 125: if (!session_check_name(group)) {
126: cmdq_error(item, "bad group name: %s", group);
127: goto error;
128: }
129: sg = session_group_find(group);
130: } else
131: sg = session_group_contains(groupwith);
132: if (sg != NULL)
133: prefix = sg->name;
134: else if (groupwith != NULL)
135: prefix = groupwith->name;
136: else
137: prefix = group;
138: } else {
1.34 nicm 139: groupwith = NULL;
1.101 nicm 140: sg = NULL;
141: prefix = NULL;
142: }
1.21 nicm 143:
1.6 nicm 144: /* Set -d if no client. */
1.34 nicm 145: detached = args_has(args, 'd');
1.48 nicm 146: if (c == NULL)
1.6 nicm 147: detached = 1;
1.107 nicm 148: else if (c->flags & CLIENT_CONTROL)
149: is_control = 1;
1.6 nicm 150:
1.48 nicm 151: /* Is this client already attached? */
152: already_attached = 0;
153: if (c != NULL && c->session != NULL)
154: already_attached = 1;
155:
1.54 nicm 156: /* Get the new session working directory. */
1.110 nicm 157: if ((tmp = args_get(args, 'c')) != NULL)
158: cwd = format_single(item, tmp, c, NULL, NULL, NULL);
1.76 nicm 159: else
1.111 ! nicm 160: cwd = xstrdup(server_client_get_cwd(c, NULL));
1.54 nicm 161:
1.15 nicm 162: /*
1.67 nicm 163: * If this is a new client, check for nesting and save the termios
164: * settings (part of which is used for new windows in this session).
1.15 nicm 165: *
1.67 nicm 166: * tcgetattr() is used rather than using tty.tio since if the client is
167: * detached, tty_open won't be called. It must be done before opening
168: * the terminal as that calls tcsetattr() to prepare for tmux taking
169: * over.
1.15 nicm 170: */
1.48 nicm 171: if (!detached && !already_attached && c->tty.fd != -1) {
1.92 nicm 172: if (server_client_check_nested(item->client)) {
173: cmdq_error(item, "sessions should be nested with care, "
1.67 nicm 174: "unset $TMUX to force");
175: return (CMD_RETURN_ERROR);
176: }
1.48 nicm 177: if (tcgetattr(c->tty.fd, &tio) != 0)
1.15 nicm 178: fatal("tcgetattr failed");
1.19 nicm 179: tiop = &tio;
1.15 nicm 180: } else
1.19 nicm 181: tiop = NULL;
1.15 nicm 182:
1.4 nicm 183: /* Open the terminal if necessary. */
1.48 nicm 184: if (!detached && !already_attached) {
1.57 nicm 185: if (server_client_open(c, &cause) != 0) {
1.92 nicm 186: cmdq_error(item, "open terminal failed: %s", cause);
1.44 nicm 187: free(cause);
1.54 nicm 188: goto error;
1.1 nicm 189: }
190: }
191:
1.16 nicm 192: /* Find new session size. */
1.107 nicm 193: if (!detached) {
1.48 nicm 194: sx = c->tty.sx;
195: sy = c->tty.sy;
1.107 nicm 196: if (!is_control &&
197: sy > 0 &&
198: options_get_number(global_s_options, "status"))
199: sy--;
1.39 nicm 200: } else {
1.4 nicm 201: sx = 80;
1.18 nicm 202: sy = 24;
1.39 nicm 203: }
1.107 nicm 204: if ((is_control || detached) && args_has(args, 'x')) {
1.48 nicm 205: sx = strtonum(args_get(args, 'x'), 1, USHRT_MAX, &errstr);
206: if (errstr != NULL) {
1.92 nicm 207: cmdq_error(item, "width %s", errstr);
1.54 nicm 208: goto error;
1.35 nicm 209: }
1.48 nicm 210: }
1.107 nicm 211: if ((is_control || detached) && args_has(args, 'y')) {
1.48 nicm 212: sy = strtonum(args_get(args, 'y'), 1, USHRT_MAX, &errstr);
213: if (errstr != NULL) {
1.92 nicm 214: cmdq_error(item, "height %s", errstr);
1.54 nicm 215: goto error;
1.35 nicm 216: }
1.1 nicm 217: }
1.4 nicm 218: if (sx == 0)
219: sx = 1;
220: if (sy == 0)
221: sy = 1;
1.16 nicm 222:
223: /* Figure out the command for the new window. */
1.60 nicm 224: argc = -1;
225: argv = NULL;
1.81 nicm 226: if (!args_has(args, 't') && args->argc != 0) {
1.60 nicm 227: argc = args->argc;
228: argv = args->argv;
1.101 nicm 229: } else if (sg == NULL && groupwith == NULL) {
1.74 nicm 230: cmd = options_get_string(global_s_options, "default-command");
1.60 nicm 231: if (cmd != NULL && *cmd != '\0') {
232: argc = 1;
1.96 nicm 233: argv = (char **)&cmd;
1.60 nicm 234: } else {
235: argc = 0;
236: argv = NULL;
237: }
238: }
1.1 nicm 239:
1.59 nicm 240: path = NULL;
241: if (c != NULL && c->session == NULL)
1.75 nicm 242: envent = environ_find(c->environ, "PATH");
1.59 nicm 243: else
1.75 nicm 244: envent = environ_find(global_environ, "PATH");
1.59 nicm 245: if (envent != NULL)
246: path = envent->value;
247:
1.10 nicm 248: /* Construct the environment. */
1.75 nicm 249: env = environ_create();
1.97 nicm 250: if (c != NULL && !args_has(args, 'E'))
251: environ_update(global_s_options, c->environ, env);
1.11 nicm 252:
1.4 nicm 253: /* Create the new session. */
1.74 nicm 254: idx = -1 - options_get_number(global_s_options, "base-index");
1.101 nicm 255: s = session_create(prefix, newname, argc, argv, path, cwd, env, tiop,
256: idx, sx, sy, &cause);
1.75 nicm 257: environ_free(env);
1.1 nicm 258: if (s == NULL) {
1.92 nicm 259: cmdq_error(item, "create session failed: %s", cause);
1.44 nicm 260: free(cause);
1.54 nicm 261: goto error;
1.1 nicm 262: }
1.4 nicm 263:
1.16 nicm 264: /* Set the initial window name if one given. */
1.110 nicm 265: if (argc >= 0 && (tmp = args_get(args, 'n')) != NULL) {
266: cp = format_single(item, tmp, c, s, NULL, NULL);
1.16 nicm 267: w = s->curw->window;
1.110 nicm 268: window_set_name(w, cp);
1.74 nicm 269: options_set_number(w->options, "automatic-rename", 0);
1.110 nicm 270: free(cp);
1.21 nicm 271: }
272:
273: /*
274: * If a target session is given, this is to be part of a session group,
275: * so add it to the group and synchronize.
276: */
1.101 nicm 277: if (group != NULL) {
278: if (sg == NULL) {
279: if (groupwith != NULL) {
280: sg = session_group_new(groupwith->name);
281: session_group_add(sg, groupwith);
282: } else
283: sg = session_group_new(group);
284: }
285: session_group_add(sg, s);
1.21 nicm 286: session_group_synchronize_to(s);
1.66 nicm 287: session_select(s, RB_MIN(winlinks, &s->windows)->idx);
1.1 nicm 288: }
1.94 nicm 289: notify_session("session-created", s);
1.1 nicm 290:
1.16 nicm 291: /*
292: * Set the client to the new session. If a command client exists, it is
293: * taking this session and needs to get MSG_READY and stay around.
1.4 nicm 294: */
1.25 nicm 295: if (!detached) {
1.73 nicm 296: if (!already_attached) {
297: if (~c->flags & CLIENT_CONTROL)
298: proc_send(c->peer, MSG_READY, -1, NULL, 0);
299: } else if (c->session != NULL)
1.48 nicm 300: c->last_session = c->session;
301: c->session = s;
1.103 nicm 302: if (~item->shared->flags & CMDQ_SHARED_REPEAT)
1.100 nicm 303: server_client_set_key_table(c, NULL);
1.69 nicm 304: status_timer_start(c);
1.93 nicm 305: notify_client("client-session-changed", c);
1.70 nicm 306: session_update_activity(s, NULL);
1.71 nicm 307: gettimeofday(&s->last_attached_time, NULL);
1.48 nicm 308: server_redraw_client(c);
1.1 nicm 309: }
310: recalculate_sizes();
1.22 nicm 311: server_update_socket();
1.26 nicm 312:
313: /*
314: * If there are still configuration file errors to display, put the new
315: * session's current window into more mode and display them now.
316: */
1.46 nicm 317: if (cfg_finished)
1.48 nicm 318: cfg_show_causes(s);
1.50 nicm 319:
320: /* Print if requested. */
321: if (args_has(args, 'P')) {
322: if ((template = args_get(args, 'F')) == NULL)
323: template = NEW_SESSION_TEMPLATE;
1.102 nicm 324: cp = format_single(item, template, c, s, NULL, NULL);
1.92 nicm 325: cmdq_print(item, "%s", cp);
1.50 nicm 326: free(cp);
327: }
1.89 nicm 328:
1.104 nicm 329: if (!detached) {
1.91 nicm 330: c->flags |= CLIENT_ATTACHED;
1.109 nicm 331: cmd_find_from_session(&item->shared->current, s, 0);
1.104 nicm 332: }
1.54 nicm 333:
1.109 nicm 334: cmd_find_from_session(&fs, s, 0);
1.92 nicm 335: hooks_insert(s->hooks, item, &fs, "after-new-session");
1.91 nicm 336:
1.110 nicm 337: free(cwd);
338: free(newname);
1.48 nicm 339: return (CMD_RETURN_NORMAL);
1.54 nicm 340:
341: error:
1.110 nicm 342: free(cwd);
343: free(newname);
1.54 nicm 344: return (CMD_RETURN_ERROR);
1.1 nicm 345: }