CONSOLE_CODES(4) Linux Programmer’s Manual CONSOLE_CODES(4) 名前 console_codes - Linux コンソールのエスケープシーケンスとコントロールシ ーケンス 説明 Linux コンソールは VT102 と ECMA-48/ISO6429/ANSI X3.64 端末コンソールの サ ブセットに、カラーパレットや文字集合のマッピングなどを変更するプライ ベートモードシーケンスを加えて実装されている。以下の表で は 、 機 能 の ECMA-48 または DEC のニーモニクス(DECで始まれば後者)を 2 番目の桁に記す 。ニーモニクスのないものは ECMA-48 でも VT102 でもないものである。 すべての出力作業が終って、実際に印字するために文字のストリームがコン ソ ー ルドライバーに届いた時、最初に行なわれるのが、処理用コードから印字用 のコードへの変換である。 もし、コンソールが UTF-8 モードなら、入って来たバイト(byte)は 16 ビット の Unicode に組み立てられる。そうでなければ、それぞれのバイトは現在の( 各バイトを Unicode の値に変換する)マッピングテーブルに従って変換され る 。これに関しては下のキャラクターセット(Charcter Sets) 節を参照のこと。 通常、Unicode の値はフォントインデックス(font index) に変換され、ビデオ メモリに蓄えられる。そして(ビデオ ROM 中に見付けられる) 対応す る グ リ フ(glyph)が画面に現れる。 Unicode を用いると、(PC 互換機の設計では) 512 の異なったグリフを同時に利用できるようになる。 現在の Unicode の値がコントロール文字であるかエスケープシーケンスを処理 し ている時には、その値は特別に扱われる。フォントのインデックスに変換さ れてグリフとして表示される代わりに、カーソルの移動やその他のコントロ ー ル 機能を実行させる。詳しいことは、後述の「LINUX コンソールコントロール 」のセクションを参照のこと。 一般に端末コントロールコードをプログラム中に直接記述するのは良いこと で はない。 Linuxでは、端末において実現可能な操作のデータベースである ter- minfo(5) をサポートしている。コンソールエスケープシーケンスをわざわざ入 力 す る よ り も、ほとんどの場合 terminfo の情報を参照する ncurses(3), tput(1), や reset(1) などのスクリーンライブラリやユーティリティーを使い たいと思うであろう。 Linux コンソールコントロール こ の章では Linux コンソールにおいて特殊な機能(つまり、現在のカーソル位 置に文字のイメージを印字する以外のこと)を起こさせる全てのコントロール文 字とエスケープシーケンスについて述べる。 コントロール文字 コ ン ト ロ ール文字とは、(マッピングテーブルによる変換前のコードが) 00 (NUL), 07 (BEL), 08 (BS), 09 (HT), 0a (LF), 0b (VT), 0c (FF), 0d (CR), 0e (SO), 0f (SI), 18 (CAN), 1a (SUB), 1b (ESC), 7f (DEL) の 14 のコード のうちのどれかである文字である。 "コントロール文字表示(display control characters)"モード(後述)を設定すると、07, 09, 0b, 18, 1a, 7f をグリフと して表示することができる。一方、 UTF-8 モードでは、00-1f の全てのコード は 、コントロール文字表示モードが指定された場合でもコントロール文字とし て扱われる。 コントロール文字がある時には、それはすぐに(たとえエスケープシーケンスの 途中でも)実行され、破棄される。エスケープシーケンスは次の文字から続けら れる (しかし、ESC は新しいエスケープシーケンスを始めるので、前の終了 し て い な いエスケープシーケンスが中止される可能性がある。さらに、CAN と SUB はどんなエスケープシーケンスも中止する)。認められているコントロール 文字は、BEL, BS, HT, LF, VT, FF, CR, SO, SI, CAN, SUB, ESC, DEL, CSI で ある。これらは期待どおりの動作をする: BEL (0x07, ^G) はビープ音を鳴らす; BS (0x08, ^H) はバックスペース (ただし、行頭は越えない); HT (0x09, ^I) は次のタブストップ(tab stop)へ移動する、そこから行末ま で にタブストップが無い場合は行の終りに移動する; LF (0x0A, ^J), VT (0x0B, ^K), FF (0x0C, ^L) これらは全てラインフィー ド(linefeed)を与える; LF/NL (ニューラインモード(new line mode)) がセットされていればキャリッジリターンもアクティブにする; CR (0x0D, ^M) はキャリッジリターンを与える; SO (0x0E, ^N) は G1 文字集合をアクティブにする。 SI (0x0F, ^O) は G0 文字集合をアクティブにする; CAN (0x18, ^X) と SUB (0x1A, ^Z) はエスケープシーケンスを中断する; ESC (0x1B, ^[) はエスケープシーケンスを始める。 DEL (0x7F) は無視される; CSI (0x9B) は ESC [ と等価。 エスケープシーケンスであるが CSI シーケンスでないもの ESC c RIS リセット。 ESC D IND ラインフィード。 ESC E NEL ニューライン。 ESC H HTS 現在の桁の位置にタブストップを設定する。 ESC M RI 逆ラインフィード ESC Z DECID DEC固有の識別用。カーネルは文字列 ESC [ ? 6 c を返 す。これは端末がVT102であることを意味する。 ESC 7 DECSC 現在の状態(カーソルの座標、属性、G0, G1 で示してい る文字集合)をセーブする。 ESC 8 DECRC 最後に ESC 7 でセーブした状態を復帰させる。 ESC [ CSI コントロールシーケンスを導入する。 ESC % キャラクターセットを選択するシーケンスを開始する。 ESC % @ デフォルト(ISO 646 / ISO 8859-1)を選択する。 ESC % G UTF-8 を選択する。 ESC % 8 UTF-8 を選択する(旧式)。 ESC # 8 DECALN DEC のスクリーン調整テスト - スクリーンを E で うめる。 ESC ( G0 文字集合を定義するシーケンスを開始する。 ESC ( B デフォルト(ISO 8859-1 マッピング)を選択する。 ESC ( 0 VT100 グラフィクスマッピングを選択する。 ESC ( U ヌルマッピングを選択する - キャラクタ ROM から 直接マッピングする。 ESC ( K ユーザー定義のマッピングを選択する - そのマップ は mapscrn(8) ユーティリティーによってロー ドされる。 ESC ) G1 を定義するシーケンスを開始する。 (すでに述べたように B, 0, U, K のどれかが次に続く) ESC > DECPNM 数値キーパッドモード(numeric keypad mode)を セットする。 ESC = DECPAM アプリケーションキーパッドモード(application keypad mode) をセットする。 ESC ] OSC ("Operating system command"のことだろう) ESC ] P nrrggbb: 最後の P のあとの 7 つの 16 進数をパラメー タとして :-( パレットをセットする。ここで、nは色 (0-15)、rrggbbは赤/緑/青の値 (0-255)を意味する。 ESC ] R: パレットをリセットする。 ECMA-48 CSI シーケンス CSI (または ESC [ ) のあとに、最大で NPAR (16) 個のセミコロンで区切られ た 10 進数のパラメータシーケンスが続く。空であるかそこにないパラメー タ は 0 とされる。パラメータシーケンスの前にはひとつのクエスチョンマークが つくことがある。 しかし、CSI [ (または ESC [ [ ) の後では、一文字が読まれ、このすべて の パラメータシーケンスが無視される(要するにファンクションキーのエコーは無 視されるということである)。 CSI シーケンスの動作は、その最後の文字によって決まる。 @ ICH 指示された数の空白文字を挿入する。 A CUU 指示された数だけカーソルを上方向に移動する。 B CUD カーソルを指示された数の行だけ下方向に移動する。 C CUF カーソルを指示された数の桁だけ右に移動する。 D CUB カーソルを指示された数の桁だけ左に移動する。 E CNL カーソルを指示された数の行だけ下の第 1 桁に移動する。 F CPL カーソルを指示された数の行だけ上の第 1 桁に移動する。 G CHA カーソルを現在の行の指示された桁に移動する。 H CUP カーソルを指示された行、桁(1,1を原点とする)に 移動する。 J ED ディスプレイの消去(デフォルト: カーソルからディスプレイの 最後まで)。 ESC [ 1 J: 最初からカーソルまでの消去。 ESC [ 2 J: ディスプレイ全体の消去。 K EL 行の消去(デフォルト: カーソルから行末まで)。 ESC [ 1 K: 行頭からカーソルまでの消去。 ESC [ 2 K: 行全体の消去。 L IL 指示された数の空行を挿入する。 M DL 指示された数の行を削除する。 P DCH 現在の行から指示された数の文字を削除する。 X ECH 現在の行から指示された数の文字を消去する。 a HPR カーソルを指示された数の桁だけ右に移動する。 c DA ESC [ ? 6 c を返す: "私はVT102です"(ということ)。 d VPA カーソルを指示された行の現在の桁に移動する。 e VPR カーソルを指示された行数だけ下に移動する。 f HVP カーソルを指示された行、桁に移動する。 g TBC パラメータなしの時: 現在位置のタブストップを削除する。 ESC [ 3 g: すべてのタブストップを削除する。 h SM モードのセット(後述)。 l RM モードのリセット(後述)。 m SGR 属性のセット(後述)。 n DSR 状態の報告(後述)。 q DECLL キーボードの LED をセットする。 ESC [ 0 q: すべての LED を消す。 ESC [ 1 q: スクロールロック LED を点灯。 ESC [ 2 q: ナンバーロック LED を点灯。 ESC [ 3 q: キャピタルロック LED を点灯。 r DECSTBM スクロールの範囲のセット; パラメータは一番上の行と 一番下の行。 s ? カーソル位置の保存。 u ? カーソル位置の復帰。 ` HPA カーソルを現在の行の指示された桁に移動する。 ECMA-48 グラフィクスレンディション(Graphics Rendition)の設定 ECMA-48 SGR シーケンス ESC [ <パラメータ> m は表示属性を設定する。セ ミ コ ロンで区切ることで、同じシーケンスでいくつかの属性を設定できる。空パ ラメータ(セミコロンか文字列開始文字か文字列終端文字の間)はゼロと解釈 さ れる。 パラメータ 結果 0 すべての属性をデフォルトにリセットする。 1 ボールド(bold)をセット。 2 ハーフブライト(half-bright)(カラーディスプレイでは色で代用)を セット。 4 下線(underscore)をセット(カラーディスプレイでは色で代用)。 (ディム や下線を代用するのに使われる色は ESC ] ... によりセット) 5 点滅(blink)をセット。 7 反転表示(reverce video)をセット。 10 選択したマッピング、ディスプレイコントロールフラグ(display control flag)、トグルメタフラグ(toggle meta flag)をリセットする (ECMA-48で は"primary font"と呼んでいる)。 11 ヌルマッピングを選択、ディスプレイコントロールフラグをセット、トグ ルメタフラグをリセットする (ECMA-48 では"first alternate font"と呼 んでいる)。 12 ヌルマッピングを選択、ディスプレイコントロールフラグ、トグルメタフ ラグをセットする (ECMA-48 では "second alternate font" と呼んでい る)。トグルメタフラグがたっていると、マッピングテーブルによる変換を する前に、バイトの上位の1ビットがトグルされる。 21 通常の輝度にセット(ECMA-48 では "doubly underlined" と呼んでいる)。 22 通常の輝度にセット。 24 下線オフ。 25 点滅オフ。 27 反転表示オフ。 30 フォアグラウンド(foreground)を黒にセット。 31 フォアグラウンドを赤にセット。 32 フォアグラウンドを緑にセット。 33 フォアグラウンドを茶にセット。 34 フォアグラウンドを青にセット。 35 フォアグラウンドをマゼンダにセット。 36 フォアグラウンドをシアンにセット。 37 フォアグラウンドを白にセット。 38 下線表示に設定し、フォアグラウンドをデフォルトにセット。 39 下線表示を解除し、フォアグラウンドをデフォルトにセット。 40 バックグラウンド(background)を黒にセット。 41 バックグラウンドを赤にセット。 42 バックグラウンドを緑にセット。 43 バックグラウンドを茶にセット。 44 バックグラウンドを青にセット。 45 バックグラウンドをマゼンダにセット。 46 バックグラウンドをシアンにセット。 47 バックグラウンドを白にセット。 49 バックグラウンドをデフォルトにセット。 ECMA-48 モードスイッチ(Mode Switches) ESC [ 3 h DECCRM (デフォルトではオフ): コントロール文字を表示する。 ESC [ 4 h DECIM (デフォルトではオフ): 挿入モードにする。 ESC [ 20 h LF/NL (デフォルトではオフ): LF, VT, FFをエコーしたあと自動的 CR をつける。 ECMA-48 状態リポートコマンド(Status Report Commands) ESC [ 5 n デバイス状態のリポート(DSR): 返事は ESC [ 0 n (端末 OK). ESC [ 6 n カーソル位置のリポート(CPR): 返事は ESC [ y ; x R、 x,y はカーソ ルの位置をあらわす。 DEC プライベートモード (DECSET/DECRST) シーケンス これらは ECMA-48 では記述されていない。ここでは、セットモードシーケンス (Set Mode sequences)を記載する; 最後の 'h' を 'l' に置き換えるとリセ ッ トモードシーケンス(Reset Mode sequences)になる。 ESC [ ? 1 h DECCKM (デフォルトはオフ): セットされた時にはカーソルキーは ESC [ ではなく ESC O を前につけて送る。 ESC [ ? 3 h DECCOLM (デフォルトはオフ = 80 桁): 80/132 の桁モード切替え。 ド ライバーのソースの注釈には、これだけでは十分でなく resizecons(8) のようなユーザーモードのユーティリティーで、コンソールビデオカー ドのハードウェアレジスタを変える必要があると書かれている。 ESC [ ? 5 h DECSCNM (デフォルトはオフ): 反転表示モードのセット。 ESC [ ? 6 h DECOM( デフォルトはオフ): セットされた時には、カーソルのアドレッ シングが、スクロール範囲の左上隅からの相対位置になる。 ESC [ ? 7 h DECAWM(デフォルトはオン): オートラップを設定。このモードの 時 は 、80 桁 (DECCOLM がオンのときは 132 桁)を超えたグラフィックキャ ラクタは、強制的に次の行の先頭に折り返されて表示される。 ESC [ ? 8 h DECARM(デフォルトはオン): キーボードのオートリピートをオンにセッ ト。 ESC [ ? 9 h X10 マウスリポート(デフォルトはオフ): リポートモードを 1 にセッ ト(または、 0 にリセット) — 後述 ESC [ ? 25 h DECTECM (デフォルトはオン): カーソルを可視(visible)にする。 ESC [ ? 1000 h X11 マウスリポート(デフォルトはオフ): リポートモードを 2 にセ ッ ト(または、 0にリセット) — 後述 Linux コンソールプライベート CSI シーケンス 以 下のシーケンスは ECMA-48 のものでも本来の VT102 のものでもでもなく、 Linuxコンソールドライバーに固有なシーケンスである。色は SGR パラメー タ で 表現される: 0 = 黒, 1= 赤, 2 = 緑, 3 = 茶, 4 = 青, 5 = マゼンタ, 6 = シアン, 7 = 白 ESC [ 1 ; n ] 下線の色をn にセットする。 ESC [ 2 ; n ] ディムの色をn にセットする。 ESC [ 8 ] 現在の色のペアをデフォルト属性にする。 ESC [ 9 ; n ] スクリーンブランク(screen blank)のタイムアウト を n 分にセットする。 ESC [ 10 ; n ] ベルの周波数(Hz)をセットする。 ESC [ 11 ; n ] ベルの鳴っている時間(msec)をセットする。 ESC [ 12 ; n ] 指定のコンソールを前面に持ってくる。 ESC [ 13 ] スクリーンをアンブランク(Unblank)する。 ESC [ 14 ] VESA電源停止インターバル(VESA powerdown interval)をセットする。 文字集合 カーネルは、バイト列からコンソールスクリーン符号の変換を 4 つ知っている 。 4 つの変換テーブルとは、a) Latin1 -> PC, b) VT100 graphics -> PC, c) PC -> PC, d) ユーザー定義, である。 G0 と G1 と呼ばれる二つの文字集合があり、そのうち一つが現在の文字集合で あ る(初期値は G0 )。 ^N をタイプすると G1 が ^O を入力すると G0 が現在 の文字集合になる。 変数 G0 と G1 は変換テーブルを指しており、ユーザーにより変更できる。 最 初 はそれぞれテーブル a) と テーブル b) を指している。 ESC ( B 、 ESC ( 0 、 ESC ( U 、 ESC ( K のそれぞれのシーケンスにより、 G0 が変換テー ブ ル a)、 b)、 c)、 d) を指すようになる。また、ESC ) B 、 ESC ) 0 、 ESC ) U 、 ESC ) K のそれぞれのシーケンスにより、G1 が変換テーブル a)、 b) 、 c)、 d) を指すようになる。 ESC c のシーケンスは端末をリセットする。スクリーンがめちゃくちゃになっ た時にそうすることが必要である。よくアドバイスされる "echo ^V^O" は G0 を現在の文字集合にするだけであり、G0 がテーブル a) を指しているという保 証はない。いくつかのディストリビューションには、 reset(1) というプロ グ ラ ムが含まれるが、これはただ "echo ^[c" を実行するものである。もし、コ ンソールの terminfo エントリーが正しい(かつ rs1=\Ec のエントリーがある) ならば、"tput reset"でも同じ効果がある。 ユ ーザー定義のマッピングテーブルは mapscrn(8) を使って定義できる。マッ ピングの結果、シンボル c が印字されるとシンボル s = map[c] がビデオメモ リ に送られる。s に対応するビットマップはキャラクター ROM にあり、 set- font(8) により変更可能である。 マウストラッキング マウストラッキング機能は、 xterm(1)-互換のマウスス テ ー タ ス リ ポ ー ト(mouse status reports)を返させるためのものである。コンソールドライバ ーはマウスのデバイスや種類について知る方法がないので、仮想ターミナル ド ライバーがマウス更新の ioctl を受け取った時だけ、マウスステータスリポー トがコンソールの入力ストリームに送られる。この ioctl は、 gpm(8) デーモ ン のようなマウス対応のユーザーモードアプリケーションが発生しなければな らない。 xterm(1) によって生成される全てのマウス追跡エスケープシーケンスのための パラメータは、数値を value+040 のように符号化し、一つの文字としてあらわ す。例えば、'!' は 1 になる。スクリーン座標は 1 をベースにする。 X10 互換モードでは、ボタンが押された時にマウスの位置と押されたマウス の ボ タンとをエンコードしたエスケープシーケンスを送る。この機能は ESC [ ? 9 h を送ると有効になり ESC [ ? 9 l により無効になる。ボタンが押されると xterm(1) は ESC [ M bxy (の 6 文字)を送る。ここで b は button-1, x と y は マウスがボタンが押された時の x と y 座標である。このコードはカーネル が発生するのと同じコードである。 ノ ーマルトラッキングモード(Normal tracking mode)(Linux 2.0.24 では実装 されていない)では、両方のボタンが押されたか離された時にエスケープシーケ ンスが送られる。モディファイアの情報も一緒に送られる。この機能は、ESC [ ? 1000 h を送ると有効になり ESC [ ? 1000 l で無効になる。ボタンが押され る か離されるかした時には、xterm(1) は ESC [ M bxy を送る。 b の低位の2 ビットにはボタン情報がエンコードされる: 0=MB1 が押された, 1=MB2 が押 さ れ た, 2=MB3 が押された, 3=離された。高位のビットには、ボタンが押された 時にどのモディファイアがダウンしていたかがエンコードさ れ る: 4=Shift, 8=Meta, 16=Control。そして、上位と下位ビットが加算される。ここでも x と y は、マウスイベントが起こった時の x と y 座標であり、左上の隅が(1,1)で ある。 他のターミナルとの比較 多くの異なるターミナルタイプが、Linux コンソールのように、"VT100互換"を 名乗っている。ここでは、Linux コンソールと 2 つの最も重要なターミナルで ある DEC VT102 と xterm(1) との違いについて述べる。 コントロール文字の取り扱い VT102 は以下のコントロール文字も認識する: NUL (0x00) は無視される; ENQ (0x05) はアンサーバックメッセージ(answerback message)を発生する; DC1 (0x11, ^Q, XON) は送信を再開する; DC3 (0x13, ^S, XOFF) は VT100 に XOFF と XON 以外のコードを無視(そして 送信の停止)を起こさせる。 tty ドライバーにより VT100 に似た DC1/DC3 処理をできる。 xterm(1) (VT100 モード)は BEL, BS, HT, LF, VT, FF, CR, SO, SI, ESC のコ ントロール文字を認識する。 エスケープシーケンス Linux コンソールで実装されていない VT100 コンソールシーケンスは以下の通 り: ESC N SS2 シングルシフト 2 (次の文字だけ G2 文字集合を選択する)。 ESC O SS3 シングルシフト 3 (次の文字だけ G3 文字集合を選択する)。 ESC P DCS デバイス制御文字列 (ESC \ で終わる) ESC X SOS 文字列の始まり。 ESC ^ PM プライバシーメッセージ(ESC \ で終わる)。 ESC \ ST 文字列の終端文字。 ESC * ... G2 文字集合を指定する。 ESC + ... G3 文字集合を指定する。 xterm(1) (VT100 モード)は ESC c, ESC # 8, ESC >, ESC =, ESC D, ESC E, ESC H, ESC M, ESC N, ESC O, ESC P ... ESC \, ESC Z を認識する("わたしは 高等ビデオオプション付きの VT100 です"という意味で ESC [ ? 1 ; 2 c と返 答 する)、ESC ^ ... ESC \ は上述と同じ意味をあらわす。ESC (, ESC ), ESC *, ESC + に続く 0, A, B を DEC 特殊文字として受け、それぞれラインドロー イング(line drawing) のセット、UK、 US-ASCII をあらわす。 ユーザーは xterm(1) が VT220 特有のコントロールシーケンスに反応するよう に設定でき、また設定と初期化のされかたによって自分自身を VT52, VT100 な どと認識する。 xterm は 、 特 定 のリソースの設定のために ESC ] (OSC) を受け付ける。 ECMA-48 の文字列終端文字 (ST) に加えて、 xterm(1) は BEL を OSC 文字 列 を終端するものとして受け付ける。以下は xterm(1) が認識する OSC コントロ ールシーケンスの一部である。 ESC ] 0 ; txt ST アイコン名とウインドウタイトルを txt に セットする。 ESC ] 1 ; txt ST アイコン名を txt にセットする。 ESC ] 2 ; txt ST ウインドウタイトルを txt にセットする。 ESC ] 4 ; num; txt ST ANSI 色 num を txt にセットする。 ESC ] 10 ; txt ST 動的テキスト色を txt にセットする。 ESC ] 4 6 ; name ST チェンジログファイルを name に変更する(通常は コンパイル時オプションにより無効になっている)。 ESC ] 5 0 ; fn ST フォントを fn にセットする。 以下のものは、少し違った意味にとられる (より多くの状態を保存し 、 よ り VT100/VT220 に近いふるまいをする): ESC 7 DECSC カーソルの保存。 ESC 8 DECRC カーソルの復元。 また、次のものも認識する: ESC F カーソルをスクリーンの左下に移動する。 (xterm(1) の hpLowerleftBugCompatリソースにより有効な時) ESC l (HP ターミナル毎に)メモリロック。 カーソルより上のメモリをロックする。 ESC m (HP ターミナル毎に)メモリロックを解除する。 ESC n LS2 G2 文字集合の呼び出し。 ESC o LS3 G3 文字集合の呼び出し。 ESC | LS3R G3 文字集合を GR として呼び出す。 ESC } LS2R G2 文字集合を GR として呼び出す。 ESC ~ LS1R G1 文字集合を GR として呼び出す。 ま た ESC % を認識し、Linux コンソールより更に完全な UTF-8 実装を提供す る。 CSI シーケンス X11R5 由来のような、古いバージョンの xterm(1) はブリンク SGR をボールド SGR として解釈する。 1995 年の XFree86 3.1.2A のような、ANSI カラーが実 装された、より新しいバージョンでは、ブリンク属性を色として表示するこ と に よってこれを改善している。最近のバージョンの xterm はブリンク SGR を テキストをブリンクさせることで実装し、さらにまた、SGR 表示の代替案と し て色付きのテキストも利用できる。 Stock X11R6 版では、XFree86 xterm が組 み入れられた X11R6.8 リリースまで色設定 SGR を認識しなかった。 Linux が 認 識 す る他のすべての ECMA-48 CSI シーケンスは xterm でも認識されるが 、xterm(1) は Linux が認識しないいくつかの ECMA-48 と DEC のコントロ ー ルシーケンスも実装している。 xterm(1) は上述のすべての DEC プライベートモードのシーケンスを認識する が、 Linux プライベートモードのシーケンスはどれも認識しない。 xterm(1) 自身のプライベートモードシーケンスに関しての議論は、 X 配布とともに入手 可能な Edward Moy, Stephen Gildea,Thomas E. Dickey による Xterm Control Sequences ドキュメントを参照されたい。このドキュメントは、簡潔なもので あるが、このマニュアルページより遥かに長いものである。年代順の概観と し ては、 http://invisible-island.net/xterm/xterm.log.html には xterm の変更の詳細がある。 vttest は http://invisible-island.net/vttest/ で 入 手 でき、これらのコントロールシーケンスの多くに関するデモを行う。 xterm(1) ソース配布パッケージにはその他の機能を学ぶことが出来るサンプル スクリプトが入っている。 注意 ESC 8 (DECRC) は ESC % で変更された文字集合を復元することはできない。 バグ 2.0.23 では CSI が壊れていて、エスケープシーケンス中の NUL が無視されな い。 古いバージョン(2.0 以降)のカーネルには、8 ビット制御シーケンスを解釈 す る。これらの "C1 コントロール" は ESC [, ESC ] および同様な制御シーケン ス起動子を置き換えるために 128 から 159 のコードを使う。新しいカーネ ル で は (UTF-8 対応の変更時に見落とされたか壊れたために) 寸断しているが、 実装は不完全で信頼できないものと評価されている。 Linux "プライベートモード" シーケンスは ECMA-48 のプライベートモード コ ントロールシーケンスのルールに従っていない。特に、 ] で終わるものは標準 終端文字を使えない。 OSC(パレット設定)シーケンスは大きな問題が あ る 。 xterm(1) はこれを文字列終端文字 (ST) が必要なコントロールシーケンスと解 釈するかもしれないからである。 (不正なコントロールシーケンスなので)無視 さ れる setterm(1) シーケンスと違い、パレットシーケンスは xterm(1) をハ ングさせるかもしれない (しかしリターンキーを押すことで回復 で き る) 。 Linux コントロールシーケンスをハードコードしているアプリケーションに適 応させるには、xterm(1) リソースの brokenLinuxOSC を真 (true) に設定する 。 このドキュメントの古いバージョンでは、Linux が ECMA-48 の不可視テキスト のコントロールシーケンスを認識するかのように書かれていた。これは無視 さ れる。 関連項目 console(4), console_ioctl(4), charsets(7) Linux 2008-01-01 CONSOLE_CODES(4)
CONSOLE_CODES(4) Linux Programmer’s Manual CONSOLE_CODES(4) NAME console_codes - Linux console escape and control sequences DESCRIPTION The Linux console implements a large subset of the VT102 and ECMA-48/ISO 6429/ANSI X3.64 terminal controls, plus certain private- mode sequences for changing the color palette, character-set mapping, etc. In the tabular descriptions below, the second column gives ECMA-48 or DEC mnemonics (the latter if prefixed with DEC) for the given function. Sequences without a mnemonic are neither ECMA-48 nor VT102. After all the normal output processing has been done, and a stream of characters arrives at the console driver for actual printing, the first thing that happens is a translation from the code used for processing to the code used for printing. If the console is in UTF-8 mode, then the incoming bytes are first assembled into 16-bit Unicode codes. Otherwise each byte is trans- formed according to the current mapping table (which translates it to a Unicode value). See the Character Sets section below for discussion. In the normal case, the Unicode value is converted to a font index, and this is stored in video memory, so that the corresponding glyph (as found in video ROM) appears on the screen. Note that the use of Uni- code (and the design of the PC hardware) allows us to use 512 different glyphs simultaneously. If the current Unicode value is a control character, or we are cur- rently processing an escape sequence, the value will treated specially. Instead of being turned into a font index and rendered as a glyph, it may trigger cursor movement or other control functions. See the Linux Console Controls section below for discussion. It is generally not good practice to hard-wire terminal controls into programs. Linux supports a terminfo(5) database of terminal capabili- ties. Rather than emitting console escape sequences by hand, you will almost always want to use a terminfo-aware screen library or utility such as ncurses(3), tput(1), or reset(1). Linux Console Controls This section describes all the control characters and escape sequences that invoke special functions (i.e., anything other than writing a glyph at the current cursor location) on the Linux console. Control characters A character is a control character if (before transformation according to the mapping table) it has one of the 14 codes 00 (NUL), 07 (BEL), 08 (BS), 09 (HT), 0a (LF), 0b (VT), 0c (FF), 0d (CR), 0e (SO), 0f (SI), 18 (CAN), 1a (SUB), 1b (ESC), 7f (DEL). One can set a "display control characters" mode (see below), and allow 07, 09, 0b, 18, 1a, 7f to be displayed as glyphs. On the other hand, in UTF-8 mode all codes 00-1f are regarded as control characters, regardless of any "display control characters" mode. If we have a control character, it is acted upon immediately and then discarded (even in the middle of an escape sequence) and the escape sequence continues with the next character. (However, ESC starts a new escape sequence, possibly aborting a previous unfinished one, and CAN and SUB abort any escape sequence.) The recognized control characters are BEL, BS, HT, LF, VT, FF, CR, SO, SI, CAN, SUB, ESC, DEL, CSI. They do what one would expect: BEL (0x07, ^G) beeps; BS (0x08, ^H) backspaces one column (but not past the beginning of the line); HT (0x09, ^I) goes to the next tab stop or to the end of the line if there is no earlier tab stop; LF (0x0A, ^J), VT (0x0B, ^K) and FF (0x0C, ^L) all give a linefeed, and if LF/NL (new-line mode) is set also a carriage return; CR (0x0D, ^M) gives a carriage return; SO (0x0E, ^N) activates the G1 character set; SI (0x0F, ^O) activates the G0 character set; CAN (0x18, ^X) and SUB (0x1A, ^Z) interrupt escape sequences; ESC (0x1B, ^[) starts an escape sequence; DEL (0x7F) is ignored; CSI (0x9B) is equivalent to ESC [. ESC- but not CSI-sequences ESC c RIS Reset. ESC D IND Linefeed. ESC E NEL Newline. ESC H HTS Set tab stop at current column. ESC M RI Reverse linefeed. ESC Z DECID DEC private identification. The kernel returns the string ESC [ ? 6 c, claiming that it is a VT102. ESC 7 DECSC Save current state (cursor coordinates, attributes, character sets pointed at by G0, G1). ESC 8 DECRC Restore state most recently saved by ESC 7. ESC [ CSI Control sequence introducer ESC % Start sequence selecting character set ESC % @ Select default (ISO 646 / ISO 8859-1) ESC % G Select UTF-8 ESC % 8 Select UTF-8 (obsolete) ESC # 8 DECALN DEC screen alignment test - fill screen with E’s. ESC ( Start sequence defining G0 character set ESC ( B Select default (ISO 8859-1 mapping) ESC ( 0 Select VT100 graphics mapping ESC ( U Select null mapping - straight to character ROM ESC ( K Select user mapping - the map that is loaded by the utility mapscrn(8). ESC ) Start sequence defining G1 (followed by one of B, 0, U, K, as above). ESC > DECPNM Set numeric keypad mode ESC = DECPAM Set application keypad mode ESC ] OSC (Should be: Operating system command) ESC ] P nrrggbb: set palette, with parameter given in 7 hexadecimal digits after the final P :-(. Here n is the color (0-15), and rrggbb indicates the red/green/blue values (0-255). ESC ] R: reset palette ECMA-48 CSI sequences CSI (or ESC [) is followed by a sequence of parameters, at most NPAR (16), that are decimal numbers separated by semicolons. An empty or absent parameter is taken to be 0. The sequence of parameters may be preceded by a single question mark. However, after CSI [ (or ESC [ [) a single character is read and this entire sequence is ignored. (The idea is to ignore an echoed function key.) The action of a CSI sequence is determined by its final character. @ ICH Insert the indicated # of blank characters. A CUU Move cursor up the indicated # of rows. B CUD Move cursor down the indicated # of rows. C CUF Move cursor right the indicated # of columns. D CUB Move cursor left the indicated # of columns. E CNL Move cursor down the indicated # of rows, to column 1. F CPL Move cursor up the indicated # of rows, to column 1. G CHA Move cursor to indicated column in current row. H CUP Move cursor to the indicated row, column (origin at 1,1). J ED Erase display (default: from cursor to end of display). ESC [ 1 J: erase from start to cursor. ESC [ 2 J: erase whole display. K EL Erase line (default: from cursor to end of line). ESC [ 1 K: erase from start of line to cursor. ESC [ 2 K: erase whole line. L IL Insert the indicated # of blank lines. M DL Delete the indicated # of lines. P DCH Delete the indicated # of characters on the current line. X ECH Erase the indicated # of characters on the current line. a HPR Move cursor right the indicated # of columns. c DA Answer ESC [ ? 6 c: "I am a VT102". d VPA Move cursor to the indicated row, current column. e VPR Move cursor down the indicated # of rows. f HVP Move cursor to the indicated row, column. g TBC Without parameter: clear tab stop at the current position. ESC [ 3 g: delete all tab stops. h SM Set Mode (see below). l RM Reset Mode (see below). m SGR Set attributes (see below). n DSR Status report (see below). q DECLL Set keyboard LEDs. ESC [ 0 q: clear all LEDs ESC [ 1 q: set Scroll Lock LED ESC [ 2 q: set Num Lock LED ESC [ 3 q: set Caps Lock LED r DECSTBM Set scrolling region; parameters are top and bottom row. s ? Save cursor location. u ? Restore cursor location. ` HPA Move cursor to indicated column in current row. ECMA-48 Set Graphics Rendition The ECMA-48 SGR sequence ESC [ parameters m sets display attributes. Several attributes can be set in the same sequence, separated by semi- colons. An empty parameter (between semicolons or string initiator or terminator) is interpreted as a zero. param result 0 reset all attributes to their defaults 1 set bold 2 set half-bright (simulated with color on a color display) 4 set underscore (simulated with color on a color display) (the colors used to simulate dim or underline are set using ESC ] ...) 5 set blink 7 set reverse video 10 reset selected mapping, display control flag, and toggle meta flag (ECMA-48 says "primary font"). 11 select null mapping, set display control flag, reset tog- gle meta flag (ECMA-48 says "first alternate font"). 12 select null mapping, set display control flag, set toggle meta flag (ECMA-48 says "second alternate font"). The toggle meta flag causes the high bit of a byte to be tog- gled before the mapping table translation is done. 21 set normal intensity (ECMA-48 says "doubly underlined") 22 set normal intensity 24 underline off 25 blink off 27 reverse video off 30 set black foreground 31 set red foreground 32 set green foreground 33 set brown foreground 34 set blue foreground 35 set magenta foreground 36 set cyan foreground 37 set white foreground 38 set underscore on, set default foreground color 39 set underscore off, set default foreground color 40 set black background 41 set red background 42 set green background 43 set brown background 44 set blue background 45 set magenta background 46 set cyan background 47 set white background 49 set default background color ECMA-48 Mode Switches ESC [ 3 h DECCRM (default off): Display control chars. ESC [ 4 h DECIM (default off): Set insert mode. ESC [ 20 h LF/NL (default off): Automatically follow echo of LF, VT or FF with CR. ECMA-48 Status Report Commands ESC [ 5 n Device status report (DSR): Answer is ESC [ 0 n (Terminal OK). ESC [ 6 n Cursor position report (CPR): Answer is ESC [ y ; x R, where x,y is the cursor location. DEC Private Mode (DECSET/DECRST) sequences These are not described in ECMA-48. We list the Set Mode sequences; the Reset Mode sequences are obtained by replacing the final 'h' by 'l'. ESC [ ? 1 h DECCKM (default off): When set, the cursor keys send an ESC O prefix, rather than ESC [. ESC [ ? 3 h DECCOLM (default off = 80 columns): 80/132 col mode switch. The driver sources note that this alone does not suffice; some user- mode utility such as resizecons(8) has to change the hardware registers on the console video card. ESC [ ? 5 h DECSCNM (default off): Set reverse-video mode. ESC [ ? 6 h DECOM (default off): When set, cursor addressing is relative to the upper left corner of the scrolling region. ESC [ ? 7 h DECAWM (default on): Set autowrap on. In this mode, a graphic character emitted after column 80 (or column 132 of DECCOLM is on) forces a wrap to the beginning of the following line first. ESC [ ? 8 h DECARM (default on): Set keyboard autorepeat on. ESC [ ? 9 h X10 Mouse Reporting (default off): Set reporting mode to 1 (or reset to 0) — see below. ESC [ ? 25 h DECTECM (default on): Make cursor visible. ESC [ ? 1000 h X11 Mouse Reporting (default off): Set reporting mode to 2 (or reset to 0) — see below. Linux Console Private CSI Sequences The following sequences are neither ECMA-48 nor native VT102. They are native to the Linux console driver. Colors are in SGR parameters: 0 = black, 1 = red, 2 = green, 3 = brown, 4 = blue, 5 = magenta, 6 = cyan, 7 = white. ESC [ 1 ; n ] Set color n as the underline color ESC [ 2 ; n ] Set color n as the dim color ESC [ 8 ] Make the current color pair the default attributes. ESC [ 9 ; n ] Set screen blank timeout to n minutes. ESC [ 10 ; n ] Set bell frequency in Hz. ESC [ 11 ; n ] Set bell duration in msec. ESC [ 12 ; n ] Bring specified console to the front. ESC [ 13 ] Unblank the screen. ESC [ 14 ; n ] Set the VESA powerdown interval in minutes. Character Sets The kernel knows about 4 translations of bytes into console-screen sym- bols. The four tables are: a) Latin1 -> PC, b) VT100 graphics -> PC, c) PC -> PC, d) user-defined. There are two character sets, called G0 and G1, and one of them is the current character set. (Initially G0.) Typing ^N causes G1 to become current, ^O causes G0 to become current. These variables G0 and G1 point at a translation table, and can be changed by the user. Initially they point at tables a) and b), respec- tively. The sequences ESC ( B and ESC ( 0 and ESC ( U and ESC ( K cause G0 to point at translation table a), b), c) and d), respectively. The sequences ESC ) B and ESC ) 0 and ESC ) U and ESC ) K cause G1 to point at translation table a), b), c) and d), respectively. The sequence ESC c causes a terminal reset, which is what you want if the screen is all garbled. The oft-advised "echo ^V^O" will only make G0 current, but there is no guarantee that G0 points at table a). In some distributions there is a program reset(1) that just does "echo ^[c". If your terminfo entry for the console is correct (and has an entry rs1=\Ec), then "tput reset" will also work. The user-defined mapping table can be set using mapscrn(8). The result of the mapping is that if a symbol c is printed, the symbol s = map[c] is sent to the video memory. The bitmap that corresponds to s is found in the character ROM, and can be changed using setfont(8). Mouse Tracking The mouse tracking facility is intended to return xterm(1)-compatible mouse status reports. Because the console driver has no way to know the device or type of the mouse, these reports are returned in the con- sole input stream only when the virtual terminal driver receives a mouse update ioctl. These ioctls must be generated by a mouse-aware user-mode application such as the gpm(8) daemon. The mouse tracking escape sequences generated by xterm(1) encode numeric parameters in a single character as value+040. For example, '!' is 1. The screen coordinate system is 1-based. The X10 compatibility mode sends an escape sequence on button press encoding the location and the mouse button pressed. It is enabled by sending ESC [ ? 9 h and disabled with ESC [ ? 9 l. On button press, xterm(1) sends ESC [ M bxy (6 characters). Here b is button-1, and x and y are the x and y coordinates of the mouse when the button was pressed. This is the same code the kernel also produces. Normal tracking mode (not implemented in Linux 2.0.24) sends an escape sequence on both button press and release. Modifier information is also sent. It is enabled by sending ESC [ ? 1000 h and disabled with ESC [ ? 1000 l. On button press or release, xterm(1) sends ESC [ M bxy. The low two bits of b encode button information: 0=MB1 pressed, 1=MB2 pressed, 2=MB3 pressed, 3=release. The upper bits encode what modifiers were down when the button was pressed and are added together: 4=Shift, 8=Meta, 16=Control. Again x and y are the x and y coordinates of the mouse event. The upper left corner is (1,1). Comparisons With Other Terminals Many different terminal types are described, like the Linux console, as being "VT100-compatible". Here we discuss differences between the Linux console and the two most important others, the DEC VT102 and xterm(1). Control-character handling The VT102 also recognized the following control characters: NUL (0x00) was ignored; ENQ (0x05) triggered an answerback message; DC1 (0x11, ^Q, XON) resumed transmission; DC3 (0x13, ^S, XOFF) caused VT100 to ignore (and stop transmitting) all codes except XOFF and XON. VT100-like DC1/DC3 processing may be enabled by the tty driver. The xterm(1) program (in VT100 mode) recognizes the control characters BEL, BS, HT, LF, VT, FF, CR, SO, SI, ESC. Escape sequences VT100 console sequences not implemented on the Linux console: ESC N SS2 Single shift 2. (Select G2 character set for the next character only.) ESC O SS3 Single shift 3. (Select G3 character set for the next character only.) ESC P DCS Device control string (ended by ESC \) ESC X SOS Start of string. ESC ^ PM Privacy message (ended by ESC \) ESC \ ST String terminator ESC * ... Designate G2 character set ESC + ... Designate G3 character set The program xterm(1) (in VT100 mode) recognizes ESC c, ESC # 8, ESC >, ESC =, ESC D, ESC E, ESC H, ESC M, ESC N, ESC O, ESC P ... ESC \, ESC Z (it answers ESC [ ? 1 ; 2 c, "I am a VT100 with advanced video option") and ESC ^ ... ESC \ with the same meanings as indicated above. It accepts ESC (, ESC ), ESC *, ESC + followed by 0, A, B for the DEC special character and line drawing set, UK, and US-ASCII, respectively. The user can configure xterm(1) to respond to VT220-specific control sequences, and it will identify itself as a VT52, VT100, and up depend- ing on the way it is configured and initialized. It accepts ESC ] (OSC) for the setting of certain resources. In addi- tion to the ECMA-48 string terminator (ST), xterm(1) accepts a BEL to terminate an OSC string. These are a few of the OSC control sequences recognized by xterm(1): ESC ] 0 ; txt ST Set icon name and window title to txt. ESC ] 1 ; txt ST Set icon name to txt. ESC ] 2 ; txt ST Set window title to txt. ESC ] 4 ; num; txt ST Set ANSI color num to txt. ESC ] 10 ; txt ST Set dynamic text color to txt. ESC ] 4 6 ; name ST Change log file to name (normally disabled by a compile-time option) ESC ] 5 0 ; fn ST Set font to fn. It recognizes the following with slightly modified meaning (saving more state, behaving closer to VT100/VT220): ESC 7 DECSC Save cursor ESC 8 DECRC Restore cursor It also recognizes ESC F Cursor to lower left corner of screen (if enabled by xterm(1)’s hpLowerleftBugCompat resource) ESC l Memory lock (per HP terminals). Locks memory above the cursor. ESC m Memory unlock (per HP terminals). ESC n LS2 Invoke the G2 character set. ESC o LS3 Invoke the G3 character set. ESC | LS3R Invoke the G3 character set as GR. ESC } LS2R Invoke the G2 character set as GR. ESC ~ LS1R Invoke the G1 character set as GR. It also recognizes ESC % and provides a more complete UTF-8 implementa- tion than Linux console. CSI Sequences Old versions of xterm(1), for example, from X11R5, interpret the blink SGR as a bold SGR. Later versions which implemented ANSI colors, for example, XFree86 3.1.2A in 1995, improved this by allowing the blink attribute to be displayed as a color. Modern versions of xterm imple- ment blink SGR as blinking text and still allow colored text as an alternate rendering of SGRs. Stock X11R6 versions did not recognize the color-setting SGRs until the X11R6.8 release, which incorporated XFree86 xterm. All ECMA-48 CSI sequences recognized by Linux are also recognized by xterm, however xterm(1) implements several ECMA-48 and DEC control sequences not recognized by Linux. The xterm(1) program recognizes all of the DEC Private Mode sequences listed above, but none of the Linux private-mode sequences. For dis- cussion of xterm(1)’s own private-mode sequences, refer to the Xterm Control Sequences document by Edward Moy, Stephen Gildea, and Thomas E. Dickey available with the X distribution. That document, though terse, is much longer than this manual page. For a chronological overview, http://invisible-island.net/xterm/xterm.log.html details changes to xterm. The vttest program http://invisible-island.net/vttest/ demonstrates many of these control sequences. The xterm(1) source dis- tribution also contains sample scripts which exercise other features. NOTES ESC 8 (DECRC) is not able to restore the character set changed with ESC %. BUGS In 2.0.23, CSI is broken, and NUL is not ignored inside escape sequences. Some older kernel versions (after 2.0) interpret 8-bit control sequences. These "C1 controls" use codes between 128 and 159 to replace ESC [, ESC ] and similar two-byte control sequence initiators. There are fragments of that in modern kernels (either overlooked or broken by changes to support UTF-8), but the implementation is incom- plete and should be regarded as unreliable. Linux "private mode" sequences do not follow the rules in ECMA-48 for private mode control sequences. In particular, those ending with ] do not use a standard terminating character. The OSC (set palette) sequence is a greater problem, since xterm(1) may interpret this as a control sequence which requires a string terminator (ST). Unlike the setterm(1) sequences which will be ignored (since they are invalid con- trol sequences), the palette sequence will make xterm(1) appear to hang (though pressing the return-key will fix that). To accommodate appli- cations which have been hardcoded to use Linux control sequences, set the xterm(1) resource brokenLinuxOSC to true. An older version of this document implied that Linux recognizes the ECMA-48 control sequence for invisible text. It is ignored. SEE ALSO console(4), console_ioctl(4), charsets(7) 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 2008-01-01 CONSOLE_CODES(4)
Copyright(C) linux-cmd.com All Rights Reserved. Author Takayuki Yukawa