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