*BSD News Article 98622


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!munnari.OZ.AU!uunet!in2.uu.net!204.178.176.27!hunter.premier.net!hammer.uoregon.edu!csulb.edu!gatech!howland.erols.net!newsfeed.internetmci.com!news.cmc.net!sloth.swcp.com!not-for-mail
From: crs@quail.swcp.com (Charlie Sorsby)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Another Printer Filter question
Date: 27 Jun 1997 11:50:53 -0600
Organization: Just me, Los Alamos, NM
Lines: 182
Message-ID: <5p0uht$3cm@quail.swcp.com>
Reply-To: crs@swcp.com
NNTP-Posting-Host: lappp06.swcp.com
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.freebsd.misc:43643

First, I am only sporadically able to keep up with the news group
so I apologize if this has already been beaten to death and I
simply missed it.  Are there any good print filters and/or scripts
to run lpf out there?  I've created a script starting from the
fragments that are listed in the FreeBSD handbook but I'm afraid it
falls into the category or a fine watch manufactured by a hatchet
mechanic...  :(

So you'll know what I'm using now:  I'm running FreeBSD 2.1.5.  My
printer is an HP DeskJet 600C--only because it was *cheap* at
WalMart.

If what I have so far is of any use to anyone who hasn't been
beating on this as long as I have, please feel free to use
it--with, of course, the understanding that it comes strictly "as
is" and "with no warranty whatever."  :)

But I post it mainly to show where I am now so that if someone has
a better approach, perhaps they will share it.

Here's my /etc/printcap:

#	@(#)printcap	5.3 (Berkeley) 6/30/90

#lp|local line printer:\
#	:lp=/dev/lpt0:sd=/var/spool/output/lpd:lf=/var/log/lpd-errs:

# NOTE:  Changing to ":pl#65" resulted in a spurious page eject after
# each page save the last when printing a multi-page document.
# Apparently, 64 is the max number of lines usable.

lp|local printer|deskjet|HP DeskJet 600C:\
	:sh:lp=/dev/lpt0:\
	:pl#64:pw#80:\
	:px#2400:py#2900:\
	:sd=/var/spool/lpd/deskjet:\
	:lf=/var/log/lpd-errs:\
	:if=/usr/local/libexec/hpif:
#	:if=/usr/libexec/lpr/lpf:

As you can see from the line that I've commented out, I tried using
/usr/libexec/lpr/lpf directly but that doesn't detect PostScript,
much less use gs(1) to process it.  Thus the following, most of
which is comments to help me keep straight about what I've tried:

Here's /usr/local/libexec/hpif:

#!/bin/sh
#
#			Revision record:
#
# hpif	Simple text input filter for lpd and HP-PCL printers.  My
# printer is an HP DeskJet 600C.  I started with IF fragments from
# the FreeBSD Handboo and evolved it from there.  I neglected to
# sign and date some of the early revision history.
#
#				Charlie Sorsby
#					crs@swcp.com
#					Los Alamos, NM  87544
#
# Installed in /usr/local/libexec/hpif.
#
# 1:
# Simply copies stdin to stdout.  Ignores all filter arguments.
# Tells printer to treat LF as CR+LF.  Writes a form-feed character
# after printing job.
#
# 2:
# Try to fix so that it can handle either text files or PostScript.
# This is a variation of the filter described for "Simulating PostScript
# on Non-PostScript Printers" in the FreeBSD Handbook.
#
# I neglected to initial and date the foregoing.  I very likely
# also tried some things that I didn't document at all and simply
# removed before going on.
#
# Here's what I began with before the current revisions:
#
# echo $first_line && /usr/libexec/lpr/lpf && exit 0
#
# I found a crude way to pass filter arguments to /usr/libexec/lpr/lpf
# so that at least some work.  I was reminded that I was not
# passing filter arguments (see comment above) to lpf when I tried
# using the indent option to lpr.  I first tried hard coding "-i5"
# to the call to the filter, /usr/libexec/lpr/lpf to verify
# that it was able to process it and it is.  Then I tried sending "$*"
# to lpf:
#
# echo $first_line && /usr/libexec/lpr/lpf $* && exit 0
#
# but, as I had suspected, things like the indent request were not
# applied to the first line.
#
# So, hoping that /usr/libexec/lpr/lpf would read standard input
# properly and that lpr would pass the flags as well as the file
# name to it, I tried:
#
# (echo $first_line && cat) | /usr/libexec/lpr/lpf $* && exit 0
#
# and, so far, it seems to work.  Leaving "$*" as an argument to
# lpf was inadvertant.  I simply forgot to delete it.  It's just as
# well--it doesn't work properly without it and I would probably
# not have thought to combine the stdin approach and the "$*"
# approach.  My guess as to what happens is this:
#
#    lpr sends the (appropriate) option flags to my filter
#    (this file) as arguments.  The "$*" argument passes them
#    on to lpf.  By sending "(echo $first_line && cat)" to
#    lpf's stdin, they are recombined so that lpf treats the
#    whole thing the same.  I had never thought of it but I
#    guess that's the way all Unix commands work.  I don't
#    believe, for example, that one can send options to the 
#    standard input of, say, ls.
#
# That's a very crude, retro-guess, at what's happening based wholly
# on observation.  Many questions remain unanswered.
#
# I wonder if I can get away with putting this in $HOME/lib and
# creating a symbolic link to /usr/local/libexec?  Then, I'll work
# on it there and also I won't have to remember to save it from
# /usr/local when I upgrade--although I will have to remember to
# recreate the symbolic link.  Maybe when the printer doesn't work
# after an OS upgrade, it will remind me.
#
# The symbolic link seems to work OK.
#
#					crs -- 27 June, 1997

#				The filter:

# Treat LF as CR+LF
#
printf "\033&k2G" || exit 2

# Read first two characters of the file to see if it is a
# PostScript file:
#
read first_line
first_two_chars=`expr "$first_line" : '\(..\)'`

# Some of the ghostscript DEVICE options I tried:
#  	-q -sDEVICE=djet500 -sOutputFile=- - \
#  	-q -sDEVICE=cdj550 -sOutputFile=- - \
#  	-q -sDEVICE=cdjcolor -sOutputFile=- - \
#  	-q -sDEVICE=cdjmono -sOutputFile=- - \
# See corresponding part of the gs command below:

if [ "$first_two_chars" = "%!" ]; then

    #PostScript job; use GhostScript to convert it:
    #
    /usr/local/bin/gs -dSAFER -dNOPAUSE -dNOPLATFONTS \
  	-q -sDEVICE=cdj550 -sOutputFile=- - \
 	&& exit 0
else
    # Plain text; just print it:
    #
    # This works--except for the first line, which is discarded:
    # /usr/libexec/lpr/lpf && exit 0
    # it is necessary to recombine the first line with the rest of
    # the file before sending to lpf.
    #
    # I don't remember this try at all.  I think I lost nlpf when I
    # had to reinstall the system after a sort of a brown out.
    # echo $first_line && /usr/local/libexec/nlpf $* && exit 0
    # /usr/local/libexec/nlpf $* && exit 0
    #
    # lpf arguments are not applied to first line:
    # echo $first_line && /usr/libexec/lpr/lpf $* && exit 0
    #
    # I think this may work:
    (echo $first_line && cat) | /usr/libexec/lpr/lpf $* && exit 0
fi
exit 2



-- 
Best regards,

Charlie "Older than dirt" Sorsby      Los Alamos, NM     "I'm the NRA!"
       crs@swcp.com www.swcp.com/~crs		     Life Member since 1965