blob: db8799dcc4eb42247b015b224ce3d9c7c540afd4 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{termios} ---
Fred Drakef6863c11999-03-02 16:37:17 +00002 \POSIX{} style tty control}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakef6863c11999-03-02 16:37:17 +00004\declaremodule{builtin}{termios}
Fred Drakea54a8871999-03-02 17:03:42 +00005 \platform{Unix}
Fred Drakec116b822001-05-09 15:50:17 +00006\modulesynopsis{\POSIX\ style tty control.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Fred Drake65b32f71998-02-09 20:27:12 +00008\indexii{\POSIX{}}{I/O control}
Guido van Rossumf9adf481995-03-31 12:26:24 +00009\indexii{tty}{I/O control}
Guido van Rossumecde7811995-03-28 13:35:14 +000010
Guido van Rossumf9adf481995-03-31 12:26:24 +000011
Fred Drake65b32f71998-02-09 20:27:12 +000012This module provides an interface to the \POSIX{} calls for tty I/O
13control. For a complete description of these calls, see the \POSIX{} or
Guido van Rossumf9adf481995-03-31 12:26:24 +000014\UNIX{} manual pages. It is only available for those \UNIX{} versions
Fred Drakee42f5951998-03-11 06:00:23 +000015that support \POSIX{} \emph{termios} style tty I/O control (and then
Guido van Rossumf9adf481995-03-31 12:26:24 +000016only if configured at installation time).
17
18All functions in this module take a file descriptor \var{fd} as their
Fred Drakea6140be2001-05-08 05:37:52 +000019first 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.
Guido van Rossumf9adf481995-03-31 12:26:24 +000022
Fred Drake969ab272001-02-27 22:01:15 +000023This 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.
Guido van Rossumf9adf481995-03-31 12:26:24 +000027
28The module defines the following functions:
29
30\begin{funcdesc}{tcgetattr}{fd}
31Return a list containing the tty attributes for file descriptor
Fred Drakee42f5951998-03-11 06:00:23 +000032\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
Fred Drake969ab272001-02-27 22:01:15 +000035length 1, except the items with indices \constant{VMIN} and
36\constant{VTIME}, which are integers when these fields are
Fred Drakee42f5951998-03-11 06:00:23 +000037defined). The interpretation of the flags and the speeds as well as
38the indexing in the \var{cc} array must be done using the symbolic
Fred Drake969ab272001-02-27 22:01:15 +000039constants defined in the \module{termios}
Fred Drakef6863c11999-03-02 16:37:17 +000040module.
Guido van Rossumf9adf481995-03-31 12:26:24 +000041\end{funcdesc}
42
Fred Drakee42f5951998-03-11 06:00:23 +000043\begin{funcdesc}{tcsetattr}{fd, when, attributes}
Guido van Rossumf9adf481995-03-31 12:26:24 +000044Set the tty attributes for file descriptor \var{fd} from the
45\var{attributes}, which is a list like the one returned by
Fred Drakee42f5951998-03-11 06:00:23 +000046\function{tcgetattr()}. The \var{when} argument determines when the
Fred Drake969ab272001-02-27 22:01:15 +000047attributes 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.
Guido van Rossumf9adf481995-03-31 12:26:24 +000051\end{funcdesc}
52
Fred Drakee42f5951998-03-11 06:00:23 +000053\begin{funcdesc}{tcsendbreak}{fd, duration}
Guido van Rossumf9adf481995-03-31 12:26:24 +000054Send 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
Fred Drakee42f5951998-03-11 06:00:23 +000064\begin{funcdesc}{tcflush}{fd, queue}
Guido van Rossumf9adf481995-03-31 12:26:24 +000065Discard queued data on file descriptor \var{fd}. The \var{queue}
Fred Drake969ab272001-02-27 22:01:15 +000066selector specifies which queue: \constant{TCIFLUSH} for the input
67queue, \constant{TCOFLUSH} for the output queue, or
68\constant{TCIOFLUSH} for both queues.
Guido van Rossumf9adf481995-03-31 12:26:24 +000069\end{funcdesc}
70
Fred Drakee42f5951998-03-11 06:00:23 +000071\begin{funcdesc}{tcflow}{fd, action}
Guido van Rossumf9adf481995-03-31 12:26:24 +000072Suspend or resume input or output on file descriptor \var{fd}. The
Fred Drake969ab272001-02-27 22:01:15 +000073\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.
Guido van Rossumf9adf481995-03-31 12:26:24 +000076\end{funcdesc}
77
Fred Drake92f3f411999-06-23 15:12:09 +000078
79\begin{seealso}
Fred Drake92f3f411999-06-23 15:12:09 +000080 \seemodule{tty}{Convenience functions for common terminal control
81 operations.}
82\end{seealso}
83
84
Guido van Rossumf9adf481995-03-31 12:26:24 +000085\subsection{Example}
86\nodename{termios Example}
87
Fred Drakee42f5951998-03-11 06:00:23 +000088Here'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:
Guido van Rossumf9adf481995-03-31 12:26:24 +000092
Fred Drake19479911998-02-13 06:58:54 +000093\begin{verbatim}
Guido van Rossumf9adf481995-03-31 12:26:24 +000094def getpass(prompt = "Password: "):
Fred Drake969ab272001-02-27 22:01:15 +000095 import termios, sys
Guido van Rossumf9adf481995-03-31 12:26:24 +000096 fd = sys.stdin.fileno()
97 old = termios.tcgetattr(fd)
98 new = termios.tcgetattr(fd)
Fred Drake969ab272001-02-27 22:01:15 +000099 new[3] = new[3] & ~termios.ECHO # lflags
Guido van Rossumf9adf481995-03-31 12:26:24 +0000100 try:
Fred Drake969ab272001-02-27 22:01:15 +0000101 termios.tcsetattr(fd, termios.TCSADRAIN, new)
Guido van Rossumf9adf481995-03-31 12:26:24 +0000102 passwd = raw_input(prompt)
103 finally:
Fred Drake969ab272001-02-27 22:01:15 +0000104 termios.tcsetattr(fd, termios.TCSADRAIN, old)
Guido van Rossumf9adf481995-03-31 12:26:24 +0000105 return passwd
Fred Drake19479911998-02-13 06:58:54 +0000106\end{verbatim}
Fred Drake83efb541998-02-19 20:07:39 +0000107
Fred Drakeb91e9341998-07-23 17:59:49 +0000108
Fred Drakebbac4321999-02-20 00:14:17 +0000109\section{\module{TERMIOS} ---
Fred Drakef6863c11999-03-02 16:37:17 +0000110 Constants used with the \module{termios} module}
Fred Drakebbac4321999-02-20 00:14:17 +0000111
112\declaremodule[TERMIOSuppercase]{standard}{TERMIOS}
Fred Drakea54a8871999-03-02 17:03:42 +0000113 \platform{Unix}
Fred Drake295da241998-08-10 19:42:37 +0000114\modulesynopsis{Symbolic constants required to use the
Fred Drakef6863c11999-03-02 16:37:17 +0000115 \module{termios} module.}
Fred Drakeb91e9341998-07-23 17:59:49 +0000116
Fred Drakebbac4321999-02-20 00:14:17 +0000117
Fred Drake65b32f71998-02-09 20:27:12 +0000118\indexii{\POSIX{}}{I/O control}
Guido van Rossumf9adf481995-03-31 12:26:24 +0000119\indexii{tty}{I/O control}
120
Fred Drake969ab272001-02-27 22:01:15 +0000121\deprecated{2.1}{Import needed constants from \refmodule{termios}
122 instead.}
Guido van Rossumf9adf481995-03-31 12:26:24 +0000123
Fred Drake969ab272001-02-27 22:01:15 +0000124This module defines the symbolic constants required to use the
125\refmodule{termios}\refbimodindex{termios} module (see the previous
126section). See the \POSIX{} or \UNIX{} manual pages for a list of
127those constants.