/src/tmux/cmd-new-session.c
Line | Count | Source |
1 | | /* $OpenBSD$ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> |
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 | 0 | #define NEW_SESSION_TEMPLATE "#{session_name}:" |
35 | | |
36 | | static enum cmd_retval cmd_new_session_exec(struct cmd *, struct cmdq_item *); |
37 | | |
38 | | const struct cmd_entry cmd_new_session_entry = { |
39 | | .name = "new-session", |
40 | | .alias = "new", |
41 | | |
42 | | .args = { "Ac:dDe:EF:f:n:Ps:t:x:Xy:", 0, -1, NULL }, |
43 | | .usage = "[-AdDEPX] [-c start-directory] [-e environment] [-F format] " |
44 | | "[-f flags] [-n window-name] [-s session-name] " |
45 | | CMD_TARGET_SESSION_USAGE " [-x width] [-y height] " |
46 | | "[shell-command [argument ...]]", |
47 | | |
48 | | .target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL }, |
49 | | |
50 | | .flags = CMD_STARTSERVER, |
51 | | .exec = cmd_new_session_exec |
52 | | }; |
53 | | |
54 | | const struct cmd_entry cmd_has_session_entry = { |
55 | | .name = "has-session", |
56 | | .alias = "has", |
57 | | |
58 | | .args = { "t:", 0, 0, NULL }, |
59 | | .usage = CMD_TARGET_SESSION_USAGE, |
60 | | |
61 | | .target = { 't', CMD_FIND_SESSION, 0 }, |
62 | | |
63 | | .flags = 0, |
64 | | .exec = cmd_new_session_exec |
65 | | }; |
66 | | |
67 | | static enum cmd_retval |
68 | | cmd_new_session_exec(struct cmd *self, struct cmdq_item *item) |
69 | 0 | { |
70 | 0 | struct args *args = cmd_get_args(self); |
71 | 0 | struct cmd_find_state *current = cmdq_get_current(item); |
72 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
73 | 0 | struct client *c = cmdq_get_client(item); |
74 | 0 | struct session *s, *as, *groupwith = NULL; |
75 | 0 | struct environ *env; |
76 | 0 | struct options *oo; |
77 | 0 | struct termios tio, *tiop; |
78 | 0 | struct session_group *sg = NULL; |
79 | 0 | const char *errstr, *template, *group, *tmp; |
80 | 0 | char *cause, *cwd = NULL, *cp, *newname = NULL; |
81 | 0 | char *name, *prefix = NULL; |
82 | 0 | int detached, already_attached, is_control = 0; |
83 | 0 | u_int sx, sy, dsx, dsy, count = args_count(args); |
84 | 0 | struct spawn_context sc = { 0 }; |
85 | 0 | enum cmd_retval retval; |
86 | 0 | struct cmd_find_state fs; |
87 | 0 | struct args_value *av; |
88 | |
|
89 | 0 | if (cmd_get_entry(self) == &cmd_has_session_entry) { |
90 | | /* |
91 | | * cmd_find_target() will fail if the session cannot be found, |
92 | | * so always return success here. |
93 | | */ |
94 | 0 | return (CMD_RETURN_NORMAL); |
95 | 0 | } |
96 | | |
97 | 0 | if (args_has(args, 't') && (count != 0 || args_has(args, 'n'))) { |
98 | 0 | cmdq_error(item, "command or window name given with target"); |
99 | 0 | return (CMD_RETURN_ERROR); |
100 | 0 | } |
101 | | |
102 | 0 | tmp = args_get(args, 's'); |
103 | 0 | if (tmp != NULL) { |
104 | 0 | name = format_single(item, tmp, c, NULL, NULL, NULL); |
105 | 0 | newname = clean_name(name, "#:."); |
106 | 0 | if (newname == NULL) { |
107 | 0 | cmdq_error(item, "invalid session: %s", name); |
108 | 0 | free(name); |
109 | 0 | return (CMD_RETURN_ERROR); |
110 | 0 | } |
111 | 0 | free(name); |
112 | 0 | } |
113 | 0 | if (args_has(args, 'A')) { |
114 | 0 | if (newname != NULL) |
115 | 0 | as = session_find(newname); |
116 | 0 | else |
117 | 0 | as = target->s; |
118 | 0 | if (as != NULL) { |
119 | 0 | retval = cmd_attach_session(item, as->name, |
120 | 0 | args_has(args, 'D'), args_has(args, 'X'), 0, |
121 | 0 | args_get(args, 'c'), args_has(args, 'E'), |
122 | 0 | args_get(args, 'f')); |
123 | 0 | free(newname); |
124 | 0 | return (retval); |
125 | 0 | } |
126 | 0 | } |
127 | 0 | if (newname != NULL && session_find(newname) != NULL) { |
128 | 0 | cmdq_error(item, "duplicate session: %s", newname); |
129 | 0 | goto fail; |
130 | 0 | } |
131 | | |
132 | | /* Is this going to be part of a session group? */ |
133 | 0 | group = args_get(args, 't'); |
134 | 0 | if (group != NULL) { |
135 | 0 | groupwith = target->s; |
136 | 0 | if (groupwith == NULL) |
137 | 0 | sg = session_group_find(group); |
138 | 0 | else |
139 | 0 | sg = session_group_contains(groupwith); |
140 | 0 | if (sg != NULL) |
141 | 0 | prefix = xstrdup(sg->name); |
142 | 0 | else if (groupwith != NULL) |
143 | 0 | prefix = xstrdup(groupwith->name); |
144 | 0 | else { |
145 | 0 | prefix = clean_name(group, "#:."); |
146 | 0 | if (prefix == NULL) { |
147 | 0 | cmdq_error(item, "invalid session group: %s", |
148 | 0 | group); |
149 | 0 | goto fail; |
150 | 0 | } |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | | /* Set -d if no client. */ |
155 | 0 | detached = args_has(args, 'd'); |
156 | 0 | if (c == NULL) |
157 | 0 | detached = 1; |
158 | 0 | else if (c->flags & CLIENT_CONTROL) |
159 | 0 | is_control = 1; |
160 | | |
161 | | /* Is this client already attached? */ |
162 | 0 | already_attached = 0; |
163 | 0 | if (c != NULL && c->session != NULL) |
164 | 0 | already_attached = 1; |
165 | | |
166 | | /* Get the new session working directory. */ |
167 | 0 | if ((tmp = args_get(args, 'c')) != NULL) |
168 | 0 | cwd = format_single(item, tmp, c, NULL, NULL, NULL); |
169 | 0 | else |
170 | 0 | cwd = xstrdup(server_client_get_cwd(c, NULL)); |
171 | | |
172 | | /* |
173 | | * If this is a new client, check for nesting and save the termios |
174 | | * settings (part of which is used for new windows in this session). |
175 | | * |
176 | | * tcgetattr() is used rather than using tty.tio since if the client is |
177 | | * detached, tty_open won't be called. It must be done before opening |
178 | | * the terminal as that calls tcsetattr() to prepare for tmux taking |
179 | | * over. |
180 | | */ |
181 | 0 | if (!detached && |
182 | 0 | !already_attached && |
183 | 0 | c->fd != -1 && |
184 | 0 | (~c->flags & CLIENT_CONTROL)) { |
185 | 0 | if (server_client_check_nested(cmdq_get_client(item))) { |
186 | 0 | cmdq_error(item, "sessions should be nested with care, " |
187 | 0 | "unset $TMUX to force"); |
188 | 0 | goto fail; |
189 | 0 | } |
190 | 0 | if (tcgetattr(c->fd, &tio) != 0) |
191 | 0 | fatal("tcgetattr failed"); |
192 | 0 | tiop = &tio; |
193 | 0 | } else |
194 | 0 | tiop = NULL; |
195 | | |
196 | | /* Open the terminal if necessary. */ |
197 | 0 | if (!detached && !already_attached) { |
198 | 0 | if (server_client_open(c, &cause) != 0) { |
199 | 0 | cmdq_error(item, "open terminal failed: %s", cause); |
200 | 0 | free(cause); |
201 | 0 | goto fail; |
202 | 0 | } |
203 | 0 | } |
204 | | |
205 | | /* Get default session size. */ |
206 | 0 | if (args_has(args, 'x')) { |
207 | 0 | tmp = args_get(args, 'x'); |
208 | 0 | if (strcmp(tmp, "-") == 0) { |
209 | 0 | if (c != NULL) |
210 | 0 | dsx = c->tty.sx; |
211 | 0 | else |
212 | 0 | dsx = 80; |
213 | 0 | } else { |
214 | 0 | dsx = strtonum(tmp, 1, USHRT_MAX, &errstr); |
215 | 0 | if (errstr != NULL) { |
216 | 0 | cmdq_error(item, "width %s", errstr); |
217 | 0 | goto fail; |
218 | 0 | } |
219 | 0 | } |
220 | 0 | } else |
221 | 0 | dsx = 80; |
222 | 0 | if (args_has(args, 'y')) { |
223 | 0 | tmp = args_get(args, 'y'); |
224 | 0 | if (strcmp(tmp, "-") == 0) { |
225 | 0 | if (c != NULL) |
226 | 0 | dsy = c->tty.sy; |
227 | 0 | else |
228 | 0 | dsy = 24; |
229 | 0 | } else { |
230 | 0 | dsy = strtonum(tmp, 1, USHRT_MAX, &errstr); |
231 | 0 | if (errstr != NULL) { |
232 | 0 | cmdq_error(item, "height %s", errstr); |
233 | 0 | goto fail; |
234 | 0 | } |
235 | 0 | } |
236 | 0 | } else |
237 | 0 | dsy = 24; |
238 | | |
239 | | /* Find new session size. */ |
240 | 0 | if (!detached && !is_control) { |
241 | 0 | sx = c->tty.sx; |
242 | 0 | sy = c->tty.sy; |
243 | 0 | if (sy > 0 && options_get_number(global_s_options, "status")) |
244 | 0 | sy--; |
245 | 0 | } else { |
246 | 0 | tmp = options_get_string(global_s_options, "default-size"); |
247 | 0 | if (sscanf(tmp, "%ux%u", &sx, &sy) != 2) { |
248 | 0 | sx = dsx; |
249 | 0 | sy = dsy; |
250 | 0 | } else { |
251 | 0 | if (args_has(args, 'x')) |
252 | 0 | sx = dsx; |
253 | 0 | if (args_has(args, 'y')) |
254 | 0 | sy = dsy; |
255 | 0 | } |
256 | 0 | } |
257 | 0 | if (sx == 0) |
258 | 0 | sx = 1; |
259 | 0 | if (sy == 0) |
260 | 0 | sy = 1; |
261 | | |
262 | | /* Create the new session. */ |
263 | 0 | oo = options_create(global_s_options); |
264 | 0 | if (args_has(args, 'x') || args_has(args, 'y')) { |
265 | 0 | if (!args_has(args, 'x')) |
266 | 0 | dsx = sx; |
267 | 0 | if (!args_has(args, 'y')) |
268 | 0 | dsy = sy; |
269 | 0 | options_set_string(oo, "default-size", 0, "%ux%u", dsx, dsy); |
270 | 0 | } |
271 | 0 | env = environ_create(); |
272 | 0 | if (c != NULL && !args_has(args, 'E')) |
273 | 0 | environ_update(global_s_options, c->environ, env); |
274 | 0 | av = args_first_value(args, 'e'); |
275 | 0 | while (av != NULL) { |
276 | 0 | environ_put(env, av->string, 0); |
277 | 0 | av = args_next_value(av); |
278 | 0 | } |
279 | 0 | s = session_create(prefix, newname, cwd, env, oo, tiop); |
280 | | |
281 | | /* Spawn the initial window. */ |
282 | 0 | sc.item = item; |
283 | 0 | sc.s = s; |
284 | 0 | if (!detached) |
285 | 0 | sc.tc = c; |
286 | |
|
287 | 0 | sc.name = args_get(args, 'n'); |
288 | 0 | args_to_vector(args, &sc.argc, &sc.argv); |
289 | |
|
290 | 0 | sc.idx = -1; |
291 | 0 | sc.cwd = args_get(args, 'c'); |
292 | |
|
293 | 0 | sc.flags = 0; |
294 | |
|
295 | 0 | if (spawn_window(&sc, &cause) == NULL) { |
296 | 0 | session_destroy(s, 0, __func__); |
297 | 0 | cmdq_error(item, "create window failed: %s", cause); |
298 | 0 | free(cause); |
299 | 0 | goto fail; |
300 | 0 | } |
301 | | |
302 | | /* |
303 | | * If a target session is given, this is to be part of a session group, |
304 | | * so add it to the group and synchronize. |
305 | | */ |
306 | 0 | if (group != NULL) { |
307 | 0 | if (sg == NULL) { |
308 | 0 | if (groupwith != NULL) { |
309 | 0 | sg = session_group_new(groupwith->name); |
310 | 0 | session_group_add(sg, groupwith); |
311 | 0 | } else |
312 | 0 | sg = session_group_new(group); |
313 | 0 | } |
314 | 0 | session_group_add(sg, s); |
315 | 0 | session_group_synchronize_to(s); |
316 | 0 | session_select(s, RB_MIN(winlinks, &s->windows)->idx); |
317 | 0 | } |
318 | 0 | notify_session("session-created", s); |
319 | | |
320 | | /* |
321 | | * Set the client to the new session. If a command client exists, it is |
322 | | * taking this session and needs to get MSG_READY and stay around. |
323 | | */ |
324 | 0 | if (!detached) { |
325 | 0 | if (args_has(args, 'f')) |
326 | 0 | server_client_set_flags(c, args_get(args, 'f')); |
327 | 0 | if (!already_attached) { |
328 | 0 | if (~c->flags & CLIENT_CONTROL) |
329 | 0 | proc_send(c->peer, MSG_READY, -1, NULL, 0); |
330 | 0 | } else if (c->session != NULL) |
331 | 0 | c->last_session = c->session; |
332 | 0 | server_client_set_session(c, s); |
333 | 0 | if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT) |
334 | 0 | server_client_set_key_table(c, NULL); |
335 | 0 | } |
336 | | |
337 | | /* Print if requested. */ |
338 | 0 | if (args_has(args, 'P')) { |
339 | 0 | if ((template = args_get(args, 'F')) == NULL) |
340 | 0 | template = NEW_SESSION_TEMPLATE; |
341 | 0 | cp = format_single(item, template, c, s, s->curw, NULL); |
342 | 0 | cmdq_print(item, "%s", cp); |
343 | 0 | free(cp); |
344 | 0 | } |
345 | |
|
346 | 0 | if (!detached) |
347 | 0 | c->flags |= CLIENT_ATTACHED; |
348 | 0 | if (!args_has(args, 'd')) |
349 | 0 | cmd_find_from_session(current, s, 0); |
350 | |
|
351 | 0 | cmd_find_from_session(&fs, s, 0); |
352 | 0 | cmdq_insert_hook(s, item, &fs, "after-new-session"); |
353 | |
|
354 | 0 | if (cfg_finished) |
355 | 0 | cfg_show_causes(s); |
356 | |
|
357 | 0 | if (sc.argv != NULL) |
358 | 0 | cmd_free_argv(sc.argc, sc.argv); |
359 | 0 | free(cwd); |
360 | 0 | free(newname); |
361 | 0 | free(prefix); |
362 | 0 | return (CMD_RETURN_NORMAL); |
363 | | |
364 | 0 | fail: |
365 | 0 | if (sc.argv != NULL) |
366 | 0 | cmd_free_argv(sc.argc, sc.argv); |
367 | 0 | free(cwd); |
368 | 0 | free(newname); |
369 | 0 | free(prefix); |
370 | 0 | return (CMD_RETURN_ERROR); |
371 | 0 | } |