blob: 7693ecd02789ccb37cd43d4b1891a9be4d8b9d6a [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`termios` --- POSIX style tty control
2==========================================
3
4.. module:: termios
5 :platform: Unix
6 :synopsis: POSIX style tty control.
7
Georg Brandl116aa622007-08-15 14:28:22 +00008.. index::
9 pair: POSIX; I/O control
10 pair: tty; I/O control
11
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040012--------------
13
Senthil Kumaran07791292016-06-02 23:49:05 -070014This module provides an interface to the POSIX calls for tty I/O control. For a
Edward Bettse197a852017-11-11 16:40:26 +000015complete description of these calls, see :manpage:`termios(3)` Unix manual
Senthil Kumaran07791292016-06-02 23:49:05 -070016page. It is only available for those Unix versions that support POSIX
17*termios* style tty I/O control configured during installation.
Georg Brandl116aa622007-08-15 14:28:22 +000018
19All functions in this module take a file descriptor *fd* as their first
20argument. This can be an integer file descriptor, such as returned by
Antoine Pitrou11cb9612010-09-15 11:11:28 +000021``sys.stdin.fileno()``, or a :term:`file object`, such as ``sys.stdin`` itself.
Georg Brandl116aa622007-08-15 14:28:22 +000022
23This module also defines all the constants needed to work with the functions
24provided here; these have the same name as their counterparts in C. Please
25refer to your system documentation for more information on using these terminal
26control interfaces.
27
28The module defines the following functions:
29
30
31.. function:: tcgetattr(fd)
32
33 Return a list containing the tty attributes for file descriptor *fd*, as
34 follows: ``[iflag, oflag, cflag, lflag, ispeed, ospeed, cc]`` where *cc* is a
35 list of the tty special characters (each a string of length 1, except the
36 items with indices :const:`VMIN` and :const:`VTIME`, which are integers when
37 these fields are defined). The interpretation of the flags and the speeds as
38 well as the indexing in the *cc* array must be done using the symbolic
39 constants defined in the :mod:`termios` module.
40
41
42.. function:: tcsetattr(fd, when, attributes)
43
44 Set the tty attributes for file descriptor *fd* from the *attributes*, which is
45 a list like the one returned by :func:`tcgetattr`. The *when* argument
46 determines when the attributes are changed: :const:`TCSANOW` to change
47 immediately, :const:`TCSADRAIN` to change after transmitting all queued output,
48 or :const:`TCSAFLUSH` to change after transmitting all queued output and
49 discarding all queued input.
50
51
52.. function:: tcsendbreak(fd, duration)
53
54 Send a break on file descriptor *fd*. A zero *duration* sends a break for 0.25
55 --0.5 seconds; a nonzero *duration* has a system dependent meaning.
56
57
58.. function:: tcdrain(fd)
59
60 Wait until all output written to file descriptor *fd* has been transmitted.
61
62
63.. function:: tcflush(fd, queue)
64
65 Discard queued data on file descriptor *fd*. The *queue* selector specifies
66 which queue: :const:`TCIFLUSH` for the input queue, :const:`TCOFLUSH` for the
67 output queue, or :const:`TCIOFLUSH` for both queues.
68
69
70.. function:: tcflow(fd, action)
71
72 Suspend or resume input or output on file descriptor *fd*. The *action*
73 argument can be :const:`TCOOFF` to suspend output, :const:`TCOON` to restart
74 output, :const:`TCIOFF` to suspend input, or :const:`TCION` to restart input.
75
76
77.. seealso::
78
79 Module :mod:`tty`
80 Convenience functions for common terminal control operations.
81
82
Georg Brandl7f01a132009-09-16 15:58:14 +000083.. _termios-example:
84
Georg Brandl116aa622007-08-15 14:28:22 +000085Example
86-------
87
Georg Brandl116aa622007-08-15 14:28:22 +000088Here's a function that prompts for a password with echoing turned off. Note the
89technique using a separate :func:`tcgetattr` call and a :keyword:`try` ...
90:keyword:`finally` statement to ensure that the old tty attributes are restored
91exactly no matter what happens::
92
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000093 def getpass(prompt="Password: "):
Georg Brandl116aa622007-08-15 14:28:22 +000094 import termios, sys
95 fd = sys.stdin.fileno()
96 old = termios.tcgetattr(fd)
97 new = termios.tcgetattr(fd)
98 new[3] = new[3] & ~termios.ECHO # lflags
99 try:
100 termios.tcsetattr(fd, termios.TCSADRAIN, new)
Georg Brandl8d5c3922007-12-02 22:48:17 +0000101 passwd = input(prompt)
Georg Brandl116aa622007-08-15 14:28:22 +0000102 finally:
103 termios.tcsetattr(fd, termios.TCSADRAIN, old)
104 return passwd
105