migueldeicaza / SwiftTerm

Xterm/VT100 Terminal emulator in Swift
MIT License
992 stars 146 forks source link

Incomplete escapes hang the terminal #145

Closed lunixbochs closed 4 years ago

lunixbochs commented 4 years ago

If you print an incomplete escape sequence, e.g. echo -e '\033]', the terminal parser seems to go into an unrecoverable state.

migueldeicaza commented 4 years ago

This is the intended behavior.

That sequence is "Operating System Command" sequence that can provide arbitrary data to the terminal and terminates with a String Terminator which is either ESC \ or 0x9c.

xterm does offer a mode brokenLinuxOsc to emulate broken implementations that terminate when a control with a carriage return, but this breaks applications that want to encode those, from: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html

iTerm2 behaves just like SwiftTerm or xterm in the default configuration (without enabling the broken mode)

o   Some controls (such as OSC ) introduce a string mode, which is ended
    on a ST  (string terminator).

    ECMA-48 describes only correct behavior, telling what types of char-
    acters are expected at each stage of the control sequences.  It says
    that the action taken in error recovery is implementation-dependent.
    XTerm decodes control sequences using a state machine.  It handles
    errors in decoding i.e., unexpected characters, by resetting to the
    initial (ground) state.  That is different from the treatment of
    unimplemented (but correctly formatted) features.

    If an application does not send the string terminator, that is also
    an error from the standpoint of a user.  To accommodate users of
    those applications, xterm has resource settings which allow work-
    arounds:

    o   The Linux console's palette sequences do not use a string termi-
        nator.  The brokenLinuxOSC resource setting tells xterm to
        ignore those particular sequences.

    o   The terminal should accept single-byte controls within the
        string.  But some applications omit a string terminator, like
        the Linux console.  The brokenStringTerm resource setting tells
        xterm to exit string mode if it decodes a common control charac-
        ter such as carriage return before the string terminator.
lunixbochs commented 4 years ago

I just installed the latest iTerm, and it recovers from this state with no configuration, as does Terminal.app.

My mistake: iTerm is recovering due to ZSH, which fixes it when initializing the line editor. Bash hangs as expected.

migueldeicaza commented 4 years ago

Well, it is working as intended. Like I said, I might consider someone sending a patch to enable a "broken mode" like xterm does, but this does break scenarios where you want to send newlines and other characters to the OSC, so it wont be the default.