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