summaryrefslogtreecommitdiff
path: root/core/src
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 /core/src
parent3ccbf254fd5a0013dfc91e272d47c74dd6ebd181 (diff)
support stdin for os.execv
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/process/open.c56
-rw-r--r--core/src/xmake/process/openv.c71
2 files changed, 117 insertions, 10 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)
{