Fix bind-key -t.
[tmux-openbsd.git] / cmd-new-session.c
blob1aa1c087a34ac4ca16337425172dcda368d6d3ca
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <pwd.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <termios.h>
25 #include <unistd.h>
27 #include "tmux.h"
30 * Create a new session and attach to the current terminal unless -d is given.
33 int cmd_new_session_check(struct args *);
34 int cmd_new_session_exec(struct cmd *, struct cmd_ctx *);
36 const struct cmd_entry cmd_new_session_entry = {
37 "new-session", "new",
38 "dn:s:t:x:y:", 0, 1,
39 "[-d] [-n window-name] [-s session-name] [-t target-session] "
40 "[-x width] [-y height] [command]",
41 CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON,
42 NULL,
43 cmd_new_session_check,
44 cmd_new_session_exec
47 int
48 cmd_new_session_check(struct args *args)
50 if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n')))
51 return (-1);
52 if (!args_has(args, 'd') &&
53 (args_has(args, 'x') || args_has(args, 'y')))
54 return (-1);
55 return (0);
58 int
59 cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
61 struct args *args = self->args;
62 struct session *s, *old_s, *groupwith;
63 struct window *w;
64 struct window_pane *wp;
65 struct environ env;
66 struct termios tio, *tiop;
67 struct passwd *pw;
68 const char *newname, *target, *update, *cwd, *errstr;
69 char *overrides, *cmd, *cause;
70 int detached, idx;
71 u_int sx, sy, i;
73 newname = args_get(args, 's');
74 if (newname != NULL && session_find(newname) != NULL) {
75 ctx->error(ctx, "duplicate session: %s", newname);
76 return (-1);
79 target = args_get(args, 't');
80 if (target != NULL) {
81 groupwith = cmd_find_session(ctx, target);
82 if (groupwith == NULL)
83 return (-1);
84 } else
85 groupwith = NULL;
88 * There are three cases:
90 * 1. If cmdclient is non-NULL, new-session has been called from the
91 * command-line - cmdclient is to become a new attached, interactive
92 * client. Unless -d is given, the terminal must be opened and then
93 * the client sent MSG_READY.
95 * 2. If cmdclient is NULL, new-session has been called from an
96 * existing client (such as a key binding).
98 * 3. Both are NULL, the command was in the configuration file. Treat
99 * this as if -d was given even if it was not.
101 * In all cases, a new additional session needs to be created and
102 * (unless -d) set as the current session for the client.
105 /* Set -d if no client. */
106 detached = args_has(args, 'd');
107 if (ctx->cmdclient == NULL && ctx->curclient == NULL)
108 detached = 1;
111 * Save the termios settings, part of which is used for new windows in
112 * this session.
114 * This is read again with tcgetattr() rather than using tty.tio as if
115 * detached, tty_open won't be called. Because of this, it must be done
116 * before opening the terminal as that calls tcsetattr() to prepare for
117 * tmux taking over.
119 if (ctx->cmdclient != NULL && ctx->cmdclient->tty.fd != -1) {
120 if (tcgetattr(ctx->cmdclient->tty.fd, &tio) != 0)
121 fatal("tcgetattr failed");
122 tiop = &tio;
123 } else
124 tiop = NULL;
126 /* Open the terminal if necessary. */
127 if (!detached && ctx->cmdclient != NULL) {
128 if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
129 ctx->error(ctx, "not a terminal");
130 return (-1);
133 overrides =
134 options_get_string(&global_s_options, "terminal-overrides");
135 if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
136 ctx->error(ctx, "open terminal failed: %s", cause);
137 xfree(cause);
138 return (-1);
142 /* Get the new session working directory. */
143 if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
144 cwd = ctx->cmdclient->cwd;
145 else {
146 pw = getpwuid(getuid());
147 if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
148 cwd = pw->pw_dir;
149 else
150 cwd = "/";
153 /* Find new session size. */
154 if (detached) {
155 sx = 80;
156 sy = 24;
157 if (args_has(args, 'x')) {
158 sx = strtonum(
159 args_get(args, 'x'), 1, USHRT_MAX, &errstr);
160 if (errstr != NULL) {
161 ctx->error(ctx, "width %s", errstr);
162 return (-1);
165 if (args_has(args, 'y')) {
166 sy = strtonum(
167 args_get(args, 'y'), 1, USHRT_MAX, &errstr);
168 if (errstr != NULL) {
169 ctx->error(ctx, "height %s", errstr);
170 return (-1);
173 } else if (ctx->cmdclient != NULL) {
174 sx = ctx->cmdclient->tty.sx;
175 sy = ctx->cmdclient->tty.sy;
176 } else {
177 sx = ctx->curclient->tty.sx;
178 sy = ctx->curclient->tty.sy;
180 if (sy > 0 && options_get_number(&global_s_options, "status"))
181 sy--;
182 if (sx == 0)
183 sx = 1;
184 if (sy == 0)
185 sy = 1;
187 /* Figure out the command for the new window. */
188 if (target != NULL)
189 cmd = NULL;
190 else if (args->argc != 0)
191 cmd = args->argv[0];
192 else
193 cmd = options_get_string(&global_s_options, "default-command");
195 /* Construct the environment. */
196 environ_init(&env);
197 update = options_get_string(&global_s_options, "update-environment");
198 if (ctx->cmdclient != NULL)
199 environ_update(update, &ctx->cmdclient->environ, &env);
201 /* Create the new session. */
202 idx = -1 - options_get_number(&global_s_options, "base-index");
203 s = session_create(newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause);
204 if (s == NULL) {
205 ctx->error(ctx, "create session failed: %s", cause);
206 xfree(cause);
207 return (-1);
209 environ_free(&env);
211 /* Set the initial window name if one given. */
212 if (cmd != NULL && args_has(args, 'n')) {
213 w = s->curw->window;
215 xfree(w->name);
216 w->name = xstrdup(args_get(args, 'n'));
218 options_set_number(&w->options, "automatic-rename", 0);
222 * If a target session is given, this is to be part of a session group,
223 * so add it to the group and synchronize.
225 if (groupwith != NULL) {
226 session_group_add(groupwith, s);
227 session_group_synchronize_to(s);
228 session_select(s, RB_ROOT(&s->windows)->idx);
232 * Set the client to the new session. If a command client exists, it is
233 * taking this session and needs to get MSG_READY and stay around.
235 if (!detached) {
236 if (ctx->cmdclient != NULL) {
237 server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
239 old_s = ctx->cmdclient->session;
240 if (old_s != NULL)
241 ctx->cmdclient->last_session = old_s;
242 ctx->cmdclient->session = s;
243 session_update_activity(s);
244 server_redraw_client(ctx->cmdclient);
245 } else {
246 old_s = ctx->curclient->session;
247 if (old_s != NULL)
248 ctx->curclient->last_session = old_s;
249 ctx->curclient->session = s;
250 session_update_activity(s);
251 server_redraw_client(ctx->curclient);
254 recalculate_sizes();
255 server_update_socket();
258 * If there are still configuration file errors to display, put the new
259 * session's current window into more mode and display them now.
261 if (cfg_finished && !ARRAY_EMPTY(&cfg_causes)) {
262 wp = s->curw->window->active;
263 window_pane_set_mode(wp, &window_copy_mode);
264 window_copy_init_for_output(wp);
265 for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
266 cause = ARRAY_ITEM(&cfg_causes, i);
267 window_copy_add(wp, "%s", cause);
268 xfree(cause);
270 ARRAY_FREE(&cfg_causes);
273 return (!detached); /* 1 means don't tell command client to exit */