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:dfF:l:hp:Pt:v", 0, -1 },
42 	.usage = "[-bdfhvP] [-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
cmd_split_window_exec(struct cmd * self,struct cmd_q * cmdq)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 = (char **)&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 	    args_has(args, 'f'));
135 	if (lc == NULL) {
136 		cause = xstrdup("pane too small");
137 		goto error;
138 	}
139 	new_wp = window_add_pane(w, wp, hlimit);
140 	layout_assign_pane(lc, new_wp);
141 
142 	path = NULL;
143 	if (cmdq->client != NULL && cmdq->client->session == NULL)
144 		envent = environ_find(cmdq->client->environ, "PATH");
145 	else
146 		envent = environ_find(s->environ, "PATH");
147 	if (envent != NULL)
148 		path = envent->value;
149 
150 	if (window_pane_spawn(new_wp, argc, argv, path, shell, cwd, env,
151 	    s->tio, &cause) != 0)
152 		goto error;
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 	environ_free(env);
164 
165 	if (args_has(args, 'P')) {
166 		if ((template = args_get(args, 'F')) == NULL)
167 			template = SPLIT_WINDOW_TEMPLATE;
168 
169 		ft = format_create(cmdq, 0);
170 		format_defaults(ft, cmdq->state.c, s, wl, new_wp);
171 
172 		cp = format_expand(ft, template);
173 		cmdq_print(cmdq, "%s", cp);
174 		free(cp);
175 
176 		format_free(ft);
177 	}
178 	notify_window_layout_changed(w);
179 
180 	if (to_free != NULL)
181 		free((void *)to_free);
182 	return (CMD_RETURN_NORMAL);
183 
184 error:
185 	environ_free(env);
186 	if (new_wp != NULL) {
187 		layout_close_pane(new_wp);
188 		window_remove_pane(w, new_wp);
189 	}
190 	cmdq_error(cmdq, "create pane failed: %s", cause);
191 	free(cause);
192 
193 	if (to_free != NULL)
194 		free((void *)to_free);
195 	return (CMD_RETURN_ERROR);
196 }
197