summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2019-08-17 12:19:14 +0800
committerruki <[email protected]>2019-08-17 12:19:14 +0800
commit51dcf34216824a7d5096cad03b5b04964e8ceba8 (patch)
tree5c3d0e4d6b725f828da4e1ed408da9c545f55d0e /core/src
parent9e0306ffe029d0e1149fb63636ade90a665767b1 (diff)
open file at subprocess
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/process/close.c19
-rw-r--r--core/src/xmake/process/open.c53
-rw-r--r--core/src/xmake/process/openv.c53
-rw-r--r--core/src/xmake/process/prefix.h33
4 files changed, 132 insertions, 26 deletions
diff --git a/core/src/xmake/process/close.c b/core/src/xmake/process/close.c
index dc1a465a3..58373c9cf 100644
--- a/core/src/xmake/process/close.c
+++ b/core/src/xmake/process/close.c
@@ -48,6 +48,25 @@ tb_int_t xm_process_close(lua_State* lua)
tb_process_ref_t process = (tb_process_ref_t)lua_touserdata(lua, 1);
tb_check_return_val(process, 0);
+ // exit subprocess
+ xm_subprocess_t* subprocess = (xm_subprocess_t*)tb_process_priv(process);
+ if (subprocess)
+ {
+ if (subprocess->outtype == TB_PROCESS_REDIRECT_TYPE_FILE && subprocess->outfile)
+ {
+ tb_file_exit(subprocess->outfile);
+ subprocess->outfile = tb_null;
+ }
+
+ if (subprocess->errtype == TB_PROCESS_REDIRECT_TYPE_FILE && subprocess->errfile)
+ {
+ tb_file_exit(subprocess->errfile);
+ subprocess->errfile = tb_null;
+ }
+
+ tb_free(subprocess);
+ }
+
// exit process
tb_process_exit(process);
diff --git a/core/src/xmake/process/open.c b/core/src/xmake/process/open.c
index fad3e8fec..676d0241f 100644
--- a/core/src/xmake/process/open.c
+++ b/core/src/xmake/process/open.c
@@ -113,22 +113,49 @@ tb_int_t xm_process_open(lua_State* lua)
// set the new environments
if (envn > 0) attr.envp = envs;
- // redirect stdout?
- if (outpath)
+ // TODO
+ if (1)
{
- // 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;
- }
+ xm_subprocess_t* subprocess = tb_malloc0_type(xm_subprocess_t);
+ if (subprocess)
+ {
+ // redirect stdout?
+ if (outpath)
+ {
+ // redirect stdout to file
+ subprocess->outfile = tb_file_init(outpath, TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT);
+ subprocess->outtype = TB_PROCESS_REDIRECT_TYPE_FILE;
+ }
- // redirect stderr?
- if (errpath)
+ // redirect stderr?
+ if (errpath)
+ {
+ // redirect stderr to file
+ subprocess->errfile = tb_file_init(errpath, TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT);
+ subprocess->errtype = TB_PROCESS_REDIRECT_TYPE_FILE;
+ }
+ attr.priv = subprocess;
+ }
+ }
+ else
{
- // 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;
+ // redirect stdout?
+ if (outpath)
+ {
+ // 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?
+ if (errpath)
+ {
+ // 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/core/src/xmake/process/openv.c b/core/src/xmake/process/openv.c
index 459a56832..7bf97c358 100644
--- a/core/src/xmake/process/openv.c
+++ b/core/src/xmake/process/openv.c
@@ -155,22 +155,49 @@ tb_int_t xm_process_openv(lua_State* lua)
// set the new environments
if (envn > 0) attr.envp = envs;
- // redirect stdout?
- if (outpath)
+ // TODO
+ if (1)
{
- // 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;
- }
+ xm_subprocess_t* subprocess = tb_malloc0_type(xm_subprocess_t);
+ if (subprocess)
+ {
+ // redirect stdout?
+ if (outpath)
+ {
+ // redirect stdout to file
+ subprocess->outfile = tb_file_init(outpath, TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT);
+ subprocess->outtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
+ }
- // redirect stderr?
- if (errpath)
+ // redirect stderr?
+ if (errpath)
+ {
+ // redirect stderr to file
+ subprocess->errfile = tb_file_init(errpath, TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT);
+ subprocess->errtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
+ }
+ attr.priv = subprocess;
+ }
+ }
+ else
{
- // 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;
+ // redirect stdout?
+ if (outpath)
+ {
+ // 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_FILE;
+ }
+
+ // redirect stderr?
+ if (errpath)
+ {
+ // 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_FILE;
+ }
}
// init process
diff --git a/core/src/xmake/process/prefix.h b/core/src/xmake/process/prefix.h
index becbb93e6..c1391fe19 100644
--- a/core/src/xmake/process/prefix.h
+++ b/core/src/xmake/process/prefix.h
@@ -26,6 +26,39 @@
*/
#include "../prefix.h"
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * types
+ */
+
+// the subprocess type
+typedef struct __xm_subprocess_t
+{
+ /// the stdout redirect type
+ tb_uint16_t outtype;
+
+ /// the stderr redirect type
+ tb_uint16_t errtype;
+
+ union
+ {
+ /// the stdout pipe
+ tb_pipe_file_ref_t outpipe;
+
+ /// the stdout file
+ tb_file_ref_t outfile;
+ };
+
+ union
+ {
+ /// the strerr pipe
+ tb_pipe_file_ref_t errpipe;
+
+ /// the stderr file
+ tb_file_ref_t errfile;
+ };
+
+}xm_subprocess_t;
+
#endif