termiosのヘルプ・マニュアル
日本語 英語
termios --help
man termios
TERMIOS(3) Linux Programmer’s Manual TERMIOS(3)
名前
termios, tcgetattr, tcsetattr, tcsendbreak, tcdrain, tcflush, tcflow,
cfmakeraw, cfgetospeed, cfgetispeed, cfsetispeed, cfsetospeed, cfset-
speed - ターミナル属性の取得・設定、ライン制御、ボーレートの取得・設定
書式
#include
#include
int tcgetattr(int fd, struct termios *termios_p);
int tcsetattr(int fd, int optional_actions,
const struct termios *termios_p);
int tcsendbreak(int fd, int duration);
int tcdrain(int fd);
int tcflush(int fd, int queue_selector);
int tcflow(int fd, int action);
void cfmakeraw(struct termios *termios_p);
speed_t cfgetispeed(const struct termios *termios_p);
speed_t cfgetospeed(const struct termios *termios_p);
int cfsetispeed(struct termios *termios_p, speed_t speed);
int cfsetospeed(struct termios *termios_p, speed_t speed);
int cfsetspeed(struct termios *termios_p, speed_t speed);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
cfsetspeed(), cfmakeraw(): _BSD_SOURCE
説明
termios 関数群は、非同期通信ポートを制御するための汎用ターミナルインタ
フェースである。
termios 構造体
ここに示されている関数の多くは、引き数に termios_p を用いる。この引き数
は termios 構造体へのポインタである。この構造体には少なくとも以下に示す
メンバが含まれる:
tcflag_t c_iflag; /* input modes */
tcflag_t c_oflag; /* output modes */
tcflag_t c_cflag; /* control modes */
tcflag_t c_lflag; /* local modes */
cc_t c_cc[NCCS]; /* control chars */
以下に、これらのフィールドに割り当て可能な値について説明する。最初の 4
つ のビットマスクフィールドでは、関係するフラグの定義のいくつかは、特定
の機能検査マクロ (feature_test_macros(7) 参照) が定義された場合にのみ公
開される。必要な機能検査マクロは角括弧 ("[]") 内に書かれている。
以 下の説明で、 "POSIX にはない" はその値が POSIX.1-2001 で規定されてい
ないことを意味し、 "XSI" はその値が POSIX.1-2001 の XSI 拡張で規定さ れ
ていることを意味する。
c_iflag フラグには以下の要素を指定できる:
IGNBRK 入力中の BREAK 信号を無視する。
BRKINT IGNBRK が設定されている場合は、BREAK 信号は無視される。 IGNBRK
が設定されていないが、BRKINT が設定されている場合は、 BREAK 信号
によって入出力キューがフラッシュされ、さらに、端末がフォアグラウ
ンドプロセスグループの制御端末の場合は、フォアグラウンドプロセス
グループに SIGINT が送られる。 IGNBRK も BRKINT も設定されていな
い場合、 BREAK を NULL バイト ('\0') として読み込 む 。 た だ し
、PARMRK が設定されている場合は \377 \0 \0 のシーケンスとして読
み込む。
IGNPAR フレームエラーおよびパリティエラーを無視する。
PARMRK IGNPAR が設定されていない場合、パリティエラーあるいはフレーム エ
ラーの発生した文字の前に \377 \0 を付加する。IGNPAR も PARMRK も
設定されていない場合、パリティエラーあるいはフレームエラーの発生
した文字を \0 として読み込む。
INPCK 入力のパリティチェックを有効にする。
ISTRIP 8 ビット目を落とす。
INLCR 入力の NL (New Line: 改行文字) を CR (Carriage Return: 復帰文字)
に置き換える。
IGNCR 入力の CR を無視する。
ICRNL (IGNCR が設定されていない場合) 入力の CR を NL に置き換える。
IUCLC (POSIX にはない) 入力の大文字を小文字に置き換える。
IXON 出力の XON/XOFF フロー制御を有効にする。
IXANY (XSI) 任意の文字を入力すると、停止していた出力を再開する (デフォ
ルトでは、START 文字でのみ出力が再開される)。
IXOFF 入力の XON/XOFF フロー制御を有効にする。
IMAXBEL
(POSIX にはない) 入力キューが一杯の時にベルを鳴らす。 Linux では
このビットは実装されておらず、常にセットされているかのように振舞
う。
IUTF8 (Linux 2.6.4 以降)
(POSIX にはない) 入力が UTF8 である; これにより cooked mode で文
字削除 (character-erase) を正しく機能させることができる。
POSIX.1 で定義されている c_oflag フラグを以下に示す:
OPOST 実装に依存した出力処理を有効にする。
残りの c_oflag フラグは、特記のない限り POSIX.1-2001 で定義されている。
OLCUC (POSIX にはない) 出力時に小文字を大文字に変換する。
ONLCR (XSI) 出力の NL を CR-NL に置き換える。
OCRNL 出力の CR を NL に置き換える。
ONOCR 0 桁目で CR を出力しない。
ONLRET CR を出力しない。
OFILL 転送時間を遅らせるのではなく、補填文字 (fill character) を送る。
(訳注:特定の文字に対して、端末側の処理を待つために転送を一定時間
遅 らせることができる。また、 OFILL を指定すると転送を遅らせる代
わりに補填文字を送る。)
OFDEL (POSIX にはない) 補填文字を ASCII DEL にする。このフラグが設定さ
れていない場合は ASCII NUL ('\0') になる。 (Linux では実装されて
いない)
NLDLY NL の遅延を設定する。値は NL0 (遅延なし) および NL1 で あ る 。
[_BSD_SOURCE か _SVID_SOURCE か _XOPEN_SOURCE が必要]
CRDLY CR の遅延を設定する。値は CR0 (遅延なし), CR1, CR2,CR3 である。
[_BSD_SOURCE か _SVID_SOURCE か _XOPEN_SOURCE が必要]
TABDLY 水平タブ (horizontal tab) の遅延を設定する。値は TAB0 ( 遅 延 な
し), TAB1, TAB2, TAB3 (XTABS) で あ る 。 [_BSD_SOURCE か
_SVID_SOURCE か _XOPEN_SOURCE が必要] XTAB3 (これは XTABS と同じ
で ある) の値はタブをスペース何個に変換するかを示す (タブは 8 桁
毎に止まる)。
BSDLY 後退 (backspace) の遅延を設定する。値は BS0 (遅延なし) あるい は
BS1 である。 (実装されたことはない) [_BSD_SOURCE か _SVID_SOURCE
か _XOPEN_SOURCE が必要]
VTDLY 垂直タブ (vertical tab) の遅延を設定する。値は VT0 (遅延なし) あ
るいは VT1 である。
FFDLY 頁送り (form feed) の遅延を設定する。値は FF0 (遅延なし) あるい
は FF1 である。 [_BSD_SOURCE か _SVID_SOURCE か _XOPEN_SOURCE が
必要]
c_cflag フラグは以下の通り:
CBAUD (POSIX にはない) ボーレートマスク (4+1 ビット)。 [_BSD_SOURCE か
_SVID_SOURCE が必要]
CBAUDEX
(POSIX にはない) 追加のボーレートマスク (1 ビット)。 CBAUD に 含
まれている。 [_BSD_SOURCE か _SVID_SOURCE が必要]
(POSIX では、 termios 構造体に格納されたボーレートは正確なもので
はなく、ボーレ ー ト を 操 作 す る た め に cfgetispeed() と
cfsetispeed() が提供されている。 c_cflag 内の CBAUD で選択された
ビットを使うシステムもあれば、 sg_ispeed や sg_ospeed といった独
立したフィールドを使うものもある。)
CSIZE 文字サイズを設定する。値は CS5, CS6, CS7, CS8 である。
CSTOPB ストップビットを 1 ではなく 2 にする。
CREAD 受信を有効にする。
PARENB 出力にパリティを付加し、入力のパリティチェックを行う。
PARODD 設定されると、入力および出力に対するパリティが奇数パリティとなる
。設定されない場合、偶数パリティが使用される。
HUPCL 最後のプロセスがデバイスをクローズした後、モデムの制御線 を low
にする (切断する)。
CLOCAL モデムの制御線を無視する。
LOBLK (POSIX にはない) 現在のシェル層以外からの出力を抑制する。
shl (シェル層) で用いられる。(Linux では実装されていない)
CIBAUD (POSIX に はない) 入力速度のマスク。 CIBAUD ビットのための値は
CBAUD ビットのための値と同じであり、左に IBSHIFT ビットシフト し
た ものである。 [_BSD_SOURCE か _SVID_SOURCE が必要] (Linux では
実装されていない)
CMSPAR (POSIX にはない) (一部のシリアルデバイスでサポートされている) 「
ス ティック (stick)」パリティ (マーク/スペース パリティ)を使用す
る。 PARODD が設定された場合パリティビットは常に 1 となり、設 定
さ れない場合は常に 0 となる。 [_BSD_SOURCE か _SVID_SOURCE が必
要]
CRTSCTS
(POSIX にはない) RTS/CTS (ハードウェア) フロー制御を有効にする。
[_BSD_SOURCE か _SVID_SOURCE が必要]
c_lflag フラグは以下の通り:
ISIG INTR, QUIT, SUSP, DSUSP の文字を受信した時、対応するシグナルを発
生させる。
ICANON カノニカルモードを有効にする (下記参照)。
XCASE (POSIX にはない; Linux では対応していない) ICANON が同時に設定さ
れ た場合、端末は大文字のみが有効である。入力された文字は \ が付
いた文字を除いて小文字に変換される。出力時は、大文字の前に \ が
付 き、小文字は大文字に変換される。 [_BSD_SOURCE が _SVID_SOURCE
か _XOPEN_SOURCE が必要]
ECHO 入力された文字をエコーする。
ECHOE ICANON も同時に設定された場合、ERASE 文字は前の文字を削 除 し 、
WERASE 文字は前の単語を削除する。
ECHOK ICANON も同時に設定された場合、KILL 文字は現在の行を削除する。
ECHONL ICANON も同時に設定された場合、 ECHO が設定されていなくても NL
文字をエコーする。
ECHOCTL
(POSIX にはない) ECHO も同時に設定された場合、 TAB, NL, START,
STOP の ASCII 制御文字が ^X としてエコーされる。 X は制御文字よ
り ASCII コードで 0x10 だけ大きな文字である。例え ば 文 字 0x28
(BS) は ^H とエコーされる。 [_BSD_SOURCE か _SVID_SOURCE が必要]
ECHOPRT
(POSIX にはない) ICANON および IECHO が同時に設定されている場 合
、削除された文字も表示される。 [_BSD_SOURCE か _SVID_SOURCE が必
要]
ECHOKE (POSIX にはない) ICANON も設定された場合、 KILL が行の各文字を消
去する代わりにエコーされる。これは ECHOE および ECHOPRT を指定す
ることと等しい。 [_BSD_SOURCE か _SVID_SOURCE が必要]
DEFECHO
(POSIX にはない) プロセスが読み込んだときにだけエ コ ー す る 。
(Linux では実装されていない)
FLUSHO (POSIX にはない; Linux では対応していない) 出力をフラッシュする
。このフラグは DISCARD 文字を入力することで 切 替 え ら れ る 。
[_BSD_SOURCE か _SVID_SOURCE が必要]
NOFLSH SIGINT, SIGQUIT, SIGSUSP シグナル発生時の入力・出力キューのフラ
ッシュを無効にする。
TOSTOP バックグラウンドプロセスのプロセスグループで制御端末へ文字を出力
しようとしているプロセスに対して SIGTTOU シグナルを送る。
PENDIN (POSIX にはない; Linux では対応していない) 次の文字を読み込んだ
時、入力キュー中の全文字を再表示する。 (bash(1) は入力行をこのよ
うに処理している。) [_BSD_SOURCE か _SVID_SOURCE が必要]
IEXTEN 実 装 依 存の入力処理を有効にする。このフラグは、特殊文字である
EOL2, LNEXT, REPRINT, WERASE や、 IUCLC フラグを有効にするために
必要である。
c_cc 配列は特殊な制御文字を定義している。シンボルの一覧 (初期値) と意味
は以下の通り。
VINTR (003, ETX, Ctrl-C か 0177, DEL, rubout) 割り込み文字。 SIGINT シ
グナルを送る。 ISIG がセットされている場合に認識し、入力には渡さ
れない。
VQUIT (034, FS, Ctrl-\) 終了文字。 SIGQUIT シグナルを送る。 ISIG が セ
ットされている場合に認識し、入力には渡されない。
VERASE (0177, DEL, rubout か 010, BS, Ctrl-H か #) 消去文字。これにより
、直前の未消去文字を消去する。しかし、EOF や行頭を超えては消去し
ない。 ICANON がセットされている場合に認識し、入力には渡されない
。
VKILL (025, NAK, Ctrl-U か Ctrl-X か @) 完全消去文字。直前の EOF か 行
頭以降の全ての入力を消去する。 ICANON がセットされている場合に認
識し、入力には渡されない。
VEOF (004, EOT, Ctrl-D) ファイル終端文字。より正確には、tty バッフ ァ
の内容を行末を待たずにユーザープログラムに送る。これが行の最初の
文字だった場合、ユーザープログラムの read(2) は 0 を返し、ファイ
ル終端であることを知らせる。 ICANON がセットされている場合に認識
し、入力には渡されない。
VMIN 非カノニカル読み込み時の最小文字数。
VEOL (0, NUL) 追加の行末文字。 ICANON がセットされている場合に認識 す
る。
VTIME 非カノニカル読み込み時のタイムアウト時間 (1/10 秒単位)。
VEOL2 (POSIX にはない; 0, NUL) 追加の行末文字。 ICANON がセットされて
いる場合に認識する。
VSWTCH (POSIX にはない; Linux では対応していない; 0, NUL) スイッチ文 字
(shl でのみ用いられる)。
VSTART (021, DC1, Ctrl-Q) 開始文字。停止文字で停止した出力を再開する。
IXON がセットされている場合に認識し、入力には渡されない。
VSTOP (023, DC3, Ctrl-S) 停止文字。開始文字が入力されるまで出力を停 止
する。 IXON が設定されている場合に認識し、入力には渡されない。
VSUSP (032, SUB, Ctrl-Z) 中断文字。 SIGTSTP シグナルを送る。 ISIG がセ
ットされている場合に認識し、入力には渡されない。
VDSUSP (POSIX にはない; Linux では対応していない; 031, EM, Ctrl-Y) 遅延
中 断 文字。ユーザープログラムから文字が読み込まれた時に SIGTSTP
シグナルを送る。 IEXTEN と ISIG がセットされていて、システムがジ
ョブ制御に対応している場合に認識し、入力には渡されない。
VLNEXT (POSIX にはない; 026, SYN Ctrl-V) リテラル。次の入力文字をエスケ
ープし、特別な意味があっても解釈しない。 IEXTEN がセットされてい
る場合に認識し、入力には渡されない。
VWERASE
(POSIX にはない; 027, ETB, Ctrl-W) 単語消去。 ICANON と IEXTEN
がセットされている場合に認識し、入力には渡されない。
VREPRINT
(POSIX にはない; 022, DC2, Ctrl-R) まだ読み込んでいない文字列 を
再表示する。 ICANON と IEXTEN がセットされている場合に認識し、入
力には渡されない。
VDISCARD
(POSIX にはない; Linux では対応していない; 017, SI, Ctrl-O) 未送
信 バッファの内容の破棄/保存を切り替える。 IEXTEN がセットされて
いる場合に認識し、入力には渡されない。
VSTATUS
(POSIX にはない; Linux では対応していない; 状態要求: 024, DC4,
Ctrl-T)
こ れ らのシンボルの示す値は全て異なる。ただし、 VTIME, VMIN はそれぞれ
VEOL, VEOF と同じ値である。非カノニカルモードでは、特殊文字の意味はタイ
ムアウトの意味に変わる。 VMIN と VTIME の説明については、下記の非カノニ
カルモードの説明を参照のこと。
端末の設定の取得と変更
tcgetattr() は fd に関するパラメータを取得し、termios_p が参照する構 造
体 termios に設定する。この関数はバックグラウンドプロセスから呼ばれるこ
ともあるが、この場合、端末の属性はフォアグラウンドプロセスによって変 化
することもある。
tcsetattr() は端末に関連したパラメータを設定する (ハードウェアの設定に
必要で、ここで設定できないものを除く)。設定には termios_p が参 照 す る
termios 構造体を用いる。 optional_actions には変更が有効となるタイミン
グを設定する:
TCSANOW
ただちに変更が有効となる。
TCSADRAIN
fd への出力がすべて転送された後に変更が有効になる。この機能は 出
力に影響するパラメータを変更する時に使用するべきである。
TCSAFLUSH
パラメータを変更する前に、 fd への出力がすべて転送され、受信した
がまだ読み込んでいないすべての入力が破棄される。
カノニカルモードと非カノニカルモード
c_lflag の ICANON フラグの設定により、端末がカノニカルモードで動作す る
かが決定される。 ICANON がセットされた場合、カノニカルモード (canonical
mode) となり、セットされない場合、非カノニカル モ ー ド (non-canonical
mode) となる。デフォルトでは、 ICANON はセットされる。
カノニカルモードでは、以下のような動作となる。
* 入力は行単位に行われる。行区切り文字が打ち込まれた時点で、入力行が利
用可能となる。行区切り文字は NL, EOL, EOL2 および行頭での EOF であ る
。 EOF 以外の場合、 read(2) が返すバッファに行区切り文字も含められる
。
* 行編集が有効となる (ERASE, KILL が効果を持つ。 IEXTEN フラグが設定 さ
れ た場合は、 WERASE, REPRINT, LNEXT も効果を持つ)。 read(2) は最大で
も 1行の入力しか返さない。 read(2) が要求したバイト数が現在の入力行の
バ イト数よりも少ない場合、要求したのと同じバイト数だけが読み込まれ、
残りの文字は次回の read(2) で読み込まれる。
非カノニカルモードでは、入力は即座に利用可能となり (ユーザは行区切り 文
字 を打ち込む必要はない)、行編集は無効となる。 MIN (c_cc[VMIN]) と TIME
(c_cc[VTIME]) の設定により、 read(2) が完了する条件が決定される。4種 類
の場合がある:
* MIN == 0; TIME == 0: データが利用可能であれば、 read(2) はすぐに返る
。このときの返り値は、そのとき利用可能なバイト数か read(2) の要求バイ
ト 数のうち小さい方となる。利用可能なデータがない場合 read(2) は 0 を
返す。
* MIN > 0; TIME == 0: read(2) は、利用可能なデータが MIN バイトか要求バ
イ ト数のいずれかに達するまで停止する。返り値は、MIN か要求バイト数の
小さい方となる。
* MIN == 0; TIME > 0: TIME はタイマの上限を規定し、単位は 1/10 秒である
。 タイマは read(2) が呼ばれた時点で開始される。 read(2) が返るのは、
少なくとも 1バイトのデータが利用可能となった時点、またはタイマが時 間
切 れとなった時点である。入力が全くなくタイマが時間切れとなった場合、
read(2) は 0 を返す。
* MIN > 0; TIME > 0: TIME はタイマの上限を規定し、単位は 1/10 秒であ る
。 入力の最初のバイトが利用可能になった後は、新たに 1バイト受信する度
にタイマがリセットされる。 read(2) が返るのは、MIN バイトか要求バイト
数 のうち少ない方まで読み出された時点か、バイト間タイマが時間切れとな
った時点である。は最初のバイトが利用可能にならないとタイマは開始さ れ
ないので、少なくとも 1バイトは読み込まれる。
Raw mode
cfmakeraw() は、端末を昔の Version 7 端末ドライバの "raw" モードのよう
に設定する。入力は文字単位に可能であり、エコーが無効となり、端末の入 出
力 文字に対する特殊処理はすべて無効となる。端末の属性は以下のように設定
される:
termios_p->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON);
termios_p->c_oflag &= ~OPOST;
termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
termios_p->c_cflag &= ~(CSIZE | PARENB);
termios_p->c_cflag |= CS8;
ライン制御
tcsendbreak() は端末が非同期のシリアルデータ転送を用いている場合に、 連
続 した0のビット列を一定間隔で転送する。 duration が 0 の場合は、0 のビ
ットを 0.25 秒以上、 0.5 秒以下の間隔で転送する。 duration が 0 でな い
場合は、 0 のビットを実装依存の時間間隔で送る。
端末が非同期のシリアルデータ転送モードでない場合、 tcsendbreak() は何も
行わずに戻る。
tcdrain() は fd が行っている出力の転送が完了するまで待つ。
tcflush() は fd が行っているデータの出力でまだ転送されていないもの、 あ
る い は 受 信 したがまだ入力していないものを破棄する。いずれを行うかは
queue_selector の値で定める:
TCIFLUSH
受信したが読んでいないデータをフラッシュする。
TCOFLUSH
書いたが送信していないデータをフラッシュする。
TCIOFLUSH
受信したが読んでいないデータ・書いたが送信していないデータ両方を
フラッシュする。
tcflow() は fd で指定されたオブジェクトにおけるデータの送信あるいは受信
を一時的に中断する。送信と受信のどちらかは、 actionで決まる:
TCOOFF 出力の中断。
TCOON 中断した出力の再開。
TCIOFF STOP 文字の送信。 STOP 文字は端末デバイスからシステムへのデー タ
送信を停止する。
TCION START 文字の送信。 START 文字は端末デバイスからシステムへのデー
タ送信を開始する。
端末ファイルがオープンされたときのデフォルトでは、入力も出力も中断さ れ
ていない。
ライン速度
ボーレート関数は termios 構造体中の入出力ボーレートを取得、設定するため
に提供される。設定された値は tcsetattr() の呼び出しが成功するまでは有効
ではない。
速度を B0 に設定した場合、モデムは停止 (hang up) する。 B38400 に該当す
る実際のビットレートは setserial(8) で変更できる。
入力および出力ボーレートは termios 構造体に格納される。
cfgetospeed() は termios_p が示している termios 構造体に格納されてい る
出力ボーレートを返す。
cfsetospeed() は termios_p で示されている termios 構造体中の出力ボーレ
ートを speed に設定する。値は以下のいずれかでなければならない:
B0
B50
B75
B110
B134
B150
B200
B300
B600
B1200
B1800
B2400
B4800
B9600
B19200
B38400
B57600
B115200
B230400
0ボー (B0) は接続の中断に用いられる。 B0が指定された場合、モデムの制 御
線 は 使 用されない状態になり、一般にはこれで接続が切断される。 CBAUDEX
はPOSIX.1で定義されている速度の範囲外 (57600 およびそれ以上) を設定する
。すなわち例えば B57600 & CBAUDEX は 0 でない。
cfgetispeed() は termios 構造体中の入力ボーレートを返す。
cfsetispeed() は termios 構造体中の入力ボーレートを speed に設定する。
speed には、上述の cfsetospeed() のボーレート定数 Bnnn のいずれか一つを
指定しなければならない。入力ボーレートが 0 に設定された場合、入力ボーレ
ートは出力ボーレートと同じ値となる。
cfsetspeed() は 4.4BSD による拡張である。この関数は cfsetispeed() と 同
じ引き数をとり、入出力両方の速度を設定する。
返り値
cfgetispeed() は termios 構造体中の入力ボーレートを返す。
cfgetospeed() は termios 構造体中の出力ボーレートを返す。
他のすべての関数の戻り値:
0 実行成功。
-1 実行失敗。 errno がエラーの種類を示す。
tcsetattr() は なんらかの 変更要求が成功した場合に成功を返すことに注意
。従って、複数の変更を行った場合には、引き続いて tcgetattr() を呼び出し
て全ての変更が実行されているかを確認する必要があるかもしれない。
準拠
tcgetattr(), tcsetattr(), tcsendbreak(), tcdrain(), tcflush(),
tcflow(), cfgetispeed(), cfgetospeed(), cfsetispeed(), cfsetospeed() は
POSIX.1-2001 で規定されている。
cfmakeraw() と cfsetspeed() は非標準だが、BSD では利用可能である。
注意
Unix V7 とその後のいくつかのシステムでは、ボーレートの 14 個のリストで
ある B0, ..., B9600 の後ろに EXTA, EXTB ("External A" と "External B")
の 2 つを追加している。多くのシステムではさらに高速なボーレートのために
リストを拡張している。
tcsendbreak() で duration に 0 以外を指定した場合の効果は様々であ る 。
SunOS は duration*N 秒のブレークを規定している。ここで N は 0.25 以上
0.5 未満である。 Linux, AIX, DU, Tru64 は duration ミリ秒のブレークを送
信 す る 。 FreeBSD, NetBSD, HP-UX, MacOS は duration の値を無視する。
Solaris と Unixware では、非ゼロの duration を指定した tcsendbreak() の
振る舞いは tcdrain() と同様である。
関連項目
stty(1), console_ioctl(4), tty_ioctl(4), setserial(8)
Linux 2007-11-26 TERMIOS(3)
TERMIOS(3) Linux Programmer’s Manual TERMIOS(3)
NAME
termios, tcgetattr, tcsetattr, tcsendbreak, tcdrain, tcflush, tcflow,
cfmakeraw, cfgetospeed, cfgetispeed, cfsetispeed, cfsetospeed, cfset-
speed - get and set terminal attributes, line control, get and set baud
rate
SYNOPSIS
#include
#include
int tcgetattr(int fd, struct termios *termios_p);
int tcsetattr(int fd, int optional_actions,
const struct termios *termios_p);
int tcsendbreak(int fd, int duration);
int tcdrain(int fd);
int tcflush(int fd, int queue_selector);
int tcflow(int fd, int action);
void cfmakeraw(struct termios *termios_p);
speed_t cfgetispeed(const struct termios *termios_p);
speed_t cfgetospeed(const struct termios *termios_p);
int cfsetispeed(struct termios *termios_p, speed_t speed);
int cfsetospeed(struct termios *termios_p, speed_t speed);
int cfsetspeed(struct termios *termios_p, speed_t speed);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
cfsetspeed(), cfmakeraw(): _BSD_SOURCE
DESCRIPTION
The termios functions describe a general terminal interface that is
provided to control asynchronous communications ports.
The termios structure
Many of the functions described here have a termios_p argument that is
a pointer to a termios structure. This structure contains at least the
following members:
tcflag_t c_iflag; /* input modes */
tcflag_t c_oflag; /* output modes */
tcflag_t c_cflag; /* control modes */
tcflag_t c_lflag; /* local modes */
cc_t c_cc[NCCS]; /* control chars */
The values that may be assigned to these fields are described below.
In the case of the first four bit-mask fields, the definitions of some
of the associated flags that may be set are only exposed if a specific
feature test macro (see feature_test_macros(7)) is defined, as noted in
brackets ("[]").
In the descriptions below, "not in POSIX" means that the value is not
specified in POSIX.1-2001, and "XSI" means that the value is specified
in POSIX.1-2001 as part of the XSI extension.
c_iflag flag constants:
IGNBRK Ignore BREAK condition on input.
BRKINT If IGNBRK is set, a BREAK is ignored. If it is not set but
BRKINT is set, then a BREAK causes the input and output queues
to be flushed, and if the terminal is the controlling terminal
of a foreground process group, it will cause a SIGINT to be sent
to this foreground process group. When neither IGNBRK nor
BRKINT are set, a BREAK reads as a null byte ('\0'), except when
PARMRK is set, in which case it reads as the sequence \377 \0
\0.
IGNPAR Ignore framing errors and parity errors.
PARMRK If IGNPAR is not set, prefix a character with a parity error or
framing error with \377 \0. If neither IGNPAR nor PARMRK is
set, read a character with a parity error or framing error as
\0.
INPCK Enable input parity checking.
ISTRIP Strip off eighth bit.
INLCR Translate NL to CR on input.
IGNCR Ignore carriage return on input.
ICRNL Translate carriage return to newline on input (unless IGNCR is
set).
IUCLC (not in POSIX) Map uppercase characters to lowercase on input.
IXON Enable XON/XOFF flow control on output.
IXANY (XSI) Typing any character will restart stopped output. (The
default is to allow just the START character to restart output.)
IXOFF Enable XON/XOFF flow control on input.
IMAXBEL
(not in POSIX) Ring bell when input queue is full. Linux does
not implement this bit, and acts as if it is always set.
IUTF8 (since Linux 2.6.4)
(not in POSIX) Input is UTF8; this allows character-erase to be
correctly performed in cooked mode.
c_oflag flag constants defined in POSIX.1:
OPOST Enable implementation-defined output processing.
The remaining c_oflag flag constants are defined in POSIX.1-2001,
unless marked otherwise.
OLCUC (not in POSIX) Map lowercase characters to uppercase on output.
ONLCR (XSI) Map NL to CR-NL on output.
OCRNL Map CR to NL on output.
ONOCR Don’t output CR at column 0.
ONLRET Don’t output CR.
OFILL Send fill characters for a delay, rather than using a timed
delay.
OFDEL (not in POSIX) Fill character is ASCII DEL (0177). If unset,
fill character is ASCII NUL ('\0'). (Not implemented on Linux.)
NLDLY Newline delay mask. Values are NL0 and NL1. [requires
_BSD_SOURCE or _SVID_SOURCE or _XOPEN_SOURCE]
CRDLY Carriage return delay mask. Values are CR0, CR1, CR2, or CR3.
[requires _BSD_SOURCE or _SVID_SOURCE or _XOPEN_SOURCE]
TABDLY Horizontal tab delay mask. Values are TAB0, TAB1, TAB2, TAB3
(or XTABS). A value of TAB3, that is, XTABS, expands tabs to
spaces (with tab stops every eight columns). [requires
_BSD_SOURCE or _SVID_SOURCE or _XOPEN_SOURCE]
BSDLY Backspace delay mask. Values are BS0 or BS1. (Has never been
implemented.) [requires _BSD_SOURCE or _SVID_SOURCE or
_XOPEN_SOURCE]
VTDLY Vertical tab delay mask. Values are VT0 or VT1.
FFDLY Form feed delay mask. Values are FF0 or FF1. [requires
_BSD_SOURCE or _SVID_SOURCE or _XOPEN_SOURCE]
c_cflag flag constants:
CBAUD (not in POSIX) Baud speed mask (4+1 bits). [requires
_BSD_SOURCE or _SVID_SOURCE]
CBAUDEX
(not in POSIX) Extra baud speed mask (1 bit), included in CBAUD.
[requires _BSD_SOURCE or _SVID_SOURCE]
(POSIX says that the baud speed is stored in the termios struc-
ture without specifying where precisely, and provides
cfgetispeed() and cfsetispeed() for getting at it. Some systems
use bits selected by CBAUD in c_cflag, other systems use sepa-
rate fields, for example, sg_ispeed and sg_ospeed.)
CSIZE Character size mask. Values are CS5, CS6, CS7, or CS8.
CSTOPB Set two stop bits, rather than one.
CREAD Enable receiver.
PARENB Enable parity generation on output and parity checking for
input.
PARODD If set, then parity for input and output is odd; otherwise even
parity is used.
HUPCL Lower modem control lines after last process closes the device
(hang up).
CLOCAL Ignore modem control lines.
LOBLK (not in POSIX) Block output from a non-current shell layer. For
use by shl (shell layers). (Not implemented on Linux.)
CIBAUD (not in POSIX) Mask for input speeds. The values for the CIBAUD
bits are the same as the values for the CBAUD bits, shifted left
IBSHIFT bits. [requires _BSD_SOURCE or _SVID_SOURCE] (Not
implemented on Linux.)
CMSPAR (not in POSIX) Use "stick" (mark/space) parity (supported on
certain serial devices): if PARODD is set, the parity bit is
always 1; if PARODD is not set, then the parity bit is always
0). [requires _BSD_SOURCE or _SVID_SOURCE]
CRTSCTS
(not in POSIX) Enable RTS/CTS (hardware) flow control.
[requires _BSD_SOURCE or _SVID_SOURCE]
c_lflag flag constants:
ISIG When any of the characters INTR, QUIT, SUSP, or DSUSP are
received, generate the corresponding signal.
ICANON Enable canonical mode (described below).
XCASE (not in POSIX; not supported under Linux) If ICANON is also set,
terminal is uppercase only. Input is converted to lowercase,
except for characters preceded by \. On output, uppercase char-
acters are preceded by \ and lowercase characters are converted
to uppercase. [requires _BSD_SOURCE or _SVID_SOURCE or
_XOPEN_SOURCE]
ECHO Echo input characters.
ECHOE If ICANON is also set, the ERASE character erases the preceding
input character, and WERASE erases the preceding word.
ECHOK If ICANON is also set, the KILL character erases the current
line.
ECHONL If ICANON is also set, echo the NL character even if ECHO is not
set.
ECHOCTL
(not in POSIX) If ECHO is also set, ASCII control signals other
than TAB, NL, START, and STOP are echoed as ^X, where X is the
character with ASCII code 0x40 greater than the control signal.
For example, character 0x08 (BS) is echoed as ^H. [requires
_BSD_SOURCE or _SVID_SOURCE]
ECHOPRT
(not in POSIX) If ICANON and IECHO are also set, characters are
printed as they are being erased. [requires _BSD_SOURCE or
_SVID_SOURCE]
ECHOKE (not in POSIX) If ICANON is also set, KILL is echoed by erasing
each character on the line, as specified by ECHOE and ECHOPRT.
[requires _BSD_SOURCE or _SVID_SOURCE]
DEFECHO
(not in POSIX) Echo only when a process is reading. (Not imple-
mented on Linux.)
FLUSHO (not in POSIX; not supported under Linux) Output is being
flushed. This flag is toggled by typing the DISCARD character.
[requires _BSD_SOURCE or _SVID_SOURCE]
NOFLSH Disable flushing the input and output queues when generating the
SIGINT, SIGQUIT, and SIGSUSP signals.
TOSTOP Send the SIGTTOU signal to the process group of a background
process which tries to write to its controlling terminal.
PENDIN (not in POSIX; not supported under Linux) All characters in the
input queue are reprinted when the next character is read.
(bash(1) handles typeahead this way.) [requires _BSD_SOURCE or
_SVID_SOURCE]
IEXTEN Enable implementation-defined input processing. This flag, as
well as ICANON must be enabled for the special characters EOL2,
LNEXT, REPRINT, WERASE to be interpreted, and for the IUCLC flag
to be effective.
The c_cc array defines the special control characters. The symbolic
indices (initial values) and meaning are:
VINTR (003, ETX, Ctrl-C, or also 0177, DEL, rubout) Interrupt charac-
ter. Send a SIGINT signal. Recognized when ISIG is set, and
then not passed as input.
VQUIT (034, FS, Ctrl-\) Quit character. Send SIGQUIT signal. Recog-
nized when ISIG is set, and then not passed as input.
VERASE (0177, DEL, rubout, or 010, BS, Ctrl-H, or also #) Erase charac-
ter. This erases the previous not-yet-erased character, but
does not erase past EOF or beginning-of-line. Recognized when
ICANON is set, and then not passed as input.
VKILL (025, NAK, Ctrl-U, or Ctrl-X, or also @) Kill character. This
erases the input since the last EOF or beginning-of-line. Rec-
ognized when ICANON is set, and then not passed as input.
VEOF (004, EOT, Ctrl-D) End-of-file character. More precisely: this
character causes the pending tty buffer to be sent to the wait-
ing user program without waiting for end-of-line. If it is the
first character of the line, the read(2) in the user program
returns 0, which signifies end-of-file. Recognized when ICANON
is set, and then not passed as input.
VMIN Minimum number of characters for non-canonical read.
VEOL (0, NUL) Additional end-of-line character. Recognized when
ICANON is set.
VTIME Timeout in deciseconds for non-canonical read.
VEOL2 (not in POSIX; 0, NUL) Yet another end-of-line character. Rec-
ognized when ICANON is set.
VSWTCH (not in POSIX; not supported under Linux; 0, NUL) Switch charac-
ter. (Used by shl only.)
VSTART (021, DC1, Ctrl-Q) Start character. Restarts output stopped by
the Stop character. Recognized when IXON is set, and then not
passed as input.
VSTOP (023, DC3, Ctrl-S) Stop character. Stop output until Start
character typed. Recognized when IXON is set, and then not
passed as input.
VSUSP (032, SUB, Ctrl-Z) Suspend character. Send SIGTSTP signal.
Recognized when ISIG is set, and then not passed as input.
VDSUSP (not in POSIX; not supported under Linux; 031, EM, Ctrl-Y)
Delayed suspend character: send SIGTSTP signal when the charac-
ter is read by the user program. Recognized when IEXTEN and
ISIG are set, and the system supports job control, and then not
passed as input.
VLNEXT (not in POSIX; 026, SYN, Ctrl-V) Literal next. Quotes the next
input character, depriving it of a possible special meaning.
Recognized when IEXTEN is set, and then not passed as input.
VWERASE
(not in POSIX; 027, ETB, Ctrl-W) Word erase. Recognized when
ICANON and IEXTEN are set, and then not passed as input.
VREPRINT
(not in POSIX; 022, DC2, Ctrl-R) Reprint unread characters.
Recognized when ICANON and IEXTEN are set, and then not passed
as input.
VDISCARD
(not in POSIX; not supported under Linux; 017, SI, Ctrl-O) Tog-
gle: start/stop discarding pending output. Recognized when IEX-
TEN is set, and then not passed as input.
VSTATUS
(not in POSIX; not supported under Linux; status request: 024,
DC4, Ctrl-T).
These symbolic subscript values are all different, except that VTIME,
VMIN may have the same value as VEOL, VEOF, respectively. In non-
canonical mode the special character meaning is replaced by the timeout
meaning. For an explanation of VMIN and VTIME, see the description of
non-canonical mode below.
Retrieving and changing terminal settings
tcgetattr() gets the parameters associated with the object referred by
fd and stores them in the termios structure referenced by termios_p.
This function may be invoked from a background process; however, the
terminal attributes may be subsequently changed by a foreground pro-
cess.
tcsetattr() sets the parameters associated with the terminal (unless
support is required from the underlying hardware that is not available)
from the termios structure referred to by termios_p. optional_actions
specifies when the changes take effect:
TCSANOW
the change occurs immediately.
TCSADRAIN
the change occurs after all output written to fd has been trans-
mitted. This function should be used when changing parameters
that affect output.
TCSAFLUSH
the change occurs after all output written to the object
referred by fd has been transmitted, and all input that has been
received but not read will be discarded before the change is
made.
Canonical and non-canonical mode
The setting of the ICANON canon flag in c_lflag determines whether the
terminal is operating in canonical mode (ICANON set) or non-canonical
mode (ICANON unset). By default, ICANON set.
In canonical mode:
* Input is made available line by line. An input line is available
when one of the line delimiters is typed (NL, EOL, EOL2; or EOF at
the start of line). Except in the case of EOF, the line delimiter is
included in the buffer returned by read(2).
* Line editing is enabled (ERASE, KILL; and if the IEXTEN flag is set:
WERASE, REPRINT, LNEXT). A read(2) returns at most one line of
input; if the read(2) requested fewer bytes than are available in the
current line of input, then only as many bytes as requested are read,
and the remaining characters will be available for a future read(2).
In non-canonical mode input is available immediately (without the user
having to type a line-delimiter character), and line editing is dis-
abled. The settings of MIN (c_cc[VMIN]) and TIME (c_cc[VTIME]) deter-
mine the circumstances in which a read(2) completes; there are four
distinct cases:
* MIN == 0; TIME == 0: If data is available, read(2) returns immedi-
ately, with the lesser of the number of bytes available, or the num-
ber of bytes requested. If no data is available, read(2) returns 0.
* MIN > 0; TIME == 0: read(2) blocks until the lesser of MIN bytes or
the number of bytes requested are available, and returns the lesser
of these two values.
* MIN == 0; TIME > 0: TIME specifies the limit for a timer in tenths of
a second. The timer is started when read(2) is called. read(2)
returns either when at least one byte of data is available, or when
the timer expires. If the timer expires without any input becoming
available, read(2) returns 0.
* MIN > 0; TIME > 0: TIME specifies the limit for a timer in tenths of
a second. Once an initial byte of input becomes available, the timer
is restarted after each further byte is received. read(2) returns
either when the lesser of the number of bytes requested or MIN byte
have been read, or when the inter-byte timeout expires. Because the
timer is only started after the initial byte becomes available, at
least one byte will be read.
Raw mode
cfmakeraw() sets the terminal to something like the "raw" mode of the
old Version 7 terminal driver: input is available character by charac-
ter, echoing is disabled, and all special processing of terminal input
and output characters is disabled. The terminal attributes are set as
follows:
termios_p->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON);
termios_p->c_oflag &= ~OPOST;
termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
termios_p->c_cflag &= ~(CSIZE | PARENB);
termios_p->c_cflag |= CS8;
Line control
tcsendbreak() transmits a continuous stream of zero-valued bits for a
specific duration, if the terminal is using asynchronous serial data
transmission. If duration is zero, it transmits zero-valued bits for
at least 0.25 seconds, and not more that 0.5 seconds. If duration is
not zero, it sends zero-valued bits for some implementation-defined
length of time.
If the terminal is not using asynchronous serial data transmission,
tcsendbreak() returns without taking any action.
tcdrain() waits until all output written to the object referred to by
fd has been transmitted.
tcflush() discards data written to the object referred to by fd but not
transmitted, or data received but not read, depending on the value of
queue_selector:
TCIFLUSH
flushes data received but not read.
TCOFLUSH
flushes data written but not transmitted.
TCIOFLUSH
flushes both data received but not read, and data written but
not transmitted.
tcflow() suspends transmission or reception of data on the object
referred to by fd, depending on the value of action:
TCOOFF suspends output.
TCOON restarts suspended output.
TCIOFF transmits a STOP character, which stops the terminal device from
transmitting data to the system.
TCION transmits a START character, which starts the terminal device
transmitting data to the system.
The default on open of a terminal file is that neither its input nor
its output is suspended.
Line speed
The baud rate functions are provided for getting and setting the values
of the input and output baud rates in the termios structure. The new
values do not take effect until tcsetattr() is successfully called.
Setting the speed to B0 instructs the modem to "hang up". The actual
bit rate corresponding to B38400 may be altered with setserial(8).
The input and output baud rates are stored in the termios structure.
cfgetospeed() returns the output baud rate stored in the termios struc-
ture pointed to by termios_p.
cfsetospeed() sets the output baud rate stored in the termios structure
pointed to by termios_p to speed, which must be one of these constants:
B0
B50
B75
B110
B134
B150
B200
B300
B600
B1200
B1800
B2400
B4800
B9600
B19200
B38400
B57600
B115200
B230400
The zero baud rate, B0, is used to terminate the connection. If B0 is
specified, the modem control lines shall no longer be asserted. Nor-
mally, this will disconnect the line. CBAUDEX is a mask for the speeds
beyond those defined in POSIX.1 (57600 and above). Thus, B57600 &
CBAUDEX is non-zero.
cfgetispeed() returns the input baud rate stored in the termios struc-
ture.
cfsetispeed() sets the input baud rate stored in the termios structure
to speed, which must be specified as one of the Bnnn constants listed
above for cfsetospeed(). If the input baud rate is set to zero, the
input baud rate will be equal to the output baud rate.
cfsetspeed() is a 4.4BSD extension. It takes the same arguments as
cfsetispeed(), and sets both input and output speed.
RETURN VALUE
cfgetispeed() returns the input baud rate stored in the termios struc-
ture.
cfgetospeed() returns the output baud rate stored in the termios struc-
ture.
All other functions return:
0 on success.
-1 on failure and set errno to indicate the error.
Note that tcsetattr() returns success if any of the requested changes
could be successfully carried out. Therefore, when making multiple
changes it may be necessary to follow this call with a further call to
tcgetattr() to check that all changes have been performed successfully.
CONFORMING TO
tcgetattr(), tcsetattr(), tcsendbreak(), tcdrain(), tcflush(),
tcflow(), cfgetispeed(), cfgetospeed(), cfsetispeed(), and cfse-
tospeed() are specified in POSIX.1-2001.
cfmakeraw() and cfsetspeed() are non-standard, but available on the
BSDs.
NOTES
Unix V7 and several later systems have a list of baud rates where after
the fourteen values B0, ..., B9600 one finds the two constants EXTA,
EXTB ("External A" and "External B"). Many systems extend the list
with much higher baud rates.
The effect of a non-zero duration with tcsendbreak() varies. SunOS
specifies a break of duration * N seconds, where N is at least 0.25,
and not more than 0.5. Linux, AIX, DU, Tru64 send a break of duration
milliseconds. FreeBSD and NetBSD and HP-UX and MacOS ignore the value
of duration. Under Solaris and Unixware, tcsendbreak() with non-zero
duration behaves like tcdrain().
SEE ALSO
stty(1), console_ioctl(4), tty_ioctl(4), setserial(8)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2007-11-26 TERMIOS(3)