| 1 | #!/bin/sh |
| 2 | #****************************************************************************** |
| 3 | # File: @(#)$Id: cups-pcl3,v 2.5 2001/05/18 14:58:24 Martin Rel $ |
| 4 | # Contents: CUPS filter converting application/vnd.cups-postscript to PCL-3 |
| 5 | # using ghostscript with the pcl3 driver |
| 6 | # Call: cups-pcl3 <CUPS filter arguments> |
| 7 | # Author: Martin Lottermoser, Greifswaldstrasse 28, 38124 Braunschweig, |
| 8 | # Germany; [email protected] |
| 9 | # |
| 10 | #****************************************************************************** |
| 11 | # * |
| 12 | # Copyright (C) 2001 by Martin Lottermoser * |
| 13 | # All rights reserved * |
| 14 | # * |
| 15 | #****************************************************************************** |
| 16 | # |
| 17 | # Required entries in PPD files |
| 18 | # ----------------------------- |
| 19 | # The PPD files distributed with pcl3 are by default already configured for |
| 20 | # this filter. If you are using one of these files, continue reading with the |
| 21 | # next section. |
| 22 | # |
| 23 | # If you want to use another PPD file or ghostscript driver, add or modify the |
| 24 | # following two statements in the PPD file: |
| 25 | # |
| 26 | # *cupsFilter: "application/vnd.cups-postscript 0 cups-pcl3" |
| 27 | # *GhostscriptOptions: "<gs options>" |
| 28 | # |
| 29 | # Replace <gs options> with a sequence of command line options for ghostscript |
| 30 | # which select the device, in the case of pcl3 also the subdevice if the device |
| 31 | # is "pcl3". From the point of view of the PPD specification, the |
| 32 | # *GhostscriptOptions statement may contain only a single-line QuotedValue |
| 33 | # without hexadecimal substrings. From the point of view of this filter, the |
| 34 | # value should not contain any characters with special meaning for the shell, |
| 35 | # except internal field separators. |
| 36 | # |
| 37 | # |
| 38 | # Installation of this filter |
| 39 | # --------------------------- |
| 40 | # Copy this file to the .../cups/filter directory. |
| 41 | # |
| 42 | # If you've not yet installed the necessary PPD files, do it now. After that |
| 43 | # you should be able to create a queue and start to print. The remaining |
| 44 | # comments refer to steps you may perform later if you wish. |
| 45 | # |
| 46 | # Exception: If the ghostscript binary you wish to use is not the one found |
| 47 | # by calling "gs" from the filter, you'll have to set "GhostscriptCommand" in |
| 48 | # a configuration file. Read the instructions in the next section. |
| 49 | # |
| 50 | # Should you encounter problems when trying to print, check CUPS' error log |
| 51 | # file. If necessary, increase the CUPS LogLevel to "debug", restart CUPS and |
| 52 | # try again. |
| 53 | # |
| 54 | # |
| 55 | # Configuration |
| 56 | # ------------- |
| 57 | # This filter uses a queue-specific PostScript configuration file if it exists. |
| 58 | # This file must have the name $PRINTER.ps ($PRINTER is the name of the queue) |
| 59 | # and must reside in the "config" subdirectory (create it if it doesn't exist) |
| 60 | # of the $CUPS_SERVERROOT directory, usually /etc/cups. This file will be |
| 61 | # handed to ghostscript before the PostScript file to be printed. |
| 62 | # |
| 63 | # Use this configuration file for correcting misalignments, modifying |
| 64 | # "InputAttributes" or "OutputAttributes", setting default transfer functions |
| 65 | # or any other queue-specific configuration independent of the document to be |
| 66 | # printed. Primarily this file should contain PostScript commands to simulate |
| 67 | # persistent state for the printer (what you would store in a real PostScript |
| 68 | # printer's memory in such a manner that it survives power-off or at least |
| 69 | # end-of-job). Note that because the file to be printed will be processed after |
| 70 | # the configuration file, the former can still override the latter. |
| 71 | # |
| 72 | # In addition, you can insert configuration statements for this filter into the |
| 73 | # file. These statements must be given as PostScript comments starting in |
| 74 | # column 1. The possiblities are: |
| 75 | # |
| 76 | # % DivertToFile: <path> |
| 77 | # CUPS' document manager does not always operate correctly. If you |
| 78 | # suspect that the settings you have demanded do not take effect or |
| 79 | # ghostscript gives an error when printing through this filter while the |
| 80 | # original file is valid PostScript, set <path> to the name of a file |
| 81 | # which is writable for the user running this filter (usually lp). |
| 82 | # Instead of printing, the filter will then copy its input to that file, |
| 83 | # prepending a comment containing the command line it would use for |
| 84 | # calling ghostscript. |
| 85 | # |
| 86 | # % DoAccounting: <True or False> |
| 87 | # This parameter determines whether pcl3 will issue CUPS page accounting |
| 88 | # messages or not. The default is "True". You should set this to "False" |
| 89 | # only if the page accounting messages are generated by the backend |
| 90 | # (later in the print pipeline). |
| 91 | # |
| 92 | # % GhostscriptCommand: <path to gs> |
| 93 | # Use this to set the command to invoke ghostscript. The default |
| 94 | # corresponds to "% GhostscriptCommand: gs". |
| 95 | # |
| 96 | # % UseIntermediateFile: <True or False> |
| 97 | # This statement determines whether the ghostscript output file is first |
| 98 | # written to disk or is copied directly to the printer. The former is |
| 99 | # safer because it is legitimate for a PostScript program to invoke |
| 100 | # operators like "print" which generate output which ghostscript maps to |
| 101 | # standard output even for "-sOutputFile=-" (in my opinion this is a bug |
| 102 | # in ghostscript). If you print such a document, the result is unlikely |
| 103 | # to be usable. The same applies if you print an illegal PostScript file |
| 104 | # because ghostscript issues some error messages on standard output. |
| 105 | # The default is therefore "% UseIntermediateFile: True". This might |
| 106 | # however require substantial disk space. |
| 107 | # |
| 108 | # Of course the file need not contain any PostScript commands if you just need |
| 109 | # it for filter configuration. |
| 110 | # |
| 111 | #****************************************************************************** |
| 112 | |
| 113 | #name=`basename "$0"` |
| 114 | name=cups-pcl3 # Invalid CUPS documentation |
| 115 | |
| 116 | printf 'DEBUG: %s: called for queue %s\n' "$name" "${PRINTER:-}" >&2 |
| 117 | printf 'DEBUG: %s: command line: %s %s\n' "$name" "$0" "$*" >&2 |
| 118 | |
| 119 | # Arguments |
| 120 | if [ $# -ne 5 -a $# -ne 6 ]; then |
| 121 | printf 'ERROR: Usage: %s job user title copies options [filename]\n' "$0" >&2 |
| 122 | exit 1 |
| 123 | fi |
| 124 | job="$1" |
| 125 | user="$2" |
| 126 | title="$3" |
| 127 | copies="$4" # can be ignored in this filter (pstops generates "NumCopies") |
| 128 | options="$5" |
| 129 | test $# -lt 6 || exec < "$6" || exit 1 |
| 130 | |
| 131 | # We ignore all options completely because, as far as they are meaningful and |
| 132 | # as far as I understand the matter, preceding filters (in particular pstops) |
| 133 | # should have taken care of them, i.e., we assume that they have been converted |
| 134 | # into PostScript code as far as possible. |
| 135 | # Besides, I'm not aware of any statement in CUPS which tells a filter's |
| 136 | # author which options can be expected (from the source code it's easiest to |
| 137 | # find out which one needs not to expect, but that is not very interesting). |
| 138 | |
| 139 | #****************************************************************************** |
| 140 | |
| 141 | # Find configuration file for the queue |
| 142 | cfgfile="${CUPS_SERVERROOT:-/etc/cups}/config/${PRINTER:-}.ps" |
| 143 | if [ -f "$cfgfile" ]; then |
| 144 | printf 'DEBUG: %s: Using configuration file %s.\n' "$name" "$cfgfile" >&2 |
| 145 | else |
| 146 | printf 'DEBUG: %s: No configuration file %s.\n' "$name" "$cfgfile" >&2 |
| 147 | cfgfile= |
| 148 | fi |
| 149 | |
| 150 | # Subroutine to extract filter options from the configuration file |
| 151 | extract() |
| 152 | { |
| 153 | # Argument $1: name of the option |
| 154 | sed -e "/^% $1:/!d" -e 's/^[^:]*: *//' "$cfgfile" | head -1 |
| 155 | } |
| 156 | |
| 157 | # Extract filter options |
| 158 | dacc= |
| 159 | divert= |
| 160 | gs_command= |
| 161 | use_intermediate_file= |
| 162 | if [ '' != "$cfgfile" ]; then |
| 163 | if [ ! -r "$cfgfile" ]; then |
| 164 | printf 'ERROR: %s: %s is not readable.\n' "$name" "$cfgfile" >&2 |
| 165 | exit 1 |
| 166 | fi |
| 167 | dacc=`extract DoAccounting` |
| 168 | divert=`extract DivertToFile` |
| 169 | gs_command=`extract GhostscriptCommand` |
| 170 | use_intermediate_file=`extract UseIntermediateFile` |
| 171 | fi |
| 172 | test '' != "$dacc" || dacc=True |
| 173 | test '' != "$gs_command" || gs_command=gs |
| 174 | test '' != "$use_intermediate_file" || use_intermediate_file=True |
| 175 | printf 'DEBUG: %s: GhostscriptCommand: %s, DoAccounting: %s, '\ |
| 176 | 'UseIntermediateFile: %s.\n' \ |
| 177 | "$name" "$gs_command" "$dacc" "$use_intermediate_file" >&2 |
| 178 | accounting=-dCUPSAccounting |
| 179 | test False != "$dacc" || accounting= |
| 180 | |
| 181 | # Find the command line options from the PPD file. We need not bother about |
| 182 | # *Include statements because CUPS doesn't support them. |
| 183 | if [ ! -f "${PPD:-}" ]; then |
| 184 | printf 'ERROR: %s: No PPD file %s.\n' "$name" "${PPD:-}" >&2 |
| 185 | exit 1 |
| 186 | fi |
| 187 | printf 'DEBUG: %s: Using PPD file %s.\n' "$name" "$PPD" >&2 |
| 188 | gs_options=`grep '^\*GhostscriptOptions:' "$PPD" | \ |
| 189 | sed -e '1!d' -e 's/^[^"]*"//' -e 's/".*$//'` |
| 190 | if [ '' = "$gs_options" ]; then |
| 191 | printf 'ERROR: %s: No *GhostscriptOptions statement in %s.\n' \ |
| 192 | "$name" "$PPD" >&2 |
| 193 | exit 1 |
| 194 | fi |
| 195 | printf 'DEBUG: %s: *GhostscriptOptions: "%s"\n' "$name" "$gs_options" >&2 |
| 196 | |
| 197 | # Treat RIP_MAX_CACHE |
| 198 | if [ '' != "${RIP_MAX_CACHE:-}" ]; then |
| 199 | printf 'DEBUG: %s: RIP_MAX_CACHE is %s.\n' "$name" "$RIP_MAX_CACHE" >&2 |
| 200 | |
| 201 | # Convert to "MaxBitmap" value (cnf. pstoraster/gdevprn.c in CUPS) |
| 202 | amount=`printf '%s\n' "$RIP_MAX_CACHE" | sed -e 's/[^0-9].*$//'` |
| 203 | unit=`printf '%s\n' "$RIP_MAX_CACHE" | sed -e 's/^[0-9]*//'` |
| 204 | if [ '' = "$amount" ]; then |
| 205 | amount=33554432 # 32 binary mega bytes |
| 206 | else |
| 207 | case "$unit" in |
| 208 | g*) |
| 209 | amount=`expr $amount '*' 1073741824`;; # binary giga bytes |
| 210 | # Note that this will fail on 32-bit systems if "amount" is larger than |
| 211 | # one. |
| 212 | m*) |
| 213 | amount=`expr $amount '*' 1048576`;; # binary mega bytes |
| 214 | k*) |
| 215 | amount=`expr $amount '*' 1024`;; # binary kilo bytes |
| 216 | *) |
| 217 | # This applies to "t" and to no unit specification. |
| 218 | amount=`expr $amount '*' 262144`;; # 256 K |
| 219 | esac |
| 220 | fi |
| 221 | |
| 222 | printf 'DEBUG: %s: MaxBitmap will become %s.\n' "$name" "$amount" >&2 |
| 223 | gs_options="$gs_options -dMaxBitmap=$amount" |
| 224 | fi |
| 225 | |
| 226 | #****************************************************************************** |
| 227 | |
| 228 | if [ '' != "$divert" ]; then |
| 229 | cat << --- > "$divert" || { \ |
| 230 | printf 'ERROR: %s: Failure to write to %s.\n' "$name" "$divert" >&2; \ |
| 231 | exit 1; } |
| 232 | %! |
| 233 | % $name input for job $job |
| 234 | % Call to ghostscript: $gs_command $gs_options -dCUPSMessages $accounting $cfgfile <this file> |
| 235 | --- |
| 236 | cat >> "$divert" || exit 1 |
| 237 | printf 'WARNING: %s: No printing, output for job %s diverted to %s.\n' \ |
| 238 | "$name" "$job" "$divert" >&2 |
| 239 | exit 0 |
| 240 | fi |
| 241 | |
| 242 | #****************************************************************************** |
| 243 | |
| 244 | if [ False = "$use_intermediate_file" ]; then |
| 245 | # Directly to stdout, hoping that other data will not appear. However, |
| 246 | # in order not to loose an error message text, the following command is |
| 247 | # used to ensure that this output will be printed properly and in |
| 248 | # particular without a "staircase effect" provided the error occurs on the |
| 249 | # first page. |
| 250 | printf '\033E\033&k2G\033&s0C' |
| 251 | # PCL: Printer Reset, Line Termination (LF -> CR+LF), End-of-Line-Wrap (ON). |
| 252 | |
| 253 | # Now call ghostscript ("-dNOPAUSE -dBATCH" are currently superfluous, but |
| 254 | # ghostscript's documentation is not sufficiently clear on this point to make |
| 255 | # it appear safe to drop them). |
| 256 | $gs_command $gs_options -dCUPSMessages $accounting -q -dNOPAUSE -dBATCH \ |
| 257 | -dSAFER -sOutputFile=- $cfgfile - |
| 258 | # Newer gs versions (>= 5.65) accept also "-_" which might be faster than "-". |
| 259 | rc=$? |
| 260 | else |
| 261 | # Call ghostscript with an intermediate file. See also comments above. |
| 262 | tmp="${TMPDIR:-/tmp}/$$.tmp" |
| 263 | rm -f "$tmp" |
| 264 | $gs_command $gs_options -dCUPSMessages $accounting -q -dNOPAUSE -dBATCH \ |
| 265 | -dSAFER -sOutputFile="$tmp" $cfgfile - >&2 |
| 266 | rc=$? |
| 267 | |
| 268 | # Copy the file to stdout on success |
| 269 | test 0 -ne $rc -o ! -f "$tmp" || cat "$tmp" || { |
| 270 | rm -f "$tmp" |
| 271 | printf 'ERROR: %s: cat to stdout failed for %s.\n' "$name" "$tmp" >&2 |
| 272 | exit 1 |
| 273 | } |
| 274 | rm -f "$tmp" |
| 275 | fi |
| 276 | |
| 277 | if [ 0 -ne $rc ]; then |
| 278 | printf 'ERROR: %s: ghostscript returned %s for job %s (invalid PostScript input?).\n' \ |
| 279 | "$name" "$rc" "$job" >&2 |
| 280 | |
| 281 | # Send a mail message if we have mailx and "$user" is something sensible |
| 282 | # (CUPS doesn't seem to guarantee that at present for remote users) |
| 283 | if type mailx > /dev/null 2>&1 && \ |
| 284 | { expr x"$user" : '.*@' > /dev/null || id "$user" > /dev/null 2>&1; } |
| 285 | then |
| 286 | mailx -s "$name"": Error printing job $job" "$user" << --- |
| 287 | Ghostscript returned an error code ($rc) for your print job $job. |
| 288 | |
| 289 | The most likely cause is an invalid input file. If you submitted a |
| 290 | PostScript file, run ghostscript or ghostview on it to check its |
| 291 | correctness. If it is valid or you submitted a non-PostScript file, |
| 292 | inspect CUPS' error log file for further information on the error. |
| 293 | |
| 294 | If that does not expose the cause, read the initial comments in |
| 295 | $0, especially on DivertToFile. |
| 296 | --- |
| 297 | fi |
| 298 | # No exit with non-zero return code because that would disable the printer. |
| 299 | # As the error is most likely due to an illegal input file, such a drastic |
| 300 | # step is not justified (it would even prevent other users from printing). |
| 301 | fi |
| 302 | |
| 303 | exit 0 |