Skip to content

Commit 128db4a

Browse files
authored
Add stdio option to process.run (#199)
1 parent 5c470d0 commit 128db4a

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

process/src/process.cpp

+40-6
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ static void allocBuffer(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf
183183
}
184184
}
185185

186+
const std::string kStdioKindDefault = "default";
187+
const std::string kStdioKindInherit = "inherit";
188+
const std::string kStdioKindNone = "none";
189+
// TODO: add forwarding
190+
// const std::string kStdioKindForward = "forward";
191+
186192
int run(lua_State* L)
187193
{
188194
std::vector<std::string> args;
@@ -210,6 +216,7 @@ int run(lua_State* L)
210216
bool useShell = false;
211217
std::string customShell;
212218
std::string cwd;
219+
std::string stdioKind;
213220
std::map<std::string, std::string> env;
214221

215222
if (lua_istable(L, 2))
@@ -233,6 +240,14 @@ int run(lua_State* L)
233240

234241
lua_pop(L, 1);
235242

243+
lua_getfield(L, 2, "stdio");
244+
if (lua_isstring(L, -1))
245+
{
246+
stdioKind = lua_tostring(L, -1);
247+
// TODO: support stdin and separate stdout/stderr kinds
248+
}
249+
lua_pop(L, 1);
250+
236251
lua_getfield(L, 2, "env");
237252
if (lua_istable(L, -1))
238253
{
@@ -339,12 +354,31 @@ int run(lua_State* L)
339354

340355
options.stdio_count = 3;
341356
uv_stdio_container_t stdio[3];
342-
stdio[0].flags = UV_INHERIT_FD;
343-
stdio[0].data.fd = 0; // Inherit stdin
344-
stdio[1].flags = static_cast<uv_stdio_flags>(UV_CREATE_PIPE | UV_WRITABLE_PIPE);
345-
stdio[1].data.stream = (uv_stream_t*)&handle->stdoutPipe;
346-
stdio[2].flags = static_cast<uv_stdio_flags>(UV_CREATE_PIPE | UV_WRITABLE_PIPE);
347-
stdio[2].data.stream = (uv_stream_t*)&handle->stderrPipe;
357+
stdio[0].flags = UV_IGNORE;
358+
if (stdioKind == kStdioKindNone)
359+
{
360+
stdio[1].flags = UV_IGNORE;
361+
stdio[2].flags = UV_IGNORE;
362+
}
363+
else if (stdioKind == kStdioKindInherit)
364+
{
365+
stdio[1].flags = UV_INHERIT_FD;
366+
stdio[1].data.fd = fileno(stdout);
367+
stdio[2].flags = UV_INHERIT_FD;
368+
stdio[2].data.fd = fileno(stderr);
369+
}
370+
else if (stdioKind == kStdioKindDefault || stdioKind.empty())
371+
{
372+
stdio[1].flags = static_cast<uv_stdio_flags>(UV_CREATE_PIPE | UV_WRITABLE_PIPE);
373+
stdio[1].data.stream = (uv_stream_t*)&handle->stdoutPipe;
374+
stdio[2].flags = static_cast<uv_stdio_flags>(UV_CREATE_PIPE | UV_WRITABLE_PIPE);
375+
stdio[2].data.stream = (uv_stream_t*)&handle->stderrPipe;
376+
}
377+
else
378+
{
379+
luaL_error(L, "Invalid stdio kind: %s", stdioKind.c_str());
380+
return 0;
381+
}
348382
options.stdio = stdio;
349383

350384
handle->process.data = handle.get();

0 commit comments

Comments
 (0)