CONSOLE_CODES - Linux手册页

Linux程序员手册 第4部分
更新日期: 2020-08-13

名称

console_codes-Linux控制台转义和控制序列

出版信息

这个页面是Linux手册页项目5.08版的一部分。有关项目的说明、有关报告错误的信息以及此页面的最新版本,请访问https://www.kernel.org/doc/man-pages/

备注

ESC 8(DECRC)无法恢复用ESC%更改的字符集。

BUGS

在2.0.23中,CSI被破坏,并且在转义序列内不忽略NUL。

一些较早的内核版本(2.0之后)解释8位控制序列。这些" C1控件"使用128到159之间的代码来替换ESC [,ESC]和类似的两字节控制序列启动器。在现代内核中有一些片段(被支持UTF-8的更改忽略或破坏了),但是实现是不完整的,应该被认为是不可靠的。

Linux"私有模式"序列不遵循ECMA-48中关于私有模式控制序列的规则。特别是,那些以]结尾的不使用标准终止符。 OSC(设置调色板)序列是一个更大的问题,因为xterm(1)可能会将其解释为需要字符串终止符(ST)的控制序列。与setterm(1)序列将被忽略(由于它们是无效的控制序列)不同,调色板序列将使xterm(1)看起来挂起(尽管按回车键可以解决该问题)。为了适应已被硬编码为使用Linux控制序列的应用程序,请将xterm(1)资源brokenLinuxOSC设置为true。

本文档的较旧版本暗示Linux可以识别不可见文本的ECMA-48控制序列。它被忽略。

另外参见

ioctl_console(2),字符集(7)

说明

Linux控制台实现了VT102和ECMA-48 / ISO 6429 / ANSI X3.64终端控件的较大子集,以及用于更改调色板,字符集映射等的某些专用模式序列。在下面的表格描述中,第二列给出了给定功能的ECMA-48或DEC助记符(后者以DEC开头)。没有助记符的序列既不是ECMA-48也不是VT102。

在完成所有正常的输出处理并且字符流到达控制台驱动程序以进行实际打印之后,首先发生的事情是将用于处理的代码转换为用于打印的代码。

如果控制台处于UTF-8模式,则输入的字节首先被组装为16位Unicode代码。否则,将根据当前映射表转换每个字节(将其转换为Unicode值)。请参阅下面的字符集部分以进行讨论。

在正常情况下,Unicode值会转换为字体索引,并将其存储在视频内存中,以便相应的字形(如在视频ROM中找到的)会出现在屏幕上。请注意,使用Unicode(以及PC硬件的设计)可以使我们同时使用512个不同的字形。

如果当前Unicode值是控制字符,或者我们当前正在处理转义序列,则将对该值进行特殊处理。它可能会触发光标移动或其他控制功能,而不是变成字体索引并显示为字形。请参阅下面的" Linux控制台控件"部分以进行讨论。

将终端控件硬连接到程序中通常不是一个好习惯。 Linux支持终端功能的te​​rminfo(5)数据库。您几乎总是希望使用支持terminfo的屏幕库或实用程序,例如ncurses(3),tput(1)或reset(1),而不是手动发出控制台转义序列。

Linux console controls

本节介绍了在Linux控制台上调用特殊功能(即,除了在当前光标位置写字形以外的任何功能)的所有控制字符和转义序列。

控制字符

如果一个字符(在根据映射表进行转换之前)具有14个代码之一(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)。可以设置"显示控制字符"模式(见下文),并允许将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,haG)哔声;
BS(0x08,haH)退格一列(但不超过行的开头);
HT(0x09,haI)转到下一个制表位,如果没有更早的制表位,则移至该行的末尾;
LF(0x0A,haJ),VT(0x0B,haK)和FF(0x0C,haL)都进行换行,如果设置了LF / NL(换行模式),则还返回回车;
CR(0x0D,haM)给出回车符;
SO(0x0E,haN)激活G1字符集;
SI(0x0F,haO)激活G0字符集;
CAN(0x18,haX)和SUB(0x1A,haZ)中止转义序列;
ESC(0x1B,ha [)开始一个转义序列;
DEL(0x7F)被忽略;
CSI(0x9B)等效于ESC [。

ESC序列而不是CSI序列

ESC cRISReset.
ESC DINDLinefeed.
ESC ENELNewline.
ESC HHTSSet tab stop at current column.
ESC MRIReverse linefeed.
ESC ZDECIDDEC private identification. The kernelreturns the string ESC [ ? 6 c, claimingthat it is a VT102.
ESC 7DECSCSave current state (cursor coordinates,attributes, character sets pointed at by G0, G1).
ESC 8DECRCRestore state most recently saved by ESC 7.
ESC [CSIControl sequence introducer
ESC %Start sequence selecting character set
ESC % @Select default (ISO 646 / ISO 8859-1)
ESC % GSelect UTF-8
ESC % 8Select UTF-8 (obsolete)
ESC # 8DECALNDEC screen alignment test - fill screen with E's.
ESC (Start sequence defining G0 character set
ESC ( BSelect default (ISO 8859-1 mapping)
ESC ( 0Select VT100 graphics mapping
ESC ( USelect null mapping - straight to character ROM
ESC ( KSelect 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 >DECPNMSet numeric keypad mode
ESC =DECPAMSet application keypad mode
ESC ]OSC(Should be: Operating system command)ESC ] P nrrggbb: set palette, with parametergiven in 7 hexadecimal doirts after the final P :-(.Here nis the color (0-15), and rrggbbindicatesthe red/green/blue values (0-255).ESC ] R: reset palette

ECMA-48 CSI序列

CSI(或ESC [])后跟一系列参数,最多NPAR(16),这些参数是用分号分隔的十进制数。空或不存在的参数将被视为0。参数序列之前可以带有单个问号。

但是,在CSI [(或ESC [[])之后,将读取单个字符,并且将忽略整个序列。 (其想法是忽略回显的功能键。)

CSI序列的作用由其最终字符决定。

@ICHInsert the indicated # of blank characters.
ACUUMove cursor up the indicated # of rows.
BCUDMove cursor down the indicated # of rows.
CCUFMove cursor right the indicated # of columns.
DCUBMove cursor left the indicated # of columns.
ECNLMove cursor down the indicated # of rows, to column 1.
FCPLMove cursor up the indicated # of rows, to column 1.
GCHAMove cursor to indicated column in current row.
HCUPMove cursor to the indicated row, column (oroirn at 1,1).
JEDErase display (default: from cursor to end of display).
ESC [ 1 J: erase from start to cursor.
ESC [ 2 J: erase whole display.
ESC [ 3 J: erase whole display including scroll-back
buffer (since Linux 3.0).
KELErase line (default: from cursor to end of line).
ESC [ 1 K: erase from start of line to cursor.
ESC [ 2 K: erase whole line.
LILInsert the indicated # of blank lines.
MDLDelete the indicated # of lines.
PDCHDelete the indicated # of characters on current line.
XECHErase the indicated # of characters on current line.
aHPRMove cursor right the indicated # of columns.
cDAAnswer ESC [ ? 6 c: "I am a VT102".
dVPAMove cursor to the indicated row, current column.
eVPRMove cursor down the indicated # of rows.
fHVPMove cursor to the indicated row, column.
gTBCWithout parameter: clear tab stop at current position.
ESC [ 3 g: delete all tab stops.
hSMSet Mode (see below).
lRMReset Mode (see below).
mSGRSet attributes (see below).
nDSRStatus report (see below).
qDECLLSet 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
rDECSTBMSet scrolling region; parameters are top and bottom row.
s?Save cursor location.
u?Restore cursor location.
`HPAMove cursor to indicated column in current row.

ECMA-48设置图形渲染

ECMA-48 SGR序列ESC [参数m设置显示属性。可以按相同的顺序设置多个属性,以分号分隔。空参数(在分号或字符串起始符或终止符之间)被解释为零。

paramresult
0reset all attributes to their defaults
1set bold
2set half-bright (simulated with color on a color display)
4set underscore (simulated with color on a color display)(the colors used to simulate dim or underline are setusing ESC ] ...)
5set blink
7set reverse video
10reset selected mapping, display control flag,and toggle meta flag (ECMA-48 says "primary font").
11select null mapping, set display control flag,reset toggle meta flag (ECMA-48 says "first alternate font").
12select null mapping, set display control flag,set toggle meta flag (ECMA-48 says "second alternate font").The toggle meta flagcauses the high bit of a byte to be toggledbefore the mapping table translation is done.
21set underline; before Linux 4.17, this valueset normal intensity (as is done in many other terminals)
22set normal intensity
24underline off
25blink off
27reverse video off
30set black foreground
31set red foreground
32set green foreground
33set brown foreground
34set blue foreground
35set magenta foreground
36set cyan foreground
37set white foreground
38256/24-bit foreground color follows, shoehorned into 16 basic colors(before Linux 3.16: set underscore on, set default foreground color)
39set default foreground color(before Linux 3.16: set underscore off, set default foreground color)
40set black background
41set red background
42set green background
43set brown background
44set blue background
45set magenta background
46set cyan background
47set white background
48256/24-bit background color follows, shoehorned into 8 basic colors
49set default background color
90..97set foreground to bright versions of 30..37
100.107set background, same as 40..47 (bright 不支持)

命令38和48需要进一步的参数:

;5;x256 color: values 0..15 are IBGR (black, red, green, ... white),16..231 a 6x6x6 color cube, 232..255 a grayscale ramp
;2;r;g;b24-bit color, r/g/b components are in the range 0..255

ECMA-48模式开关

ESC [ 3 h
DECCRM(默认关闭):显示控制字符。
ESC [ 4 h
DECIM(默认关闭):设置插入模式。
ESC [ 20 h
LF / NL(默认关闭):自动跟踪带有CR的LF,VT或FF的回波。

ECMA-48状态报告命令

ESC [ 5 n
设备状态报告(DSR):答案为ESC [0 n(端子正常)。
ESC [ 6 n
光标位置报告(CPR):答案为ESC [y; x R,其中x,y是光标位置。

DEC专用模式(DECSET / DECRST)序列

这些在ECMA-48中没有描述。我们列出了设置模式序列;重置模式序列是通过用aqlaq替换最终的aqhaq来获得的。

ESC [ ? 1 h
DECCKM(默认关闭):设置后,光标键发送ESC O前缀,而不是ESC [。
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(默认打开):使光标可见。
ESC [ ? 1000 h
X11鼠标报告(默认关闭):将报告模式设置为2(或重置为0)---参见下文。

Linux控制台专用CSI序列

以下序列既不是ECMA-48也不是本地VT102。它们是Linux控制台驱动程序固有的。颜色在SGR参数中:0 =黑色,1 =红色,2 =绿色,3 =棕色,4 =蓝色,5 =洋红色,6 =青色,7 =白色; 8-15 = 0-7的明亮版本。

ESC [ 1 ; n]Set color nas the underline color.
ESC [ 2 ; n]Set color nas the dim color.
ESC [ 8 ]Make the current color pair the default attributes.
ESC [ 9 ; n]Set screen blank timeout to nminutes.
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.
ESC [ 15 ]Bring the previous console to the front(since Linux 2.6.0).
ESC [ 16 ; n]Set the cursor blink interval in milliseconds(since Linux 4.2).

Character sets

内核知道将字节转换为控制台屏幕符号的4种方式。这四个表是:a)Latin1-> PC,b)VT100图形-> PC,c)PC-> PC,d)用户定义的。

有两个字符集,分别称为G0和G1,其中之一是当前字符集。 (最初为G0。)键入haN使G1变为电流,键入haO导致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 haVhaO"将仅使G0成为当前电流,但不能保证G0指向表a)。在某些发行版中,有一个程序reset(1)只是执行" echo ha [c""。如果控制台的terminfo条目正确(并且具有rs1 = \ Ec条目),则" tput reset"也将起作用。

可以使用mapscrn(8)设置用户定义的映射表。映射的结果是,如果打印符号c,则符号s = map [c]被发送到视频存储器。对应于s的位图在字符ROM中找到,可以使用setfont(8)进行更改。

Mouse tracking

鼠标跟踪工具旨在返回兼容xterm(1)的鼠标状态报告。因为控制台驱动程序无法知道鼠标的设备或类型,所以仅当虚拟终端驱动程序收到鼠标更新ioctl时,这些报告才会在控制台输入流中返回。这些ioctl必须由鼠标感知的用户模式应用程序(例如gpm(8)守护程序)生成。

xterm(1)生成的鼠标跟踪转义序列将单个字符中的数字参数编码为value + 040。例如,aq!aq为1。屏幕坐标系基于1。

X10兼容模式在按下按钮时对编码位置和按下的鼠标按钮发送一个转义序列。通过发送ESC [? 9小时,并禁用ESC [? 9升按下按钮时,xterm(1)发送ESC [M bxy(6个字符)。这里b是button-1,x和y是按下按钮时鼠标的x和y坐标。这与内核生成的代码相同。

正常跟踪模式(在Linux 2.0.24中未实现)在按下和释放按钮时都会发送转义序列。修改器信息也被发送。通过发送ESC [? 1000小时并使用ESC禁用[? 1000升按下或释放按钮时,xterm(1)发送ESC [M bxy。 b的低两位对按钮信息进行编码:0 =按下MB1,1 =按下MB2,2 =按下MB3,3 =释放。高位编码表示按下按钮时按下的修饰符并加在一起:4 = Shift,8 = Meta,16 = Control。同样,x和y是鼠标事件的x和y坐标。左上角是(1,1)。

Comparisons with other terminals

描述了许多不同的终端类型,例如Linux控制台,它们是" VT100兼容的"。在这里,我们讨论Linux控制台与两个最重要的控制台DEC VT102和xterm(1)之间的区别。

控制字符处理

VT102还识别以下控制字符:

NUL(0x00)被忽略;
ENQ(0x05)触发了应答消息;
DC1(0x11,haQ,XON)恢复发送;
DC3(0x13,haS,XOFF)导致VT100忽略(并停止发送)除XOFF和XON之外的所有代码。

终端驱动程序可以启用类似VT100的DC1 / DC3处理。

xterm(1)程序(在VT100模式下)可识别控制字符BEL,BS,HT,LF,VT,FF,CR,SO,SI,ESC。

转义序列

在Linux控制台上未实现的VT100控制台序列:

ESC NSS2Single shift 2. (Select G2 character set for the next
character only.)
ESC OSS3Single shift 3. (Select G3 character set for the next
character only.)
ESC PDCSDevice control string (ended by ESC )
ESC XSOSStart of string.
ESC haPMPrivacy message (ended by ESC )
ESC |STString terminator
ESC * ...Designate G2 character set
ESC + ...Designate G3 character set

程序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(它回答ESC [?1; 2 c,"我是具有高级视频选项的VT100")和ESC ha ... ESC \,具有与上述相同的含义。它接受ESC(,ESC),ESC *,ESC +,后接0,A,B(分别表示DEC特殊字符和线条图集,UK和US-ASCII)。

用户可以配置xterm(1)来响应特定于VT220的控制序列,并根据其配置和初始化方式将其自身标识为VT52,VT100和更高版本。

它接受ESC](OSC)来设置某些资源。除了ECMA-48字符串终止符(ST),xterm(1)接受BEL来终止OSC字符串。以下是xterm(1)识别的一些OSC控制序列:

ESC ] 0 ; txtSTSet icon name and window title to txt.
ESC ] 1 ; txtSTSet icon name to txt.
ESC ] 2 ; txtSTSet window title to txt.
ESC ] 4 ; num; txtSTSet ANSI color numto txt.
ESC ] 10 ; txtSTSet dynamic text color to txt.
ESC ] 4 6 ; nameSTChange log file to name(normally disabled
by a compile-time option)
ESC ] 5 0 ; fnSTSet font to fn.

它以稍微修改的含义识别以下内容(保存更多状态,表现得更接近VT100 / VT220):

ESC 7 DECSCSave cursor
ESC 8 DECRCRestore cursor

它也承认

ESC FCursor to lower left corner of screen (if enabled by
xterm(1)'s hpLowerleftBugCompatresource)
ESC lMemory lock (per HP terminals).
Locks memory above the cursor.
ESC mMemory unlock (per HP terminals).
ESC nLS2Invoke the G2 character set.
ESC oLS3Invoke the G3 character set.
ESCLS3R
ESC }LS2RInvoke the G2 character set as GR.
ESC tiLS1RInvoke the G1 character set as GR.

它还可以识别ESC%,并提供比Linux控制台更完整的UTF-8实现。

CSI序列

例如,来自X11R5的xterm(1)的旧版本将闪烁SGR解释为粗体SGR。后来实现ANSI颜色的版本,例如1995年的XFree86 3.1.2A,通过允许将眨眼属性显示为颜色来改进了此功能。现代版本的xterm将闪烁的SGR实现为闪烁的文本,并且仍然允许使用彩色文本作为SGR的替代呈现。直到X11R6.8发行版(包含XFree86 xterm)后,股票X11R6版本才认识到色彩设置SGR。 xterm也可以识别Linux识别的所有ECMA-48 CSI序列,但是xterm(1)实现了Linux无法识别的多个ECMA-48和DEC控制序列。

xterm(1)程序可以识别上面列出的所有DEC私有模式序列,但不能识别Linux私有模式序列。有关xterm(1)自己的私有模式序列的讨论,请参阅Edwards Moy,Stephen Gildea和Thomas E. Dickey随X发行的Xterm控制序列文档。该文档虽然简短,但是比本手册页长得多。有关按时间顺序的概述,

详细说明对xterm的更改。

vttest程序

演示了许多控制序列。 xterm(1)源代码发行版还包含行使其他功能的示例脚本。

日期:2019-08-20 18:01:45 来源:oir作者:oir