[Chapter 5] 5.5 Querying Your Terminal Type: qterm
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:33:03Z

#

| Chapter 5 Setting Up Your Terminal |

## 5.5 Querying Your Terminal Type: qterm

tset (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)

A problem with tset, however, is that it 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 tend to be confused by the tset prompt.

qterm | As an alternative, try Michael Cooper's qterm program on our Power Tools disc. 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:

    `...`
    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 what your terminal type is, it just 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 they correspond to. By default, qterm looks for the listings in a systemwide location, such as /usr/usc/lib/qtermtab or /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".
>     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
    % echo "^[Z"
    ^[[E;Y|

(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)
>     vt100

The string `Terminal` `recognized` `as` `...` is sent to standard error (13.1); only the terminal type itself is sent to standard output (13.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
>     vt100

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 be able to find a string that it does respond uniquely to. And some terminals do uncanny imitations of others. For example, I'm currently using an xterm (1.31) window, but qterm thinks I'm using a vt100:

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

As a hack, you can just edit your .qtermtab file. For example, I could edit my .qtermtab so the `^[[?1;2c` response is mapped to xterm:

>     #^[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

5.4 Setting the TERMCAP Variable with tset |  | 5.6 Checklist: Terminal Hangs When I Log In