Coverage Report

Created: 2025-07-11 06:20

/src/tmux/cmd-split-window.c
Line
Count
Source (jump to first uncovered line)
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
0
#define SPLIT_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
34
35
static enum cmd_retval  cmd_split_window_exec(struct cmd *,
36
          struct cmdq_item *);
37
38
const struct cmd_entry cmd_split_window_entry = {
39
  .name = "split-window",
40
  .alias = "splitw",
41
42
  .args = { "bc:de:fF:hIl:p:Pt:vZ", 0, -1, NULL },
43
  .usage = "[-bdefhIPvZ] [-c start-directory] [-e environment] "
44
     "[-F format] [-l size] " CMD_TARGET_PANE_USAGE
45
     " [shell-command [argument ...]]",
46
47
  .target = { 't', CMD_FIND_PANE, 0 },
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
0
{
56
0
  struct args   *args = cmd_get_args(self);
57
0
  struct cmd_find_state *current = cmdq_get_current(item);
58
0
  struct cmd_find_state *target = cmdq_get_target(item);
59
0
  struct spawn_context   sc = { 0 };
60
0
  struct client   *tc = cmdq_get_target_client(item);
61
0
  struct session    *s = target->s;
62
0
  struct winlink    *wl = target->wl;
63
0
  struct window   *w = wl->window;
64
0
  struct window_pane  *wp = target->wp, *new_wp;
65
0
  enum layout_type   type;
66
0
  struct layout_cell  *lc;
67
0
  struct cmd_find_state  fs;
68
0
  int      size, flags, input;
69
0
  const char    *template;
70
0
  char      *cause = NULL, *cp;
71
0
  struct args_value *av;
72
0
  u_int      count = args_count(args), curval = 0;
73
74
0
  type = LAYOUT_TOPBOTTOM;
75
0
  if (args_has(args, 'h'))
76
0
    type = LAYOUT_LEFTRIGHT;
77
78
  /* If the 'p' flag is dropped then this bit can be moved into 'l'. */
79
0
  if (args_has(args, 'l') || args_has(args, 'p')) {
80
0
    if (args_has(args, 'f')) {
81
0
      if (type == LAYOUT_TOPBOTTOM)
82
0
        curval = w->sy;
83
0
      else
84
0
        curval = w->sx;
85
0
    } else {
86
0
      if (type == LAYOUT_TOPBOTTOM)
87
0
        curval = wp->sy;
88
0
      else
89
0
        curval = wp->sx;
90
0
    }
91
0
  }
92
93
0
  size = -1;
94
0
  if (args_has(args, 'l')) {
95
0
    size = args_percentage_and_expand(args, 'l', 0, INT_MAX, curval,
96
0
        item, &cause);
97
0
  } else if (args_has(args, 'p')) {
98
0
    size = args_strtonum_and_expand(args, 'p', 0, 100, item,
99
0
        &cause);
100
0
    if (cause == NULL)
101
0
      size = curval * size / 100;
102
0
  }
103
0
  if (cause != NULL) {
104
0
    cmdq_error(item, "size %s", cause);
105
0
    free(cause);
106
0
    return (CMD_RETURN_ERROR);
107
0
  }
108
109
0
  window_push_zoom(wp->window, 1, args_has(args, 'Z'));
110
0
  input = (args_has(args, 'I') && count == 0);
111
112
0
  flags = 0;
113
0
  if (args_has(args, 'b'))
114
0
    flags |= SPAWN_BEFORE;
115
0
  if (args_has(args, 'f'))
116
0
    flags |= SPAWN_FULLSIZE;
117
0
  if (input || (count == 1 && *args_string(args, 0) == '\0'))
118
0
    flags |= SPAWN_EMPTY;
119
120
0
  lc = layout_split_pane(wp, type, size, flags);
121
0
  if (lc == NULL) {
122
0
    cmdq_error(item, "no space for new pane");
123
0
    return (CMD_RETURN_ERROR);
124
0
  }
125
126
0
  sc.item = item;
127
0
  sc.s = s;
128
0
  sc.wl = wl;
129
130
0
  sc.wp0 = wp;
131
0
  sc.lc = lc;
132
133
0
  args_to_vector(args, &sc.argc, &sc.argv);
134
0
  sc.environ = environ_create();
135
136
0
  av = args_first_value(args, 'e');
137
0
  while (av != NULL) {
138
0
    environ_put(sc.environ, av->string, 0);
139
0
    av = args_next_value(av);
140
0
  }
141
142
0
  sc.idx = -1;
143
0
  sc.cwd = args_get(args, 'c');
144
145
0
  sc.flags = flags;
146
0
  if (args_has(args, 'd'))
147
0
    sc.flags |= SPAWN_DETACHED;
148
0
  if (args_has(args, 'Z'))
149
0
    sc.flags |= SPAWN_ZOOM;
150
151
0
  if ((new_wp = spawn_pane(&sc, &cause)) == NULL) {
152
0
    cmdq_error(item, "create pane failed: %s", cause);
153
0
    free(cause);
154
0
    if (sc.argv != NULL)
155
0
      cmd_free_argv(sc.argc, sc.argv);
156
0
    environ_free(sc.environ);
157
0
    return (CMD_RETURN_ERROR);
158
0
  }
159
0
  if (input) {
160
0
    switch (window_pane_start_input(new_wp, item, &cause)) {
161
0
    case -1:
162
0
      server_client_remove_pane(new_wp);
163
0
      layout_close_pane(new_wp);
164
0
      window_remove_pane(wp->window, new_wp);
165
0
      cmdq_error(item, "%s", cause);
166
0
      free(cause);
167
0
      if (sc.argv != NULL)
168
0
        cmd_free_argv(sc.argc, sc.argv);
169
0
      environ_free(sc.environ);
170
0
      return (CMD_RETURN_ERROR);
171
0
    case 1:
172
0
      input = 0;
173
0
      break;
174
0
    }
175
0
  }
176
0
  if (!args_has(args, 'd'))
177
0
    cmd_find_from_winlink_pane(current, wl, new_wp, 0);
178
0
  window_pop_zoom(wp->window);
179
0
  server_redraw_window(wp->window);
180
0
  server_status_session(s);
181
182
0
  if (args_has(args, 'P')) {
183
0
    if ((template = args_get(args, 'F')) == NULL)
184
0
      template = SPLIT_WINDOW_TEMPLATE;
185
0
    cp = format_single(item, template, tc, s, wl, new_wp);
186
0
    cmdq_print(item, "%s", cp);
187
0
    free(cp);
188
0
  }
189
190
0
  cmd_find_from_winlink_pane(&fs, wl, new_wp, 0);
191
0
  cmdq_insert_hook(s, item, &fs, "after-split-window");
192
193
0
  if (sc.argv != NULL)
194
0
    cmd_free_argv(sc.argc, sc.argv);
195
0
  environ_free(sc.environ);
196
0
  if (input)
197
0
    return (CMD_RETURN_WAIT);
198
0
  return (CMD_RETURN_NORMAL);
199
0
}