1 Jun 2005 01:34
Re: PATCH: Lua 5.0.2 popen/pclose fix (was Re: segfault in io.close and a plea for pipes)
David Burgess <dburgess <at> gmail.com>
2005-05-31 23:34:38 GMT
2005-05-31 23:34:38 GMT
I think I have addressed my popen() problem for win32.
After some iteration, I have a popen() function whose only
requirement is that it is openlib()ed. The rest of the IO
library just works with it (work6). Plus
- The GUI program restriction of the MS RTL _popen() is removed
- pclose() is not required, the handle is mapped to a FILE* so it
closes with fclose() just like any other fp.
- tested for handle leaks
- additional mode characters, namely;
2 - maps stderr to stdout
s - uses the windows shell (COMSPEC)
h - hides any child process windows
n - uses a NULL handle for unspecified child handles.
Without this option the parent handle is inherited by the child.
process.
If the parent stdin, stdout, stderr are not initialized correctly
(as in the case of a GUI parent) the popen() may fail unless this
option is provided.
The C module also comes with an exec() function that spawns using
IO completion ports and provides Lua call backs to perform std io for the
child process.
I can send you this code if you want it.
My presumption is that popen() is generally not an issue for Unix systems
and that some kind soul will provide the perfect popen() implementation
for work6.
(Continue reading)
Attached is a 10 line (!) patch against lauxlib.c from
Lua 5.1-work6 to allow for method inheritance on userdata
metatables. See below for a detailed explanation.
Since the above change makes inheriting from liolib.c very easy,
I wrote the posix.popen functionality, too. See the second
attachment.
I made it into a standalone module for easier testing. But
of course it's intended to be merged into lhf's POSIX module
(yes, I donate the code).
posix.popen(command [,mode]) creates a userdata object with the
"FILE*.PIPE" handle metatable. It overrides the "close" method
and the "__gc" and "__tostring" metamethods. All other methods
are inherited from the "FILE*" handle metatable provided by
liolib.c.
RSS Feed