blob: 5e2ce1ef481ceeb080dbd1f35c3112877b9d6674 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{termios}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-termios}
Guido van Rossumf9adf481995-03-31 12:26:24 +00003\bimodindex{termios}
Fred Drake65b32f71998-02-09 20:27:12 +00004\indexii{\POSIX{}}{I/O control}
Guido van Rossumf9adf481995-03-31 12:26:24 +00005\indexii{tty}{I/O control}
Guido van Rossumecde7811995-03-28 13:35:14 +00006
Guido van Rossumf9adf481995-03-31 12:26:24 +00007
Fred Drake65b32f71998-02-09 20:27:12 +00008This module provides an interface to the \POSIX{} calls for tty I/O
9control. For a complete description of these calls, see the \POSIX{} or
Guido van Rossumf9adf481995-03-31 12:26:24 +000010\UNIX{} manual pages. It is only available for those \UNIX{} versions
Fred Drakee42f5951998-03-11 06:00:23 +000011that support \POSIX{} \emph{termios} style tty I/O control (and then
Guido van Rossumf9adf481995-03-31 12:26:24 +000012only if configured at installation time).
13
14All functions in this module take a file descriptor \var{fd} as their
15first argument. This must be an integer file descriptor, such as
16returned by \code{sys.stdin.fileno()}.
17
Fred Drakee42f5951998-03-11 06:00:23 +000018This module should be used in conjunction with the
19\module{TERMIOS}\refstmodindex{TERMIOS} module, which defines the
20relevant symbolic constants (see the next section).
Guido van Rossumf9adf481995-03-31 12:26:24 +000021
22The module defines the following functions:
23
24\begin{funcdesc}{tcgetattr}{fd}
25Return a list containing the tty attributes for file descriptor
Fred Drakee42f5951998-03-11 06:00:23 +000026\var{fd}, as follows: \code{[}\var{iflag}, \var{oflag}, \var{cflag},
27\var{lflag}, \var{ispeed}, \var{ospeed}, \var{cc}\code{]} where
28\var{cc} is a list of the tty special characters (each a string of
29length 1, except the items with indices \constant{TERMIOS.VMIN} and
30\constant{TERMIOS.VTIME}, which are integers when these fields are
31defined). The interpretation of the flags and the speeds as well as
32the indexing in the \var{cc} array must be done using the symbolic
33constants defined in the \module{TERMIOS} module.
Guido van Rossumf9adf481995-03-31 12:26:24 +000034\end{funcdesc}
35
Fred Drakee42f5951998-03-11 06:00:23 +000036\begin{funcdesc}{tcsetattr}{fd, when, attributes}
Guido van Rossumf9adf481995-03-31 12:26:24 +000037Set the tty attributes for file descriptor \var{fd} from the
38\var{attributes}, which is a list like the one returned by
Fred Drakee42f5951998-03-11 06:00:23 +000039\function{tcgetattr()}. The \var{when} argument determines when the
40attributes are changed: \constant{TERMIOS.TCSANOW} to change
41immediately, \constant{TERMIOS.TCSADRAIN} to change after transmitting
42all queued output, or \constant{TERMIOS.TCSAFLUSH} to change after
43transmitting all queued output and discarding all queued input.
Guido van Rossumf9adf481995-03-31 12:26:24 +000044\end{funcdesc}
45
Fred Drakee42f5951998-03-11 06:00:23 +000046\begin{funcdesc}{tcsendbreak}{fd, duration}
Guido van Rossumf9adf481995-03-31 12:26:24 +000047Send a break on file descriptor \var{fd}. A zero \var{duration} sends
48a break for 0.25--0.5 seconds; a nonzero \var{duration} has a system
49dependent meaning.
50\end{funcdesc}
51
52\begin{funcdesc}{tcdrain}{fd}
53Wait until all output written to file descriptor \var{fd} has been
54transmitted.
55\end{funcdesc}
56
Fred Drakee42f5951998-03-11 06:00:23 +000057\begin{funcdesc}{tcflush}{fd, queue}
Guido van Rossumf9adf481995-03-31 12:26:24 +000058Discard queued data on file descriptor \var{fd}. The \var{queue}
Fred Drakee42f5951998-03-11 06:00:23 +000059selector specifies which queue: \constant{TERMIOS.TCIFLUSH} for the
60input queue, \constant{TERMIOS.TCOFLUSH} for the output queue, or
61\constant{TERMIOS.TCIOFLUSH} for both queues.
Guido van Rossumf9adf481995-03-31 12:26:24 +000062\end{funcdesc}
63
Fred Drakee42f5951998-03-11 06:00:23 +000064\begin{funcdesc}{tcflow}{fd, action}
Guido van Rossumf9adf481995-03-31 12:26:24 +000065Suspend or resume input or output on file descriptor \var{fd}. The
Fred Drakee42f5951998-03-11 06:00:23 +000066\var{action} argument can be \constant{TERMIOS.TCOOFF} to suspend
67output, \constant{TERMIOS.TCOON} to restart output,
68\constant{TERMIOS.TCIOFF} to suspend input, or
69\constant{TERMIOS.TCION} to restart input.
Guido van Rossumf9adf481995-03-31 12:26:24 +000070\end{funcdesc}
71
72\subsection{Example}
73\nodename{termios Example}
74
Fred Drakee42f5951998-03-11 06:00:23 +000075Here's a function that prompts for a password with echoing turned
76off. Note the technique using a separate \function{tcgetattr()} call
77and a \keyword{try} ... \keyword{finally} statement to ensure that the
78old tty attributes are restored exactly no matter what happens:
Guido van Rossumf9adf481995-03-31 12:26:24 +000079
Fred Drake19479911998-02-13 06:58:54 +000080\begin{verbatim}
Guido van Rossumf9adf481995-03-31 12:26:24 +000081def getpass(prompt = "Password: "):
82 import termios, TERMIOS, sys
83 fd = sys.stdin.fileno()
84 old = termios.tcgetattr(fd)
85 new = termios.tcgetattr(fd)
Fred Drakee42f5951998-03-11 06:00:23 +000086 new[3] = new[3] & ~TERMIOS.ECHO # lflags
Guido van Rossumf9adf481995-03-31 12:26:24 +000087 try:
88 termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new)
89 passwd = raw_input(prompt)
90 finally:
91 termios.tcsetattr(fd, TERMIOS.TCSADRAIN, old)
92 return passwd
Fred Drake19479911998-02-13 06:58:54 +000093\end{verbatim}
Fred Drake83efb541998-02-19 20:07:39 +000094
Fred Drake3a0351c1998-04-04 07:23:21 +000095\section{Standard Module \module{TERMIOS}}
Fred Drake83efb541998-02-19 20:07:39 +000096\label{module-TERMIOSuppercase}
Guido van Rossumf9adf481995-03-31 12:26:24 +000097\stmodindex{TERMIOS}
Fred Drake65b32f71998-02-09 20:27:12 +000098\indexii{\POSIX{}}{I/O control}
Guido van Rossumf9adf481995-03-31 12:26:24 +000099\indexii{tty}{I/O control}
100
Guido van Rossumf9adf481995-03-31 12:26:24 +0000101
102This module defines the symbolic constants required to use the
Fred Drakee42f5951998-03-11 06:00:23 +0000103\module{termios}\refbimodindex{termios} module (see the previous
104section). See the \POSIX{} or \UNIX{} manual pages (or the source)
105for a list of those constants.
Guido van Rossumf9adf481995-03-31 12:26:24 +0000106
107Note: this module resides in a system-dependent subdirectory of the
108Python library directory. You may have to generate it for your
109particular system using the script \file{Tools/scripts/h2py.py}.