[add System.IO.Pipe.
David Roundy <roundyd@physics.oregonstate.edu>**20090920222021
 Ignore-this: 41e186c1bb31ebcfea1dbaade4198b52f7cfefc6
] addfile ./lib/base/System/IO/Pipe.hs
hunk ./lib/base/System/IO/Pipe.hs 1
+{-# OPTIONS_JHC -fffi #-}
+module System.IO.Pipe ( openPipe, openBinaryPipe ) where
+
+import Jhc.Handle ( openPipe, openBinaryPipe )
hunk ./lib/base/base.cabal 46
+        System.IO.Pipe,
hunk ./lib/jhc/Jhc/Handle.hs 11
+    openBinaryPipe,
+    openPipe,
hunk ./lib/jhc/Jhc/Handle.hs 44
+    handleIsPipe :: !Bool,
hunk ./lib/jhc/Jhc/Handle.hs 53
-make_builtin mode name std = Handle { handleName = "(" ++ name ++ ")", handleFile = std, handleIOMode = mode, handleBinary = False }
+make_builtin mode name std = Handle { handleName = "(" ++ name ++ ")", handleFile = std, handleIOMode = mode, handleBinary = False, handleIsPipe = False }
hunk ./lib/jhc/Jhc/Handle.hs 79
-        False -> c_fclose ptr >> poke (handleFile h) nullPtr
+        False -> do ec <- if handleIsPipe h then c_pclose ptr
+                                            else c_fclose ptr
+                    if ec /= 0 then fail ("hClose "++handleName h++" failed")
+                               else return ()
+                    poke (handleFile h) nullPtr
hunk ./lib/jhc/Jhc/Handle.hs 101
-        return Handle { handleBinary = False, handleName = fp, handleIOMode = m, handleFile = pptr }
+        return Handle { handleBinary = False, handleIsPipe = False, handleName = fp, handleIOMode = m, handleFile = pptr }
hunk ./lib/jhc/Jhc/Handle.hs 103
+openPipe :: String -> IOMode -> IO Handle
+openPipe c m = do
+    ptr <- withCString c $ \command -> c_popen command (Ptr (toStr m))
+    -- if ptr == nullPtr then throwErrnoFN "openPipe" c else do
+    pptr <- new ptr
+    return Handle { handleBinary = False, handleIsPipe = True, handleName = c, handleIOMode = m, handleFile = pptr }
+
+openBinaryPipe :: String -> IOMode -> IO Handle
+openBinaryPipe c m = do
+    ptr <- withCString c $ \command -> c_popen command (Ptr (toStr m))
+    if ptr == nullPtr then throwErrnoFN "openPipe" c  else do
+        pptr <- new ptr
+        return Handle { handleBinary = True, handleIsPipe = True, handleName = c, handleIOMode = m, handleFile = pptr }
+
hunk ./lib/jhc/System/C/Stdio.hs 12
+foreign import ccall "stdio.h popen" c_popen               :: Ptr CChar -> Ptr CChar -> IO FILE
hunk ./lib/jhc/System/C/Stdio.hs 14
+foreign import ccall "stdio.h pclose" c_pclose             :: FILE -> IO CInt