terminal utilities steen lumholt from termios import all cfmakeraw cfmakecbreak setraw setcbreak indices for termios list iflag 0 oflag 1 cflag 2 lflag 3 ispeed 4 ospeed 5 cc 6 def cfmakerawmode clear all posix 12017 input mode flags see chapter 11 general terminal interface of posix 12017 base definitions do not postprocess output disable parity generation and detection clear character size mask let character size be 8 bits clear all posix 12017 local mode flags posix 12017 11 1 7 noncanonical mode input processing case b min0 time0 a pending read shall block until min here 1 bytes are received or a signal is received make termios mode cbreak do not map cr to nl on input modeiflag icrnl do not echo characters disable canonical input modelflag echo icanon posix 12017 11 1 7 noncanonical mode input processing case b min0 time0 a pending read shall block until min here 1 bytes are received or a signal is received modecc listmodecc modeccvmin 1 modeccvtime 0 def setrawfd whentcsaflush put terminal into cbreak mode mode tcgetattrfd new listmode cfmakecbreaknew tcsetattrfd when new return mode steen lumholt indices for termios list make termios mode raw clear all posix 1 2017 input mode flags see chapter 11 general terminal interface of posix 1 2017 base definitions do not post process output disable parity generation and detection clear character size mask let character size be 8 bits clear all posix 1 2017 local mode flags posix 1 2017 11 1 7 non canonical mode input processing case b min 0 time 0 a pending read shall block until min here 1 bytes are received or a signal is received make termios mode cbreak do not map cr to nl on input do not echo characters disable canonical input posix 1 2017 11 1 7 non canonical mode input processing case b min 0 time 0 a pending read shall block until min here 1 bytes are received or a signal is received put terminal into raw mode put terminal into cbreak mode
from termios import *

__all__ = ["cfmakeraw", "cfmakecbreak", "setraw", "setcbreak"]


IFLAG = 0
OFLAG = 1
CFLAG = 2
LFLAG = 3
ISPEED = 4
OSPEED = 5
CC = 6

def cfmakeraw(mode):
    
    
    
    
    mode[IFLAG] &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP |
                     INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF)

    
    mode[OFLAG] &= ~OPOST

    
    
    mode[CFLAG] &= ~(PARENB | CSIZE)
    mode[CFLAG] |= CS8

    
    mode[LFLAG] &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON |
                     IEXTEN | ISIG | NOFLSH | TOSTOP)

    
    
    
    
    mode[CC] = list(mode[CC])
    mode[CC][VMIN] = 1
    mode[CC][VTIME] = 0

def cfmakecbreak(mode):
    
    
    mode[IFLAG] &= ~(ICRNL)

    
    mode[LFLAG] &= ~(ECHO | ICANON)

    
    
    
    
    mode[CC] = list(mode[CC])
    mode[CC][VMIN] = 1
    mode[CC][VTIME] = 0

def setraw(fd, when=TCSAFLUSH):
    
    mode = tcgetattr(fd)
    new = list(mode)
    cfmakeraw(new)
    tcsetattr(fd, when, new)
    return mode

def setcbreak(fd, when=TCSAFLUSH):
    
    mode = tcgetattr(fd)
    new = list(mode)
    cfmakecbreak(new)
    tcsetattr(fd, when, new)
    return mode