blob: 9c9c3c82d32846ac66b653262751dcfa20daac09 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{termios} ---
2 \POSIX{} style tty control.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{termios}
4
5\modulesynopsis{\POSIX{} style tty control.}
6
Fred Drake65b32f71998-02-09 20:27:12 +00007\indexii{\POSIX{}}{I/O control}
Guido van Rossumf9adf481995-03-31 12:26:24 +00008\indexii{tty}{I/O control}
Guido van Rossumecde7811995-03-28 13:35:14 +00009
Guido van Rossumf9adf481995-03-31 12:26:24 +000010
Fred Drake65b32f71998-02-09 20:27:12 +000011This module provides an interface to the \POSIX{} calls for tty I/O
12control. For a complete description of these calls, see the \POSIX{} or
Guido van Rossumf9adf481995-03-31 12:26:24 +000013\UNIX{} manual pages. It is only available for those \UNIX{} versions
Fred Drakee42f5951998-03-11 06:00:23 +000014that support \POSIX{} \emph{termios} style tty I/O control (and then
Guido van Rossumf9adf481995-03-31 12:26:24 +000015only if configured at installation time).
16
17All functions in this module take a file descriptor \var{fd} as their
18first argument. This must be an integer file descriptor, such as
19returned by \code{sys.stdin.fileno()}.
20
Fred Drakee42f5951998-03-11 06:00:23 +000021This module should be used in conjunction with the
22\module{TERMIOS}\refstmodindex{TERMIOS} module, which defines the
23relevant symbolic constants (see the next section).
Guido van Rossumf9adf481995-03-31 12:26:24 +000024
25The module defines the following functions:
26
27\begin{funcdesc}{tcgetattr}{fd}
28Return a list containing the tty attributes for file descriptor
Fred Drakee42f5951998-03-11 06:00:23 +000029\var{fd}, as follows: \code{[}\var{iflag}, \var{oflag}, \var{cflag},
30\var{lflag}, \var{ispeed}, \var{ospeed}, \var{cc}\code{]} where
31\var{cc} is a list of the tty special characters (each a string of
32length 1, except the items with indices \constant{TERMIOS.VMIN} and
33\constant{TERMIOS.VTIME}, which are integers when these fields are
34defined). The interpretation of the flags and the speeds as well as
35the indexing in the \var{cc} array must be done using the symbolic
36constants defined in the \module{TERMIOS} module.
Guido van Rossumf9adf481995-03-31 12:26:24 +000037\end{funcdesc}
38
Fred Drakee42f5951998-03-11 06:00:23 +000039\begin{funcdesc}{tcsetattr}{fd, when, attributes}
Guido van Rossumf9adf481995-03-31 12:26:24 +000040Set the tty attributes for file descriptor \var{fd} from the
41\var{attributes}, which is a list like the one returned by
Fred Drakee42f5951998-03-11 06:00:23 +000042\function{tcgetattr()}. The \var{when} argument determines when the
43attributes are changed: \constant{TERMIOS.TCSANOW} to change
44immediately, \constant{TERMIOS.TCSADRAIN} to change after transmitting
45all queued output, or \constant{TERMIOS.TCSAFLUSH} to change after
46transmitting all queued output and discarding all queued input.
Guido van Rossumf9adf481995-03-31 12:26:24 +000047\end{funcdesc}
48
Fred Drakee42f5951998-03-11 06:00:23 +000049\begin{funcdesc}{tcsendbreak}{fd, duration}
Guido van Rossumf9adf481995-03-31 12:26:24 +000050Send a break on file descriptor \var{fd}. A zero \var{duration} sends
51a break for 0.25--0.5 seconds; a nonzero \var{duration} has a system
52dependent meaning.
53\end{funcdesc}
54
55\begin{funcdesc}{tcdrain}{fd}
56Wait until all output written to file descriptor \var{fd} has been
57transmitted.
58\end{funcdesc}
59
Fred Drakee42f5951998-03-11 06:00:23 +000060\begin{funcdesc}{tcflush}{fd, queue}
Guido van Rossumf9adf481995-03-31 12:26:24 +000061Discard queued data on file descriptor \var{fd}. The \var{queue}
Fred Drakee42f5951998-03-11 06:00:23 +000062selector specifies which queue: \constant{TERMIOS.TCIFLUSH} for the
63input queue, \constant{TERMIOS.TCOFLUSH} for the output queue, or
64\constant{TERMIOS.TCIOFLUSH} for both queues.
Guido van Rossumf9adf481995-03-31 12:26:24 +000065\end{funcdesc}
66
Fred Drakee42f5951998-03-11 06:00:23 +000067\begin{funcdesc}{tcflow}{fd, action}
Guido van Rossumf9adf481995-03-31 12:26:24 +000068Suspend or resume input or output on file descriptor \var{fd}. The
Fred Drakee42f5951998-03-11 06:00:23 +000069\var{action} argument can be \constant{TERMIOS.TCOOFF} to suspend
70output, \constant{TERMIOS.TCOON} to restart output,
71\constant{TERMIOS.TCIOFF} to suspend input, or
72\constant{TERMIOS.TCION} to restart input.
Guido van Rossumf9adf481995-03-31 12:26:24 +000073\end{funcdesc}
74
75\subsection{Example}
76\nodename{termios Example}
77
Fred Drakee42f5951998-03-11 06:00:23 +000078Here's a function that prompts for a password with echoing turned
79off. Note the technique using a separate \function{tcgetattr()} call
80and a \keyword{try} ... \keyword{finally} statement to ensure that the
81old tty attributes are restored exactly no matter what happens:
Guido van Rossumf9adf481995-03-31 12:26:24 +000082
Fred Drake19479911998-02-13 06:58:54 +000083\begin{verbatim}
Guido van Rossumf9adf481995-03-31 12:26:24 +000084def getpass(prompt = "Password: "):
85 import termios, TERMIOS, sys
86 fd = sys.stdin.fileno()
87 old = termios.tcgetattr(fd)
88 new = termios.tcgetattr(fd)
Fred Drakee42f5951998-03-11 06:00:23 +000089 new[3] = new[3] & ~TERMIOS.ECHO # lflags
Guido van Rossumf9adf481995-03-31 12:26:24 +000090 try:
91 termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new)
92 passwd = raw_input(prompt)
93 finally:
94 termios.tcsetattr(fd, TERMIOS.TCSADRAIN, old)
95 return passwd
Fred Drake19479911998-02-13 06:58:54 +000096\end{verbatim}
Fred Drake83efb541998-02-19 20:07:39 +000097
Fred Drake295da241998-08-10 19:42:37 +000098\section{\module{TERMIOS} ---
99 Constants used with the \module{termios} module.}
Fred Drakeb91e9341998-07-23 17:59:49 +0000100\declaremodule[TERMIOSuppercase]{standard}{TERMIOS}
101
Fred Drake295da241998-08-10 19:42:37 +0000102\modulesynopsis{Symbolic constants required to use the
103\module{termios} module.}
Fred Drakeb91e9341998-07-23 17:59:49 +0000104
Fred Drake65b32f71998-02-09 20:27:12 +0000105\indexii{\POSIX{}}{I/O control}
Guido van Rossumf9adf481995-03-31 12:26:24 +0000106\indexii{tty}{I/O control}
107
Guido van Rossumf9adf481995-03-31 12:26:24 +0000108
109This module defines the symbolic constants required to use the
Fred Drakee42f5951998-03-11 06:00:23 +0000110\module{termios}\refbimodindex{termios} module (see the previous
111section). See the \POSIX{} or \UNIX{} manual pages (or the source)
112for a list of those constants.
Guido van Rossumf9adf481995-03-31 12:26:24 +0000113
114Note: this module resides in a system-dependent subdirectory of the
115Python library directory. You may have to generate it for your
116particular system using the script \file{Tools/scripts/h2py.py}.