Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
turboprint / usr / lib / turboprint / tpfilter
Size: Mime:
#! /bin/bash
#
# tpfilter - line printer input filter for turboprint driver system
#
# Copyright 2000-2004 ZEDOnet
# Version 1.8
# Created 12.4.2000 Florian Zeiler
# 17.11.2000: improved lprng compatibility (flags, don't use rewindstdin)
# 27.04.2001: DVI printing implemented
# 08.01.2002: PNG, XPM, GZIP and unknown format handling added. HoRo
#             PDF handling corrected, user raw printing added. HoRo
# 03.10.2002: Postscript files are parsed for PPD-file options
# 07.04.2003: process substitution >(...) removed (problems with bash 2.05)
# 14.07.2004: process substitution used again (problems with gs writing 
#             messages to stdout)
########
# uncomment this line if you want some logging (every nonempty string works!)
#TPFILTERDEBUG=1
########

PATH=/bin:/usr/bin:/usr/local/bin
PATH=${PATH}:/usr/bin/TeX:/usr/X11R6/bin
PATH=${PATH}:/sbin:/usr/sbin
#export PATH

########
# get Turboprint paths
########
#
eval $(cat "/etc/turboprint/system.cfg")
LOGFILE="$TPPATH_LOG/turboprint/print.log"
TPSTDIN="$TPPATH_FILTERS/tpstdin"
SMBPRINT="$TPPATH_FILTERS/smbprint"
SOCKETPRINT="$TPPATH_FILTERS/socketprint"
NCPPRINT="$TPPATH_FILTERS/ncpprint"

echo >> $LOGFILE || LOGFILE="/dev/null"
echo "#######################################################" >> $LOGFILE
echo "New print job $(date) (tpfilter 1.92)" >> $LOGFILE
echo $0 $* >> $LOGFILE

########
# parse parameters
########
#
# lpr:
#  -c -wwidth -llength -iident -n login -h host acct-file
#  -c set with lpr -l ... means transmit file unchanged ("raw") to printer
# LPRng:
#  -Ffileformatcharacter -ejobdatafile -kcontrolfile
#  -Hhost -Jjob -Llogin -Pprintername -Qqueue -aacct-file -dspooldir -fjobfilename
#  -hhost -jjobnumber -llength -nlogin -wwidth -xxpos -yypos acct-file
#  -c means transmit file unchanged ("raw") to printer
#

while [ -z "${1%%-*}" -a -n "$1" ] ; do
    temp="${1#-}"
    shift
    case "$temp" in
	a*) # account file (lprng)
	{
	  ACCOUNTFILE="${temp#?}"
	};;
        #c) # raw printer file
        #{
        #  TPHEADER="raw"
        #};;
	e*) # data file (lprng)
	{
	  DATAFILE="${temp#?}"
	};;
	f*) # job name (lprng)
	{
	  JOBFILE="${temp#?}"
	};;
	h*) # host name
	{
	  HOST="${temp#?}"
          if [ -z "$HOST" ] ; then
		HOST="$1"; shift
	  fi
	};;
	j*) # job name (lpr) or number (lprng)
	{
	  JOB="${temp#?}"
          if [ -z "$JOB" ] ; then
		JOB="$1"; shift
	  fi
	};;
	n*) # login name
	{
	  LOGIN="${temp#?}"
          if [ -z "$LOGIN" ] ; then
		LOGIN="$1"; shift
	  fi
	};;
	*);; # other flags: ignore
    esac
done

# account file set by -a or is last parameter
if [ -z "$ACCOUNTFILE" ] ; then
    eval temp="\${$#}"
    if [ -n "$temp" -a "$temp" != "$0" ]; then
	ACCOUNTFILE="$temp"
    fi
    unset temp
fi

# job name set by -f (lprng) or -j (lpr)
if [ $JOBFILE ] ; then # lprng
    JOB="$JOBFILE"
fi

# the spool path is the path to the account file
SPOOLPATH=$(dirname $ACCOUNTFILE)

# the configuration name is the last part the spool path
CONFIGNAME=$(basename $SPOOLPATH)

########
# create temporary file names
########
#
# this file holds the first chunk (up to 64K) of stdin
CHUNKFILE="$TPPATH_TEMP/tpfilter$$.chunk"
# this is a temp file needed by some commands
TEMPFILE="$TPPATH_TEMP/tpfilter$$.tmp"

########
# find user's home directory
########
#
YPD="`domainname`"
YPS="`grep -E '^\+' /etc/passwd|head -1c`"

if [ -n "$YPD" -a "$YPS" = "+" ] ; then
    set -- `(cat /etc/passwd; ypcat passwd)|grep "^$LOGIN:"|cut -d':' -f6`
else
    set -- `grep "^$LOGIN:" /etc/passwd |cut -d':' -f6`
fi
HOMEDIR="$1"

########
# process config files
########
#
# the global config file is in the spool path
CONFIGFILE="$SPOOLPATH/tpprinter.cfg"

# the user config file is in the home directory
USERCONFIG="$HOMEDIR/.turboprint/$CONFIGNAME.cfg"

########
# do some logging
########
#
if [ $TPFILTERDEBUG ]; then
	echo "CONFIGNAME=$CONFIGNAME CONFIGFILE=$CONFIGFILE USERCONFIG=$USERCONFIG" >> $LOGFILE
	echo "JOB=$JOB DATA=$DATAFILE LOGIN=$LOGIN HOST=$HOST HOMEDIR=$HOMEDIR TEMP=$TEMPFILE" >> $LOGFILE
fi

# evaluate config files; user settings overwrite global settings
eval $(cat $CONFIGFILE)
# user config: put "declare" in front of file (security)
if [ -e "$USERCONFIG" ] ; then
	eval $(echo "declare " $(cat $USERCONFIG))
fi

########
# check for file header, cut off a chunk (64K) from stdin for filetype determination
# $CHUNKFILE is needed for printing, DON'T change its contents
########
#
eval $($TPSTDIN --cut $CHUNKFILE)

########
# check file type
########
#
if [ $TPHEADER -a $TPHEADER = "raw" ] ; then
	# don't interpret, send directly to the printer (PCL, ESCP2, ...)
	FILETYPE="tpraw"
else
	# get file type using command "file" and translate to lower case
	# -b   - strip filename
	# -z   - look into compressed files
	FILETYPE=$(file -b -z $CHUNKFILE | tr 'A-Z' 'a-z')
	# if compressed file...
        case "$FILETYPE" in
          *gzip\ compressed\ data*)
        	DECOMPRESS="zcat |";;
	esac
fi

echo "FILETYPE=$FILETYPE" >> $LOGFILE

########
# select input filter depending on file type
########
#
# default values
INPUTFILTER=""
GSINPUT=""

case "$FILETYPE" in

 tpraw)
    # raw printer commands from tpconfig or via lpr -l ...
    {
    	INPUTFILTER="";
    };;

 # first the formats that go through ghostscript

 *postscript*)
    # postscript or EPS: this is our preferred input
    {
	GSINPUT="ps";
	# parse chunkfile for ppd-file options
	# keep defaults if no options found
	#       keep zoom mode
	GSPPDFOUND=""
	if [ $TPNEWFILTER ] ; then
		tpprint -v2 -d$TPDRIVER -c$TPCARTRIDGE -q$TPQUALITY -m$TPPAPER\
 -g$TPCOLORMODE -f$TPPAGESIZE -l$LOGFILE --psfeatures $CHUNKFILE $TEMPFILE >> $LOGFILE
		echo "----------- Start of var file -----------" >> $LOGFILE
		cat $TEMPFILE >> $LOGFILE
		echo "----------- End of var file -----------" >> $LOGFILE
		eval $(cat $TEMPFILE)

		if [ $GSPPDFOUND ] ; then
			GSXDPI=$[$GSXDPI * $GSXSCALE / 100]
			GSYDPI=$[$GSYDPI * $GSYSCALE / 100]
			case $GSCOLORMODE in
			 0) GSDRIVER="pbmraw" ;;
			 1) GSDRIVER="pgmraw" ;;
			 2) GSDRIVER="pcx24b" ;;
			 3) GSDRIVER="pcx256" ;;
			 4) GSDRIVER="bmp32b" ;;
			 *) GSDRIVER="pcx24b" ;;
			esac
		fi
	fi
    };;

 pdf\ document*)
    # portable document format:
    {
        # almost the same as postscript...
        # ...BUT gs need a temp file (selected by GSINPUT="pdf";)
	GSINPUT="pdf";
    };;

 fig\ image\ text*)
    # fig drawing
    {
	# use fig2dev
	# -Lps - language = postscript
	# -P   - don't create eps but printable ps file
	# -c   - center on page
	INPUTFILTER="fig2dev -Lps -P -c | ";
	GSINPUT="ps";
    };;

 tex\ dvi\ file*)
    # tex dvi document
    {
	INPUTFILTER="cat - > $TEMPFILE | dvips -f $TEMPFILE | "
	TPCOLORMODE=0
	GSINPUT="ps";
    };;


 troff\ preprocessor*)
    # groff document
    {
	# use grog
	# -S   - safer mode
	# -Tps - output format = postscript
        # -    - input from stdin
	INPUTFILTER="grog -S -Tps - | "
	TPCOLORMODE=0
	GSINPUT="ps";
    };;

 html\ document*)
    # html document
    {
	# use html2ps
	# -e   - charset
	# -u   - underline links
	# -H   - use hyphenation
	INPUTFILTER="html2ps -e ISO-8859-1 -u -H | ";
	TPCOLORMODE=0
	GSINPUT="ps";
    };;

########
# TEMPLATE for document format XXXX
# filter program creates postscript output
########
# XXXX\ document*)
#    # XXXX format
#    {
#	# use XXXXtops; output format ps is read by ghostscript
#	INPUTFILTER="XXXXtops | ";
#	GSINPUT="ps";
#    };;
########

 # bitmap images that are handled directly by turboprint

 pbm*image*|netpbm\ pbm*image*|\
 pgm*image*|netpbm\ pgm*image*|\
 ppm*image*|netpbm\ ppm*image*)
    # portable bit/grey/pixmap image
    {
	# pnm is read by turboprint
	INPUTFILTER="";
    };;

 # bitmap images that are converted into p[bgp]m format

 pc\ bitmap*)
    # bmp image
    {
	# use bmptoppm; ppm is read by turboprint
        # the problem with p?m formats is that there
	# is no picture size (dpi) information
	INPUTFILTER="bmptoppm | ";
    };;

 tiff\ image*)
    # tiff image
    {
	# use tifftopnm; pnm is read by turboprint
	INPUTFILTER="tifftopnm | ";
    };;

 gif\ image*)
    # gif image
    {
	# use giftopnm; pnm is read by turboprint
	INPUTFILTER="giftopnm | ";
    };;

 jpeg\ image*)
    # jpeg image
    {
	# use djpeg; output format pnm is read by turboprint
	# todo: options for djpeg...?
	INPUTFILTER="djpeg | ";
    };;

 png\ image*)
    # png image
    {
	# use pngtopnm; pnm is read by turboprint
	INPUTFILTER="pngtopnm | ";
    };;

 x\ pixmap\ image*)
    # xpm image
    {
        # use xpmtoppm; ppm is read by turboprint
        INPUTFILTER="xpmtoppm | ";
    };;

 sun\ raster\ image*|rasterfile*)
    # sunraster image
    {
	# use rasttopnm; pnm is read by turboprint
	# todo: options for rasttopnm...?
	INPUTFILTER="rasttopnm | ";
    };;

########
# TEMPLATE for image format XXXX
# filter program creates portable bit/grey/pixmap format
########
# XXXX\ image*)
#    # XXXX image
#    {
#	# use XXXXtop?m; output format p?m is read by turboprint
#	INPUTFILTER="XXXXtop?m | ";
#    };;
########

 *image*)
    ########
    # other image format, print error message
    # add other unsupported formats here
    # DON'T use *data* (gzip compressed data...)
    ########
    #
    {
        INPUTFILTER="echo -e \"Sorry, this image format is not (yet) supported:\n$FILETYPE\nAdd your own handler to $TPPATH_FILTERS/tpfilter\" | pbmtext | ";
    };;

 # pcx not (yet) known by 'file'

 ########
 # ascii text must follow all image formats...
 # ...because some images are in text format
 # (xpm: x pixmap image text)
 # add your text format strings here
 ########
 #
 *ascii*|*text*|*english*|*script*|*mail*|*news*)
    # interpret as ascii text
    {
	# todo:
	# - set page size
	# - ...?

	# if a2ps doesn't exist, use enscript, else ??
	A2PSCOMMAND="a2ps"
	type a2ps &> /dev/null || A2PSCOMMAND="enscript"
	A2PSOPTIONS="--output=-"
	if [ $A2PSCOMMAND = "a2ps" ] ; then
		A2PSOPTIONS="$A2PSOPTIONS --medium=$A2PSMEDIUM"
	else
		A2PSOPTIONS="$A2PSOPTIONS --media=$A2PSMEDIUM"
	fi
	# either use defaults or set landscape / number of columns
	if [ $A2PSDEFAULTS -ne 1 ]; then
		if [ $A2PSLANDSCAPE -eq 1 ]; then
			A2PSOPTIONS="$A2PSOPTIONS --landscape"
		else
			A2PSOPTIONS="$A2PSOPTIONS --portrait"
		fi
		if [ $A2PSHEADERS -eq 0 ]; then
			A2PSOPTIONS="$A2PSOPTIONS --no-header"
		else
			if [ $JOB ]; then
				if [ $A2PSCOMMAND = "a2ps" ] ; then
				        A2PSOPTIONS="$A2PSOPTIONS --center-title=\"$JOB\""
				else
					A2PSOPTIONS="$A2PSOPTIONS --title=\"$JOB\""
				fi
			fi
		fi
		if [ $A2PSCOMMAND = "a2ps" ] ; then
			A2PSOPTIONS="$A2PSOPTIONS --borders=$A2PSBORDERS"
		else
			if [ $A2PSBORDERS -eq 1 ] ; then
				A2PSOPTIONS="$A2PSOPTIONS --borders"
			fi
		fi
		A2PSOPTIONS="$A2PSOPTIONS --columns=$A2PSCOLUMNS"
	fi
	INPUTFILTER="$A2PSCOMMAND $A2PSOPTIONS | "
	TPCOLORMODE=0
	GSDRIVER="pbmraw"
	GSINPUT="ps";
    };;

 *)
    ########
    # all other data, print error message
    ########
    #
    {
    	#
        INPUTFILTER="echo -e \"Sorry, this file format is not (yet) supported:\n$FILETYPE\nAdd your own handler to $TPPATH_FILTERS/tpfilter\" | pbmtext | ";
    };;

esac

if [ $TPFILTERDEBUG ]; then
	echo "INPUTFILTER=$INPUTFILTER" >> $LOGFILE
fi
#
########
# end of input filter selection
########

########
# select output filter for printing to a remote printer
########
#
case $REMOTEOUTPUT in
 device)
    {
	# no output filter needed
	OUTPUTFILTER="";
    };;
 file)
    {
	# to be implemented
	OUTPUTFILTER=" | cat > $PRINTTOFILE";
    };;
 remote)
    {
	# lpr queue on other unix machine
	# the "old" bsd lpr doesn't filter remote output, so
	# output is redirected to a second queue - the remote queue
	LPROPTIONS="-P${CONFIGNAME}remote"
	# setting the user doesn't work with LPRng
	#if [ $LOGIN ]; then
	#	LPROPTIONS="$LPROPTIONS -U '$LOGIN'"
	#fi
	OUTPUTFILTER=" | lpr $LPROPTIONS";
    };;
 smb)
    {
	# print to a Windows machine via SAMBA
	# handled by the smbprint script
	# configfile with server name, printer name, etc.
	# is passed on to this script
	OUTPUTFILTER=" | $SMBPRINT $CONFIGFILE";
    };;
 socket)
    {
	# print to a network printer or server box port
	# handled by the socketprint script
	# configfile with server name, port number
	# is passed on to this script
	OUTPUTFILTER=" | $SOCKETPRINT $CONFIGFILE";
    };;
 ncp)
    {
	# *** NOT YET TESTED ***
	# print to a Netware server via SAMBA
	# handled by the ncpprint script
	# configfile with server name, printer name, etc.
	# is passed on to this script
	OUTPUTFILTER=" | $NCPPRINT $CONFIGFILE";
    };;
esac

if [ $TPFILTERDEBUG ]; then
	echo "REMOTEOUTPUT=$REMOTEOUTPUT" >> $LOGFILE
	echo "OUTPUTFILTER=$OUTPUTFILTER" >> $LOGFILE
fi
#
########
# end of output filter selection
########

########
# build command line for ghostscript
# -sDEVICE= name of output format
# -r resolution
# -g output image size in pixels
# -dSAFER interpret postscript in read-only mode
# -dNOPAUSE don't wait after page end
# -dBATCH don't wait after file end
# -sOutputFile=- write to stdout
# -q don't write debug output to stdout
# - instead of input file: read from stdin
########
#
GSCOMMANDLINE="gs -sDEVICE=$GSDRIVER -r${GSXDPI}x$GSYDPI -g${GSWIDTH}x$GSHEIGHT\
 -dSAFER -dNOPAUSE -dBATCH "
if [ $GSBUFFER -gt 0 ]; then
	GSCOMMANDLINE="$GSCOMMANDLINE -dBufferSpace=$GSBUFFER"
fi
if [ $GSSCREENLPI -gt 0 ]; then
	GSCOMMANDLINE="$GSCOMMANDLINE  -dDITHERPPI=$GSSCREENLPI"
fi

# swap odd pages upside-down in duplex mode
#if [ $TPDUPLEX -eq 2 ]; then
#       GSCOMMANDLINE="pstops -q '2:0,1U@1(1w,1h)' | $GSCOMMANDLINE"
#fi

########
# build command line for tpprint
########
#
TPCOLORSETTINGS="t${TPTRUEMATCH}x${TPMIRROR}"
if [ $TPUSERCOLOR -gt 0 ]; then
	TPCOLORSETTINGS="${TPCOLORSETTINGS}b${TPBRIGHTNESS}o$TPCONTRAST"
	TPCOLORSETTINGS="${TPCOLORSETTINGS}i${TPINKVOLUME}s$TPCOLORSATURATION"
	TPCOLORSETTINGS="${TPCOLORSETTINGS}g$TPSCREENGAMMA"
	TPCOLORSETTINGS="${TPCOLORSETTINGS}y${TPIYELLOW}m$TPIMAGENTA"
	TPCOLORSETTINGS="${TPCOLORSETTINGS}c${TPICYAN}k$TPIBLACK"
fi

if [ $GSINPUT ] ; then
  # command line for turboprint (called from ghostscript)
  # -s width x height in 1/1000 inches
  # -p left x top position in 1/1000 inches
  # -a0 ignore original image aspect
  # -e1 ignore unprintable page margins
  # -v2 print all debug messages, -v0 none
  # -d driver name
  # -c cartridge id number
  # -q quality id number
  # -m paper ("medium") id number
  # -g color mode (0=bw, 1=grey, 2=color)
  # -f page size id number
  # -i input feed id number
  # -o output tray id number
  # -u duplex mode id number
  # -t dithering mode id number
  # - instead of input&output file: use stdin/stdout
  if [ $GSPPDFOUND ] ; then
   TPCOMMANDLINE="tpprint -s${TPWIDTH}x$TPHEIGHT -p${GSXOFFSET}x$GSYOFFSET -a0 -e1 -v2\
 -d$TPDRIVER -c$TPCARTRIDGE -q$TPQUALITY -m$TPPAPER -g$TPCOLORMODE\
 -f$TPPAGESIZE -i$TPFEED -o$TPTRAY -u$TPDUPLEX -t$TPDITHERING\
 -y$TPCOPIES -b$TPCOLORSETTINGS -l$LOGFILE --psheader=$CHUNKFILE - -"
  else
   TPCOMMANDLINE="tpprint -s${TPWIDTH}x$TPHEIGHT -p${TPLEFT}x$TPTOP -a0 -e1 -v2\
 -d$TPDRIVER -c$TPCARTRIDGE -q$TPQUALITY -m$TPPAPER -g$TPCOLORMODE\
 -f$TPPAGESIZE -i$TPFEED -o$TPTRAY -u$TPDUPLEX -t$TPDITHERING\
 -y$TPCOPIES -b$TPCOLORSETTINGS -l$LOGFILE - -"
  fi

else
  # command line for turboprint image printing (called directly)
  # -s width x height in 1/1000 inches
  # -v2 print all debug messages, v0 none
  # -p left x top position in 1/1000 inches
  # -a keep original image aspect ratio
  # -e ignore unprintable margins
  # -d driver name
  # -c cartridge id number
  # -q quality id number
  # -m paper ("medium") id number
  # -g color mode (0=bw, 1=grey, 2=color)
  # -f page size id number
  # -i input feed id number
  # -o output tray id number
  # -u duplex mode id number
  # -t dithering mode id number
  # - instead of input&output file: use stdin/stdout
  if [ $TPPICCENTERX -eq 1 ] ; then
	TPPICLEFT="c"
  fi
  if [ $TPPICCENTERY -eq 1 ] ; then
	TPPICTOP="c"
  fi
  TPCOMMANDLINE="tpprint -s${TPPICWIDTH}x$TPPICHEIGHT -v2\
 -p${TPPICLEFT}x$TPPICTOP -a$TPPICASPECT -e$TPPICNOMARGINS\
 -d$TPDRIVER -c$TPCARTRIDGE -q$TPQUALITY -m$TPPAPER -g$TPCOLORMODE\
 -f$TPPAGESIZE -i$TPFEED -o$TPTRAY -u$TPDUPLEX -t$TPDITHERING\
 -y$TPCOPIES -b$TPCOLORSETTINGS -l$LOGFILE - -"
fi


########
# THIS IS A DEBUG HACK FOR tpfilter
########
# uncomment the next command if you want tpfilter's output
#OUTPUTFILTER=" | cat >/tmp/turboprint.debug"
# uncomment the next command if you want to bypass tpprint
#TPCOMMANDLINE="cat"
########

TPPIPE="$TPCOMMANDLINE $OUTPUTFILTER"

if [ "$FILETYPE" = "tpraw" ] ; then
	COMPLETEPIPE="$TPSTDIN --paste $CHUNKFILE $OUTPUTFILTER"
else
	# put together first chunk and remainder of stdin; uncompress if necessary
	INPUTFILTER="$TPSTDIN --paste $CHUNKFILE | $DECOMPRESS $INPUTFILTER"
        # do some logging
	if [ $TPFILTERDEBUG ]; then
		echo "GSCOMMANDLINE=$GSCOMMANDLINE" >> $LOGFILE
		echo "TPCOMMANDLINE=$TPCOMMANDLINE" >> $LOGFILE
        fi
	case $GSINPUT in
	  ps)
	    # gs reads postscript format from stdin through a pipe
	    {
		COMPLETEPIPE="$INPUTFILTER $GSCOMMANDLINE -sOutputFile=>($TPPIPE) - >> $LOGFILE";
	    };;
	  pdf)
	    # pdf input format MUST be seekable - we use a temp file
	    {
		COMPLETEPIPE="$INPUTFILTER cat - >$TEMPFILE | $GSCOMMANDLINE -sOutputFile=>($TPPIPE) $TEMPFILE >> $LOGFILE";
	    };;
	  *)
	    # other formats (pbm, pgm, ppm) are processed directly by tpprint
	    {
		COMPLETEPIPE="$INPUTFILTER $TPPIPE";
	    };;
        esac
fi

echo "----------- Start of print job -----------" >> $LOGFILE
# now execute ghostscript & other pipe commands
echo "$COMPLETEPIPE" >> $LOGFILE
eval $COMPLETEPIPE
echo "------------ End of print job ------------" >> $LOGFILE

########
# wrap around log file
########
$TPSTDIN --cutlogfile $LOGFILE 128

########
# remove temp files
########
rm $CHUNKFILE >> $LOGFILE
if [ -e "$TEMPFILE" ] ; then
	rm $TEMPFILE >> $LOGFILE
fi

exit 0