"termios" --- POSIX スタイルの端末制御
**************************************

======================================================================

このモジュールでは端末 I/O 制御のための POSIX 準拠の関数呼び出しインタ
ーフェースを提供します。これら呼び出しのための完全な記述については、
Unix マニュアルページの *termios(3)* を参照してください。これは POSIX
*termios* 形式の端末制御をサポートしていてインストール時に有効にした
Unix のバージョンでのみ利用可能です。

Availability: Unix.

このモジュールの関数は全て、ファイル記述子 *fd* を最初の引数としてとり
ます。この値は、 "sys.stdin.fileno()" が返すような整数のファイル記述子
でも、 "sys.stdin" 自体のような *file object* でもかまいません。

このモジュールではまた、モジュールで提供されている関数を使う上で必要と
なる全ての定数を定義しています; これらの定数は C の対応する関数と同じ
名前を持っています。これらの端末制御インターフェースを利用する上でのさ
らなる情報については、あなたのシステムのドキュメンテーションを参考にし
てください。

このモジュールには、以下の関数が定義されています:

termios.tcgetattr(fd)

   Return a list containing the tty attributes for file descriptor
   *fd*, as follows: "[iflag, oflag, cflag, lflag, ispeed, ospeed,
   cc]" where *cc* is a list of the tty special characters (each a
   string of length 1, except the items with indices "VMIN" and
   "VTIME", which are integers when these fields are defined).  The
   interpretation of the flags and the speeds as well as the indexing
   in the *cc* array must be done using the symbolic constants defined
   in the "termios" module.

termios.tcsetattr(fd, when, attributes)

   Set the tty attributes for file descriptor *fd* from the
   *attributes*, which is a list like the one returned by
   "tcgetattr()".  The *when* argument determines when the attributes
   are changed:

   termios.TCSANOW

      Change attributes immediately.

   termios.TCSADRAIN

      Change attributes after transmitting all queued output.

   termios.TCSAFLUSH

      Change attributes after transmitting all queued output and
      discarding all queued input.

termios.tcsendbreak(fd, duration)

   ファイル記述子 *fd* にブレークを送信します。*duration* をゼロにする
   と、0.25〜0.5 秒間のブレークを送信します; *duration* の値がゼロでな
   い場合、その意味はシステム依存です。

termios.tcdrain(fd)

   ファイル記述子 *fd* に書き込まれた全ての出力が転送されるまで待ちま
   す。

termios.tcflush(fd, queue)

   ファイル記述子 *fd* にキューされたデータを無視します。どのキューか
   は *queue* セレクタで指定します: "TCIFLUSH" は入力キュー、
   "TCOFLUSH" は出力キュー、 "TCIOFLUSH" は両方のキューです。

termios.tcflow(fd, action)

   ファイル記述子 *fd* の入力または出力をサスペンドしたりレジュームし
   たりします。引数 *action* は出力をサスペンドする "TCOOFF" 、出力を
   レジュームする "TCOON" 、入力をサスペンドする "TCIOFF" 、入力をレジ
   ュームする "TCION" をとることができます。

termios.tcgetwinsize(fd)

   Return a tuple "(ws_row, ws_col)" containing the tty window size
   for file descriptor *fd*. Requires "termios.TIOCGWINSZ" or
   "termios.TIOCGSIZE".

   Added in version 3.11.

termios.tcsetwinsize(fd, winsize)

   Set the tty window size for file descriptor *fd* from *winsize*,
   which is a two-item tuple "(ws_row, ws_col)" like the one returned
   by "tcgetwinsize()". Requires at least one of the pairs
   ("termios.TIOCGWINSZ", "termios.TIOCSWINSZ"); ("termios.TIOCGSIZE",
   "termios.TIOCSSIZE") to be defined.

   Added in version 3.11.

参考:

  "tty" モジュール
     一般的な端末制御操作のための便利な関数。


使用例
======

以下はエコーバックを切った状態でパスワード入力を促す関数です。ユーザの
入力に関わらず以前の端末属性を正確に回復するために、二つの
"tcgetattr()" と "try" ... "finally" 文によるテクニックが使われていま
す:

   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 = input(prompt)
       finally:
           termios.tcsetattr(fd, termios.TCSADRAIN, old)
       return passwd
