Skip to content

Commit b394004

Browse files
committed
fix(cli): preserve piped stdin in direct script runner
1 parent dbd0eea commit b394004

3 files changed

Lines changed: 21 additions & 8 deletions

File tree

src/commands/run/RunFlow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ namespace vix::commands::RunCommand::detail
623623

624624
if (code == 130)
625625
{
626-
hint("Server interrupted by user (SIGINT).");
626+
hint("Program interrupted by user (SIGINT).");
627627
return;
628628
}
629629

src/commands/run/RunProcess.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ namespace vix::commands::RunCommand::detail
955955
::kill(pid, sig);
956956
}
957957

958-
void child_exec_shell(const std::string &cmd, int masterFd, int slaveFd, bool useSan)
958+
void child_exec_shell(const std::string &cmd, int masterFd, int slaveFd, bool useSan, bool inheritParentStdin)
959959
{
960960
struct sigaction saChild{};
961961
saChild.sa_handler = SIG_DFL;
@@ -996,7 +996,9 @@ namespace vix::commands::RunCommand::detail
996996

997997
::close(masterFd);
998998

999-
::dup2(slaveFd, STDIN_FILENO);
999+
if (!inheritParentStdin)
1000+
::dup2(slaveFd, STDIN_FILENO);
1001+
10001002
::dup2(slaveFd, STDOUT_FILENO);
10011003
::dup2(slaveFd, STDERR_FILENO);
10021004

@@ -1119,15 +1121,21 @@ namespace vix::commands::RunCommand::detail
11191121
return result;
11201122
}
11211123

1124+
const bool stdinIsTty = (::isatty(STDIN_FILENO) != 0);
1125+
const bool inheritParentStdin = passthroughRuntime && !stdinIsTty;
1126+
bool forwardStdin = passthroughRuntime && stdinIsTty;
1127+
11221128
if (pid == 0)
1123-
child_exec_shell(cmd, pty.masterFd, pty.slaveFd, useSan);
1129+
child_exec_shell(
1130+
cmd,
1131+
pty.masterFd,
1132+
pty.slaveFd,
1133+
useSan,
1134+
inheritParentStdin);
11241135

11251136
close_safe(pty.slaveFd);
11261137
::setpgid(pid, pid);
11271138

1128-
const bool forwardStdin =
1129-
passthroughRuntime && (::isatty(STDIN_FILENO) != 0);
1130-
11311139
bool spinnerActive = false;
11321140
std::size_t frameIndex = 0;
11331141

@@ -1281,10 +1289,15 @@ namespace vix::commands::RunCommand::detail
12811289
{
12821290
char inbuf[4096];
12831291
const ssize_t n = ::read(STDIN_FILENO, inbuf, sizeof(inbuf));
1292+
12841293
if (n > 0)
12851294
{
12861295
write_all(pty.masterFd, inbuf, static_cast<std::size_t>(n));
12871296
}
1297+
else if (n == 0)
1298+
{
1299+
forwardStdin = false;
1300+
}
12881301
}
12891302

12901303
if (pty.masterFd >= 0 && FD_ISSET(pty.masterFd, &fds))

src/commands/run/RunScript.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ namespace vix::commands::RunCommand::detail
681681

682682
if (interruptedBySigint)
683683
{
684-
hint("Server interrupted by user (SIGINT).");
684+
hint("Program interrupted by user (SIGINT).");
685685
return 0;
686686
}
687687

0 commit comments

Comments
 (0)