cinap_lenrekadd hg and python4bb9e2215 years agoHistory
106 lines · 3.9 KB · TeX
RawDownload
\section{\module{termios} ---         \POSIX{} style tty control} \declaremodule{builtin}{termios}  \platform{Unix}\modulesynopsis{\POSIX\ style tty control.} \indexii{\POSIX}{I/O control}\indexii{tty}{I/O control}  This module provides an interface to the \POSIX{} calls for tty I/Ocontrol.  For a complete description of these calls, see the \POSIX{} or\UNIX{} manual pages.  It is only available for those \UNIX{} versionsthat support \POSIX{} \emph{termios} style tty I/O control (and thenonly if configured at installation time). All functions in this module take a file descriptor \var{fd} as theirfirst argument.  This can be an integer file descriptor, such asreturned by \code{sys.stdin.fileno()}, or a file object, such as\code{sys.stdin} itself. This module also defines all the constants needed to work with thefunctions provided here; these have the same name as theircounterparts in C.  Please refer to your system documentation for moreinformation on using these terminal control interfaces. The module defines the following functions: \begin{funcdesc}{tcgetattr}{fd}Return a list containing the tty attributes for file descriptor\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 oflength 1, except the items with indices \constant{VMIN} and\constant{VTIME}, which are integers when these fields aredefined).  The interpretation of the flags and the speeds as well asthe indexing in the \var{cc} array must be done using the symbolicconstants defined in the \module{termios}module.\end{funcdesc} \begin{funcdesc}{tcsetattr}{fd, when, attributes}Set the tty attributes for file descriptor \var{fd} from the\var{attributes}, which is a list like the one returned by\function{tcgetattr()}.  The \var{when} argument determines when theattributes are changed: \constant{TCSANOW} to change immediately,\constant{TCSADRAIN} to change after transmitting all queued output,or \constant{TCSAFLUSH} to change after transmitting all queuedoutput and discarding all queued input.\end{funcdesc} \begin{funcdesc}{tcsendbreak}{fd, duration}Send a break on file descriptor \var{fd}.  A zero \var{duration} sendsa break for 0.25--0.5 seconds; a nonzero \var{duration} has a systemdependent meaning.\end{funcdesc} \begin{funcdesc}{tcdrain}{fd}Wait until all output written to file descriptor \var{fd} has beentransmitted.\end{funcdesc} \begin{funcdesc}{tcflush}{fd, queue}Discard queued data on file descriptor \var{fd}.  The \var{queue}selector specifies which queue: \constant{TCIFLUSH} for the inputqueue, \constant{TCOFLUSH} for the output queue, or\constant{TCIOFLUSH} for both queues.\end{funcdesc} \begin{funcdesc}{tcflow}{fd, action}Suspend or resume input or output on file descriptor \var{fd}.  The\var{action} argument can be \constant{TCOOFF} to suspend output,\constant{TCOON} to restart output, \constant{TCIOFF} to suspendinput, or \constant{TCION} to restart input.\end{funcdesc}  \begin{seealso}  \seemodule{tty}{Convenience functions for common terminal control                  operations.}\end{seealso}  \subsection{Example}\nodename{termios Example} Here's a function that prompts for a password with echoing turnedoff.  Note the technique using a separate \function{tcgetattr()} calland a \keyword{try} ... \keyword{finally} statement to ensure that theold tty attributes are restored exactly no matter what happens: \begin{verbatim}def getpass(prompt = "Password: "):    import termios, sys    fd = sys.stdin.fileno()    old = termios.tcgetattr(fd)    new = termios.tcgetattr(fd)    new[3] = new[3] & ~termios.ECHO          # lflags    try:        termios.tcsetattr(fd, termios.TCSADRAIN, new)        passwd = raw_input(prompt)    finally:        termios.tcsetattr(fd, termios.TCSADRAIN, old)    return passwd\end{verbatim}