[comp.unix.wizards] Termid

amos@taux01.UUCP (Amos Shapir) (12/29/89)

In article <1030@dialogic.UUCP> drich@dialogic.UUCP (Dan Rich) writes:
>Before I go off into an answer for this, does anyone know of a way to
>auto-identify a terminal at login?  I know that this can be done,

Here's how, at least for ANSI-compatible terminals:

#!/bin/sh
: "This is a shell archive, meaning:                              "
: "1. Remove everything above the #! /bin/sh line.                "
: "2. Save the rest in a file.                                    "
: "3. Execute the file with /bin/sh (not csh) to create the files:"
: "	termid.1"
: "	termid.c"
: "	lib.termid"
: "This archive created:  Thu Dec 28 23:36:50 EET 1989 "
if [ -f termid.1 ]
then
echo file termid.1 exists
else
echo extracting file: termid.1
sed 's/^X//' >termid.1 << 'END-of-termid.1'
X.TH TERMID 1 
X.SH NAME
Xtermid \- use terminal id escape sequence to determine terminal type
X.SH SYNOPSIS
X.B termid
X[-u default]
X.SH DESCRIPTION
X.I Termid
Xdetermines the type of terminal it is run on, by
Xusing the ANSI escape sequence to request the terminal id
X(ESC [ c),
Xthen matching the terminal's reply with its type in a file of
Xcorresponding types. The found type is printed to standard output.
X.PP
XIn case of failure, i.e. the terminal responds with an unknown
Xreply or does not respond at all,
X.I termid
Xprints the
X.I default
Xtype given by the
X.B -u
Xflag; if no flag is given it prints
X.B `unknown'.
X.PP
XThe reply is checked for sanity - it should start with `ESC [ ?'
Xand end with a `c'. The types file contains, one per line, an identifiable
Xprefix of the reply string (minus the esacpe sequence), a tab, and
Xthe corresponding terminal type. The first matching type is printed.
X.SH EXAMPLE
XIn a `sh' environment, put in your .profile:
X.nf
X.sp
XTERM=`termid -u $TERM`; export TERM
X.sp
X.fi
Xto set the terminal type.
XWith `csh', this can be done in the .login file by:
X.nf
X.sp
Xset term=`termid -u $term`
X.sp
X.fi
XThe -u flag is used to preserve the previous value of the shell variable,
X(as set by
X.I login
Xor
X.I tset),
Xin case
X.I termid
Xfails.
X.SH AUTHORS
XOri Danieli & Amos Shapir
X.SH "SEE ALSO"
Xtset(1)
X.SH FILES
X.IP /usr/local/lib/termid
Xsuffixes of id strings and their corresponding
Xterminal types.
X.SH BUGS
END-of-termid.1
fi
if [ -f termid.c ]
then
echo file termid.c exists
else
echo extracting file: termid.c
sed 's/^X//' >termid.c << 'END-of-termid.c'
X/*
X * determine terminal type
X */
X#include <fcntl.h>
X#include <sgtty.h>
X#include <stdio.h>
Xstruct sgttyb sttb;
Xchar devtty[] = "/dev/tty";
Xchar buf[80];
Xchar *undef = "unknown";
X#define IDFILE "/usr/local/lib/termid"
Xchar idfile[] = IDFILE;
Xextern errno;
X
Xmain(argc, argv)
X	char **argv;
X{
X	register tty, i;
X	register char *p;
X	register FILE *idf;
X	register flg, flags;
X
X	if(argc>2 && argv[1][0]=='-' && argv[1][1]=='u')
X		undef = argv[2];
X	if((tty = open(devtty, 2, 0)) < 0 ||
X	(flg=fcntl(tty, F_GETFL, 0)) == -1 ||
X	fcntl(tty, F_SETFL, flg|FNDELAY) == -1)
X		unknown(devtty);
X	gtty(tty, &sttb);
X	flags = sttb.sg_flags;
X	sttb.sg_flags &= ~ECHO;
X	sttb.sg_flags |= RAW;
X	stty(tty, &sttb);
X	write(tty, "\033[c", 3);
X	sleep(2);	/* give it time to respond */
X	for(p=buf; (i = read(tty, p, 1))>=0; p+=i);
X	errno = 0;
X	sttb.sg_flags = flags;
X	stty(tty, &sttb);
X	fcntl(tty, F_SETFL, flg);
X
X	/* check for ANSI-standard reply */
X	if(strncmp(buf, "\033[?", 3))
X		unknown(&buf[1]);
X	if((idf = fopen(idfile, "r")) == NULL)
X		unknown(idfile);
X	do {
X		p = &buf[3];
X		while((i = getc(idf)) == *p++);
X		if(i == EOF)
X			unknown(&buf[3]);
X		if(i == '\t' && (p[-1] == 'c' || p[-1] == ';')) {	/* found it */
X			while((i = getc(idf)) != '\n' && i != EOF)
X				putchar(i);
X			putchar('\n');
X			exit(0);
X		} /* else */
X		while((i = getc(idf)) != '\n' && i != EOF);
X	} while(i != EOF);
X}
X
Xunknown(s)
X	char *s;
X{
X	if(errno != 0)
X		perror(s);
X	puts(undef);
X	exit(1);
X}
END-of-termid.c
fi
if [ -f lib.termid ]
then
echo file lib.termid exists
else
echo extracting file: lib.termid
sed 's/^X//' >lib.termid << 'END-of-lib.termid'
Xformat: answerback prefix <tab> terminal type
X10	dw4
X1	vt100
X62	vt200
X63	tv9220
X8	tvi970
END-of-lib.termid
fi
exit
-- 
	Amos Shapir		amos@taux01.nsc.com, amos@nsc.nsc.com
National Semiconductor (Israel) P.O.B. 3007, Herzlia 46104, Israel
Tel. +972 52 522261  TWX: 33691, fax: +972-52-558322 GEO: 34 48 E / 32 10 N

davidb@Pacer.UUCP (David Barts) (01/03/90)

In article <3153@taux01.UUCP>, amos@taux01.UUCP (Amos Shapir) writes:
> In article <1030@dialogic.UUCP> drich@dialogic.UUCP (Dan Rich) writes:
> >Before I go off into an answer for this, does anyone know of a way to
> >auto-identify a terminal at login?  I know that this can be done,
> 
> Here's how, at least for ANSI-compatible terminals:
> :
> :

This is a neat trick, basically what SET TERMINAL/INQUIRE does on
VAX/VMS.  Go ahead and put it in your own .login or .profile, but
*please* don't put it in the sitewide files.  There is a chance that
if the terminal being used is not an ANSI terminal, the ANSI
`who-are-you' escape sequence will be far from innocuous.

As a case in point, when I was in college I would cu from an HP system
to a VAX/VMS system.  I found out the hard way that the series of
escape codes sent out by SET TERMINAL/INQUIRE just happen to cause the
HP terminal I was using to do a master reset (everything was reset,
down to baud, parity, and handshaking).  I wasn't the only user
bitten by this change, and needless to say the site-wide login script
was changed back in a matter of hours.
-- 
David Barts			Pacer Corporation
davidb@pacer.uucp		...!fluke!pacer!davidb

prc@erbe.se (Robert Claeson) (01/04/90)

In article <266@zircon.UUCP>, davidb@Pacer.UUCP (David Barts) writes:

> This is a neat trick, basically what SET TERMINAL/INQUIRE does on
> VAX/VMS.  Go ahead and put it in your own .login or .profile, but
> *please* don't put it in the sitewide files.  There is a chance that
> if the terminal being used is not an ANSI terminal, the ANSI
> `who-are-you' escape sequence will be far from innocuous.

In fact, there are even a couple of VTxxx- compatible terminals and
PC-based emulators that hangs when they see the <ESC> Z or the other
(don't remember exactly what it is) escape sequence used to have the
terminal report its type back to the host.


-- 
          Robert Claeson      E-mail: rclaeson@erbe.se
	  ERBE DATA AB

fst@gtenmc.UUCP (Fariborz "Skip" Tavakkolian) (01/05/90)

In article <1075@maxim.erbe.se> prc@erbe.se (Robert Claeson) writes:
>In article <266@zircon.UUCP>, davidb@Pacer.UUCP (David Barts) writes:
>> This is a neat trick, basically what SET TERMINAL/INQUIRE does on
>> VAX/VMS.
[delete]
>In fact, there are even a couple of VTxxx- compatible terminals and
>PC-based emulators that hangs when they see the <ESC> Z or the other
[deleted]
>-- 
>          Robert Claeson      E-mail: rclaeson@erbe.se

I have always used the following with almost all ANSI terminals I have
worked with:

1) Set the ``answer-back'' string to your terminal type followed by
   RETURN (i.e. 4424<RETURN> or <ESC>vt100<RETURN><ESC>, etc.)

2) Then inquire about the term type by sending '^E' or '\005' to the
   terminal. This causes the term to return the ``answer-back'' string.
   i.e.

    echo "Enter term type: \005\c" ; read TERM

This has worked on AT&T 442X, 542X, DEC vt1XX and compatibles.

Hope this helps.
Skip
-- 
----------------------------------------------------------------------------
Fariborz "Skip" Tavakkolian  -of-  Automated Cellular Engineering
Currently consulting         -at-  GTE Telecom, Inc. Bothell, Wa
Mail:                              tiny1!fst@mcgp1  -or-  fst@gtenmc