1 /* $OpenBSD: cmd-new-window.c,v 1.79 2019/04/28 20:05:50 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> 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 <errno.h> 22 #include <fcntl.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <unistd.h> 26 27 #include "tmux.h" 28 29 /* 30 * Create a new window. 31 */ 32 33 #define NEW_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}" 34 35 static enum cmd_retval cmd_new_window_exec(struct cmd *, struct cmdq_item *); 36 37 const struct cmd_entry cmd_new_window_entry = { 38 .name = "new-window", 39 .alias = "neww", 40 41 .args = { "ac:de:F:kn:Pt:", 0, -1 }, 42 .usage = "[-adkP] [-c start-directory] [-e environment] [-F format] " 43 "[-n window-name] " CMD_TARGET_WINDOW_USAGE " [command]", 44 45 .target = { 't', CMD_FIND_WINDOW, CMD_FIND_WINDOW_INDEX }, 46 47 .flags = 0, 48 .exec = cmd_new_window_exec 49 }; 50 51 static enum cmd_retval 52 cmd_new_window_exec(struct cmd *self, struct cmdq_item *item) 53 { 54 struct args *args = self->args; 55 struct cmd_find_state *current = &item->shared->current; 56 struct spawn_context sc; 57 struct client *c = cmd_find_client(item, NULL, 1); 58 struct session *s = item->target.s; 59 struct winlink *wl = item->target.wl; 60 int idx = item->target.idx; 61 struct winlink *new_wl; 62 char *cause = NULL, *cp; 63 const char *template, *add; 64 struct cmd_find_state fs; 65 struct args_value *value; 66 67 if (args_has(args, 'a') && (idx = winlink_shuffle_up(s, wl)) == -1) { 68 cmdq_error(item, "couldn't get a window index"); 69 return (CMD_RETURN_ERROR); 70 } 71 72 memset(&sc, 0, sizeof sc); 73 sc.item = item; 74 sc.s = s; 75 76 sc.name = args_get(args, 'n'); 77 sc.argc = args->argc; 78 sc.argv = args->argv; 79 sc.environ = environ_create(); 80 81 add = args_first_value(args, 'e', &value); 82 while (add != NULL) { 83 environ_put(sc.environ, add); 84 add = args_next_value(&value); 85 } 86 87 sc.idx = idx; 88 sc.cwd = args_get(args, 'c'); 89 90 sc.flags = 0; 91 if (args_has(args, 'd')) 92 sc.flags |= SPAWN_DETACHED; 93 if (args_has(args, 'k')) 94 sc.flags |= SPAWN_KILL; 95 96 if ((new_wl = spawn_window(&sc, &cause)) == NULL) { 97 cmdq_error(item, "create window failed: %s", cause); 98 free(cause); 99 return (CMD_RETURN_ERROR); 100 } 101 if (!args_has(args, 'd') || new_wl == s->curw) { 102 cmd_find_from_winlink(current, new_wl, 0); 103 server_redraw_session_group(s); 104 } else 105 server_status_session_group(s); 106 107 if (args_has(args, 'P')) { 108 if ((template = args_get(args, 'F')) == NULL) 109 template = NEW_WINDOW_TEMPLATE; 110 cp = format_single(item, template, c, s, new_wl, NULL); 111 cmdq_print(item, "%s", cp); 112 free(cp); 113 } 114 115 cmd_find_from_winlink(&fs, new_wl, 0); 116 cmdq_insert_hook(s, item, &fs, "after-new-window"); 117 118 environ_free(sc.environ); 119 return (CMD_RETURN_NORMAL); 120 } 121