Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{termios} --- |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 2 | \POSIX{} style tty control} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{termios} |
Fred Drake | a54a887 | 1999-03-02 17:03:42 +0000 | [diff] [blame] | 5 | \platform{Unix} |
Fred Drake | c116b82 | 2001-05-09 15:50:17 +0000 | [diff] [blame] | 6 | \modulesynopsis{\POSIX\ style tty control.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 8 | \indexii{\POSIX{}}{I/O control} |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 9 | \indexii{tty}{I/O control} |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 11 | |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 12 | This module provides an interface to the \POSIX{} calls for tty I/O |
| 13 | 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] | 14 | \UNIX{} manual pages. It is only available for those \UNIX{} versions |
Fred Drake | e42f595 | 1998-03-11 06:00:23 +0000 | [diff] [blame] | 15 | 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] | 16 | only if configured at installation time). |
| 17 | |
| 18 | All functions in this module take a file descriptor \var{fd} as their |
Fred Drake | a6140be | 2001-05-08 05:37:52 +0000 | [diff] [blame] | 19 | first argument. This can be an integer file descriptor, such as |
| 20 | returned by \code{sys.stdin.fileno()}, or a file object, such as |
| 21 | \code{sys.stdin} itself. |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 22 | |
Fred Drake | 969ab27 | 2001-02-27 22:01:15 +0000 | [diff] [blame] | 23 | This module also defines all the constants needed to work with the |
| 24 | functions provided here; these have the same name as their |
| 25 | counterparts in C. Please refer to your system documentation for more |
| 26 | information on using these terminal control interfaces. |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 27 | |
| 28 | The module defines the following functions: |
| 29 | |
| 30 | \begin{funcdesc}{tcgetattr}{fd} |
| 31 | Return a list containing the tty attributes for file descriptor |
Fred Drake | e42f595 | 1998-03-11 06:00:23 +0000 | [diff] [blame] | 32 | \var{fd}, as follows: \code{[}\var{iflag}, \var{oflag}, \var{cflag}, |
| 33 | \var{lflag}, \var{ispeed}, \var{ospeed}, \var{cc}\code{]} where |
| 34 | \var{cc} is a list of the tty special characters (each a string of |
Fred Drake | 969ab27 | 2001-02-27 22:01:15 +0000 | [diff] [blame] | 35 | length 1, except the items with indices \constant{VMIN} and |
| 36 | \constant{VTIME}, which are integers when these fields are |
Fred Drake | e42f595 | 1998-03-11 06:00:23 +0000 | [diff] [blame] | 37 | defined). The interpretation of the flags and the speeds as well as |
| 38 | the indexing in the \var{cc} array must be done using the symbolic |
Fred Drake | 969ab27 | 2001-02-27 22:01:15 +0000 | [diff] [blame] | 39 | constants defined in the \module{termios} |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 40 | module. |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 41 | \end{funcdesc} |
| 42 | |
Fred Drake | e42f595 | 1998-03-11 06:00:23 +0000 | [diff] [blame] | 43 | \begin{funcdesc}{tcsetattr}{fd, when, attributes} |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 44 | Set the tty attributes for file descriptor \var{fd} from the |
| 45 | \var{attributes}, which is a list like the one returned by |
Fred Drake | e42f595 | 1998-03-11 06:00:23 +0000 | [diff] [blame] | 46 | \function{tcgetattr()}. The \var{when} argument determines when the |
Fred Drake | 969ab27 | 2001-02-27 22:01:15 +0000 | [diff] [blame] | 47 | attributes are changed: \constant{TCSANOW} to change immediately, |
| 48 | \constant{TCSADRAIN} to change after transmitting all queued output, |
| 49 | or \constant{TCSAFLUSH} to change after transmitting all queued |
| 50 | output and discarding all queued input. |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 51 | \end{funcdesc} |
| 52 | |
Fred Drake | e42f595 | 1998-03-11 06:00:23 +0000 | [diff] [blame] | 53 | \begin{funcdesc}{tcsendbreak}{fd, duration} |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 54 | Send a break on file descriptor \var{fd}. A zero \var{duration} sends |
| 55 | a break for 0.25--0.5 seconds; a nonzero \var{duration} has a system |
| 56 | dependent meaning. |
| 57 | \end{funcdesc} |
| 58 | |
| 59 | \begin{funcdesc}{tcdrain}{fd} |
| 60 | Wait until all output written to file descriptor \var{fd} has been |
| 61 | transmitted. |
| 62 | \end{funcdesc} |
| 63 | |
Fred Drake | e42f595 | 1998-03-11 06:00:23 +0000 | [diff] [blame] | 64 | \begin{funcdesc}{tcflush}{fd, queue} |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 65 | Discard queued data on file descriptor \var{fd}. The \var{queue} |
Fred Drake | 969ab27 | 2001-02-27 22:01:15 +0000 | [diff] [blame] | 66 | selector specifies which queue: \constant{TCIFLUSH} for the input |
| 67 | queue, \constant{TCOFLUSH} for the output queue, or |
| 68 | \constant{TCIOFLUSH} for both queues. |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 69 | \end{funcdesc} |
| 70 | |
Fred Drake | e42f595 | 1998-03-11 06:00:23 +0000 | [diff] [blame] | 71 | \begin{funcdesc}{tcflow}{fd, action} |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 72 | Suspend or resume input or output on file descriptor \var{fd}. The |
Fred Drake | 969ab27 | 2001-02-27 22:01:15 +0000 | [diff] [blame] | 73 | \var{action} argument can be \constant{TCOOFF} to suspend output, |
| 74 | \constant{TCOON} to restart output, \constant{TCIOFF} to suspend |
| 75 | input, or \constant{TCION} to restart input. |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 76 | \end{funcdesc} |
| 77 | |
Fred Drake | 92f3f41 | 1999-06-23 15:12:09 +0000 | [diff] [blame] | 78 | |
| 79 | \begin{seealso} |
Fred Drake | 92f3f41 | 1999-06-23 15:12:09 +0000 | [diff] [blame] | 80 | \seemodule{tty}{Convenience functions for common terminal control |
| 81 | operations.} |
| 82 | \end{seealso} |
| 83 | |
| 84 | |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 85 | \subsection{Example} |
| 86 | \nodename{termios Example} |
| 87 | |
Fred Drake | e42f595 | 1998-03-11 06:00:23 +0000 | [diff] [blame] | 88 | Here's a function that prompts for a password with echoing turned |
| 89 | off. Note the technique using a separate \function{tcgetattr()} call |
| 90 | and a \keyword{try} ... \keyword{finally} statement to ensure that the |
| 91 | old tty attributes are restored exactly no matter what happens: |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 92 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 93 | \begin{verbatim} |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 94 | def getpass(prompt = "Password: "): |
Fred Drake | 969ab27 | 2001-02-27 22:01:15 +0000 | [diff] [blame] | 95 | import termios, sys |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 96 | fd = sys.stdin.fileno() |
| 97 | old = termios.tcgetattr(fd) |
| 98 | new = termios.tcgetattr(fd) |
Fred Drake | 969ab27 | 2001-02-27 22:01:15 +0000 | [diff] [blame] | 99 | new[3] = new[3] & ~termios.ECHO # lflags |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 100 | try: |
Fred Drake | 969ab27 | 2001-02-27 22:01:15 +0000 | [diff] [blame] | 101 | termios.tcsetattr(fd, termios.TCSADRAIN, new) |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 102 | passwd = raw_input(prompt) |
| 103 | finally: |
Fred Drake | 969ab27 | 2001-02-27 22:01:15 +0000 | [diff] [blame] | 104 | termios.tcsetattr(fd, termios.TCSADRAIN, old) |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 105 | return passwd |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 106 | \end{verbatim} |
Fred Drake | 83efb54 | 1998-02-19 20:07:39 +0000 | [diff] [blame] | 107 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 108 | |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 109 | \section{\module{TERMIOS} --- |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 110 | Constants used with the \module{termios} module} |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 111 | |
| 112 | \declaremodule[TERMIOSuppercase]{standard}{TERMIOS} |
Fred Drake | a54a887 | 1999-03-02 17:03:42 +0000 | [diff] [blame] | 113 | \platform{Unix} |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 114 | \modulesynopsis{Symbolic constants required to use the |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 115 | \module{termios} module.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 116 | |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 117 | |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 118 | \indexii{\POSIX{}}{I/O control} |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 119 | \indexii{tty}{I/O control} |
| 120 | |
Fred Drake | 969ab27 | 2001-02-27 22:01:15 +0000 | [diff] [blame] | 121 | \deprecated{2.1}{Import needed constants from \refmodule{termios} |
| 122 | instead.} |
Guido van Rossum | f9adf48 | 1995-03-31 12:26:24 +0000 | [diff] [blame] | 123 | |
Fred Drake | 969ab27 | 2001-02-27 22:01:15 +0000 | [diff] [blame] | 124 | This module defines the symbolic constants required to use the |
| 125 | \refmodule{termios}\refbimodindex{termios} module (see the previous |
| 126 | section). See the \POSIX{} or \UNIX{} manual pages for a list of |
| 127 | those constants. |