summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib/libposix/posix007.hs
blob: 2e628205114fbf5a035f80fe08999775609989ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import Posix

-- This test is an example of where something more sophisticated than runstdtest
-- is required, as its interactive.

main = do
    installHandler keyboardSignal (Catch doCtrlC) Nothing
    ta  <- getTerminalAttributes stdInput
    case (controlChar ta Interrupt) of
      Nothing -> fixMe ta
      Just x  -> continue x

fixMe ta = do
    putStr "Oops...no interrupt character?\nI can fix that...\n"
    setTerminalAttributes stdInput (withCC ta (Interrupt, '\ETX')) Immediately
    ta   <- getTerminalAttributes stdInput
    case (controlChar ta Interrupt) of
      Nothing -> putStr "...Then again, maybe I can't\n"
      Just x -> continue x

continue x =
    putStr "Press '"
    putStr (ccStr x)
    putStr "'.\n"
    awaitSignal Nothing
    putStr "How did I get here?\n"

doCtrlC =
    putStr "Caught an interrupt.\n"

ccStr '\DEL' = "^?"
ccStr x 
  | x >= ' ' = [x]
  | otherwise = ['^', (toEnum (fromEnum x + fromEnum '@'))]