Annotation of src/usr.bin/tmux/cmd-new-window.c, Revision 1.41
1.41 ! nicm 1: /* $OpenBSD: cmd-new-window.c,v 1.40 2014/05/13 08:08:32 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:
1.35 nicm 21: #include <errno.h>
22: #include <fcntl.h>
1.1 nicm 23: #include <stdlib.h>
1.35 nicm 24: #include <string.h>
25: #include <unistd.h>
1.1 nicm 26:
27: #include "tmux.h"
28:
29: /*
30: * Create a new window.
31: */
32:
1.31 nicm 33: enum cmd_retval cmd_new_window_exec(struct cmd *, struct cmd_q *);
1.1 nicm 34:
35: const struct cmd_entry cmd_new_window_entry = {
36: "new-window", "neww",
1.40 nicm 37: "ac:dF:kn:Pt:", 0, -1,
1.21 nicm 38: "[-adkP] [-c start-directory] [-F format] [-n window-name] "
1.29 nicm 39: CMD_TARGET_WINDOW_USAGE " [command]",
1.17 nicm 40: 0,
41: NULL,
42: cmd_new_window_exec
1.1 nicm 43: };
44:
1.26 nicm 45: enum cmd_retval
1.31 nicm 46: cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 47: {
1.21 nicm 48: struct args *args = self->args;
49: struct session *s;
50: struct winlink *wl;
51: struct client *c;
1.39 nicm 52: const char *cmd, *path, *template;
1.40 nicm 53: char **argv, *cause, *cp;
54: int argc, idx, last, detached, cwd, fd = -1;
1.21 nicm 55: struct format_tree *ft;
1.39 nicm 56: struct environ_entry *envent;
1.17 nicm 57:
58: if (args_has(args, 'a')) {
1.31 nicm 59: wl = cmd_find_window(cmdq, args_get(args, 't'), &s);
1.17 nicm 60: if (wl == NULL)
1.26 nicm 61: return (CMD_RETURN_ERROR);
1.13 nicm 62: idx = wl->idx + 1;
63:
64: /* Find the next free index. */
65: for (last = idx; last < INT_MAX; last++) {
66: if (winlink_find_by_index(&s->windows, last) == NULL)
67: break;
68: }
69: if (last == INT_MAX) {
1.31 nicm 70: cmdq_error(cmdq, "no free window indexes");
1.26 nicm 71: return (CMD_RETURN_ERROR);
1.13 nicm 72: }
73:
74: /* Move everything from last - 1 to idx up a bit. */
75: for (; last > idx; last--) {
76: wl = winlink_find_by_index(&s->windows, last - 1);
77: server_link_window(s, wl, s, last, 0, 0, NULL);
78: server_unlink_window(s, wl);
79: }
80: } else {
1.39 nicm 81: idx = cmd_find_index(cmdq, args_get(args, 't'), &s);
82: if (idx == -2)
1.26 nicm 83: return (CMD_RETURN_ERROR);
1.13 nicm 84: }
1.17 nicm 85: detached = args_has(args, 'd');
1.1 nicm 86:
1.40 nicm 87: if (args->argc == 0) {
1.1 nicm 88: cmd = options_get_string(&s->options, "default-command");
1.40 nicm 89: if (cmd != NULL && *cmd != '\0') {
90: argc = 1;
1.41 ! nicm 91: argv = (char **)&cmd;
1.40 nicm 92: } else {
93: argc = 0;
94: argv = NULL;
95: }
96: } else {
97: argc = args->argc;
98: argv = args->argv;
99: }
1.35 nicm 100:
1.39 nicm 101: path = NULL;
102: if (cmdq->client != NULL && cmdq->client->session == NULL)
103: envent = environ_find(&cmdq->client->environ, "PATH");
104: else
105: envent = environ_find(&s->environ, "PATH");
106: if (envent != NULL)
107: path = envent->value;
108:
1.35 nicm 109: if (args_has(args, 'c')) {
110: ft = format_create();
111: if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
112: format_client(ft, c);
113: format_session(ft, s);
114: format_winlink(ft, s, s->curw);
115: format_window_pane(ft, s->curw->window->active);
116: cp = format_expand(ft, args_get(args, 'c'));
117: format_free(ft);
118:
1.37 nicm 119: if (cp != NULL && *cp != '\0') {
120: fd = open(cp, O_RDONLY|O_DIRECTORY);
121: free(cp);
122: if (fd == -1) {
123: cmdq_error(cmdq, "bad working directory: %s",
124: strerror(errno));
125: return (CMD_RETURN_ERROR);
126: }
127: } else if (cp != NULL)
128: free(cp);
1.35 nicm 129: cwd = fd;
1.36 nicm 130: } else if (cmdq->client != NULL && cmdq->client->session == NULL)
1.35 nicm 131: cwd = cmdq->client->cwd;
132: else
133: cwd = s->cwd;
1.38 nicm 134:
135: wl = NULL;
136: if (idx != -1)
137: wl = winlink_find_by_index(&s->windows, idx);
138: if (wl != NULL && args_has(args, 'k')) {
139: /*
140: * Can't use session_detach as it will destroy session if this
141: * makes it empty.
142: */
143: notify_window_unlinked(s, wl->window);
144: wl->flags &= ~WINLINK_ALERTFLAGS;
145: winlink_stack_remove(&s->lastw, wl);
146: winlink_remove(&s->windows, wl);
147:
148: /* Force select/redraw if current. */
149: if (wl == s->curw) {
150: detached = 0;
151: s->curw = NULL;
152: }
153: }
1.1 nicm 154:
1.6 nicm 155: if (idx == -1)
156: idx = -1 - options_get_number(&s->options, "base-index");
1.40 nicm 157: wl = session_new(s, args_get(args, 'n'), argc, argv, path, cwd, idx,
158: &cause);
1.1 nicm 159: if (wl == NULL) {
1.31 nicm 160: cmdq_error(cmdq, "create window failed: %s", cause);
1.25 nicm 161: free(cause);
1.35 nicm 162: goto error;
1.1 nicm 163: }
1.17 nicm 164: if (!detached) {
1.1 nicm 165: session_select(s, wl->idx);
1.8 nicm 166: server_redraw_session_group(s);
1.11 nicm 167: } else
1.8 nicm 168: server_status_session_group(s);
1.1 nicm 169:
1.21 nicm 170: if (args_has(args, 'P')) {
1.24 nicm 171: if ((template = args_get(args, 'F')) == NULL)
1.27 nicm 172: template = NEW_WINDOW_TEMPLATE;
1.21 nicm 173:
174: ft = format_create();
1.31 nicm 175: if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
176: format_client(ft, c);
1.21 nicm 177: format_session(ft, s);
178: format_winlink(ft, s, wl);
1.24 nicm 179: format_window_pane(ft, wl->window->active);
1.21 nicm 180:
181: cp = format_expand(ft, template);
1.31 nicm 182: cmdq_print(cmdq, "%s", cp);
1.25 nicm 183: free(cp);
1.21 nicm 184:
185: format_free(ft);
186: }
187:
1.35 nicm 188: if (fd != -1)
189: close(fd);
1.26 nicm 190: return (CMD_RETURN_NORMAL);
1.35 nicm 191:
192: error:
193: if (fd != -1)
194: close(fd);
195: return (CMD_RETURN_ERROR);
1.1 nicm 196: }