Termid (was: Re: Login (unique) problems with terminal emulations.)
Amos Shapir
amos at taux01.UUCP
Fri Dec 29 08:44:26 AEST 1989
In article <1030 at dialogic.UUCP> drich at 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 at taux01.nsc.com, amos at 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
More information about the Comp.unix.wizards
mailing list