xref: /openbsd-src/usr.bin/tmux/cmd-new-session.c (revision 6f05df2d9be0954bec42d51d943d77bd250fb664)
1 /* $OpenBSD: cmd-new-session.c,v 1.63 2014/10/20 23:35:28 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <termios.h>
26 #include <unistd.h>
27 
28 #include "tmux.h"
29 
30 /*
31  * Create a new session and attach to the current terminal unless -d is given.
32  */
33 
34 #define NEW_SESSION_TEMPLATE "#{session_name}:"
35 
36 enum cmd_retval	 cmd_new_session_exec(struct cmd *, struct cmd_q *);
37 
38 const struct cmd_entry cmd_new_session_entry = {
39 	"new-session", "new",
40 	"Ac:dDF:n:Ps:t:x:y:", 0, -1,
41 	"[-AdDP] [-c start-directory] [-F format] [-n window-name] "
42 	"[-s session-name] " CMD_TARGET_SESSION_USAGE " [-x width] "
43 	"[-y height] [command]",
44 	CMD_STARTSERVER|CMD_CANTNEST,
45 	cmd_new_session_exec
46 };
47 
48 const struct cmd_entry cmd_has_session_entry = {
49 	"has-session", "has",
50 	"t:", 0, 0,
51 	CMD_TARGET_SESSION_USAGE,
52 	0,
53 	cmd_new_session_exec
54 };
55 
56 enum cmd_retval
57 cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
58 {
59 	struct args		*args = self->args;
60 	struct client		*c = cmdq->client, *c0;
61 	struct session		*s, *groupwith;
62 	struct window		*w;
63 	struct environ		 env;
64 	struct termios		 tio, *tiop;
65 	const char		*newname, *target, *update, *errstr, *template;
66 	const char		*path;
67 	char		       **argv, *cmd, *cause, *cp;
68 	int			 detached, already_attached, idx, cwd, fd = -1;
69 	int			 argc;
70 	u_int			 sx, sy;
71 	struct format_tree	*ft;
72 	struct environ_entry	*envent;
73 
74 	if (self->entry == &cmd_has_session_entry) {
75 		if (cmd_find_session(cmdq, args_get(args, 't'), 0) == NULL)
76 			return (CMD_RETURN_ERROR);
77 		return (CMD_RETURN_NORMAL);
78 	}
79 
80 	if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n'))) {
81 		cmdq_error(cmdq, "command or window name given with target");
82 		return (CMD_RETURN_ERROR);
83 	}
84 
85 	newname = args_get(args, 's');
86 	if (newname != NULL) {
87 		if (!session_check_name(newname)) {
88 			cmdq_error(cmdq, "bad session name: %s", newname);
89 			return (CMD_RETURN_ERROR);
90 		}
91 		if (session_find(newname) != NULL) {
92 			if (args_has(args, 'A')) {
93 				return (cmd_attach_session(cmdq, newname,
94 				    args_has(args, 'D'), 0, NULL));
95 			}
96 			cmdq_error(cmdq, "duplicate session: %s", newname);
97 			return (CMD_RETURN_ERROR);
98 		}
99 	}
100 
101 	target = args_get(args, 't');
102 	if (target != NULL) {
103 		groupwith = cmd_find_session(cmdq, target, 0);
104 		if (groupwith == NULL)
105 			return (CMD_RETURN_ERROR);
106 	} else
107 		groupwith = NULL;
108 
109 	/* Set -d if no client. */
110 	detached = args_has(args, 'd');
111 	if (c == NULL)
112 		detached = 1;
113 
114 	/* Is this client already attached? */
115 	already_attached = 0;
116 	if (c != NULL && c->session != NULL)
117 		already_attached = 1;
118 
119 	/* Get the new session working directory. */
120 	if (args_has(args, 'c')) {
121 		ft = format_create();
122 		if ((c0 = cmd_find_client(cmdq, NULL, 1)) != NULL)
123 			format_client(ft, c0);
124 		cp = format_expand(ft, args_get(args, 'c'));
125 		format_free(ft);
126 
127 		if (cp != NULL && *cp != '\0') {
128 			fd = open(cp, O_RDONLY|O_DIRECTORY);
129 			free(cp);
130 			if (fd == -1) {
131 				cmdq_error(cmdq, "bad working directory: %s",
132 				    strerror(errno));
133 				return (CMD_RETURN_ERROR);
134 			}
135 		} else if (cp != NULL)
136 			free(cp);
137 		cwd = fd;
138 	} else if (c != NULL && c->session == NULL)
139 		cwd = c->cwd;
140 	else if ((c0 = cmd_current_client(cmdq)) != NULL)
141 		cwd = c0->session->cwd;
142 	else {
143 		fd = open(".", O_RDONLY);
144 		cwd = fd;
145 	}
146 
147 	/*
148 	 * Save the termios settings, part of which is used for new windows in
149 	 * this session.
150 	 *
151 	 * This is read again with tcgetattr() rather than using tty.tio as if
152 	 * detached, tty_open won't be called. Because of this, it must be done
153 	 * before opening the terminal as that calls tcsetattr() to prepare for
154 	 * tmux taking over.
155 	 */
156 	if (!detached && !already_attached && c->tty.fd != -1) {
157 		if (tcgetattr(c->tty.fd, &tio) != 0)
158 			fatal("tcgetattr failed");
159 		tiop = &tio;
160 	} else
161 		tiop = NULL;
162 
163 	/* Open the terminal if necessary. */
164 	if (!detached && !already_attached) {
165 		if (server_client_open(c, &cause) != 0) {
166 			cmdq_error(cmdq, "open terminal failed: %s", cause);
167 			free(cause);
168 			goto error;
169 		}
170 	}
171 
172 	/* Find new session size. */
173 	if (c != NULL) {
174 		sx = c->tty.sx;
175 		sy = c->tty.sy;
176 	} else {
177 		sx = 80;
178 		sy = 24;
179 	}
180 	if (detached && args_has(args, 'x')) {
181 		sx = strtonum(args_get(args, 'x'), 1, USHRT_MAX, &errstr);
182 		if (errstr != NULL) {
183 			cmdq_error(cmdq, "width %s", errstr);
184 			goto error;
185 		}
186 	}
187 	if (detached && args_has(args, 'y')) {
188 		sy = strtonum(args_get(args, 'y'), 1, USHRT_MAX, &errstr);
189 		if (errstr != NULL) {
190 			cmdq_error(cmdq, "height %s", errstr);
191 			goto error;
192 		}
193 	}
194 	if (sy > 0 && options_get_number(&global_s_options, "status"))
195 		sy--;
196 	if (sx == 0)
197 		sx = 1;
198 	if (sy == 0)
199 		sy = 1;
200 
201 	/* Figure out the command for the new window. */
202 	argc = -1;
203 	argv = NULL;
204 	if (target == NULL && args->argc != 0) {
205 		argc = args->argc;
206 		argv = args->argv;
207 	} else if (target == NULL) {
208 		cmd = options_get_string(&global_s_options, "default-command");
209 		if (cmd != NULL && *cmd != '\0') {
210 			argc = 1;
211 			argv = &cmd;
212 		} else {
213 			argc = 0;
214 			argv = NULL;
215 		}
216 	}
217 
218 	path = NULL;
219 	if (c != NULL && c->session == NULL)
220 		envent = environ_find(&c->environ, "PATH");
221 	else
222 		envent = environ_find(&global_environ, "PATH");
223 	if (envent != NULL)
224 		path = envent->value;
225 
226 	/* Construct the environment. */
227 	environ_init(&env);
228 	update = options_get_string(&global_s_options, "update-environment");
229 	if (c != NULL)
230 		environ_update(update, &c->environ, &env);
231 
232 	/* Create the new session. */
233 	idx = -1 - options_get_number(&global_s_options, "base-index");
234 	s = session_create(newname, argc, argv, path, cwd, &env, tiop, idx, sx,
235 	    sy, &cause);
236 	if (s == NULL) {
237 		cmdq_error(cmdq, "create session failed: %s", cause);
238 		free(cause);
239 		goto error;
240 	}
241 	environ_free(&env);
242 
243 	/* Set the initial window name if one given. */
244 	if (argc >= 0 && args_has(args, 'n')) {
245 		w = s->curw->window;
246 		window_set_name(w, args_get(args, 'n'));
247 		options_set_number(&w->options, "automatic-rename", 0);
248 	}
249 
250 	/*
251 	 * If a target session is given, this is to be part of a session group,
252 	 * so add it to the group and synchronize.
253 	 */
254 	if (groupwith != NULL) {
255 		session_group_add(groupwith, s);
256 		session_group_synchronize_to(s);
257 		session_select(s, RB_ROOT(&s->windows)->idx);
258 	}
259 
260 	/*
261 	 * Set the client to the new session. If a command client exists, it is
262 	 * taking this session and needs to get MSG_READY and stay around.
263 	 */
264 	if (!detached) {
265 		if (!already_attached)
266 			server_write_ready(c);
267 		else if (c->session != NULL)
268 			c->last_session = c->session;
269 		c->session = s;
270 		notify_attached_session_changed(c);
271 		session_update_activity(s);
272 		server_redraw_client(c);
273 	}
274 	recalculate_sizes();
275 	server_update_socket();
276 
277 	/*
278 	 * If there are still configuration file errors to display, put the new
279 	 * session's current window into more mode and display them now.
280 	 */
281 	if (cfg_finished)
282 		cfg_show_causes(s);
283 
284 	/* Print if requested. */
285 	if (args_has(args, 'P')) {
286 		if ((template = args_get(args, 'F')) == NULL)
287 			template = NEW_SESSION_TEMPLATE;
288 
289 		ft = format_create();
290 		if ((c0 = cmd_find_client(cmdq, NULL, 1)) != NULL)
291 			format_client(ft, c0);
292 		format_session(ft, s);
293 
294 		cp = format_expand(ft, template);
295 		cmdq_print(cmdq, "%s", cp);
296 		free(cp);
297 
298 		format_free(ft);
299 	}
300 
301 	if (!detached)
302 		cmdq->client_exit = 0;
303 
304 	if (fd != -1)
305 		close(fd);
306 	return (CMD_RETURN_NORMAL);
307 
308 error:
309 	if (fd != -1)
310 		close(fd);
311 	return (CMD_RETURN_ERROR);
312 }
313