From e81bdb41e2bd6fe024804b4e015a75d1ce35c48f Mon Sep 17 00:00:00 2001 From: Nick Winans Date: Wed, 23 Apr 2025 22:51:26 -0700 Subject: [PATCH 1/4] Add stdio inherit to process.create --- process/src/process.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/process/src/process.cpp b/process/src/process.cpp index 55ee4e23..f45273c9 100644 --- a/process/src/process.cpp +++ b/process/src/process.cpp @@ -210,6 +210,7 @@ int create(lua_State* L) bool useShell = false; std::string customShell; std::string cwd; + bool inheritStdio = false; std::map env; if (lua_istable(L, 2)) @@ -233,6 +234,18 @@ int create(lua_State* L) lua_pop(L, 1); + lua_getfield(L, 2, "stdio"); + if (lua_isstring(L, -1)) + { + std::string stdioOpt = lua_tostring(L, -1); + if (stdioOpt == "inherit") + { + inheritStdio = true; + } + // TODO: handle custom stdio + } + lua_pop(L, 1); + lua_getfield(L, 2, "env"); if (lua_istable(L, -1)) { @@ -341,10 +354,20 @@ int create(lua_State* L) uv_stdio_container_t stdio[3]; stdio[0].flags = UV_INHERIT_FD; stdio[0].data.fd = 0; // Inherit stdin - stdio[1].flags = static_cast(UV_CREATE_PIPE | UV_WRITABLE_PIPE); - stdio[1].data.stream = (uv_stream_t*)&handle->stdoutPipe; - stdio[2].flags = static_cast(UV_CREATE_PIPE | UV_WRITABLE_PIPE); - stdio[2].data.stream = (uv_stream_t*)&handle->stderrPipe; + if (inheritStdio) + { + stdio[1].flags = UV_INHERIT_FD; + stdio[1].data.fd = 1; // Inherit stdout + stdio[2].flags = UV_INHERIT_FD; + stdio[2].data.fd = 2; // Inherit stderr + } + else + { + stdio[1].flags = static_cast(UV_CREATE_PIPE | UV_WRITABLE_PIPE); + stdio[1].data.stream = (uv_stream_t*)&handle->stdoutPipe; + stdio[2].flags = static_cast(UV_CREATE_PIPE | UV_WRITABLE_PIPE); + stdio[2].data.stream = (uv_stream_t*)&handle->stderrPipe; + } options.stdio = stdio; handle->process.data = handle.get(); From 0b135e651eee3e33473017d28e88f70e2ed06957 Mon Sep 17 00:00:00 2001 From: Nick Winans Date: Fri, 25 Apr 2025 17:47:33 -0700 Subject: [PATCH 2/4] Add stdio kinds --- process/src/process.cpp | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/process/src/process.cpp b/process/src/process.cpp index f45273c9..1766664d 100644 --- a/process/src/process.cpp +++ b/process/src/process.cpp @@ -183,6 +183,12 @@ static void allocBuffer(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf } } +const std::string kStdioKindDefault = "default"; +const std::string kStdioKindInherit = "inherit"; +const std::string kStdioKindNone = "none"; +// TODO: add forwarding +// const std::string kStdioKindForward = "forward"; + int create(lua_State* L) { std::vector args; @@ -210,7 +216,7 @@ int create(lua_State* L) bool useShell = false; std::string customShell; std::string cwd; - bool inheritStdio = false; + std::string stdioKind; std::map env; if (lua_istable(L, 2)) @@ -237,12 +243,8 @@ int create(lua_State* L) lua_getfield(L, 2, "stdio"); if (lua_isstring(L, -1)) { - std::string stdioOpt = lua_tostring(L, -1); - if (stdioOpt == "inherit") - { - inheritStdio = true; - } - // TODO: handle custom stdio + stdioKind = lua_tostring(L, -1); + // TODO: support stdin and separate stdout/stderr kinds } lua_pop(L, 1); @@ -352,22 +354,31 @@ int create(lua_State* L) options.stdio_count = 3; uv_stdio_container_t stdio[3]; - stdio[0].flags = UV_INHERIT_FD; - stdio[0].data.fd = 0; // Inherit stdin - if (inheritStdio) + stdio[0].flags = UV_IGNORE; + if (stdioKind == kStdioKindNone) + { + stdio[1].flags = UV_IGNORE; + stdio[2].flags = UV_IGNORE; + } + else if (stdioKind == kStdioKindInherit) { stdio[1].flags = UV_INHERIT_FD; stdio[1].data.fd = 1; // Inherit stdout stdio[2].flags = UV_INHERIT_FD; stdio[2].data.fd = 2; // Inherit stderr } - else + else if (stdioKind == kStdioKindDefault || stdioKind.empty()) { stdio[1].flags = static_cast(UV_CREATE_PIPE | UV_WRITABLE_PIPE); stdio[1].data.stream = (uv_stream_t*)&handle->stdoutPipe; stdio[2].flags = static_cast(UV_CREATE_PIPE | UV_WRITABLE_PIPE); stdio[2].data.stream = (uv_stream_t*)&handle->stderrPipe; } + else + { + luaL_error(L, "Invalid stdio kind: %s", stdioKind.c_str()); + return 0; + } options.stdio = stdio; handle->process.data = handle.get(); From bbed6fdc19cd0cb250c62326b2b9ee4dc5ac7900 Mon Sep 17 00:00:00 2001 From: Nick Winans Date: Fri, 25 Apr 2025 17:55:05 -0700 Subject: [PATCH 3/4] remove useless comments --- process/src/process.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/process/src/process.cpp b/process/src/process.cpp index 1766664d..ee6c0a8e 100644 --- a/process/src/process.cpp +++ b/process/src/process.cpp @@ -363,9 +363,9 @@ int create(lua_State* L) else if (stdioKind == kStdioKindInherit) { stdio[1].flags = UV_INHERIT_FD; - stdio[1].data.fd = 1; // Inherit stdout + stdio[1].data.fd = 1; stdio[2].flags = UV_INHERIT_FD; - stdio[2].data.fd = 2; // Inherit stderr + stdio[2].data.fd = 2; } else if (stdioKind == kStdioKindDefault || stdioKind.empty()) { From 8cc3322648a6381faf52b02d798b3f0b16684407 Mon Sep 17 00:00:00 2001 From: Nick Winans Date: Sun, 27 Apr 2025 20:57:50 -0700 Subject: [PATCH 4/4] use fileno --- process/src/process.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/process/src/process.cpp b/process/src/process.cpp index ab517d77..1c0bbf48 100644 --- a/process/src/process.cpp +++ b/process/src/process.cpp @@ -363,9 +363,9 @@ int run(lua_State* L) else if (stdioKind == kStdioKindInherit) { stdio[1].flags = UV_INHERIT_FD; - stdio[1].data.fd = 1; + stdio[1].data.fd = fileno(stdout); stdio[2].flags = UV_INHERIT_FD; - stdio[2].data.fd = 2; + stdio[2].data.fd = fileno(stderr); } else if (stdioKind == kStdioKindDefault || stdioKind.empty()) {