Section 3.34. Determining Your Terminal s Type: tset


[Page 77 (continued)]

3.34. Determining Your Terminal's Type: tset

Several GNU utilities, including the two standard editors vim and emacs, need to know what kind of terminal you're using so that they can control the screen correctly. In most cases, you're working in a terminal window or on the console itself, and the system already knows those types, so you shouldn't need to help it out. But if you are connected to a Linux system via the network or a dial up line from another type of system, it might not realize that the special characters used to drive your terminal will be different.


[Page 78]

The type of your terminal is stored by your shell in something called an environment variable. Environment variables are described in more detail in Chapter 5, "The Linux Shells." You may think of them as being rather like global variables that hold strings.

Before vim or emacs can work correctly, your shell's TERM environment variable must be set to your terminal type. There are several ways that this variable can be set:

  • Your shell startup file, described in the next section, can set TERM directly by containing a line of the form: "setenv TERM vt100" (C shell) or "TERM=vt100 ; export TERM" (Korn and Bash shells). This is only practical if you know the type of your terminal in advance and you always log into the same terminal.

  • You can manually set TERM from a shell.

If it is necessary to manually set your terminal type, the best way is to use tset from your login shell. Figure 3-45 describes how tset works.

Figure 3-45. Description of the tset command.

Utility: tset -s [-ec] [-ic] {-m portId:[?]terminalType}*

tset is a utility that tries to determine your terminal's type and then resets it for standard operation.

If the -s option is not used, tset assumes that your terminal type is already stored in the TERM environment variable, and resets it using terminal capability information stored in "/etc/termcap" or the terminfo database.

If you use the -s option, tset examines the "/etc/ttys" file and tries to map your terminal's port to a terminal type listed there. It then generates shell commands to standard output that, when executed, cause the TERM and TERMCAP environment variables to be set properly. tset uses the contents of the SHELL environment variable to determine which kind of shell commands to generate. Filename expansion must be temporarily inhibited during the execution of the command sequence that tset generates, since there could be special characters that might be misinterpreted by the shell. Examples of this follow shortly.

The -e option sets the terminal's erase character to c instead of the default Control-H setting. Control characters may be indicated either by typing the character directly or by preceding the character by a ^ (i.e., use ^h to indicate Control-H).

The -i option sets the terminal's interrupt character to c instead of the default Control-C setting. Control characters may be indicated as described in the previous paragraph.

The "/etc/ttys" mappings may be overridden or supplemented by using the -m option. The sequence "-m pp:tt" tells tset that if the terminal's port type is pp, then it should assume that the terminal is of type tt. If a question mark (?) is placed after the colon (:), tset displays tt and asks the user to either press Enter to confirm that the terminal type is indeed tt, or to enter the actual terminal type that tset should use.



[Page 79]

In the following example, I found out my actual port name by using the tty utility (described in Chapter 4, "GNU Utilities for Power Users") and then examined the output from the tset command:

$ tty               ...display my terminal's port id. /dev/pts/1 $ tset -s           ...call tset. TERM=xterm;         ...shell command generated by tset. $ _ 


The previous example is only provided to illustrate how tset does its stuff. To actually make tset change the TERM and TERMCAP variables, which after all is the reason for using it in the first place, you must 'eval' its output. The eval shell command is described fully on page 197 in Chapter 5, "The Linux Shells." Here's a more realistic example of tset:

$ set noglob        ...temporarily inhibit filename expansion. $ eval `tset -s`    ...evaluate output from tset. $ unset noglob          ...re-enable filename expansion. $ echo $TERM            ...look at the new value of TERM. network                 ...the terminal type that tset found. $ _ 


Unfortunately, the terminal type network is not very useful, as it assumes that my terminal has almost no capabilities at all. The tset command may be presented with a rule that tells it, "If the terminal type is discovered to be network, assume that the terminal is a vt100." Here is the variation of tset that does this:

$ set noglob             ...disable filename expansion. $ eval `tset -s -m 'network:vt100'`    ...provide rule $ unset noglob      ...re-enable filename expansion. $ echo $TERM        ...display new TERM setting. vt100               ...this is the terminal type that tset used. $ _ 


If you aren't sure what type of terminal you might be on when you log in from the network, you can have tset prompt you for confirmation (or let you set something else):

$ set noglob             ...disable filename expansion. $ eval `tset -s -m 'network:?vt100'`   ...provide rule but ask. Terminal type? [vt100] xterm     ...specify xterm, hit <Enter>. $ unset noglob      ...re-enable filename expansion. $ echo $TERM        ...display new TERM setting. xterm               ...this is the terminal type that tset used. $ _ 



[Page 80]

In summary, it's wise to contain a command in your shell's startup file that calls tset to set your terminal type. Shell startup files are described in Chapter 5, "The Linux Shells." The simplest form of tset is the following:

C shell

setenv TERM vt100 tset 


Bash/Korn shell

TERM=vt100; export TERM tset 


The more sophisticated form of tset searches the "/etc/ttys" file for your terminal type, and should look somewhat like this:

C shell

set noglob eval `tset -s -m 'network:?vt100'` unset noglob 


Bash/Korn shell

eval `tset -s -m 'network:?vt100'` 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net