Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Terminal utilities.""" |
| 2 | |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 3 | # Author: Steen Lumholt. |
| 4 | |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 5 | from termios import * |
| 6 | |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 7 | __all__ = ["setraw", "setcbreak"] |
| 8 | |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 9 | # Indexes for termios list. |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 10 | IFLAG = 0 |
| 11 | OFLAG = 1 |
| 12 | CFLAG = 2 |
| 13 | LFLAG = 3 |
| 14 | ISPEED = 4 |
| 15 | OSPEED = 5 |
| 16 | CC = 6 |
| 17 | |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 18 | def setraw(fd, when=TCSAFLUSH): |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 19 | """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 Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 30 | def setcbreak(fd, when=TCSAFLUSH): |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 31 | """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) |