2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
4 \section[LibPosixTTY]{Haskell 1.3 POSIX Device-Specific Functions}
12 TerminalAttributes(..),
20 getTerminalAttributes,
21 getTerminalProcessGroupID,
27 setTerminalAttributes,
28 setTerminalProcessGroupID,
45 type TerminalAttributes = _ByteArray ()
47 data TerminalMode = InterruptOnBreak
73 | BackgroundWriteInterrupt
75 withoutMode :: TerminalAttributes -> TerminalMode -> TerminalAttributes
76 withoutMode termios InterruptOnBreak = clearInputFlag ``BRKINT'' termios
77 withoutMode termios MapCRtoLF = clearInputFlag ``ICRNL'' termios
78 withoutMode termios IgnoreBreak = clearInputFlag ``IGNBRK'' termios
79 withoutMode termios IgnoreCR = clearInputFlag ``IGNCR'' termios
80 withoutMode termios IgnoreParityErrors = clearInputFlag ``IGNPAR'' termios
81 withoutMode termios MapLFtoCR = clearInputFlag ``INLCR'' termios
82 withoutMode termios CheckParity = clearInputFlag ``INPCK'' termios
83 withoutMode termios StripHighBit = clearInputFlag ``ISTRIP'' termios
84 withoutMode termios StartStopInput = clearInputFlag ``IXOFF'' termios
85 withoutMode termios StartStopOutput = clearInputFlag ``IXON'' termios
86 withoutMode termios MarkParityErrors = clearInputFlag ``PARMRK'' termios
87 withoutMode termios ProcessOutput = unsafePerformPrimIO (
88 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
89 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
90 ((struct termios *)%0)->c_oflag &= ~OPOST;'' bytes termios
92 freeze bytes `thenStrictlyST` \ termios ->
94 withoutMode termios LocalMode = clearControlFlag ``CLOCAL'' termios
95 withoutMode termios ReadEnable = clearControlFlag ``CREAD'' termios
96 withoutMode termios TwoStopBits = clearControlFlag ``CSTOPB'' termios
97 withoutMode termios HangupOnClose = clearControlFlag ``HUPCL'' termios
98 withoutMode termios EnableParity = clearControlFlag ``PARENB'' termios
99 withoutMode termios OddParity = clearControlFlag ``PARODD'' termios
100 withoutMode termios EnableEcho = clearLocalFlag ``ECHO'' termios
101 withoutMode termios EchoErase = clearLocalFlag ``ECHOE'' termios
102 withoutMode termios EchoKill = clearLocalFlag ``ECHOK'' termios
103 withoutMode termios EchoLF = clearLocalFlag ``ECHONL'' termios
104 withoutMode termios ProcessInput = clearLocalFlag ``ICANON'' termios
105 withoutMode termios ExtendedFunctions = clearLocalFlag ``IEXTEN'' termios
106 withoutMode termios KeyboardInterrupts = clearLocalFlag ``ISIG'' termios
107 withoutMode termios NoFlushOnInterrupt = setLocalFlag ``NOFLSH'' termios
108 withoutMode termios BackgroundWriteInterrupt = clearLocalFlag ``TOSTOP'' termios
110 withMode :: TerminalAttributes -> TerminalMode -> TerminalAttributes
111 withMode termios InterruptOnBreak = setInputFlag ``BRKINT'' termios
112 withMode termios MapCRtoLF = setInputFlag ``ICRNL'' termios
113 withMode termios IgnoreBreak = setInputFlag ``IGNBRK'' termios
114 withMode termios IgnoreCR = setInputFlag ``IGNCR'' termios
115 withMode termios IgnoreParityErrors = setInputFlag ``IGNPAR'' termios
116 withMode termios MapLFtoCR = setInputFlag ``INLCR'' termios
117 withMode termios CheckParity = setInputFlag ``INPCK'' termios
118 withMode termios StripHighBit = setInputFlag ``ISTRIP'' termios
119 withMode termios StartStopInput = setInputFlag ``IXOFF'' termios
120 withMode termios StartStopOutput = setInputFlag ``IXON'' termios
121 withMode termios MarkParityErrors = setInputFlag ``PARMRK'' termios
122 withMode termios ProcessOutput = unsafePerformPrimIO (
123 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
124 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
125 ((struct termios *)%0)->c_oflag |= OPOST;'' bytes termios
127 freeze bytes `thenStrictlyST` \ termios ->
128 returnPrimIO termios)
129 withMode termios LocalMode = setControlFlag ``CLOCAL'' termios
130 withMode termios ReadEnable = setControlFlag ``CREAD'' termios
131 withMode termios TwoStopBits = setControlFlag ``CSTOPB'' termios
132 withMode termios HangupOnClose = setControlFlag ``HUPCL'' termios
133 withMode termios EnableParity = setControlFlag ``PARENB'' termios
134 withMode termios OddParity = setControlFlag ``PARODD'' termios
135 withMode termios EnableEcho = setLocalFlag ``ECHO'' termios
136 withMode termios EchoErase = setLocalFlag ``ECHOE'' termios
137 withMode termios EchoKill = setLocalFlag ``ECHOK'' termios
138 withMode termios EchoLF = setLocalFlag ``ECHONL'' termios
139 withMode termios ProcessInput = setLocalFlag ``ICANON'' termios
140 withMode termios ExtendedFunctions = setLocalFlag ``IEXTEN'' termios
141 withMode termios KeyboardInterrupts = setLocalFlag ``ISIG'' termios
142 withMode termios NoFlushOnInterrupt = clearLocalFlag ``NOFLSH'' termios
143 withMode termios BackgroundWriteInterrupt = setLocalFlag ``TOSTOP'' termios
145 terminalMode :: TerminalMode -> TerminalAttributes -> Bool
146 terminalMode InterruptOnBreak = testInputFlag ``BRKINT''
147 terminalMode MapCRtoLF = testInputFlag ``ICRNL''
148 terminalMode IgnoreBreak = testInputFlag ``IGNBRK''
149 terminalMode IgnoreCR = testInputFlag ``IGNCR''
150 terminalMode IgnoreParityErrors = testInputFlag ``IGNPAR''
151 terminalMode MapLFtoCR = testInputFlag ``INLCR''
152 terminalMode CheckParity = testInputFlag ``INPCK''
153 terminalMode StripHighBit = testInputFlag ``ISTRIP''
154 terminalMode StartStopInput = testInputFlag ``IXOFF''
155 terminalMode StartStopOutput = testInputFlag ``IXON''
156 terminalMode MarkParityErrors = testInputFlag ``PARMRK''
157 terminalMode ProcessOutput = \ termios -> unsafePerformPrimIO (
158 _casm_ ``%r = ((struct termios *)%0)->c_oflag & OPOST;'' termios
159 `thenPrimIO` \ (W# flags#) ->
160 returnPrimIO (flags# `neWord#` int2Word# 0#))
161 terminalMode LocalMode = testControlFlag ``CLOCAL''
162 terminalMode ReadEnable = testControlFlag ``CREAD''
163 terminalMode TwoStopBits = testControlFlag ``CSTOPB''
164 terminalMode HangupOnClose = testControlFlag ``HUPCL''
165 terminalMode EnableParity = testControlFlag ``PARENB''
166 terminalMode OddParity = testControlFlag ``PARODD''
167 terminalMode EnableEcho = testLocalFlag ``ECHO''
168 terminalMode EchoErase = testLocalFlag ``ECHOE''
169 terminalMode EchoKill = testLocalFlag ``ECHOK''
170 terminalMode EchoLF = testLocalFlag ``ECHONL''
171 terminalMode ProcessInput = testLocalFlag ``ICANON''
172 terminalMode ExtendedFunctions = testLocalFlag ``IEXTEN''
173 terminalMode KeyboardInterrupts = testLocalFlag ``ISIG''
174 terminalMode NoFlushOnInterrupt = not . testLocalFlag ``NOFLSH''
175 terminalMode BackgroundWriteInterrupt = testLocalFlag ``TOSTOP''
177 bitsPerByte :: TerminalAttributes -> Int
178 bitsPerByte termios = unsafePerformPrimIO (
179 _casm_ ``%r = ((struct termios *)%0)->c_cflag & CSIZE;'' termios
181 returnPrimIO (word2Bits w))
183 word2Bits :: _Word -> Int
185 if x == ``CS5'' then 5
186 else if x == ``CS6'' then 6
187 else if x == ``CS7'' then 7
188 else if x == ``CS8'' then 8
191 withBits :: TerminalAttributes -> Int -> TerminalAttributes
192 withBits termios bits = unsafePerformPrimIO (
193 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
194 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
195 ((struct termios *)%0)->c_cflag =
196 (((struct termios *)%1)->c_cflag & ~CSIZE) | %2;''
197 bytes termios (mask bits) `thenPrimIO` \ () ->
198 freeze bytes `thenStrictlyST` \ termios ->
199 returnPrimIO termios)
206 mask _ = error "withBits bit value out of range [5..8]"
208 data ControlCharacter = EndOfFile
218 controlChar :: TerminalAttributes -> ControlCharacter -> Maybe Char
219 controlChar termios cc = unsafePerformPrimIO (
220 _casm_ ``%r = ((struct termios *)%0)->c_cc[%1];'' termios (cc2Word cc)
221 `thenPrimIO` \ val ->
222 if val == ``_POSIX_VDISABLE'' then
225 returnPrimIO (Just (chr val)))
227 withCC :: TerminalAttributes
228 -> (ControlCharacter, Char)
229 -> TerminalAttributes
230 withCC termios (cc, c) = unsafePerformPrimIO (
231 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
232 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
233 ((struct termios *)%0)->c_cc[%2] = %3;''
234 bytes termios (cc2Word cc) c `thenPrimIO` \ () ->
235 freeze bytes `thenStrictlyST` \ termios ->
236 returnPrimIO termios)
238 withoutCC :: TerminalAttributes
240 -> TerminalAttributes
241 withoutCC termios cc = unsafePerformPrimIO (
242 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
243 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
244 ((struct termios *)%0)->c_cc[%2] = _POSIX_VDISABLE;''
245 bytes termios (cc2Word cc) `thenPrimIO` \ () ->
246 freeze bytes `thenStrictlyST` \ termios ->
247 returnPrimIO termios)
249 inputTime :: TerminalAttributes -> Int
250 inputTime termios = unsafePerformPrimIO (
251 _casm_ ``%r = ((struct termios *)%0)->c_cc[VTIME];'' termios
252 `thenPrimIO` \ count ->
255 withTime :: TerminalAttributes -> Int -> TerminalAttributes
256 withTime termios time = unsafePerformPrimIO (
257 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
258 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
259 ((struct termios *)%0)->c_cc[VTIME] = %2;'' bytes termios time
261 freeze bytes `thenStrictlyST` \ termios ->
262 returnPrimIO termios)
264 minInput :: TerminalAttributes -> Int
265 minInput termios = unsafePerformPrimIO (
266 _casm_ ``%r = ((struct termios *)%0)->c_cc[VMIN];'' termios
267 `thenPrimIO` \ count ->
270 withMinInput :: TerminalAttributes -> Int -> TerminalAttributes
271 withMinInput termios count = unsafePerformPrimIO (
272 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
273 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
274 ((struct termios *)%0)->c_cc[VMIN] = %2;'' bytes termios count
276 freeze bytes `thenStrictlyST` \ termios ->
277 returnPrimIO termios)
296 inputSpeed :: TerminalAttributes -> BaudRate
297 inputSpeed termios = unsafePerformPrimIO (
298 _casm_ ``%r = cfgetispeed((struct termios *)%0);'' termios
300 returnPrimIO (word2Baud w))
302 withInputSpeed :: TerminalAttributes -> BaudRate -> TerminalAttributes
303 withInputSpeed termios br = unsafePerformPrimIO (
304 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
305 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
306 cfsetispeed((struct termios *)%0, %2);'' bytes termios (baud2Word br)
308 freeze bytes `thenStrictlyST` \ termios ->
309 returnPrimIO termios)
311 outputSpeed :: TerminalAttributes -> BaudRate
312 outputSpeed termios = unsafePerformPrimIO (
313 _casm_ ``%r = cfgetospeed((struct termios *)%0);'' termios
315 returnPrimIO (word2Baud w))
317 withOutputSpeed :: TerminalAttributes -> BaudRate -> TerminalAttributes
318 withOutputSpeed termios br = unsafePerformPrimIO (
319 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
320 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
321 cfsetospeed((struct termios *)%0, %2);'' bytes termios (baud2Word br)
323 freeze bytes `thenStrictlyST` \ termios ->
324 returnPrimIO termios)
326 getTerminalAttributes :: Channel -> IO TerminalAttributes
327 getTerminalAttributes fd =
328 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
329 _casm_ ``%r = tcgetattr(%0,(struct termios *)%1);'' fd bytes
332 freeze bytes `thenStrictlyST` \ termios ->
335 syserr "getTerminalAttributes"
337 data TerminalState = Immediately
341 setTerminalAttributes :: Channel
342 -> TerminalAttributes
345 setTerminalAttributes fd termios state =
346 _casm_ ``%r = tcsetattr(%0,%1,(struct termios *)%2);'' fd (state2Int state) termios
351 syserr "setTerminalAttributes"
353 state2Int :: TerminalState -> Int
354 state2Int Immediately = ``TCSANOW''
355 state2Int WhenDrained = ``TCSADRAIN''
356 state2Int WhenFlushed = ``TCSAFLUSH''
358 sendBreak :: Channel -> Int -> IO ()
359 sendBreak fd duration =
360 _ccall_ tcsendbreak fd duration `thenPrimIO` \ rc ->
366 drainOutput :: Channel -> IO ()
368 _ccall_ tcdrain fd `thenPrimIO` \ rc ->
374 data QueueSelector = InputQueue
378 discardData :: Channel -> QueueSelector -> IO ()
379 discardData fd queue =
380 _ccall_ tcflush fd (queue2Int queue) `thenPrimIO` \ rc ->
386 queue2Int :: QueueSelector -> Int
387 queue2Int InputQueue = ``TCIFLUSH''
388 queue2Int OutputQueue = ``TCOFLUSH''
389 queue2Int BothQueues = ``TCIOFLUSH''
391 data FlowAction = SuspendOutput
396 controlFlow :: Channel -> FlowAction -> IO ()
397 controlFlow fd action =
398 _ccall_ tcflow fd (action2Int action) `thenPrimIO` \ rc ->
404 action2Int :: FlowAction -> Int
405 action2Int SuspendOutput = ``TCOOFF''
406 action2Int RestartOutput = ``TCOON''
407 action2Int TransmitStop = ``TCIOFF''
408 action2Int TransmitStart = ``TCION''
410 getTerminalProcessGroupID :: Channel -> IO ProcessGroupID
411 getTerminalProcessGroupID fd =
412 _ccall_ tcgetpgrp fd `thenPrimIO` \ pgid ->
416 syserr "getTerminalProcessGroupID"
418 setTerminalProcessGroupID :: Channel -> ProcessGroupID -> IO ()
419 setTerminalProcessGroupID fd pgid =
420 _ccall_ tcsetpgrp fd pgid `thenPrimIO` \ rc ->
424 syserr "setTerminalProcessGroupID"
428 Local utility functions
432 -- Convert Haskell ControlCharacter to Int
434 cc2Word :: ControlCharacter -> _Word
435 cc2Word EndOfFile = ``VEOF''
436 cc2Word EndOfLine = ``VEOL''
437 cc2Word Erase = ``VERASE''
438 cc2Word Interrupt = ``VINTR''
439 cc2Word Kill = ``VKILL''
440 cc2Word Quit = ``VQUIT''
441 cc2Word Suspend = ``VSUSP''
442 cc2Word Start = ``VSTART''
443 cc2Word Stop = ``VSTOP''
445 -- Convert Haskell BaudRate to unsigned integral type (_Word)
447 baud2Word :: BaudRate -> _Word
448 baud2Word B0 = ``B0''
449 baud2Word B50 = ``B50''
450 baud2Word B75 = ``B75''
451 baud2Word B110 = ``B110''
452 baud2Word B134 = ``B134''
453 baud2Word B150 = ``B150''
454 baud2Word B200 = ``B200''
455 baud2Word B300 = ``B300''
456 baud2Word B600 = ``B600''
457 baud2Word B1200 = ``B1200''
458 baud2Word B1800 = ``B1800''
459 baud2Word B2400 = ``B2400''
460 baud2Word B4800 = ``B4800''
461 baud2Word B9600 = ``B9600''
462 baud2Word B19200 = ``B19200''
463 baud2Word B38400 = ``B38400''
465 -- And convert a word back to a baud rate
466 -- We really need some cpp macros here.
468 word2Baud :: _Word -> BaudRate
470 if x == ``B0'' then B0
471 else if x == ``B50'' then B50
472 else if x == ``B75'' then B75
473 else if x == ``B110'' then B110
474 else if x == ``B134'' then B134
475 else if x == ``B150'' then B150
476 else if x == ``B200'' then B200
477 else if x == ``B300'' then B300
478 else if x == ``B600'' then B600
479 else if x == ``B1200'' then B1200
480 else if x == ``B1800'' then B1800
481 else if x == ``B2400'' then B2400
482 else if x == ``B4800'' then B4800
483 else if x == ``B9600'' then B9600
484 else if x == ``B19200'' then B19200
485 else if x == ``B38400'' then B38400
486 else error "unknown baud rate"
488 -- Clear termios i_flag
490 clearInputFlag :: _Word -> TerminalAttributes -> TerminalAttributes
491 clearInputFlag flag termios = unsafePerformPrimIO (
492 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
493 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
494 ((struct termios *)%0)->c_iflag &= ~%2;'' bytes termios flag
496 freeze bytes `thenStrictlyST` \ termios ->
497 returnPrimIO termios)
499 -- Set termios i_flag
501 setInputFlag :: _Word -> TerminalAttributes -> TerminalAttributes
502 setInputFlag flag termios = unsafePerformPrimIO (
503 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
504 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
505 ((struct termios *)%0)->c_iflag |= %2;'' bytes termios flag
507 freeze bytes `thenStrictlyST` \ termios ->
508 returnPrimIO termios)
510 -- Examine termios i_flag
512 testInputFlag :: _Word -> TerminalAttributes -> Bool
513 testInputFlag flag termios = unsafePerformPrimIO (
514 _casm_ ``%r = ((struct termios *)%0)->c_iflag & %1;'' termios flag
515 `thenPrimIO` \ (W# flags#) ->
516 returnPrimIO (flags# `neWord#` int2Word# 0#))
518 -- Clear termios c_flag
520 clearControlFlag :: _Word -> TerminalAttributes -> TerminalAttributes
521 clearControlFlag flag termios = unsafePerformPrimIO (
522 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
523 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
524 ((struct termios *)%0)->c_cflag &= ~%2;'' bytes termios flag
526 freeze bytes `thenStrictlyST` \ termios ->
527 returnPrimIO termios)
529 -- Set termios c_flag
531 setControlFlag :: _Word -> TerminalAttributes -> TerminalAttributes
532 setControlFlag flag termios = unsafePerformPrimIO (
533 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
534 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
535 ((struct termios *)%0)->c_cflag |= %2;'' bytes termios flag
537 freeze bytes `thenStrictlyST` \ termios ->
538 returnPrimIO termios)
540 -- Examine termios c_flag
542 testControlFlag :: _Word -> TerminalAttributes -> Bool
543 testControlFlag flag termios = unsafePerformPrimIO (
544 _casm_ ``%r = ((struct termios *)%0)->c_cflag & %1;'' termios flag
545 `thenPrimIO` \ (W# flags#) ->
546 returnPrimIO (flags# `neWord#` int2Word# 0#))
548 -- Clear termios l_flag
550 clearLocalFlag :: _Word -> TerminalAttributes -> TerminalAttributes
551 clearLocalFlag flag termios = unsafePerformPrimIO (
552 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
553 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
554 ((struct termios *)%0)->c_lflag &= ~%2;'' bytes termios flag
556 freeze bytes `thenStrictlyST` \ termios ->
557 returnPrimIO termios)
559 -- Set termios l_flag
561 setLocalFlag :: _Word -> TerminalAttributes -> TerminalAttributes
562 setLocalFlag flag termios = unsafePerformPrimIO (
563 allocChars ``sizeof(struct termios)'' `thenStrictlyST` \ bytes ->
564 _casm_ ``*(struct termios *)%0 = *(struct termios *)%1;
565 ((struct termios *)%0)->c_lflag |= %2;'' bytes termios flag
567 freeze bytes `thenStrictlyST` \ termios ->
568 returnPrimIO termios)
570 -- Examine termios l_flag
572 testLocalFlag :: _Word -> TerminalAttributes -> Bool
573 testLocalFlag flag termios = unsafePerformPrimIO (
574 _casm_ ``%r = ((struct termios *)%0)->c_iflag & %1;'' termios flag
575 `thenPrimIO` \ (W# flags#) ->
576 returnPrimIO (flags# `neWord#` int2Word# 0#))