summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-08-16 22:38:34 +0800
committerruki <[email protected]>2019-08-16 17:49:18 +0800
commit7bf7eabafb4c7f4bc36bd3964a914358cd7bb46c (patch)
tree95ab3093f5363a3715b6456a48302329ad47f819
parent524b4d10954233517fd25374b36c3e4080b29113 (diff)
improve process.openv
-rw-r--r--core/src/xmake/process/open.c6
-rw-r--r--core/src/xmake/process/openv.c111
-rw-r--r--xmake/actions/build/cleaner.lua2
-rw-r--r--xmake/actions/build/statistics.lua2
-rw-r--r--xmake/core/base/os.lua2
-rw-r--r--xmake/core/sandbox/modules/process.lua23
6 files changed, 80 insertions, 66 deletions
diff --git a/core/src/xmake/process/open.c b/core/src/xmake/process/open.c
index 7a65c8d31..fad3e8fec 100644
--- a/core/src/xmake/process/open.c
+++ b/core/src/xmake/process/open.c
@@ -109,12 +109,6 @@ tb_int_t xm_process_open(lua_State* lua)
}
lua_pop(lua, 1);
}
- else
- {
- // @deprecated compatible with process.open(cmd, outpath, errpath)
- outpath = lua_tostring(lua, 2);
- errpath = lua_tostring(lua, 3);
- }
// set the new environments
if (envn > 0) attr.envp = envs;
diff --git a/core/src/xmake/process/openv.c b/core/src/xmake/process/openv.c
index bdb3e2ba9..459a56832 100644
--- a/core/src/xmake/process/openv.c
+++ b/core/src/xmake/process/openv.c
@@ -34,7 +34,7 @@
* implementation
*/
-// p = process.openv(shellname, argv, outpath, errpath, envs)
+// p = process.openv(shellname, argv, {outpath = "", errpath = "", envs = {"PATH=xxx", "XXX=yyy"})
tb_int_t xm_process_openv(lua_State* lua)
{
// check
@@ -49,53 +49,10 @@ tb_int_t xm_process_openv(lua_State* lua)
return 0;
}
- // get the output and error file
+ // get shellname
tb_char_t const* shellname = lua_tostring(lua, 1);
- tb_char_t const* outpath = lua_tostring(lua, 3);
- tb_char_t const* errpath = lua_tostring(lua, 4);
tb_check_return_val(shellname, 0);
- // get environments
- tb_char_t const* envs[256] = {0};
- tb_size_t envn = 0;
- if (lua_istable(lua, 5))
- {
- // get environment variables count
- envn = (tb_size_t)lua_objlen(lua, 5);
-
- // get all passed environment variables
- tb_size_t i;
- for (i = 0; i < envn; i++)
- {
- // get envs[i]
- lua_pushinteger(lua, i + 1);
- lua_gettable(lua, 5);
-
- // is string?
- if (lua_isstring(lua, -1))
- {
- // add this environment value
- if (i + 1 < tb_arrayn(envs))
- envs[i] = lua_tostring(lua, -1);
- else
- {
- // error
- lua_pushfstring(lua, "envs is too large(%lu > %d) for process.openv", envn, tb_arrayn(envs) - 1);
- lua_error(lua);
- }
- }
- else
- {
- // error
- lua_pushfstring(lua, "invalid envs[%ld] type(%s) for process.openv", i, luaL_typename(lua, -1));
- lua_error(lua);
- }
-
- // pop it
- lua_pop(lua, 1);
- }
- }
-
// get the arguments count
tb_long_t argn = lua_objlen(lua, 2);
tb_check_return_val(argn >= 0, 0);
@@ -133,6 +90,68 @@ tb_int_t xm_process_openv(lua_State* lua)
// init attributes
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;
+ if (lua_istable(lua, 3))
+ {
+ // get outpath
+ lua_pushstring(lua, "outpath");
+ lua_gettable(lua, 3);
+ outpath = lua_tostring(lua, -1);
+ lua_pop(lua, 1);
+
+ // get errpath
+ lua_pushstring(lua, "errpath");
+ lua_gettable(lua, 3);
+ errpath = lua_tostring(lua, -1);
+ lua_pop(lua, 1);
+
+ // get environments
+ lua_pushstring(lua, "envs");
+ lua_gettable(lua, 3);
+ if (lua_istable(lua, -1))
+ {
+ // get environment variables count
+ envn = (tb_size_t)lua_objlen(lua, -1);
+
+ // get all passed environment variables
+ tb_size_t i;
+ for (i = 0; i < envn; i++)
+ {
+ // get envs[i]
+ lua_pushinteger(lua, i + 1);
+ lua_gettable(lua, -2);
+
+ // is string?
+ if (lua_isstring(lua, -1))
+ {
+ // add this environment value
+ if (i + 1 < tb_arrayn(envs))
+ envs[i] = lua_tostring(lua, -1);
+ else
+ {
+ // error
+ lua_pushfstring(lua, "envs is too large(%lu > %d) for process.openv", envn, tb_arrayn(envs) - 1);
+ lua_error(lua);
+ }
+ }
+ else
+ {
+ // error
+ lua_pushfstring(lua, "invalid envs[%ld] type(%s) for process.openv", i, luaL_typename(lua, -1));
+ lua_error(lua);
+ }
+
+ // pop it
+ lua_pop(lua, 1);
+ }
+ }
+ lua_pop(lua, 1);
+ }
+
// set the new environments
if (envn > 0) attr.envp = envs;
@@ -142,6 +161,7 @@ tb_int_t xm_process_openv(lua_State* lua)
// redirect stdout to file
attr.outpath = outpath;
attr.outmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
+ attr.outtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
}
// redirect stderr?
@@ -150,6 +170,7 @@ tb_int_t xm_process_openv(lua_State* lua)
// redirect stderr to file
attr.errpath = errpath;
attr.errmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
+ attr.errtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
}
// init process
diff --git a/xmake/actions/build/cleaner.lua b/xmake/actions/build/cleaner.lua
index 5b39f5542..db589491e 100644
--- a/xmake/actions/build/cleaner.lua
+++ b/xmake/actions/build/cleaner.lua
@@ -53,7 +53,7 @@ function cleanup()
try
{
function ()
- local proc = process.openv("xmake", argv, path.join(os.tmpdir(), "cleaner.log"))
+ local proc = process.openv("xmake", argv, {outpath = path.join(os.tmpdir(), "cleaner.log")})
if proc ~= nil then
process.close(proc)
end
diff --git a/xmake/actions/build/statistics.lua b/xmake/actions/build/statistics.lua
index ebce99f13..4edcfe187 100644
--- a/xmake/actions/build/statistics.lua
+++ b/xmake/actions/build/statistics.lua
@@ -83,7 +83,7 @@ function post()
try
{
function ()
- local proc = process.openv("xmake", argv, path.join(os.tmpdir(), projectname .. ".stats.log"))
+ local proc = process.openv("xmake", argv, {outpath = path.join(os.tmpdir(), projectname .. ".stats.log")})
if proc ~= nil then
process.close(proc)
end
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index b8a81c8bd..18dc682da 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -626,7 +626,7 @@ function os.execv(program, argv, opt)
-- open command
local ok = -1
- local proc = process.openv(filename, argv, opt.stdout, opt.stderr, envs)
+ local proc = process.openv(filename, argv, {outpath = opt.stdout, errpath = opt.stderr, envs = envs})
if proc ~= nil then
-- wait process
diff --git a/xmake/core/sandbox/modules/process.lua b/xmake/core/sandbox/modules/process.lua
index f1d73cb00..3a4faa31d 100644
--- a/xmake/core/sandbox/modules/process.lua
+++ b/xmake/core/sandbox/modules/process.lua
@@ -28,6 +28,10 @@ local vformat = require("sandbox/modules/vformat")
local sandbox_process = sandbox_process or {}
-- open process
+---
+-- @param command the command
+-- @param opt the arguments option, {outpath = "", errpath = "", envs = {"PATH=xxx", "XXX=yyy"}
+--
function sandbox_process.open(command, opt)
-- check
@@ -47,7 +51,12 @@ function sandbox_process.open(command, opt)
end
-- open process with arguments
-function sandbox_process.openv(filename, argv, outfile, errfile)
+--
+-- @param filename the command/file name
+-- @param argv the command arguments
+-- @param opt the arguments option, {outpath = "", errpath = "", envs = {"PATH=xxx", "XXX=yyy"}
+--
+function sandbox_process.openv(filename, argv, opt)
-- check
assert(argv)
@@ -55,18 +64,8 @@ function sandbox_process.openv(filename, argv, outfile, errfile)
-- format filename first
filename = vformat(filename)
- -- format output file if exists
- if outfile then
- outfile = vformat(outfile)
- end
-
- -- format error file if exists
- if errfile then
- errfile = vformat(errfile)
- end
-
-- open process
- local proc = process.openv(filename, argv, outfile, errfile)
+ local proc = process.openv(filename, argv, opt)
if not proc then
raise("openv process(%s, %s) failed!", filename, table.concat(argv, " "))
end