xref: /third_party/python/Doc/library/termios.rst (revision 7db96d56)
17db96d56Sopenharmony_ci:mod:`termios` --- POSIX style tty control
27db96d56Sopenharmony_ci==========================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: termios
57db96d56Sopenharmony_ci   :platform: Unix
67db96d56Sopenharmony_ci   :synopsis: POSIX style tty control.
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci.. index::
97db96d56Sopenharmony_ci   pair: POSIX; I/O control
107db96d56Sopenharmony_ci   pair: tty; I/O control
117db96d56Sopenharmony_ci
127db96d56Sopenharmony_ci--------------
137db96d56Sopenharmony_ci
147db96d56Sopenharmony_ciThis module provides an interface to the POSIX calls for tty I/O control. For a
157db96d56Sopenharmony_cicomplete description of these calls, see :manpage:`termios(3)` Unix manual
167db96d56Sopenharmony_cipage.  It is only available for those Unix versions that support POSIX
177db96d56Sopenharmony_ci*termios* style tty I/O control configured during installation.
187db96d56Sopenharmony_ci
197db96d56Sopenharmony_ciAll functions in this module take a file descriptor *fd* as their first
207db96d56Sopenharmony_ciargument.  This can be an integer file descriptor, such as returned by
217db96d56Sopenharmony_ci``sys.stdin.fileno()``, or a :term:`file object`, such as ``sys.stdin`` itself.
227db96d56Sopenharmony_ci
237db96d56Sopenharmony_ciThis module also defines all the constants needed to work with the functions
247db96d56Sopenharmony_ciprovided here; these have the same name as their counterparts in C.  Please
257db96d56Sopenharmony_cirefer to your system documentation for more information on using these terminal
267db96d56Sopenharmony_cicontrol interfaces.
277db96d56Sopenharmony_ci
287db96d56Sopenharmony_ciThe module defines the following functions:
297db96d56Sopenharmony_ci
307db96d56Sopenharmony_ci
317db96d56Sopenharmony_ci.. function:: tcgetattr(fd)
327db96d56Sopenharmony_ci
337db96d56Sopenharmony_ci   Return a list containing the tty attributes for file descriptor *fd*, as
347db96d56Sopenharmony_ci   follows: ``[iflag, oflag, cflag, lflag, ispeed, ospeed, cc]`` where *cc* is a
357db96d56Sopenharmony_ci   list of the tty special characters (each a string of length 1, except the
367db96d56Sopenharmony_ci   items with indices :const:`VMIN` and :const:`VTIME`, which are integers when
377db96d56Sopenharmony_ci   these fields are defined).  The interpretation of the flags and the speeds as
387db96d56Sopenharmony_ci   well as the indexing in the *cc* array must be done using the symbolic
397db96d56Sopenharmony_ci   constants defined in the :mod:`termios` module.
407db96d56Sopenharmony_ci
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci.. function:: tcsetattr(fd, when, attributes)
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ci   Set the tty attributes for file descriptor *fd* from the *attributes*, which is
457db96d56Sopenharmony_ci   a list like the one returned by :func:`tcgetattr`.  The *when* argument
467db96d56Sopenharmony_ci   determines when the attributes are changed: :const:`TCSANOW` to change
477db96d56Sopenharmony_ci   immediately, :const:`TCSADRAIN` to change after transmitting all queued output,
487db96d56Sopenharmony_ci   or :const:`TCSAFLUSH` to change after transmitting all queued output and
497db96d56Sopenharmony_ci   discarding all queued input.
507db96d56Sopenharmony_ci
517db96d56Sopenharmony_ci
527db96d56Sopenharmony_ci.. function:: tcsendbreak(fd, duration)
537db96d56Sopenharmony_ci
547db96d56Sopenharmony_ci   Send a break on file descriptor *fd*.  A zero *duration* sends a break for
557db96d56Sopenharmony_ci   0.25--0.5 seconds; a nonzero *duration* has a system dependent meaning.
567db96d56Sopenharmony_ci
577db96d56Sopenharmony_ci
587db96d56Sopenharmony_ci.. function:: tcdrain(fd)
597db96d56Sopenharmony_ci
607db96d56Sopenharmony_ci   Wait until all output written to file descriptor *fd* has been transmitted.
617db96d56Sopenharmony_ci
627db96d56Sopenharmony_ci
637db96d56Sopenharmony_ci.. function:: tcflush(fd, queue)
647db96d56Sopenharmony_ci
657db96d56Sopenharmony_ci   Discard queued data on file descriptor *fd*.  The *queue* selector specifies
667db96d56Sopenharmony_ci   which queue: :const:`TCIFLUSH` for the input queue, :const:`TCOFLUSH` for the
677db96d56Sopenharmony_ci   output queue, or :const:`TCIOFLUSH` for both queues.
687db96d56Sopenharmony_ci
697db96d56Sopenharmony_ci
707db96d56Sopenharmony_ci.. function:: tcflow(fd, action)
717db96d56Sopenharmony_ci
727db96d56Sopenharmony_ci   Suspend or resume input or output on file descriptor *fd*.  The *action*
737db96d56Sopenharmony_ci   argument can be :const:`TCOOFF` to suspend output, :const:`TCOON` to restart
747db96d56Sopenharmony_ci   output, :const:`TCIOFF` to suspend input, or :const:`TCION` to restart input.
757db96d56Sopenharmony_ci
767db96d56Sopenharmony_ci
777db96d56Sopenharmony_ci.. function:: tcgetwinsize(fd)
787db96d56Sopenharmony_ci
797db96d56Sopenharmony_ci   Return a tuple ``(ws_row, ws_col)`` containing the tty window size for file
807db96d56Sopenharmony_ci   descriptor *fd*. Requires :const:`termios.TIOCGWINSZ` or
817db96d56Sopenharmony_ci   :const:`termios.TIOCGSIZE`.
827db96d56Sopenharmony_ci
837db96d56Sopenharmony_ci   .. versionadded:: 3.11
847db96d56Sopenharmony_ci
857db96d56Sopenharmony_ci
867db96d56Sopenharmony_ci.. function:: tcsetwinsize(fd, winsize)
877db96d56Sopenharmony_ci
887db96d56Sopenharmony_ci   Set the tty window size for file descriptor *fd* from *winsize*, which is
897db96d56Sopenharmony_ci   a two-item tuple ``(ws_row, ws_col)`` like the one returned by
907db96d56Sopenharmony_ci   :func:`tcgetwinsize`. Requires at least one of the pairs
917db96d56Sopenharmony_ci   (:const:`termios.TIOCGWINSZ`, :const:`termios.TIOCSWINSZ`);
927db96d56Sopenharmony_ci   (:const:`termios.TIOCGSIZE`, :const:`termios.TIOCSSIZE`) to be defined.
937db96d56Sopenharmony_ci
947db96d56Sopenharmony_ci   .. versionadded:: 3.11
957db96d56Sopenharmony_ci
967db96d56Sopenharmony_ci
977db96d56Sopenharmony_ci.. seealso::
987db96d56Sopenharmony_ci
997db96d56Sopenharmony_ci   Module :mod:`tty`
1007db96d56Sopenharmony_ci      Convenience functions for common terminal control operations.
1017db96d56Sopenharmony_ci
1027db96d56Sopenharmony_ci
1037db96d56Sopenharmony_ci.. _termios-example:
1047db96d56Sopenharmony_ci
1057db96d56Sopenharmony_ciExample
1067db96d56Sopenharmony_ci-------
1077db96d56Sopenharmony_ci
1087db96d56Sopenharmony_ciHere's a function that prompts for a password with echoing turned off.  Note the
1097db96d56Sopenharmony_citechnique using a separate :func:`tcgetattr` call and a :keyword:`try` ...
1107db96d56Sopenharmony_ci:keyword:`finally` statement to ensure that the old tty attributes are restored
1117db96d56Sopenharmony_ciexactly no matter what happens::
1127db96d56Sopenharmony_ci
1137db96d56Sopenharmony_ci   def getpass(prompt="Password: "):
1147db96d56Sopenharmony_ci       import termios, sys
1157db96d56Sopenharmony_ci       fd = sys.stdin.fileno()
1167db96d56Sopenharmony_ci       old = termios.tcgetattr(fd)
1177db96d56Sopenharmony_ci       new = termios.tcgetattr(fd)
1187db96d56Sopenharmony_ci       new[3] = new[3] & ~termios.ECHO          # lflags
1197db96d56Sopenharmony_ci       try:
1207db96d56Sopenharmony_ci           termios.tcsetattr(fd, termios.TCSADRAIN, new)
1217db96d56Sopenharmony_ci           passwd = input(prompt)
1227db96d56Sopenharmony_ci       finally:
1237db96d56Sopenharmony_ci           termios.tcsetattr(fd, termios.TCSADRAIN, old)
1247db96d56Sopenharmony_ci       return passwd
1257db96d56Sopenharmony_ci
126