On 15 August 2013 10:08, steve donovan <
steve.j.donovan@gmail.com> wrote:
> On Thu, Aug 15, 2013 at 9:15 AM, Jakub Piotr Cłapa <
jpc-ml@zenburn.net> wrote:
>>
>> What would be really nice is a wrapper around luaposix and lua-winapi modules which would work like the subprocess module in Python.
>
> Nice and very doable, I've been thinking about this. Generally people pick a platform and use the appropriate native API, but it _is_ useful to have cross-platform scripting that hides some implementation details.
There is already an implementation of an API allowing you to spawn processes and having a better control of the standard streams -
http://lua-users.org/wiki/ExtensionProposal - available for POSIX and Windows. Here is an example of "popen2", which spawns a process and creates necessary pipes for standard input and standard output. It should be easily extendable to include stderr.
-- popen2()
function popen2(...)
local in_rd, in_wr = io.pipe()
local out_rd, out_wr = io.pipe()
local proc, err = os.spawn{stdin = in_rd, stdout = out_wr, ...}
in_rd:close(); out_wr:close()
if not proc then
in_wr:close(); out_rd:close()
return proc, err
end
return proc, out_rd, in_wr
end
-- usage:
local p, i, o = assert(popen2("wc", "-w"))
o:write("Hello world"); o:close()
print(i:read"*l"); i:close()
p:wait()