Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Michal Čihař
updater
Commits
8ebc0ab6
Verified
Commit
8ebc0ab6
authored
Feb 13, 2018
by
Karel Koci
🤘
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add possibility to specify environment variables to subprocess
parent
7a6a2d78
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
14 deletions
+45
-14
src/lib/interpreter.c
src/lib/interpreter.c
+28
-7
src/lib/lua_funcs.txt
src/lib/lua_funcs.txt
+6
-2
tests/subprocess.lua
tests/subprocess.lua
+11
-5
No files found.
src/lib/interpreter.c
View file @
8ebc0ab6
...
...
@@ -42,6 +42,7 @@
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
...
...
@@ -458,18 +459,38 @@ static int lua_subprocess(lua_State *L) {
// TODO verify type?
const
char
*
message
=
luaL_checkstring
(
L
,
2
);
int
timeout
=
luaL_checkinteger
(
L
,
3
);
const
char
*
command
=
luaL_checkstring
(
L
,
4
);
const
char
*
command
=
luaL_checkstring
(
L
,
5
);
// Environment
luaL_checktype
(
L
,
4
,
LUA_TTABLE
);
int
env_size
=
1
;
// For terminating NULL
lua_pushnil
(
L
);
while
(
lua_next
(
L
,
4
)
!=
0
)
{
env_size
++
;
lua_pop
(
L
,
1
);
// pop pushed key
}
struct
env_change
env
[
env_size
];
lua_pushnil
(
L
);
int
i
=
0
;
while
(
lua_next
(
L
,
4
)
!=
0
)
{
env
[
i
].
name
=
strdup
(
luaL_checkstring
(
L
,
-
2
));
env
[
i
].
value
=
strdup
(
luaL_checkstring
(
L
,
-
1
));
lua_pop
(
L
,
1
);
// pop pushed key
i
++
;
}
env
[
env_size
-
1
]
=
(
struct
env_change
){
NULL
};
int
ec
;
char
*
output
;
if
(
lua_gettop
(
L
)
>
4
)
{
if
(
lua_gettop
(
L
)
>
5
)
{
const
char
*
args
[
lua_gettop
(
L
)
-
4
];
for
(
int
i
=
5
;
i
<=
lua_gettop
(
L
);
i
++
)
args
[
i
-
5
]
=
luaL_checkstring
(
L
,
i
);
args
[
lua_gettop
(
L
)
-
4
]
=
NULL
;
ec
=
lsubprocl
(
type
,
message
,
&
output
,
timeout
,
command
,
args
);
for
(
int
i
=
6
;
i
<=
lua_gettop
(
L
);
i
++
)
{
args
[
i
-
6
]
=
luaL_checkstring
(
L
,
i
);
}
args
[
lua_gettop
(
L
)
-
5
]
=
NULL
;
ec
=
lsubprocle
(
type
,
message
,
&
output
,
timeout
,
env
,
command
,
args
);
}
else
{
ec
=
lsubprocv
(
type
,
message
,
&
output
,
timeout
,
command
,
NULL
);
ec
=
lsubprocv
e
(
type
,
message
,
&
output
,
timeout
,
env
,
command
,
NULL
);
}
lua_pushinteger
(
L
,
ec
);
...
...
src/lib/lua_funcs.txt
View file @
8ebc0ab6
...
...
@@ -90,10 +90,10 @@ Subprocesses
------------
Family of functions ``subproc*`` defined in ``subprocess.h`` are exported to lua
in form of function `subproc`.
in form of function
s
`subproc
ess
`.
Function `subprocess` is defined as follows:
`sub
o
process(type, message, timeout, command ...)`
`subprocess(type, message, timeout,
environment,
command ...)`
`type` is identification used to specify what type of subprocess it's. Allowed
predefined constants are as follows:
...
...
@@ -106,6 +106,10 @@ Function `subprocess` is defined as follows:
`timeout` is time in seconds after which subprocess will be automatically
killed.
`environment` is (hash) table containing changes to be done to environment
variables. Key has to be only strings but values can be anything that can be
later converted to string using `tostring` function.
`command` is any arbitrary number of string arguments that are passed as command
and its additional arguments.
...
...
tests/subprocess.lua
View file @
8ebc0ab6
...
...
@@ -22,27 +22,33 @@ require 'lunit'
module
(
"subproc"
,
package
.
seeall
,
lunit
.
testcase
)
function
test_exit_code
()
local
ok
,
out
=
subprocess
(
LST_HOOK
,
"Test: true"
,
1
,
"true"
)
local
ok
,
out
=
subprocess
(
LST_HOOK
,
"Test: true"
,
1
,
{},
"true"
)
assert_equal
(
0
,
ok
)
assert_equal
(
""
,
out
)
local
ok
,
out
=
subprocess
(
LST_HOOK
,
"Test: false"
,
1
,
"false"
)
local
ok
,
out
=
subprocess
(
LST_HOOK
,
"Test: false"
,
1
,
{},
"false"
)
assert_not_equal
(
0
,
ok
)
assert_equal
(
""
,
out
)
end
function
test_output
()
local
ok
,
out
=
subprocess
(
LST_HOOK
,
"Test: echo"
,
1
,
"echo"
,
"hello"
)
local
ok
,
out
=
subprocess
(
LST_HOOK
,
"Test: echo"
,
1
,
{},
"echo"
,
"hello"
)
assert_equal
(
0
,
ok
)
assert_equal
(
"hello\n"
,
out
)
local
ok
,
out
=
subprocess
(
LST_HOOK
,
"Test: echo stderr"
,
1
,
"sh"
,
"-c"
,
"echo hello >&2"
)
local
ok
,
out
=
subprocess
(
LST_HOOK
,
"Test: echo stderr"
,
1
,
{},
"sh"
,
"-c"
,
"echo hello >&2"
)
assert_equal
(
0
,
ok
)
assert_equal
(
"hello\n"
,
out
)
end
function
test_timeout
()
local
ok
,
out
=
subprocess
(
LST_HOOK
,
"Test: sleep"
,
1
,
"sleep"
,
"2"
)
local
ok
,
out
=
subprocess
(
LST_HOOK
,
"Test: sleep"
,
1
,
{},
"sleep"
,
"2"
)
assert_not_equal
(
0
,
ok
)
assert_equal
(
""
,
out
)
end
function
test_env
()
local
ok
,
out
=
subprocess
(
LST_HOOK
,
"Test: env"
,
1
,
{[
'TESTMSG'
]
=
'hello'
},
"sh"
,
"-c"
,
"echo $TESTMSG"
)
assert_equal
(
0
,
ok
)
assert_equal
(
"hello\n"
,
out
)
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment