Annotation of src/usr.bin/tmux/cmd-new-session.c, Revision 1.8
1.8 ! nicm 1: /* $OpenBSD: cmd-new-session.c,v 1.7 2009/07/23 15:47:56 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;
111: char *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:
150: if (tty_open(&ctx->cmdclient->tty, &cause) != 0) {
151: ctx->error(ctx, "open terminal failed: %s", cause);
152: xfree(cause);
1.1 nicm 153: return (-1);
154: }
155: }
156:
1.4 nicm 157: /* Find new session size and options. */
1.6 nicm 158: if (detached) {
1.4 nicm 159: sx = 80;
160: sy = 25;
161: } else {
162: if (ctx->cmdclient != NULL) {
163: sx = ctx->cmdclient->tty.sx;
164: sy = ctx->cmdclient->tty.sy;
165: } else {
166: sx = ctx->curclient->tty.sx;
167: sy = ctx->curclient->tty.sy;
168: }
1.1 nicm 169: }
1.7 nicm 170: if (sy > 0 && options_get_number(&global_s_options, "status"))
1.4 nicm 171: sy--;
172: if (sx == 0)
173: sx = 1;
174: if (sy == 0)
175: sy = 1;
176: if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
177: cwd = ctx->cmdclient->cwd;
178: else
1.7 nicm 179: cwd = options_get_string(&global_s_options, "default-path");
1.4 nicm 180: if (data->cmd != NULL)
181: cmd = data->cmd;
1.1 nicm 182: else
1.7 nicm 183: cmd = options_get_string(&global_s_options, "default-command");
1.1 nicm 184:
1.4 nicm 185: /* Create the new session. */
1.1 nicm 186: s = session_create(data->newname, cmd, cwd, sx, sy, &cause);
187: if (s == NULL) {
188: ctx->error(ctx, "create session failed: %s", cause);
189: xfree(cause);
190: return (-1);
191: }
1.4 nicm 192:
1.1 nicm 193: if (data->winname != NULL) {
194: xfree(s->curw->window->name);
195: s->curw->window->name = xstrdup(data->winname);
196: options_set_number(
197: &s->curw->window->options, "automatic-rename", 0);
198: }
199:
1.4 nicm 200: /*
201: * If a command client exists, it is either taking this session (and
202: * needs to get MSG_READY and stay around), or -d is given and it needs
203: * to exit.
204: */
205: if (ctx->cmdclient != NULL) {
1.6 nicm 206: if (!detached)
1.4 nicm 207: server_write_client(ctx->cmdclient, MSG_READY, NULL, 0);
208: else
209: server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);
210: }
211:
212: /* Set the client to the new session. */
1.6 nicm 213: if (!detached) {
1.4 nicm 214: if (ctx->cmdclient != NULL) {
215: ctx->cmdclient->session = s;
216: server_redraw_client(ctx->cmdclient);
217: } else {
218: ctx->curclient->session = s;
219: server_redraw_client(ctx->curclient);
220: }
1.1 nicm 221: }
222: recalculate_sizes();
223:
1.4 nicm 224: return (1); /* 1 means don't tell command client to exit */
1.1 nicm 225: }
226:
227: void
228: cmd_new_session_free(struct cmd *self)
229: {
230: struct cmd_new_session_data *data = self->data;
231:
232: if (data->newname != NULL)
233: xfree(data->newname);
234: if (data->winname != NULL)
235: xfree(data->winname);
236: if (data->cmd != NULL)
237: xfree(data->cmd);
238: xfree(data);
239: }
240:
241: size_t
242: cmd_new_session_print(struct cmd *self, char *buf, size_t len)
243: {
244: struct cmd_new_session_data *data = self->data;
245: size_t off = 0;
246:
247: off += xsnprintf(buf, len, "%s", self->entry->name);
248: if (data == NULL)
249: return (off);
250: if (off < len && data->flag_detached)
251: off += xsnprintf(buf + off, len - off, " -d");
252: if (off < len && data->newname != NULL)
253: off += cmd_prarg(buf + off, len - off, " -s ", data->newname);
254: if (off < len && data->winname != NULL)
255: off += cmd_prarg(buf + off, len - off, " -n ", data->winname);
256: if (off < len && data->cmd != NULL)
257: off += cmd_prarg(buf + off, len - off, " ", data->cmd);
258: return (off);
259: }