5

Help me to decipher the escape sequences created by ncurses library and catched by strace. I am exploring how ncurses interacts with terminal and want to understand its "handshake protocol". I have found some descriptions already, but didn't understand all of them though, like "Set cursor key to cursor".

echo $TERM prints xterm-256color.

Original

write(1, "\33[?1049h\33[22;0;0t\33[1;39r\33(B\33[m\33[4l\33[?7h\33[H\33[2J", 46) = 46
write(1, "Hello World !!!", 15)   = 15  
write(1, "\33[39;1H\33[?1049l\33[23;0;0t\r\33[?1l\33>", 32) = 32

My assumptions

write(1, "
    \33[?1049h       # go to alternate screen
    \33[22;0;0t
    \33[1;39r
    \33(B            # Set United States G0 character set 
    \33[m            # Turn off character attributes
    \33[4l
    \33[?7h          # Set auto-wrap mode
    \33[H            # Move cursor to upper left corner
    \33[2J           # Clear entire screen
", 46) = 46

write(1, "Hello World !!!", 15)   = 15

write(1, "
    \33[39;1H
    \33[?1049l       # Go back to the initial screen
    \33[23;0;0t\r
    \33[?1l          # Set cursor key to cursor 
    \33>
", 32) = 32

The testing program source

int main()
{   
    napms(25000);               /* This pause is needed to catch the process by strace*/
    initscr();                  /* Start curses mode          */
    printw("Hello World !!!");  /* Print Hello World          */
    refresh();                  /* Print it on to the real screen */
    endwin();                   /* End curses mode        */

    return 0;
}
3

1 Answer 1

5

For XTerm and anything that claims compatibility with it, you'll want this:

https://invisible-island.net/xterm/ctlseqs/ctlseqs.html

You'll also need the manual for the VT100 terminal, which XTerm emulates and expands on:

https://vt100.net/docs/vt100-ug/contents.html

The Linux console_codes(4) man page describes the control codes used by the Linux console, which is also a superset of VT100 and the man page sometimes has more verbose descriptions than the other sources above:

http://man7.org/linux/man-pages/man4/console_codes.4.html

The unknown codes in your example:

\33[22;0;0t

Here, the first part \33[ (or ESC [ ) is known as CSI or Control Sequence Introducer.

The CSI <number> ; <number> ; <number> t is a window manipulation sequence. Control sequences ending with t always take three numeric parameters, but won't always use all of them. The first parameter is 22, and the second is 0, so this code tells the terminal emulator to save the current window and icon titles so that they can be restored later.

\33[1;39r

This is CSI <number> ; <number> r. The meaning is "set scrolling region". Setting this to something smaller than the size of the current window would efficiently allow keeping something static like a menu line at the top of a TUI display, a status line at the bottom, or both while displaying a lot of text within the scrolling region.

\33[4l

This is CSI <one or more numbers> l. The meaning is "reset mode". Value 4 resets (disables) "insertion-replace mode", or in plain terms, tells that anything printed to the screen should simply overwrite what was there before.

\33[39;1H

This is CSI <number> ; <number> H. This moves the cursor to the 39th line, 1st column.

\33[23;0;0t

This is another window manipulation sequence. This restores the previously-saved window and icon titles. Obviously your test program did not change the titles at all, but these sequences are part of the standard initialization/exit procedures done by initscr() and endwin() respectively.

\33[?1l          # Set cursor key to cursor

This sets the cursor keys of the VT100 keypad to the regular "cursor key mode". There was also another mode, intended to allow these keys to be used for application-specific purposes, like an extra set of function keys. The VT100 terminal produced different output for these keys according to the mode settings; this just ensures that if the application switched the cursor keys to a non-default mode, they will be returned to default mode before the program exits.

\33>

This is just ESC >. This is similar to the previous code, but for the numeric keypad.

1
  • 1
    "vt100" is too often, as here, bandied about for things that are not VT 100s. The XTerm FAQ goes into some detail on this error. The Linux KVTs are not supersets of VT 100s, or even exactly compatible with DEC VTs. They are very limited terminal emulators, with some well-known significant differences from DEC VTs. Ironically, the SCO Console is a much clearer relation. Other errors: CSI tdoes not always take 3 parameters. The 3 parameter form is a DTTerm extension. VT100s didn't have function keys. And really terminfo is the way to understand ncurses.
    – JdeBP
    May 5, 2020 at 0:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .