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