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