/Doc/library/termios.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 105 lines · 70 code · 35 blank · 0 comment · 0 complexity · faa75b56bdeb035b434cc93f0091d173 MD5 · raw file

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