-#ifndef lint
-static char sccsid[] = "@(#)tset.c 1.13 (Berkeley) %G%";
-#endif
-
-/*
-** TSET -- set terminal modes
-**
-** This program does sophisticated terminal initialization.
-** I recommend that you include it in your .start_up or .login
-** file to initialize whatever terminal you are on.
-**
-** There are several features:
-**
-** A special file or sequence (as controlled by the ttycap 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 htmp file, used by ex, etc., can be updated.
-**
-** The current terminal type can be queried.
-**
-** Usage:
-** tset [-] [-EC] [-eC] [-kC] [-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 neither -E or -e
-** are specified, the erase character is set to
-** control-H if the terminal can both backspace
-** and not overstrike (e.g., a CRT). If the erase
-** character is NULL (zero byte), it will be reset
-** to '#' if nothing else is specified.
-** -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 '@'.
-** -iC -- reserved for setable interrupt character.
-** -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.
-** CTRL('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.
-*/
-
-# ifdef USG
-# define index strchr
-# define rindex strrchr
-# define curerase mode.c_cc[VERASE]
-# define curkill mode.c_cc[VKILL]
-# define olderase oldmode.c_cc[VERASE]
-# define oldkill oldmode.c_cc[VKILL]
-# else
-# define curerase mode.sg_erase
-# define curkill mode.sg_kill
-# define olderase oldmode.sg_erase
-# define oldkill oldmode.sg_kill
-# 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 CTRL
-# define CTRL(x) (x ^ 0100)
-# define BACKSPACE (CTRL('H'))
-# define CHK(val, dft) (val<=0 ? dft : val)
-# define isdigit(c) (c >= '0' && c <= '9')
-# define isalnum(c) (c > ' ' && !(index("<@=>!:|\177", c)) )
-# define OLDERASE '#'
-# define OLDKILL '@'
-
-# 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] [-m [ident][test speed]:type] [type]\n"
-# else
-# define USAGE "usage: tset [-] [-rsIQS] [-eC] [-kC] [-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