| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | #****************************************************************************** |
| 3 | # File: @(#)$Id: if-pcl3,v 2.6 2001/07/07 10:05:34 Martin Rel $ |
| 4 | # Contents: Example of a pcl3-based input filter for a Berkeley spooler (lpr) |
| 5 | # driving a PCL-3 printer |
| 6 | # Call: if-pcl3 <input filter arguments> |
| 7 | # Author: Martin Lottermoser, Greifswaldstrasse 28, 38124 Braunschweig, |
| 8 | # Germany. E-mail: [email protected]. |
| 9 | # |
| 10 | #****************************************************************************** |
| 11 | # * |
| 12 | # Copyright (C) 1998, 2000, 2001 by Martin Lottermoser * |
| 13 | # All rights reserved * |
| 14 | # * |
| 15 | #****************************************************************************** |
| 16 | # |
| 17 | # This input filter has the following interesting because unusual features: |
| 18 | # - If an error occurs, the user is notified by mail. |
| 19 | # - The filter is configurable based on the spool directory and permits in |
| 20 | # particular |
| 21 | # - the configuration of a "transparent" queue and |
| 22 | # - the inclusion of a file with PostScript configuration commands. |
| 23 | # - The filter does pac(8) accounting if this is requested in the printcap file. |
| 24 | # |
| 25 | # This is not the best of all possible filters. However, all the others I've |
| 26 | # seen so far -- excepting those I've modified myself :-) -- lack several |
| 27 | # important though elementary features. Much of this is admittedly due to the |
| 28 | # inflexibility of the Berkeley spool system (compared to the AT&T spooler) |
| 29 | # but, speaking entirely subjectively, I've also often had the impression that |
| 30 | # the filters' authors are not as familiar with PCL, ghostscript or the |
| 31 | # Berkeley spooler as one should be when trying to write universally useful |
| 32 | # software. You should form your own opinion on this point and, if necessary, |
| 33 | # ruthlessly modify whatever filter you determine to use until it satisfies |
| 34 | # your requirements. |
| 35 | # |
| 36 | #****************************************************************************** |
| 37 | # |
| 38 | # Installation |
| 39 | # ============ |
| 40 | # |
| 41 | # printcap entry |
| 42 | # -------------- |
| 43 | # Read the man page printcap(5). You should set at least 'if' (absolute path |
| 44 | # name of this filter), 'lp' (printer device), 'sd' (absolute path name of the |
| 45 | # spool directory, choose it to be queue-specific), and 'sh' (suppress header, |
| 46 | # should be true). Here's an example for a queue named "draft": |
| 47 | # |
| 48 | # draft:if=/var/spool/lpd/if-pcl3:lp=/dev/lp0:sd=/var/spool/lpd/draft:sh: |
| 49 | # |
| 50 | # Other entries which are worth considering are 'af' (this must usually be a |
| 51 | # file named 'acct' in the spool directory, see pac(8)), 'lf', and 'mx'. |
| 52 | # |
| 53 | # You must create the spool directory if it does not exist. Usually, it should |
| 54 | # be owned by "daemon", group "lp", and have the file mode 770. |
| 55 | # |
| 56 | # |
| 57 | # Filter configuration |
| 58 | # -------------------- |
| 59 | # This filter looks for a file if-pcl3.cfg in the spool directory. If it exists |
| 60 | # and is readable it is sourced as a shell script. The file is intended for |
| 61 | # setting shell variables used by this filter, redefining the two accounting |
| 62 | # routines, and for modifying the environment. |
| 63 | # |
| 64 | # Here follows a description of shell variables used by this filter. Note that |
| 65 | # you can also add other command line arguments to the gs call by setting |
| 66 | # GS_OPTIONS in the environment. |
| 67 | # |
| 68 | # - GS: This is normally the path name of the ghostscript executable. But if |
| 69 | # the configuration file sets it to the empty string, this filter will not |
| 70 | # call ghostscript but simply 'cat'. This is intended for setting up a |
| 71 | # "transparent" queue which passes files through to the printer without |
| 72 | # modifications. |
| 73 | # - INIT_TRANS: This must be empty or a parameter-less format string for the |
| 74 | # 'printf' command. If $GS is empty, this string will be sent before the file |
| 75 | # to be printed. Use it for PCL commands under the assumption that the user |
| 76 | # wishes to print a text file. In particular, this is the place to set the |
| 77 | # media size to ISO A4 if that is your default paper size. My recommendation |
| 78 | # for A4 and ISO 8859-1 is: |
| 79 | # INIT_TRANS='\033&l26A\033(0N\033&a10L\033(s12H\033&l6.5D\033&l5E\033&l66F' |
| 80 | # For US Letter size, replace the "26" near the beginning by "2". |
| 81 | # Setting INIT_TRANS should have no influence on PCL files submitted to such |
| 82 | # a queue because all PCL-generating programs should start their output with |
| 83 | # the "Printer Reset" command (pcl3 does this) which resets all parameters to |
| 84 | # their defaults. |
| 85 | # - RESOLUTION: This must be empty or an acceptable argument for ghostscript's |
| 86 | # option '-r'. In the first case, pcl3's default resolution will be used |
| 87 | # unless the document overrides it. Remember that "-r" sets also |
| 88 | # "-dFIXEDRESOLUTION". |
| 89 | # - SUBDEVICE, COLOURMODEL, QUALITY: These must contain values for pcl3's |
| 90 | # options "Subdevice", "ColourModel" and "PrintQuality". |
| 91 | # - PSCONFIGFILE: If this variable contains the name of an existing and |
| 92 | # readable file it will be included in the call to ghostscript. This file is |
| 93 | # intended to contain PostScript configuration commands, for example setting |
| 94 | # the 'PageOffset' array, defining transfer functions, setting the halftone |
| 95 | # screen or configuring 'InputAttributes'. |
| 96 | # - PAGECOUNTFILE: If pcl3 has been compiled without EPRN_NO_PAGECOUNTFILE |
| 97 | # defined, it has the ability to update a page count file with the number of |
| 98 | # pages printed. If this variable is non-empty (and that is the default), |
| 99 | # page counting will be enabled using the file specified in the variable and |
| 100 | # the count will be accumulated across jobs. |
| 101 | # - USE_INTERMEDIATE_FILE: When printing via ghostscript, this variable should |
| 102 | # be "yes" or "no". It controls whether ghostscript's output is sent directly |
| 103 | # to a printer or first to a file. Using "yes" needs substantial temporary |
| 104 | # disk space but prevents wrong output in some situations and is therefore |
| 105 | # the default. |
| 106 | # |
| 107 | # The defaults for these variables can be found below before the point where |
| 108 | # the configuration file is sourced. |
| 109 | # |
| 110 | # The two accounting functions, 'acct_start' and 'acct_stop', are always called, |
| 111 | # respectively, before and after the print command (cat or gs). 'acct_start' |
| 112 | # should return a string on standard output which will be passed as an argument |
| 113 | # to the later call to 'acct_stop'. The latter function should then return on |
| 114 | # standard output a string representation of an integer or a floating point |
| 115 | # number. If accounting has been enabled by an 'af' entry in the printcap file |
| 116 | # and the print command was successful, this value will be recorded together |
| 117 | # with the job owner's user name in the accounting file for later evaluation by |
| 118 | # 'pac'. The usual interpretation is that this value represents pages printed |
| 119 | # or feet of paper consumed. |
| 120 | # |
| 121 | # The default implementation of the accounting functions counts the number of |
| 122 | # jobs submitted for a transparent queue and the number of pages printed for a |
| 123 | # pcl3-based one. If PAGECOUNTFILE is empty or page counting manifestly does |
| 124 | # not work, the filter counts jobs also in the latter case. |
| 125 | # |
| 126 | # |
| 127 | # |
| 128 | # Restrictions |
| 129 | # ============ |
| 130 | # A spool queue based on this filter processes only PostScript files correctly |
| 131 | # except when configured as a transparent queue in which case only text or PCL |
| 132 | # files should be sent. This is not a so-called "intelligent filter" which |
| 133 | # adapts its behaviour to the type of file received! |
| 134 | # |
| 135 | #****************************************************************************** |
| 136 | |
| 137 | name=`basenamescip-shell system . . basename. "$0"` |
| 138 | |
| 139 | # Reset umask if 0. Otherwise an ordinary user could modify the accounting |
| 140 | # information if this is the first time accounting is used. This does not work |
| 141 | # on systems not supporting the old octal notation but is harmless there. |
| 142 | exprscip-shell system . . expr. "x`umask`" : 'x00*$' > /dev/nullscip-shell filesystem . . `/dev/null`. && umask 002 |
| 143 | |
| 144 | # Berkeley input filters are called in the spool directory. This is used to |
| 145 | # locate configuration files and it usually identifies the spool queue. |
| 146 | spool_directory=`pwdscip-shell system . . pwd.` |
| 147 | |
| 148 | #****************************************************************************** |
| 149 | |
| 150 | # In order to notify the user of errors occurring in the backend I'm copying |
| 151 | # standard error into a temporary file and mail it to the user if it is |
| 152 | # non-empty when the filter terminates. |
| 153 | |
| 154 | errlog="${TMPDIR:-/tmp}/$$-1.tmp" |
| 155 | rmscip-shell system . . rm. -f "$errlog" |
| 156 | notify_user=root # This will be overwritten after we've checked the |
| 157 | notify_host=`unamescip-shell system . . uname. -n` # arguments in the call. |
| 158 | |
| 159 | |
| 160 | finishscip-shell shell ghostscript 10.07.1~dfsg-1 finish().() |
| 161 | { |
| 162 | if [scip-shell system . . `[`. -s "$errlog" ]; then |
| 163 | ${MAILX:-mailx} -s "$name: Error while printing" \ |
| 164 | "$notify_user"@"$notify_host" << --- |
| 165 | The following error occurred while running $0 |
| 166 | in $spool_directory: |
| 167 | |
| 168 | `catscip-shell system . . cat. $errlog` |
| 169 | --- |
| 170 | catscip-shell system . . cat. "$errlog" >&3 # for the log file |
| 171 | rmscip-shell system . . rm. -f "$errlog" |
| 172 | exit 2 |
| 173 | fi |
| 174 | rmscip-shell system . . rm. -f "$errlog" |
| 175 | exit 0 |
| 176 | } |
| 177 | |
| 178 | |
| 179 | # Set up a trap for finish() on exit and SIGINT |
| 180 | trap finish 0 2 |
| 181 | |
| 182 | # Copy standard error into a file. We duplicate file descriptor 2 as 3 because |
| 183 | # stderr can be appended to a log file by the 'lf' field in printcap and we |
| 184 | # should like to be able to have both, a cumulative log file for the |
| 185 | # administrator and a job-specific mail message to the user. |
| 186 | exec 3>&2 |
| 187 | exec 2> "$errlog" |
| 188 | |
| 189 | #****************************************************************************** |
| 190 | |
| 191 | # Process options. Most are irrelevant for gs and this filter. |
| 192 | print_controls=no # print control characters instead of interpreting them |
| 193 | host= # host name of job owner |
| 194 | user= # user name of job owner |
| 195 | length=0 # page length in lines |
| 196 | indentation=0 # amount of indentation in characters |
| 197 | width=0 # page width in characters |
| 198 | while getopts ch:i:l:n:w: option; do |
| 199 | case "$option" in |
| 200 | c) |
| 201 | print_controls=yes;; |
| 202 | h) |
| 203 | host="$OPTARG";; |
| 204 | i) |
| 205 | indentation="$OPTARG";; |
| 206 | l) |
| 207 | length="$OPTARG";; |
| 208 | n) |
| 209 | user="$OPTARG";; |
| 210 | w) |
| 211 | width="$OPTARG";; |
| 212 | *) |
| 213 | echoscip-shell system . . echo. "? $name"": Illegal option specification. The call was:" >&2 |
| 214 | printfscip-shell system . . printf. ' %s\n' "$*" >&2 |
| 215 | exit 2;; |
| 216 | esac |
| 217 | done |
| 218 | testscip-shell system . . test. 1 = "$OPTIND" || shift `exprscip-shell system . . expr. $OPTIND - 1` |
| 219 | |
| 220 | # Deal with non-option arguments |
| 221 | if [scip-shell system . . `[`. $# -gt 1 ]; then |
| 222 | echoscip-shell system . . echo. "? $name"": More than one non-option argument in the call:" >&2 |
| 223 | printfscip-shell system . . printf. ' %s\n' "$*" >&2 |
| 224 | exit 2 |
| 225 | fi |
| 226 | if [scip-shell system . . `[`. $# -gt 0 ]; then |
| 227 | accounting_file="$1" |
| 228 | else |
| 229 | accounting_file= |
| 230 | fi |
| 231 | |
| 232 | # Starting here, any errors occurring are sent to the job's owner |
| 233 | if [scip-shell system . . `[`. '' != "$user" ]; then |
| 234 | notify_user="$user" |
| 235 | testscip-shell system . . test. '' = "$host" || notify_host="$host" |
| 236 | fi |
| 237 | |
| 238 | #****************************************************************************** |
| 239 | |
| 240 | # Set up default values. |
| 241 | |
| 242 | # These are the parameters which can be redefined in the configuration file. |
| 243 | # You might also have to use GS_OPTIONS, for example for the media type. |
| 244 | GS=gs |
| 245 | SUBDEVICE=unspec |
| 246 | COLOURMODEL=Gray # Americans note the "U"! Britons note the "a"! :-) |
| 247 | RESOLUTION= |
| 248 | QUALITY=normal |
| 249 | PSCONFIGFILE=if-pcl3.ps |
| 250 | PAGECOUNTFILE=gs-pages.count |
| 251 | INIT_TRANS= |
| 252 | USE_INTERMEDIATE_FILE=yes |
| 253 | |
| 254 | |
| 255 | # Default accounting: count the number of files printed or, if supported, the |
| 256 | # number of pages. |
| 257 | |
| 258 | acct_startscip-shell shell ghostscript 10.07.1~dfsg-1 acct_start().() |
| 259 | { |
| 260 | testscip-shell system . . test. '' = "$PAGECOUNTFILE" -o ! -f "$PAGECOUNTFILE" || catscip-shell system . . cat. "$PAGECOUNTFILE" |
| 261 | |
| 262 | return |
| 263 | } |
| 264 | |
| 265 | acct_stopscip-shell shell ghostscript 10.07.1~dfsg-1 acct_stop().() |
| 266 | { |
| 267 | pages_before="$1" |
| 268 | testscip-shell system . . test. '' != "$pages_before" || pages_before=0 |
| 269 | |
| 270 | if [scip-shell system . . `[`. '' != "$PAGECOUNTFILE" -a -f "$PAGECOUNTFILE" ]; then |
| 271 | pages_after=`catscip-shell system . . cat. "$PAGECOUNTFILE"` |
| 272 | pages=`exprscip-shell system . . expr. $pages_after - $pages_before` |
| 273 | if [scip-shell system . . `[`. '' = "$pages" -o 0 -ge "$pages" ]; then |
| 274 | testscip-shell system . . test. 0 -ne "$rc" || echoscip-shell system . . echo. "? $name"": Wrong value for number of pages" \ |
| 275 | "printed: \`$pages'." >&2 |
| 276 | pages=1 # count files instead |
| 277 | fi |
| 278 | else |
| 279 | pages=1 # count files |
| 280 | fi |
| 281 | echoscip-shell system . . echo. $pages |
| 282 | |
| 283 | return |
| 284 | } |
| 285 | |
| 286 | #****************************************************************************** |
| 287 | |
| 288 | # Read the configuration file for the filter if present in the spool directory |
| 289 | truescip-shell system . . true. |
| 290 | cfg=if-pcl3.cfg |
| 291 | testscip-shell system . . test. ! -f $cfg -o ! -r $cfg || . ./$cfg |
| 292 | testscip-shell system . . test. $? -eq 0 || exit 2 |
| 293 | |
| 294 | # RESOLUTION and PSCONFIGFILE must not contain field separators |
| 295 | if exprscip-shell system . . expr. "x$RESOLUTION$PSCONFIGFILE" : "x.*[$IFS]" > /dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 296 | prscip-shell system . . printf.intf '? %s: Shell field separator(s) in RESOLUTION (%s)\n' \ |
| 297 | "$name" "$RESOLUTION" >&2 |
| 298 | printfscip-shell system . . printf. ' or PSCONFIGFILE (%s).\n' "$PSCONFIGFILE" >&2 |
| 299 | exit 2 |
| 300 | fi |
| 301 | |
| 302 | #****************************************************************************** |
| 303 | |
| 304 | # Initialize accounting |
| 305 | LC_NUMERIC=C; export LC_NUMERIC |
| 306 | acct_saved=`acct_startscip-shell shell ghostscript 10.07.1~dfsg-1 acct_start().` |
| 307 | |
| 308 | # stdin is the input file, stdout the printer. |
| 309 | |
| 310 | rc=0 |
| 311 | if [scip-shell system . . `[`. '' = "$GS" ]; then |
| 312 | # Transparent queue |
| 313 | printfscip-shell system . . printf. '\033E\033&k2G' # PCL: Printer Reset, Line Termination (LF -> CR+LF) |
| 314 | testscip-shell system . . test. '' = "$INIT_TRANS" || printfscip-shell system . . printf. "$INIT_TRANS" |
| 315 | catscip-shell system . . cat. |
| 316 | rc=$? |
| 317 | else |
| 318 | # An empty specifications for RESOLUTION means "use the default". |
| 319 | testscip-shell system . . test. '' = "$RESOLUTION" || RESOLUTION="-r$RESOLUTION" |
| 320 | |
| 321 | # Remove the PostScript configuration file from the command line if it does |
| 322 | # not exist or is not readable |
| 323 | testscip-shell system . . test. '' != "$PSCONFIGFILE" -a -f "$PSCONFIGFILE" -a -r "$PSCONFIGFILE" || \ |
| 324 | PSCONFIGFILE= |
| 325 | |
| 326 | # If PAGECOUNTFILE is non-empty, insert an option with it in the call. |
| 327 | pcf_option= |
| 328 | testscip-shell system . . test. '' = "$PAGECOUNTFILE" || pcf_option=-sPageCountFile="$PAGECOUNTFILE" |
| 329 | |
| 330 | # We have to pass the resulting PCL file to standard output. Unfortunately, |
| 331 | # if one does this directly from ghostscript (via "-sOutputFile=-"), the |
| 332 | # output can contain unwanted data: error messages and data written to what |
| 333 | # is standard output for PostScript. (I consider this behaviour to be a bug |
| 334 | # in gs.) The latter feature is for example used by some MS Windows drivers |
| 335 | # for PostScript printers in order to report the current processing state |
| 336 | # back to the host. |
| 337 | # If we want neither error messages nor statements like "%%[ Page: 1 ]%%" to |
| 338 | # be intermixed with what is to be printed, we have to create a file first |
| 339 | # and copy it to stdout in a second step. This might require substantial disk |
| 340 | # space. |
| 341 | # Hence I provide both solutions. Choose the one you like best by setting |
| 342 | # USE_INTERMEDIATE_FILE. |
| 343 | |
| 344 | if [scip-shell system . . `[`. no = "$USE_INTERMEDIATE_FILE" ]; then |
| 345 | # Directly to stdout, hoping that other data will not appear. However, |
| 346 | # in order not to loose the error message text, the following command is |
| 347 | # used to ensure that this output will be printed properly and in |
| 348 | # particular without a "staircase effect" provided the error occurs on the |
| 349 | # first page. |
| 350 | # This is for example what happens if the input file is not PostScript. |
| 351 | printfscip-shell system . . printf. '\033E\033&k2G\033&s0C' |
| 352 | # PCL: Printer Reset, Line Termination (LF -> CR+LF), End-of-Line-Wrap (ON). |
| 353 | |
| 354 | # Call ghostscript |
| 355 | ${GS:-gs} -q -dNOPAUSE -dSAFER -sDEVICE=pcl3 -sSubdevice="$SUBDEVICE" \ |
| 356 | -sColourModel="$COLOURMODEL" $RESOLUTION -sPrintQuality="$QUALITY" \ |
| 357 | $pcf_option -sOutputFile=- $PSCONFIGFILE - |
| 358 | rc=$? |
| 359 | testscip-shell system . . test. 0 -eq $rc || \ |
| 360 | printf '\n? %s: %s returned an exit code of %s.\n' "$name" "$GS" "$rc" >&2 |
| 361 | else |
| 362 | # Two-step process with an intermediate file |
| 363 | tmp="${TMPDIR:-/tmp}/$$-2.tmp" |
| 364 | rmscip-shell system . . rm. -f "$tmp" |
| 365 | |
| 366 | # Call ghostscript, redirecting stdout to stderr. Note that permissible |
| 367 | # output (e.g., from "=") will be redirected to stderr as well, hence |
| 368 | # we can't use the size of the resulting file as the sole indication that |
| 369 | # an error has occurred. |
| 370 | prev_umask=`umask` |
| 371 | umask 027 # Ensure privacy. The group should be lp. |
| 372 | ${GS:-gs} -q -dNOPAUSE -dSAFER -sDEVICE=pcl3 -sSubdevice="$SUBDEVICE" \ |
| 373 | -sColourModel="$COLOURMODEL" $RESOLUTION -sPrintQuality="$QUALITY" \ |
| 374 | $pcf_option -sOutputFile="$tmp" $PSCONFIGFILE - >&2 |
| 375 | rc=$? |
| 376 | testscip-shell system . . test. 0 -eq $rc || \ |
| 377 | printf '\n? %s: %s returned an exit code of %s.\n' "$name" "$GS" "$rc" >&2 |
| 378 | umask "$prev_umask" |
| 379 | |
| 380 | # If an error occurred, don't bother printing. |
| 381 | if [scip-shell system . . `[`. 0 -eq $rc ]; then |
| 382 | catscip-shell system . . cat. "$tmp" |
| 383 | rc=$? |
| 384 | testscip-shell system . . test. $rc -eq 0 || printfscip-shell system . . printf. \ |
| 385 | '? %s: Error copying the generated file to stdout.\n' "$name" >&2 |
| 386 | fi |
| 387 | |
| 388 | rmscip-shell system . . rm. -f "$tmp" |
| 389 | fi |
| 390 | fi |
| 391 | |
| 392 | # Reset the printer to its default state. This also ejects any unfinished pages |
| 393 | # which is needed if the transparent queue is used for printing ordinary text. |
| 394 | # It is superfluous when printing with pcl3 because pcl3 generates this at the |
| 395 | # end of its output already, but a second reset is harmless (unless the printer |
| 396 | # knows more than one command language and PCL-3+ is not the default) and it |
| 397 | # works even if gs crashes. |
| 398 | printfscip-shell system . . printf. '\033E' # PCL: Printer Reset |
| 399 | |
| 400 | # Terminate accounting |
| 401 | pages=`acct_stopscip-shell shell ghostscript 10.07.1~dfsg-1 acct_stop(). "$acct_saved"` |
| 402 | |
| 403 | # Exit on error |
| 404 | testscip-shell system . . test. 0 -eq $rc || exit 2 |
| 405 | |
| 406 | #****************************************************************************** |
| 407 | |
| 408 | # User-specific accounting on success |
| 409 | if [scip-shell system . . `[`. '' != "$accounting_file" ]; then |
| 410 | printfscip-shell system . . printf. '%7.2f\t%s:%s\n' "$pages" "$host" "$user" >> "$accounting_file" |
| 411 | fi |
| 412 | |
| 413 | # Remove the trap for finish() in case the stderr log is non-empty although |
| 414 | # everything worked. |
| 415 | trap - 0 2 |
| 416 | |
| 417 | exit 0 |