| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`termios` --- POSIX style tty control | 
 | 2 | ========================================== | 
 | 3 |  | 
 | 4 | .. module:: termios | 
 | 5 |    :platform: Unix | 
 | 6 |    :synopsis: POSIX style tty control. | 
 | 7 |  | 
 | 8 |  | 
 | 9 | .. index:: | 
 | 10 |    pair: POSIX; I/O control | 
 | 11 |    pair: tty; I/O control | 
 | 12 |  | 
 | 13 | This module provides an interface to the POSIX calls for tty I/O control.  For a | 
 | 14 | complete description of these calls, see the POSIX or Unix manual pages.  It is | 
 | 15 | only available for those Unix versions that support POSIX *termios* style tty | 
 | 16 | I/O control (and then only if configured at installation time). | 
 | 17 |  | 
 | 18 | All functions in this module take a file descriptor *fd* as their first | 
 | 19 | argument.  This can be an integer file descriptor, such as returned by | 
| Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 20 | ``sys.stdin.fileno()``, or a :term:`file object`, such as ``sys.stdin`` itself. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 |  | 
 | 22 | This module also defines all the constants needed to work with the functions | 
 | 23 | provided here; these have the same name as their counterparts in C.  Please | 
 | 24 | refer to your system documentation for more information on using these terminal | 
 | 25 | control interfaces. | 
 | 26 |  | 
 | 27 | The module defines the following functions: | 
 | 28 |  | 
 | 29 |  | 
 | 30 | .. function:: tcgetattr(fd) | 
 | 31 |  | 
 | 32 |    Return a list containing the tty attributes for file descriptor *fd*, as | 
 | 33 |    follows: ``[iflag, oflag, cflag, lflag, ispeed, ospeed, cc]`` where *cc* is a | 
 | 34 |    list of the tty special characters (each a string of length 1, except the | 
 | 35 |    items with indices :const:`VMIN` and :const:`VTIME`, which are integers when | 
 | 36 |    these fields are defined).  The interpretation of the flags and the speeds as | 
 | 37 |    well as the indexing in the *cc* array must be done using the symbolic | 
 | 38 |    constants defined in the :mod:`termios` module. | 
 | 39 |  | 
 | 40 |  | 
 | 41 | .. function:: tcsetattr(fd, when, attributes) | 
 | 42 |  | 
 | 43 |    Set the tty attributes for file descriptor *fd* from the *attributes*, which is | 
 | 44 |    a list like the one returned by :func:`tcgetattr`.  The *when* argument | 
 | 45 |    determines when the attributes are changed: :const:`TCSANOW` to change | 
 | 46 |    immediately, :const:`TCSADRAIN` to change after transmitting all queued output, | 
 | 47 |    or :const:`TCSAFLUSH` to change after transmitting all queued output and | 
 | 48 |    discarding all queued input. | 
 | 49 |  | 
 | 50 |  | 
 | 51 | .. function:: tcsendbreak(fd, duration) | 
 | 52 |  | 
 | 53 |    Send a break on file descriptor *fd*.  A zero *duration* sends a break for 0.25 | 
 | 54 |    --0.5 seconds; a nonzero *duration* has a system dependent meaning. | 
 | 55 |  | 
 | 56 |  | 
 | 57 | .. function:: tcdrain(fd) | 
 | 58 |  | 
 | 59 |    Wait until all output written to file descriptor *fd* has been transmitted. | 
 | 60 |  | 
 | 61 |  | 
 | 62 | .. function:: tcflush(fd, queue) | 
 | 63 |  | 
 | 64 |    Discard queued data on file descriptor *fd*.  The *queue* selector specifies | 
 | 65 |    which queue: :const:`TCIFLUSH` for the input queue, :const:`TCOFLUSH` for the | 
 | 66 |    output queue, or :const:`TCIOFLUSH` for both queues. | 
 | 67 |  | 
 | 68 |  | 
 | 69 | .. function:: tcflow(fd, action) | 
 | 70 |  | 
 | 71 |    Suspend or resume input or output on file descriptor *fd*.  The *action* | 
 | 72 |    argument can be :const:`TCOOFF` to suspend output, :const:`TCOON` to restart | 
 | 73 |    output, :const:`TCIOFF` to suspend input, or :const:`TCION` to restart input. | 
 | 74 |  | 
 | 75 |  | 
 | 76 | .. seealso:: | 
 | 77 |  | 
 | 78 |    Module :mod:`tty` | 
 | 79 |       Convenience functions for common terminal control operations. | 
 | 80 |  | 
 | 81 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 82 | .. _termios-example: | 
 | 83 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | Example | 
 | 85 | ------- | 
 | 86 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | Here's a function that prompts for a password with echoing turned off.  Note the | 
 | 88 | technique using a separate :func:`tcgetattr` call and a :keyword:`try` ... | 
 | 89 | :keyword:`finally` statement to ensure that the old tty attributes are restored | 
 | 90 | exactly no matter what happens:: | 
 | 91 |  | 
| Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 92 |    def getpass(prompt="Password: "): | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 |        import termios, sys | 
 | 94 |        fd = sys.stdin.fileno() | 
 | 95 |        old = termios.tcgetattr(fd) | 
 | 96 |        new = termios.tcgetattr(fd) | 
 | 97 |        new[3] = new[3] & ~termios.ECHO          # lflags | 
 | 98 |        try: | 
 | 99 |            termios.tcsetattr(fd, termios.TCSADRAIN, new) | 
| Georg Brandl | 8d5c392 | 2007-12-02 22:48:17 +0000 | [diff] [blame] | 100 |            passwd = input(prompt) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 101 |        finally: | 
 | 102 |            termios.tcsetattr(fd, termios.TCSADRAIN, old) | 
 | 103 |        return passwd | 
 | 104 |  |