15494e770Schristos /* $OpenBSD$ */
2698d5317Sjmmv
3698d5317Sjmmv /*
4ed4e6cd4Schristos * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5698d5317Sjmmv *
6698d5317Sjmmv * Permission to use, copy, modify, and distribute this software for any
7698d5317Sjmmv * purpose with or without fee is hereby granted, provided that the above
8698d5317Sjmmv * copyright notice and this permission notice appear in all copies.
9698d5317Sjmmv *
10698d5317Sjmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11698d5317Sjmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12698d5317Sjmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13698d5317Sjmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14698d5317Sjmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15698d5317Sjmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16698d5317Sjmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17698d5317Sjmmv */
18698d5317Sjmmv
19698d5317Sjmmv #include <sys/types.h>
20698d5317Sjmmv
21928fc495Schristos #include <errno.h>
22928fc495Schristos #include <fcntl.h>
23d530c4d0Sjmmv #include <stdlib.h>
24698d5317Sjmmv #include <string.h>
25698d5317Sjmmv #include <termios.h>
26698d5317Sjmmv #include <unistd.h>
27698d5317Sjmmv
28698d5317Sjmmv #include "tmux.h"
29698d5317Sjmmv
30698d5317Sjmmv /*
31698d5317Sjmmv * Create a new session and attach to the current terminal unless -d is given.
32698d5317Sjmmv */
33698d5317Sjmmv
345494e770Schristos #define NEW_SESSION_TEMPLATE "#{session_name}:"
355494e770Schristos
36e9a2d6faSchristos static enum cmd_retval cmd_new_session_exec(struct cmd *, struct cmdq_item *);
37698d5317Sjmmv
38698d5317Sjmmv const struct cmd_entry cmd_new_session_entry = {
39ed4e6cd4Schristos .name = "new-session",
40ed4e6cd4Schristos .alias = "new",
41ed4e6cd4Schristos
4246548964Swiz .args = { "Ac:dDe:EF:f:n:Ps:t:x:Xy:", 0, -1, NULL },
43e271dbb8Schristos .usage = "[-AdDEPX] [-c start-directory] [-e environment] [-F format] "
44e271dbb8Schristos "[-f flags] [-n window-name] [-s session-name] "
4546548964Swiz CMD_TARGET_SESSION_USAGE " [-x width] [-y height] "
4646548964Swiz "[shell-command]",
47ed4e6cd4Schristos
48fe99a117Schristos .target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL },
49ed4e6cd4Schristos
50ed4e6cd4Schristos .flags = CMD_STARTSERVER,
51ed4e6cd4Schristos .exec = cmd_new_session_exec
525494e770Schristos };
535494e770Schristos
545494e770Schristos const struct cmd_entry cmd_has_session_entry = {
55ed4e6cd4Schristos .name = "has-session",
56ed4e6cd4Schristos .alias = "has",
57ed4e6cd4Schristos
5846548964Swiz .args = { "t:", 0, 0, NULL },
59ed4e6cd4Schristos .usage = CMD_TARGET_SESSION_USAGE,
60ed4e6cd4Schristos
61fe99a117Schristos .target = { 't', CMD_FIND_SESSION, 0 },
62ed4e6cd4Schristos
63ed4e6cd4Schristos .flags = 0,
64ed4e6cd4Schristos .exec = cmd_new_session_exec
65698d5317Sjmmv };
66698d5317Sjmmv
67e9a2d6faSchristos static enum cmd_retval
cmd_new_session_exec(struct cmd * self,struct cmdq_item * item)68e9a2d6faSchristos cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
69698d5317Sjmmv {
70e271dbb8Schristos struct args *args = cmd_get_args(self);
71e271dbb8Schristos struct cmd_find_state *current = cmdq_get_current(item);
72e271dbb8Schristos struct cmd_find_state *target = cmdq_get_target(item);
73e271dbb8Schristos struct client *c = cmdq_get_client(item);
74e271dbb8Schristos struct session *s, *as, *groupwith = NULL;
75ed4e6cd4Schristos struct environ *env;
760a274e86Schristos struct options *oo;
77698d5317Sjmmv struct termios tio, *tiop;
78e271dbb8Schristos struct session_group *sg = NULL;
7946548964Swiz const char *errstr, *template, *group, *tmp;
8030744affSchristos char *cause, *cwd = NULL, *cp, *newname = NULL;
81e271dbb8Schristos char *name, *prefix = NULL;
8230744affSchristos int detached, already_attached, is_control = 0;
8346548964Swiz u_int sx, sy, dsx = 80, dsy = 24, count = args_count(args);
8446548964Swiz struct spawn_context sc = { 0 };
85c7e17de0Schristos enum cmd_retval retval;
8630744affSchristos struct cmd_find_state fs;
8746548964Swiz struct args_value *av;
885494e770Schristos
89e271dbb8Schristos if (cmd_get_entry(self) == &cmd_has_session_entry) {
90ed4e6cd4Schristos /*
91fe99a117Schristos * cmd_find_target() will fail if the session cannot be found,
92fe99a117Schristos * so always return success here.
93ed4e6cd4Schristos */
945494e770Schristos return (CMD_RETURN_NORMAL);
955494e770Schristos }
96928fc495Schristos
9746548964Swiz if (args_has(args, 't') && (count != 0 || args_has(args, 'n'))) {
98e9a2d6faSchristos cmdq_error(item, "command or window name given with target");
99928fc495Schristos return (CMD_RETURN_ERROR);
100928fc495Schristos }
101698d5317Sjmmv
10268e6ba84Schristos tmp = args_get(args, 's');
10368e6ba84Schristos if (tmp != NULL) {
104e271dbb8Schristos name = format_single(item, tmp, c, NULL, NULL, NULL);
105e271dbb8Schristos newname = session_check_name(name);
10646548964Swiz if (newname == NULL) {
10746548964Swiz cmdq_error(item, "invalid session: %s", name);
10846548964Swiz free(name);
10946548964Swiz return (CMD_RETURN_ERROR);
11046548964Swiz }
111e271dbb8Schristos free(name);
11268e6ba84Schristos }
113928fc495Schristos if (args_has(args, 'A')) {
11468e6ba84Schristos if (newname != NULL)
11568e6ba84Schristos as = session_find(newname);
11668e6ba84Schristos else
117e271dbb8Schristos as = target->s;
11868e6ba84Schristos if (as != NULL) {
11968e6ba84Schristos retval = cmd_attach_session(item, as->name,
12068e6ba84Schristos args_has(args, 'D'), args_has(args, 'X'), 0, NULL,
121e271dbb8Schristos args_has(args, 'E'), args_get(args, 'f'));
122c7e17de0Schristos free(newname);
123c7e17de0Schristos return (retval);
124928fc495Schristos }
12568e6ba84Schristos }
12668e6ba84Schristos if (newname != NULL && session_find(newname) != NULL) {
127e9a2d6faSchristos cmdq_error(item, "duplicate session: %s", newname);
12830744affSchristos goto fail;
129d530c4d0Sjmmv }
130d530c4d0Sjmmv
131e9a2d6faSchristos /* Is this going to be part of a session group? */
132e9a2d6faSchristos group = args_get(args, 't');
133e9a2d6faSchristos if (group != NULL) {
134e271dbb8Schristos groupwith = target->s;
135e271dbb8Schristos if (groupwith == NULL)
136e9a2d6faSchristos sg = session_group_find(group);
137e271dbb8Schristos else
138e9a2d6faSchristos sg = session_group_contains(groupwith);
139e9a2d6faSchristos if (sg != NULL)
140e271dbb8Schristos prefix = xstrdup(sg->name);
141e9a2d6faSchristos else if (groupwith != NULL)
142e271dbb8Schristos prefix = xstrdup(groupwith->name);
14346548964Swiz else {
144e271dbb8Schristos prefix = session_check_name(group);
14546548964Swiz if (prefix == NULL) {
14646548964Swiz cmdq_error(item, "invalid session group: %s",
14746548964Swiz group);
14846548964Swiz goto fail;
14946548964Swiz }
15046548964Swiz }
151e9a2d6faSchristos }
152698d5317Sjmmv
153698d5317Sjmmv /* Set -d if no client. */
154d530c4d0Sjmmv detached = args_has(args, 'd');
155928fc495Schristos if (c == NULL)
156698d5317Sjmmv detached = 1;
157fe99a117Schristos else if (c->flags & CLIENT_CONTROL)
158fe99a117Schristos is_control = 1;
159698d5317Sjmmv
160928fc495Schristos /* Is this client already attached? */
161928fc495Schristos already_attached = 0;
162928fc495Schristos if (c != NULL && c->session != NULL)
163928fc495Schristos already_attached = 1;
164928fc495Schristos
165928fc495Schristos /* Get the new session working directory. */
166c7e17de0Schristos if ((tmp = args_get(args, 'c')) != NULL)
167c7e17de0Schristos cwd = format_single(item, tmp, c, NULL, NULL, NULL);
168ed4e6cd4Schristos else
169c7e17de0Schristos cwd = xstrdup(server_client_get_cwd(c, NULL));
170928fc495Schristos
171698d5317Sjmmv /*
1725494e770Schristos * If this is a new client, check for nesting and save the termios
1735494e770Schristos * settings (part of which is used for new windows in this session).
174698d5317Sjmmv *
1755494e770Schristos * tcgetattr() is used rather than using tty.tio since if the client is
1765494e770Schristos * detached, tty_open won't be called. It must be done before opening
1775494e770Schristos * the terminal as that calls tcsetattr() to prepare for tmux taking
1785494e770Schristos * over.
179698d5317Sjmmv */
180e271dbb8Schristos if (!detached &&
181e271dbb8Schristos !already_attached &&
182e271dbb8Schristos c->fd != -1 &&
183e271dbb8Schristos (~c->flags & CLIENT_CONTROL)) {
184e271dbb8Schristos if (server_client_check_nested(cmdq_get_client(item))) {
185e9a2d6faSchristos cmdq_error(item, "sessions should be nested with care, "
1865494e770Schristos "unset $TMUX to force");
18730744affSchristos goto fail;
1885494e770Schristos }
189e271dbb8Schristos if (tcgetattr(c->fd, &tio) != 0)
190698d5317Sjmmv fatal("tcgetattr failed");
191698d5317Sjmmv tiop = &tio;
192698d5317Sjmmv } else
193698d5317Sjmmv tiop = NULL;
194698d5317Sjmmv
195698d5317Sjmmv /* Open the terminal if necessary. */
196928fc495Schristos if (!detached && !already_attached) {
1975494e770Schristos if (server_client_open(c, &cause) != 0) {
198e9a2d6faSchristos cmdq_error(item, "open terminal failed: %s", cause);
199928fc495Schristos free(cause);
20030744affSchristos goto fail;
201698d5317Sjmmv }
202698d5317Sjmmv }
203698d5317Sjmmv
2040a274e86Schristos /* Get default session size. */
2050a274e86Schristos if (args_has(args, 'x')) {
206c7e17de0Schristos tmp = args_get(args, 'x');
207c7e17de0Schristos if (strcmp(tmp, "-") == 0) {
208c7e17de0Schristos if (c != NULL)
2090a274e86Schristos dsx = c->tty.sx;
21030744affSchristos else
21130744affSchristos dsx = 80;
212c7e17de0Schristos } else {
2130a274e86Schristos dsx = strtonum(tmp, 1, USHRT_MAX, &errstr);
214d530c4d0Sjmmv if (errstr != NULL) {
215e9a2d6faSchristos cmdq_error(item, "width %s", errstr);
21630744affSchristos goto fail;
217d530c4d0Sjmmv }
218d530c4d0Sjmmv }
219e271dbb8Schristos } else
220e271dbb8Schristos dsx = 80;
2210a274e86Schristos if (args_has(args, 'y')) {
222c7e17de0Schristos tmp = args_get(args, 'y');
223c7e17de0Schristos if (strcmp(tmp, "-") == 0) {
224c7e17de0Schristos if (c != NULL)
2250a274e86Schristos dsy = c->tty.sy;
22630744affSchristos else
22730744affSchristos dsy = 24;
228c7e17de0Schristos } else {
2290a274e86Schristos dsy = strtonum(tmp, 1, USHRT_MAX, &errstr);
230d530c4d0Sjmmv if (errstr != NULL) {
231e9a2d6faSchristos cmdq_error(item, "height %s", errstr);
23230744affSchristos goto fail;
233d530c4d0Sjmmv }
234d530c4d0Sjmmv }
235e271dbb8Schristos } else
236e271dbb8Schristos dsy = 24;
2370a274e86Schristos
2380a274e86Schristos /* Find new session size. */
2390a274e86Schristos if (!detached && !is_control) {
2400a274e86Schristos sx = c->tty.sx;
2410a274e86Schristos sy = c->tty.sy;
2420a274e86Schristos if (sy > 0 && options_get_number(global_s_options, "status"))
2430a274e86Schristos sy--;
2440a274e86Schristos } else {
24530744affSchristos tmp = options_get_string(global_s_options, "default-size");
24630744affSchristos if (sscanf(tmp, "%ux%u", &sx, &sy) != 2) {
247e271dbb8Schristos sx = dsx;
248e271dbb8Schristos sy = dsy;
249e271dbb8Schristos } else {
2500a274e86Schristos if (args_has(args, 'x'))
2510a274e86Schristos sx = dsx;
2520a274e86Schristos if (args_has(args, 'y'))
2530a274e86Schristos sy = dsy;
2540a274e86Schristos }
255e271dbb8Schristos }
256698d5317Sjmmv if (sx == 0)
257698d5317Sjmmv sx = 1;
258698d5317Sjmmv if (sy == 0)
259698d5317Sjmmv sy = 1;
260698d5317Sjmmv
26130744affSchristos /* Create the new session. */
26230744affSchristos oo = options_create(global_s_options);
26330744affSchristos if (args_has(args, 'x') || args_has(args, 'y')) {
26430744affSchristos if (!args_has(args, 'x'))
26530744affSchristos dsx = sx;
26630744affSchristos if (!args_has(args, 'y'))
26730744affSchristos dsy = sy;
26830744affSchristos options_set_string(oo, "default-size", 0, "%ux%u", dsx, dsy);
2695494e770Schristos }
270ed4e6cd4Schristos env = environ_create();
271e9a2d6faSchristos if (c != NULL && !args_has(args, 'E'))
272e9a2d6faSchristos environ_update(global_s_options, c->environ, env);
27346548964Swiz av = args_first_value(args, 'e');
27446548964Swiz while (av != NULL) {
27546548964Swiz environ_put(env, av->string, 0);
27646548964Swiz av = args_next_value(av);
277e271dbb8Schristos }
27830744affSchristos s = session_create(prefix, newname, cwd, env, oo, tiop);
279698d5317Sjmmv
28030744affSchristos /* Spawn the initial window. */
28130744affSchristos sc.item = item;
28230744affSchristos sc.s = s;
28346548964Swiz if (!detached)
28459b94b2cSchristos sc.tc = c;
2850a274e86Schristos
28630744affSchristos sc.name = args_get(args, 'n');
28746548964Swiz args_to_vector(args, &sc.argc, &sc.argv);
28830744affSchristos
28930744affSchristos sc.idx = -1;
29030744affSchristos sc.cwd = args_get(args, 'c');
29130744affSchristos
29230744affSchristos sc.flags = 0;
29330744affSchristos
29430744affSchristos if (spawn_window(&sc, &cause) == NULL) {
29530744affSchristos session_destroy(s, 0, __func__);
29630744affSchristos cmdq_error(item, "create window failed: %s", cause);
297928fc495Schristos free(cause);
29830744affSchristos goto fail;
299698d5317Sjmmv }
300698d5317Sjmmv
301698d5317Sjmmv /*
302698d5317Sjmmv * If a target session is given, this is to be part of a session group,
303698d5317Sjmmv * so add it to the group and synchronize.
304698d5317Sjmmv */
305e9a2d6faSchristos if (group != NULL) {
306e9a2d6faSchristos if (sg == NULL) {
307698d5317Sjmmv if (groupwith != NULL) {
308e9a2d6faSchristos sg = session_group_new(groupwith->name);
309e9a2d6faSchristos session_group_add(sg, groupwith);
310e9a2d6faSchristos } else
311e9a2d6faSchristos sg = session_group_new(group);
312e9a2d6faSchristos }
313e9a2d6faSchristos session_group_add(sg, s);
314698d5317Sjmmv session_group_synchronize_to(s);
3155494e770Schristos session_select(s, RB_MIN(winlinks, &s->windows)->idx);
316698d5317Sjmmv }
317e9a2d6faSchristos notify_session("session-created", s);
318698d5317Sjmmv
319698d5317Sjmmv /*
320698d5317Sjmmv * Set the client to the new session. If a command client exists, it is
321698d5317Sjmmv * taking this session and needs to get MSG_READY and stay around.
322698d5317Sjmmv */
323698d5317Sjmmv if (!detached) {
324e271dbb8Schristos if (args_has(args, 'f'))
325e271dbb8Schristos server_client_set_flags(c, args_get(args, 'f'));
326ed4e6cd4Schristos if (!already_attached) {
327ed4e6cd4Schristos if (~c->flags & CLIENT_CONTROL)
328ed4e6cd4Schristos proc_send(c->peer, MSG_READY, -1, NULL, 0);
329ed4e6cd4Schristos } else if (c->session != NULL)
330928fc495Schristos c->last_session = c->session;
33146548964Swiz server_client_set_session(c, s);
332e271dbb8Schristos if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
333ed4e6cd4Schristos server_client_set_key_table(c, NULL);
334698d5317Sjmmv }
335698d5317Sjmmv
336928fc495Schristos /* Print if requested. */
337928fc495Schristos if (args_has(args, 'P')) {
338928fc495Schristos if ((template = args_get(args, 'F')) == NULL)
339928fc495Schristos template = NEW_SESSION_TEMPLATE;
34068e6ba84Schristos cp = format_single(item, template, c, s, s->curw, NULL);
341e9a2d6faSchristos cmdq_print(item, "%s", cp);
342928fc495Schristos free(cp);
343698d5317Sjmmv }
344698d5317Sjmmv
345e271dbb8Schristos if (!detached)
346e9a2d6faSchristos c->flags |= CLIENT_ATTACHED;
347e271dbb8Schristos if (!args_has(args, 'd'))
348e271dbb8Schristos cmd_find_from_session(current, s, 0);
349928fc495Schristos
350fe99a117Schristos cmd_find_from_session(&fs, s, 0);
35130744affSchristos cmdq_insert_hook(s, item, &fs, "after-new-session");
352e9a2d6faSchristos
353*f844e94eSwiz if (cfg_finished)
354*f844e94eSwiz cfg_show_causes(s);
355*f844e94eSwiz
35646548964Swiz if (sc.argv != NULL)
35746548964Swiz cmd_free_argv(sc.argc, sc.argv);
358c7e17de0Schristos free(cwd);
359c7e17de0Schristos free(newname);
360e271dbb8Schristos free(prefix);
361928fc495Schristos return (CMD_RETURN_NORMAL);
362928fc495Schristos
36330744affSchristos fail:
36446548964Swiz if (sc.argv != NULL)
36546548964Swiz cmd_free_argv(sc.argc, sc.argv);
366c7e17de0Schristos free(cwd);
367c7e17de0Schristos free(newname);
368e271dbb8Schristos free(prefix);
369928fc495Schristos return (CMD_RETURN_ERROR);
370698d5317Sjmmv }
371