Bash 一个模糊不清的例子:记录在案的VT100';软包装&x27;逃逸序列?

Bash 一个模糊不清的例子:记录在案的VT100';软包装&x27;逃逸序列?,bash,console,word-wrap,ansi-escape,vt100,Bash,Console,Word Wrap,Ansi Escape,Vt100,当通过SSH(终端类型设置为vt100)连接到远程BASH会话时,当光标指向第80列时,控制台命令行将软换行 我试图发现的是,此时发送的序列是否记录在任何地方 例如,发送以下字符串 std::string str = "0123456789" // 1 "0123456789" "0123456789" // 3 "0123456789"

当通过SSH(终端类型设置为vt100)连接到远程BASH会话时,当光标指向第80列时,控制台命令行将软换行

我试图发现的是,此时发送的
序列是否记录在任何地方

例如,发送以下字符串

    std::string str = "0123456789"  // 1
                        "0123456789"
                        "0123456789"    // 3
                        "0123456789"
                        "0123456789"    // 5
                        "012345678 9"
                        "0123456789_"   // 7
                        "0123456789"
                        "0";
从主机(Linux Mint)返回以下响应

012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789_01234567890

观察到的行为并不是bash的一部分;相反,它是
readline
库行为的一部分。如果只是使用
echo
(bash内置)输出足够的文本以强制自动换行,则不会发生这种情况,如果bash生成的错误消息比控制台更宽,也不会发生这种情况。(例如,尝试使用参数超过80个字符的命令
,该参数与任何现有文件都不对应。)

因此,它不是官方的“软包装序列”,也不是任何标准的一部分。相反,这是一个务实的解决方案,解决了许多与控制台显示管理相关的恼人问题之一

换行的终端实现存在歧义:

  • 在最右侧位置插入字符后,端子将换行

  • 在发送下一个字符之前,终端将换行

  • 因此,不可能在最后一列位置之后可靠地发送换行符。如果终端已经包装(上面的选项1),那么换行符将创建一个额外的空行。否则(选项2),以下换行符将被“吃掉”

    如今,几乎所有的终端都采用了方案2的一些变体,即DEC VT-100终端的性能。在终端描述数据库的词汇表中,这被称为
    xenl
    :eat新线故障

    选项2实际上有两个可能的子变量。在实际由VT-100(和xterm)实现的一种情况下,光标在行的末尾处于异常状态;实际上,它离屏幕只有一个字符的位置,因此您仍然可以在同一行中对光标进行退格。其他历史终端“吃”了换行符,但无论如何都将光标定位在下一行的开头,这样就不可能退格。(除非终端具有
    bw
    功能。)

    这就给需要精确跟踪光标位置的程序带来了问题,即使对于回音输入这样的简单应用程序也是如此。(显然,回显输入的最简单方法是让终端自己回显输入,但这样就无法实现额外的控制字符,如制表符完成。)假设用户输入的文本一直到右边距,然后键入退格字符以删除最后键入的字符。通常,您可以通过输出
    cub1
    (向左移动1)代码和
    el
    (清除到行尾)来实现退格删除。(如果删除是在中间行,但原理是相同的。)

    但是,如果光标可能位于下一行的开头,这将不起作用。如果您知道光标位于下一行的开头,则可以在执行
    el
    之前先向上移动,然后向右移动,但如果光标仍在同一行上,则无法执行此操作

    从历史上看,被认为是“正确的”是强制光标返回到下一行。(以下引用自
    ncurses
    发行版中的文件
    terminfo.src
    。我不知道是谁写的,也不知道是什么时候写的):

    xn
    xenl
    的缩写)


    笔记
  • 当我键入此答案时,注释位于
    git
    存储库当前视图中
    display.c
    的第1326行。在将来的版本中,它可能位于不同的行号,因此提供的链接将无法工作。如果您注意到它已更改,请随时更正链接

  • 在这个答案的原始版本中,我将这个过程描述为“终端处理民俗的一部分”,其中我使用“民俗”一词来描述程序员之间传递的知识,而不是学术文本和国际标准的一部分。虽然“民间传说”经常带有负面含义,但我使用它时没有这种偏见。“知识”(根据)指“通过教育或经验积累起来的关于某一特定学科的所有事实和传统”,来源于一个古老的日耳曼词,意思是“教学”。因此,民俗学是“民间”积累的教育和经验,而不是建立:在埃里克·S·雷蒙德的比喻中,民俗学是集市的知识基础

    这种用法引起了至少一位技术高超的人的注意,他们建议使用“深奥”一词来描述关于终端处理的这一点信息。“深奥”(同样根据wiktionary)适用于源自希腊语的信息,“旨在或可能仅为少数具有专业知识或兴趣的人或开明的内部圈子所理解”ἐσωτερικς,“内圈”。(换句话说,就是对大教堂的了解。)

    虽然语义讨论至少是有趣的,但我改变了文本,使用了希望不那么情绪化的单词“folkcraft”


  • 将线包装作为一种特殊情况有不止一个原因(这似乎是一个不合适的术语):

    • xterm FAQ是讨论vt100换行的众多地方之一
    • vim和vim都注意不要使用光标寻址来避免换行,因为这会干扰sele
      01234567890123456789012345678901234567890123456789012345678<WS><WS><CR>90123456789_01234567890
      
      # Note that the <xenl> glitch in vt100 is not quite the same as on the Concept,
      # since the cursor is left in a different position while in the
      # weird state (concept at beginning of next line, vt100 at end
      # of this line) so all versions of vi before 3.7 don't handle
      # <xenl> right on vt100. The correct way to handle <xenl> is when
      # you output the char in column 80, immediately output CR LF
      # and then assume you are in column 1 of the next line. If <xenl>
      # is on, am should be on too.
      
        /* If we're at the right edge of a terminal that supports xn, we're
           ready to wrap around, so do so.  This fixes problems with knowing
           the exact cursor position and cut-and-paste with certain terminal
           emulators.  In this calculation, TEMP is the physical screen
           position of the cursor. */
      
      # # --------------------------------
      #
      # dec: DEC (DIGITAL EQUIPMENT CORPORATION)
      #
      # Manufacturer: DEC (DIGITAL EQUIPTMENT CORP.)
      # Class:    II
      # 
      # Info:
      #   Note that xenl glitch in vt100 is not quite the same as concept,
      #   since the cursor is left in a different position while in the
      #   weird state (concept at beginning of next line, vt100 at end
      #   of this line) so all versions of vi before 3.7 don't handle
      #   xenl right on vt100. The correct way to handle xenl is when
      #   you output the char in column 80, immediately output CR LF
      #   and then assume you are in column 1 of the next line. If xenl
      #   is on, am should be on too.
      #   
      #   I assume you have smooth scroll off or are at a slow enough baud
      #   rate that it doesn't matter (1200? or less). Also this assumes
      #   that you set auto-nl to "on", if you set it off use vt100-nam 
      #   below.
      #   
      #   The padding requirements listed here are guesses. It is strongly
      #   recommended that xon/xoff be enabled, as this is assumed here.
      #   
      #   The vt100 uses rs2 and rf rather than is2/tbc/hts because the 
      #   tab settings are in non-volatile memory and don't need to be 
      #   reset upon login. Also setting the number of columns glitches 
      #   the screen annoyingly. You can type "reset" to get them set.
      #
      # smkx and rmkx, given below, were removed. 
      # smkx=\E[?1h\E=, rmkx=\E[?1l\E>,
      # Somtimes smkx and rmkx are included.  This will put the auxilliary keypad in
      # dec application mode, which is not appropriate for SCO applications.
      vt100|vt100-am|dec vt100 (w/advanced video),