lua-users home
lua-l archive

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


On 14.08.13 21:02, Lorenzo Donati wrote:
Hi all!

Recently I tried to use io.popen to read the output produced by some
shell commands (in Windows XP).

The problem is that io.popen only returns a handle to stdout for the
child process. If I want to capture stderr apparently I have only two
options:

(1) to redirect it to a file and then read it back.
(2) to forfeit stdout and redirect stderr to stdout.
(3) merge stdout and stderr using XP redirection tricks.

None of the above suits my needs. I need both stdout AND stderr output
so this rules out (2), while (3) is not general enough (I cannot be sure
I can parse stdout and stderr apart).

(1) is a tradeoff, but I'd like not to write anything on disk if possible.

Is there a way to do it in pure Lua?

I searched the WIKI to no avail, and in lua-l archive there are only
very old posts. The only recent messages I found were about two-way
popen (not supported and considered unreliable without threading), but I
don't need to both read and write, I only need to read both output streams.

Is it possible in pure Lua (5.2 would be enough)?

If not, may I suggest to enhance io.popen in Lua 5.3, e.g. by returning
two handles, one to stdout and one to stderr. I guess (hope?) it
shouldn't be too hard or cause problems or add relevant bloat. BTW wxLua
implements a similar functionality in wx.wxExecuteStdoutStderr, which
captures the two streams independently.

Lua io.popen is just a thin wrapper around the popen provided by the C library. Some C libraries (Mac OS X, BSD) allow you to pass "rw" and get a bidirectional pipe but none of the C libraries can return separate stdout and stderr (the function only has one return value).

What would be really nice is a wrapper around luaposix and lua-winapi modules which would work like the subprocess module in Python.

--
regards,
Jakub Piotr Cłapa
LoEE.pl