-/*
-** TSET -- set terminal modes
-**
-** This program does sophisticated terminal initialization.
-** I recommend that you include it in your .profile or .login
-** file to initialize whatever terminal you are on.
-**
-** There are several features:
-**
-** A special file or sequence (as controlled by the termcap file)
-** is sent to the terminal.
-**
-** Mode bits are set on a per-terminal_type basis (much better
-** than UNIX itself). This allows special delays, automatic
-** tabs, etc.
-**
-** Erase and Kill characters can be set to whatever you want.
-** Default is to change erase to control-H on a terminal which
-** can overstrike, and leave it alone on anything else. Kill
-** is always left alone unless specifically requested. These
-** characters can be represented as "^X" meaning control-X;
-** X is any character.
-**
-** Terminals which are dialups or plugboard types can be aliased
-** to whatever type you may have in your home or office. Thus,
-** if you know that when you dial up you will always be on a
-** TI 733, you can specify that fact to tset. You can represent
-** a type as "?type". This will ask you what type you want it
-** to be -- if you reply with just a newline, it will default
-** to the type given.
-**
-** The current terminal type can be queried.
-**
-** Usage:
-** tset [-] [-EC] [-eC] [-kC] [-iC] [-s] [-h] [-u] [-r]
-** [-m [ident] [test baudrate] :type]
-** [-Q] [-I] [-S] [type]
-**
-** In systems with environments, use:
-** eval `tset -s ...`
-** Actually, this doesn't work in old csh's.
-** Instead, use:
-** tset -s ... > tset.tmp
-** source tset.tmp
-** rm tset.tmp
-** or:
-** set noglob
-** set term=(`tset -S ....`)
-** setenv TERM $term[1]
-** setenv TERMCAP "$term[2]"
-** unset term
-** unset noglob
-**
-** Positional Parameters:
-** type -- the terminal type to force. If this is
-** specified, initialization is for this
-** terminal type.
-**
-** Flags:
-** - -- report terminal type. Whatever type is
-** decided on is reported. If no other flags
-** are stated, the only affect is to write
-** the terminal type on the standard output.
-** -r -- report to user in addition to other flags.
-** -EC -- set the erase character to C on all terminals
-** except those which cannot backspace (e.g.,
-** a TTY 33). C defaults to control-H.
-** -eC -- set the erase character to C on all terminals.
-** C defaults to control-H. If not specified,
-** the erase character is untouched; however, if
-** not specified and the erase character is NULL
-** (zero byte), the erase character is set to delete.
-** -kC -- set the kill character to C on all terminals.
-** Default for C is control-X. If not specified,
-** the kill character is untouched; however, if
-** not specified and the kill character is NULL
-** (zero byte), the kill character is set to control-U.
-** -iC -- set the interrupt character to C on all terminals.
-** Default for C is control-C. If not specified, the
-** interrupt character is untouched; however, if
-** not specified and the interrupt character is NULL
-** (zero byte), the interrupt character is set to
-** control-C.
-** -qC -- reserved for setable quit character.
-** -m -- map the system identified type to some user
-** specified type. The mapping can be baud rate
-** dependent. This replaces the old -d, -p flags.
-** (-d type -> -m dialup:type)
-** (-p type -> -m plug:type)
-** Syntax: -m identifier [test baudrate] :type
-** where: ``identifier'' is terminal type found in
-** /etc/ttys for this port, (abscence of an identifier
-** matches any identifier); ``test'' may be any combination
-** of > = < ! @; ``baudrate'' is as with stty(1);
-** ``type'' is the actual terminal type to use if the
-** mapping condition is met. Multiple maps are scanned
-** in order and the first match prevails.
-** -n -- If the new tty driver from UCB is available, this flag
-** will activate the new options for erase and kill
-** processing. This will be different for printers
-** and crt's. For crts, if the baud rate is < 1200 then
-** erase and kill don't remove characters from the screen.
-** -h -- don't read htmp file. Normally the terminal type
-** is determined by reading the htmp file or the
-** environment (unless some mapping is specified).
-** This forces a read of the ttytype file -- useful
-** when htmp is somehow wrong. (V6 only)
-** -u -- don't update htmp. It seemed like this should
-** be put in. Note that htmp is never actually
-** written if there are no changes, so don't bother
-** bother using this for efficiency reasons alone.
-** -s -- output setenv commands for TERM. This can be
-** used with
-** `tset -s ...`
-** and is to be prefered to:
-** setenv TERM `tset - ...`
-** because -s sets the TERMCAP variable also.
-** -S -- Similar to -s but outputs 2 strings suitable for
-** use in csh .login files as follows:
-** set noglob
-** set term=(`tset -S .....`)
-** setenv TERM $term[1]
-** setenv TERMCAP "$term[2]"
-** unset term
-** unset noglob
-** -Q -- be quiet. don't output 'Erase set to' etc.
-** -I -- don't do terminal initialization (is & if
-** strings).
-** -v -- On virtual terminal systems, don't set up a
-** virtual terminal. Otherwise tset will tell
-** the operating system what kind of terminal you
-** are on (if it is a known terminal) and fix up
-** the output of -s to use virtual terminal sequences.
-**
-** Files:
-** /etc/ttys
-** contains a terminal id -> terminal type
-** mapping; used when any user mapping is specified,
-** or the environment doesn't have TERM set.
-** /etc/termcap
-** a terminal_type -> terminal_capabilities
-** mapping.
-**
-** Return Codes:
-** -1 -- couldn't open ttycap.
-** 1 -- bad terminal type, or standard output not tty.
-** 0 -- ok.
-**
-** Defined Constants:
-** DIALUP -- the type code for a dialup port.
-** PLUGBOARD -- the type code for a plugboard port.
-** ARPANET -- the type code for an arpanet port.
-** BACKSPACE -- control-H, the default for -e.
-** CNTL('X') -- control-X, the default for -k.
-** OLDERASE -- the system default erase character.
-** OLDKILL -- the system default kill character.
-** FILEDES -- the file descriptor to do the operation
-** on, nominally 1 or 2.
-** STDOUT -- the standard output file descriptor.
-** UIDMASK -- the bit pattern to mask with the getuid()
-** call to get just the user id.
-** GTTYN -- defines file containing generalized ttynames
-** and compiles code to look there.
-**
-** Requires:
-** Routines to handle htmp, ttys, and ttycap.
-**
-** Compilation Flags:
-** OLDFLAGS -- must be defined to compile code for any of
-** the -d, -p, or -a flags.
-** OLDDIALUP -- accept the -d flag.
-** OLDPLUGBOARD -- accept the -p flag.
-** OLDARPANET -- accept the -a flag.
-** V6 -- if clear, use environments, not htmp.
-** also use TIOCSETN rather than stty to avoid flushing
-** GTTYN -- if set, compiles code to look at /etc/ttys.
-** UCB_NTTY -- set to handle new tty driver modes.
-**
-** Trace Flags:
-** none
-**
-** Diagnostics:
-** Bad flag
-** An incorrect option was specified.
-** Too few args
-** more command line arguments are required.
-** Unexpected arg
-** wrong type of argument was encountered.
-** Cannot open ...
-** The specified file could not be openned.
-** Type ... unknown
-** An unknown terminal type was specified.
-** Cannot update htmp
-** Cannot update htmp file when the standard
-** output is not a terminal.
-** Erase set to ...
-** Telling that the erase character has been
-** set to the specified character.
-** Kill set to ...
-** Ditto for kill
-** Erase is ... Kill is ...
-** Tells that the erase/kill characters were
-** wierd before, but they are being left as-is.
-** Not a terminal
-** Set if FILEDES is not a terminal.
-**
-** Compilation Instructions:
-** cc -n -O tset.c -ltermlib
-** mv a.out tset
-** chown bin tset
-** chmod 4755 tset
-**
-** where 'bin' should be whoever owns the 'htmp' file.
-** If 'htmp' is 666, then tset need not be setuid.
-**
-** For version 6 the compile command should be:
-** cc -n -O -I/usr/include/retrofit tset.c -ltermlib -lretro -lS
-**
-** Author:
-** Eric Allman
-** Electronics Research Labs
-** U.C. Berkeley
-**
-** History:
-** 1/81 -- Added alias checking for mapping identifiers.
-** 9/80 -- Added UCB_NTTY mods to setup the new tty driver.
-** Added the 'reset ...' invocation.
-** 7/80 -- '-S' added. '-m' mapping added. TERMCAP string
-** cleaned up.
-** 3/80 -- Changed to use tputs. Prc & flush added.
-** 10/79 -- '-s' option extended to handle TERMCAP
-** variable, set noglob, quote the entry,
-** and know about the Bourne shell. Terminal
-** initialization moved to before any information
-** output so screen clears would not screw you.
-** '-Q' option added.
-** 8/79 -- '-' option alone changed to only output
-** type. '-s' option added. 'VERSION7'
-** changed to 'V6' for compatibility.
-** 12/78 -- modified for eventual migration to VAX/UNIX,
-** so the '-' option is changed to output only
-** the terminal type to STDOUT instead of
-** FILEDES.
-** 9/78 -- '-' and '-p' options added (now fully
-** compatible with ttytype!), and spaces are
-** permitted between the -d and the type.
-** 8/78 -- The sense of -h and -u were reversed, and the
-** -f flag is dropped -- same effect is available
-** by just stating the terminal type.
-** 10/77 -- Written.
-*/
-
-#define UCB_NTTY
-
-# ifdef USG
-# define index strchr
-# define rindex strrchr
-# define curerase mode.c_cc[VERASE]
-# define curkill mode.c_cc[VKILL]
-# define curintr mode.c_cc[VINTR]
-# define olderase oldmode.c_cc[VERASE]
-# define oldkill oldmode.c_cc[VKILL]
-# define oldintr oldmode.c_cc[VINTR]
-# else
-# define curerase mode.sg_erase
-# define curkill mode.sg_kill
-# define curintr tchar.t_intrc
-# define olderase oldmode.sg_erase
-# define oldkill oldmode.sg_kill
-# define oldintr oldtchar.t_intrc
-# endif
-
-# ifndef V6
-# define GTTYN
-# include <ttyent.h>
-# endif
-
-# ifndef USG
-# include <sgtty.h>
-# else
-# include <termio.h>
-# endif
-
-# include <stdio.h>
-# include <signal.h>
-# ifdef V6
-# include <retrofit.h>
-# endif
-
-# define YES 1
-# define NO 0
-#undef CNTL
-# define CNTL(c) ((c)&037)
-# define BACKSPACE (CNTL('H'))
-# define CHK(val, dft) (val<=0 ? dft : val)
-# define isdigit(c) (c >= '0' && c <= '9')
-# define isalnum(c) (c > ' ' && (index("<@=>!:|\177", c) == NULL))
-# define OLDERASE '#'
-# define OLDKILL '@'
-# define OLDINTR '\177' /* del */
-
-/* default special characters */
-#ifndef CERASE
-#define CERASE '\177'
-#endif
-#ifndef CKILL
-#define CKILL CNTL('U')
-#endif
-#ifndef CINTR
-#define CINTR CNTL('C')
-#endif
-#ifndef CDSUSP
-#define CQUIT 034 /* FS, ^\ */
-#define CSTART CNTL('Q')
-#define CSTOP CNTL('S')
-#define CEOF CNTL('D')
-#define CEOT CEOF
-#define CBRK 0377
-#define CSUSP CNTL('Z')
-#define CDSUSP CNTL('Y')
-#define CRPRNT CNTL('R')
-#define CDISCARD CNTL('O')
-#define CWERASE CNTL('W')
-#define CLNEXT CNTL('V')
-#endif
-
-# define FILEDES 2 /* do gtty/stty on this descriptor */
-# define STDOUT 1 /* output of -s/-S to this descriptor */
-
-# ifdef V6
-# define UIDMASK 0377
-# else
-# define UIDMASK -1
-# endif
-
-# ifdef UCB_NTTY
-# define USAGE "usage: tset [-] [-nrsIQS] [-eC] [-kC] [-iC] [-m [ident][test speed]:type] [type]\n"
-# else
-# define USAGE "usage: tset [-] [-rsIQS] [-eC] [-kC] [-iC] [-m [ident][test speed]:type] [type]\n"
-# endif
-
-# define OLDFLAGS
-# define DIALUP "dialup"
-# define OLDDIALUP "sd"
-# define PLUGBOARD "plugboard"
-# define OLDPLUGBOARD "sp"
-/***
-# define ARPANET "arpanet"
-# define OLDARPANET "sa"
-/***/
-
-# define DEFTYPE "unknown"
-
-
-# ifdef GTTYN
-# define NOTTY 0
-# else
-# define NOTTY 'x'
-# endif
-
-/*
- * Baud Rate Conditionals
- */
-# define ANY 0
-# define GT 1
-# define EQ 2
-# define LT 4
-# define GE (GT|EQ)
-# define LE (LT|EQ)
-# define NE (GT|LT)
-# define ALL (GT|EQ|LT)
-
-
-
-# define NMAP 10
-
-struct map {
- char *Ident;
- char Test;
- char Speed;
- char *Type;
-} map[NMAP];
-
-struct map *Map = map;
-
-/* This should be available in an include file */
-struct
-{
- char *string;
- int speed;
- int baudrate;
-} speeds[] = {
- "0", B0, 0,
- "50", B50, 50,
- "75", B75, 75,
- "110", B110, 110,
- "134", B134, 134,
- "134.5",B134, 134,
- "150", B150, 150,
- "200", B200, 200,
- "300", B300, 300,
- "600", B600, 600,
- "1200", B1200, 1200,
- "1800", B1800, 1800,
- "2400", B2400, 2400,
- "4800", B4800, 4800,
- "9600", B9600, 9600,
- "19200",EXTA, 19200,
- "exta", EXTA, 19200,
- "extb", EXTB, 38400,
- 0,
-};
-
-#ifdef CBVIRTTERM
-struct vterm {
- char cap[2];
- char *value;
-} vtab [] = {
- "al", "\033\120",
- "cd", "\033\114",
- "ce", "\033\113",
- "cm", "\033\107%r%.%.",
- "cl", "\033\112",
- "dc", "\033\115",
- "dl", "\033\116",
- "ic", "\033\117",
- "kl", "\033\104",
- "kr", "\033\103",
- "ku", "\033\101",
- "kd", "\033\102",
- "kh", "\033\105",
- "nd", "\033\103",
- "se", "\033\142\004",
- "so", "\033\141\004",
- "ue", "\033\142\001",
- "up", "\033\101",
- "us", "\033\141\001",
- "\0\0", NULL,
-};
-
-int VirTermNo = -2;
-int HasAM; /* True if terminal has automatic margins */
-# endif CBVIRTTERM
-
-char Erase_char; /* new erase character */
-char Kill_char; /* new kill character */
-char Intr_char; /* new interrupt character */
-char Specialerase; /* set => Erase_char only on terminals with backspace */
-
-# ifdef GTTYN
-char *Ttyid = NOTTY; /* terminal identifier */
-# else
-char Ttyid = NOTTY; /* terminal identifier */
-# endif
-char *TtyType; /* type of terminal */
-char *DefType; /* default type if none other computed */
-char *NewType; /* mapping identifier based on old flags */
-int Mapped; /* mapping has been specified */
-int Dash_u; /* don't update htmp */
-int Dash_h; /* don't read htmp */
-int DoSetenv; /* output setenv commands */
-int BeQuiet; /* be quiet */
-int NoInit; /* don't output initialization string */
-int IsReset; /* invoked as reset */
-int Report; /* report current type */
-int Ureport; /* report to user */
-int RepOnly; /* report only */
-int CmndLine; /* output full command lines (-s option) */
-int Ask; /* ask user for termtype */
-int DoVirtTerm = YES; /* Set up a virtual terminal */
-int PadBaud; /* Min rate of padding needed */
-int lines, columns;
-
-# define CAPBUFSIZ 1024
-char Capbuf[CAPBUFSIZ]; /* line from /etc/termcap for this TtyType */
-char *Ttycap; /* termcap line from termcap or environ */
-
-char Aliasbuf[128];
-char *Alias[16];
-
-extern char *strcpy();
-extern char *index();
-extern char *tgetstr();
-extern int prc();
-
-struct delay
-{
- int d_delay;
- int d_bits;
-};
-
-# include "tset.delays.h"
-
-# ifndef USG
-struct sgttyb mode;
-struct sgttyb oldmode;
-struct tchars tchar;
-struct tchars oldtchar;
-# else
-struct termio mode;
-struct termio oldmode;
-# endif
-# ifdef CBVIRTTERM
-struct termcb block = {0, 2, 0, 0, 0, 20};
-# endif CBVIRTTERM
-
-