DynaComm & UNIX termcap/terminfo
This paper describes how the termcap or terminfo database defines terminals on UNIX systems and how this can impact the behavior of connected terminals/emulators.
The termcap or terminfo is used by UNIX systems to allow host applications to work with different terminal devices. The host application responds to and uses standard commands defined in termcap or terminfo, which translates them to the corresponding escape sequence before being sent to the terminal/emulator. This insolates the host application from terminal-specific development, but at the same time, involves the termcap and/or terminfo as a role player in running host applications.
In the simple case of connecting with well-defined terminals such as the DEC VT220, the correct definition of the terminal in termcap or terminfo is dictated by the terminal specification, or at least the subset of VT220 terminal functionality used by applications on that UNIX system. However, termcap and terminfo can be modified for various reasons, to add custom functionality to a host application, to make use of new terminals, or to define a hybrid terminal that uses features or keymappings of a different terminal. Missing or modified termcap or terminfo entries may instigate issues such as the ones discussed later in this paper.
There is one more relevant variable to introduce here, the UNIX TERM variable. When a connection is made, the UNIX system uses one or more ways to determine the terminal model with which the user is connecting. This may be determined by a system default, by a logon script for the UNIX account, within the telnet negotiation when connecting via TCP/IP, or by querying the terminal/emulator for its terminal model. The important point to make here is that it matters what the UNIX system understands to be the terminal model since this is used to find the section in termcap or terminfo that defines the terminal. Your UNIX system may have a well tested termcap or terminfo definition of the VT220 terminal, but if something changes and you are identified as a VT320 terminal the next time you connect, you may get entirely different terminal behavior due to a faulty or customized VT320 terminal definition.
Termcap / terminfo problems may exhibit different symptoms. For example:
- Emulators behave differently when connected to the same host. This may happen if the termcap/terminfo and/or emulator was modified, so one or both are non compliant with the terminal specification.
- A terminal/emulator works differently when running the same application on a different host. The two UNIX systems may have differences in their termcap/terminfo.
- The same emulator, connecting to the same UNIX system, running the same host application behaves differently, depending on the UNIX logon used. This may happen if the user’s environment is set to use a different terminal than the default, or if the user has a modified termcap in their home directory.
What are termcap and terminfo
Both the termcap and terminfo terms apply to databases on the UNIX host that define the capabilities of many terminals. The difference between the two is that the termcap is an ASCII, text based database, while the terminfo is a compiled database. The terminfo is more efficient for host programs to access, but the termcap is easier to read and modify. UNIX systems are able to use both, but pure UNIX System V systems use terminfo and the Berkeley Software Distribution (BSD 4.x) systems use termcap. Either the termcap or the terminfo can be used by a host application and this depends on which one the program wants to use. If the program has specified that it will only use terminfo, then the termcap database has no effect on the terminal operation.
The termcap file is located in the /etc/ directory and is either noted as “.termcap” or just “termcap”. Any UNIX text editor (vi for example) can be used to view it on most UNIX Systems. The terminfo database is usually located beneath /etc/ or /usr/ in the lib/terminfo directory. In this directory there is a directory for each letter of the alphabet which contains the compiled terminfo entry for a terminal. For example, beneath the /etc/lib/terminfo/v directory, the vt220 entry should exist. To view this entry, infocmp must be used to decompile and display the entry. Performing an “ls” in any of the alphabetic directories will show what terminals starting with the corresponding directory letter are supported in the terminfo database.
Both databases are similar in that most of the entries’ syntax and function representation are the same. View the man pages on either termcap or terminfo for further information on syntax and structure, especially when looking at terminal definitions on a new UNIX version. These databases are not standard across vendor’s versions of UNIX and functions supported may differ between vendors. Also, it is important to know which database the host application uses, because, as noted above, some use terminfo and some use termcap. This is not an environment variable that can be viewed.
How termcap and terminfo work
The termcap and terminfo databases represent how a UNIX program should control the display of data. In UNIX, there are “terminal functions” with standard names which can be accessed by UNIX programs for display purposes. For example, if a program wants to act on the user pressing the UP arrow, then it will wait for an “up” signal from the operating system, regardless of what terminal Escape sequence generates the “up” signal. This sequence is defined in the termcap and terminfo files. So, if the user is working with a VT220 terminal or emulation and the TERM variable in there UNIX session is set to vt220, then the terminal/emulator will send the sequence ESC[A when the UP arrow key is pressed. This will be translated by UNIX via the termcap to the “up” function which is sent to the active program running on the UNIX system. Figure 1 illustrates this process.
Figure 1 -- Moving the Cursor Up
How termcap and terminfo affect terminal behavior
As introduced earlier, the UNIX system identifies the terminal model of the user when the connection is made, or soon thereafter. When connecting via TELNET, this process is transparent to the user and occurs in the Terminal Type Negotiation part of initiating a connection. With DynaComm, the default selection is to use the currently selected emulation for terminal type negotiation, but this setting can be altered within the connector properties to either disable this negotiation or specify a custom terminal type. In some cases, this may be necessary since the terminal ID response is not a complete standard and the different flavors of UNIX sometimes expect different names for a terminal. For example, some UNIX hosts respond to “vt220” while others only respond to “DEC-vt220”. The termcap and terminfo files define what the UNIX host expects in regard to a Terminal Type response. The Terminal Type can also be set manually by defining the TERM environment variable once connected to the UNIX operating system. In cases where the required TERM variable is unknown, the UNIX “set” command can be used to show the values of all environment variables in the UNIX session. If a UNIX logon script is used, this may also set the TERM variable.
In addition to Terminal Type problems, the terminal definition in the UNIX termcap can instigate incompatibilities. There is no “official” definition that is always used for a vt220 termcap entry. Each flavor of UNIX may use a different syntax for the termcap entry, and the system administrators can alter this. Since there is no standard that is adhered to in the definition of the termcap, an apparent emulator problem could actually be caused by an invalid termcap entry. For example, ESC [ nA (where n is an integer) is defined to move the Cursor up in the VT220 emulation. But, the integer n can be omitted or 0 and the emulation will default the value to 1. Some termcap entries are defined in such a way as to require a value of 1 for the integer and they do not understand when it is omitted.
Other manifestations of the previous problem are a termcap definition which includes features not specifically defined in the Terminal Specifications, or one which does not include needed features. This causes UNIX programs to behave oddly and/or inefficiently. For example, “vi” is a common UNIX editor. When launched, it consults the TERM variable and then looks in the corresponding termcap entry to see what features the terminal supports. If the termcap says that this particular terminal supports “insert character”, then when “vi” needs to insert a character it uses this function. But, if the terminal/emulator does not support this, “vi” will not seem to work correctly. A similar thing happens when “vi” does not see “insert line” and “delete line” defined in the termcap. Instead of scrolling by removing a line from the top or bottom and adding a line to the bottom or top (respectively), it sends a whole new screen that has been scrolled. This is quite a bit slower since it has to send about 2000 bytes as opposed to just around 100 bytes per line scrolled. This may seem to be a speed issue with the emulation, but it is actually a problem with how the termcap entry is defined.
Modifying the termcap or terminfo database
For most purposes, the modification of these files can only take place in the user’s directory. UNIX allows for the use of alternate databases through several different means. There are several options in specifying where a program will look for the termcap that may depend on the UNIX system. Some UNIX systems require the variable TERMCAP to be set to the location of the desired termcap file or they will use the system termcap. Others will also use an alternate database if it is in the user’s directory and the filename is termcap. In either case, if the entry specified by the TERM environment variable is not in the specified termcap database, the program will try to find it in the system termcap database. Once the system file is copied to the users directory, the user should be able to use vi to add or alter terminal entries.
The terminfo database is a little more difficult to alter. It is also important to copy and alter this database in the user’s directory so the system database will not be corrupted. To change where a program will look for the terminfo database, the TERMINFO variable can be set to the directory where the terminal descriptions will be located. Any text editor can be used to create the entry, but with terminfo, the entry must be compiled with the tic command.
For more information on modifying either database, consult the man pages for the UNIX system being used.
Debugging terminal emulation problems
The following steps should be followed when debugging an emulation problem that occurs connecting to a UNIX host:
- Use the trace facility provided with DynaComm to capture and view the data sent\received by DynaComm. The trace captures all data, including control characters and escape sequences which are hidden from the user. Check to see if the data is a valid sequence for what is trying to be accomplished. For example, ESC[A if the UP arrow was pressed. The on-line documentation gives the definitions of the terminal functions and keys, or if available, check the terminal definition provided by the terminal manufacturer. Contact FutureSoft Professional Support Services to review the traces or assist with the analysis.
- If DynaComm is sending the proper data according to the terminal specification, then the host is probably in error. Check the TERM environment variable to see if it is set to the correct value. Also, check the termcap and terminfo databases to see what the TERM variable should be set to if unknown. If the TERM variable is set correctly, then the problem is probably with the terminal definition in one of the databases. At this point, which database is being used must be discerned. The man pages on the databases (termcap and terminfo) may help, or make a local copy of termcap and test a modification to see if it has the expected effect. Now the corresponding terminal definition should be examined to see if it has the problem Escape sequence in it.
- If the escape sequence is not in the database, then find the corresponding function in the man page and modify the entry. If it is already in the database, then it should be verified that it is correct with respect to syntax and variable expectation.
More detailed information about the termcap or terminfo databases can be obtained from the book termcap & terminfo from O’Reilly & Associates or from the man pages on termcap, term and terminfo that reside on any UNIX host.