source: vendor/python/2.5/Doc/lib/libtermios.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 3.9 KB
Line 
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
12This module provides an interface to the \POSIX{} calls for tty I/O
13control. For a complete description of these calls, see the \POSIX{} or
14\UNIX{} manual pages. It is only available for those \UNIX{} versions
15that support \POSIX{} \emph{termios} style tty I/O control (and then
16only if configured at installation time).
17
18All functions in this module take a file descriptor \var{fd} as their
19first argument. This can be an integer file descriptor, such as
20returned by \code{sys.stdin.fileno()}, or a file object, such as
21\code{sys.stdin} itself.
22
23This module also defines all the constants needed to work with the
24functions provided here; these have the same name as their
25counterparts in C. Please refer to your system documentation for more
26information on using these terminal control interfaces.
27
28The module defines the following functions:
29
30\begin{funcdesc}{tcgetattr}{fd}
31Return a list containing the tty attributes for file descriptor
32\var{fd}, as follows: \code{[}\var{iflag}, \var{oflag}, \var{cflag},
33\var{lflag}, \var{ispeed}, \var{ospeed}, \var{cc}\code{]} where
34\var{cc} is a list of the tty special characters (each a string of
35length 1, except the items with indices \constant{VMIN} and
36\constant{VTIME}, which are integers when these fields are
37defined). The interpretation of the flags and the speeds as well as
38the indexing in the \var{cc} array must be done using the symbolic
39constants defined in the \module{termios}
40module.
41\end{funcdesc}
42
43\begin{funcdesc}{tcsetattr}{fd, when, attributes}
44Set the tty attributes for file descriptor \var{fd} from the
45\var{attributes}, which is a list like the one returned by
46\function{tcgetattr()}. The \var{when} argument determines when the
47attributes are changed: \constant{TCSANOW} to change immediately,
48\constant{TCSADRAIN} to change after transmitting all queued output,
49or \constant{TCSAFLUSH} to change after transmitting all queued
50output and discarding all queued input.
51\end{funcdesc}
52
53\begin{funcdesc}{tcsendbreak}{fd, duration}
54Send a break on file descriptor \var{fd}. A zero \var{duration} sends
55a break for 0.25--0.5 seconds; a nonzero \var{duration} has a system
56dependent meaning.
57\end{funcdesc}
58
59\begin{funcdesc}{tcdrain}{fd}
60Wait until all output written to file descriptor \var{fd} has been
61transmitted.
62\end{funcdesc}
63
64\begin{funcdesc}{tcflush}{fd, queue}
65Discard queued data on file descriptor \var{fd}. The \var{queue}
66selector specifies which queue: \constant{TCIFLUSH} for the input
67queue, \constant{TCOFLUSH} for the output queue, or
68\constant{TCIOFLUSH} for both queues.
69\end{funcdesc}
70
71\begin{funcdesc}{tcflow}{fd, action}
72Suspend or resume input or output on file descriptor \var{fd}. The
73\var{action} argument can be \constant{TCOOFF} to suspend output,
74\constant{TCOON} to restart output, \constant{TCIOFF} to suspend
75input, or \constant{TCION} to restart input.
76\end{funcdesc}
77
78
79\begin{seealso}
80 \seemodule{tty}{Convenience functions for common terminal control
81 operations.}
82\end{seealso}
83
84
85\subsection{Example}
86\nodename{termios Example}
87
88Here's a function that prompts for a password with echoing turned
89off. Note the technique using a separate \function{tcgetattr()} call
90and a \keyword{try} ... \keyword{finally} statement to ensure that the
91old tty attributes are restored exactly no matter what happens:
92
93\begin{verbatim}
94def getpass(prompt = "Password: "):
95 import termios, sys
96 fd = sys.stdin.fileno()
97 old = termios.tcgetattr(fd)
98 new = termios.tcgetattr(fd)
99 new[3] = new[3] & ~termios.ECHO # lflags
100 try:
101 termios.tcsetattr(fd, termios.TCSADRAIN, new)
102 passwd = raw_input(prompt)
103 finally:
104 termios.tcsetattr(fd, termios.TCSADRAIN, old)
105 return passwd
106\end{verbatim}
Note: See TracBrowser for help on using the repository browser.