Friday, July 11, 2008

Linux/Unix Terminal

Terminfo database
At first, we need a database which describes capabilities of terminals. There are two basic databases there: TermCap and Terminfo. The latter is more widely used currently. You can create a new description file and edit it in textual mode. Then you can use a tool called tic to compile original text into binary format which is used by other programs. The Terminfo is generally located under: /usr/share/terminfo.
Some useful information you may care about can be accessed here and here. From there, you also can download latest terminfo database which contains information about many kinds of terminals.
Single UNIX Specification contains a section defining terminfo source format which can be accessed here.

Text terminal
Control terminals in low-level:
ioctls: gives very low-level access.
termios: "The termios functions describe a general terminal interface that is provided to control asynchronous communications ports."(From manpage).

High-level wrapper libraries:
Low-level APIs(ioctls/termios) don't hide complexity of terminal programming. Your programs must depend on specific terminal type. Some wrapper libraries exist which provide portable interface to many terminal types.
(1) ncurses (http://invisible-island.net/ncurses/ncurses.html)
Allow programmer to write TUI in a terminal-independent manner. It also does optimization work to reduce latency when using remote shells. Written in C.
Moreover, another package called Curses Development Kit(CDK) provides more functionalities. Mainly, it provides a library of curses widgets which can be used and customized in your programs.
Here is a good tutorial about how to use Ncurses in C.
Though ncurses tests $TERMINFO first, otherwise it reads from $HOME/.terminfo before the standard location,
(2) shell curses(http://www.mtxia.com/css/Downloads/Scripts/Korn/Functions/shcurses/)
It enables shell programmers to do TUI programming easily. It worked originally in Korn shell. I am not sure whether it has been ported to other shells.
(3) S-Lang

GUI
Currently, GUI exists almost everywhere in the computer world. Pure text terminal is not used very often. X window system is the big player in linux/unix. As a result, programmers can provide a GUI to the customers instead of TUI.
Some interesting articles: State of Linux graphics, various Linux/Unix desktops, X window resource/link collection.

Pseudo terminal/Terminal emulator
With disappearance(maybe a few ones still are being used) of traditional serial port terminals, code of terminal control was rewritten to promote reuse. So currently terminal is just a term describing such devices/programs which can send requests to master and handle responses from master. No physical serial port is involved. Most of the time we use remote shells running on internet. In directory /dev/, there are corresponding device files for these pseudo terminals. Commonly used file names are pty*, pts*,ptty...

Some useful commands:
(*) tic - the terminfo entry-description compiler.
translates  a terminfo file from source format into compiled format.  The compiled format is necessary for use with the library routines in ncurses.
(*)captoinfo (alias for tic) - convert a termcap description into a terminfo description
   infotocap(alias for tic) -
(*) screen: The screen program allows you to run multiple virtual terminals, each with its own interactive shell, on a single physical terminal or terminal emulation window.
/******* Set/Rest terminal ******/
(*) tset : terminal initialization
(*) setterm: set terminal attributes.
    setterm  writes  to  standard  output  a character string that will invoke the specified terminal capabilities. Where possible terminfo is consulted to find the string to use. The attribute names used in this command are different from cap names in terminfo database!!!
(*) stty - prints or changes terminal characteristics, such as baud rate.(related to line settings)
/****** Query  ********/
(*) infocmp - compare or print out terminfo descriptions. It can be used to print all capabilities.
(*) tput : query terminfo database. Query a specific capability.
e.g. tput setaf     //get foreground color.
       tput cols      //get number of columns
       tput longname  //get longname description of current terminal.
(*)  toe - table of (terminfo) entries. With  no  options,  toe  lists  all available terminal types by primary name with descriptions.

How to know your current terminal type?
tset -q
infocmp
How to tell linux type of your terminal?
Usually, you must explicitly tell linux type of your terminal. You can set environment variable $TERM.