Add -B to new-pane to block and return a return code from the process.

This commit is contained in:
Michael Grant
2026-06-09 00:14:10 +02:00
parent cb237b5193
commit 37560e3716
5 changed files with 74 additions and 3 deletions

View File

@@ -38,8 +38,8 @@ const struct cmd_entry cmd_new_pane_entry = {
.name = "new-pane", .name = "new-pane",
.alias = "newp", .alias = "newp",
.args = { "bc:de:EfF:hIkl:Lm:p:PR:s:S:t:vx:X:y:Y:Z", 0, -1, NULL }, .args = { "bBc:de:EfF:hIkl:Lm:p:PR:s:S:t:vx:X:y:Y:Z", 0, -1, NULL },
.usage = "[-bdefhIklPvZ] [-c start-directory] [-e environment] " .usage = "[-bBdefhIklPvZ] [-c start-directory] [-e environment] "
"[-F format] [-l size] [-m message] [-p percentage] " "[-F format] [-l size] [-m message] [-p percentage] "
"[-s style] [-S active-border-style] " "[-s style] [-S active-border-style] "
"[-R inactive-border-style] [-x width] [-y height] " "[-R inactive-border-style] [-x width] [-y height] "
@@ -238,5 +238,15 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
environ_free(sc.environ); environ_free(sc.environ);
if (input) if (input)
return (CMD_RETURN_WAIT); return (CMD_RETURN_WAIT);
/*
* With -B, block this command queue item until the pane's command
* exits; window_pane_block_finish() then sets the client return code
* and continues the queue (see server_child_exited()).
*/
if (args_has(args, 'B')) {
new_wp->block_item = item;
return (CMD_RETURN_WAIT);
}
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }

View File

@@ -497,6 +497,8 @@ server_child_exited(pid_t pid, int status)
log_debug("%%%u exited", wp->id); log_debug("%%%u exited", wp->id);
wp->flags |= PANE_EXITED; wp->flags |= PANE_EXITED;
window_pane_block_finish(wp);
if (window_pane_destroy_ready(wp)) if (window_pane_destroy_ready(wp))
server_destroy_pane(wp, 1); server_destroy_pane(wp, 1);
break; break;

18
tmux.1
View File

@@ -3330,7 +3330,7 @@ but a different format may be specified with
.Fl F . .Fl F .
.Tg newp .Tg newp
.It Xo Ic new\-pane .It Xo Ic new\-pane
.Op Fl bdefhIkPvZ .Op Fl bBdefhIkPvZ
.Op Fl c Ar start\-directory .Op Fl c Ar start\-directory
.Op Fl e Ar environment .Op Fl e Ar environment
.Op Fl F Ar format .Op Fl F Ar format
@@ -3403,6 +3403,22 @@ but also sets the
option for this pane to option for this pane to
.Ar message . .Ar message .
.Pp .Pp
.Fl B
blocks until the
.Ar shell\-command
running in the new pane exits, then returns its exit status as the exit
code of the issuing client.
This makes it possible to run a command in a pane from a script and act on
its result, for example a
.Xr whiptail 1
dialog.
Only the command queue of the issuing client waits; the server and other
clients are unaffected and the new pane remains interactive.
If the pane is killed or terminated by a signal, the status is
.Li 128
plus the signal number, as for
.Ic run\-shell .
.Pp
.Fl E , .Fl E ,
or an empty or an empty
.Ar shell\-command , .Ar shell\-command ,

2
tmux.h
View File

@@ -1294,6 +1294,7 @@ struct window_pane {
char tty[TTY_NAME_MAX]; char tty[TTY_NAME_MAX];
int status; int status;
struct timeval dead_time; struct timeval dead_time;
struct cmdq_item *block_item; /* new-pane -B: waiting for pane exit */
int fd; int fd;
struct bufferevent *event; struct bufferevent *event;
@@ -3422,6 +3423,7 @@ struct window *window_find_by_id(u_int);
void window_update_activity(struct window *); void window_update_activity(struct window *);
struct window *window_create(u_int, u_int, u_int, u_int); struct window *window_create(u_int, u_int, u_int, u_int);
void window_pane_set_event(struct window_pane *); void window_pane_set_event(struct window_pane *);
void window_pane_block_finish(struct window_pane *);
struct window_pane *window_get_active_at(struct window *, u_int, u_int); struct window_pane *window_get_active_at(struct window *, u_int, u_int);
struct window_pane *window_find_string(struct window *, const char *); struct window_pane *window_find_string(struct window *, const char *);
int window_has_floating_panes(struct window *); int window_has_floating_panes(struct window *);

View File

@@ -18,6 +18,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/wait.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
@@ -380,6 +381,15 @@ window_pane_destroy_ready(struct window_pane *wp)
if (~wp->flags & PANE_EXITED) if (~wp->flags & PANE_EXITED)
return (0); return (0);
/*
* If a command queue item is blocked on this pane (new-pane -B), wait
* for the child's exit status to be reaped before destroying it, so
* the correct return code is reported. The PTY EOF and SIGCHLD paths
* otherwise race and the pane could be freed with no status.
*/
if (wp->block_item != NULL && (~wp->flags & PANE_STATUSREADY))
return (0);
return (1); return (1);
} }
@@ -1064,12 +1074,43 @@ window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
return (wp); return (wp);
} }
/*
* Finish a new-pane -B wait: if this pane has a blocked command queue item,
* set the issuing client's return code from the pane's exit status and continue
* the queue. Idempotent and safe to call from any pane teardown path.
*/
void
window_pane_block_finish(struct window_pane *wp)
{
struct cmdq_item *item = wp->block_item;
struct client *c;
int retcode = 0;
if (item == NULL)
return;
wp->block_item = NULL;
if (wp->flags & PANE_STATUSREADY) {
if (WIFEXITED(wp->status))
retcode = WEXITSTATUS(wp->status);
else if (WIFSIGNALED(wp->status))
retcode = WTERMSIG(wp->status) + 128;
}
c = cmdq_get_client(item);
if (c != NULL && c->session == NULL)
c->retval = retcode;
cmdq_continue(item);
}
static void static void
window_pane_destroy(struct window_pane *wp) window_pane_destroy(struct window_pane *wp)
{ {
struct window_pane_resize *r; struct window_pane_resize *r;
struct window_pane_resize *r1; struct window_pane_resize *r1;
window_pane_block_finish(wp);
window_pane_reset_mode_all(wp); window_pane_reset_mode_all(wp);
free(wp->searchstr); free(wp->searchstr);