diff options
| author | ruki <[email protected]> | 2019-08-16 22:35:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-08-16 17:49:18 +0800 |
| commit | 524b4d10954233517fd25374b36c3e4080b29113 (patch) | |
| tree | 799503a7c4684e2d5ec1b691c4e2ebd3619069d4 /core/src/xmake/process | |
| parent | 11ae86985a2d2d8811d34eff4bbd37e3582aafc9 (diff) | |
add envs to process.open
Diffstat (limited to 'core/src/xmake/process')
| -rw-r--r-- | core/src/xmake/process/open.c | 49 | ||||
| -rw-r--r-- | core/src/xmake/process/openv.c | 14 |
2 files changed, 55 insertions, 8 deletions
diff --git a/core/src/xmake/process/open.c b/core/src/xmake/process/open.c index 2b552028d..7a65c8d31 100644 --- a/core/src/xmake/process/open.c +++ b/core/src/xmake/process/open.c @@ -34,7 +34,7 @@ * implementation */ -// p = process.open(command, {outpath = "", errpath = ""}) +// p = process.open(command, {outpath = "", errpath = "", envs = {"PATH=xxx", "XXX=yyy"}}) tb_int_t xm_process_open(lua_State* lua) { // check @@ -49,6 +49,8 @@ tb_int_t xm_process_open(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; if (lua_istable(lua, 2)) @@ -64,6 +66,48 @@ tb_int_t xm_process_open(lua_State* lua) lua_gettable(lua, 2); errpath = lua_tostring(lua, -1); lua_pop(lua, 1); + + // get environments + lua_pushstring(lua, "envs"); + lua_gettable(lua, 2); + 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); } else { @@ -72,6 +116,9 @@ tb_int_t xm_process_open(lua_State* lua) errpath = lua_tostring(lua, 3); } + // set the new environments + if (envn > 0) attr.envp = envs; + // redirect stdout? if (outpath) { diff --git a/core/src/xmake/process/openv.c b/core/src/xmake/process/openv.c index d5539512a..bdb3e2ba9 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, outfile, errfile, envs) +// p = process.openv(shellname, argv, outpath, errpath, 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* outfile = lua_tostring(lua, 3); - tb_char_t const* errfile = lua_tostring(lua, 4); + 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 @@ -137,18 +137,18 @@ tb_int_t xm_process_openv(lua_State* lua) if (envn > 0) attr.envp = envs; // redirect stdout? - if (outfile) + if (outpath) { // redirect stdout to file - attr.outfile = outfile; + attr.outpath = outpath; attr.outmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT; } // redirect stderr? - if (errfile) + if (errpath) { // redirect stderr to file - attr.errfile = errfile; + attr.errpath = errpath; attr.errmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT; } |
