#  @(#)ptnnx	35.2
#  @(#)Copyright (C) 1986  by  Motorola Computer Systems.
#  General Motorola lp printer model backend file.
#
#  MODIFICATIONS:
#  05/15/86  SBW - now call csoffset filter instead of offset.
#  05/15/86  SBW - added color options for the pt37 and pt38.
#  05/15/86  SBW - commented out all references to LANGUAGE selection.
#  05/15/86  SBW - escaped the double quotes within the trap statements.
#  04/08/87  SAJ - moved all output to printers inside critical section.
#                - added ISP support back in.
#                - added pt40 support.
#                - changed all sttys to use -ixany
#  05/01/87  SAJ - Extensively revised for ISP as well as general 
#                  sanitariness.  (It is still a mess!)
#  06/08/87  SAJ - Do stty istrip on all 7-bit printers.
#                - Do stty -istrip on all 8-bit printers.
#                - Eliminate parity checking for pt36
#                - Eliminate carriage return delay from all sttys.
#  06/18/87  SAJ - Set BANNERP to null if filter not csoffset
#  06/22/87  SAJ - Eliminate form feed delay from all sttys (They
#                  break the MVME335 driver!).
#  06/26/87  RSS - Eliminate incompatible control/combination modes
#                  for all sttys.
#                - Eliminate null command (syntax error) in trap sequence.
#  07/09/87  SAJ - Redirect the std err of all stty's to /dev/null
#                  (MVME335 port causes stty to emit an error message)
#                - Enforce rule: All 9600 baud printers:
#                                8 bits
#                                no parity
#                                1 stop bit
#                                All 1200 baud printers:
#                                7 bits
#                                Even parity
#                                2 stop bits
#  07/17/87   SAJ - Removed ""s from $OPTS  (oops!)
#  07/30/87   MCM - Add sleep command after call to printfile to allow PT4000
#		    to complete receive of last line
#  07/30/87   MCM - Add PT235 support.
#  08/01/87   MCM - Change pt40 support to 8 bit.
#  08/07/87   MCM - Add PAC support.
#  08/10/87   MCM - Put the stty variables setup into subroutines
#  08/13/87   MCM - Add BINIT variable so that the pitch and other options
#		    can be changed when printing the EOJ banner.
#  08/17/87   MCM - Add test for operator specified pitch of sw which
#		    signifies that the pitch has been set in the printer's
#		    switches
#  08/17/87   MCM - Add test for locking file MPSlock if Qplock does not exist.
#  08/20/87   MCM - Add 'c' option to allow specification of command set
#  09/08/87   MCM - Allow external definition of the BAUD, PITCH and character 
#			size (CSX) variables.
#  09/09/87   MCM - Allow external definition of the ISTRIP, PARITY, OPOST,
#			ONLCR, CSTOPB, and PARENB variables.
#  09/16/87   MCM - For those printers that have multiple pitches, print
#			banner data at 10cpi.
#		  - Add b option (BANNERS=yes)
#  10/23/87   MCM - Add pt41 (NEC LC-890) support.
#		  - Add s option (STYLE).
#  11/2/87    MCM - Add support for pt43, pt44, pt45, pt46.
#  11/10/87   MCM - If request is for ya processing, associate ya filter
#		    with YA_FILTER.
#  11/30/87   MCM - Add UNSETC support in trap processing.
#  12/2/87    MCM - Test for 332xt device.  If device is attached to a 332xt we
#			don't need to do the sleeps.
#  12/02/87   MCM - Remove call to sleep after call to printfile.  
#  12/11/87   MCM - Export YATIND
#		  - If errcode non-zero, test for YA=yes before doing disable
#		  - Add auxprint support
#		  - export DEV when calling YA-FILTER
#  12/14/87   MCM - Add VFU file support
#  12/29/87   MCM - Remove change of 11/10/87. Use $FILTER as variable name for
#			all routines used to filter main body output.
#  12/29/87   MCM - Make sure we don't try to print banners on the HP plotter
#  12/29/87   MCM - Add support for postscript text filter
#  01/07/88   MCM - Add PT4010 status checking and sleep required when going
#			from non-PostScript emulation to PostScript emulation.
#  01/07/88   MCM - Eliminate multiple stty's.
#  01/21/88   MCM - Do not allow filter name to be passed from calling routine.
#  01/28/88   MCM - Put traps into functions, add initialization sequence for
#			plotter, capitilize Epson80 and Epson132.
#  02/10/88   MCM - Flush the stdin input buffer at the start of a PT4010 
#			print request
#  03/03/88   MCM - Set -opost for line printers
#  		    Set stty noflsh so that printers on 332xt parallel port
#			won't flush data when printers drop out of ready
#  03/10/88   MCM - Set ADDCR = no for line printers to prevent inadvertent
#			conversion of skip-to-channel/space-n-lines commands
#  03/16/88   MCM - Send a PostScript "stop" instruction as part of the end-of-
#			file sequence.  If the PT4010 dosen't see the stop
#			sequence, it dosen't send a "Flushing" response.
#  03/17/88   MCM - Color selection sequence is being sent at the wrong time.
#			Send the sequence AFTER printing the banner.
#  03/24/88   MCM - ptnnx is trying to print banners using FLiP filter when
#			-y or -Y option is specified.  Set BANNERFILTER 
#			to default to /usr/bin/csoffset
#  04/15/88   MCM - Change sequence for 6/8 lpi selection on line printers
#  04/19/88   MCM - Allow users to pass FILTER=no shell variable.
#		    Add support for "np" option (opost = false)
#  04/28/88   MCM - Make sure that opost is true while printing begining and
#			ending banners
#  05/05/88   MCM - Add the "pst" (PostScript trace) option
#  05/06/88   MCM - PT4010 is not always waiting for DC1 when switching from
#			laserjet/diablo to PostScript Batch.  Flush the input
#			queue before beginning the emulation switch.
#
#       Printer-specific interface files created by 'lp.cnfg'
#       set some shell variables and then exec this file.
#	These interface files always set the variables MODEL, LANG, DEV 
#       and USESTTY, and may optionally set the variables FILTERARGS,
#       BANNER, BAUD, CSX, and PITCH.
#	Output is passed through the /usr/bin/csoffset filter,
#	unless an option selects another filter, such as /usr/bin/450.
#	If the filter is not executable, then this interface will
#	disable the lp printer which it was called to service.
#
#	Parameters are:
#	$0 - Printer name (this file name)
#	$1 - Request id number
#	$2 - User name
#	$3 - Optional title
#	$4 - Number of copies to print
#	$5 - This is the set of -o option values from 'lp'
#	$6... These are the names of files to print
#

# Define function to turn color printers to black mode

printblack () {
case $MODEL in 		# be sure the color gets turned back to black
    pt37 | pt38 | pt39)
	echo "$BLACK\c"
	;;
    *)
	;;
esac
}

# Define function to print the files to standard output

printfiles () {
	i=1
	while [ $i -le "$COPIES" ]
	do
		for file in $FILES
		do
			echo "$XSTRING\c"
			/bin/cat "$file" 2>&1
			if [ "$FORMFEED" = yes ]
			then
				echo "\014\c"
			fi
		done
	i=`fexpr $i + 1`
	done 
}

printbanner () {
	echo "$XSTRING\c"
	echo "\n\n\n\n\c"
	/usr/bin/banner "$NAME" $OPTITLE
	echo "\n\n\c"
	user=`/bin/grep "^$NAME:" /etc/passwd | /bin/line | /usr/bin/cut -d: -f5`
	if [ -n "$user" ]
	then
		echo "User: $user $VIA\n\n\c"
	else
		echo "$VIA\n\n\c"
	fi
	BANNEREMULATE=""
	if [ -n "$EMULATION" ]
	then
		BANNEREMULATE="Emulation: $EMULATION"
	fi
	echo "Request id: $REQUEST    Printer: $PRINTER    $BANNEREMULATE\n\n\c"
	/bin/date
	echo "\nCopies=$COPIES  Pitch=$PITCH  Offset=$OFFSET  AddCR=$ADDCR  Filter=$FILTER\n\n\c"
	for X in $FLIST
	do 
		echo "--- $X\n\c" 
	done
	echo "\014\c"
}

yafilter () {
	if [ "$YAG" = yes ]
	then
		FILTER="$YATIND/bin/ya_gdriver"
	elif [ "$YA" = yes ]
	then
		FILTER="$YATIND/bin/ya_raster"
	fi
}

#  Set the stty parameters back to their normal state upon completion
#  of an auxprint request

returnstty () {
	echo "\033[4i\c"
	TERMSTTY=`cat $STTYFILE`
	/bin/stty "$TERMSTTY" <&1 2>/dev/null
	/bin/rm "$STTYFILE" 2>/dev/null
	echo "Auxiliary print request complete\n"
}

#  Save the PostScript machine responses

trace890 () {
	if [ "$SAVEPS" = yes ]
	then
		echo "$RESPONSE" >>$SAVEPSFILE
	fi
}

#  Check the status of the LC890

query890 () {
	while :
	do
		read RESPONSE <&1
		trace890
		echo "$RESPONSE" | /bin/grep "Flushing:" >/dev/null 2>/dev/null
		if [ "$?" = 0 ]
		then
			while :
			do
				echo "\024\c"
				read RESPONSE <&1
				trace890
				echo "$RESPONSE" | /bin/grep "status: idle" >/dev/null 2>/dev/null
				if [ "$?" = 0 ]
				then
					break 2
				else
					/bin/sleep 1
				fi
			done
		fi
	done
}

noshatoi () {
	/usr/bin/disable -r"Can't find \"/usr/bin/shatoi\"" $PRINTER
}

#  Define default stty variables for 7-bit devices

set7bit () {
	BAUD="${BAUD:-1200}"		# 1200 baud
	CSX="${CSX:-cs7}"		# 7 data bits
	ISTRIP="${ISTRIP:-istrip}"	# strip the most significant bit
	PARITY="${PARITY:--parodd}"	# even parity
	OPOST="${OPOST:-opost}"		# post-process output
	ONLCR="${ONLCR:-onlcr"}		# map NL tp CR-NL on output
	CSTOPB="${CSTOPB:-cstopb}"	# two stop bits
	PARENB="${PARENB:-parenb}"	# generate and check parity
}

#  Define default stty variables for 8-bit devices

set8bit () {
	BAUD="${BAUD:-9600}"		# 9600 baud
	CSX="${CSX:-cs8}"		# 8 data bits
	ISTRIP="${ISTRIP:--istrip}"	# don't strip input characters to 7 bits
	PARITY="${PARITY:-parodd}"	# odd parity
	OPOST="${OPOST:-opost}"		# post-process output
	ONLCR="${ONLCR:-onlcr}"		# map NL to CR-NL on output
	CSTOPB="${CSTOPB:--cstopb}"	# 1 stop bit
	PARENB="${PARENB:--parenb}"	# do not generate or check parity
}

#  Trap process for PT4010 when operating in LaserJet or Diablo emulations.

trap1 () {
	trap "echo '\n\n\n\nRequest Canceled'; \
      	echo '\014\c'; \
      	echo '$UNSETC\c'; \
      	/bin/sleep 3; \
      	$UNLOCK ; \
      	exit 0" 15
}

#  Trap process for PT4010 when operating in PostScript emulation.

trap2 () {
	trap "echo '\03';\
	/bin/sleep 3;\
	$UNLOCK ;\
	exit 0" 15
}

#  Trap process for PT4000 when operating in Proprinter emulation.

trap3 () {
	trap "echo '\n\n\n\nRequest Canceled'; \
      	echo '\014\034Cz\c'; \
      	/bin/sleep 3; \
      	$UNLOCK ; \
      	exit 0" 15
}

#  General trap process

trap4 () {
	trap "echo '\n\n\n\nRequest Canceled'; \
      	echo '\014\c'; \
      	printblack;    \ 
      	/bin/sleep 3; \
      	$UNLOCK ; \
      	exit 0" 15
}

MFLAG=no
CFLAG=no
VFLAG=no
YFLAG=no
OFLAG=no
GFLAG=no
REQUEST="$1"
USERID="$2"
OPTITLE="$3"
COPIES="$4"
OPTS="$5"
shift 5
FILES="$*"
PRINTER=`basename $0`
EOP="\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
EOP1="\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
EOP2="\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
x="X                                                                              X"
y="X                         _____ perforation here _____                         X"
z="XXXXXXXXXXXXXXXXXXXXXXXXXXX Motorola Computer Systems XXXXXXXXXXXXXXXXXXXXXXXXXX"
BANNER="${BANNER:-yes}"
FLIST=""
NAME="$USERID"
VIA=""
FILTERARGS="${FILTERARGS:-}"
OXSTRING=""
PITCH="${PITCH:-}"
OFFSET=0
UOFFSET=no
ADDCR=yes
CROPT=""
TABS="-tabs"
BANNERP="onlcr"
FORMFEED=yes
QONE=no
YA=no
YAG=no
BINIT=
EMULATION="${EMULATION:-}"
STYLE="${STYLE:-}"
SETC=""
UNSETC=""
TOHPSETC="statusdict begin
5 setsoftwareiomode
end
\04"
TOD630SETC="statusdict begin
2 setsoftwareiomode
end
\04"
CSET=""
AUXPRINT="${AUXPRINT:-}"
DEF6A="\0175@B@B@B@B@B@B@B@B@B@b@B@B@B@B@R@B@B@B@B@j@B@B@B@B@B@B@B@B@B@v@B@B@"
DEF6B="B@B@B@B@B@B@B@j@B@B@B@B@R@B@B@B@B@b@B@B@B@B@B@B@B@B@BA@@@@@B@@@@@@"
DEF8A="\0175@B@B@B@B@B@B@B@B@B@B@B@B@b@B@B@B@B@B@B@R@B@B@B@B@B@j@B@B@B@B@B@B@B@B@B@B@B@B@B@v@B@B@B@"
DEF8B="B@B@B@B@B@B@B@B@B@j@B@B@B@B@B@B@R@B@B@B@B@B@B@b@B@B@B@B@B@B@B@B@B@B@B@BA@@@@@@@B@@@@@@@@"
VFU=no
VFUDATA=""
SUB="\032"
FS="\034"
GS="\035"
RS="\036"
VFUSTART="$GS"
VFUEND="$RS"
PSF=no
BANNERFILTER="/usr/bin/csoffset"
ENDOFFILE=""
LC890UNSET=no
DOFLUSH=no
COLOR=""
FORMDEF=""
FORMLENGTH="${FORMLENGTH:-}"
DEFFLENGTH=66
LEFTMARGIN="${LEFTMARGIN:-}"
RIGHTMARGIN="${RIGHTMARGIN:-}"
FIRSTLINE="${FIRSTLINE:-}"
LASTLINE="${LASTLINE:-}"
SAVEPS=no
SAVEPSFILE=""

set `ls -l "$DEV"`
case $5 in
     34* | 33*)
	DOSLEEP=no
	;;
esac

if [ "$FILTER" = no ]
then
	FILTER="/bin/cat"
else
	FILTER="/usr/bin/csoffset"
fi

for X in $OPTS
do
	case "$X" in
	black | -black | bl | -bl)		# DEFAULT print black
		;;
	magenta | -magenta | ma | -ma)		# print magenta
		MFLAG=yes
		;;
	cyan | -cyan | cy | -cy)		# print cyan
		CFLAG=yes
		;;
	violet | -violet | vi | -vi)		# print violet
		VFLAG=yes
		;;
	yellow | -yellow | ye | -ye)		# print yellow
		YFLAG=yes
		;;
	orange | -orange | or | -or)		# print orange
		OFLAG=yes
		;;
	green | -green | gr | -gr)		# print green
		GFLAG=yes
		;;
	B | -B)				# suppress banner pages
		BANNER=no
		;;
	b | -b)				# print banner pages
		BANNER=yes
		;;
	e* | -e*)			# select emulation
		EMULATION=`/bin/expr "x$X" : 'x-*e\(.*\)'`
		EMULATION=`echo "$EMULATION" | tr '[A-Z]' '[a-z]'`
		;;
	F | -F)				# suppress form feed after each copy
		FORMFEED=no
		;;
	fl[0-9]* | -fl[0-9]*)		# form length
		FORMLENGTH=`/bin/expr "$X" : '-*fl\(.*\)'`
		;;
	f | -f)				# use 'cat' (effectively no filter)
		FILTER=/bin/cat
		;;
	g | -g)				# use 450-style graphics
		ADDCR=no
		FILTER=/usr/bin/450
		;;
	L* | -L*)			# list of file names for front banner
		FLIST=`echo "$X" | /bin/sed -e 's/-*L//' -e 's// /g'`
		;;
	np | -np)			# do stty -opost
		OPOST="-opost"
		;;
	o | -o)				# no offset from left margin
		OFFSET=0
		UOFFSET=yes
		;;
	o[0-9]* | -o[0-9]*)		# offset by number of chars from left margin
		OFFSET=`/bin/expr "x$X" : 'x-*o\([0-9]*\)'`
		UOFFSET=yes
		;;
	psf | -psf)			# use the postscript text filter
		PSF=yes
		;;
	pst* | -pst*)			# save the PostScript responses
		SAVEPS=yes
		SAVEPSFILE=`/bin/expr "$X" : '-*pst\(.*\)'`
		;;
	psw | -psw | PSW | -PSW)	#set pitch according to printer switches
		PITCH=sw
		;;
	p[1-9]* | -p[1-9]* | -[1-9]* | [1-9]*)	# set pitch to <n> chars/inch
		PITCH=`/bin/expr "x$X" : 'x-*p*\([1-9][.0-9]*\)'`
		;;
	q* | -q*)			# special for Q1
		QONE="yes"
		ADDCR=no
		FORMFEED=no
		TABS=
		;;
	s* | -s*)			# STYLE name
		STYLE=`/bin/expr "$X" : '-*s\(.*\)'`
		;;
	u?* | -u?*)			# user name for banners
		NAME=`/bin/expr "x$X" : 'x-*u\(.*\)'`
		VIA="via: $USERID "
		;;
	xs | xs=y* | -xs | -xs=y*)	# select Xerox string input
		OXSTRING="\0377\000"
		;;
	xs=n* | -xs=n*)			# select Motorola private input
		OXSTRING="\0377\040"
		;;
	v | -v)				# Download standard 6lpi VFU image
		VFU=VFU6
		DEFAULTVFU=$DEF6A$DEF6B
		VFUFILE=""
		;;
	v* | -v*)			# Download custom 6lpi VFU image
		VFU=VFU6
		DEFAULTVFU=""
		VFUFILE=`/bin/expr "x$X" : 'x-*v\(.*\)'`
		;;
	V | -V)				# Download standard 8lpi VFU image
		VFU=VFU8
		DEFAULTVFU=$DEF8A$DEF8B
		VFUFILE=""
		;;
	V* | -V*)			# Download custom 8lpi VFU image
		VFU=VFU8
		DEFAULTVFU=""
		VFUFILE=`/bin/expr "x$X" : 'x-*V\(.*\)'`
		;;
	y | -y)				# special for PAC
		YA=yes			# us ya_raster
		if [ -r /install/ya.tind ]
		then
			YATIND=`/bin/sed -n 2p </install/ya.tind`
			export YATIND
		fi
		;;
	Y | -Y)				# special for PAC
		YA=yes			#use one of the YA graph filters
		YAG=yes
		if [ -r /install/ya.tind ]
		then
			YATIND=`/bin/sed -n 2p </install/ya.tind`
			export YATIND
		fi
		;;
	esac
done

NEXTLINE="\n"
case "$MODEL" in
    pt30)
	yafilter
	case $PITCH in
    		15)	DPITCH="\033\037\011" ;;
    		12)	DPITCH="\033\037\013" ;;
    		*)	DPITCH="\033\037\015"; PITCH=10 ;;
	esac

	set7bit

	INITSEQ="\033\037\015"
	BINIT="\033\037\015"
	EOP="\r\033\013?"
	CSET="D630"
	;;

    pt35)
	yafilter
	case $PITCH in
    	    sw)	DPITCH="\033S"  ;;
	    15)	DPITCH="\033\037\011" ;;
    	    12)	DPITCH="\033\037\013" ;;
    	    *)	DPITCH="\033\037\015"; PITCH=10 ;;
	esac

	set7bit

	INITSEQ="\033\037\015"
	BINIT="\033\037\015"
	NEXTLINE="\f\r"
	EOP="\r\033\013?"
	CSET="D630"
	;;

    pt31|pt32)
	yafilter
	case $PITCH in
    	    16|16.36)	DPITCH="\033\024" ;;
    	    *)		DPITCH="\033\023"; PITCH=10 ;;
	esac

	set8bit

	INITSEQ="\033\023"
	BINIT="\033\023"
	;;

    pt34)
	yafilter
	# in ANSI mode
	case $PITCH in
    	    16|16.5)	DPITCH="\033[4w" ;;
    	    15)		DPITCH="\033[9w" ;;
    	    13|13.2)	DPITCH="\033[3w" ;;
    	    12)		DPITCH="\033[2w" ;;
    	    8|8.25)	DPITCH="\033[8w" ;;
    	    7.5)	DPITCH="\033[10w" ;;
    	    6.6)	DPITCH="\033[7w" ;;
    	    6)		DPITCH="\033[6w" ;;
    	    5)		DPITCH="\033[5w" ;;
    	    *)		DPITCH="\033[w"; PITCH=10 ;;
	esac

	set8bit

	INITSEQ="\003\033(B\033[z\033[m\033[w"
	BINIT="\033[w"
	CSET=C353
	;;

    pt36)		# Dataproducts line printers
	yafilter

	PITCH=10

	set8bit
	OPOST=-opost

	INITSEQ=""
	CSET=""
	VFU=no
	;;

    pt37 | ptTJ)        # fujitsu dx2100, 80 column
	yafilter
	case $PITCH in
	    15) DPITCH="\033m" ;;
	    12) DPITCH="\033M" ;;
	    *)  DPITCH="\033P" ; PITCH=10 ;;
	esac

	set8bit

	BLACK="\033\0162\060"
	INITSEQ="\033@\033P"
	BINIT="\033@\033P"
	case "$MODEL" in
    	    pt37)
		CSET="Epson80" ;;
    	    ptTJ)
		CSET="TJ" ;;
	esac
	;;

    pt38 | ptFX)        # fujitsu dx2200, 136 column
	yafilter
	case $PITCH in
	    15) DPITCH="\033m" ;;
	    12) DPITCH="\033M" ;;
	    *)  DPITCH="\033P" ; PITCH=10 ;;
	esac
 
	set8bit

	BLACK="\033\0162\060"
	INITSEQ="\033@\033P"
	BINIT="\033@\033P"
	CSET="Epson132"
	;;

    pt39)		#fujitsu dl2400
	yafilter
	case $EMULATION in
	    dpl24i | ibmgph | fx80)
		case $PITCH in
		    18) DPITCH="\033\017" ;;
		    12) DPITCH="\033M" ;;
		    *)  DPITCH="\033P"; PITCH=10 ;;
		esac

		INITSEQ="\033!\000\033V0\033x\033\065\033T\033\000\033B\000\033i0\033U0\033P"
		BINIT=$INITSEQ
		CSET="Epson132"
		;;
    	    *)
		EMULATION="DIABLO"
		case $PITCH in
		    sw) DPITCH="\033S" ;;
		    15) DPITCH="\033\037\011" ;;
		    12) DPITCH="\033\037\013" ;;
		    *)  DPITCH="\033\037\015"; PITCH=10 ;;
		esac

		INITSEQ="\033B\033C\017\033X\033Q\033\062\033\064\033\037\015"
		BINIT="\033\rP"
		CSET=D630
		;;
	esac

	set8bit

	BLACK="\033\0162\060"
	EOP="\r\033\013?"
	;;

    pt40 | pt4005 | ptLC)        # NEC LC800 8ppm laser
	yafilter

	case $EMULATION in
	    [hH][pP]*)
		EMULATION="HPGL"
		FILTER=/bin/cat
		OPOST=-opost
		INITSEQ="\034AaDF;"
		BINIT="\034Az\034Cz\033\032I\033\037\015\034)\001\000"
		CSET="HPGL"
		;;
	    [pP]*)
		EMULATION="PROPRINTER"
		case $PITCH in
	    	    12) DPITCH="\033\072" ;;
	     	    *) DPITCH="\022"; PITCH=10 ;;
		esac
		case $STYLE in
	    	    [cC][lL])		#Courier Landscape
			FONT="\034)\0202\000" ;;
	    	    [cC][iI])		#Courier Italics
			FONT="\034)\002\000" ;;
	    	    *)			#Courier 10 Portrait
			FONT="\034)\001\000" ;;
		esac

		INITSEQ="\034Ca\024\033F\033H\033I0\033R\033T\022\034)\001\000"
		BINIT="\034Cz\033\032I\033\037\015\034)\001\000"
		CSET="FS"
		;;
    	    *)
		EMULATION="DIABLO"
		case $PITCH in
	    	    sw) DPITCH="\033S" ;;
	    	    15) DPITCH="\033\037\011" ;;
		    12) DPITCH="\033\037\013" ;;
		     *) DPITCH="\033\037\015"; PITCH=10 ;;
		esac
		case $STYLE in
		    [cC][lL])		#Courier Landscape
			FONT="\034)\0202\000" ;;
		    [cC][iI])		#Courier Italics
			FONT="\034)\002\000" ;;
		    f03)		#Font Cartridge 03 (Courier 12 pitch, portrait)
			FONT="\034)\003\000"
			DPITCH="\033\037\013"
			PITCH=12 ;;
		    f04)		# Font Cartridge 04 (Courier, 15 pitch, portrait)
			FONT="\034)\004\000" 
			DPITCH="\033\037\011"
			PITCH=15 ;;
		    f08)		# Font Cartridge 08 (Prestige Elite, 12 pitch, portrait)
			FONT="\034)\010\000"
			DPITCH="\033\037\013"
			PITCH=12 ;;
		    f09)		# Font Cartridge 09 (Prestige Pica, 10 pitch, portrait)
			FONT="\034)\011\000"
			DPITCH="\033\037\015"
			PITCH=10 ;;
		    f0[aA])		# Font Cartridge 0A (Letter Gothic,12 pitch, portrait)
			FONT="\034)\012\000"
			DPITCH="\033\037\013"
			PITCH=12 ;;
		    f0[bB])		# Font Cartridge 0B (Super Focus, 10 pitch, portrait)
			FONT="\034)\013\000"
			DPITCH="\033\037\015"
			PITCH=15 ;;
		    f18)		# Font Cartridge 18 (Letter Gothic, 15 pitch, portrait)
			FONT="\034)\030\000" 
			DPITCH="\033\037\011"
			PITCH=15 ;;
		    f82)		# Font Cartridge 82 (Courier 12 pitch, landscape)
			FONT="\034)\0202\000"
			DPITCH="\033\037\013"
			PITCH=12 ;;
		    f83)		# Font Cartridge 83 (Courier, 15 pitch, landscape)
			FONT="\034)\0203\000"
			DPITCH="\033\037\011"
			PITCH=15 ;;
		    f84)		# Font Cartridge 84 (Super Focus, 10 pitch, landscape)
			FONT="\034)\0204\000"
			DPITCH="\033\037\015"
			PITCH=15 ;;
		    f85)		# Font Cartridge 85 (Letter Gothic, 15 pitch, landscape)
			FONT="\034)\0205\000" 
			DPITCH="\033\037\011"
			PITCH=15 ;;
		    f86)		# Font Cartridge 86 (Prestige Elite, 12 pitch, landscape)
			FONT="\034)\0206\000"
			DPITCH="\033\037\013"
			PITCH=12 ;;
		    f87)		# Font Cartridge 87 (Letter Gothic, 12 pitch, landscape)
			FONT="\034)\0207\000" 
			DPITCH="\033\037\013"
			PITCH=12 ;;
		    f88)		# Font Cartridge 88 (Prestige Pica, 10 pitch, landscape)
			FONT="\034)\0210\000"
			DPITCH="\033\037\015"
			PITCH=10 ;;
		    *)			#Courier 10 Portrait
			FONT="\034)\001\000" ;;
		esac

		INITSEQ="\034Cz\033\032R\033\032I\016\033\037\015\034)\001\000"
		BINIT="\033\032I\033\037\015\034)\001\000"
		CSET="FS"
		;;
	esac

	set8bit

	EOP=$EOP2
	y=$x
	;;

    pt41)			# NEC LC-890
	case $EMULATION in
	    [dD]*)
		EMULATION="DIABLO" 
		yafilter
		case $PITCH in
	    		15) DPITCH="\033\037\011" ;;
	    		12) DPITCH="\033\037\013" ;;
	    		*) DPITCH="\033\037\015"; PITCH=10 ;;
		esac
		case $STYLE in
			[pP]60)
				FONT="\033\013\004\033T\033\013\077\033L\033\013\004"
				if [ "$UOFFSET" = no ]
				then
					OFFSET=1
				fi
				;;
			*)
				FONT="" ;;
		esac

		INITSEQ="\033&\033C\033\062\033R\033X\033\037\015"
		BINIT="\033\037\015"
		EOP=$EOP2
		y=$x
		SETC=$TOD630SETC
		UNSETC="\033\0177\060\04"
		LC890UNSET=yes
		CSET="D630"
		;;
	    [lL]*)			# HP LaserJet Plus
		EMULATION="LASERJET"
		yafilter
		case $PITCH in
			16|16.66)  FPITCH="\033(s16.66H" ;;
			15)	   FPITCH="\033(s15H" ;;
			12)	   FPITCH="\033(s12H" ;;
			*)  	   FPITCH="\033(s10H"; PITCH=10 ;;
		esac
		DPITCH=""
		case $STYLE in
			[cC][lL]*)
				FONT="\033&l1O\033(8U\033(s0P$FPITCH\033(s12v0b3T"
				;;
			[lL][pP]*)
				FPITCH="\033(s13.6H"
				PITCH="13.6"
				FONT="\033&l1O\033(8U\033(s0P$FPITCH\033(s0b0T\033&l8d60F\033&a4L"
				;;
			[pP]66)
				FONT="\033&l0O\033(8U\033(s0P$FPITCH\033(s12v0b3T\033&l7.27c66F"
				;;
			*)
				FONT="\033&l0O\033(8U\033(s0P$FPITCH\033(s12v0b3T"
				;;
		esac

		INITSEQ="\033E"
		BINIT="\033E"
		SETC=$TOHPSETC
		UNSETC="\033\0177\060\04"
		LC890UNSET=yes
		EOP=$EOP2
		y=$x
		CSET="LJ+"
		;;
	    *)				# We assume PostScript
		EMULATION="POSTSCRIPT"
		yafilter
		if [ "$PSF" = yes ]
		then
			FILTER="/usr/bin/pstext.filter"
		fi

		QUERY890=yes
		ENDOFFILE="stop\n\04"
		BANNERFILTER="/usr/bin/pstext.filter"
		CSET="PS"
		EOP=$EOP2
		y=$x
		;;
	esac

	set8bit
	DOFLUSH=yes

	;;

    pt4[3456])		# Hitachi line printers
	yafilter

	PITCH=10

	set8bit
	OPOST=-opost

	ADDCR=no
	INITSEQ=""
	CSET=""
	;;

    ptLJ)			# HP 2686A Laser Jet
	yafilter

	case $PITCH in
		16|16.36)  DPITCH="\033(s16.66H" ;;
		*)  	   DPITCH="\033(s10H"; PITCH=10 ;;
	esac

	set8bit

	INITSEQ="\033E"
	BINIT="\033E"
	EOP=$EOP2
	y=$x
	CSET="LJ+"
	;;

    ptLW)			# Apple Laserwriter
	yafilter
	if [ "$PSF" = yes ]
	then
		FILTER="/usr/bin/pstext.filter"
	fi
	
	set8bit

	INITSEQ=""
	BANNERFILTER="/usr/bin/pstext.filter"
	EOP=$EOP2
	y=$x
	CSET="PS"
	;;

    ptPL)			# HP 7475A 6-pen Plotter
	if [ "yes" = "$YAG" ]
	then 
		FILTER="$YATIND/bin/ya_gdriver"
	fi

	BANNER=no

	set8bit
	OPOST=-opost
	ONLCR=-onlcr

	INITSEQ="\033.I64;;17:\033.N;19:"
	CSET="HPGL"
	;;

    *)
	/usr/bin/disable -r"unrecognized model '$MODEL' in $PRINTER" $PRINTER
	exit 1

esac

if [ "$LANG" != ""  -a  "$FILTER" = /usr/bin/csoffset ]
then 
	if [ ! -r /usr/lib/cs.printer/$MODEL.$LANG ]
	then
		/usr/bin/disable -r"unknown language '$LANG' on $PRINTER" $PRINTER
		exit 1
	fi
	XSTRING="${OXSTRING:-\0377\040}" 	# Select character set 
	XLATETBL="t=/usr/lib/cs.printer/$MODEL.$LANG"
else 
	XSTRING=""
	XLATETBL=""
fi

if [ ! -x $FILTER ]
then
	/usr/bin/disable -r"can't find/execute $FILTER:" $PRINTER
	exit 1
fi

if [ -n "$BANNERFILTER"  -a  ! -x "$BANNERFILTER" ]
then
	/usr/bin/disable -r"can't find/execute banner filter $BANNERFILTER:" $PRINTER
	exit 1
fi

if [ "$VFU" = VFU6  -o  "$VFU" = VFU8 ]
then
	if [ "$VFU" = VFU6 ]
	then
		VFUSTART="\033\066$VFUSTART"
	else
		VFUSTART="\033\070$VFUSTART"
	fi
	if [ "$DEFAULTVFU" != "" ]
	then
		VFUDATA="$VFUSTART${DEFAULTVFU}$VFUEND"
	else
		if [ ! -r "$VFUFILE" ]
		then
			/usr/bin/disable -r"Can't find VFU file \""$VFUFILE"\"" $PRINTER
			exit 1
		else
			VFUDATA=$VFUSTART
			for i in `cat "$VFUFILE"`
			do
				VFUDATA="$VFUDATA$i"
			done
			VFUDATA=$VFUDATA$VFUEND
		fi
	fi
fi

if [ -n "$FORMLENGTH" ]
then
	if [ $FORMLENGTH -ge 1  -a  $FORMLENGTH -lt 127 ]
	then
		if [ ! -x /usr/bin/shatoi ]
		then
			noshatoi
			exit 1
		else
			FORMLENGTH=`/usr/bin/shatoi $FORMLENGTH`
			case $MODEL in
			    pt39 | pt40)
				if [ "$EMULATION" = DIABLO ]
				then
					FORMLENGTH="\033`/usr/bin/shatoi $FORMLENGTH`"
				fi
				;;
			    pt37 | pt38)
				FORMLENGTH="\033C`/usr/bin/shatoi $FORMLENGTH`"
				;;
			esac
		fi
	fi
fi

if [ "$SAVEPS" = yes ]
then
	if [ "$SAVEPSFILE" = "" ]
	then
		SAVEPSFILE="/usr/tmp"
	fi
	if [ ! -d $SAVEPSFILE ]
	then
		/usr/bin/disable -r"Can't find pst option directory \""$SAVEPSFILE"\"" $PRINTER
		exit 1
	fi
	SAVEPSFILE=${SAVEPSFILE}/ps..$REQUEST
fi

case $MODEL in 		# note: only last color choice sticks
    pt37 | pt38 | pt39)
        if [ $MFLAG = yes ]
        then
              	COLOR="\033\0162\061"
       	fi
        if [ $CFLAG = yes ]
        then
             	COLOR="\033\0162\062"
        fi
        if [ $VFLAG = yes ]
        then
              	COLOR="\033\0162\063"
        fi
        if [ $YFLAG = yes ]
        then
               	COLOR="\033\0162\064"
        fi
        if [ $OFLAG = yes ]
        then
               	COLOR="\033\0162\065"
        fi
        if [ $GFLAG = yes ]
        then
             	COLOR="\033\0162\066"
       	fi
        ;;
    *)
	;;
esac

case "$FILTER" in
    */csoffset)
	if [ "$ADDCR" = yes ] 
	then 
		CROPT=onlcr 
	else 
		OFFSET=0 
	fi
	FILTERP="$TABS"
	;;
    *)
	OFFSET=""
	FILTERP=""
	BANNERP=""
	;;
esac

# Since we are about to talk to the printer, make sure someone else is not using
# it.  Establish a trap handler to undo the lock if this job is canceled.

LOCKFILE=`basename $DEV`
LOCKFILE=/usr/tmp/LCK..$LOCKFILE
LOCK=":"
UNLOCK=":"
if [ -x /usr/bin/Qplock ]
then
	LOCK="/usr/bin/Qplock get 2 3 4 5 $LOCKFILE 7 8 9 10 11 >/dev/null 2>&1"
	UNLOCK="/usr/bin/Qplock release 2 3 4 5 $LOCKFILE 7 8 9 10 11 > /dev/null 2>&1"
elif [ -x /usr/bin/MPSlock ]
then
	LOCK="/usr/bin/MPSlock get 2 3 4 5 $LOCKFILE 7 8 9 10 11 > /dev/null 2>&1"
	UNLOCK="/usr/bin/MPSlock release 2 3 4 5 $LOCKFILE 7 8 9 10 11 > /dev/null 2>&1"
fi

#  Setup trap processing

case $MODEL in
    ptLW | pt41)
	case $EMULATION in
	    L* | D*)
		TRAP="trap1"
		;;
	    *)
		TRAP="trap2"
		;;
	esac
	;;
    pt40)
	case $EMULATION in
	    P*)
		TRAP="trap3"
	    	;;		
	    *)
		TRAP="trap4"
		;;
	esac
	;;
    *)
	TRAP="trap4"
	;;
esac

# Establish the critical section

$LOCK
$TRAP

#
#  If this is an auxprint operations, we need to save the existing stty setup
#  for this user and then set the stty parameters that change the physical
#  transmission chararcteristics to nulls.
#
 
if [ "$AUXPRINT" = yes ]
then
	SAVESTTY=`basename $DEV`
	STTYFILE=/usr/tmp/STTY..$SAVESTTY
	/bin/stty -g <&1 >$STTYFILE 2>/dev/null
	echo "\nBeginning auxiliary print request"
	echo "\033[5i"
	BAUD=""
	CSX=""
	ISTRIP=""
	PARITY=""
	CSTOPB=""
	PARENB=""
fi

TAB="tabs"
if [ "$QONE" = yes ]
then
	OPOST="-opost"
	ONLCR="-onlcr"
fi

if [ "$YA" = yes ]
then
	OPOST="-opost"
	ONLCR="-onlcr"
fi

#  Set the tty modes 

if [ "$USESTTY" = yes ]
then
	/bin/stty $BAUD $CSX $ISTRIP $PARITY $OPOST $ONLCR $CSTOPB \
              $PARENB $TAB icanon ixon -ixany ixoff clocal \
              -hupcl cread -ignbrk brkint -parmrk -inpck inlcr \
              -igncr icrnl -iuclc -isig -xcase -echo -echoe -echok \
              -echonl noflsh -olcuc -ocrnl -onocr -onlret \
              -ofill -ofdel eof "^D" <&1 2>/dev/null
	if [ "$DOSLEEP" = yes ]
	then
		/bin/sleep 1
	fi
fi

if [ "$DOFLUSH" = yes ]
then
	exec <&1
	/usr/bin/psflush
	if [ "$SAVEPS" = yes ]
	then
		>$SAVEPSFILE
	fi
fi

if [ -n "$SETC" ]
then
	echo "$SETC"
	/bin/sleep 3
fi

echo "$INITSEQ\c"
echo "$VFUDATA\c"

if [ "$BANNER" = yes ]
then
	printblack		#Make sure the banner gets printed in black
	(				# start sub-shell
	printbanner
	) | $BANNERFILTER $FILTERARGS onlcr $XLATETBL   # end sub-shell

	echo "$ENDOFFILE\c"

	if [ "$QUERY890" = yes ]
	then
		query890
	fi
fi

/bin/stty -opost <&1 2>/dev/null
if [ "$DOSLEEP" = yes ]
then
	/bin/sleep 1
fi
echo "$FONT\c"
echo "$DPITCH\c"
echo "${FORMLENGTH}\c"
printblack
echo "$COLOR\c"

/bin/stty "$OPOST" <&1 2>/dev/null
if [ "$DOSLEEP" = yes ]
then
	/bin/sleep 1
fi

if [ "$QONE" = "yes" ]
then
	CROPT=""
	OFFSET=""
	FILTERP=""
fi

case $FILTER in
    */cat)
	printfiles
	errcode=$?
	;;
    */csoffset)
	printfiles | $FILTER $FILTERARGS $CROPT $OFFSET $FILTERP \
                            $XLATETBL
	errcode=$?
	;;
    */450)
	printfiles | $FILTER
	errcode=$?
	;;
    *)			# Must be one of the ya filters
	export DEV
	printfiles | $FILTER $MODEL $CSET
	errcode=$?
	;;
esac

echo "$ENDOFFILE\c"

if [ "$QUERY890" = yes ]
then
	query890
fi

echo "$BINIT\c"
printblack		# Make sure the color gets set back to black

if [ "$BANNER" = yes ]
then
	(				# start sub-shell
	echo "$XSTRING\c"
	echo "$EOP$z\n$x\n$y$NEXTLINE$x\n$x\n$z\n                             --- End of listing ---\n\014\c" 
	) | $BANNERFILTER $FILTERARGS onlcr $XLATETBL

	echo "$ENDOFFILE\c"

	if [ "$QUERY890" = yes ]
	then
		query890
	fi
fi

if [ $errcode -ne 0 ]
then
	/usr/bin/disable -r"error code $errcode from $FILTER filter" $PRINTER
	if [ -n "$UNSETC" ]
	then
		echo "$UNSETC\c"
	fi
	if [ "$AUXPRINT" = yes ]
	then
		returnstty
	fi
        $UNLOCK       # unlock the critical section
	exit 1
fi

if [ -n "$UNSETC" ]
then
	if [ "$LC890UNSET" = yes ]
	then
		/usr/bin/psflush
		echo "$UNSETC\c"
		/bin/stty -ixon eof "^Q" <&1 2>/dev/null
		if [ "$DOSLEEP" = yes ]
		then
			/bin/sleep 1
		fi
		while read RESPONSE <&1
		do
			:
		done
		/bin/stty ixon eof "^D" <&1 2>/dev/null
		if [ "$DOSLEEP" = yes ]
		then
			/bin/sleep 1
		fi
	else
		echo "$UNSETC\c"
	fi
fi

if [ "$AUXPRINT" = yes ]
then
	returnstty
fi

$UNLOCK		# unlock the critical section

exit 0
