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