Annotation of src/usr.bin/tmux/cmd-new-session.c, Revision 1.34
1.34 ! nicm 1: /* $OpenBSD: cmd-new-session.c,v 1.33 2011/01/01 01:12:09 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.11 nicm 22: #include <string.h>
23: #include <termios.h>
1.30 nicm 24: #include <unistd.h>
1.11 nicm 25:
1.1 nicm 26: #include "tmux.h"
27:
28: /*
29: * Create a new session and attach to the current terminal unless -d is given.
30: */
31:
1.34 ! nicm 32: int cmd_new_session_check(struct args *);
1.1 nicm 33: int cmd_new_session_exec(struct cmd *, struct cmd_ctx *);
34:
35: const struct cmd_entry cmd_new_session_entry = {
36: "new-session", "new",
1.34 ! nicm 37: "dn:s:t:", 0, 1,
1.21 nicm 38: "[-d] [-n window-name] [-s session-name] [-t target-session] [command]",
1.34 ! nicm 39: CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON,
! 40: NULL,
! 41: cmd_new_session_check,
! 42: cmd_new_session_exec
1.1 nicm 43: };
44:
45: int
1.34 ! nicm 46: cmd_new_session_check(struct args *args)
1.1 nicm 47: {
1.34 ! nicm 48: if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n')))
! 49: return (-1);
1.1 nicm 50: return (0);
51: }
52:
53: int
54: cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
55: {
1.34 ! nicm 56: struct args *args = self->args;
! 57: struct session *s, *old_s, *groupwith;
! 58: struct window *w;
! 59: struct window_pane *wp;
! 60: struct environ env;
! 61: struct termios tio, *tiop;
! 62: struct passwd *pw;
! 63: const char *newname, *target, *update, *cwd;
! 64: char *overrides, *cmd, *cause;
! 65: int detached, idx;
! 66: u_int sx, sy, i;
! 67:
! 68: newname = args_get(args, 's');
! 69: if (newname != NULL && session_find(newname) != NULL) {
! 70: ctx->error(ctx, "duplicate session: %s", newname);
1.4 nicm 71: return (-1);
72: }
1.1 nicm 73:
1.34 ! nicm 74: target = args_get(args, 't');
! 75: if (target != NULL) {
! 76: groupwith = cmd_find_session(ctx, target);
! 77: if (groupwith == NULL)
! 78: return (-1);
! 79: } else
! 80: groupwith = NULL;
1.21 nicm 81:
1.4 nicm 82: /*
1.6 nicm 83: * There are three cases:
1.4 nicm 84: *
85: * 1. If cmdclient is non-NULL, new-session has been called from the
86: * command-line - cmdclient is to become a new attached, interactive
87: * client. Unless -d is given, the terminal must be opened and then
88: * the client sent MSG_READY.
89: *
90: * 2. If cmdclient is NULL, new-session has been called from an
91: * existing client (such as a key binding).
92: *
1.6 nicm 93: * 3. Both are NULL, the command was in the configuration file. Treat
94: * this as if -d was given even if it was not.
95: *
96: * In all cases, a new additional session needs to be created and
1.4 nicm 97: * (unless -d) set as the current session for the client.
98: */
99:
1.6 nicm 100: /* Set -d if no client. */
1.34 ! nicm 101: detached = args_has(args, 'd');
1.6 nicm 102: if (ctx->cmdclient == NULL && ctx->curclient == NULL)
103: detached = 1;
104:
1.15 nicm 105: /*
1.19 nicm 106: * Save the termios settings, part of which is used for new windows in
107: * this session.
1.15 nicm 108: *
109: * This is read again with tcgetattr() rather than using tty.tio as if
110: * detached, tty_open won't be called. Because of this, it must be done
111: * before opening the terminal as that calls tcsetattr() to prepare for
112: * tmux taking over.
113: */
114: if (ctx->cmdclient != NULL && ctx->cmdclient->tty.fd != -1) {
115: if (tcgetattr(ctx->cmdclient->tty.fd, &tio) != 0)
116: fatal("tcgetattr failed");
1.19 nicm 117: tiop = &tio;
1.15 nicm 118: } else
1.19 nicm 119: tiop = NULL;
1.15 nicm 120:
1.4 nicm 121: /* Open the terminal if necessary. */
1.6 nicm 122: if (!detached && ctx->cmdclient != NULL) {
1.4 nicm 123: if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
124: ctx->error(ctx, "not a terminal");
1.1 nicm 125: return (-1);
126: }
1.25 nicm 127:
128: overrides =
1.9 nicm 129: options_get_string(&global_s_options, "terminal-overrides");
130: if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
1.4 nicm 131: ctx->error(ctx, "open terminal failed: %s", cause);
132: xfree(cause);
1.1 nicm 133: return (-1);
134: }
135: }
136:
1.16 nicm 137: /* Get the new session working directory. */
138: if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
139: cwd = ctx->cmdclient->cwd;
1.30 nicm 140: else {
141: pw = getpwuid(getuid());
142: if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
143: cwd = pw->pw_dir;
144: else
145: cwd = "/";
146: }
1.16 nicm 147:
148: /* Find new session size. */
1.6 nicm 149: if (detached) {
1.4 nicm 150: sx = 80;
1.18 nicm 151: sy = 24;
1.16 nicm 152: } else if (ctx->cmdclient != NULL) {
153: sx = ctx->cmdclient->tty.sx;
154: sy = ctx->cmdclient->tty.sy;
1.4 nicm 155: } else {
1.16 nicm 156: sx = ctx->curclient->tty.sx;
157: sy = ctx->curclient->tty.sy;
1.1 nicm 158: }
1.7 nicm 159: if (sy > 0 && options_get_number(&global_s_options, "status"))
1.4 nicm 160: sy--;
161: if (sx == 0)
162: sx = 1;
163: if (sy == 0)
164: sy = 1;
1.16 nicm 165:
166: /* Figure out the command for the new window. */
1.34 ! nicm 167: if (target != NULL)
1.21 nicm 168: cmd = NULL;
1.34 ! nicm 169: else if (args->argc != 0)
! 170: cmd = args->argv[0];
1.1 nicm 171: else
1.7 nicm 172: cmd = options_get_string(&global_s_options, "default-command");
1.1 nicm 173:
1.10 nicm 174: /* Construct the environment. */
175: environ_init(&env);
176: update = options_get_string(&global_s_options, "update-environment");
177: if (ctx->cmdclient != NULL)
178: environ_update(update, &ctx->cmdclient->environ, &env);
1.11 nicm 179:
1.4 nicm 180: /* Create the new session. */
1.13 nicm 181: idx = -1 - options_get_number(&global_s_options, "base-index");
1.34 ! nicm 182: s = session_create(newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause);
1.1 nicm 183: if (s == NULL) {
184: ctx->error(ctx, "create session failed: %s", cause);
185: xfree(cause);
186: return (-1);
187: }
1.10 nicm 188: environ_free(&env);
1.4 nicm 189:
1.16 nicm 190: /* Set the initial window name if one given. */
1.34 ! nicm 191: if (cmd != NULL && args_has(args, 'n')) {
1.16 nicm 192: w = s->curw->window;
193:
194: xfree(w->name);
1.34 ! nicm 195: w->name = xstrdup(args_get(args, 'n'));
1.16 nicm 196:
197: options_set_number(&w->options, "automatic-rename", 0);
1.21 nicm 198: }
199:
200: /*
201: * If a target session is given, this is to be part of a session group,
202: * so add it to the group and synchronize.
203: */
204: if (groupwith != NULL) {
205: session_group_add(groupwith, s);
206: session_group_synchronize_to(s);
207: session_select(s, RB_ROOT(&s->windows)->idx);
1.1 nicm 208: }
209:
1.16 nicm 210: /*
211: * Set the client to the new session. If a command client exists, it is
212: * taking this session and needs to get MSG_READY and stay around.
1.4 nicm 213: */
1.25 nicm 214: if (!detached) {
1.4 nicm 215: if (ctx->cmdclient != NULL) {
1.16 nicm 216: server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
1.32 nicm 217:
218: old_s = ctx->cmdclient->session;
219: if (old_s != NULL)
220: ctx->cmdclient->last_session = old_s;
1.25 nicm 221: ctx->cmdclient->session = s;
1.33 nicm 222: session_update_activity(s);
1.4 nicm 223: server_redraw_client(ctx->cmdclient);
224: } else {
1.32 nicm 225: old_s = ctx->curclient->session;
226: if (old_s != NULL)
227: ctx->curclient->last_session = old_s;
1.25 nicm 228: ctx->curclient->session = s;
1.33 nicm 229: session_update_activity(s);
1.4 nicm 230: server_redraw_client(ctx->curclient);
231: }
1.1 nicm 232: }
233: recalculate_sizes();
1.22 nicm 234: server_update_socket();
1.26 nicm 235:
236: /*
237: * If there are still configuration file errors to display, put the new
238: * session's current window into more mode and display them now.
239: */
1.27 nicm 240: if (cfg_finished && !ARRAY_EMPTY(&cfg_causes)) {
1.26 nicm 241: wp = s->curw->window->active;
1.29 nicm 242: window_pane_set_mode(wp, &window_copy_mode);
243: window_copy_init_for_output(wp);
1.27 nicm 244: for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
245: cause = ARRAY_ITEM(&cfg_causes, i);
1.29 nicm 246: window_copy_add(wp, "%s", cause);
1.27 nicm 247: xfree(cause);
1.26 nicm 248: }
1.27 nicm 249: ARRAY_FREE(&cfg_causes);
1.26 nicm 250: }
1.1 nicm 251:
1.12 nicm 252: return (!detached); /* 1 means don't tell command client to exit */
1.1 nicm 253: }