Search code examples
linuxhaskelltty

Convert FD to Handle


I'd like to open a pseudo-tty and use the resulting slave terminal to connect to stdin/stdout on a subprocess. openPseudoTerminal gives me an IO (System.Posix.Types.Fd, System.Posix.Types.Fd), which I sought to translate into a pair of handles using fdToHandle in GHC.IO.Handle.Fd (GHC specific, but I couldn't find another such function). However, I get the following:

liftA (fdToHandle *** fdToHandle) openPseudoTerminal
Couldn't match type `System.Posix.Types.Fd'
               with `System.Posix.Internals.FD'

Any ideas how I convert between these two (presumably similar) things?

For bonus points, this will give me an IO (IO Handle, IO Handle) - is there a neat way to convert it to an IO (Handle, Handle)?


Solution

  • openPseudoTerminal is in the unix package, which also provides an fdToHandle with the appropriate type in System.Posix.IO.

    I'll throw in the best one-liner I have come up with so far, to deal with the pair of IO Handles:

    getHandles :: IO (Handle, Handle)
    getHandles =
      openPseudoTerminal >>= uncurry ap . (fmap (,) . fdToHandle *** fdToHandle)
    

    or:

    getHandles =
      openPseudoTerminal >>= uncurry (ap . fmap (,)) . join (***) fdToHandle