Unix 我可以确定终端是否解释C1控制代码吗?
ISO/IEC 2022定义了。C0集是ASCII、ISO-8859-1和UTF-8(如ESC、CR、LF)中Unix 我可以确定终端是否解释C1控制代码吗?,unix,ncurses,gnu-screen,vt100,termcap,Unix,Ncurses,Gnu Screen,Vt100,Termcap,ISO/IEC 2022定义了。C0集是ASCII、ISO-8859-1和UTF-8(如ESC、CR、LF)中0x00和0x1f之间的常见代码 一些VT100终端仿真器(例如,screen(1),PuTTY)也支持C1集。这些是介于0x80和0x9f之间的值(例如,0x84将光标向下移动一行) 我正在显示用户提供的输入。我不希望用户输入能够改变终端状态(例如移动光标)。我目前正在过滤掉C0集合中的字符代码;然而,如果终端将它们解释为控制代码,我也希望有条件地过滤掉C1集 是否有一种方法可以从数据
0x00
和0x1f
之间的常见代码
一些VT100终端仿真器(例如,screen(1)
,PuTTY)也支持C1集。这些是介于0x80
和0x9f
之间的值(例如,0x84
将光标向下移动一行)
我正在显示用户提供的输入。我不希望用户输入能够改变终端状态(例如移动光标)。我目前正在过滤掉C0集合中的字符代码;然而,如果终端将它们解释为控制代码,我也希望有条件地过滤掉C1集
是否有一种方法可以从数据库(如
termcap
)中获取此信息?我能想到的唯一方法是使用C1请求并测试返回值:
$ echo `echo -en "\x9bc"`
^[[?1;2c
$ echo `echo -e "\x9b5n"`
^[[0n
$ echo `echo -e "\x9b6n"`
^[[39;1R
$ echo `echo -e "\x9b0x" `
^[[2;1;1;112;112;1;0x
以上是:
CSI c Primary DA; request Device Attributes
CSI 5 n DSR; Device Status Report
CSI 6 n CPR; Cursor Position Report
CSI 0 x DECREQTPARM; Request Terminal Parameters
ESR维护()的terminfo/termcap在用户字符串7和9(user7/u7,user9/u9)中有两个这样的请求:
当然,如果您只想防止显示损坏,可以使用
less
方法,让用户在显示/不显示控制字符之间切换(-code>less中的-r和-r选项)。此外,如果您知道输出字符集,ISO-8859字符集为控制代码保留了C1范围(因此在该范围内没有可打印的字符)。实际上,PuTTY似乎不支持C1控制
测试此功能的常用方法是使用,它提供了用于分别更改输入和输出以使用8位控件的菜单项。PuTTY未通过对每个菜单项的健全性检查,如果检查被禁用,结果将确认PuTTY不支持这些控件。我认为没有一种简单的方法可以查询终端是否支持这些控件。您可以尝试一些令人讨厌的黑客解决方法(比如打印它们,然后查询光标位置),但我真的不推荐使用这些方法 我想你可以无条件地过滤掉这些C1代码。Unicode声明U+0080。。不管怎么说,U+009F范围作为控制字符,我认为你不应该用它们来做任何不同的事情
(注意:您将示例
0x84
用于光标向下。事实上,U+0084
以终端使用的任何编码编码,例如,UTF-8的0xC2 0x84
进行编码。)100%自动完成这项工作充其量是一项挑战。许多(如果不是大多数的话)Unix接口都是智能的(xterms和诸如此类的),但实际上您不知道是否连接到一台或一台运行MSDOS的PC
如果没有应答,您可以尝试一些终端询问转义序列和超时。但是,你可能不得不回过头来问用户他们使用的是哪种终端
# INTERPRETATION OF USER CAPABILITIES
#
# The System V Release 4 and XPG4 terminfo format defines ten string
# capabilities for use by applications, .... In this file, we use
# certain of these capabilities to describe functions which are not covered
# by terminfo. The mapping is as follows:
#
# u9 terminal enquire string (equiv. to ANSI/ECMA-48 DA)
# u8 terminal answerback description
# u7 cursor position request (equiv. to VT100/ANSI/ECMA-48 DSR 6)
# u6 cursor position report (equiv. to ANSI/ECMA-48 CPR)
#
# The terminal enquire string should elicit an answerback response
# from the terminal. Common values for will be ^E (on older ASCII
# terminals) or \E[c (on newer VT100/ANSI/ECMA-48-compatible terminals).
#
# The cursor position request () string should elicit a cursor position
# report. A typical value (for VT100 terminals) is \E[6n.
#
# The terminal answerback description (u8) must consist of an expected
# answerback string. The string may contain the following scanf(3)-like
# escapes:
#
# %c Accept any character
# %[...] Accept any number of characters in the given set
#
# The cursor position report () string must contain two scanf(3)-style
# %d format elements. The first of these must correspond to the Y coordinate
# and the second to the %d. If the string contains the sequence %i, it is
# taken as an instruction to decrement each value after reading it (this is
# the inverse sense from the cup string). The typical CPR value is
# \E[%i%d;%dR (on VT100/ANSI/ECMA-48-compatible terminals).
#
# These capabilities are used by tac(1m), the terminfo action checker
# (distributed with ncurses 5.0).
$ echo `tput u7`
^[[39;1R
$ echo `tput u9`
^[[?1;2c