3a37dc7545df078d85fd88ea1650441a991deb4e
[ghc-hetmet.git] / ghc / misc / examples / posix / po007 / Main.hs
1 import Posix
2
3 main = 
4     installHandler keyboardSignal (Catch doCtrlC) Nothing >>
5     getTerminalAttributes stdInput >>= \ ta ->
6     case (controlChar ta Interrupt) of
7       Nothing -> fixMe ta
8       Just x -> continue x
9
10 fixMe ta =
11     putStr "Oops...no interrupt character?\nI can fix that...\n" >>
12     setTerminalAttributes stdInput (withCC ta (Interrupt, '\ETX')) Immediately >>
13     getTerminalAttributes stdInput >>= \ ta ->
14     case (controlChar ta Interrupt) of
15       Nothing -> putStr "...Then again, maybe I can't\n"
16       Just x -> continue x
17
18 continue x =
19     putStr "Press '" >>
20     putStr (ccStr x) >>
21     putStr "'.\n" >>
22     awaitSignal Nothing >>
23     putStr "How did I get here?\n"
24
25 doCtrlC =
26     putStr "Caught an interrupt.\n"
27
28 ccStr '\DEL' = "^?"
29 ccStr x 
30   | x >= ' ' = [x]
31   | otherwise = ['^', (toEnum (fromEnum x + fromEnum '@'))]