Annotation of src/usr.bin/tmux/cmd-new-session.c, Revision 1.9
1.9 ! nicm 1: /* $OpenBSD: cmd-new-session.c,v 1.8 2009/07/26 12:58:44 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.3 nicm 43: CMD_STARTSERVER|CMD_CANTNEST, 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.9 ! nicm 111: char *overrides, *cmd, *cwd, *cause;
1.6 nicm 112: int detached;
1.1 nicm 113: u_int sx, sy;
114:
1.4 nicm 115: if (data->newname != NULL && session_find(data->newname) != NULL) {
116: ctx->error(ctx, "duplicate session: %s", data->newname);
117: return (-1);
118: }
1.1 nicm 119:
1.4 nicm 120: /*
1.6 nicm 121: * There are three cases:
1.4 nicm 122: *
123: * 1. If cmdclient is non-NULL, new-session has been called from the
124: * command-line - cmdclient is to become a new attached, interactive
125: * client. Unless -d is given, the terminal must be opened and then
126: * the client sent MSG_READY.
127: *
128: * 2. If cmdclient is NULL, new-session has been called from an
129: * existing client (such as a key binding).
130: *
1.6 nicm 131: * 3. Both are NULL, the command was in the configuration file. Treat
132: * this as if -d was given even if it was not.
133: *
134: * In all cases, a new additional session needs to be created and
1.4 nicm 135: * (unless -d) set as the current session for the client.
136: */
137:
1.6 nicm 138: /* Set -d if no client. */
139: detached = data->flag_detached;
140: if (ctx->cmdclient == NULL && ctx->curclient == NULL)
141: detached = 1;
142:
1.4 nicm 143: /* Open the terminal if necessary. */
1.6 nicm 144: if (!detached && ctx->cmdclient != NULL) {
1.4 nicm 145: if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) {
146: ctx->error(ctx, "not a terminal");
1.1 nicm 147: return (-1);
148: }
1.4 nicm 149:
1.9 ! nicm 150: overrides =
! 151: options_get_string(&global_s_options, "terminal-overrides");
! 152: if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) {
1.4 nicm 153: ctx->error(ctx, "open terminal failed: %s", cause);
154: xfree(cause);
1.1 nicm 155: return (-1);
156: }
157: }
158:
1.4 nicm 159: /* Find new session size and options. */
1.6 nicm 160: if (detached) {
1.4 nicm 161: sx = 80;
162: sy = 25;
163: } else {
164: if (ctx->cmdclient != NULL) {
165: sx = ctx->cmdclient->tty.sx;
166: sy = ctx->cmdclient->tty.sy;
167: } else {
168: sx = ctx->curclient->tty.sx;
169: sy = ctx->curclient->tty.sy;
170: }
1.1 nicm 171: }
1.7 nicm 172: if (sy > 0 && options_get_number(&global_s_options, "status"))
1.4 nicm 173: sy--;
174: if (sx == 0)
175: sx = 1;
176: if (sy == 0)
177: sy = 1;
178: if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
179: cwd = ctx->cmdclient->cwd;
180: else
1.7 nicm 181: cwd = options_get_string(&global_s_options, "default-path");
1.4 nicm 182: if (data->cmd != NULL)
183: cmd = data->cmd;
1.1 nicm 184: else
1.7 nicm 185: cmd = options_get_string(&global_s_options, "default-command");
1.1 nicm 186:
1.4 nicm 187: /* Create the new session. */
1.1 nicm 188: s = session_create(data->newname, cmd, cwd, sx, sy, &cause);
189: if (s == NULL) {
190: ctx->error(ctx, "create session failed: %s", cause);
191: xfree(cause);
192: return (-1);
193: }
1.4 nicm 194:
1.1 nicm 195: if (data->winname != NULL) {
196: xfree(s->curw->window->name);
197: s->curw->window->name = xstrdup(data->winname);
198: options_set_number(
199: &s->curw->window->options, "automatic-rename", 0);
200: }
201:
1.4 nicm 202: /*
203: * If a command client exists, it is either taking this session (and
204: * needs to get MSG_READY and stay around), or -d is given and it needs
205: * to exit.
206: */
207: if (ctx->cmdclient != NULL) {
1.6 nicm 208: if (!detached)
1.4 nicm 209: server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
210: else
211: server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);
212: }
213:
214: /* Set the client to the new session. */
1.6 nicm 215: if (!detached) {
1.4 nicm 216: if (ctx->cmdclient != NULL) {
217: ctx->cmdclient->session = s;
218: server_redraw_client(ctx->cmdclient);
219: } else {
220: ctx->curclient->session = s;
221: server_redraw_client(ctx->curclient);
222: }
1.1 nicm 223: }
224: recalculate_sizes();
225:
1.4 nicm 226: return (1); /* 1 means don't tell command client to exit */
1.1 nicm 227: }
228:
229: void
230: cmd_new_session_free(struct cmd *self)
231: {
232: struct cmd_new_session_data *data = self->data;
233:
234: if (data->newname != NULL)
235: xfree(data->newname);
236: if (data->winname != NULL)
237: xfree(data->winname);
238: if (data->cmd != NULL)
239: xfree(data->cmd);
240: xfree(data);
241: }
242:
243: size_t
244: cmd_new_session_print(struct cmd *self, char *buf, size_t len)
245: {
246: struct cmd_new_session_data *data = self->data;
247: size_t off = 0;
248:
249: off += xsnprintf(buf, len, "%s", self->entry->name);
250: if (data == NULL)
251: return (off);
252: if (off < len && data->flag_detached)
253: off += xsnprintf(buf + off, len - off, " -d");
254: if (off < len && data->newname != NULL)
255: off += cmd_prarg(buf + off, len - off, " -s ", data->newname);
256: if (off < len && data->winname != NULL)
257: off += cmd_prarg(buf + off, len - off, " -n ", data->winname);
258: if (off < len && data->cmd != NULL)
259: off += cmd_prarg(buf + off, len - off, " ", data->cmd);
260: return (off);
261: }