
On Sat, 11 Sep 2010 00:21:48 +0200 "Boris Schaeling" <boris@highscore.de> wrote:
On Fri, 10 Sep 2010 01:33:53 +0200, Ilya Sokolov <ilyasokol@gmail.com> wrote:
[...]The following should solve problems that I know of:
typedef std::vector<std::pair<fileno, handle::native_type>> child_streams;
typedef boost::function2< std::pair<handle, handle>, fileno, const child_streams& > behavior;
std::pair<handle, handle> inherit(fileno fn, const child_streams&) { #ifdef _WIN32 handle child_end(GetStdHandle(DWORD(-10-fn)), dont_close); #else handle child_end(fn, dont_close); #endif handle parent_end; return std::make_pair(child_end, parent_end); }
class redirect_to { fileno to_; public: redirect_to(fileno to): to_(to) {} std::pair<handle, handle> operator()(fileno fn, const child_streams& cs) { BOOST_ASSERT(fn > to_); child_streams::const_iterator it = // find pair in cs where it->first == to_ #ifndef _WIN32 handle child_end(posix_dup2(it->second, fn)); #else // ... #endif handle parent_end; return std::make_pair(child_end, parent_end); } }; enum std_fileno { stdin, stdout, stderr };
Example of usage:
ctx.stderr = redirect_to(stdout);
I'm a bit confused: If you use fileno like above on Windows (0=stdin, 1=stdout and 2=stderr) then we could also use an enumeration?
I was thinking about the ability to set behavior of fileno > 2 on posix.
At least your implementation of inherit makes me think so that it's not the file descriptor itself you are interested in but only the information whether the standard input, output or error stream is configured.
yes
Then there would be no difference between your and mine solution?
???
[snip]