(root)/
Python-3.12.0/
Lib/
tty.py
       1  """Terminal utilities."""
       2  
       3  # Author: Steen Lumholt.
       4  
       5  from termios import *
       6  
       7  __all__ = ["cfmakeraw", "cfmakecbreak", "setraw", "setcbreak"]
       8  
       9  # Indices for termios list.
      10  IFLAG = 0
      11  OFLAG = 1
      12  CFLAG = 2
      13  LFLAG = 3
      14  ISPEED = 4
      15  OSPEED = 5
      16  CC = 6
      17  
      18  def cfmakeraw(mode):
      19      """Make termios mode raw."""
      20      # Clear all POSIX.1-2017 input mode flags.
      21      # See chapter 11 "General Terminal Interface"
      22      # of POSIX.1-2017 Base Definitions.
      23      mode[IFLAG] &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP |
      24                       INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF)
      25  
      26      # Do not post-process output.
      27      mode[OFLAG] &= ~OPOST
      28  
      29      # Disable parity generation and detection; clear character size mask;
      30      # let character size be 8 bits.
      31      mode[CFLAG] &= ~(PARENB | CSIZE)
      32      mode[CFLAG] |= CS8
      33  
      34      # Clear all POSIX.1-2017 local mode flags.
      35      mode[LFLAG] &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON |
      36                       IEXTEN | ISIG | NOFLSH | TOSTOP)
      37  
      38      # POSIX.1-2017, 11.1.7 Non-Canonical Mode Input Processing,
      39      # Case B: MIN>0, TIME=0
      40      # A pending read shall block until MIN (here 1) bytes are received,
      41      # or a signal is received.
      42      mode[CC][VMIN] = 1
      43      mode[CC][VTIME] = 0
      44  
      45  def cfmakecbreak(mode):
      46      """Make termios mode cbreak."""
      47      # Do not map CR to NL on input.
      48      mode[IFLAG] &= ~(ICRNL)
      49  
      50      # Do not echo characters; disable canonical input.
      51      mode[LFLAG] &= ~(ECHO | ICANON)
      52  
      53      # POSIX.1-2017, 11.1.7 Non-Canonical Mode Input Processing,
      54      # Case B: MIN>0, TIME=0
      55      # A pending read shall block until MIN (here 1) bytes are received,
      56      # or a signal is received.
      57      mode[CC][VMIN] = 1
      58      mode[CC][VTIME] = 0
      59  
      60  def setraw(fd, when=TCSAFLUSH):
      61      """Put terminal into raw mode."""
      62      mode = tcgetattr(fd)
      63      new = list(mode)
      64      cfmakeraw(new)
      65      tcsetattr(fd, when, new)
      66      return mode
      67  
      68  def setcbreak(fd, when=TCSAFLUSH):
      69      """Put terminal into cbreak mode."""
      70      mode = tcgetattr(fd)
      71      new = list(mode)
      72      cfmakecbreak(new)
      73      tcsetattr(fd, when, new)
      74      return mode