{-# LINE 1 "System/Posix/Terminal/Common.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE Trustworthy #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  System.Posix.Terminal.Common
-- 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
--
-----------------------------------------------------------------------------

-- see https://android.googlesource.com/platform/bionic/+/9ae59c0/libc/bionic/pathconf.c#37

{-# LINE 24 "System/Posix/Terminal/Common.hsc" #-}


module System.Posix.Terminal.Common (
  -- * Terminal support

  -- ** Terminal attributes
  TerminalAttributes,
  getTerminalAttributes,
  TerminalState(..),
  setTerminalAttributes,

  CTermios,
  TerminalMode(..),
  withoutMode,
  withMode,
  terminalMode,
  bitsPerByte,
  withBits,

  ControlCharacter(..),
  controlChar,
  withCC,
  withoutCC,

  inputTime,
  withTime,
  minInput,
  withMinInput,

  BaudRate
    ( ..
    , B0
    , B50
    , B75
    , B110
    , B134
    , B150
    , B200
    , B300
    , B600
    , B1200
    , B1800
    , B2400
    , B4800
    , B9600
    , B19200
    , B38400

{-# LINE 74 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 77 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 80 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 81 "System/Posix/Terminal/Common.hsc" #-}
    , B57600

{-# LINE 83 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 86 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 87 "System/Posix/Terminal/Common.hsc" #-}
    , B115200

{-# LINE 89 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 90 "System/Posix/Terminal/Common.hsc" #-}
    , B230400

{-# LINE 92 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 93 "System/Posix/Terminal/Common.hsc" #-}
    , B460800

{-# LINE 95 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 96 "System/Posix/Terminal/Common.hsc" #-}
    , B500000

{-# LINE 98 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 99 "System/Posix/Terminal/Common.hsc" #-}
    , B576000

{-# LINE 101 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 102 "System/Posix/Terminal/Common.hsc" #-}
    , B921600

{-# LINE 104 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 105 "System/Posix/Terminal/Common.hsc" #-}
    , B1000000

{-# LINE 107 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 108 "System/Posix/Terminal/Common.hsc" #-}
    , B1152000

{-# LINE 110 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 111 "System/Posix/Terminal/Common.hsc" #-}
    , B1500000

{-# LINE 113 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 114 "System/Posix/Terminal/Common.hsc" #-}
    , B2000000

{-# LINE 116 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 117 "System/Posix/Terminal/Common.hsc" #-}
    , B2500000

{-# LINE 119 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 120 "System/Posix/Terminal/Common.hsc" #-}
    , B3000000

{-# LINE 122 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 123 "System/Posix/Terminal/Common.hsc" #-}
    , B3500000

{-# LINE 125 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 126 "System/Posix/Terminal/Common.hsc" #-}
    , B4000000

{-# LINE 128 "System/Posix/Terminal/Common.hsc" #-}
    ),
  inputSpeed,
  withInputSpeed,
  outputSpeed,
  withOutputSpeed,

  -- ** Terminal operations
  sendBreak,
  drainOutput,
  QueueSelector(..),
  discardData,
  FlowAction(..),
  controlFlow,

  -- ** Process groups
  getTerminalProcessGroupID,
  setTerminalProcessGroupID,

  -- ** Testing a file descriptor
  queryTerminal,
  ) where



import Data.Bits
import Data.Char
import Foreign.C.Error ( throwErrnoIfMinus1, throwErrnoIfMinus1_ )
import Foreign.C.Types
import Foreign.ForeignPtr ( ForeignPtr, withForeignPtr, mallocForeignPtrBytes )
import Foreign.Marshal.Utils ( copyBytes )
import Foreign.Ptr ( Ptr, plusPtr )
import Foreign.Storable ( Storable(..) )
import System.IO.Unsafe ( unsafePerformIO )
import System.Posix.Types
import System.Posix.Internals ( CTermios )


{-# LINE 169 "System/Posix/Terminal/Common.hsc" #-}


{-# LINE 171 "System/Posix/Terminal/Common.hsc" #-}

-- -----------------------------------------------------------------------------
-- Terminal attributes

newtype TerminalAttributes = TerminalAttributes (ForeignPtr CTermios)

makeTerminalAttributes :: ForeignPtr CTermios -> TerminalAttributes
makeTerminalAttributes = TerminalAttributes

withTerminalAttributes :: TerminalAttributes -> (Ptr CTermios -> IO a) -> IO a
withTerminalAttributes (TerminalAttributes termios) = withForeignPtr termios


{-# LINE 188 "System/Posix/Terminal/Common.hsc" #-}


data TerminalMode
        -- input flags
   = InterruptOnBreak           -- ^ @BRKINT@ - Signal interrupt on break
   | MapCRtoLF                  -- ^ @ICRNL@ - Map CR to NL on input
   | IgnoreBreak                -- ^ @IGNBRK@ - Ignore break condition
   | IgnoreCR                   -- ^ @IGNCR@ - Ignore CR
   | IgnoreParityErrors         -- ^ @IGNPAR@ - Ignore characters with parity errors
   | MapLFtoCR                  -- ^ @INLCR@ - Map NL to CR on input
   | CheckParity                -- ^ @INPCK@ - Enable input parity check
   | StripHighBit               -- ^ @ISTRIP@ - Strip character
   | RestartOnAny               -- ^ @IXANY@ - Enable any character to restart output
   | StartStopInput             -- ^ @IXOFF@ - Enable start/stop input control
   | StartStopOutput            -- ^ @IXON@ - Enable start/stop output control
   | MarkParityErrors           -- ^ @PARMRK@ - Mark parity errors

        -- output flags
   | ProcessOutput              -- ^ @OPOST@ - Post-process output
   | MapLFtoCRLF                -- ^ @ONLCR@ - (XSI) Map NL to CR-NL on output
                                --
                                -- @since 2.8.0.0
   | OutputMapCRtoLF            -- ^ @OCRNL@ - (XSI) Map CR to NL on output
                                --
                                -- @since 2.8.0.0
   | NoCRAtColumnZero           -- ^ @ONOCR@ - (XSI) No CR output at column 0
                                --
                                -- @since 2.8.0.0
   | ReturnMeansLF              -- ^ @ONLRET@ - (XSI) NL performs CR function
                                --
                                -- @since 2.8.0.0
   | TabDelayMask0              -- ^ @TABDLY(TAB0)@ - (XSI) Select horizontal-tab delays: type 0
                                --
                                -- @since 2.8.0.0
   | TabDelayMask3              -- ^ @TABDLY(TAB3)@ - (XSI) Select horizontal-tab delays: type 3
                                --
                                -- @since 2.8.0.0

        -- control flags
   | LocalMode                  -- ^ @CLOCAL@ - Ignore modem status lines
   | ReadEnable                 -- ^ @CREAD@ - Enable receiver
   | TwoStopBits                -- ^ @CSTOPB@ - Send two stop bits, else one
   | HangupOnClose              -- ^ @HUPCL@ - Hang up on last close
   | EnableParity               -- ^ @PARENB@ - Parity enable
   | OddParity                  -- ^ @PARODD@ - Odd parity, else even

        -- local modes
   | EnableEcho                 -- ^ @ECHO@ - Enable echo
   | EchoErase                  -- ^ @ECHOE@ - Echo erase character as error-correcting backspace
   | EchoKill                   -- ^ @ECHOK@ - Echo KILL
   | EchoLF                     -- ^ @ECHONL@ - Echo NL
   | ProcessInput               -- ^ @ICANON@ - Canonical input (erase and kill processing)
   | ExtendedFunctions          -- ^ @IEXTEN@ - Enable extended input character processing
   | KeyboardInterrupts         -- ^ @ISIG@ - Enable signals
   | NoFlushOnInterrupt         -- ^ @NOFLSH@ - Disable flush after interrupt or quit
   | BackgroundWriteInterrupt   -- ^ @TOSTOP@ - Send @SIGTTOU@ for background output


{-# LINE 273 "System/Posix/Terminal/Common.hsc" #-}

withoutMode :: TerminalAttributes -> TerminalMode -> TerminalAttributes
withoutMode termios InterruptOnBreak = clearInputFlag (2) termios
{-# LINE 276 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios MapCRtoLF = clearInputFlag (256) termios
{-# LINE 277 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios IgnoreBreak = clearInputFlag (1) termios
{-# LINE 278 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios IgnoreCR = clearInputFlag (128) termios
{-# LINE 279 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios IgnoreParityErrors = clearInputFlag (4) termios
{-# LINE 280 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios MapLFtoCR = clearInputFlag (64) termios
{-# LINE 281 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios CheckParity = clearInputFlag (16) termios
{-# LINE 282 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios StripHighBit = clearInputFlag (32) termios
{-# LINE 283 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios RestartOnAny = clearInputFlag (2048) termios
{-# LINE 284 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios StartStopInput = clearInputFlag (4096) termios
{-# LINE 285 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios StartStopOutput = clearInputFlag (1024) termios
{-# LINE 286 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios MarkParityErrors = clearInputFlag (8) termios
{-# LINE 287 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios ProcessOutput = clearOutputFlag (1) termios
{-# LINE 288 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios MapLFtoCRLF = clearOutputFlag (4) termios
{-# LINE 289 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios OutputMapCRtoLF = clearOutputFlag (8) termios
{-# LINE 290 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios NoCRAtColumnZero = clearOutputFlag (16) termios
{-# LINE 291 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios ReturnMeansLF = clearOutputFlag (32) termios
{-# LINE 292 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios TabDelayMask0 = clearOutputFlag (0) termios
{-# LINE 293 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios TabDelayMask3 = clearOutputFlag (6144) termios
{-# LINE 294 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios LocalMode = clearControlFlag (2048) termios
{-# LINE 295 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios ReadEnable = clearControlFlag (128) termios
{-# LINE 296 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios TwoStopBits = clearControlFlag (64) termios
{-# LINE 297 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios HangupOnClose = clearControlFlag (1024) termios
{-# LINE 298 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios EnableParity = clearControlFlag (256) termios
{-# LINE 299 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios OddParity = clearControlFlag (512) termios
{-# LINE 300 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios EnableEcho = clearLocalFlag (8) termios
{-# LINE 301 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios EchoErase = clearLocalFlag (16) termios
{-# LINE 302 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios EchoKill = clearLocalFlag (32) termios
{-# LINE 303 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios EchoLF = clearLocalFlag (64) termios
{-# LINE 304 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios ProcessInput = clearLocalFlag (2) termios
{-# LINE 305 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios ExtendedFunctions = clearLocalFlag (32768) termios
{-# LINE 306 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios KeyboardInterrupts = clearLocalFlag (1) termios
{-# LINE 307 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios NoFlushOnInterrupt = setLocalFlag (128) termios
{-# LINE 308 "System/Posix/Terminal/Common.hsc" #-}
withoutMode termios BackgroundWriteInterrupt = clearLocalFlag (256) termios
{-# LINE 309 "System/Posix/Terminal/Common.hsc" #-}

withMode :: TerminalAttributes -> TerminalMode -> TerminalAttributes
withMode termios InterruptOnBreak = setInputFlag (2) termios
{-# LINE 312 "System/Posix/Terminal/Common.hsc" #-}
withMode termios MapCRtoLF = setInputFlag (256) termios
{-# LINE 313 "System/Posix/Terminal/Common.hsc" #-}
withMode termios IgnoreBreak = setInputFlag (1) termios
{-# LINE 314 "System/Posix/Terminal/Common.hsc" #-}
withMode termios IgnoreCR = setInputFlag (128) termios
{-# LINE 315 "System/Posix/Terminal/Common.hsc" #-}
withMode termios IgnoreParityErrors = setInputFlag (4) termios
{-# LINE 316 "System/Posix/Terminal/Common.hsc" #-}
withMode termios MapLFtoCR = setInputFlag (64) termios
{-# LINE 317 "System/Posix/Terminal/Common.hsc" #-}
withMode termios CheckParity = setInputFlag (16) termios
{-# LINE 318 "System/Posix/Terminal/Common.hsc" #-}
withMode termios StripHighBit = setInputFlag (32) termios
{-# LINE 319 "System/Posix/Terminal/Common.hsc" #-}
withMode termios RestartOnAny = setInputFlag (2048) termios
{-# LINE 320 "System/Posix/Terminal/Common.hsc" #-}
withMode termios StartStopInput = setInputFlag (4096) termios
{-# LINE 321 "System/Posix/Terminal/Common.hsc" #-}
withMode termios StartStopOutput = setInputFlag (1024) termios
{-# LINE 322 "System/Posix/Terminal/Common.hsc" #-}
withMode termios MarkParityErrors = setInputFlag (8) termios
{-# LINE 323 "System/Posix/Terminal/Common.hsc" #-}
withMode termios ProcessOutput = setOutputFlag (1) termios
{-# LINE 324 "System/Posix/Terminal/Common.hsc" #-}
withMode termios MapLFtoCRLF = setOutputFlag (4) termios
{-# LINE 325 "System/Posix/Terminal/Common.hsc" #-}
withMode termios OutputMapCRtoLF = setOutputFlag (8) termios
{-# LINE 326 "System/Posix/Terminal/Common.hsc" #-}
withMode termios NoCRAtColumnZero = setOutputFlag (16) termios
{-# LINE 327 "System/Posix/Terminal/Common.hsc" #-}
withMode termios ReturnMeansLF = setOutputFlag (32) termios
{-# LINE 328 "System/Posix/Terminal/Common.hsc" #-}
withMode termios TabDelayMask0 = setOutputFlag (0) termios
{-# LINE 329 "System/Posix/Terminal/Common.hsc" #-}
withMode termios TabDelayMask3 = setOutputFlag (6144) termios
{-# LINE 330 "System/Posix/Terminal/Common.hsc" #-}
withMode termios LocalMode = setControlFlag (2048) termios
{-# LINE 331 "System/Posix/Terminal/Common.hsc" #-}
withMode termios ReadEnable = setControlFlag (128) termios
{-# LINE 332 "System/Posix/Terminal/Common.hsc" #-}
withMode termios TwoStopBits = setControlFlag (64) termios
{-# LINE 333 "System/Posix/Terminal/Common.hsc" #-}
withMode termios HangupOnClose = setControlFlag (1024) termios
{-# LINE 334 "System/Posix/Terminal/Common.hsc" #-}
withMode termios EnableParity = setControlFlag (256) termios
{-# LINE 335 "System/Posix/Terminal/Common.hsc" #-}
withMode termios OddParity = setControlFlag (512) termios
{-# LINE 336 "System/Posix/Terminal/Common.hsc" #-}
withMode termios EnableEcho = setLocalFlag (8) termios
{-# LINE 337 "System/Posix/Terminal/Common.hsc" #-}
withMode termios EchoErase = setLocalFlag (16) termios
{-# LINE 338 "System/Posix/Terminal/Common.hsc" #-}
withMode termios EchoKill = setLocalFlag (32) termios
{-# LINE 339 "System/Posix/Terminal/Common.hsc" #-}
withMode termios EchoLF = setLocalFlag (64) termios
{-# LINE 340 "System/Posix/Terminal/Common.hsc" #-}
withMode termios ProcessInput = setLocalFlag (2) termios
{-# LINE 341 "System/Posix/Terminal/Common.hsc" #-}
withMode termios ExtendedFunctions = setLocalFlag (32768) termios
{-# LINE 342 "System/Posix/Terminal/Common.hsc" #-}
withMode termios KeyboardInterrupts = setLocalFlag (1) termios
{-# LINE 343 "System/Posix/Terminal/Common.hsc" #-}
withMode termios NoFlushOnInterrupt = clearLocalFlag (128) termios
{-# LINE 344 "System/Posix/Terminal/Common.hsc" #-}
withMode termios BackgroundWriteInterrupt = setLocalFlag (256) termios
{-# LINE 345 "System/Posix/Terminal/Common.hsc" #-}

terminalMode :: TerminalMode -> TerminalAttributes -> Bool
terminalMode InterruptOnBreak = testInputFlag (2)
{-# LINE 348 "System/Posix/Terminal/Common.hsc" #-}
terminalMode MapCRtoLF = testInputFlag (256)
{-# LINE 349 "System/Posix/Terminal/Common.hsc" #-}
terminalMode IgnoreBreak = testInputFlag (1)
{-# LINE 350 "System/Posix/Terminal/Common.hsc" #-}
terminalMode IgnoreCR = testInputFlag (128)
{-# LINE 351 "System/Posix/Terminal/Common.hsc" #-}
terminalMode IgnoreParityErrors = testInputFlag (4)
{-# LINE 352 "System/Posix/Terminal/Common.hsc" #-}
terminalMode MapLFtoCR = testInputFlag (64)
{-# LINE 353 "System/Posix/Terminal/Common.hsc" #-}
terminalMode CheckParity = testInputFlag (16)
{-# LINE 354 "System/Posix/Terminal/Common.hsc" #-}
terminalMode StripHighBit = testInputFlag (32)
{-# LINE 355 "System/Posix/Terminal/Common.hsc" #-}
terminalMode RestartOnAny = testInputFlag (2048)
{-# LINE 356 "System/Posix/Terminal/Common.hsc" #-}
terminalMode StartStopInput = testInputFlag (4096)
{-# LINE 357 "System/Posix/Terminal/Common.hsc" #-}
terminalMode StartStopOutput = testInputFlag (1024)
{-# LINE 358 "System/Posix/Terminal/Common.hsc" #-}
terminalMode MarkParityErrors = testInputFlag (8)
{-# LINE 359 "System/Posix/Terminal/Common.hsc" #-}
terminalMode ProcessOutput = testOutputFlag (1)
{-# LINE 360 "System/Posix/Terminal/Common.hsc" #-}
terminalMode MapLFtoCRLF = testOutputFlag (4)
{-# LINE 361 "System/Posix/Terminal/Common.hsc" #-}
terminalMode OutputMapCRtoLF = testOutputFlag (8)
{-# LINE 362 "System/Posix/Terminal/Common.hsc" #-}
terminalMode NoCRAtColumnZero = testOutputFlag (16)
{-# LINE 363 "System/Posix/Terminal/Common.hsc" #-}
terminalMode ReturnMeansLF = testOutputFlag (32)
{-# LINE 364 "System/Posix/Terminal/Common.hsc" #-}
terminalMode TabDelayMask0 = testOutputFlag (0)
{-# LINE 365 "System/Posix/Terminal/Common.hsc" #-}
terminalMode TabDelayMask3 = testOutputFlag (6144)
{-# LINE 366 "System/Posix/Terminal/Common.hsc" #-}
terminalMode LocalMode = testControlFlag (2048)
{-# LINE 367 "System/Posix/Terminal/Common.hsc" #-}
terminalMode ReadEnable = testControlFlag (128)
{-# LINE 368 "System/Posix/Terminal/Common.hsc" #-}
terminalMode TwoStopBits = testControlFlag (64)
{-# LINE 369 "System/Posix/Terminal/Common.hsc" #-}
terminalMode HangupOnClose = testControlFlag (1024)
{-# LINE 370 "System/Posix/Terminal/Common.hsc" #-}
terminalMode EnableParity = testControlFlag (256)
{-# LINE 371 "System/Posix/Terminal/Common.hsc" #-}
terminalMode OddParity = testControlFlag (512)
{-# LINE 372 "System/Posix/Terminal/Common.hsc" #-}
terminalMode EnableEcho = testLocalFlag (8)
{-# LINE 373 "System/Posix/Terminal/Common.hsc" #-}
terminalMode EchoErase = testLocalFlag (16)
{-# LINE 374 "System/Posix/Terminal/Common.hsc" #-}
terminalMode EchoKill = testLocalFlag (32)
{-# LINE 375 "System/Posix/Terminal/Common.hsc" #-}
terminalMode EchoLF = testLocalFlag (64)
{-# LINE 376 "System/Posix/Terminal/Common.hsc" #-}
terminalMode ProcessInput = testLocalFlag (2)
{-# LINE 377 "System/Posix/Terminal/Common.hsc" #-}
terminalMode ExtendedFunctions = testLocalFlag (32768)
{-# LINE 378 "System/Posix/Terminal/Common.hsc" #-}
terminalMode KeyboardInterrupts = testLocalFlag (1)
{-# LINE 379 "System/Posix/Terminal/Common.hsc" #-}
terminalMode NoFlushOnInterrupt = not . testLocalFlag (128)
{-# LINE 380 "System/Posix/Terminal/Common.hsc" #-}
terminalMode BackgroundWriteInterrupt = testLocalFlag (256)
{-# LINE 381 "System/Posix/Terminal/Common.hsc" #-}

bitsPerByte :: TerminalAttributes -> Int
bitsPerByte termios = unsafePerformIO $ do
  withTerminalAttributes termios $ \p -> do
    cflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p
{-# LINE 386 "System/Posix/Terminal/Common.hsc" #-}
    return $! (word2Bits (cflag .&. (48)))
{-# LINE 387 "System/Posix/Terminal/Common.hsc" #-}
  where
    word2Bits :: CTcflag -> Int
    word2Bits x =
        if x == (0) then 5
{-# LINE 391 "System/Posix/Terminal/Common.hsc" #-}
        else if x == (16) then 6
{-# LINE 392 "System/Posix/Terminal/Common.hsc" #-}
        else if x == (32) then 7
{-# LINE 393 "System/Posix/Terminal/Common.hsc" #-}
        else if x == (48) then 8
{-# LINE 394 "System/Posix/Terminal/Common.hsc" #-}
        else 0

withBits :: TerminalAttributes -> Int -> TerminalAttributes
withBits termios bits = unsafePerformIO $ do
  withNewTermios termios $ \p -> do
    cflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p
{-# LINE 400 "System/Posix/Terminal/Common.hsc" #-}
    ((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p
{-# LINE 401 "System/Posix/Terminal/Common.hsc" #-}
       ((cflag .&. complement (48)) .|. mask bits)
{-# LINE 402 "System/Posix/Terminal/Common.hsc" #-}
  where
    mask :: Int -> CTcflag
    mask 5 = (0)
{-# LINE 405 "System/Posix/Terminal/Common.hsc" #-}
    mask 6 = (16)
{-# LINE 406 "System/Posix/Terminal/Common.hsc" #-}
    mask 7 = (32)
{-# LINE 407 "System/Posix/Terminal/Common.hsc" #-}
    mask 8 = (48)
{-# LINE 408 "System/Posix/Terminal/Common.hsc" #-}
    mask _ = error "withBits bit value out of range [5..8]"


{-# LINE 411 "System/Posix/Terminal/Common.hsc" #-}

data ControlCharacter
  = EndOfFile           -- VEOF
  | EndOfLine           -- VEOL
  | Erase               -- VERASE
  | Interrupt           -- VINTR
  | Kill                -- VKILL
  | Quit                -- VQUIT
  | Start               -- VSTART
  | Stop                -- VSTOP
  | Suspend             -- VSUSP


{-# LINE 517 "System/Posix/Terminal/Common.hsc" #-}

controlChar :: TerminalAttributes -> ControlCharacter -> Maybe Char
controlChar termios cc = unsafePerformIO $ do
  withTerminalAttributes termios $ \p -> do
    let c_cc = ((\hsc_ptr -> hsc_ptr `plusPtr` 17)) p
{-# LINE 522 "System/Posix/Terminal/Common.hsc" #-}
    val <- peekElemOff c_cc (cc2Word cc)
    if val == ((0)::CCc)
{-# LINE 524 "System/Posix/Terminal/Common.hsc" #-}
       then return Nothing
       else return (Just (chr (fromEnum val)))

withCC :: TerminalAttributes
       -> (ControlCharacter, Char)
       -> TerminalAttributes
withCC termios (cc, c) = unsafePerformIO $ do
  withNewTermios termios $ \p -> do
    let c_cc = ((\hsc_ptr -> hsc_ptr `plusPtr` 17)) p
{-# LINE 533 "System/Posix/Terminal/Common.hsc" #-}
    pokeElemOff c_cc (cc2Word cc) (fromIntegral (ord c) :: CCc)

withoutCC :: TerminalAttributes
          -> ControlCharacter
          -> TerminalAttributes
withoutCC termios cc = unsafePerformIO $ do
  withNewTermios termios $ \p -> do
    let c_cc = ((\hsc_ptr -> hsc_ptr `plusPtr` 17)) p
{-# LINE 541 "System/Posix/Terminal/Common.hsc" #-}
    pokeElemOff c_cc (cc2Word cc) ((0) :: CCc)
{-# LINE 542 "System/Posix/Terminal/Common.hsc" #-}

inputTime :: TerminalAttributes -> Int
inputTime termios = unsafePerformIO $ do
  withTerminalAttributes termios $ \p -> do
    c <- peekElemOff (((\hsc_ptr -> hsc_ptr `plusPtr` 17)) p) (5)
{-# LINE 547 "System/Posix/Terminal/Common.hsc" #-}
    return (fromEnum (c :: CCc))

withTime :: TerminalAttributes -> Int -> TerminalAttributes
withTime termios time = unsafePerformIO $ do
  withNewTermios termios $ \p -> do
    let c_cc = ((\hsc_ptr -> hsc_ptr `plusPtr` 17)) p
{-# LINE 553 "System/Posix/Terminal/Common.hsc" #-}
    pokeElemOff c_cc (5) (fromIntegral time :: CCc)
{-# LINE 554 "System/Posix/Terminal/Common.hsc" #-}

minInput :: TerminalAttributes -> Int
minInput termios = unsafePerformIO $ do
  withTerminalAttributes termios $ \p -> do
    c <- peekElemOff (((\hsc_ptr -> hsc_ptr `plusPtr` 17)) p) (6)
{-# LINE 559 "System/Posix/Terminal/Common.hsc" #-}
    return (fromEnum (c :: CCc))

withMinInput :: TerminalAttributes -> Int -> TerminalAttributes
withMinInput termios count = unsafePerformIO $ do
  withNewTermios termios $ \p -> do
    let c_cc = ((\hsc_ptr -> hsc_ptr `plusPtr` 17)) p
{-# LINE 565 "System/Posix/Terminal/Common.hsc" #-}
    pokeElemOff c_cc (6) (fromIntegral count :: CCc)
{-# LINE 566 "System/Posix/Terminal/Common.hsc" #-}

-- | Serial line baudrate.  The set of supported speeds is system-dependent.
-- Portable use of the provided pattern synonyms that are outside the list
-- mandated by POSIX requires @#ifdef@ guards.
--
-- Applications may need to be prepared to encounter speeds not known at
-- compile time, these can be handled generically via the 'BaudRate'
-- constructor.  In other words, the provided pattern synonyms are not
-- necessarily a @COMPLETE@ set.
--
-- All non-zero /pseudo-terminal/ baud rates are functionally equivalent, and
-- the @pty@ driver may accept any speed within a suitable range.  Requested
-- speeds may be rounded up or down to fit into the supported range.
--
newtype BaudRate = BaudRate CSpeed deriving (Eq, Ord, Show, Enum, Real, Num)

-- | Hang up
pattern B0 :: BaudRate
pattern B0 = BaudRate (0)
{-# LINE 585 "System/Posix/Terminal/Common.hsc" #-}
-- | 50 baud
pattern B50 :: BaudRate
pattern B50 = BaudRate (1)
{-# LINE 588 "System/Posix/Terminal/Common.hsc" #-}
-- | 75 baud
pattern B75 :: BaudRate
pattern B75 = BaudRate (2)
{-# LINE 591 "System/Posix/Terminal/Common.hsc" #-}
-- | 110 baud
pattern B110 :: BaudRate
pattern B110 = BaudRate (3)
{-# LINE 594 "System/Posix/Terminal/Common.hsc" #-}
-- | 134.5 baud
pattern B134 :: BaudRate
pattern B134 = BaudRate (4)
{-# LINE 597 "System/Posix/Terminal/Common.hsc" #-}
-- | 150 baud
pattern B150 :: BaudRate
pattern B150 = BaudRate (5)
{-# LINE 600 "System/Posix/Terminal/Common.hsc" #-}
-- | 200 baud
pattern B200 :: BaudRate
pattern B200 = BaudRate (6)
{-# LINE 603 "System/Posix/Terminal/Common.hsc" #-}
-- | 300 baud
pattern B300 :: BaudRate
pattern B300 = BaudRate (7)
{-# LINE 606 "System/Posix/Terminal/Common.hsc" #-}
-- | 600 baud
pattern B600 :: BaudRate
pattern B600 = BaudRate (8)
{-# LINE 609 "System/Posix/Terminal/Common.hsc" #-}
-- | 1200 baud
pattern B1200 :: BaudRate
pattern B1200 = BaudRate (9)
{-# LINE 612 "System/Posix/Terminal/Common.hsc" #-}
-- | 1800 baud
pattern B1800 :: BaudRate
pattern B1800 = BaudRate (10)
{-# LINE 615 "System/Posix/Terminal/Common.hsc" #-}
-- | 2400 baud
pattern B2400 :: BaudRate
pattern B2400 = BaudRate (11)
{-# LINE 618 "System/Posix/Terminal/Common.hsc" #-}
-- | 4800 baud
pattern B4800 :: BaudRate
pattern B4800 = BaudRate (12)
{-# LINE 621 "System/Posix/Terminal/Common.hsc" #-}
-- | 9600 baud
pattern B9600 :: BaudRate
pattern B9600 = BaudRate (13)
{-# LINE 624 "System/Posix/Terminal/Common.hsc" #-}
-- | 19200 baud
pattern B19200 :: BaudRate
pattern B19200 = BaudRate (14)
{-# LINE 627 "System/Posix/Terminal/Common.hsc" #-}
-- | 38400 baud
pattern B38400 :: BaudRate
pattern B38400 = BaudRate (15)
{-# LINE 630 "System/Posix/Terminal/Common.hsc" #-}


{-# LINE 636 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 641 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 646 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 647 "System/Posix/Terminal/Common.hsc" #-}
-- | 57600 baud, non-POSIX system-dependent extension
pattern B57600 :: BaudRate
pattern B57600 = BaudRate (4097)
{-# LINE 650 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 651 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 656 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 657 "System/Posix/Terminal/Common.hsc" #-}
-- | 115200 baud, non-POSIX system-dependent extension
pattern B115200 :: BaudRate
pattern B115200 = BaudRate (4098)
{-# LINE 660 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 661 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 662 "System/Posix/Terminal/Common.hsc" #-}
-- | 230400 baud, non-POSIX system-dependent extension
pattern B230400 :: BaudRate
pattern B230400 = BaudRate (4099)
{-# LINE 665 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 666 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 667 "System/Posix/Terminal/Common.hsc" #-}
-- | 460800 baud, non-POSIX system-dependent extension
pattern B460800 :: BaudRate
pattern B460800 = BaudRate (4100)
{-# LINE 670 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 671 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 672 "System/Posix/Terminal/Common.hsc" #-}
-- | 500000 baud, non-POSIX system-dependent extension
pattern B500000 :: BaudRate
pattern B500000 = BaudRate (4101)
{-# LINE 675 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 676 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 677 "System/Posix/Terminal/Common.hsc" #-}
-- | 576000 baud, non-POSIX system-dependent extension
pattern B576000 :: BaudRate
pattern B576000 = BaudRate (4102)
{-# LINE 680 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 681 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 682 "System/Posix/Terminal/Common.hsc" #-}
-- | 921600 baud, non-POSIX system-dependent extension
pattern B921600 :: BaudRate
pattern B921600 = BaudRate (4103)
{-# LINE 685 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 686 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 687 "System/Posix/Terminal/Common.hsc" #-}
-- | 1000000 baud, non-POSIX system-dependent extension
pattern B1000000 :: BaudRate
pattern B1000000 = BaudRate (4104)
{-# LINE 690 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 691 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 692 "System/Posix/Terminal/Common.hsc" #-}
-- | 1152000 baud, non-POSIX system-dependent extension
pattern B1152000 :: BaudRate
pattern B1152000 = BaudRate (4105)
{-# LINE 695 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 696 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 697 "System/Posix/Terminal/Common.hsc" #-}
-- | 1500000 baud, non-POSIX system-dependent extension
pattern B1500000 :: BaudRate
pattern B1500000 = BaudRate (4106)
{-# LINE 700 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 701 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 702 "System/Posix/Terminal/Common.hsc" #-}
-- | 2000000 baud, non-POSIX system-dependent extension
pattern B2000000 :: BaudRate
pattern B2000000 = BaudRate (4107)
{-# LINE 705 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 706 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 707 "System/Posix/Terminal/Common.hsc" #-}
-- | 2500000 baud, non-POSIX system-dependent extension
pattern B2500000 :: BaudRate
pattern B2500000 = BaudRate (4108)
{-# LINE 710 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 711 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 712 "System/Posix/Terminal/Common.hsc" #-}
-- | 3000000 baud, non-POSIX system-dependent extension
pattern B3000000 :: BaudRate
pattern B3000000 = BaudRate (4109)
{-# LINE 715 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 716 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 717 "System/Posix/Terminal/Common.hsc" #-}
-- | 3500000 baud, non-POSIX system-dependent extension
pattern B3500000 :: BaudRate
pattern B3500000 = BaudRate (4110)
{-# LINE 720 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 721 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 722 "System/Posix/Terminal/Common.hsc" #-}
-- | 4000000 baud, non-POSIX system-dependent extension
pattern B4000000 :: BaudRate
pattern B4000000 = BaudRate (4111)
{-# LINE 725 "System/Posix/Terminal/Common.hsc" #-}

{-# LINE 726 "System/Posix/Terminal/Common.hsc" #-}


{-# LINE 728 "System/Posix/Terminal/Common.hsc" #-}


{-# LINE 757 "System/Posix/Terminal/Common.hsc" #-}

inputSpeed :: TerminalAttributes -> BaudRate
inputSpeed termios = unsafePerformIO $ do
  withTerminalAttributes termios $ \p -> do
    w <- c_cfgetispeed p
    return (BaudRate w)

foreign import capi unsafe "termios.h cfgetispeed"
  c_cfgetispeed :: Ptr CTermios -> IO CSpeed

withInputSpeed :: TerminalAttributes -> BaudRate -> TerminalAttributes
withInputSpeed termios (BaudRate br) = unsafePerformIO $ do
  withNewTermios termios $ \p -> c_cfsetispeed p br

foreign import capi unsafe "termios.h cfsetispeed"
  c_cfsetispeed :: Ptr CTermios -> CSpeed -> IO CInt


outputSpeed :: TerminalAttributes -> BaudRate
outputSpeed termios = unsafePerformIO $ do
  withTerminalAttributes termios $ \p ->  do
    w <- c_cfgetospeed p
    return (BaudRate w)

foreign import capi unsafe "termios.h cfgetospeed"
  c_cfgetospeed :: Ptr CTermios -> IO CSpeed

withOutputSpeed :: TerminalAttributes -> BaudRate -> TerminalAttributes
withOutputSpeed termios (BaudRate br) = unsafePerformIO $ do
  withNewTermios termios $ \p -> c_cfsetospeed p br

foreign import capi unsafe "termios.h cfsetospeed"
  c_cfsetospeed :: Ptr CTermios -> CSpeed -> IO CInt

-- | @getTerminalAttributes fd@ calls @tcgetattr@ to obtain
--   the @TerminalAttributes@ associated with @Fd@ @fd@.
getTerminalAttributes :: Fd -> IO TerminalAttributes
getTerminalAttributes (Fd fd) = do
  fp <- mallocForeignPtrBytes (60)
{-# LINE 796 "System/Posix/Terminal/Common.hsc" #-}
  withForeignPtr fp $ \p ->
      throwErrnoIfMinus1_ "getTerminalAttributes" (c_tcgetattr fd p)
  return $ makeTerminalAttributes fp

foreign import capi unsafe "termios.h tcgetattr"
  c_tcgetattr :: CInt -> Ptr CTermios -> IO CInt


{-# LINE 804 "System/Posix/Terminal/Common.hsc" #-}

data TerminalState
  = Immediately
  | WhenDrained
  | WhenFlushed


{-# LINE 831 "System/Posix/Terminal/Common.hsc" #-}

-- | @setTerminalAttributes fd attr ts@ calls @tcsetattr@ to change
--   the @TerminalAttributes@ associated with @Fd@ @fd@ to
--   @attr@, when the terminal is in the state indicated by @ts@.
setTerminalAttributes :: Fd
                      -> TerminalAttributes
                      -> TerminalState
                      -> IO ()
setTerminalAttributes (Fd fd) termios state = do
  withTerminalAttributes termios $ \p ->
    throwErrnoIfMinus1_ "setTerminalAttributes"
      (c_tcsetattr fd (state2Int state) p)
  where
    state2Int :: TerminalState -> CInt
    state2Int Immediately = (0)
{-# LINE 846 "System/Posix/Terminal/Common.hsc" #-}
    state2Int WhenDrained = (1)
{-# LINE 847 "System/Posix/Terminal/Common.hsc" #-}
    state2Int WhenFlushed = (2)
{-# LINE 848 "System/Posix/Terminal/Common.hsc" #-}

foreign import capi unsafe "termios.h tcsetattr"
   c_tcsetattr :: CInt -> CInt -> Ptr CTermios -> IO CInt

-- | @sendBreak fd duration@ calls @tcsendbreak@ to transmit a
--   continuous stream of zero-valued bits on @Fd@ @fd@ for the
--   specified implementation-dependent @duration@.
sendBreak :: Fd -> Int -> IO ()
sendBreak (Fd fd) duration
  = throwErrnoIfMinus1_ "sendBreak" (c_tcsendbreak fd (fromIntegral duration))

foreign import capi unsafe "termios.h tcsendbreak"
  c_tcsendbreak :: CInt -> CInt -> IO CInt

-- | @drainOutput fd@ calls @tcdrain@ to block until all output
--   written to @Fd@ @fd@ has been transmitted.
--
-- Throws 'IOError' (\"unsupported operation\") if platform does not
-- provide @tcdrain(3)@ (use @#if HAVE_TCDRAIN@ CPP guard to
-- detect availability).
drainOutput :: Fd -> IO ()

{-# LINE 870 "System/Posix/Terminal/Common.hsc" #-}
drainOutput (Fd fd) = throwErrnoIfMinus1_ "drainOutput" (c_tcdrain fd)

foreign import capi safe "termios.h tcdrain"
  c_tcdrain :: CInt -> IO CInt

{-# LINE 879 "System/Posix/Terminal/Common.hsc" #-}


{-# LINE 881 "System/Posix/Terminal/Common.hsc" #-}

data QueueSelector
  = InputQueue          -- TCIFLUSH
  | OutputQueue         -- TCOFLUSH
  | BothQueues          -- TCIOFLUSH


{-# LINE 895 "System/Posix/Terminal/Common.hsc" #-}

-- | @discardData fd queues@ calls @tcflush@ to discard
--   pending input and\/or output for @Fd@ @fd@,
--   as indicated by the @QueueSelector@ @queues@.
discardData :: Fd -> QueueSelector -> IO ()
discardData (Fd fd) queue =
  throwErrnoIfMinus1_ "discardData" (c_tcflush fd (queue2Int queue))
  where
    queue2Int :: QueueSelector -> CInt
    queue2Int InputQueue  = (0)
{-# LINE 905 "System/Posix/Terminal/Common.hsc" #-}
    queue2Int OutputQueue = (1)
{-# LINE 906 "System/Posix/Terminal/Common.hsc" #-}
    queue2Int BothQueues  = (2)
{-# LINE 907 "System/Posix/Terminal/Common.hsc" #-}

foreign import capi unsafe "termios.h tcflush"
  c_tcflush :: CInt -> CInt -> IO CInt


{-# LINE 912 "System/Posix/Terminal/Common.hsc" #-}

data FlowAction
  = SuspendOutput       -- ^ TCOOFF
  | RestartOutput       -- ^ TCOON
  | TransmitStop        -- ^ TCIOFF
  | TransmitStart       -- ^ TCION


{-# LINE 937 "System/Posix/Terminal/Common.hsc" #-}

-- | @controlFlow fd action@ calls @tcflow@ to control the
--   flow of data on @Fd@ @fd@, as indicated by
--   @action@.
controlFlow :: Fd -> FlowAction -> IO ()
controlFlow (Fd fd) action =
  throwErrnoIfMinus1_ "controlFlow" (c_tcflow fd (action2Int action))
  where
    action2Int :: FlowAction -> CInt
    action2Int SuspendOutput = (0)
{-# LINE 947 "System/Posix/Terminal/Common.hsc" #-}
    action2Int RestartOutput = (1)
{-# LINE 948 "System/Posix/Terminal/Common.hsc" #-}
    action2Int TransmitStop  = (2)
{-# LINE 949 "System/Posix/Terminal/Common.hsc" #-}
    action2Int TransmitStart = (3)
{-# LINE 950 "System/Posix/Terminal/Common.hsc" #-}

foreign import capi unsafe "termios.h tcflow"
  c_tcflow :: CInt -> CInt -> IO CInt

-- | @getTerminalProcessGroupID fd@ calls @tcgetpgrp@ to
--   obtain the @ProcessGroupID@ of the foreground process group
--   associated with the terminal attached to @Fd@ @fd@.
getTerminalProcessGroupID :: Fd -> IO ProcessGroupID
getTerminalProcessGroupID (Fd fd) = do
  throwErrnoIfMinus1 "getTerminalProcessGroupID" (c_tcgetpgrp fd)

foreign import ccall unsafe "tcgetpgrp"
  c_tcgetpgrp :: CInt -> IO CPid

-- | @setTerminalProcessGroupID fd pgid@ calls @tcsetpgrp@ to
--   set the @ProcessGroupID@ of the foreground process group
--   associated with the terminal attached to @Fd@
--   @fd@ to @pgid@.
setTerminalProcessGroupID :: Fd -> ProcessGroupID -> IO ()
setTerminalProcessGroupID (Fd fd) pgid =
  throwErrnoIfMinus1_ "setTerminalProcessGroupID" (c_tcsetpgrp fd pgid)

foreign import ccall unsafe "tcsetpgrp"
  c_tcsetpgrp :: CInt -> CPid -> IO CInt


{-# LINE 976 "System/Posix/Terminal/Common.hsc" #-}

-- -----------------------------------------------------------------------------
-- file descriptor queries

-- | @queryTerminal fd@ calls @isatty@ to determine whether or
--   not @Fd@ @fd@ is associated with a terminal.
queryTerminal :: Fd -> IO Bool
queryTerminal (Fd fd) = do
  r <- c_isatty fd
  return (r == 1)
  -- ToDo: the spec says that it can set errno to EBADF if the result is zero

foreign import ccall unsafe "isatty"
  c_isatty :: CInt -> IO CInt

-- -----------------------------------------------------------------------------
-- Local utility functions

-- Convert Haskell ControlCharacter to Int


{-# LINE 997 "System/Posix/Terminal/Common.hsc" #-}

cc2Word :: ControlCharacter -> Int
cc2Word EndOfFile = (4)
{-# LINE 1000 "System/Posix/Terminal/Common.hsc" #-}
cc2Word EndOfLine = (11)
{-# LINE 1001 "System/Posix/Terminal/Common.hsc" #-}
cc2Word Erase     = (2)
{-# LINE 1002 "System/Posix/Terminal/Common.hsc" #-}
cc2Word Interrupt = (0)
{-# LINE 1003 "System/Posix/Terminal/Common.hsc" #-}
cc2Word Kill      = (3)
{-# LINE 1004 "System/Posix/Terminal/Common.hsc" #-}
cc2Word Quit      = (1)
{-# LINE 1005 "System/Posix/Terminal/Common.hsc" #-}
cc2Word Suspend   = (10)
{-# LINE 1006 "System/Posix/Terminal/Common.hsc" #-}
cc2Word Start     = (8)
{-# LINE 1007 "System/Posix/Terminal/Common.hsc" #-}
cc2Word Stop      = (9)
{-# LINE 1008 "System/Posix/Terminal/Common.hsc" #-}

-- Clear termios i_flag

clearInputFlag :: CTcflag -> TerminalAttributes -> TerminalAttributes
clearInputFlag flag termios = unsafePerformIO $ do
  fp <- mallocForeignPtrBytes (60)
{-# LINE 1014 "System/Posix/Terminal/Common.hsc" #-}
  withForeignPtr fp $ \p1 -> do
    withTerminalAttributes termios $ \p2 -> do
      copyBytes p1 p2 (60)
{-# LINE 1017 "System/Posix/Terminal/Common.hsc" #-}
      iflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) p2
{-# LINE 1018 "System/Posix/Terminal/Common.hsc" #-}
      ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p1 (iflag .&. complement flag)
{-# LINE 1019 "System/Posix/Terminal/Common.hsc" #-}
  return $ makeTerminalAttributes fp

-- Set termios i_flag

setInputFlag :: CTcflag -> TerminalAttributes -> TerminalAttributes
setInputFlag flag termios = unsafePerformIO $ do
  fp <- mallocForeignPtrBytes (60)
{-# LINE 1026 "System/Posix/Terminal/Common.hsc" #-}
  withForeignPtr fp $ \p1 -> do
    withTerminalAttributes termios $ \p2 -> do
      copyBytes p1 p2 (60)
{-# LINE 1029 "System/Posix/Terminal/Common.hsc" #-}
      iflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) p2
{-# LINE 1030 "System/Posix/Terminal/Common.hsc" #-}
      ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p1 (iflag .|. flag)
{-# LINE 1031 "System/Posix/Terminal/Common.hsc" #-}
  return $ makeTerminalAttributes fp

-- Examine termios i_flag

testInputFlag :: CTcflag -> TerminalAttributes -> Bool
testInputFlag flag termios = unsafePerformIO $
  withTerminalAttributes termios $ \p ->  do
    iflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) p
{-# LINE 1039 "System/Posix/Terminal/Common.hsc" #-}
    return $! ((iflag .&. flag) /= 0)

-- Clear termios c_flag

clearControlFlag :: CTcflag -> TerminalAttributes -> TerminalAttributes
clearControlFlag flag termios = unsafePerformIO $ do
  fp <- mallocForeignPtrBytes (60)
{-# LINE 1046 "System/Posix/Terminal/Common.hsc" #-}
  withForeignPtr fp $ \p1 -> do
    withTerminalAttributes termios $ \p2 -> do
      copyBytes p1 p2 (60)
{-# LINE 1049 "System/Posix/Terminal/Common.hsc" #-}
      cflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p2
{-# LINE 1050 "System/Posix/Terminal/Common.hsc" #-}
      ((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p1 (cflag .&. complement flag)
{-# LINE 1051 "System/Posix/Terminal/Common.hsc" #-}
  return $ makeTerminalAttributes fp

-- Set termios c_flag

setControlFlag :: CTcflag -> TerminalAttributes -> TerminalAttributes
setControlFlag flag termios = unsafePerformIO $ do
  fp <- mallocForeignPtrBytes (60)
{-# LINE 1058 "System/Posix/Terminal/Common.hsc" #-}
  withForeignPtr fp $ \p1 -> do
    withTerminalAttributes termios $ \p2 -> do
      copyBytes p1 p2 (60)
{-# LINE 1061 "System/Posix/Terminal/Common.hsc" #-}
      cflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p2
{-# LINE 1062 "System/Posix/Terminal/Common.hsc" #-}
      ((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p1 (cflag .|. flag)
{-# LINE 1063 "System/Posix/Terminal/Common.hsc" #-}
  return $ makeTerminalAttributes fp

-- Examine termios c_flag

testControlFlag :: CTcflag -> TerminalAttributes -> Bool
testControlFlag flag termios = unsafePerformIO $
  withTerminalAttributes termios $ \p -> do
    cflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p
{-# LINE 1071 "System/Posix/Terminal/Common.hsc" #-}
    return $! ((cflag .&. flag) /= 0)

-- Clear termios l_flag

clearLocalFlag :: CTcflag -> TerminalAttributes -> TerminalAttributes
clearLocalFlag flag termios = unsafePerformIO $ do
  fp <- mallocForeignPtrBytes (60)
{-# LINE 1078 "System/Posix/Terminal/Common.hsc" #-}
  withForeignPtr fp $ \p1 -> do
    withTerminalAttributes termios $ \p2 -> do
      copyBytes p1 p2 (60)
{-# LINE 1081 "System/Posix/Terminal/Common.hsc" #-}
      lflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 12)) p2
{-# LINE 1082 "System/Posix/Terminal/Common.hsc" #-}
      ((\hsc_ptr -> pokeByteOff hsc_ptr 12)) p1 (lflag .&. complement flag)
{-# LINE 1083 "System/Posix/Terminal/Common.hsc" #-}
  return $ makeTerminalAttributes fp

-- Set termios l_flag

setLocalFlag :: CTcflag -> TerminalAttributes -> TerminalAttributes
setLocalFlag flag termios = unsafePerformIO $ do
  fp <- mallocForeignPtrBytes (60)
{-# LINE 1090 "System/Posix/Terminal/Common.hsc" #-}
  withForeignPtr fp $ \p1 -> do
    withTerminalAttributes termios $ \p2 -> do
      copyBytes p1 p2 (60)
{-# LINE 1093 "System/Posix/Terminal/Common.hsc" #-}
      lflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 12)) p2
{-# LINE 1094 "System/Posix/Terminal/Common.hsc" #-}
      ((\hsc_ptr -> pokeByteOff hsc_ptr 12)) p1 (lflag .|. flag)
{-# LINE 1095 "System/Posix/Terminal/Common.hsc" #-}
  return $ makeTerminalAttributes fp

-- Examine termios l_flag

testLocalFlag :: CTcflag -> TerminalAttributes -> Bool
testLocalFlag flag termios = unsafePerformIO $
  withTerminalAttributes termios $ \p ->  do
    lflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 12)) p
{-# LINE 1103 "System/Posix/Terminal/Common.hsc" #-}
    return $! ((lflag .&. flag) /= 0)

-- Clear termios o_flag

clearOutputFlag :: CTcflag -> TerminalAttributes -> TerminalAttributes
clearOutputFlag flag termios = unsafePerformIO $ do
  fp <- mallocForeignPtrBytes (60)
{-# LINE 1110 "System/Posix/Terminal/Common.hsc" #-}
  withForeignPtr fp $ \p1 -> do
    withTerminalAttributes termios $ \p2 -> do
      copyBytes p1 p2 (60)
{-# LINE 1113 "System/Posix/Terminal/Common.hsc" #-}
      oflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) p2
{-# LINE 1114 "System/Posix/Terminal/Common.hsc" #-}
      ((\hsc_ptr -> pokeByteOff hsc_ptr 4)) p1 (oflag .&. complement flag)
{-# LINE 1115 "System/Posix/Terminal/Common.hsc" #-}
  return $ makeTerminalAttributes fp

-- Set termios o_flag

setOutputFlag :: CTcflag -> TerminalAttributes -> TerminalAttributes
setOutputFlag flag termios = unsafePerformIO $ do
  fp <- mallocForeignPtrBytes (60)
{-# LINE 1122 "System/Posix/Terminal/Common.hsc" #-}
  withForeignPtr fp $ \p1 -> do
    withTerminalAttributes termios $ \p2 -> do
      copyBytes p1 p2 (60)
{-# LINE 1125 "System/Posix/Terminal/Common.hsc" #-}
      oflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) p2
{-# LINE 1126 "System/Posix/Terminal/Common.hsc" #-}
      ((\hsc_ptr -> pokeByteOff hsc_ptr 4)) p1 (oflag .|. flag)
{-# LINE 1127 "System/Posix/Terminal/Common.hsc" #-}
  return $ makeTerminalAttributes fp

-- Examine termios o_flag

testOutputFlag :: CTcflag -> TerminalAttributes -> Bool
testOutputFlag flag termios = unsafePerformIO $
  withTerminalAttributes termios $ \p -> do
    oflag <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) p
{-# LINE 1135 "System/Posix/Terminal/Common.hsc" #-}
    return $! ((oflag .&. flag) /= 0)

withNewTermios :: TerminalAttributes -> (Ptr CTermios -> IO a)
  -> IO TerminalAttributes
withNewTermios termios action = do
  fp1 <- mallocForeignPtrBytes (60)
{-# LINE 1141 "System/Posix/Terminal/Common.hsc" #-}
  withForeignPtr fp1 $ \p1 -> do
   withTerminalAttributes termios $ \p2 -> do
    copyBytes p1 p2 (60)
{-# LINE 1144 "System/Posix/Terminal/Common.hsc" #-}
    _ <- action p1
    return ()
  return $ makeTerminalAttributes fp1


{-# LINE 1149 "System/Posix/Terminal/Common.hsc" #-}