blob: e55aab4119fe15b0d092133febb7ebfc5be2b7d5 [file] [log] [blame]
Guido van Rossumecde7811995-03-28 13:35:14 +00001\section{Built-in Module \sectcode{termios}}
Guido van Rossumf9adf481995-03-31 12:26:24 +00002\bimodindex{termios}
3\indexii{Posix}{I/O control}
4\indexii{tty}{I/O control}
Guido van Rossumecde7811995-03-28 13:35:14 +00005
Guido van Rossumf9adf481995-03-31 12:26:24 +00006\renewcommand{\indexsubitem}{(in module termios)}
7
8This module provides an interface to the Posix calls for tty I/O
9control. For a complete description of these calls, see the Posix or
10\UNIX{} manual pages. It is only available for those \UNIX{} versions
11that support Posix \code{termios} style tty I/O control (and then
12only 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
18This module should be used in conjunction with the \code{TERMIOS}
19module, which defines the relevant symbolic constants (see the next
20section).
21
22The module defines the following functions:
23
24\begin{funcdesc}{tcgetattr}{fd}
25Return a list containing the tty attributes for file descriptor
26\var{fd}, as follows: \code{[\var{iflag}, \var{oflag}, \var{cflag},
27\var{lflag}, \var{ispeed}, \var{ospeed}, \var{cc}]} where \var{cc} is
28a list of the tty special characters (each a string of length 1,
29except the items with indices \code{VMIN} and \code{VTIME}, which are
30integers when these fields are defined). The interpretation of the
31flags and the speeds as well as the indexing in the \var{cc} array
32must be done using the symbolic constants defined in the
33\code{TERMIOS} module.
34\end{funcdesc}
35
36\begin{funcdesc}{tcsetattr}{fd\, when\, attributes}
37Set the tty attributes for file descriptor \var{fd} from the
38\var{attributes}, which is a list like the one returned by
39\code{tcgetattr()}. The \var{when} argument determines when the
40attributes are changed: \code{TERMIOS.TCSANOW} to change immediately,
41\code{TERMIOS.TCSADRAIN} to change after transmitting all queued
42output, or \code{TERMIOS.TCSAFLUSH} to change after transmitting all
43queued output and discarding all queued input.
44\end{funcdesc}
45
46\begin{funcdesc}{tcsendbreak}{fd\, duration}
47Send 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
57\begin{funcdesc}{tcflush}{fd\, queue}
58Discard queued data on file descriptor \var{fd}. The \var{queue}
59selector specifies which queue: \code{TERMIOS.TCIFLUSH} for the input
60queue, \code{TERMIOS.TCOFLUSH} for the output queue, or
61\code{TERMIOS.TCIOFLUSH} for both queues.
62\end{funcdesc}
63
64\begin{funcdesc}{tcflow}{fd\, action}
65Suspend or resume input or output on file descriptor \var{fd}. The
66\var{action} argument can be \code{TERMIOS.TCOOFF} to suspend output,
67\code{TERMIOS.TCOON} to restart output, \code{TERMIOS.TCIOFF} to
68suspend input, or \code{TERMIOS.TCION} to restart input.
69\end{funcdesc}
70
71\subsection{Example}
72\nodename{termios Example}
73
74Here's a function that prompts for a password with echoing turned off.
75Note the technique using a separate \code{termios.tcgetattr()} call
76and a \code{try {\ldots} finally} statement to ensure that the old tty
77attributes are restored exactly no matter what happens:
78
79\begin{verbatim}
80def getpass(prompt = "Password: "):
81 import termios, TERMIOS, sys
82 fd = sys.stdin.fileno()
83 old = termios.tcgetattr(fd)
84 new = termios.tcgetattr(fd)
85 new[3] = new[3] & ~TERMIOS.ECHO # lflags
86 try:
87 termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new)
88 passwd = raw_input(prompt)
89 finally:
90 termios.tcsetattr(fd, TERMIOS.TCSADRAIN, old)
91 return passwd
92\end{verbatim}
93
94
95\section{Standard Module \sectcode{TERMIOS}}
96\stmodindex{TERMIOS}
97\indexii{Posix}{I/O control}
98\indexii{tty}{I/O control}
99
100\renewcommand{\indexsubitem}{(in module TERMIOS)}
101
102This module defines the symbolic constants required to use the
103\code{termios} module (see the previous section). See the Posix or
104\UNIX{} manual pages (or the source) for a list of those constants.
105
106Note: this module resides in a system-dependent subdirectory of the
107Python library directory. You may have to generate it for your
108particular system using the script \file{Tools/scripts/h2py.py}.