Annotation of src/usr.bin/tmux/cmd-new-session.c, Revision 1.32
1.32 ! nicm 1: /* $OpenBSD: cmd-new-session.c,v 1.31 2010/12/11 18:39:25 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:
32: int cmd_new_session_parse(struct cmd *, int, char **, char **);
33: int cmd_new_session_exec(struct cmd *, struct cmd_ctx *);
34: void cmd_new_session_free(struct cmd *);
35: void cmd_new_session_init(struct cmd *, int);
36: size_t cmd_new_session_print(struct cmd *, char *, size_t);
37:
38: struct cmd_new_session_data {
1.21 nicm 39: char *target;
1.1 nicm 40: char *newname;
41: char *winname;
42: char *cmd;
43: int flag_detached;
44: };
45:
46: const struct cmd_entry cmd_new_session_entry = {
47: "new-session", "new",
1.21 nicm 48: "[-d] [-n window-name] [-s session-name] [-t target-session] [command]",
1.23 nicm 49: CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON, "",
1.1 nicm 50: cmd_new_session_init,
51: cmd_new_session_parse,
52: cmd_new_session_exec,
53: cmd_new_session_free,
54: cmd_new_session_print
55: };
56:
1.24 nicm 57: /* ARGSUSED */
1.1 nicm 58: void
59: cmd_new_session_init(struct cmd *self, unused int arg)
60: {
61: struct cmd_new_session_data *data;
62:
63: self->data = data = xmalloc(sizeof *data);
64: data->flag_detached = 0;
1.21 nicm 65: data->target = NULL;
1.1 nicm 66: data->newname = NULL;
67: data->winname = NULL;
68: data->cmd = NULL;
69: }
70:
71: int
72: cmd_new_session_parse(struct cmd *self, int argc, char **argv, char **cause)
73: {
74: struct cmd_new_session_data *data;
75: int opt;
76:
1.20 nicm 77: self->entry->init(self, KEYC_NONE);
1.1 nicm 78: data = self->data;
79:
1.21 nicm 80: while ((opt = getopt(argc, argv, "ds:t:n:")) != -1) {
1.1 nicm 81: switch (opt) {
82: case 'd':
83: data->flag_detached = 1;
84: break;
85: case 's':
86: if (data->newname == NULL)
87: data->newname = xstrdup(optarg);
88: break;
1.21 nicm 89: case 't':
90: if (data->target == NULL)
91: data->target = xstrdup(optarg);
92: break;
1.1 nicm 93: case 'n':
94: if (data->winname == NULL)
95: data->winname = xstrdup(optarg);
96: break;
97: default:
98: goto usage;
99: }
100: }
101: argc -= optind;
102: argv += optind;
103: if (argc != 0 && argc != 1)
104: goto usage;
105:
1.21 nicm 106: if (data->target != NULL && (argc == 1 || data->winname != NULL))
107: goto usage;
108:
1.1 nicm 109: if (argc == 1)
110: data->cmd = xstrdup(argv[0]);
111:
112: return (0);
113:
114: usage:
115: xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
116:
117: self->entry->free(self);
118: return (-1);
119: }
120:
121: int
122: cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
123: {
124: struct cmd_new_session_data *data = self->data;
1.32 ! nicm 125: struct session *s, *old_s, *groupwith;
1.16 nicm 126: struct window *w;
1.26 nicm 127: struct window_pane *wp;
1.10 nicm 128: struct environ env;
1.19 nicm 129: struct termios tio, *tiop;
1.30 nicm 130: struct passwd *pw;
131: const char *update, *cwd;
132: char *overrides, *cmd, *cause;
1.13 nicm 133: int detached, idx;
1.26 nicm 134: u_int sx, sy, i;
1.1 nicm 135:
1.4 nicm 136: if (data->newname != NULL && session_find(data->newname) != NULL) {
137: ctx->error(ctx, "duplicate session: %s", data->newname);
138: return (-1);
139: }
1.1 nicm 140:
1.21 nicm 141: groupwith = NULL;
142: if (data->target != NULL &&
143: (groupwith = cmd_find_session(ctx, data->target)) == NULL)
144: return (-1);
145:
1.4 nicm 146: /*
1.6 nicm 147: * There are three cases:
1.4 nicm 148: *
149: * 1. If cmdclient is non-NULL, new-session has been called from the
150: * command-line - cmdclient is to become a new attached, interactive
151: * client. Unless -d is given, the terminal must be opened and then
152: * the client sent MSG_READY.
153: *
154: * 2. If cmdclient is NULL, new-session has been called from an
155: * existing client (such as a key binding).
156: *
1.6 nicm 157: * 3. Both are NULL, the command was in the configuration file. Treat
158: * this as if -d was given even if it was not.
159: *
160: * In all cases, a new additional session needs to be created and
1.4 nicm 161: * (unless -d) set as the current session for the client.
162: */
163:
1.6 nicm 164: /* Set -d if no client. */
165: detached = data->flag_detached;
166: if (ctx->cmdclient == NULL && ctx->curclient == NULL)
167: detached = 1;
168:
1.15 nicm 169: /*
1.19 nicm 170: * Save the termios settings, part of which is used for new windows in
171: * this session.
1.15 nicm 172: *
173: * This is read again with tcgetattr() rather than using tty.tio as if
174: * detached, tty_open won't be called. Because of this, it must be done
175: * before opening the terminal as that calls tcsetattr() to prepare for
176: * tmux taking over.
177: */
178: if (ctx->cmdclient != NULL && ctx->cmdclient->tty.fd != -1) {
179: if (tcgetattr(ctx->cmdclient->tty.fd, &tio) != 0)
180: fatal("tcgetattr failed");
1.19 nicm 181: tiop = &tio;
1.15 nicm 182: } else
1.19 nicm 183: tiop = NULL;
1.15 nicm 184:
1.4 nicm 185: /* Open the terminal if necessary. */
1.6 nicm 186: if (!detached && ctx->cmdclient != NULL) {
1.4 nicm 187: if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
188: ctx->error(ctx, "not a terminal");
1.1 nicm 189: return (-1);
190: }
1.25 nicm 191:
192: overrides =
1.9 nicm 193: options_get_string(&global_s_options, "terminal-overrides");
194: if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
1.4 nicm 195: ctx->error(ctx, "open terminal failed: %s", cause);
196: xfree(cause);
1.1 nicm 197: return (-1);
198: }
199: }
200:
1.16 nicm 201: /* Get the new session working directory. */
202: if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
203: cwd = ctx->cmdclient->cwd;
1.30 nicm 204: else {
205: pw = getpwuid(getuid());
206: if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
207: cwd = pw->pw_dir;
208: else
209: cwd = "/";
210: }
1.16 nicm 211:
212: /* Find new session size. */
1.6 nicm 213: if (detached) {
1.4 nicm 214: sx = 80;
1.18 nicm 215: sy = 24;
1.16 nicm 216: } else if (ctx->cmdclient != NULL) {
217: sx = ctx->cmdclient->tty.sx;
218: sy = ctx->cmdclient->tty.sy;
1.4 nicm 219: } else {
1.16 nicm 220: sx = ctx->curclient->tty.sx;
221: sy = ctx->curclient->tty.sy;
1.1 nicm 222: }
1.7 nicm 223: if (sy > 0 && options_get_number(&global_s_options, "status"))
1.4 nicm 224: sy--;
225: if (sx == 0)
226: sx = 1;
227: if (sy == 0)
228: sy = 1;
1.16 nicm 229:
230: /* Figure out the command for the new window. */
1.21 nicm 231: if (data->target != NULL)
232: cmd = NULL;
233: else if (data->cmd != NULL)
1.4 nicm 234: cmd = data->cmd;
1.1 nicm 235: else
1.7 nicm 236: cmd = options_get_string(&global_s_options, "default-command");
1.1 nicm 237:
1.10 nicm 238: /* Construct the environment. */
239: environ_init(&env);
240: update = options_get_string(&global_s_options, "update-environment");
241: if (ctx->cmdclient != NULL)
242: environ_update(update, &ctx->cmdclient->environ, &env);
1.11 nicm 243:
1.4 nicm 244: /* Create the new session. */
1.13 nicm 245: idx = -1 - options_get_number(&global_s_options, "base-index");
246: s = session_create(
1.19 nicm 247: data->newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause);
1.1 nicm 248: if (s == NULL) {
249: ctx->error(ctx, "create session failed: %s", cause);
250: xfree(cause);
251: return (-1);
252: }
1.10 nicm 253: environ_free(&env);
1.4 nicm 254:
1.16 nicm 255: /* Set the initial window name if one given. */
1.21 nicm 256: if (cmd != NULL && data->winname != NULL) {
1.16 nicm 257: w = s->curw->window;
258:
259: xfree(w->name);
260: w->name = xstrdup(data->winname);
261:
262: options_set_number(&w->options, "automatic-rename", 0);
1.21 nicm 263: }
264:
265: /*
266: * If a target session is given, this is to be part of a session group,
267: * so add it to the group and synchronize.
268: */
269: if (groupwith != NULL) {
270: session_group_add(groupwith, s);
271: session_group_synchronize_to(s);
272: session_select(s, RB_ROOT(&s->windows)->idx);
1.1 nicm 273: }
274:
1.16 nicm 275: /*
276: * Set the client to the new session. If a command client exists, it is
277: * taking this session and needs to get MSG_READY and stay around.
1.4 nicm 278: */
1.25 nicm 279: if (!detached) {
1.4 nicm 280: if (ctx->cmdclient != NULL) {
1.16 nicm 281: server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
1.32 ! nicm 282:
! 283: old_s = ctx->cmdclient->session;
! 284: if (old_s != NULL)
! 285: ctx->cmdclient->last_session = old_s;
1.25 nicm 286: ctx->cmdclient->session = s;
1.4 nicm 287: server_redraw_client(ctx->cmdclient);
288: } else {
1.32 ! nicm 289: old_s = ctx->curclient->session;
! 290: if (old_s != NULL)
! 291: ctx->curclient->last_session = old_s;
1.25 nicm 292: ctx->curclient->session = s;
1.4 nicm 293: server_redraw_client(ctx->curclient);
294: }
1.1 nicm 295: }
296: recalculate_sizes();
1.22 nicm 297: server_update_socket();
1.26 nicm 298:
299: /*
300: * If there are still configuration file errors to display, put the new
301: * session's current window into more mode and display them now.
302: */
1.27 nicm 303: if (cfg_finished && !ARRAY_EMPTY(&cfg_causes)) {
1.26 nicm 304: wp = s->curw->window->active;
1.29 nicm 305: window_pane_set_mode(wp, &window_copy_mode);
306: window_copy_init_for_output(wp);
1.27 nicm 307: for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
308: cause = ARRAY_ITEM(&cfg_causes, i);
1.29 nicm 309: window_copy_add(wp, "%s", cause);
1.27 nicm 310: xfree(cause);
1.26 nicm 311: }
1.27 nicm 312: ARRAY_FREE(&cfg_causes);
1.26 nicm 313: }
1.1 nicm 314:
1.12 nicm 315: return (!detached); /* 1 means don't tell command client to exit */
1.1 nicm 316: }
317:
318: void
319: cmd_new_session_free(struct cmd *self)
320: {
321: struct cmd_new_session_data *data = self->data;
322:
323: if (data->newname != NULL)
324: xfree(data->newname);
325: if (data->winname != NULL)
326: xfree(data->winname);
327: if (data->cmd != NULL)
328: xfree(data->cmd);
329: xfree(data);
330: }
331:
332: size_t
333: cmd_new_session_print(struct cmd *self, char *buf, size_t len)
334: {
335: struct cmd_new_session_data *data = self->data;
336: size_t off = 0;
337:
338: off += xsnprintf(buf, len, "%s", self->entry->name);
339: if (data == NULL)
340: return (off);
341: if (off < len && data->flag_detached)
342: off += xsnprintf(buf + off, len - off, " -d");
1.28 nicm 343: if (off < len && data->winname != NULL)
344: off += cmd_prarg(buf + off, len - off, " -n ", data->winname);
1.1 nicm 345: if (off < len && data->newname != NULL)
346: off += cmd_prarg(buf + off, len - off, " -s ", data->newname);
1.28 nicm 347: if (off < len && data->target != NULL)
348: off += cmd_prarg(buf + off, len - off, " -t ", data->target);
1.1 nicm 349: if (off < len && data->cmd != NULL)
350: off += cmd_prarg(buf + off, len - off, " ", data->cmd);
351: return (off);
352: }