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