Cursor movement in terminals

system-programmingterminal

I'm trying to wrap my head how shells move the screen cursor around (Moving around input the arrow keys and such).

I've been doing a lot of testing, and I haven't found any system call that allows for movement of the screen cursor in a terminal. Is this done using specific terminal extensions? If so, is there any documentation as to how this is achieved for, say, xterm?

This has been driving me crazy, any light you can shed on this is appreciated.

Best Answer

There is no such system call, or set of system calls. It's all done by convention.

Back in the old days, when a "terminal" was a large clunky piece of equiment linked to a computer via a cable, "smart terminals" would do things like move the cursor, or draw a line, or fill in a polygon with colors. The smart terminal usually did this via "escape sequenes". A program issued a special sequence of byte values, usually beginning with ASCII 0x1b, 'ESC', to move a cursor, or color a word, or some special thing other than imitating a teletypewriter.

That's the "convention". Nowadays, a "terminal" is almost always just one window among many on a bitmapped screen, xterm, rxvt, etc etc. It's up to xterm (or rxvt or whatever) to read the bytes coming from "the program" running in it, and interpret those bytes according to a given convention. I just typed this in one of my xterm windows:

6 % echo $TERM 
xterm

But I know that old, old xterms used to say "vt100" in that case. Any program that "moves the cursor" has to output escape sequences for the "xterm" convention to move the cursor in an xterm instance.

You could do man curses and man terminfo for more information.

PS

People have done whole windowing systems with escape sequences. See MGR as an example.