Annotation of src/usr.bin/tmux/cmd-new-session.c, Revision 1.10
1.10 ! nicm 1: /* $OpenBSD: cmd-new-session.c,v 1.9 2009/08/03 14:10:54 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:
21: #include "tmux.h"
22:
23: /*
24: * Create a new session and attach to the current terminal unless -d is given.
25: */
26:
27: int cmd_new_session_parse(struct cmd *, int, char **, char **);
28: int cmd_new_session_exec(struct cmd *, struct cmd_ctx *);
29: void cmd_new_session_free(struct cmd *);
30: void cmd_new_session_init(struct cmd *, int);
31: size_t cmd_new_session_print(struct cmd *, char *, size_t);
32:
33: struct cmd_new_session_data {
34: char *newname;
35: char *winname;
36: char *cmd;
37: int flag_detached;
38: };
39:
40: const struct cmd_entry cmd_new_session_entry = {
41: "new-session", "new",
42: "[-d] [-n window-name] [-s session-name] [command]",
1.10 ! nicm 43: CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON, 0,
1.1 nicm 44: cmd_new_session_init,
45: cmd_new_session_parse,
46: cmd_new_session_exec,
47: cmd_new_session_free,
48: cmd_new_session_print
49: };
50:
51: void
52: cmd_new_session_init(struct cmd *self, unused int arg)
53: {
54: struct cmd_new_session_data *data;
55:
56: self->data = data = xmalloc(sizeof *data);
57: data->flag_detached = 0;
58: data->newname = NULL;
59: data->winname = NULL;
60: data->cmd = NULL;
61: }
62:
63: int
64: cmd_new_session_parse(struct cmd *self, int argc, char **argv, char **cause)
65: {
66: struct cmd_new_session_data *data;
67: int opt;
68:
69: self->entry->init(self, 0);
70: data = self->data;
71:
72: while ((opt = getopt(argc, argv, "ds:n:")) != -1) {
73: switch (opt) {
74: case 'd':
75: data->flag_detached = 1;
76: break;
77: case 's':
78: if (data->newname == NULL)
79: data->newname = xstrdup(optarg);
80: break;
81: case 'n':
82: if (data->winname == NULL)
83: data->winname = xstrdup(optarg);
84: break;
85: default:
86: goto usage;
87: }
88: }
89: argc -= optind;
90: argv += optind;
91: if (argc != 0 && argc != 1)
92: goto usage;
93:
94: if (argc == 1)
95: data->cmd = xstrdup(argv[0]);
96:
97: return (0);
98:
99: usage:
100: xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
101:
102: self->entry->free(self);
103: return (-1);
104: }
105:
106: int
107: cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
108: {
109: struct cmd_new_session_data *data = self->data;
110: struct session *s;
1.10 ! nicm 111: struct environ env;
! 112: const char *update;
1.9 nicm 113: char *overrides, *cmd, *cwd, *cause;
1.6 nicm 114: int detached;
1.1 nicm 115: u_int sx, sy;
116:
1.4 nicm 117: if (data->newname != NULL && session_find(data->newname) != NULL) {
118: ctx->error(ctx, "duplicate session: %s", data->newname);
119: return (-1);
120: }
1.1 nicm 121:
1.4 nicm 122: /*
1.6 nicm 123: * There are three cases:
1.4 nicm 124: *
125: * 1. If cmdclient is non-NULL, new-session has been called from the
126: * command-line - cmdclient is to become a new attached, interactive
127: * client. Unless -d is given, the terminal must be opened and then
128: * the client sent MSG_READY.
129: *
130: * 2. If cmdclient is NULL, new-session has been called from an
131: * existing client (such as a key binding).
132: *
1.6 nicm 133: * 3. Both are NULL, the command was in the configuration file. Treat
134: * this as if -d was given even if it was not.
135: *
136: * In all cases, a new additional session needs to be created and
1.4 nicm 137: * (unless -d) set as the current session for the client.
138: */
139:
1.6 nicm 140: /* Set -d if no client. */
141: detached = data->flag_detached;
142: if (ctx->cmdclient == NULL && ctx->curclient == NULL)
143: detached = 1;
144:
1.4 nicm 145: /* Open the terminal if necessary. */
1.6 nicm 146: if (!detached && ctx->cmdclient != NULL) {
1.4 nicm 147: if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
148: ctx->error(ctx, "not a terminal");
1.1 nicm 149: return (-1);
150: }
1.4 nicm 151:
1.9 nicm 152: overrides =
153: options_get_string(&global_s_options, "terminal-overrides");
154: if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
1.4 nicm 155: ctx->error(ctx, "open terminal failed: %s", cause);
156: xfree(cause);
1.1 nicm 157: return (-1);
158: }
159: }
160:
1.4 nicm 161: /* Find new session size and options. */
1.6 nicm 162: if (detached) {
1.4 nicm 163: sx = 80;
164: sy = 25;
165: } else {
166: if (ctx->cmdclient != NULL) {
167: sx = ctx->cmdclient->tty.sx;
168: sy = ctx->cmdclient->tty.sy;
169: } else {
170: sx = ctx->curclient->tty.sx;
171: sy = ctx->curclient->tty.sy;
172: }
1.1 nicm 173: }
1.7 nicm 174: if (sy > 0 && options_get_number(&global_s_options, "status"))
1.4 nicm 175: sy--;
176: if (sx == 0)
177: sx = 1;
178: if (sy == 0)
179: sy = 1;
180: if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
181: cwd = ctx->cmdclient->cwd;
182: else
1.7 nicm 183: cwd = options_get_string(&global_s_options, "default-path");
1.4 nicm 184: if (data->cmd != NULL)
185: cmd = data->cmd;
1.1 nicm 186: else
1.7 nicm 187: cmd = options_get_string(&global_s_options, "default-command");
1.1 nicm 188:
1.10 ! nicm 189: /* Construct the environment. */
! 190: environ_init(&env);
! 191: update = options_get_string(&global_s_options, "update-environment");
! 192: if (ctx->cmdclient != NULL)
! 193: environ_update(update, &ctx->cmdclient->environ, &env);
! 194:
1.4 nicm 195: /* Create the new session. */
1.10 ! nicm 196: s = session_create(data->newname, cmd, cwd, &env, sx, sy, &cause);
1.1 nicm 197: if (s == NULL) {
198: ctx->error(ctx, "create session failed: %s", cause);
199: xfree(cause);
200: return (-1);
201: }
1.10 ! nicm 202: environ_free(&env);
1.4 nicm 203:
1.1 nicm 204: if (data->winname != NULL) {
205: xfree(s->curw->window->name);
206: s->curw->window->name = xstrdup(data->winname);
207: options_set_number(
208: &s->curw->window->options, "automatic-rename", 0);
209: }
210:
1.4 nicm 211: /*
212: * If a command client exists, it is either taking this session (and
213: * needs to get MSG_READY and stay around), or -d is given and it needs
214: * to exit.
215: */
216: if (ctx->cmdclient != NULL) {
1.6 nicm 217: if (!detached)
1.4 nicm 218: server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
219: else
220: server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);
221: }
222:
223: /* Set the client to the new session. */
1.6 nicm 224: if (!detached) {
1.4 nicm 225: if (ctx->cmdclient != NULL) {
226: ctx->cmdclient->session = s;
227: server_redraw_client(ctx->cmdclient);
228: } else {
229: ctx->curclient->session = s;
230: server_redraw_client(ctx->curclient);
231: }
1.1 nicm 232: }
233: recalculate_sizes();
234:
1.4 nicm 235: return (1); /* 1 means don't tell command client to exit */
1.1 nicm 236: }
237:
238: void
239: cmd_new_session_free(struct cmd *self)
240: {
241: struct cmd_new_session_data *data = self->data;
242:
243: if (data->newname != NULL)
244: xfree(data->newname);
245: if (data->winname != NULL)
246: xfree(data->winname);
247: if (data->cmd != NULL)
248: xfree(data->cmd);
249: xfree(data);
250: }
251:
252: size_t
253: cmd_new_session_print(struct cmd *self, char *buf, size_t len)
254: {
255: struct cmd_new_session_data *data = self->data;
256: size_t off = 0;
257:
258: off += xsnprintf(buf, len, "%s", self->entry->name);
259: if (data == NULL)
260: return (off);
261: if (off < len && data->flag_detached)
262: off += xsnprintf(buf + off, len - off, " -d");
263: if (off < len && data->newname != NULL)
264: off += cmd_prarg(buf + off, len - off, " -s ", data->newname);
265: if (off < len && data->winname != NULL)
266: off += cmd_prarg(buf + off, len - off, " -n ", data->winname);
267: if (off < len && data->cmd != NULL)
268: off += cmd_prarg(buf + off, len - off, " ", data->cmd);
269: return (off);
270: }