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