summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-12-05 00:15:52 +0800
committerruki <[email protected]>2020-12-05 00:15:52 +0800
commit026fcce91e056d53cfeb1abe87c11c6548908bce (patch)
tree46a586bd568140298d1b78f216c8c67b70cfcd3e
parent3ccbf254fd5a0013dfc91e272d47c74dd6ebd181 (diff)
support stdin for os.execv
-rw-r--r--core/src/xmake/process/open.c56
-rw-r--r--core/src/xmake/process/openv.c71
-rw-r--r--xmake/core/base/os.lua4
-rw-r--r--xmake/core/base/process.lua32
4 files changed, 147 insertions, 16 deletions
diff --git a/core/src/xmake/process/open.c b/core/src/xmake/process/open.c
index f3ec6c522..e18f86243 100644
--- a/core/src/xmake/process/open.c
+++ b/core/src/xmake/process/open.c
@@ -36,7 +36,10 @@
*/
/* p = process.open(command,
- * {outpath = "", errpath = "", outfile = "", errfile = "", outpipe = "", errpipe = "", envs = {"PATH=xxx", "XXX=yyy"}})
+ * {outpath = "", errpath = "", outfile = "",
+ * errfile = "", outpipe = "", errpipe = "",
+ * infile = "", inpipe = "", inpipe = "",
+ * envs = {"PATH=xxx", "XXX=yyy"}})
*/
tb_int_t xm_process_open(lua_State* lua)
{
@@ -54,10 +57,13 @@ tb_int_t xm_process_open(lua_State* lua)
// get option arguments
tb_size_t envn = 0;
tb_char_t const* envs[256] = {0};
+ tb_char_t const* inpath = tb_null;
tb_char_t const* outpath = tb_null;
tb_char_t const* errpath = tb_null;
+ xm_io_file_t* infile = tb_null;
xm_io_file_t* outfile = tb_null;
xm_io_file_t* errfile = tb_null;
+ tb_pipe_file_ref_t inpipe = tb_null;
tb_pipe_file_ref_t outpipe = tb_null;
tb_pipe_file_ref_t errpipe = tb_null;
if (lua_istable(lua, 2))
@@ -75,6 +81,12 @@ tb_int_t xm_process_open(lua_State* lua)
attr.curdir = lua_tostring(lua, -1);
lua_pop(lua, 1);
+ // get inpath
+ lua_pushstring(lua, "inpath");
+ lua_gettable(lua, 2);
+ inpath = lua_tostring(lua, -1);
+ lua_pop(lua, 1);
+
// get outpath
lua_pushstring(lua, "outpath");
lua_gettable(lua, 2);
@@ -87,6 +99,15 @@ tb_int_t xm_process_open(lua_State* lua)
errpath = lua_tostring(lua, -1);
lua_pop(lua, 1);
+ // get infile
+ if (!inpath)
+ {
+ lua_pushstring(lua, "infile");
+ lua_gettable(lua, 3);
+ infile = (xm_io_file_t*)lua_touserdata(lua, -1);
+ lua_pop(lua, 1);
+ }
+
// get outfile
if (!outpath)
{
@@ -105,6 +126,15 @@ tb_int_t xm_process_open(lua_State* lua)
lua_pop(lua, 1);
}
+ // get inpipe
+ if (!inpath && !infile)
+ {
+ lua_pushstring(lua, "inpipe");
+ lua_gettable(lua, 3);
+ inpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1);
+ lua_pop(lua, 1);
+ }
+
// get outpipe
if (!outpath && !outfile)
{
@@ -166,6 +196,30 @@ tb_int_t xm_process_open(lua_State* lua)
lua_pop(lua, 1);
}
+ // redirect stdin?
+ if (inpath)
+ {
+ // redirect stdin to file
+ attr.inpath = inpath;
+ attr.inmode = TB_FILE_MODE_RO;
+ attr.intype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
+ }
+ else if (infile && xm_io_file_is_file(infile))
+ {
+ tb_file_ref_t rawfile = tb_null;
+ if (tb_stream_ctrl(infile->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) && rawfile)
+ {
+ attr.infile = rawfile;
+ attr.intype = TB_PROCESS_REDIRECT_TYPE_FILE;
+ }
+ }
+ else if (inpipe)
+ {
+ attr.inpipe = inpipe;
+ attr.intype = TB_PROCESS_REDIRECT_TYPE_PIPE;
+ }
+
+
// redirect stdout?
if (outpath)
{
diff --git a/core/src/xmake/process/openv.c b/core/src/xmake/process/openv.c
index 6e7f212fb..5b7e49082 100644
--- a/core/src/xmake/process/openv.c
+++ b/core/src/xmake/process/openv.c
@@ -36,7 +36,10 @@
*/
/* p = process.openv(shellname, argv,
- * {outpath = "", errpath = "", outfile = , errfile = , outpipe = , errpipe, envs = {"PATH=xxx", "XXX=yyy"})
+ * {outpath = "", errpath = "", outfile = "",
+ * errfile = "", outpipe = "", errpipe = "",
+ * infile = "", inpipe = "", inpipe = "",
+ * envs = {"PATH=xxx", "XXX=yyy"}})
*/
tb_int_t xm_process_openv(lua_State* lua)
{
@@ -94,14 +97,17 @@ tb_int_t xm_process_openv(lua_State* lua)
tb_process_attr_t attr = {0};
// get option arguments
- tb_size_t envn = 0;
- tb_char_t const* envs[256] = {0};
- tb_char_t const* outpath = tb_null;
- tb_char_t const* errpath = tb_null;
- xm_io_file_t* outfile = tb_null;
- xm_io_file_t* errfile = tb_null;
- tb_pipe_file_ref_t outpipe = tb_null;
- tb_pipe_file_ref_t errpipe = tb_null;
+ tb_size_t envn = 0;
+ tb_char_t const* envs[256] = {0};
+ tb_char_t const* inpath = tb_null;
+ tb_char_t const* outpath = tb_null;
+ tb_char_t const* errpath = tb_null;
+ xm_io_file_t* infile = tb_null;
+ xm_io_file_t* outfile = tb_null;
+ xm_io_file_t* errfile = tb_null;
+ tb_pipe_file_ref_t inpipe = tb_null;
+ tb_pipe_file_ref_t outpipe = tb_null;
+ tb_pipe_file_ref_t errpipe = tb_null;
if (lua_istable(lua, 3))
{
// is detached?
@@ -117,6 +123,12 @@ tb_int_t xm_process_openv(lua_State* lua)
attr.curdir = lua_tostring(lua, -1);
lua_pop(lua, 1);
+ // get inpath
+ lua_pushstring(lua, "inpath");
+ lua_gettable(lua, 3);
+ inpath = lua_tostring(lua, -1);
+ lua_pop(lua, 1);
+
// get outpath
lua_pushstring(lua, "outpath");
lua_gettable(lua, 3);
@@ -129,6 +141,15 @@ tb_int_t xm_process_openv(lua_State* lua)
errpath = lua_tostring(lua, -1);
lua_pop(lua, 1);
+ // get infile
+ if (!inpath)
+ {
+ lua_pushstring(lua, "infile");
+ lua_gettable(lua, 3);
+ infile = (xm_io_file_t*)lua_touserdata(lua, -1);
+ lua_pop(lua, 1);
+ }
+
// get outfile
if (!outpath)
{
@@ -147,6 +168,15 @@ tb_int_t xm_process_openv(lua_State* lua)
lua_pop(lua, 1);
}
+ // get inpipe
+ if (!inpath && !infile)
+ {
+ lua_pushstring(lua, "inpipe");
+ lua_gettable(lua, 3);
+ inpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1);
+ lua_pop(lua, 1);
+ }
+
// get outpipe
if (!outpath && !outfile)
{
@@ -208,6 +238,29 @@ tb_int_t xm_process_openv(lua_State* lua)
lua_pop(lua, 1);
}
+ // redirect stdin?
+ if (inpath)
+ {
+ // redirect stdin to file
+ attr.inpath = inpath;
+ attr.inmode = TB_FILE_MODE_RO;
+ attr.intype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
+ }
+ else if (infile && xm_io_file_is_file(infile))
+ {
+ tb_file_ref_t rawfile = tb_null;
+ if (tb_stream_ctrl(infile->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) && rawfile)
+ {
+ attr.infile = rawfile;
+ attr.intype = TB_PROCESS_REDIRECT_TYPE_FILE;
+ }
+ }
+ else if (inpipe)
+ {
+ attr.inpipe = inpipe;
+ attr.intype = TB_PROCESS_REDIRECT_TYPE_PIPE;
+ }
+
// redirect stdout?
if (outpath)
{
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 6923aaa0c..2442cea95 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -662,7 +662,7 @@ end
-- @param program "clang", "xcrun -sdk macosx clang", "~/dir/test\ xxx/clang"
-- filename "clang", "xcrun"", "~/dir/test\ xxx/clang"
-- @param argv the arguments
--- @param opt the options, e.g. {stdout = filepath/file/pipe, stderr = filepath/file/pipe,
+-- @param opt the options, e.g. {stdin = filepath/file/pipe, stdout = filepath/file/pipe, stderr = filepath/file/pipe,
-- envs = {PATH = "xxx;xx", CFLAGS = "xx"}}
--
function os.execv(program, argv, opt)
@@ -700,7 +700,7 @@ function os.execv(program, argv, opt)
end
-- init open options
- local openopt = {envs = envs, stdout = opt.stdout, stderr = opt.stderr, curdir = opt.curdir, detach = opt.detach}
+ local openopt = {envs = envs, stdin = opt.stdin, stdout = opt.stdout, stderr = opt.stderr, curdir = opt.curdir, detach = opt.detach}
-- open command
local ok = -1
diff --git a/xmake/core/base/process.lua b/xmake/core/base/process.lua
index 40755d6ef..23a3882f4 100644
--- a/xmake/core/base/process.lua
+++ b/xmake/core/base/process.lua
@@ -163,14 +163,26 @@ end
-- open a subprocess
--
-- @param command the process command
--- @param opt the option arguments, e.g. {stdout = filepath/file/pipe, stderr = filepath/file/pipe, envs = {"PATH=xxx", "XXX=yyy"}})
+-- @param opt the option arguments, e.g. {stdin = filepath/file/pipe, stdout = filepath/file/pipe, stderr = filepath/file/pipe, envs = {"PATH=xxx", "XXX=yyy"}})
--
-- @return the subprocess
--
function process.open(command, opt)
- -- get stdout and pass to subprocess
+ -- get stdin and pass to subprocess
opt = opt or {}
+ local stdin = opt.stdin
+ if type(stdin) == "string" then
+ opt.inpath = stdin
+ elseif type(stdin) == "table" then
+ if stdin.otype and stdin:otype() == 2 then
+ opt.inpipe = stdin:cdata()
+ else
+ opt.infile = stdin:cdata()
+ end
+ end
+
+ -- get stdout and pass to subprocess
local stdout = opt.stdout
if type(stdout) == "string" then
opt.outpath = stdout
@@ -207,14 +219,26 @@ end
--
-- @param program the program
-- @param argv the arguments list
--- @param opt the option arguments, e.g. {stdout = filepath/file/pipe, stderr = filepath/file/pipe, envs = {"PATH=xxx", "XXX=yyy"}})
+-- @param opt the option arguments, e.g. {stdin = filepath/file/pipe, stdout = filepath/file/pipe, stderr = filepath/file/pipe, envs = {"PATH=xxx", "XXX=yyy"}})
--
-- @return the subprocess
--
function process.openv(program, argv, opt)
- -- get stdout and pass to subprocess
+ -- get stdin and pass to subprocess
opt = opt or {}
+ local stdin = opt.stdin
+ if type(stdin) == "string" then
+ opt.inpath = stdin
+ elseif type(stdin) == "table" then
+ if stdin.otype and stdin:otype() == 2 then
+ opt.inpipe = stdin:cdata()
+ else
+ opt.infile = stdin:cdata()
+ end
+ end
+
+ -- get stdout and pass to subprocess
local stdout = opt.stdout
if type(stdout) == "string" then
opt.outpath = stdout