blob: a72eb6755450bb07d9bb77fb9cdc3cf2f0148534 [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Terminal utilities."""
2
Guido van Rossum23cb2a81994-09-12 10:36:35 +00003# Author: Steen Lumholt.
4
Guido van Rossum23cb2a81994-09-12 10:36:35 +00005from termios import *
6
Skip Montanaro40fc1602001-03-01 04:27:19 +00007__all__ = ["setraw", "setcbreak"]
8
Tim Petersb90f89a2001-01-15 03:26:36 +00009# Indexes for termios list.
Guido van Rossum23cb2a81994-09-12 10:36:35 +000010IFLAG = 0
11OFLAG = 1
12CFLAG = 2
13LFLAG = 3
14ISPEED = 4
15OSPEED = 5
16CC = 6
17
Guido van Rossum23cb2a81994-09-12 10:36:35 +000018def setraw(fd, when=TCSAFLUSH):
Tim Petersb90f89a2001-01-15 03:26:36 +000019 """Put terminal into a raw mode."""
20 mode = tcgetattr(fd)
21 mode[IFLAG] = mode[IFLAG] & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON)
22 mode[OFLAG] = mode[OFLAG] & ~(OPOST)
23 mode[CFLAG] = mode[CFLAG] & ~(CSIZE | PARENB)
24 mode[CFLAG] = mode[CFLAG] | CS8
25 mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON | IEXTEN | ISIG)
26 mode[CC][VMIN] = 1
27 mode[CC][VTIME] = 0
28 tcsetattr(fd, when, mode)
Guido van Rossum23cb2a81994-09-12 10:36:35 +000029
Guido van Rossum23cb2a81994-09-12 10:36:35 +000030def setcbreak(fd, when=TCSAFLUSH):
Tim Petersb90f89a2001-01-15 03:26:36 +000031 """Put terminal into a cbreak mode."""
32 mode = tcgetattr(fd)
33 mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON)
34 mode[CC][VMIN] = 1
35 mode[CC][VTIME] = 0
36 tcsetattr(fd, when, mode)