home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Unix Power ToolsUnix Power ToolsSearch this book

5.4. Querying Your Terminal Type: qterm

tset (Section 5.3) is a powerful tool to use if you often log in at different terminals. You can use tset to prompt you with a default terminal type, giving you the opportunity to specify a new terminal type when you log in:

TERM = (vt100)

However, tset requires you to know your terminal type. You might log in at a new terminal and have no idea what to set the terminal type to. Or your terminal might be configured to emulate another terminal type without your knowledge. New users in particular are confused by the tset prompt. In some respects, this is not a surprise, as the prompt itself can be confusing without a bit of context.

Figure Go to http://examples.oreilly.com/upt3 for more information on: qterm

As an alternative, try Michael Cooper's qterm program on our CD-ROM [see http://examples.oreilly.com/upt3]. qterm sends the terminal a test string and determines what sort of terminal you're using based on how the terminal responds. Using qterm, you can make sure you always use the correct terminal type by placing the following line in your .login:

'...' Section 28.14

setenv TERM `qterm`

or in .profile:

TERM=`qterm`;export TERM

The advantage of qterm is that it sets the terminal type without your intervention. You don't need to know your terminal type; it gets set automatically.

qterm works by sending the terminal a query string and returning the terminal type depending on the terminal's response. qterm is configured using a listing of responses and the terminals to which they correspond. By default, qterm looks for the listings in a system-wide location such as /usr/local/lib/qtermtab. In addition, you can call qterm with the +usrtab option, so that it will look for a file called .qtermtab in your home directory.

The string used to query the terminal is usually ESC Z. The sample qtermtab file distributed with qterm defines the responses several different terminals give for that string:

# QtermTab - Query terminal table for qterm.
#SendStr ReceiveStr         TermName       FullTermName
^[Z     ^[[?1;0c            vt100          Base vt100
^[Z     ^[[?1;1c            vt100          vt100 with STP
^[Z     ^[[?1;2c            vt100          ANSI/VT100 Clone
^[Z     ^[/K                h29            Zenith z29 in zenith mode
^[Z     ^[/Z                vt52           Generic vt52
^[Z     ^[[0n               vt100          AT&T Unix PC 7300

If your terminal isn't listed here, you can just add it. To find out your terminal's response to the query string, just echo ESC Z to your terminal and see what the response is. For example, I logged in from my Macintosh terminal emulator at home and found that qterm didn't recognize my terminal type:

% qterm
Terminal NOT recognized - defaults to "vt100".

qterm defaults to the right terminal description, but I'd still rather define my own entry. I find out my terminal's response to the ESC Z string:

% echo "^[Z"

(Note that ESC prints as ^[.) Then I add the entry to my qterm description file:

^[Z         ^[[E;Y|         vt100       Macintosh terminal emulator

Now when I run qterm, the terminal is recognized:

% qterm
Terminal recognized as vt100 (Macintosh terminal emulator)

The string Terminal recognized as ... is sent to standard error (Section 43.1); only the terminal type itself is sent to standard output (Section 43.1). So if you use the following command line:

% setenv TERM `qterm`
Terminal recognized as vt100 (Macintosh terminal emulator)

the TERM variable is set correctly:

% echo $TERM

Now for the caveat: qterm's results are only as accurate as the qtermtab file. Not all terminals respond to the ESC Z string, and you may not find a string to which it responds uniquely. And some terminals do uncanny imitations of others. For example, I'm currently using an xterm (Section 24.20) window, but qterm thinks I'm using a vt100:

% echo $TERM
% qterm
Terminal recognized as vt100 (ANSI/VT100 Clone)

As a hack, you can just edit your .qtermtab file. For example, I could comment out the old vt100 entry and map ^[[?1;2c to xterm instead:

#^[Z     ^[[?1;2c            vt100          ANSI/VT100 Clone
^[Z     ^[[?1;2c            xterm          xterm window

and then call qterm with the +usrtab command-line option:

setenv TERM `qterm +usrtab`

--LM and SJC

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.