blob: 18b63bf4b81cd247e3a91abaa419619446145cad [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{termios}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{builtin}{termios}
3
4\modulesynopsis{\POSIX{} style tty control.}
5
Fred Drake65b32f71998-02-09 20:27:12 +00006\indexii{\POSIX{}}{I/O control}
Guido van Rossumf9adf481995-03-31 12:26:24 +00007\indexii{tty}{I/O control}
Guido van Rossumecde7811995-03-28 13:35:14 +00008
Guido van Rossumf9adf481995-03-31 12:26:24 +00009
Fred Drake65b32f71998-02-09 20:27:12 +000010This module provides an interface to the \POSIX{} calls for tty I/O
11control. For a complete description of these calls, see the \POSIX{} or
Guido van Rossumf9adf481995-03-31 12:26:24 +000012\UNIX{} manual pages. It is only available for those \UNIX{} versions
Fred Drakee42f5951998-03-11 06:00:23 +000013that support \POSIX{} \emph{termios} style tty I/O control (and then
Guido van Rossumf9adf481995-03-31 12:26:24 +000014only if configured at installation time).
15
16All functions in this module take a file descriptor \var{fd} as their
17first argument. This must be an integer file descriptor, such as
18returned by \code{sys.stdin.fileno()}.
19
Fred Drakee42f5951998-03-11 06:00:23 +000020This module should be used in conjunction with the
21\module{TERMIOS}\refstmodindex{TERMIOS} module, which defines the
22relevant symbolic constants (see the next section).
Guido van Rossumf9adf481995-03-31 12:26:24 +000023
24The module defines the following functions:
25
26\begin{funcdesc}{tcgetattr}{fd}
27Return a list containing the tty attributes for file descriptor
Fred Drakee42f5951998-03-11 06:00:23 +000028\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
31length 1, except the items with indices \constant{TERMIOS.VMIN} and
32\constant{TERMIOS.VTIME}, which are integers when these fields are
33defined). The interpretation of the flags and the speeds as well as
34the indexing in the \var{cc} array must be done using the symbolic
35constants defined in the \module{TERMIOS} module.
Guido van Rossumf9adf481995-03-31 12:26:24 +000036\end{funcdesc}
37
Fred Drakee42f5951998-03-11 06:00:23 +000038\begin{funcdesc}{tcsetattr}{fd, when, attributes}
Guido van Rossumf9adf481995-03-31 12:26:24 +000039Set the tty attributes for file descriptor \var{fd} from the
40\var{attributes}, which is a list like the one returned by
Fred Drakee42f5951998-03-11 06:00:23 +000041\function{tcgetattr()}. The \var{when} argument determines when the
42attributes are changed: \constant{TERMIOS.TCSANOW} to change
43immediately, \constant{TERMIOS.TCSADRAIN} to change after transmitting
44all queued output, or \constant{TERMIOS.TCSAFLUSH} to change after
45transmitting all queued output and discarding all queued input.
Guido van Rossumf9adf481995-03-31 12:26:24 +000046\end{funcdesc}
47
Fred Drakee42f5951998-03-11 06:00:23 +000048\begin{funcdesc}{tcsendbreak}{fd, duration}
Guido van Rossumf9adf481995-03-31 12:26:24 +000049Send a break on file descriptor \var{fd}. A zero \var{duration} sends
50a break for 0.25--0.5 seconds; a nonzero \var{duration} has a system
51dependent meaning.
52\end{funcdesc}
53
54\begin{funcdesc}{tcdrain}{fd}
55Wait until all output written to file descriptor \var{fd} has been
56transmitted.
57\end{funcdesc}
58
Fred Drakee42f5951998-03-11 06:00:23 +000059\begin{funcdesc}{tcflush}{fd, queue}
Guido van Rossumf9adf481995-03-31 12:26:24 +000060Discard queued data on file descriptor \var{fd}. The \var{queue}
Fred Drakee42f5951998-03-11 06:00:23 +000061selector specifies which queue: \constant{TERMIOS.TCIFLUSH} for the
62input queue, \constant{TERMIOS.TCOFLUSH} for the output queue, or
63\constant{TERMIOS.TCIOFLUSH} for both queues.
Guido van Rossumf9adf481995-03-31 12:26:24 +000064\end{funcdesc}
65
Fred Drakee42f5951998-03-11 06:00:23 +000066\begin{funcdesc}{tcflow}{fd, action}
Guido van Rossumf9adf481995-03-31 12:26:24 +000067Suspend or resume input or output on file descriptor \var{fd}. The
Fred Drakee42f5951998-03-11 06:00:23 +000068\var{action} argument can be \constant{TERMIOS.TCOOFF} to suspend
69output, \constant{TERMIOS.TCOON} to restart output,
70\constant{TERMIOS.TCIOFF} to suspend input, or
71\constant{TERMIOS.TCION} to restart input.
Guido van Rossumf9adf481995-03-31 12:26:24 +000072\end{funcdesc}
73
74\subsection{Example}
75\nodename{termios Example}
76
Fred Drakee42f5951998-03-11 06:00:23 +000077Here's a function that prompts for a password with echoing turned
78off. Note the technique using a separate \function{tcgetattr()} call
79and a \keyword{try} ... \keyword{finally} statement to ensure that the
80old tty attributes are restored exactly no matter what happens:
Guido van Rossumf9adf481995-03-31 12:26:24 +000081
Fred Drake19479911998-02-13 06:58:54 +000082\begin{verbatim}
Guido van Rossumf9adf481995-03-31 12:26:24 +000083def getpass(prompt = "Password: "):
84 import termios, TERMIOS, sys
85 fd = sys.stdin.fileno()
86 old = termios.tcgetattr(fd)
87 new = termios.tcgetattr(fd)
Fred Drakee42f5951998-03-11 06:00:23 +000088 new[3] = new[3] & ~TERMIOS.ECHO # lflags
Guido van Rossumf9adf481995-03-31 12:26:24 +000089 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 Drake19479911998-02-13 06:58:54 +000095\end{verbatim}
Fred Drake83efb541998-02-19 20:07:39 +000096
Fred Drake3a0351c1998-04-04 07:23:21 +000097\section{Standard Module \module{TERMIOS}}
Fred Drakeb91e9341998-07-23 17:59:49 +000098\declaremodule[TERMIOSuppercase]{standard}{TERMIOS}
99
100\modulesynopsis{The symbolic constants required to use the \module{termios} module.}
101
Fred Drake65b32f71998-02-09 20:27:12 +0000102\indexii{\POSIX{}}{I/O control}
Guido van Rossumf9adf481995-03-31 12:26:24 +0000103\indexii{tty}{I/O control}
104
Guido van Rossumf9adf481995-03-31 12:26:24 +0000105
106This module defines the symbolic constants required to use the
Fred Drakee42f5951998-03-11 06:00:23 +0000107\module{termios}\refbimodindex{termios} module (see the previous
108section). See the \POSIX{} or \UNIX{} manual pages (or the source)
109for a list of those constants.
Guido van Rossumf9adf481995-03-31 12:26:24 +0000110
111Note: this module resides in a system-dependent subdirectory of the
112Python library directory. You may have to generate it for your
113particular system using the script \file{Tools/scripts/h2py.py}.