Annotation of src/usr.bin/tmux/cmd-new-session.c, Revision 1.2
1.2 ! nicm 1: /* $OpenBSD: cmd-new-session.c,v 1.1 2009/06/01 22:58:49 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]",
45: CMD_STARTSERVER|CMD_CANTNEST,
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 client *c = ctx->cmdclient;
115: struct session *s;
116: char *cmd, *cwd, *cause;
117: u_int sx, sy;
118:
119: if (ctx->curclient != NULL)
120: return (0);
121:
122: if (!data->flag_detached) {
123: if (c == NULL) {
124: ctx->error(ctx, "no client to attach to");
125: return (-1);
126: }
127: if (!(c->flags & CLIENT_TERMINAL)) {
128: ctx->error(ctx, "not a terminal");
129: return (-1);
130: }
131: }
132:
133: if (data->newname != NULL && session_find(data->newname) != NULL) {
134: ctx->error(ctx, "duplicate session: %s", data->newname);
135: return (-1);
136: }
137:
138: cmd = data->cmd;
139: if (cmd == NULL)
1.2 ! nicm 140: cmd = options_get_string(&global_s_options, "default-command");
1.1 nicm 141: if (c == NULL || c->cwd == NULL)
1.2 ! nicm 142: cwd = options_get_string(&global_s_options, "default-path");
1.1 nicm 143: else
144: cwd = c->cwd;
145:
146: sx = 80;
147: sy = 25;
148: if (!data->flag_detached) {
149: sx = c->tty.sx;
150: sy = c->tty.sy;
151: }
152:
1.2 ! nicm 153: if (options_get_number(&global_s_options, "status")) {
1.1 nicm 154: if (sy == 0)
155: sy = 1;
156: else
157: sy--;
158: }
159:
160: if (!data->flag_detached && tty_open(&c->tty, &cause) != 0) {
161: ctx->error(ctx, "open terminal failed: %s", cause);
162: xfree(cause);
163: return (-1);
164: }
165:
166:
167: s = session_create(data->newname, cmd, cwd, sx, sy, &cause);
168: if (s == NULL) {
169: ctx->error(ctx, "create session failed: %s", cause);
170: xfree(cause);
171: return (-1);
172: }
173: if (data->winname != NULL) {
174: xfree(s->curw->window->name);
175: s->curw->window->name = xstrdup(data->winname);
176: options_set_number(
177: &s->curw->window->options, "automatic-rename", 0);
178: }
179:
180: if (data->flag_detached) {
181: if (c != NULL)
182: server_write_client(c, MSG_EXIT, NULL, 0);
183: } else {
184: c->session = s;
185: server_write_client(c, MSG_READY, NULL, 0);
186: server_redraw_client(c);
187: }
188: recalculate_sizes();
189:
190: return (1);
191: }
192:
193: void
194: cmd_new_session_send(struct cmd *self, struct buffer *b)
195: {
196: struct cmd_new_session_data *data = self->data;
197:
198: buffer_write(b, data, sizeof *data);
199: cmd_send_string(b, data->newname);
200: cmd_send_string(b, data->winname);
201: cmd_send_string(b, data->cmd);
202: }
203:
204: void
205: cmd_new_session_recv(struct cmd *self, struct buffer *b)
206: {
207: struct cmd_new_session_data *data;
208:
209: self->data = data = xmalloc(sizeof *data);
210: buffer_read(b, data, sizeof *data);
211: data->newname = cmd_recv_string(b);
212: data->winname = cmd_recv_string(b);
213: data->cmd = cmd_recv_string(b);
214: }
215:
216: void
217: cmd_new_session_free(struct cmd *self)
218: {
219: struct cmd_new_session_data *data = self->data;
220:
221: if (data->newname != NULL)
222: xfree(data->newname);
223: if (data->winname != NULL)
224: xfree(data->winname);
225: if (data->cmd != NULL)
226: xfree(data->cmd);
227: xfree(data);
228: }
229:
230: size_t
231: cmd_new_session_print(struct cmd *self, char *buf, size_t len)
232: {
233: struct cmd_new_session_data *data = self->data;
234: size_t off = 0;
235:
236: off += xsnprintf(buf, len, "%s", self->entry->name);
237: if (data == NULL)
238: return (off);
239: if (off < len && data->flag_detached)
240: off += xsnprintf(buf + off, len - off, " -d");
241: if (off < len && data->newname != NULL)
242: off += cmd_prarg(buf + off, len - off, " -s ", data->newname);
243: if (off < len && data->winname != NULL)
244: off += cmd_prarg(buf + off, len - off, " -n ", data->winname);
245: if (off < len && data->cmd != NULL)
246: off += cmd_prarg(buf + off, len - off, " ", data->cmd);
247: return (off);
248: }