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