libtermios.tex 3.88 KB
Newer Older
Fred Drake's avatar
Fred Drake committed
1
\section{\module{termios} ---
2
         \POSIX{} style tty control}
3

4
\declaremodule{builtin}{termios}
5
  \platform{Unix}
6
\modulesynopsis{\POSIX\ style tty control.}
7

8
\indexii{\POSIX}{I/O control}
9
\indexii{tty}{I/O control}
10

11

12 13
This module provides an interface to the \POSIX{} calls for tty I/O
control.  For a complete description of these calls, see the \POSIX{} or
14
\UNIX{} manual pages.  It is only available for those \UNIX{} versions
Fred Drake's avatar
Fred Drake committed
15
that support \POSIX{} \emph{termios} style tty I/O control (and then
16 17 18
only if configured at installation time).

All functions in this module take a file descriptor \var{fd} as their
19 20 21
first argument.  This can be an integer file descriptor, such as
returned by \code{sys.stdin.fileno()}, or a file object, such as
\code{sys.stdin} itself.
22

23 24 25 26
This module also defines all the constants needed to work with the
functions provided here; these have the same name as their
counterparts in C.  Please refer to your system documentation for more
information on using these terminal control interfaces.
27 28 29 30 31

The module defines the following functions:

\begin{funcdesc}{tcgetattr}{fd}
Return a list containing the tty attributes for file descriptor
Fred Drake's avatar
Fred Drake committed
32 33 34
\var{fd}, as follows: \code{[}\var{iflag}, \var{oflag}, \var{cflag},
\var{lflag}, \var{ispeed}, \var{ospeed}, \var{cc}\code{]} where
\var{cc} is a list of the tty special characters (each a string of
35 36
length 1, except the items with indices \constant{VMIN} and
\constant{VTIME}, which are integers when these fields are
Fred Drake's avatar
Fred Drake committed
37 38
defined).  The interpretation of the flags and the speeds as well as
the indexing in the \var{cc} array must be done using the symbolic
39
constants defined in the \module{termios}
40
module.
41 42
\end{funcdesc}

Fred Drake's avatar
Fred Drake committed
43
\begin{funcdesc}{tcsetattr}{fd, when, attributes}
44 45
Set the tty attributes for file descriptor \var{fd} from the
\var{attributes}, which is a list like the one returned by
Fred Drake's avatar
Fred Drake committed
46
\function{tcgetattr()}.  The \var{when} argument determines when the
47 48 49 50
attributes are changed: \constant{TCSANOW} to change immediately,
\constant{TCSADRAIN} to change after transmitting all queued output,
or \constant{TCSAFLUSH} to change after transmitting all queued
output and discarding all queued input.
51 52
\end{funcdesc}

Fred Drake's avatar
Fred Drake committed
53
\begin{funcdesc}{tcsendbreak}{fd, duration}
54 55 56 57 58 59 60 61 62 63
Send a break on file descriptor \var{fd}.  A zero \var{duration} sends
a break for 0.25--0.5 seconds; a nonzero \var{duration} has a system
dependent meaning.
\end{funcdesc}

\begin{funcdesc}{tcdrain}{fd}
Wait until all output written to file descriptor \var{fd} has been
transmitted.
\end{funcdesc}

Fred Drake's avatar
Fred Drake committed
64
\begin{funcdesc}{tcflush}{fd, queue}
65
Discard queued data on file descriptor \var{fd}.  The \var{queue}
66 67 68
selector specifies which queue: \constant{TCIFLUSH} for the input
queue, \constant{TCOFLUSH} for the output queue, or
\constant{TCIOFLUSH} for both queues.
69 70
\end{funcdesc}

Fred Drake's avatar
Fred Drake committed
71
\begin{funcdesc}{tcflow}{fd, action}
72
Suspend or resume input or output on file descriptor \var{fd}.  The
73 74 75
\var{action} argument can be \constant{TCOOFF} to suspend output,
\constant{TCOON} to restart output, \constant{TCIOFF} to suspend
input, or \constant{TCION} to restart input.
76 77
\end{funcdesc}

78 79 80 81 82 83 84

\begin{seealso}
  \seemodule{tty}{Convenience functions for common terminal control
                  operations.}
\end{seealso}


85 86 87
\subsection{Example}
\nodename{termios Example}

Fred Drake's avatar
Fred Drake committed
88 89 90 91
Here's a function that prompts for a password with echoing turned
off.  Note the technique using a separate \function{tcgetattr()} call
and a \keyword{try} ... \keyword{finally} statement to ensure that the
old tty attributes are restored exactly no matter what happens:
92

93
\begin{verbatim}
94
def getpass(prompt = "Password: "):
95
    import termios, sys
96 97 98
    fd = sys.stdin.fileno()
    old = termios.tcgetattr(fd)
    new = termios.tcgetattr(fd)
99
    new[3] = new[3] & ~termios.ECHO          # lflags
100
    try:
101
        termios.tcsetattr(fd, termios.TCSADRAIN, new)
102 103
        passwd = raw_input(prompt)
    finally:
104
        termios.tcsetattr(fd, termios.TCSADRAIN, old)
105
    return passwd
106
\end{verbatim}