diff options
| author | ruki <[email protected]> | 2019-08-16 22:32:05 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-08-16 17:49:18 +0800 |
| commit | 11ae86985a2d2d8811d34eff4bbd37e3582aafc9 (patch) | |
| tree | c23759758dd97390beaa5522b6aec6175b6f81bc | |
| parent | 9588bc931c37322955165eb085c83d6e2dd4fee3 (diff) | |
improve process.open
| -rw-r--r-- | core/src/xmake/process/open.c | 30 | ||||
| -rw-r--r-- | core/src/xmake/process/openv.c | 16 | ||||
| -rw-r--r-- | tests/modules/process/test.lua | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/process.lua | 14 |
4 files changed, 36 insertions, 26 deletions
diff --git a/core/src/xmake/process/open.c b/core/src/xmake/process/open.c index 3318f6017..2b552028d 100644 --- a/core/src/xmake/process/open.c +++ b/core/src/xmake/process/open.c @@ -34,22 +34,44 @@ * implementation */ -// p = process.open(command, outpath, errpath) +// p = process.open(command, {outpath = "", errpath = ""}) tb_int_t xm_process_open(lua_State* lua) { // check tb_assert_and_check_return_val(lua, 0); - // get the command + // get command size_t command_size = 0; tb_char_t const* command = luaL_checklstring(lua, 1, &command_size); - tb_char_t const* outpath = lua_tostring(lua, 2); - tb_char_t const* errpath = lua_tostring(lua, 3); tb_check_return_val(command, 0); // init attributes tb_process_attr_t attr = {0}; + // get option arguments + tb_char_t const* outpath = tb_null; + tb_char_t const* errpath = tb_null; + if (lua_istable(lua, 2)) + { + // get outpath + lua_pushstring(lua, "outpath"); + lua_gettable(lua, 2); + outpath = lua_tostring(lua, -1); + lua_pop(lua, 1); + + // get errpath + lua_pushstring(lua, "errpath"); + lua_gettable(lua, 2); + errpath = lua_tostring(lua, -1); + lua_pop(lua, 1); + } + else + { + // @deprecated compatible with process.open(cmd, outpath, errpath) + outpath = lua_tostring(lua, 2); + errpath = lua_tostring(lua, 3); + } + // redirect stdout? if (outpath) { diff --git a/core/src/xmake/process/openv.c b/core/src/xmake/process/openv.c index 92e552783..d5539512a 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, outfile, errfile, envs) tb_int_t xm_process_openv(lua_State* lua) { // check @@ -51,8 +51,8 @@ tb_int_t xm_process_openv(lua_State* lua) // get the output and error file 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_char_t const* outfile = lua_tostring(lua, 3); + tb_char_t const* errfile = lua_tostring(lua, 4); tb_check_return_val(shellname, 0); // get environments @@ -137,21 +137,19 @@ tb_int_t xm_process_openv(lua_State* lua) if (envn > 0) attr.envp = envs; // redirect stdout? - if (outpath) + if (outfile) { // redirect stdout to file - attr.outpath = outpath; + attr.outfile = outfile; attr.outmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT; - attr.outtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH; } // redirect stderr? - if (errpath) + if (errfile) { // redirect stderr to file - attr.errpath = errpath; + attr.errfile = errfile; 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/tests/modules/process/test.lua b/tests/modules/process/test.lua index aa57a56cc..c5ee27d9f 100644 --- a/tests/modules/process/test.lua +++ b/tests/modules/process/test.lua @@ -7,7 +7,7 @@ function test_single_process(t) local stdout = os.tmpfile() local stderr = os.tmpfile() for i = 1, 2 do - local pro = process.open("echo -n awd", stdout, stderr) + local pro = process.open("echo -n awd", {outpath = stdout, errpath = stderr}) process.wait(pro, inftimeout) process.close(pro) t:are_equal(io.readfile(stdout), "awd") diff --git a/xmake/core/sandbox/modules/process.lua b/xmake/core/sandbox/modules/process.lua index da42c4b85..f1d73cb00 100644 --- a/xmake/core/sandbox/modules/process.lua +++ b/xmake/core/sandbox/modules/process.lua @@ -28,7 +28,7 @@ local vformat = require("sandbox/modules/vformat") local sandbox_process = sandbox_process or {} -- open process -function sandbox_process.open(command, outfile, errfile) +function sandbox_process.open(command, opt) -- check assert(command) @@ -36,18 +36,8 @@ function sandbox_process.open(command, outfile, errfile) -- format command first command = vformat(command) - -- 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.open(command, outfile, errfile) + local proc = process.open(command, opt) if not proc then raise("open process(%s) failed!", command) end |
