Handle no client better in display-message.
[tmux-openbsd.git] / cmd-new-session.c
blob1cc6fbab339d31af52d03b28d900de27470a6f48
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 enum cmd_retval cmd_new_session_check(struct args *);
34 enum cmd_retval cmd_new_session_exec(struct cmd *, struct cmd_q *);
36 const struct cmd_entry cmd_new_session_entry = {
37 "new-session", "new",
38 "AdDn:s:t:x:y:", 0, 1,
39 "[-AdD] [-n window-name] [-s session-name] " CMD_TARGET_SESSION_USAGE
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 enum cmd_retval
48 cmd_new_session_check(struct args *args)
50 if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n')))
51 return (CMD_RETURN_ERROR);
52 return (CMD_RETURN_NORMAL);
55 enum cmd_retval
56 cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
58 struct args *args = self->args;
59 struct client *c = cmdq->client;
60 struct session *s, *groupwith;
61 struct window *w;
62 struct environ env;
63 struct termios tio, *tiop;
64 struct passwd *pw;
65 const char *newname, *target, *update, *cwd, *errstr;
66 char *cmd, *cause;
67 int detached, idx;
68 u_int sx, sy;
69 int already_attached;
72 newname = args_get(args, 's');
73 if (newname != NULL) {
74 if (!session_check_name(newname)) {
75 cmdq_error(cmdq, "bad session name: %s", newname);
76 return (CMD_RETURN_ERROR);
78 if (session_find(newname) != NULL) {
79 if (args_has(args, 'A')) {
80 return (cmd_attach_session(cmdq, newname,
81 args_has(args, 'D'), 0));
83 cmdq_error(cmdq, "duplicate session: %s", newname);
84 return (CMD_RETURN_ERROR);
88 target = args_get(args, 't');
89 if (target != NULL) {
90 groupwith = cmd_find_session(cmdq, target, 0);
91 if (groupwith == NULL)
92 return (CMD_RETURN_ERROR);
93 } else
94 groupwith = NULL;
96 /* Set -d if no client. */
97 detached = args_has(args, 'd');
98 if (c == NULL)
99 detached = 1;
101 /* Is this client already attached? */
102 already_attached = 0;
103 if (c != NULL && c->session != NULL)
104 already_attached = 1;
107 * Save the termios settings, part of which is used for new windows in
108 * this session.
110 * This is read again with tcgetattr() rather than using tty.tio as if
111 * detached, tty_open won't be called. Because of this, it must be done
112 * before opening the terminal as that calls tcsetattr() to prepare for
113 * tmux taking over.
115 if (!detached && !already_attached && c->tty.fd != -1) {
116 if (tcgetattr(c->tty.fd, &tio) != 0)
117 fatal("tcgetattr failed");
118 tiop = &tio;
119 } else
120 tiop = NULL;
122 /* Open the terminal if necessary. */
123 if (!detached && !already_attached) {
124 if (server_client_open(c, NULL, &cause) != 0) {
125 cmdq_error(cmdq, "open terminal failed: %s", cause);
126 free(cause);
127 return (CMD_RETURN_ERROR);
131 /* Get the new session working directory. */
132 if (c != NULL && c->cwd != NULL)
133 cwd = c->cwd;
134 else {
135 pw = getpwuid(getuid());
136 if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
137 cwd = pw->pw_dir;
138 else
139 cwd = "/";
142 /* Find new session size. */
143 if (c != NULL) {
144 sx = c->tty.sx;
145 sy = c->tty.sy;
146 } else {
147 sx = 80;
148 sy = 24;
150 if (detached && args_has(args, 'x')) {
151 sx = strtonum(args_get(args, 'x'), 1, USHRT_MAX, &errstr);
152 if (errstr != NULL) {
153 cmdq_error(cmdq, "width %s", errstr);
154 return (CMD_RETURN_ERROR);
157 if (detached && args_has(args, 'y')) {
158 sy = strtonum(args_get(args, 'y'), 1, USHRT_MAX, &errstr);
159 if (errstr != NULL) {
160 cmdq_error(cmdq, "height %s", errstr);
161 return (CMD_RETURN_ERROR);
164 if (sy > 0 && options_get_number(&global_s_options, "status"))
165 sy--;
166 if (sx == 0)
167 sx = 1;
168 if (sy == 0)
169 sy = 1;
171 /* Figure out the command for the new window. */
172 if (target != NULL)
173 cmd = NULL;
174 else if (args->argc != 0)
175 cmd = args->argv[0];
176 else
177 cmd = options_get_string(&global_s_options, "default-command");
179 /* Construct the environment. */
180 environ_init(&env);
181 update = options_get_string(&global_s_options, "update-environment");
182 if (c != NULL)
183 environ_update(update, &c->environ, &env);
185 /* Create the new session. */
186 idx = -1 - options_get_number(&global_s_options, "base-index");
187 s = session_create(newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause);
188 if (s == NULL) {
189 cmdq_error(cmdq, "create session failed: %s", cause);
190 free(cause);
191 return (CMD_RETURN_ERROR);
193 environ_free(&env);
195 /* Set the initial window name if one given. */
196 if (cmd != NULL && args_has(args, 'n')) {
197 w = s->curw->window;
198 window_set_name(w, args_get(args, 'n'));
199 options_set_number(&w->options, "automatic-rename", 0);
203 * If a target session is given, this is to be part of a session group,
204 * so add it to the group and synchronize.
206 if (groupwith != NULL) {
207 session_group_add(groupwith, s);
208 session_group_synchronize_to(s);
209 session_select(s, RB_ROOT(&s->windows)->idx);
213 * Set the client to the new session. If a command client exists, it is
214 * taking this session and needs to get MSG_READY and stay around.
216 if (!detached) {
217 if (!already_attached)
218 server_write_ready(c);
219 else if (c->session != NULL)
220 c->last_session = c->session;
221 c->session = s;
222 notify_attached_session_changed(c);
223 session_update_activity(s);
224 server_redraw_client(c);
226 recalculate_sizes();
227 server_update_socket();
230 * If there are still configuration file errors to display, put the new
231 * session's current window into more mode and display them now.
233 if (cfg_finished)
234 cfg_show_causes(s);
236 if (!detached)
237 cmdq->client_exit = 0;
238 return (CMD_RETURN_NORMAL);