-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add withHandleToHANDLE from ansi-terminal #70
Conversation
Thanks @RyanGlScott ! W.r.t the inverse function, they're both essentially the same. I wrote the one in GHCi.hsc but don't know where the second came from. But they both do the same. so the one in |
Hrm. I tried implementing diff --git a/System/Win32/Types.hsc b/System/Win32/Types.hsc
index 4393c51..1e5e447 100755
--- a/System/Win32/Types.hsc
+++ b/System/Win32/Types.hsc
@@ -32,12 +32,13 @@ import Data.Word (Word8, Word16, Word32, Word64)
import Foreign.C.Error (Errno(..), errnoToIOError)
import Foreign.C.String (newCWString, withCWStringLen)
import Foreign.C.String (peekCWString, peekCWStringLen, withCWString)
-import Foreign.C.Types (CChar, CUChar, CWchar, CInt(..), CIntPtr, CUIntPtr)
+import Foreign.C.Types (CChar, CUChar, CWchar, CInt(..), CIntPtr(..), CUIntPtr)
import Foreign.ForeignPtr (ForeignPtr, newForeignPtr, newForeignPtr_)
-import Foreign.Ptr (FunPtr, Ptr, nullPtr)
+import Foreign.Ptr (FunPtr, Ptr, nullPtr, ptrToIntPtr)
import Foreign.StablePtr (StablePtr, freeStablePtr, newStablePtr)
import Foreign (allocaArray)
import GHC.IO.FD (FD(..))
+import GHC.IO.Handle.FD (fdToHandle)
import GHC.IO.Handle.Types (Handle(..), Handle__(..))
import Numeric (showHex)
import System.IO.Error (ioeSetErrorString)
@@ -56,6 +57,7 @@ finiteBitSize :: (Bits a) => a -> Int
finiteBitSize = bitSize
#endif
+#include <fcntl.h>
#include <windows.h>
##include "windows_cconv.h"
@@ -218,6 +220,13 @@ nullFinalHANDLE = unsafePerformIO (newForeignPtr_ nullPtr)
iNVALID_HANDLE_VALUE :: HANDLE
iNVALID_HANDLE_VALUE = castUINTPtrToPtr (-1)
+foreign import ccall "_open_osfhandle"
+ _open_osfhandle :: CIntPtr -> CInt -> IO CInt
+
+hANDLEToHandle :: HANDLE -> IO Handle
+hANDLEToHandle handle =
+ _open_osfhandle (fromIntegral (ptrToIntPtr handle)) (#const _O_BINARY) >>= fdToHandle
+
foreign import ccall unsafe "_get_osfhandle"
c_get_osfhandle :: CInt -> IO HANDLE It works, but only in a limited sense. You can, for instance, print to standard output:
However,
Is this acceptable? |
To be perfectly honest, I'm not even sure that |
OK, I've added |
Many thanks kind sir!
…On Thu, 12 Jan 2017, 16:03 Ryan Scott, ***@***.***> wrote:
OK, I've added hANDLEToHandle with a little disclaimer.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#70 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABH3KXQu3Af8fu1_wLBQ6C4qYf6gGzVTks5rRk7sgaJpZM4LdwVF>
.
|
Unfortunately trac.haskell.org doesn't exist anymore.
This brings over the
withHandleToHANDLE
function from theansi-terminal
library. I also added a basic test to ensure that it works with the standard handles.I contemplated implementing the opposite direction (
hANDLEToHandle
?), but I'm not quite sure what the best way to do it would be. There's at least two different implementations (this and this) in GHC, for instance. As a result, I decided to leave that part alone.Fixes #51.