| 1 |
{-# LANGUAGE Safe #-} |
| 2 |
{-# LANGUAGE ScopedTypeVariables #-} |
| 3 |
|
| 4 |
{-| This module is based on the corresponding code in the mintty package and the |
| 5 |
Win32 package, in order to avoid a dependency on those packages. |
| 6 |
-} |
| 7 |
module System.Console.ANSI.Windows.Win32.MinTTY |
| 8 |
( isMinTTYHandle |
| 9 |
) where |
| 10 |
|
| 11 |
import Control.Exception ( catch ) |
| 12 |
import Data.Int ( Int32 ) |
| 13 |
import Data.List ( isInfixOf ) |
| 14 |
import Data.Word ( Word8 ) |
| 15 |
import Foreign.C.String |
| 16 |
( peekCWStringLen, withCAString, withCWString, withCWStringLen ) |
| 17 |
import Foreign.C.Types ( CInt (..) ) |
| 18 |
import Foreign.Marshal.Alloc ( alloca, allocaBytes ) |
| 19 |
import Foreign.Marshal.Array ( advancePtr, copyArray ) |
| 20 |
import Foreign.Marshal.Utils ( maybeWith ) |
| 21 |
import Foreign.Ptr ( FunPtr, Ptr, castPtr, castPtrToFunPtr, plusPtr ) |
| 22 |
import Foreign.Storable ( Storable (..) ) |
| 23 |
|
| 24 |
-- Provided by the ansi-terminal package |
| 25 |
import System.Console.ANSI.Windows.Win32.Types |
| 26 |
( Addr, BOOL, DWORD, FileType, HANDLE, HMODULE, LPCSTR, LPCTSTR, LPTSTR |
| 27 |
, TCHAR, ULONG, USHORT, failIfFalse_, failIfNeg, failIfNull |
| 28 |
) |
| 29 |
|
| 30 |
-- The headers that are shipped with GHC's copy of MinGW-w64 assume Windows XP. |
| 31 |
-- Since we need some structs that are only available with Vista or later, |
| 32 |
-- we must manually set WINVER/_WIN32_WINNT accordingly. |
| 33 |
#undef WINVER |
| 34 |
#define WINVER 0x0600 |
| 35 |
#undef _WIN32_WINNT |
| 36 |
#define _WIN32_WINNT 0x0600 |
| 37 |
#include <windows.h> |
| 38 |
#include "winternl_compat.h" |
| 39 |
|
| 40 |
#if __GLASGOW_HASKELL__ < 800 |
| 41 |
#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__) |
| 42 |
#endif |
| 43 |
|
| 44 |
type F_NtQueryObject = |
| 45 |
HANDLE |
| 46 |
-> CInt |
| 47 |
-> Ptr OBJECT_NAME_INFORMATION |
| 48 |
-> ULONG |
| 49 |
-> Ptr ULONG |
| 50 |
-> IO NTSTATUS |
| 51 |
type F_GetFileInformationByHandleEx = |
| 52 |
HANDLE -> CInt -> Ptr FILE_NAME_INFO -> DWORD -> IO BOOL |
| 53 |
type NTSTATUS = #type NTSTATUS |
| 54 |
|
| 55 |
data FILE_NAME_INFO = FILE_NAME_INFO |
| 56 |
{ fniFileNameLength :: DWORD |
| 57 |
, fniFileName :: String |
| 58 |
} |
| 59 |
|
| 60 |
instance Storable FILE_NAME_INFO where |
| 61 |
sizeOf _ = #size FILE_NAME_INFO |
| 62 |
alignment _ = #alignment FILE_NAME_INFO |
| 63 |
poke buf fni = withTStringLen (fniFileName fni) $ \(str, len) -> do |
| 64 |
let len' = (min mAX_PATH len) * sizeOfTCHAR |
| 65 |
start = advancePtr (castPtr buf) (#offset FILE_NAME_INFO, FileName) |
| 66 |
end = advancePtr start len' |
| 67 |
(#poke FILE_NAME_INFO, FileNameLength) buf len' |
| 68 |
copyArray start (castPtr str :: Ptr Word8) len' |
| 69 |
poke (castPtr end) (0 :: TCHAR) |
| 70 |
peek buf = do |
| 71 |
vfniFileNameLength <- (#peek FILE_NAME_INFO, FileNameLength) buf |
| 72 |
let len = fromIntegral vfniFileNameLength `div` sizeOfTCHAR |
| 73 |
vfniFileName <- |
| 74 |
peekTStringLen (plusPtr buf (#offset FILE_NAME_INFO, FileName), len) |
| 75 |
pure $ FILE_NAME_INFO |
| 76 |
{ fniFileNameLength = vfniFileNameLength |
| 77 |
, fniFileName = vfniFileName |
| 78 |
} |
| 79 |
|
| 80 |
newtype OBJECT_NAME_INFORMATION = OBJECT_NAME_INFORMATION |
| 81 |
{ oniName :: UNICODE_STRING } |
| 82 |
|
| 83 |
instance Storable OBJECT_NAME_INFORMATION where |
| 84 |
sizeOf _ = #size OBJECT_NAME_INFORMATION |
| 85 |
alignment _ = #alignment OBJECT_NAME_INFORMATION |
| 86 |
poke buf oni = (#poke OBJECT_NAME_INFORMATION, Name) buf (oniName oni) |
| 87 |
peek buf = |
| 88 |
fmap OBJECT_NAME_INFORMATION $ (#peek OBJECT_NAME_INFORMATION, Name) buf |
| 89 |
|
| 90 |
data UNICODE_STRING = UNICODE_STRING |
| 91 |
{ usLength :: USHORT |
| 92 |
, usMaximumLength :: USHORT |
| 93 |
, usBuffer :: String |
| 94 |
} |
| 95 |
|
| 96 |
instance Storable UNICODE_STRING where |
| 97 |
sizeOf _ = #size UNICODE_STRING |
| 98 |
alignment _ = #alignment UNICODE_STRING |
| 99 |
poke buf us = withTStringLen (usBuffer us) $ \(str, len) -> do |
| 100 |
let len' = (min mAX_PATH len) * sizeOfTCHAR |
| 101 |
start = advancePtr (castPtr buf) (#size UNICODE_STRING) |
| 102 |
end = advancePtr start len' |
| 103 |
(#poke UNICODE_STRING, Length) buf len' |
| 104 |
(#poke UNICODE_STRING, MaximumLength) buf (len' + sizeOfTCHAR) |
| 105 |
(#poke UNICODE_STRING, Buffer) buf start |
| 106 |
copyArray start (castPtr str :: Ptr Word8) len' |
| 107 |
poke (castPtr end) (0 :: TCHAR) |
| 108 |
peek buf = do |
| 109 |
vusLength <- (#peek UNICODE_STRING, Length) buf |
| 110 |
vusMaximumLength <- (#peek UNICODE_STRING, MaximumLength) buf |
| 111 |
vusBufferPtr <- (#peek UNICODE_STRING, Buffer) buf |
| 112 |
let len = fromIntegral vusLength `div` sizeOfTCHAR |
| 113 |
vusBuffer <- peekTStringLen (vusBufferPtr, len) |
| 114 |
pure $ UNICODE_STRING |
| 115 |
{ usLength = vusLength |
| 116 |
, usMaximumLength = vusMaximumLength |
| 117 |
, usBuffer = vusBuffer |
| 118 |
} |
| 119 |
|
| 120 |
-- | Returns 'True' is the given handle is attached to a MinTTY console |
| 121 |
-- (e.g., Cygwin or MSYS). Returns 'False' otherwise. |
| 122 |
isMinTTYHandle :: HANDLE -> IO Bool |
| 123 |
isMinTTYHandle h = do |
| 124 |
fileType <- getFileType h |
| 125 |
if fileType /= fILE_TYPE_PIPE |
| 126 |
then pure False |
| 127 |
else isMinTTYVista h `catch` \(_ :: IOError) -> isMinTTYCompat h |
| 128 |
-- GetFileNameByHandleEx is only available on Vista and later (hence |
| 129 |
-- the name isMinTTYVista). If we're on an older version of Windows, |
| 130 |
-- getProcAddress will throw an IOException when it fails to find |
| 131 |
-- GetFileNameByHandleEx, and thus we will default to using |
| 132 |
-- NtQueryObject (isMinTTYCompat). |
| 133 |
|
| 134 |
isMinTTYVista :: HANDLE -> IO Bool |
| 135 |
isMinTTYVista h = do |
| 136 |
fn <- getFileNameByHandle h |
| 137 |
pure $ cygwinMSYSCheck fn |
| 138 |
`catch` \(_ :: IOError) -> pure False |
| 139 |
|
| 140 |
cygwinMSYSCheck :: String -> Bool |
| 141 |
cygwinMSYSCheck fn = |
| 142 |
("cygwin-" `isInfixOf` fn || "msys-" `isInfixOf` fn) |
| 143 |
&& "-pty" `isInfixOf` fn |
| 144 |
-- Note that GetFileInformationByHandleEx might return a filepath like: |
| 145 |
-- |
| 146 |
-- \msys-dd50a72ab4668b33-pty1-to-master |
| 147 |
-- |
| 148 |
-- But NtQueryObject might return something like: |
| 149 |
-- |
| 150 |
-- \Device\NamedPipe\msys-dd50a72ab4668b33-pty1-to-master |
| 151 |
-- |
| 152 |
-- This means we can't rely on "\cygwin-" or "\msys-" being at the very start |
| 153 |
-- of the filepath. As a result, we use `isPrefixOf` to check for "cygwin" and |
| 154 |
-- "msys". |
| 155 |
-- |
| 156 |
-- It's unclear if "-master" will always appear in the filepath name. Recent |
| 157 |
-- versions of MinTTY have been known to give filepaths like this (#186): |
| 158 |
-- |
| 159 |
-- \msys-dd50a72ab4668b33-pty0-to-master-nat |
| 160 |
-- |
| 161 |
-- Just in case MinTTY ever changes this convention, we don't bother checking |
| 162 |
-- for the presence of "-master" in the filepath name at all. |
| 163 |
|
| 164 |
isMinTTYCompat :: HANDLE -> IO Bool |
| 165 |
isMinTTYCompat h = do |
| 166 |
fn <- ntQueryObjectNameInformation h |
| 167 |
pure $ cygwinMSYSCheck fn |
| 168 |
`catch` \(_ :: IOError) -> pure False |
| 169 |
|
| 170 |
fILE_TYPE_PIPE :: FileType |
| 171 |
fILE_TYPE_PIPE = 3 |
| 172 |
|
| 173 |
ntQueryObjectNameInformation :: HANDLE -> IO String |
| 174 |
ntQueryObjectNameInformation h = do |
| 175 |
let sizeOfONI = sizeOf (undefined :: OBJECT_NAME_INFORMATION) |
| 176 |
bufSize = sizeOfONI + mAX_PATH * sizeOfTCHAR |
| 177 |
allocaBytes bufSize $ \buf -> |
| 178 |
alloca $ \p_len -> do |
| 179 |
hwnd <- getModuleHandle (Just "ntdll.exe") |
| 180 |
addr <- getProcAddress hwnd "NtQueryObject" |
| 181 |
let c_NtQueryObject = mk_NtQueryObject (castPtrToFunPtr addr) |
| 182 |
_ <- failIfNeg "NtQueryObject" $ c_NtQueryObject |
| 183 |
h objectNameInformation buf (fromIntegral bufSize) p_len |
| 184 |
oni <- peek buf |
| 185 |
pure $ usBuffer $ oniName oni |
| 186 |
|
| 187 |
sizeOfTCHAR :: Int |
| 188 |
sizeOfTCHAR = sizeOf (undefined :: TCHAR) |
| 189 |
|
| 190 |
getFileNameByHandle :: HANDLE -> IO String |
| 191 |
getFileNameByHandle h = do |
| 192 |
let sizeOfDWORD = sizeOf (undefined :: DWORD) |
| 193 |
-- note: implicitly assuming that DWORD has stronger alignment than wchar_t |
| 194 |
bufSize = sizeOfDWORD + mAX_PATH * sizeOfTCHAR |
| 195 |
allocaBytes bufSize $ \buf -> do |
| 196 |
getFileInformationByHandleEx h fileNameInfo buf (fromIntegral bufSize) |
| 197 |
fni <- peek buf |
| 198 |
pure $ fniFileName fni |
| 199 |
|
| 200 |
getFileInformationByHandleEx :: |
| 201 |
HANDLE |
| 202 |
-> CInt |
| 203 |
-> Ptr FILE_NAME_INFO |
| 204 |
-> DWORD |
| 205 |
-> IO () |
| 206 |
getFileInformationByHandleEx h cls buf bufSize = do |
| 207 |
lib <- getModuleHandle (Just "kernel32.dll") |
| 208 |
ptr <- getProcAddress lib "GetFileInformationByHandleEx" |
| 209 |
let c_GetFileInformationByHandleEx = |
| 210 |
mk_GetFileInformationByHandleEx (castPtrToFunPtr ptr) |
| 211 |
failIfFalse_ "getFileInformationByHandleEx" |
| 212 |
(c_GetFileInformationByHandleEx h cls buf bufSize) |
| 213 |
|
| 214 |
getModuleHandle :: Maybe String -> IO HMODULE |
| 215 |
getModuleHandle mb_name = |
| 216 |
maybeWith withTString mb_name $ \ c_name -> |
| 217 |
failIfNull "GetModuleHandle" $ c_GetModuleHandle c_name |
| 218 |
|
| 219 |
getProcAddress :: HMODULE -> String -> IO Addr |
| 220 |
getProcAddress hmod procname = |
| 221 |
withCAString procname $ \ c_procname -> |
| 222 |
failIfNull "GetProcAddress" $ c_GetProcAddress hmod c_procname |
| 223 |
|
| 224 |
peekTStringLen :: (LPCTSTR, Int) -> IO String |
| 225 |
peekTStringLen = peekCWStringLen |
| 226 |
|
| 227 |
withTString :: String -> (LPTSTR -> IO a) -> IO a |
| 228 |
withTString = withCWString |
| 229 |
|
| 230 |
withTStringLen :: String -> ((LPTSTR, Int) -> IO a) -> IO a |
| 231 |
withTStringLen = withCWStringLen |
| 232 |
|
| 233 |
fileNameInfo :: CInt |
| 234 |
fileNameInfo = #const FileNameInfo |
| 235 |
|
| 236 |
mAX_PATH :: Num a => a |
| 237 |
mAX_PATH = #const MAX_PATH |
| 238 |
|
| 239 |
objectNameInformation :: CInt |
| 240 |
objectNameInformation = #const ObjectNameInformation |
| 241 |
|
| 242 |
foreign import ccall "dynamic" |
| 243 |
mk_GetFileInformationByHandleEx :: |
| 244 |
FunPtr F_GetFileInformationByHandleEx -> F_GetFileInformationByHandleEx |
| 245 |
|
| 246 |
foreign import ccall unsafe "windows.h GetFileType" |
| 247 |
getFileType :: HANDLE -> IO FileType |
| 248 |
|
| 249 |
foreign import ccall unsafe "windows.h GetProcAddress" |
| 250 |
c_GetProcAddress :: HMODULE -> LPCSTR -> IO Addr |
| 251 |
|
| 252 |
foreign import ccall "dynamic" |
| 253 |
mk_NtQueryObject :: FunPtr F_NtQueryObject -> F_NtQueryObject |
| 254 |
|
| 255 |
foreign import ccall unsafe "windows.h GetModuleHandleW" |
| 256 |
c_GetModuleHandle :: LPCTSTR -> IO HMODULE |