lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 5/28/07, DarkGod <darkgod@t-o-m-e.net> wrote:
I've made myself a small C module for Lua that allows to call an
external program while capturing
both the input and output streams (using the classic combinaison of
fork/exec/dup2) and to ask if
a file handle is readable/writable and to bind the "wait" system call.

How about a more general extension?
http://lua-users.org/wiki/ExtensionProposal

require "ex"
-- 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()