Annotation of src/usr.bin/tmux/cmd-new-window.c, Revision 1.50
1.50 ! nicm 1: /* $OpenBSD: cmd-new-window.c,v 1.49 2015/10/28 09:51:55 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: */
1.43 nicm 32:
33: #define NEW_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
1.1 nicm 34:
1.31 nicm 35: enum cmd_retval cmd_new_window_exec(struct cmd *, struct cmd_q *);
1.1 nicm 36:
37: const struct cmd_entry cmd_new_window_entry = {
38: "new-window", "neww",
1.40 nicm 39: "ac:dF:kn:Pt:", 0, -1,
1.21 nicm 40: "[-adkP] [-c start-directory] [-F format] [-n window-name] "
1.29 nicm 41: CMD_TARGET_WINDOW_USAGE " [command]",
1.17 nicm 42: 0,
43: cmd_new_window_exec
1.1 nicm 44: };
45:
1.26 nicm 46: enum cmd_retval
1.31 nicm 47: cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 48: {
1.21 nicm 49: struct args *args = self->args;
50: struct session *s;
51: struct winlink *wl;
1.50 ! nicm 52: const char *cmd, *path, *template, *cwd, *to_free;
1.40 nicm 53: char **argv, *cause, *cp;
1.50 ! nicm 54: int argc, idx, detached;
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.46 nicm 62: if ((idx = winlink_shuffle_up(s, wl)) == -1) {
1.31 nicm 63: cmdq_error(cmdq, "no free window indexes");
1.26 nicm 64: return (CMD_RETURN_ERROR);
1.13 nicm 65: }
66: } else {
1.39 nicm 67: idx = cmd_find_index(cmdq, args_get(args, 't'), &s);
68: if (idx == -2)
1.26 nicm 69: return (CMD_RETURN_ERROR);
1.13 nicm 70: }
1.17 nicm 71: detached = args_has(args, 'd');
1.1 nicm 72:
1.40 nicm 73: if (args->argc == 0) {
1.48 nicm 74: cmd = options_get_string(s->options, "default-command");
1.40 nicm 75: if (cmd != NULL && *cmd != '\0') {
76: argc = 1;
1.41 nicm 77: argv = (char **)&cmd;
1.40 nicm 78: } else {
79: argc = 0;
80: argv = NULL;
81: }
82: } else {
83: argc = args->argc;
84: argv = args->argv;
85: }
1.35 nicm 86:
1.39 nicm 87: path = NULL;
88: if (cmdq->client != NULL && cmdq->client->session == NULL)
1.49 nicm 89: envent = environ_find(cmdq->client->environ, "PATH");
1.39 nicm 90: else
1.49 nicm 91: envent = environ_find(s->environ, "PATH");
1.39 nicm 92: if (envent != NULL)
93: path = envent->value;
94:
1.50 ! nicm 95: to_free = NULL;
1.35 nicm 96: if (args_has(args, 'c')) {
97: ft = format_create();
1.44 nicm 98: format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, NULL,
99: NULL);
1.50 ! nicm 100: cwd = format_expand(ft, args_get(args, 'c'));
1.35 nicm 101: format_free(ft);
102:
1.50 ! nicm 103: if (access(cwd, X_OK) != 0) {
! 104: free((void *)cwd);
! 105: cmdq_error(cmdq, "bad working directory: %s",
! 106: strerror(errno));
! 107: return (CMD_RETURN_ERROR);
! 108: }
1.36 nicm 109: } else if (cmdq->client != NULL && cmdq->client->session == NULL)
1.35 nicm 110: cwd = cmdq->client->cwd;
111: else
112: cwd = s->cwd;
1.38 nicm 113:
114: wl = NULL;
115: if (idx != -1)
116: wl = winlink_find_by_index(&s->windows, idx);
117: if (wl != NULL && args_has(args, 'k')) {
118: /*
119: * Can't use session_detach as it will destroy session if this
120: * makes it empty.
121: */
122: notify_window_unlinked(s, wl->window);
123: wl->flags &= ~WINLINK_ALERTFLAGS;
124: winlink_stack_remove(&s->lastw, wl);
125: winlink_remove(&s->windows, wl);
126:
127: /* Force select/redraw if current. */
128: if (wl == s->curw) {
129: detached = 0;
130: s->curw = NULL;
131: }
132: }
1.1 nicm 133:
1.6 nicm 134: if (idx == -1)
1.48 nicm 135: idx = -1 - options_get_number(s->options, "base-index");
1.40 nicm 136: wl = session_new(s, args_get(args, 'n'), argc, argv, path, cwd, idx,
137: &cause);
1.1 nicm 138: if (wl == NULL) {
1.31 nicm 139: cmdq_error(cmdq, "create window failed: %s", cause);
1.25 nicm 140: free(cause);
1.35 nicm 141: goto error;
1.1 nicm 142: }
1.17 nicm 143: if (!detached) {
1.1 nicm 144: session_select(s, wl->idx);
1.8 nicm 145: server_redraw_session_group(s);
1.11 nicm 146: } else
1.8 nicm 147: server_status_session_group(s);
1.1 nicm 148:
1.21 nicm 149: if (args_has(args, 'P')) {
1.24 nicm 150: if ((template = args_get(args, 'F')) == NULL)
1.27 nicm 151: template = NEW_WINDOW_TEMPLATE;
1.21 nicm 152:
153: ft = format_create();
1.44 nicm 154: format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, wl,
155: NULL);
1.21 nicm 156:
157: cp = format_expand(ft, template);
1.31 nicm 158: cmdq_print(cmdq, "%s", cp);
1.25 nicm 159: free(cp);
1.21 nicm 160:
161: format_free(ft);
162: }
163:
1.50 ! nicm 164: if (to_free != NULL)
! 165: free((void *)to_free);
1.26 nicm 166: return (CMD_RETURN_NORMAL);
1.35 nicm 167:
168: error:
1.50 ! nicm 169: if (to_free != NULL)
! 170: free((void *)to_free);
1.35 nicm 171: return (CMD_RETURN_ERROR);
1.1 nicm 172: }