xref: /openbsd-src/usr.bin/tmux/cmd-split-window.c (revision 1adc95ebcfac5fbbc6375f336c8233de42e202a6)
1 /* $OpenBSD: cmd-split-window.c,v 1.81 2017/04/21 17:22:20 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 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 <paths.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include "tmux.h"
29 
30 /*
31  * Split a window (add a new pane).
32  */
33 
34 #define SPLIT_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
35 
36 static enum cmd_retval	cmd_split_window_exec(struct cmd *,
37 			    struct cmdq_item *);
38 
39 const struct cmd_entry cmd_split_window_entry = {
40 	.name = "split-window",
41 	.alias = "splitw",
42 
43 	.args = { "bc:dfF:l:hp:Pt:v", 0, -1 },
44 	.usage = "[-bdfhvP] [-c start-directory] [-F format] "
45 		 "[-p percentage|-l size] " CMD_TARGET_PANE_USAGE " [command]",
46 
47 	.tflag = CMD_PANE,
48 
49 	.flags = 0,
50 	.exec = cmd_split_window_exec
51 };
52 
53 static enum cmd_retval
54 cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
55 {
56 	struct args		*args = self->args;
57 	struct client		*c = item->state.c;
58 	struct session		*s = item->state.tflag.s;
59 	struct winlink		*wl = item->state.tflag.wl;
60 	struct window		*w = wl->window;
61 	struct window_pane	*wp = item->state.tflag.wp, *new_wp = NULL;
62 	struct environ		*env;
63 	const char		*cmd, *path, *shell, *template, *cwd, *to_free;
64 	char		       **argv, *cause, *new_cause, *cp;
65 	u_int			 hlimit;
66 	int			 argc, size, percentage;
67 	enum layout_type	 type;
68 	struct layout_cell	*lc;
69 	struct environ_entry	*envent;
70 	struct cmd_find_state    fs;
71 
72 	server_unzoom_window(w);
73 
74 	if (args->argc == 0) {
75 		cmd = options_get_string(s->options, "default-command");
76 		if (cmd != NULL && *cmd != '\0') {
77 			argc = 1;
78 			argv = (char **)&cmd;
79 		} else {
80 			argc = 0;
81 			argv = NULL;
82 		}
83 	} else {
84 		argc = args->argc;
85 		argv = args->argv;
86 	}
87 
88 	to_free = NULL;
89 	if (args_has(args, 'c')) {
90 		cwd = args_get(args, 'c');
91 		to_free = cwd = format_single(item, cwd, c, s, NULL, NULL);
92 	} else if (item->client != NULL && item->client->session == NULL)
93 		cwd = item->client->cwd;
94 	else
95 		cwd = s->cwd;
96 
97 	type = LAYOUT_TOPBOTTOM;
98 	if (args_has(args, 'h'))
99 		type = LAYOUT_LEFTRIGHT;
100 
101 	size = -1;
102 	if (args_has(args, 'l')) {
103 		size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
104 		if (cause != NULL) {
105 			xasprintf(&new_cause, "size %s", cause);
106 			free(cause);
107 			cause = new_cause;
108 			goto error;
109 		}
110 	} else if (args_has(args, 'p')) {
111 		percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause);
112 		if (cause != NULL) {
113 			xasprintf(&new_cause, "percentage %s", cause);
114 			free(cause);
115 			cause = new_cause;
116 			goto error;
117 		}
118 		if (type == LAYOUT_TOPBOTTOM)
119 			size = (wp->sy * percentage) / 100;
120 		else
121 			size = (wp->sx * percentage) / 100;
122 	}
123 	hlimit = options_get_number(s->options, "history-limit");
124 
125 	shell = options_get_string(s->options, "default-shell");
126 	if (*shell == '\0' || areshell(shell))
127 		shell = _PATH_BSHELL;
128 
129 	lc = layout_split_pane(wp, type, size, args_has(args, 'b'),
130 	    args_has(args, 'f'));
131 	if (lc == NULL) {
132 		cause = xstrdup("pane too small");
133 		goto error;
134 	}
135 	new_wp = window_add_pane(w, wp, args_has(args, 'b'), hlimit);
136 	layout_assign_pane(lc, new_wp);
137 
138 	path = NULL;
139 	if (item->client != NULL && item->client->session == NULL)
140 		envent = environ_find(item->client->environ, "PATH");
141 	else
142 		envent = environ_find(s->environ, "PATH");
143 	if (envent != NULL)
144 		path = envent->value;
145 
146 	env = environ_for_session(s);
147 	if (window_pane_spawn(new_wp, argc, argv, path, shell, cwd, env,
148 	    s->tio, &cause) != 0) {
149 		environ_free(env);
150 		goto error;
151 	}
152 	environ_free(env);
153 
154 	server_redraw_window(w);
155 
156 	if (!args_has(args, 'd')) {
157 		window_set_active_pane(w, new_wp);
158 		session_select(s, wl->idx);
159 		server_redraw_session(s);
160 	} else
161 		server_status_session(s);
162 
163 	if (args_has(args, 'P')) {
164 		if ((template = args_get(args, 'F')) == NULL)
165 			template = SPLIT_WINDOW_TEMPLATE;
166 		cp = format_single(item, template, c, s, wl, new_wp);
167 		cmdq_print(item, "%s", cp);
168 		free(cp);
169 	}
170 	notify_window("window-layout-changed", w);
171 
172 	if (to_free != NULL)
173 		free((void *)to_free);
174 
175 	cmd_find_from_winlink_pane(&fs, wl, new_wp);
176 	hooks_insert(s->hooks, item, &fs, "after-split-window");
177 
178 	return (CMD_RETURN_NORMAL);
179 
180 error:
181 	if (new_wp != NULL) {
182 		layout_close_pane(new_wp);
183 		window_remove_pane(w, new_wp);
184 	}
185 	cmdq_error(item, "create pane failed: %s", cause);
186 	free(cause);
187 
188 	if (to_free != NULL)
189 		free((void *)to_free);
190 	return (CMD_RETURN_ERROR);
191 }
192