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