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