Annotation of src/usr.bin/tmux/cmd-new-session.c, Revision 1.15
1.15 ! nicm 1: /* $OpenBSD: cmd-new-session.c,v 1.14 2009/08/19 14:32:15 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:
24: #define TTYDEFCHARS
25: #include <sys/ttydefaults.h>
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:
33: int cmd_new_session_parse(struct cmd *, int, char **, char **);
34: int cmd_new_session_exec(struct cmd *, struct cmd_ctx *);
35: void cmd_new_session_free(struct cmd *);
36: void cmd_new_session_init(struct cmd *, int);
37: size_t cmd_new_session_print(struct cmd *, char *, size_t);
38:
39: struct cmd_new_session_data {
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",
48: "[-d] [-n window-name] [-s session-name] [command]",
1.10 nicm 49: CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON, 0,
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:
57: void
58: cmd_new_session_init(struct cmd *self, unused int arg)
59: {
60: struct cmd_new_session_data *data;
61:
62: self->data = data = xmalloc(sizeof *data);
63: data->flag_detached = 0;
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:
75: self->entry->init(self, 0);
76: data = self->data;
77:
78: while ((opt = getopt(argc, argv, "ds:n:")) != -1) {
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;
87: case 'n':
88: if (data->winname == NULL)
89: data->winname = xstrdup(optarg);
90: break;
91: default:
92: goto usage;
93: }
94: }
95: argc -= optind;
96: argv += optind;
97: if (argc != 0 && argc != 1)
98: goto usage;
99:
100: if (argc == 1)
101: data->cmd = xstrdup(argv[0]);
102:
103: return (0);
104:
105: usage:
106: xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
107:
108: self->entry->free(self);
109: return (-1);
110: }
111:
112: int
113: cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
114: {
115: struct cmd_new_session_data *data = self->data;
116: struct session *s;
1.10 nicm 117: struct environ env;
1.11 nicm 118: struct termios tio;
1.10 nicm 119: const char *update;
1.9 nicm 120: char *overrides, *cmd, *cwd, *cause;
1.13 nicm 121: int detached, idx;
1.1 nicm 122: u_int sx, sy;
123:
1.4 nicm 124: if (data->newname != NULL && session_find(data->newname) != NULL) {
125: ctx->error(ctx, "duplicate session: %s", data->newname);
126: return (-1);
127: }
1.1 nicm 128:
1.4 nicm 129: /*
1.6 nicm 130: * There are three cases:
1.4 nicm 131: *
132: * 1. If cmdclient is non-NULL, new-session has been called from the
133: * command-line - cmdclient is to become a new attached, interactive
134: * client. Unless -d is given, the terminal must be opened and then
135: * the client sent MSG_READY.
136: *
137: * 2. If cmdclient is NULL, new-session has been called from an
138: * existing client (such as a key binding).
139: *
1.6 nicm 140: * 3. Both are NULL, the command was in the configuration file. Treat
141: * this as if -d was given even if it was not.
142: *
143: * In all cases, a new additional session needs to be created and
1.4 nicm 144: * (unless -d) set as the current session for the client.
145: */
146:
1.6 nicm 147: /* Set -d if no client. */
148: detached = data->flag_detached;
149: if (ctx->cmdclient == NULL && ctx->curclient == NULL)
150: detached = 1;
151:
1.15 ! nicm 152: /*
! 153: * Fill in the termios settings used for new windows in this session;
! 154: * if there is a command client, use the control characters from it.
! 155: *
! 156: * This is read again with tcgetattr() rather than using tty.tio as if
! 157: * detached, tty_open won't be called. Because of this, it must be done
! 158: * before opening the terminal as that calls tcsetattr() to prepare for
! 159: * tmux taking over.
! 160: */
! 161: if (ctx->cmdclient != NULL && ctx->cmdclient->tty.fd != -1) {
! 162: if (tcgetattr(ctx->cmdclient->tty.fd, &tio) != 0)
! 163: fatal("tcgetattr failed");
! 164: } else
! 165: memcpy(tio.c_cc, ttydefchars, sizeof tio.c_cc);
! 166: tio.c_iflag = TTYDEF_IFLAG;
! 167: tio.c_oflag = TTYDEF_OFLAG;
! 168: tio.c_lflag = TTYDEF_LFLAG;
! 169: tio.c_cflag = TTYDEF_CFLAG;
! 170: cfsetispeed(&tio, TTYDEF_SPEED);
! 171: cfsetospeed(&tio, TTYDEF_SPEED);
! 172:
1.4 nicm 173: /* Open the terminal if necessary. */
1.6 nicm 174: if (!detached && ctx->cmdclient != NULL) {
1.4 nicm 175: if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
176: ctx->error(ctx, "not a terminal");
1.1 nicm 177: return (-1);
178: }
1.4 nicm 179:
1.9 nicm 180: overrides =
181: options_get_string(&global_s_options, "terminal-overrides");
182: if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
1.4 nicm 183: ctx->error(ctx, "open terminal failed: %s", cause);
184: xfree(cause);
1.1 nicm 185: return (-1);
186: }
187: }
188:
1.4 nicm 189: /* Find new session size and options. */
1.6 nicm 190: if (detached) {
1.4 nicm 191: sx = 80;
192: sy = 25;
193: } else {
194: if (ctx->cmdclient != NULL) {
195: sx = ctx->cmdclient->tty.sx;
196: sy = ctx->cmdclient->tty.sy;
197: } else {
198: sx = ctx->curclient->tty.sx;
199: sy = ctx->curclient->tty.sy;
200: }
1.1 nicm 201: }
1.7 nicm 202: if (sy > 0 && options_get_number(&global_s_options, "status"))
1.4 nicm 203: sy--;
204: if (sx == 0)
205: sx = 1;
206: if (sy == 0)
207: sy = 1;
208: if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
209: cwd = ctx->cmdclient->cwd;
210: else
1.7 nicm 211: cwd = options_get_string(&global_s_options, "default-path");
1.4 nicm 212: if (data->cmd != NULL)
213: cmd = data->cmd;
1.1 nicm 214: else
1.7 nicm 215: cmd = options_get_string(&global_s_options, "default-command");
1.1 nicm 216:
1.10 nicm 217: /* Construct the environment. */
218: environ_init(&env);
219: update = options_get_string(&global_s_options, "update-environment");
220: if (ctx->cmdclient != NULL)
221: environ_update(update, &ctx->cmdclient->environ, &env);
1.11 nicm 222:
1.4 nicm 223: /* Create the new session. */
1.13 nicm 224: idx = -1 - options_get_number(&global_s_options, "base-index");
225: s = session_create(
226: data->newname, cmd, cwd, &env, &tio, idx, sx, sy, &cause);
1.1 nicm 227: if (s == NULL) {
228: ctx->error(ctx, "create session failed: %s", cause);
229: xfree(cause);
230: return (-1);
231: }
1.10 nicm 232: environ_free(&env);
1.4 nicm 233:
1.1 nicm 234: if (data->winname != NULL) {
235: xfree(s->curw->window->name);
236: s->curw->window->name = xstrdup(data->winname);
237: options_set_number(
238: &s->curw->window->options, "automatic-rename", 0);
239: }
240:
1.4 nicm 241: /*
242: * If a command client exists, it is either taking this session (and
243: * needs to get MSG_READY and stay around), or -d is given and it needs
244: * to exit.
245: */
246: if (ctx->cmdclient != NULL) {
1.6 nicm 247: if (!detached)
1.4 nicm 248: server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
249: }
250:
251: /* Set the client to the new session. */
1.6 nicm 252: if (!detached) {
1.4 nicm 253: if (ctx->cmdclient != NULL) {
254: ctx->cmdclient->session = s;
255: server_redraw_client(ctx->cmdclient);
256: } else {
257: ctx->curclient->session = s;
258: server_redraw_client(ctx->curclient);
259: }
1.1 nicm 260: }
261: recalculate_sizes();
262:
1.12 nicm 263: return (!detached); /* 1 means don't tell command client to exit */
1.1 nicm 264: }
265:
266: void
267: cmd_new_session_free(struct cmd *self)
268: {
269: struct cmd_new_session_data *data = self->data;
270:
271: if (data->newname != NULL)
272: xfree(data->newname);
273: if (data->winname != NULL)
274: xfree(data->winname);
275: if (data->cmd != NULL)
276: xfree(data->cmd);
277: xfree(data);
278: }
279:
280: size_t
281: cmd_new_session_print(struct cmd *self, char *buf, size_t len)
282: {
283: struct cmd_new_session_data *data = self->data;
284: size_t off = 0;
285:
286: off += xsnprintf(buf, len, "%s", self->entry->name);
287: if (data == NULL)
288: return (off);
289: if (off < len && data->flag_detached)
290: off += xsnprintf(buf + off, len - off, " -d");
291: if (off < len && data->newname != NULL)
292: off += cmd_prarg(buf + off, len - off, " -s ", data->newname);
293: if (off < len && data->winname != NULL)
294: off += cmd_prarg(buf + off, len - off, " -n ", data->winname);
295: if (off < len && data->cmd != NULL)
296: off += cmd_prarg(buf + off, len - off, " ", data->cmd);
297: return (off);
298: }