The
ifhp
filter operates by first reading a configuration file
to determine the type of printer it is working with,
and then proceeds to carry out operations requested by the values of
option variables passed on the command line or found in the configuration
files.
In normal operation,
input is read from
STDIN
(file descriptor 0)
and results written to
STDOUT (file descriptor 1).
Status reports are written to
a status file or optionally to
STDERR (file descriptor 2),
together with any error messages or diagnostics.
In addition to normal operation
the filter can run in the
OF mode and act as a printer initializer and job terminator.
This is discussed in detail in the LPRng documentation.
When in the OF mode
the two character sequence "\031\001"
will cause the filter to suspend itself by sending itself a SIGSUSP
signal.
The print spooler will detect this and then
send job files to the same output device.
After the files have been transferred the
the filter will be restarted with a SIGCONT signal.
The details of the filter operations are
described in the following
pseudo-code.
The sections marked with ### are discussed later in this document
in detail.
/// See:
Options, Initialization and Setup
###+++ Initialization and Setup
// get ifhp information from PRINTCAP_ENTRY environment variable
if( PRINTCAP_ENTRY environment variable has a value ){
split printcap information into printcap fields
if( :ifhp=options,options is present in printcap ){
split the options list and place in the Toptions list
}
}
Add the -T command line options to the Toptions list
Add the -Z command line options to the Zoptions list
foreach option in -Toptions do
if( option = "debug=level" ){
set Debuglevel = level;
}
if( option = "trace" ){
output error and trace on STDERR
}
if( option = "config=pathlist" ){
set configuration pathlist = pathlist;
}
if( option = "model=name" and model not set ){
set model = name;
}
}
Read the configuration files from the config file list
Prepend each file with a [ default ] header
Scan the configuration files for [ default ] entries;
later entry values will override earlier ones.
Repeat the scan, but this time search for [ model ] entries
matching the specified model.
Put the command line options and -T options into configuration
information, effectively overriding the information from the
configuration files.
// open a connection to the printer if required
// usually only done when appsocket protocol is used
if( device specified using -Tdev=device ){
// if device is host%port, we open TCP/IP connection
fd = open(device);
// Note - option read_write will open the device or file read write
dup fd to 1; close fd;
}
###---
/// See:
Synchronization and Pagecount
###+++ Synchronization and Pagecount
if( status returned by printer and sync requested ){
do{
send command and wait for timeout;
} while( no response );
if( appsocket ){
close and reopen TCP/IP connection;
}
}
if( status and pagecount requested ){
// pagecount has the form pagecount@ (none),
// pagecount=ps, pagecount=pjl, ...
if( pagecount=language has value ) do {
if( pagecount TRUE ){
set pagecount= pjl or ps depending on availability
}
if( pagecount = pjl and PJL INFO available ){
send PJL INFO PAGECOUNT command to printer
} else if( pagecount = ps ){
send PS program to printer
} else {
terminate with error;
}
} while( no pagecount response );
if( appsocket ){
close and reopen TCP/IP connection;
}
}
###---
/// See:
PJL Initialization
### PJL INITIALIZATION
if( PJL enabled ){
language = "pjl_"
foreach option in pjl_init=[...] {
expand the option using the language value
#+++ PJL OPTION ACTIONS +++
if( option in pjl_vars_set=[ ... ]
and option not in pjl_vars_except
expand "@PJL SET OPTION=\%{option}"
output = expanded string value
} else {
if( option value is a string ){
output = expanded string value;
}
}
// output has the form @PJL COMMAND ....
if( COMMAND is in pjl_only=[ ... ]
and not in pjl_except=[ ... ] ){
send output to printer
}
#--- end PJL OPTION ACTIONS
}
if( !OF_mode ){
foreach option in -Toption=value {
if( option in pjl_user_opts ){
#+++ USER PJL OPTIONS
// join 'pjl_' and the option name
expand 'pjl_' . option
// perform PJL actions as above
#+++ PJL OPTION ACTIONS +++
....
#-- PJL OPTION ACTIONS +++
#--- USER PJL OPTIONS
}
}
foreach option in -Zoption=value {
if( option in pjl_user_opts ){
// perform USER PJL actions as above
#+++ USER PJL OPTIONS
#--- USER PJL OPTIONS
}
}
}
}
###--- PJL INITIALIZATION
/// See:
File Conversion Support
// language is set to the type of job language
// - PS, PCL, TEXT, RAW, UNKNOWN
// the first part of the job file is read and the filter takes
// a (wimpy) guess at the job file based only on the first couple
// of characters; language is be PJL, PS, or TEXT, or RAW
// This is the same algorithm as the UNIX FILE utility
language = default_language (from configuration);
if( command line -c (binary) option present ){
language = RAW;
} else if( -Zlanguage=xxx option present ){
language=xxx
} else if( forceconversion set ){
use UNIX file utility to get file type
} else if( file is PS file ){
language=PS
if( file starts with PS EOJ (CTRL-D)
and no_ps_eoj is set ){
remove the PS EOJ
} else {
send a PS EOJ first
}
} else if( file is PCL file ){
language=PCL
if( file starts with PCL EOJ (ESC E)
and no_pcl_eoj is set ){
remove the PCL EOJ
}
}
if( file conversion table specified then ){
look up file type in conversion table;
if( conversion program specified ){
run input through conversion program
}
set file type to output type
}
if( language = TEXT and PCL allowed ){
language = PCL;
}
if( language not recognized by printer ){
exit with error;
}
if( PJL ENTER supported ){
use PJL ENTER command to select language;
send nullpad NULLS to force full buffer condition
}
/// See:
Language Specific Initialization
// LANGUAGE SPECIFIC INITIALIZATIONS
if( language = PCL ){
foreach option in pcl_init {
###+++ expansion
do expansion similar to PJL OPTION actions
using "pcl_" prefix for option lookup;
###---
}
if( not in OF_MODE ){
foreach option in -Toption do {
if( option in pcl_user_vars=[ ... ] ){
###+++ expansion as above
###---
}
foreach option in -Zoption do {
if( option in pcl_user_opts=[ ... ] ){
###+++ expansion as above
###---
}
}
remove whitespace and expand string results;
} else if( language = PS ){
###+++ language specific actions as above,
using the ps_ prefix for lookup
allow only user option in the ps_user_opts list
expand string results but do not remove whitespace
}
/// See:
File Transfer and Error Status Monitoring
Transfer job to printer, reading error and other information
back from the printer if enabled
if( language = PCL ){
send PCL End of Job
} else if( language = PS ){
send PS End of Job
}
// job termination
###+++ Synchronization and Pagecount as above
finished = 0
while( waitend and not finished ){
// timeouts and retries are done here
if( time taken is too long ){
give up and report an error
}
if( appsocket ){
send \r\n to get status reply
} else if( waitend with PJL ){
wait for end of job using UINFO;
} else if( waitend with PS ){
send PostScript echo program to printer
if end_ctrl_t then add ^T
}
wait for response
if( response has end of job indication ) {
finished = 1;
}
}
if( pagecount ){
if( appsocket ){
close and reopen connection;
}
get pagecount using previously described algorithm
}
###---
exit
During the setup step,
the ifhp system will extract command line options
and scan configuration files for printer entries.
These operations are covered in detail in other sections.
These flags set the languages that are recognized or processed by the filter.
If PJL is enabled and this flag is SET, a PJL JOB and PJL EOJ command will be generated and sent to the printer. The JOB command has the form:
@PJL JOB NAME = "..." [ START = nnn ] [ END = mmm ]
The START and END values can be specified by
-Zstartpage=nnn
and
-Zendpage=mmm
command line options.
The EOJ command has must match the JOB command.
@PJL EOJ NAME = "..." [ START = nnn ] [ END = nnn ]
If PJL is enabled and this flag is SET, a PJL ENTER LANGUAGE = xx command will be generated when PCL or PS files are sent to the printer.
@PJL ENTER LANGUAGE = PCL
@PJL ENTER LANGUAGE = POSTSCRIPT
Some older model HP printers required sending a large number of
NULL (0) characters to force commands in the input buffer to be read.
This can be done using the
nullpad option.
In practice, this has turned out to be largely historical, as most printers do not have this problem.
When this flag is set,
PJL is available,
and the PJL
RDYMSG
command is supported,
then a short message will be put on the console.
The
remove_ctrl string option species a list of (control) characters
that will be removed from PostScript jobs.
This solves the problem of jobs with embedded Control-T or Control-C
characters causing abnormal printer operation.
For example:
remove_ctrl=CT
would cause Control-C and Control-T characters to be removed.
The
tbcp
flag can be specified as a user option as well as a
configuration file option.
If the file type is PostScript and this flag is set,
then the file is transferred using the Transparent Binary Communication
Protocol.
(See the Adobe PostScript Language Reference Manual for details on
the protocol.)
At the start of the PostScript job,
the sequence \001 M is sent.
Afterwards, all control characters in the set
0x01, 0x03, 0x04, 0x05,
0x11, 0x13, 0x14, 0x1C,
are replaced by the two character sequence \001
X+'@' or
X+'\100' or
is sent.
For example:
C\001\003 -> \001\115\103\001\101\001\103 or \001MC\001A\001C
Many printers are able to provide status information back to the
filter.
It is assumed that in these circumstances file descriptor 1
(FD1)
is
bidirectional
and status information can be read from it.
When the
status
option is TRUE,
then the filter assumes that it can read FD1.
In order to simplify configuration,
the
ifhp
filter will test FD1, and if it is not
a serial port or a network socket, will set
status@
or OFF.
Synchronization is usually done in order to ensure that a previously spooled job or printer action has completed correctly, and the printer is ready to accept a new job. It is usually carried out by sending a request to the printer to echo a string back to the filter. Clearly, if the printer cannot provide status or echo values back, then synchronization is impossible.
The value of the
sync option determines if a PJL ECHO command or simple PostScript
program is used.
The PostScript program has the form:
\004%!PS-Adobe-2.0
( %%[ echo: TODSTR ]%% ) print () = flush
\004
where TODSTR is replaced with the current Time of Day.
To control obtaining synchronization,
the
and
sync_timeout=nnn
options are used.
The PJL or PS command is repeated at
sync_interval=nnn
second intervals; if nnn is 0, then it is sent only once.
If synchronization is not obtained within
sync_timeout=nnn
seconds, then the filter exits with an error status.
A 0 value or
sync_timeout@
disables timeouts.
Pagecounts are used to do accounting and report the number of pages used for a job. Most printer have a hardware based pagecounter mechanism whose value can be read by the appropriate PJL command or PostScript program. For example, if the PJL INFO command
@PJL INFO PAGECOUNT
is supported by a printer,
the printer will return a status message containing the current pagecounter
value.
Printers that support PostScript may also be able to access the pagecounter
value using a PostScript program.
The exact details of the PostScript program vary from vendor to vendor and
the
pagecount_ps_code=...
option specifies the PostScript program to use.
For example:
pagecount_ps_code=
/p {print} def ( %%[ pagecount: ) p
statusdict begin pagecount end 20 string cvs p
( ]%% ) p () = flush
Pagecounting is supported by the
pagecount=language,
pagecount_interval=nnn,
and
pagecount_timeout=nnn
options.
The
pagecount=language
option enables pagecounting, and sets the language to be used.
Currently
ps (PostScript)
and
pjl (PJL)
are supported.
The pagecount request is repeated every
pagecount_interval=nnn
second intervals; if nnn is 0, then it is sent only once.
If no pagecount value is obtained within
pagecount_timeout=nnn
seconds, then the filter exits with an error.
A 0 value or
sync_timeout@
disables timeouts.
Some printers do not correctly report end of job and must be polled until the pagecount information stabilizes. The PJL TEOJ (True End Of Job) PJL has been tried with limited success on various printers to force End of Job reporting only when the job has finished.
pjl_init=[ ... teoj ... ]
pjl_teoj=@PJL TEOJ=ON
If a printer supports PJL, the many printer operations can be initiated and controlled using PJL commands. Unfortunately, not all printers support the same set of commands. In addition, not all printers support the same set of operations or options. A PJL command has the form:
@PJL COMMAND OPTION OPTION ...
A PJL variable is set using:
@PJL SET var = value ...
The
pjl_only=[ ... ],
pjl_except=[ ... ],
pjl_vars_set=[ ... ],
and
pjl_vars_except=[ ... ]
options are used to control which PJL commands and which PJL variables
can be set.
The
pjl_only
variable lists the commands supported by the printer,
and the
pjl_except
lists commands not
supported by the printer.
Before sending a PJL command,
the
ifhp
filter checks to make sure that the command name is in
pjl_only
and not in
pjl_except.
If the tests fail, then tne command is not sent.
Similarly,
when sending a command to set a PJL variable,
the
pjl_vars_set
and
pjl_vars_except
lists are checked to determine if the variable name is in
pjl_vars_set
and not in
pjl_except
list.
If the tests fail, then tne command is not sent.
If PJL is enabled, then the following actions are taken.
\033%-12345X is sent to the printer.
This is required to ensure that the following PJL commands are accepted.
-Zstartpage=nnn
or
-Zendpage=mmm
option is present, then the PJL JOB command has the form:
@PJL JOB START=nnn END=mmm
pjl_init=[ ... ]
value option is expanded using the PJL
("pjl_") language context as described above.-Toption=values
and
-Zoption=values
are scanned for matching option names in the
pjl_user_opts=[ ... ]
list.
If they are found,
then the options are recursively evaluated in the PJL language context.
The expansion algorithm will cause the option value to be used to set PJL
variables.
For example:
Configuration:
pjl_vars_set=[ OUTBIN AUTOSELECT JAM=YES ]
Command
ifhp -Zoutbin=upper,autoselect,jam
PJL command generated:
@PJL SET OUTBIN=UPPER
@PJL SET AUTOSELECT=ON
@PJL SET JAM=YES
The lpr -l or lp -b flags indicate that the spooled files
are not to be processed by an output file.
The LPRng spooler recognizes this option and passes the -c
command line option to suppress any language specific processing for files.
However, many PostScript printers cannot handle text files, and produce many hundreds of pages of garbage output if they are sent to the printer without being translated into PostScript, and some printers require language specific setup in order to print PCL, PostScript or text files correctly.
The ifhp filter has builtin tests for PJL,
PCL, and PostScript files.
These tests are almost identical to those used
by many printers which do
autodetection.
If you need to recognize a wider range of file types,
you can configure ifhp to use the
UNIX file(1) program.
Finally,
some printers have a very specialized job format that requires
conversion to by a
rasterizer
program.
This is handled as detailed in the following sections.
By default,
the builtin tests are first used to determine the file type.
These tests are very successful in determining if a file is
PJL, PostScript,
and PCL, and text
with language types
pjl,
ps,
pcl,
and
text respectively.
If it cannot determine the language type, it is set to the
default_language value, usually text.
If this simple method is not satisfactory or is too limited,
then setting the
forceconversion
option will cause ifhp to use the UNIX
file utility
utility to determine the file and produce a text description on
its STDOUT.
For example:
## default
default_language=text
## force use of file program
force_conversion
## file utility path
file_util_path=/usr/bin/file -
The output of the file utility is converted to lower case and used as the language type for further processing.
Once the language type has been determined,
the ifhp filter then decides if a conversion program
needs to be run and will convert the input file to a required file type.
This activity is controlled by the file_output_match
table.
file_output_match = [
*postscript* ps \%s{ps_converter}
*pcl* pcl \%s{pcl_converter}
*pjl* pjl \%s{pjl_converter}
*printer*job*language* pjl
# if your printer has PCL, use:
*text* pcl \%s{pcl_converter}
# if your printer does not have PCL, use:
# *text* pcl \%s{text_converter}
]
Each line of the
file_output_match
table contains
a (URL encoded) glob pattern,
the language type (ps, pcl, etc.) produced by the conversion program,
and the (optional) conversion program.
The format:
file_output_match = </pathname
will cause ifhp to open and read the specified file
for the file_output_match table.
The file's contents must have the same format as the
file_output_match table but without the [ or ] delimiters.
The file_match_table is scanned from first to last entry
for a glob pattern that
matches the file type determined by the ifhp program or the output of the
file_util_path program.
If no match is found,
then the language is set to the
default_language value.
The output language of the conversion program is set to the second entry. The conversion program will be run with its STDIN set to the input file and its STDOUT used as the converted output. If there is no conversion program then the original file is used and only the language type is modified. The following shows how you can use the
# device=epsonc
# resolution=-r1440
# gs=/usr/bin/gs
# ps_converter= \%s{gs} -dBATCH -q -sDEVICE=\%s{device} \
# \%s{resolution} -sOutputFile=- -
ps_converter=
pcl_converter=
pjl_converter=
text_converter=/usr/bin/a2ps -q -B -1 -M Letter
--borders=no -o- \%s{ps_converter}
text_converter_output=ps
There are a few details that should be observed when using this facility.
file_output_match@ is specified
then the file_output_match table will not be used.'*%20gif%20*' is used as '* gif *'.|,
;,
>,
<,
etc,
then it will be executed using
/bin/sh -c 'command'.
This allows a pipe of conversion commands to be constructed.
This is discussed in detail below.unknown or not supported by the printer then the file is not printed. wrapper program with these utilities is discussed below.ifhp filter.The conversion program must exit with a 0 (success) error code.
Unfortunately,
the enscript program
will exit with a non-zero error codes even for successful conversions.
We need to use a
wrapper script that will run them and then return the correct
error code as shown below.
#!/bin/sh
# /usr.../wrapper path [options]
# wrapper script for a2ps, enscript and others
# path is the path to the program and options are the
# options to pass. The program is run and then the exit
# code is corrected
"$@"
status=$?
case "$status" in
1 ) exit $status ;;
esac
exit 0
If the conversion program specification contains one or more of the
shell meta-characters
|, <, or <, etc,
then it will be invoked using the /bin/sh -c filterprogram.
This allows users to specify some rather bizarre conversion
programs and to chain together conversion programs.
For example,
You can print a gif file by first converging it ppm and then
to PostScript:
then to a
# gif to ppm
*ppm* ps ppm2ps
*gif* ps gif2ppm | ppm2ps
A more important use of this feature is when there is a need to rasterize a file by converting it from a standard format to a specialized printer raster format. You can set this up by using the following method.
rasterizer= /usr/bin/gs -sDEVICE=epson -dBATCH \
-sOutputFile=- -sPAPERSIZE=letter -q -
text_converter=/usr/bin/a2ps -q -B -1 -M Letter
--borders=no -o-
file_output_match = [
*postscript* ps \%s{rasterizer}
*text* ps \%s{text_converter} | \%s{rasterizer}
* unknown
]
The rasterizer option in the configuration program
is expanded to produce the necessary pipeline to do conversion.
When processing text or pcl files,
the crlf option will enable translation of
LF (\n) to CR/LF (\r\n) sequences.
If you are using ifhp to simply do LF to CR/LF translation,
then you can use:
ifhp -Tcrlf
Text is simply PCL with no special formatting codes.
However, you will still need to send the PCL initialization
strings to the printer.
You can do this by using the following entry in the
file_output_match table:
file_output_match = [
*text* pcl
]
Your printer may be capable of handling a wide variety of job
formats. If you want to simply pass through files of
unknown type or language then use the following entry in the
file_output_match table:
file_output_match = [
* raw
]
Generating a raster image from a PostScript or PCL file in a timely manner requires a high speed processor and substantial amounts of memory. Many of the low cost printers require the user's system to do the raster conversion, and a raster file is then transferred to the printer. These files are usually in a proprietary format.
The
GhostScript
program can process PostScript files and produce raster output for a
wide range of devices.
See the GhostScript documentation for details.
The ghostscript printer configuration is intended to be used
with these printers.
# PRINTER ghostscript - Printer with GhostScript conversion to raster files
[ ghostscript gs ]
pcl@
pjl@
ps
text
# device=epsonc
# resolution=-r240x72
ps_converter= [ /usr/bin/gs -dSAFER -dBATCH -q -sDEVICE=\%s{device}
\%s{resolution} -sOutputFile=- -
]
text_converter= [ /usr/bin/a2ps -q -B -1 -M Letter --borders=no -o-
| \%s{ps_converter}
]
file_output_match = [
# PostScript to Raster
*postscript* raw \%s{ps_converter}
# text to PostScript to Raster conversion
*text* raw \%s{text_converter}
]
The \%s{device} and \%s{resolution} parameters can now be
specified in the printcap.
The following shows a typical printcap entry for use with this entry.
# force clients (lpr, lpq, to use server)
lp:lp=lp@serverhost
# server information
lp:server
:sd=spooldir
:lp=/dev/lpt0
:...
:ifhp=model=gs,device=epson,resolution=-r240x72
#path to ifhp filter
:if=/.../ifhp
The ifhp configuration entry uses GhostScript to do the
rasterization of the PostScript file,
and the a2ps program to do a text to PostScript conversion.
After determining the output file language type,
language specific operations are then carried out by
expanding the
language__init=[ ... ]
options in the language context,
and then the options in the
-Toption=value
and
-Zoption=value
command line options.
The
-T
options are expanded before the
-Z,
allowing the
-Z actions to override any set by the
-T actions.
As mentioned elsewhere, the reason for the language specific processing is to allow different actions for the same command line option, depending on the file type that is being processed. For example, when processing a PCL file it might be necessary to send PCL command strings and when processing a PostScript file, you would need to send PostScript commands.
If the printer can return status, i.e., the
status option is on,
the filter will read status information from the printer.
If the
logall
flag is SET,
then all error messages will be written to the status or log file.
If the printer is returning PJL status information, then this has a specific format:
@PJL UINFO DEVICE
CODE=nnnn
DISPLAY="value"
...
@PJL UINFO JOB
START
...
@PJL UINFO JOB
END
...
The
ifhp program will extract the
CODE
and job start and end flags,
and log these as appropriate.
Unfortunately,
some PJL based printers are extremely verbose in their generation of status
messages.
In order to reduce the amount of logging of redundant information,
ifhp
will only record when a device status has
changed,
rather than when it has been reported.
The
pjl_quiet_codes=[ code code code ]
value is used to suppress reporting of selected error codes.
If the error code is in the pjl_quiet_codes list, then the error status
will not be reported to the user unless the
logall
option is set.
For example:
pjl_quiet_codes=[ 10000 10001 10003 10023 10024 35078 ]
Also, there may be error codes which does not have a builtin error
message available. New messages can be added using the
pjl_error_codes
option.
Its value is a list of lines, each line consisting of an error code
followed by the corresponding error message:
pjl_error_codes=[
code=msg
code=msg
...
]
Example:
pjl_error_codes=[
10000=powersave mode
10001=Ready Online
10002=Ready Offline
10003=Warming Up
10004=Self Test
10005=Reset
]
The waitend option controls the job termination sequence.
By default,
this will do the same work as the
sync operation,
and the option takes the same set of values.
If waitend
is suppressed using waitend@,
then as soon as a job has been transferred,
the next step,
pagecount,
will be attempted.
If the print job has not finished at this point,
then erroneous page counts will be reported.
When using the appsocket protocol,
then suppressing waitend will cause no error messages from the printer to
be reported.
Some printers do not have a True End Of Job reporting capability
using PJL.
This means that the job will be reported as done,
but paper is still moving through the print engine.
If you try to get pagecounts at this point you will get the wrong value.
An alternative method is to set
waitend=ps
and
The end_ctrl_t=word:word:...
This will cause a CONTROL-T to be sent to the printer,
a PostScript convention that will cause the PostScript interpreter to return
the actual printing status.
In most printers this will be
printing or something other
than
idle or busy
as long as paper is moving in the print engine.
When status is returned,
the words in the
end_ctrl_t=word:word:...
list value are examined for a match.
If the status word is present then the end of job condition is assumed.
The waitend_interval
value controls how often the waitend operation is repeated.
This is usually set to a fairly large value,
as it is normally used only to recover from printer failures
such as users turning the printer on and off.
The waitend_ctrl_t_interval
controls how often the printer is queried for status using CTRL-T
and is usually set to a short (2 or 3 second) value.
The appsocket flag is used to specify that data transfer will
be done using the
AppSocket
protocol.
The ifhp filter will open a connection to the ip address and
port specified by the dev=host%port option
and carry out the various operations that it needs to do.
Rather than closing a connection,
ifhp will half-close it,
that is the shutdown() system call is used to
to close the connection for transmission but leave it open for receiving.
The printer will send status information until the job is completed,
and then close the connection.
If page count information is needed,
the ifhp filter will then reopen the connection and get the page count information.