{-# LINE 1 "libraries/unix/System/Posix/Terminal.hsc" #-} {-# LINE 2 "libraries/unix/System/Posix/Terminal.hsc" #-} {-# LANGUAGE Trustworthy #-} {-# LINE 4 "libraries/unix/System/Posix/Terminal.hsc" #-} ----------------------------------------------------------------------------- -- | -- Module : System.Posix.Terminal -- Copyright : (c) The University of Glasgow 2002 -- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : libraries@haskell.org -- Stability : provisional -- Portability : non-portable (requires POSIX) -- -- POSIX Terminal support -- ----------------------------------------------------------------------------- module System.Posix.Terminal ( -- * Terminal support -- ** Terminal attributes TerminalAttributes, getTerminalAttributes, TerminalState(..), setTerminalAttributes, TerminalMode(..), withoutMode, withMode, terminalMode, bitsPerByte, withBits, ControlCharacter(..), controlChar, withCC, withoutCC, inputTime, withTime, minInput, withMinInput, BaudRate(..), inputSpeed, withInputSpeed, outputSpeed, withOutputSpeed, -- ** Terminal operations sendBreak, drainOutput, QueueSelector(..), discardData, FlowAction(..), controlFlow, -- ** Process groups getTerminalProcessGroupID, setTerminalProcessGroupID, -- ** Testing a file descriptor queryTerminal, getTerminalName, getControllingTerminalName, -- ** Pseudoterminal operations openPseudoTerminal, getSlaveTerminalName ) where {-# LINE 73 "libraries/unix/System/Posix/Terminal.hsc" #-} import Foreign import Foreign.C import System.Posix.Terminal.Common import System.Posix.Types {-# LINE 79 "libraries/unix/System/Posix/Terminal.hsc" #-} import System.Posix.IO {-# LINE 81 "libraries/unix/System/Posix/Terminal.hsc" #-} import System.Posix.Internals (peekFilePath) -- | @getTerminalName fd@ calls @ttyname@ to obtain a name associated -- with the terminal for @Fd@ @fd@. If @fd@ is associated -- with a terminal, @getTerminalName@ returns the name of the -- terminal. getTerminalName :: Fd -> IO FilePath getTerminalName (Fd fd) = do s <- throwErrnoIfNull "getTerminalName" (c_ttyname fd) peekFilePath s foreign import ccall unsafe "ttyname" c_ttyname :: CInt -> IO CString -- | @getControllingTerminalName@ calls @ctermid@ to obtain -- a name associated with the controlling terminal for the process. If a -- controlling terminal exists, -- @getControllingTerminalName@ returns the name of the -- controlling terminal. getControllingTerminalName :: IO FilePath getControllingTerminalName = do s <- throwErrnoIfNull "getControllingTerminalName" (c_ctermid nullPtr) peekFilePath s foreign import ccall unsafe "ctermid" c_ctermid :: CString -> IO CString -- | @getSlaveTerminalName@ calls @ptsname@ to obtain the name of the -- slave terminal associated with a pseudoterminal pair. The file -- descriptor to pass in must be that of the master. getSlaveTerminalName :: Fd -> IO FilePath {-# LINE 115 "libraries/unix/System/Posix/Terminal.hsc" #-} getSlaveTerminalName (Fd fd) = do s <- throwErrnoIfNull "getSlaveTerminalName" (c_ptsname fd) peekFilePath s foreign import ccall unsafe "__hsunix_ptsname" c_ptsname :: CInt -> IO CString {-# LINE 125 "libraries/unix/System/Posix/Terminal.hsc" #-} -- ----------------------------------------------------------------------------- -- openPseudoTerminal needs to be here because it depends on -- getSlaveTerminalName. -- | @openPseudoTerminal@ creates a pseudoterminal (pty) pair, and -- returns the newly created pair as a (@master@, @slave@) tuple. openPseudoTerminal :: IO (Fd, Fd) {-# LINE 148 "libraries/unix/System/Posix/Terminal.hsc" #-} openPseudoTerminal = do (Fd master) <- openFd "/dev/ptmx" ReadWrite Nothing defaultFileFlags{noctty=True} throwErrnoIfMinus1_ "openPseudoTerminal" (c_grantpt master) throwErrnoIfMinus1_ "openPseudoTerminal" (c_unlockpt master) slaveName <- getSlaveTerminalName (Fd master) slave <- openFd slaveName ReadWrite Nothing defaultFileFlags{noctty=True} pushModule slave "ptem" pushModule slave "ldterm" {-# LINE 158 "libraries/unix/System/Posix/Terminal.hsc" #-} pushModule slave "ttcompat" {-# LINE 160 "libraries/unix/System/Posix/Terminal.hsc" #-} return (Fd master, slave) -- Push a STREAMS module, for System V systems. pushModule :: Fd -> String -> IO () pushModule (Fd fd) name = withCString name $ \p_name -> throwErrnoIfMinus1_ "openPseudoTerminal" (c_push_module fd p_name) foreign import ccall unsafe "__hsunix_push_module" c_push_module :: CInt -> CString -> IO CInt {-# LINE 173 "libraries/unix/System/Posix/Terminal.hsc" #-} foreign import ccall unsafe "__hsunix_grantpt" c_grantpt :: CInt -> IO CInt foreign import ccall unsafe "__hsunix_unlockpt" c_unlockpt :: CInt -> IO CInt {-# LINE 185 "libraries/unix/System/Posix/Terminal.hsc" #-} {-# LINE 186 "libraries/unix/System/Posix/Terminal.hsc" #-}