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