Okay, okay, I'm convinced that trying to mess with it in C is easier than in Haskell. =P<br><br clear="all">Louis Wasserman<br><a href="mailto:wasserman.louis@gmail.com">wasserman.louis@gmail.com</a><br><a href="http://profiles.google.com/wasserman.louis">http://profiles.google.com/wasserman.louis</a><br>
<br><br><div class="gmail_quote">On Fri, Feb 26, 2010 at 2:14 PM, Brandon S. Allbery KF8NH <span dir="ltr"><<a href="mailto:allbery@ece.cmu.edu">allbery@ece.cmu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div style="word-wrap: break-word;"><div><div class="im"><div>On Feb 26, 2010, at 14:44 , Louis Wasserman wrote:</div><blockquote type="cite">Is there any way I can temporarily reassign stdout? In particular, will this be handled well by the FFI? I'm hoping there's maybe something like PHP's output buffering <a href="http://php.net/manual/en/book.outcontrol.php" target="_blank">thingy</a>. I'm *binding* (not doing a process call) to an external library which outputs directly to stdout, but whose output I'd like to use...<br clear="all">
</blockquote><div><br></div></div><div>That way lies pain. It can be done, but if it breaks it will break rather thoroughly and leave you stranded. Untested (aside from compilation), no warranty, etc.:</div><div><br></div>
<div> module Redirect (withRedirectedStdout) where</div><div><br></div><div> import System.IO</div><div> import <a href="http://System.Posix.IO" target="_blank">System.Posix.IO</a></div><div><br></div><div> -- usage: withRedirectedStdout destinationFileName $ yourIOActionHere</div>
<div> -- this might work with Haskell I/O, if you're lucky; don't bet on it</div><div> -- stick to FFI calls that use raw I/O</div><div> withRedirectedStdout :: FilePath -> IO a -> IO a</div><div> withRedirectedStdout f io= do</div>
<div> hFlush stdout</div><div> -- style note: should be stdOutput but that doesn't work for the binds</div><div> -- and I'm not sure it's worth doing those "right". maybe if someone wants this</div>
<div> -- for the library I'll clean it up</div><div> old <- dup 1</div><div> closeFd 1</div><div> -- this will pattern-match fail if we don't get stdout back; if you manage to trigger it,</div><div>
-- you get to keep both pieces. (you probably closed stdin, which is usually a</div><div> -- recipe for disaster, or at minimum extreme confusion)</div><div> 1 <- openFd f WriteOnly (Just 0600) (defaultFileFlags {noctty = True, trunc = True, nonBlock = False}) </div>
<div> res <- io</div><div> closeFd 1</div><div> 1 <- dup old</div><div> closeFd old</div><div> return res</div></div><div><br></div><div>Actually, I bet this fails because your FFI function uses C/C++ stdio and doesn't flush it. That will probably require bringing in C stdio's fflush(stdout), which will be even more painful.</div>
<br><div> <span style="border-collapse: separate; border-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div style="word-wrap: break-word;">
<span style="border-collapse: separate; border-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><span style="border-collapse: separate; border-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div>
<font face="Monaco"><span style="font-family: Monaco;"><span style="font-family: Monaco;">-- </span></span></font></div><div><font face="Monaco"><span style="font-family: Monaco;"><span style="font-family: Monaco;">brandon s. allbery [solaris,freebsd,perl,pugs,haskell] <a href="mailto:allbery@kf8nh.com" target="_blank">allbery@kf8nh.com</a></span></span></font></div>
<div><font face="Monaco"><span style="font-family: Monaco;"><span style="font-family: Monaco;">system administrator [openafs,heimdal,too many hats] <a href="mailto:allbery@ece.cmu.edu" target="_blank">allbery@ece.cmu.edu</a></span></span></font></div>
<div><font face="Monaco"><span style="font-family: Monaco;"><span style="font-family: Monaco;">electrical and computer engineering, carnegie mellon university KF8NH</span></span></font></div><span style="border-collapse: separate; border-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><br>
</span></span></span></div></span> </div><br></div></blockquote></div><br>