Size on split-window is -l not -s. Doh.
[tmux-openbsd.git] / cmd-new-session.c
blobf5e1316f038e8c8e60dcee325dc8e590482521da
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 return (0);
55 int
56 cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
58 struct args *args = self->args;
59 struct session *s, *old_s, *groupwith;
60 struct window *w;
61 struct window_pane *wp;
62 struct environ env;
63 struct termios tio, *tiop;
64 struct passwd *pw;
65 const char *newname, *target, *update, *cwd, *errstr;
66 char *overrides, *cmd, *cause;
67 int detached, idx;
68 u_int sx, sy, i;
70 newname = args_get(args, 's');
71 if (newname != NULL && session_find(newname) != NULL) {
72 ctx->error(ctx, "duplicate session: %s", newname);
73 return (-1);
76 target = args_get(args, 't');
77 if (target != NULL) {
78 groupwith = cmd_find_session(ctx, target);
79 if (groupwith == NULL)
80 return (-1);
81 } else
82 groupwith = NULL;
85 * There are three cases:
87 * 1. If cmdclient is non-NULL, new-session has been called from the
88 * command-line - cmdclient is to become a new attached, interactive
89 * client. Unless -d is given, the terminal must be opened and then
90 * the client sent MSG_READY.
92 * 2. If cmdclient is NULL, new-session has been called from an
93 * existing client (such as a key binding).
95 * 3. Both are NULL, the command was in the configuration file. Treat
96 * this as if -d was given even if it was not.
98 * In all cases, a new additional session needs to be created and
99 * (unless -d) set as the current session for the client.
102 /* Set -d if no client. */
103 detached = args_has(args, 'd');
104 if (ctx->cmdclient == NULL && ctx->curclient == NULL)
105 detached = 1;
108 * Save the termios settings, part of which is used for new windows in
109 * this session.
111 * This is read again with tcgetattr() rather than using tty.tio as if
112 * detached, tty_open won't be called. Because of this, it must be done
113 * before opening the terminal as that calls tcsetattr() to prepare for
114 * tmux taking over.
116 if (ctx->cmdclient != NULL && ctx->cmdclient->tty.fd != -1) {
117 if (tcgetattr(ctx->cmdclient->tty.fd, &tio) != 0)
118 fatal("tcgetattr failed");
119 tiop = &tio;
120 } else
121 tiop = NULL;
123 /* Open the terminal if necessary. */
124 if (!detached && ctx->cmdclient != NULL) {
125 if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
126 ctx->error(ctx, "not a terminal");
127 return (-1);
130 overrides =
131 options_get_string(&global_s_options, "terminal-overrides");
132 if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
133 ctx->error(ctx, "open terminal failed: %s", cause);
134 xfree(cause);
135 return (-1);
139 /* Get the new session working directory. */
140 if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
141 cwd = ctx->cmdclient->cwd;
142 else {
143 pw = getpwuid(getuid());
144 if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
145 cwd = pw->pw_dir;
146 else
147 cwd = "/";
150 /* Find new session size. */
151 if (detached) {
152 sx = 80;
153 sy = 24;
154 if (args_has(args, 'x')) {
155 sx = strtonum(
156 args_get(args, 'x'), 1, USHRT_MAX, &errstr);
157 if (errstr != NULL) {
158 ctx->error(ctx, "width %s", errstr);
159 return (-1);
162 if (args_has(args, 'y')) {
163 sy = strtonum(
164 args_get(args, 'y'), 1, USHRT_MAX, &errstr);
165 if (errstr != NULL) {
166 ctx->error(ctx, "height %s", errstr);
167 return (-1);
170 } else if (ctx->cmdclient != NULL) {
171 sx = ctx->cmdclient->tty.sx;
172 sy = ctx->cmdclient->tty.sy;
173 } else {
174 sx = ctx->curclient->tty.sx;
175 sy = ctx->curclient->tty.sy;
177 if (sy > 0 && options_get_number(&global_s_options, "status"))
178 sy--;
179 if (sx == 0)
180 sx = 1;
181 if (sy == 0)
182 sy = 1;
184 /* Figure out the command for the new window. */
185 if (target != NULL)
186 cmd = NULL;
187 else if (args->argc != 0)
188 cmd = args->argv[0];
189 else
190 cmd = options_get_string(&global_s_options, "default-command");
192 /* Construct the environment. */
193 environ_init(&env);
194 update = options_get_string(&global_s_options, "update-environment");
195 if (ctx->cmdclient != NULL)
196 environ_update(update, &ctx->cmdclient->environ, &env);
198 /* Create the new session. */
199 idx = -1 - options_get_number(&global_s_options, "base-index");
200 s = session_create(newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause);
201 if (s == NULL) {
202 ctx->error(ctx, "create session failed: %s", cause);
203 xfree(cause);
204 return (-1);
206 environ_free(&env);
208 /* Set the initial window name if one given. */
209 if (cmd != NULL && args_has(args, 'n')) {
210 w = s->curw->window;
212 xfree(w->name);
213 w->name = xstrdup(args_get(args, 'n'));
215 options_set_number(&w->options, "automatic-rename", 0);
219 * If a target session is given, this is to be part of a session group,
220 * so add it to the group and synchronize.
222 if (groupwith != NULL) {
223 session_group_add(groupwith, s);
224 session_group_synchronize_to(s);
225 session_select(s, RB_ROOT(&s->windows)->idx);
229 * Set the client to the new session. If a command client exists, it is
230 * taking this session and needs to get MSG_READY and stay around.
232 if (!detached) {
233 if (ctx->cmdclient != NULL) {
234 server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
236 old_s = ctx->cmdclient->session;
237 if (old_s != NULL)
238 ctx->cmdclient->last_session = old_s;
239 ctx->cmdclient->session = s;
240 session_update_activity(s);
241 server_redraw_client(ctx->cmdclient);
242 } else {
243 old_s = ctx->curclient->session;
244 if (old_s != NULL)
245 ctx->curclient->last_session = old_s;
246 ctx->curclient->session = s;
247 session_update_activity(s);
248 server_redraw_client(ctx->curclient);
251 recalculate_sizes();
252 server_update_socket();
255 * If there are still configuration file errors to display, put the new
256 * session's current window into more mode and display them now.
258 if (cfg_finished && !ARRAY_EMPTY(&cfg_causes)) {
259 wp = s->curw->window->active;
260 window_pane_set_mode(wp, &window_copy_mode);
261 window_copy_init_for_output(wp);
262 for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
263 cause = ARRAY_ITEM(&cfg_causes, i);
264 window_copy_add(wp, "%s", cause);
265 xfree(cause);
267 ARRAY_FREE(&cfg_causes);
270 return (!detached); /* 1 means don't tell command client to exit */