すべての出力作業が終って、実際に印字するために文字のストリームが コンソールドライバーに届いた時、最初に行なわれるのが、処理用コードから 印字用のコードへの変換である。
もし、コンソールが UTF-8 モードなら、入って来たバイト(byte)は 16 ビットの Unicode に組み立てられる。そうでなければ、それぞれの バイトは現在の(各バイトを Unicode の値に変換する)マッピングテーブルに 従って変換される。これに関しては下のキャラクターセット(Charcter Sets) 節を参照のこと。
通常、Unicode の値はフォントインデックス(font index) に変換され、 ビデオメモリーに蓄えられる。そして(ビデオ ROM 中に見付けられる) 対応するグリフ(glyph)が画面に現れる。 Unicode を用いると、(PC 互換機の設計では) 512 の異なった グリフを同時に利用できるようになる。
現在の Unicode の値がコントロール文字であるかエスケープシーケンスを 処理している時には、その値は特別に扱われる。 フォントのインデックスに変換されてグリフとして表示される代わりに、カーソルの 移動やその他のコントロール機能を実行させる。 詳しいことは、後述の「LINUX コンソールコントロール」のセクションを参照のこと。
一般に端末コントロールコードをプログラム中に直接記述するのは 良いことではない。 Linuxでは、端末において実現可能な操作のデータベースである terminfo(5) をサポートしている。 コンソールエスケープシーケンスをわざわざ入力するよりも、ほとんどの場合 terminfo の情報を参照する ncurses(3), tput(1), や reset(1) などのスクリーンライブラリやユーティリティーを使いたいと思うであろう。
コントロール文字
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.
コントロール文字がある時には、それはすぐに(たとえエスケープシーケンスの 途中でも)実行され、破棄される。エスケープシーケンスは次の文字から続けられる (しかし、ESC は新しいエスケープシーケンスを始めるので、 前の終了していないエスケープシーケンスが中止される可能性がある。 さらに、CAN と SUB はどんなエスケープシーケンスも中止する)。 認められているコントロール文字は、BEL, BS, HT, LF, VT, FF, CR, SO, SI, CAN, SUB, ESC, DEL, CSI である。これらは期待どおりの動作をする:
エスケープシーケンスであるが CSI シーケンスでないもの
| ESC c | RIS | リセット。 |
| ESC D | IND | ラインフィード。 |
| ESC E | NEL | ニューライン。 |
| ESC H | HTS |
現在の桁の位置にタブストップを設定する。
|
| ESC M | RI | 逆ラインフィード |
| 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 |
最後に ESC 7 でセーブした状態を復帰させる。
|
| ESC % | キャラクターセットを選択するシーケンスを開始する。 | |
| ESC % @ |
Select default (ISO/IEC~646 / ISO/IEC~8859-1)
| |
| ESC % G | Select UTF-8 | |
| ESC % 8 | Select UTF-8 (obsolete) | |
| ESC # 8 | DECALN |
DEC のスクリーン調整テスト - スクリーンを E でうめる。
|
| ESC ( |
Start sequence defining G0 character set
(followed by one of B, 0, U, K, as below)
| |
| ESC ( B |
Select default (ISO/IEC~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 | 数値キーパッドモード(numeric keypad mode)をセットする。 |
| ESC = | DECPAM | アプリケーションキーパッドモード(application keypad mode) をセットする。 |
| ESC ] | OSC |
Operating System Command prefix.
|
| ESC ] R | Reset palette. | |
| ESC ] P |
Set palette, with parameter given in 7 hexadecimal digits
nrrggbb
after the final P.
Here
n
is the color (0-15),
and
rrggbb
indicates
the red/green/blue values (0-255).
|
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: ディスプレイ全体の消去。
| ||
|
ESC [ 3 J: erase whole display including scroll-back
buffer (since Linux 3.0).
| ||
| 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 |
Set scrolling region;
parameters are top and bottom row.
|
| s | SCOSC | カーソル位置の保存。 |
| u | SCORC | カーソル位置の復帰。 |
| ` | HPA |
カーソルを現在の行の指示された桁に移動する。
|
ECMA-48 Select Graphic Rendition
ECMA-48 SGR シーケンス ESC [ <パラメーター> m は表示属性を設定する。 セミコロンで区切ることで、同じシーケンスでいくつかの属性を設定できる。 空パラメーター(セミコロンか文字列開始文字か文字列終端文字の間)はゼロと解釈される。
| parameter | 結果 |
| 0 |
すべての属性をデフォルトにリセットする。
|
| 1 | ボールド(bold)をセット。 |
| 2 |
ハーフブライト(half-bright)(カラーディスプレイでは色で代用)をセット。
|
| 3 |
set italic (since Linux 2.6.22; simulated with color on a color display)
|
| 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 |
set underline;
before Linux 4.17,
this value set normal intensity
(as is done in many other terminals)
|
| 22 | 通常の輝度にセット。 |
| 23 | italic off (since Linux 2.6.22) |
| 24 | 下線オフ。 |
| 25 | 点滅オフ。 |
| 27 | 反転表示オフ。 |
| 30 | フォアグラウンド(foreground)を黒にセット。 |
| 31 | フォアグラウンドを赤にセット。 |
| 32 | フォアグラウンドを緑にセット。 |
| 33 | フォアグラウンドを茶にセット。 |
| 34 | フォアグラウンドを青にセット。 |
| 35 | フォアグラウンドをマゼンダにセット。 |
| 36 | フォアグラウンドをシアンにセット。 |
| 37 | フォアグラウンドを白にセット。 |
| 38 |
256/24-bit foreground color follows, shoehorned into 16 basic colors
(before Linux 3.16: set underscore on, set default foreground color)
|
| 39 |
デフォルトのフォアグラウンドの色を設定する
(Linux 3.16 より前: 下線表示を解除し、デフォルトのフォアグラウンドの色をセット)
|
| 40 | バックグラウンド(background)を黒にセット。 |
| 41 | バックグラウンドを赤にセット。 |
| 42 | バックグラウンドを緑にセット。 |
| 43 | バックグラウンドを茶にセット。 |
| 44 | バックグラウンドを青にセット。 |
| 45 | バックグラウンドをマゼンダにセット。 |
| 46 | バックグラウンドをシアンにセット。 |
| 47 | バックグラウンドを白にセット。 |
| 48 |
256/24-bit background color follows, shoehorned into 8 basic colors
|
| 49 | バックグラウンドをデフォルトにセット。 |
| 90..97 |
set foreground to bright versions of 30..37
|
| 100..107 |
set background, same as 40..47 (bright not supported)
|
Commands 38 and 48 require further arguments:
| ;5;x |
256 color: values 0..15 are IBGR (black, red, green, ..., white),
16..231 a 6x6x6 color cube, 232..255 a grayscale ramp
|
| ;2;r;g;b |
24-bit color, r/g/b components are in the range 0..255
|
ECMA-48 モードスイッチ(Mode Switches)
ECMA-48 状態リポートコマンド(Status Report Commands)
DEC プライベートモード (DECSET/DECRST) シーケンス
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'.
Linux コンソールプライベート CSI シーケンス
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; 8-15 = bright versions of 0-7.
| ESC [ 1 ; n ] |
Set color n as the underline color.
|
| ESC [ 2 ; n ] |
Set color n as the dim color.
|
| ESC [ 8 ] |
現在の色のペアをデフォルト属性にする。
|
| ESC [ 9 ; n ] |
Set screen blank timeout to
n
minutes.
|
| ESC [ 10 ; n ] |
ベルの周波数(Hz)をセットする。
|
| ESC [ 11 ; n ] |
ベルの鳴っている時間(msec)をセットする。
|
| ESC [ 12 ; n ] |
指定のコンソールを前面に持ってくる。
|
| ESC [ 13 ] |
スクリーンをアンブランク(unblank)する。
|
| ESC [ 14 ; n ] |
VESA電源停止インターバル(VESA powerdown interval)をセットする。
|
| ESC [ 15 ] |
直前のコンソールを前面に持ってくる
(Linux 2.6.0 以降)
|
| ESC [ 16 ; n ] |
Set the cursor blink interval in milliseconds
(since Linux 4.2).
|
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), respectively. 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 make only 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.
ユーザー定義のマッピングテーブルは mapscrn(8) を使って定義できる。 マッピングの結果、シンボル c が印字されるとシンボル s = map[c] が ビデオメモリーに送られる。s に対応するビットマップはキャラクター ROM にあり、 setfont(8) により変更可能である。
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).
コントロール文字の取り扱い
VT102 は以下のコントロール文字も認識する:
端末ドライバーにより VT100 に似た DC1/DC3 処理を有効にする。
xterm(1) (VT100 モード)は BEL, BS, HT, LF, VT, FF, CR, SO, SI, ESC の コントロール文字を認識する。
エスケープシーケンス
Linux コンソールで実装されていない VT100 コンソールシーケンスは以下の通り:
| 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 | 文字列の始まり。 |
| ESC ^ | PM | Privacy message (ended by ESC \) |
| ESC \ | ST | 文字列の終端文字。 |
| ESC * ... | G2 文字集合を指定する。 | |
| ESC + ... | G3 文字集合を指定する。 |
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 depending on the way it is configured and initialized.
It accepts ESC ] (OSC) for the setting of certain resources. In addition 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. |
以下のものは、少し違った意味にとられる (より多くの状態を保存し、より VT100/VT220 に近いふるまいをする):
| ESC 7 DECSC | カーソルの保存。 | |
| ESC 8 DECRC | カーソルの復元。 |
また、次のものも認識する:
| 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 | (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 シーケンス
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(1) implement 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(1). All ECMA-48 CSI sequences recognized by Linux are also recognized by xterm(1), however xterm(1) implements several ECMA-48 and DEC control sequences not recognized by Linux.
xterm(1) は上述のすべての DEC プライベートモードのシーケンスを認識するが、 Linux プライベートモードのシーケンスはどれも認識しない。 xterm(1) 自身のプライベートモードシーケンスに関しての議論は、 X 配布とともに入手可能な Edward Moy, Stephen Gildea,Thomas E. Dickey による Xterm Control Sequences ドキュメントを参照されたい。 このドキュメントは、簡潔なものであるが、このマニュアルページより 遥かに長いものである。 年代順の概観としては、
details changes to xterm(1).
The vttest(1) program
demonstrates many of these control sequences. The xterm(1) source distribution also contains sample scripts which exercise other features.
Some older kernel versions (after Linux 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 incomplete 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 control sequences), the palette sequence will make xterm(1) appear to hang (though pressing the return-key will fix that). To accommodate applications which have been hardcoded to use Linux control sequences, set the xterm(1) resource brokenLinuxOSC to true.
このドキュメントの古いバージョンでは、Linux が ECMA-48 の不可視テキストの コントロールシーケンスを認識するかのように書かれていた。 これは無視される。