Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{termios}} |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 2 | \bimodindex{termios} |
| 3 | \indexii{Posix}{I/O control} |
| 4 | \indexii{tty}{I/O control} |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 6 | \renewcommand{\indexsubitem}{(in module termios)} |
| 7 | |
| 8 | This module provides an interface to the Posix calls for tty I/O |
| 9 | control. For a complete description of these calls, see the Posix or |
| 10 | \UNIX{} manual pages. It is only available for those \UNIX{} versions |
| 11 | that support Posix \code{termios} style tty I/O control (and then |
| 12 | only if configured at installation time). |
| 13 | |
| 14 | All functions in this module take a file descriptor \var{fd} as their |
| 15 | first argument. This must be an integer file descriptor, such as |
| 16 | returned by \code{sys.stdin.fileno()}. |
| 17 | |
| 18 | This module should be used in conjunction with the \code{TERMIOS} |
| 19 | module, which defines the relevant symbolic constants (see the next |
| 20 | section). |
| 21 | |
| 22 | The module defines the following functions: |
| 23 | |
| 24 | \begin{funcdesc}{tcgetattr}{fd} |
| 25 | Return a list containing the tty attributes for file descriptor |
| 26 | \var{fd}, as follows: \code{[\var{iflag}, \var{oflag}, \var{cflag}, |
| 27 | \var{lflag}, \var{ispeed}, \var{ospeed}, \var{cc}]} where \var{cc} is |
| 28 | a list of the tty special characters (each a string of length 1, |
| 29 | except the items with indices \code{VMIN} and \code{VTIME}, which are |
| 30 | integers when these fields are defined). The interpretation of the |
| 31 | flags and the speeds as well as the indexing in the \var{cc} array |
| 32 | must be done using the symbolic constants defined in the |
| 33 | \code{TERMIOS} module. |
| 34 | \end{funcdesc} |
| 35 | |
| 36 | \begin{funcdesc}{tcsetattr}{fd\, when\, attributes} |
| 37 | Set the tty attributes for file descriptor \var{fd} from the |
| 38 | \var{attributes}, which is a list like the one returned by |
| 39 | \code{tcgetattr()}. The \var{when} argument determines when the |
| 40 | attributes are changed: \code{TERMIOS.TCSANOW} to change immediately, |
| 41 | \code{TERMIOS.TCSADRAIN} to change after transmitting all queued |
| 42 | output, or \code{TERMIOS.TCSAFLUSH} to change after transmitting all |
| 43 | queued output and discarding all queued input. |
| 44 | \end{funcdesc} |
| 45 | |
| 46 | \begin{funcdesc}{tcsendbreak}{fd\, duration} |
| 47 | Send a break on file descriptor \var{fd}. A zero \var{duration} sends |
| 48 | a break for 0.25--0.5 seconds; a nonzero \var{duration} has a system |
| 49 | dependent meaning. |
| 50 | \end{funcdesc} |
| 51 | |
| 52 | \begin{funcdesc}{tcdrain}{fd} |
| 53 | Wait until all output written to file descriptor \var{fd} has been |
| 54 | transmitted. |
| 55 | \end{funcdesc} |
| 56 | |
| 57 | \begin{funcdesc}{tcflush}{fd\, queue} |
| 58 | Discard queued data on file descriptor \var{fd}. The \var{queue} |
| 59 | selector specifies which queue: \code{TERMIOS.TCIFLUSH} for the input |
| 60 | queue, \code{TERMIOS.TCOFLUSH} for the output queue, or |
| 61 | \code{TERMIOS.TCIOFLUSH} for both queues. |
| 62 | \end{funcdesc} |
| 63 | |
| 64 | \begin{funcdesc}{tcflow}{fd\, action} |
| 65 | Suspend or resume input or output on file descriptor \var{fd}. The |
| 66 | \var{action} argument can be \code{TERMIOS.TCOOFF} to suspend output, |
| 67 | \code{TERMIOS.TCOON} to restart output, \code{TERMIOS.TCIOFF} to |
| 68 | suspend input, or \code{TERMIOS.TCION} to restart input. |
| 69 | \end{funcdesc} |
| 70 | |
| 71 | \subsection{Example} |
| 72 | \nodename{termios Example} |
| 73 | |
| 74 | Here's a function that prompts for a password with echoing turned off. |
| 75 | Note the technique using a separate \code{termios.tcgetattr()} call |
Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame^] | 76 | and a \code{try \ldots{} finally} statement to ensure that the old tty |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 77 | attributes are restored exactly no matter what happens: |
| 78 | |
| 79 | \begin{verbatim} |
| 80 | def getpass(prompt = "Password: "): |
| 81 | import termios, TERMIOS, sys |
| 82 | fd = sys.stdin.fileno() |
| 83 | old = termios.tcgetattr(fd) |
| 84 | new = termios.tcgetattr(fd) |
| 85 | new[3] = new[3] & ~TERMIOS.ECHO # lflags |
| 86 | try: |
| 87 | termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new) |
| 88 | passwd = raw_input(prompt) |
| 89 | finally: |
| 90 | termios.tcsetattr(fd, TERMIOS.TCSADRAIN, old) |
| 91 | return passwd |
| 92 | \end{verbatim} |
| 93 | |
| 94 | |
| 95 | \section{Standard Module \sectcode{TERMIOS}} |
| 96 | \stmodindex{TERMIOS} |
| 97 | \indexii{Posix}{I/O control} |
| 98 | \indexii{tty}{I/O control} |
| 99 | |
| 100 | \renewcommand{\indexsubitem}{(in module TERMIOS)} |
| 101 | |
| 102 | This module defines the symbolic constants required to use the |
| 103 | \code{termios} module (see the previous section). See the Posix or |
| 104 | \UNIX{} manual pages (or the source) for a list of those constants. |
| 105 | |
| 106 | Note: this module resides in a system-dependent subdirectory of the |
| 107 | Python library directory. You may have to generate it for your |
| 108 | particular system using the script \file{Tools/scripts/h2py.py}. |