aboutsummaryrefslogtreecommitdiffhomepage
path: root/System/Posix/Terminal/ByteString.hsc
diff options
context:
space:
mode:
authorGravatar Herbert Valerio Riedel <hvr@gnu.org>2016-01-30 16:46:56 +0100
committerGravatar Herbert Valerio Riedel <hvr@gnu.org>2016-01-30 16:56:57 +0100
commitd17b03d4d4525103f1995441045eae4c2c73355d (patch)
tree4fda6f1009df39f4a87b8cd9c7e887e6fc2f59e6 /System/Posix/Terminal/ByteString.hsc
parent4f3b5d8b87eef07d8df62a8d7240830bb81a8a6b (diff)
Don't assume `tcdrain` and `ctermid` exist always
This follows the scheme suggested in #24 This fixes #55
Diffstat (limited to 'System/Posix/Terminal/ByteString.hsc')
-rw-r--r--System/Posix/Terminal/ByteString.hsc17
1 files changed, 16 insertions, 1 deletions
diff --git a/System/Posix/Terminal/ByteString.hsc b/System/Posix/Terminal/ByteString.hsc
index fd44c85..3c7abfb 100644
--- a/System/Posix/Terminal/ByteString.hsc
+++ b/System/Posix/Terminal/ByteString.hsc
@@ -1,3 +1,4 @@
+{-# LANGUAGE CApiFFI #-}
#if __GLASGOW_HASKELL__ >= 709
{-# LANGUAGE Safe #-}
#elif __GLASGOW_HASKELL__ >= 703
@@ -91,6 +92,10 @@ import Foreign.C hiding (
import System.Posix.ByteString.FilePath
+#if !HAVE_CTERMID
+import System.IO.Error ( ioeSetLocation )
+import GHC.IO.Exception ( unsupportedOperation )
+#endif
-- | @getTerminalName fd@ calls @ttyname@ to obtain a name associated
-- with the terminal for @Fd@ @fd@. If @fd@ is associated
@@ -109,13 +114,23 @@ foreign import ccall unsafe "ttyname"
-- controlling terminal exists,
-- @getControllingTerminalName@ returns the name of the
-- controlling terminal.
+--
+-- Throws 'IOError' (\"unsupported operation\") if platform does not
+-- provide @ctermid(3)@ (use @#if HAVE_CTERMID@ CPP guard to
+-- detect availability).
getControllingTerminalName :: IO RawFilePath
+#if HAVE_CTERMID
getControllingTerminalName = do
s <- throwErrnoIfNull "getControllingTerminalName" (c_ctermid nullPtr)
peekFilePath s
-foreign import ccall unsafe "ctermid"
+foreign import capi unsafe "termios.h ctermid"
c_ctermid :: CString -> IO CString
+#else
+{-# WARNING getControllingTerminalName
+ "operation will throw 'IOError' \"unsupported operation\" (CPP guard: @#if HAVE_CTERMID@)" #-}
+getControllingTerminalName = ioError (ioeSetLocation unsupportedOperation "getControllingTerminalName")
+#endif
-- | @getSlaveTerminalName@ calls @ptsname@ to obtain the name of the
-- slave terminal associated with a pseudoterminal pair. The file