Annotation of src/usr.bin/tmux/cmd-new-session.c, Revision 1.27
1.27 ! nicm 1: /* $OpenBSD: cmd-new-session.c,v 1.26 2010/02/06 17:15:33 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.26 nicm 125: struct window_pane *wp;
1.10 nicm 126: struct environ env;
1.19 nicm 127: struct termios tio, *tiop;
1.10 nicm 128: const char *update;
1.9 nicm 129: char *overrides, *cmd, *cwd, *cause;
1.13 nicm 130: int detached, idx;
1.26 nicm 131: u_int sx, sy, i;
1.1 nicm 132:
1.4 nicm 133: if (data->newname != NULL && session_find(data->newname) != NULL) {
134: ctx->error(ctx, "duplicate session: %s", data->newname);
135: return (-1);
136: }
1.1 nicm 137:
1.21 nicm 138: groupwith = NULL;
139: if (data->target != NULL &&
140: (groupwith = cmd_find_session(ctx, data->target)) == NULL)
141: return (-1);
142:
1.4 nicm 143: /*
1.6 nicm 144: * There are three cases:
1.4 nicm 145: *
146: * 1. If cmdclient is non-NULL, new-session has been called from the
147: * command-line - cmdclient is to become a new attached, interactive
148: * client. Unless -d is given, the terminal must be opened and then
149: * the client sent MSG_READY.
150: *
151: * 2. If cmdclient is NULL, new-session has been called from an
152: * existing client (such as a key binding).
153: *
1.6 nicm 154: * 3. Both are NULL, the command was in the configuration file. Treat
155: * this as if -d was given even if it was not.
156: *
157: * In all cases, a new additional session needs to be created and
1.4 nicm 158: * (unless -d) set as the current session for the client.
159: */
160:
1.6 nicm 161: /* Set -d if no client. */
162: detached = data->flag_detached;
163: if (ctx->cmdclient == NULL && ctx->curclient == NULL)
164: detached = 1;
165:
1.15 nicm 166: /*
1.19 nicm 167: * Save the termios settings, part of which is used for new windows in
168: * this session.
1.15 nicm 169: *
170: * This is read again with tcgetattr() rather than using tty.tio as if
171: * detached, tty_open won't be called. Because of this, it must be done
172: * before opening the terminal as that calls tcsetattr() to prepare for
173: * tmux taking over.
174: */
175: if (ctx->cmdclient != NULL && ctx->cmdclient->tty.fd != -1) {
176: if (tcgetattr(ctx->cmdclient->tty.fd, &tio) != 0)
177: fatal("tcgetattr failed");
1.19 nicm 178: tiop = &tio;
1.15 nicm 179: } else
1.19 nicm 180: tiop = NULL;
1.15 nicm 181:
1.4 nicm 182: /* Open the terminal if necessary. */
1.6 nicm 183: if (!detached && ctx->cmdclient != NULL) {
1.4 nicm 184: if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
185: ctx->error(ctx, "not a terminal");
1.1 nicm 186: return (-1);
187: }
1.25 nicm 188:
189: overrides =
1.9 nicm 190: options_get_string(&global_s_options, "terminal-overrides");
191: if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
1.4 nicm 192: ctx->error(ctx, "open terminal failed: %s", cause);
193: xfree(cause);
1.1 nicm 194: return (-1);
195: }
196: }
197:
1.16 nicm 198: /* Get the new session working directory. */
199: if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
200: cwd = ctx->cmdclient->cwd;
201: else
202: cwd = options_get_string(&global_s_options, "default-path");
203:
204: /* Find new session size. */
1.6 nicm 205: if (detached) {
1.4 nicm 206: sx = 80;
1.18 nicm 207: sy = 24;
1.16 nicm 208: } else if (ctx->cmdclient != NULL) {
209: sx = ctx->cmdclient->tty.sx;
210: sy = ctx->cmdclient->tty.sy;
1.4 nicm 211: } else {
1.16 nicm 212: sx = ctx->curclient->tty.sx;
213: sy = ctx->curclient->tty.sy;
1.1 nicm 214: }
1.7 nicm 215: if (sy > 0 && options_get_number(&global_s_options, "status"))
1.4 nicm 216: sy--;
217: if (sx == 0)
218: sx = 1;
219: if (sy == 0)
220: sy = 1;
1.16 nicm 221:
222: /* Figure out the command for the new window. */
1.21 nicm 223: if (data->target != NULL)
224: cmd = NULL;
225: else if (data->cmd != NULL)
1.4 nicm 226: cmd = data->cmd;
1.1 nicm 227: else
1.7 nicm 228: cmd = options_get_string(&global_s_options, "default-command");
1.1 nicm 229:
1.10 nicm 230: /* Construct the environment. */
231: environ_init(&env);
232: update = options_get_string(&global_s_options, "update-environment");
233: if (ctx->cmdclient != NULL)
234: environ_update(update, &ctx->cmdclient->environ, &env);
1.11 nicm 235:
1.4 nicm 236: /* Create the new session. */
1.13 nicm 237: idx = -1 - options_get_number(&global_s_options, "base-index");
238: s = session_create(
1.19 nicm 239: data->newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause);
1.1 nicm 240: if (s == NULL) {
241: ctx->error(ctx, "create session failed: %s", cause);
242: xfree(cause);
243: return (-1);
244: }
1.10 nicm 245: environ_free(&env);
1.4 nicm 246:
1.16 nicm 247: /* Set the initial window name if one given. */
1.21 nicm 248: if (cmd != NULL && data->winname != NULL) {
1.16 nicm 249: w = s->curw->window;
250:
251: xfree(w->name);
252: w->name = xstrdup(data->winname);
253:
254: options_set_number(&w->options, "automatic-rename", 0);
1.21 nicm 255: }
256:
257: /*
258: * If a target session is given, this is to be part of a session group,
259: * so add it to the group and synchronize.
260: */
261: if (groupwith != NULL) {
262: session_group_add(groupwith, s);
263: session_group_synchronize_to(s);
264: session_select(s, RB_ROOT(&s->windows)->idx);
1.1 nicm 265: }
266:
1.16 nicm 267: /*
268: * Set the client to the new session. If a command client exists, it is
269: * taking this session and needs to get MSG_READY and stay around.
1.4 nicm 270: */
1.25 nicm 271: if (!detached) {
1.4 nicm 272: if (ctx->cmdclient != NULL) {
1.16 nicm 273: server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
1.25 nicm 274: ctx->cmdclient->session = s;
1.4 nicm 275: server_redraw_client(ctx->cmdclient);
276: } else {
1.25 nicm 277: ctx->curclient->session = s;
1.4 nicm 278: server_redraw_client(ctx->curclient);
279: }
1.1 nicm 280: }
281: recalculate_sizes();
1.22 nicm 282: server_update_socket();
1.26 nicm 283:
284: /*
285: * If there are still configuration file errors to display, put the new
286: * session's current window into more mode and display them now.
287: */
1.27 ! nicm 288: if (cfg_finished && !ARRAY_EMPTY(&cfg_causes)) {
1.26 nicm 289: wp = s->curw->window->active;
290: window_pane_set_mode(wp, &window_more_mode);
1.27 ! nicm 291: for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
! 292: cause = ARRAY_ITEM(&cfg_causes, i);
! 293: window_more_add(wp, "%s", cause);
! 294: xfree(cause);
1.26 nicm 295: }
1.27 ! nicm 296: ARRAY_FREE(&cfg_causes);
1.26 nicm 297: }
1.1 nicm 298:
1.12 nicm 299: return (!detached); /* 1 means don't tell command client to exit */
1.1 nicm 300: }
301:
302: void
303: cmd_new_session_free(struct cmd *self)
304: {
305: struct cmd_new_session_data *data = self->data;
306:
307: if (data->newname != NULL)
308: xfree(data->newname);
309: if (data->winname != NULL)
310: xfree(data->winname);
311: if (data->cmd != NULL)
312: xfree(data->cmd);
313: xfree(data);
314: }
315:
316: size_t
317: cmd_new_session_print(struct cmd *self, char *buf, size_t len)
318: {
319: struct cmd_new_session_data *data = self->data;
320: size_t off = 0;
321:
322: off += xsnprintf(buf, len, "%s", self->entry->name);
323: if (data == NULL)
324: return (off);
325: if (off < len && data->flag_detached)
326: off += xsnprintf(buf + off, len - off, " -d");
327: if (off < len && data->newname != NULL)
328: off += cmd_prarg(buf + off, len - off, " -s ", data->newname);
329: if (off < len && data->winname != NULL)
330: off += cmd_prarg(buf + off, len - off, " -n ", data->winname);
331: if (off < len && data->cmd != NULL)
332: off += cmd_prarg(buf + off, len - off, " ", data->cmd);
333: return (off);
334: }