aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
authorfelics2018-05-21 13:04:47 +0200
committerfelics2018-05-21 13:04:47 +0200
commit82afc95121eb73e51c51137a9132c819432f2939 (patch)
treec0fc918a598424c9c3e12e2eec45523225a73ecc
parent1bd51a9076b76ce97028a3a6e18d0f97a208e7fc (diff)
downloadaur-82afc95121eb73e51c51137a9132c819432f2939.tar.gz
Improvements to the software.
-rw-r--r--.SRCINFO16
-rw-r--r--PKGBUILD21
-rw-r--r--README.md73
-rw-r--r--cups-programme.conf15
-rwxr-xr-xcups-programme.sh397
5 files changed, 329 insertions, 193 deletions
diff --git a/.SRCINFO b/.SRCINFO
index cbd2ff7bd7b1..9467763add23 100644
--- a/.SRCINFO
+++ b/.SRCINFO
@@ -1,13 +1,17 @@
pkgbase = cups-print-to-programme
pkgdesc = Print filter for cups which prints to a file (type: ) and opens that with a programme the user can choose in the settings. For example, print to gimp.
- pkgver = 0.1
- pkgrel = 4
+ pkgver = 0.1.5
+ pkgrel = 1
url = http://felics.kettenbruch.de/software/cups-print-to-programme/
install = cups-programme.install
arch = any
license = GPL3
depends = cups
depends = mariadb
+ optdepends = ghostscript: To convert to PDF (also needed for SVG).
+ optdepends = pdf2svg: To convert to SVG.
+ optdepends = imagemagick: To convert to PNG, TIFF, GIF or JPEG.
+ optdepends = graphicsmagick: To convert to PNG, TIFF, GIF or JPEG.
optdepends = kde-cli-tools: For 'kdesu' graphical frontend to run command as different user.
optdepends = kdesudo: For 'kdesudo' graphical frontend to run command as different user.
optdepends = x11-ssh-askpass: For a graphical password input frontend for sudo.
@@ -16,16 +20,16 @@ pkgbase = cups-print-to-programme
optdepends = seahorse: For a graphical password input frontend for sudo.
optdepends = ksshaskpass: For a graphical password input frontend for sudo.
optdepends = gnome-ssh-askpass2: For a graphical password input frontend for sudo.
- provides = cups-print-to-programme-doc=0.1
+ provides = cups-print-to-programme-doc=0.1.5
backup = etc/cups/cups-programme.conf
source = cups-programme.sh
source = cups-programme.conf
source = README.md
source = GPL3.txt
source = cups-programme.install
- sha256sums = 3d5116b2cf83c1b4057f5d2081d598446225531a32424ba70a1d148d835b9853
- sha256sums = e7cb6af420fb7fdfa860a94480ddd75d89033f1f66c9b0489719e525a2b4c0cc
- sha256sums = 31f6a2fd3d34dde35415681450057c5c8a730df8b1f015989018696ccd22cd3b
+ sha256sums = 4f1cbf645a85df7b52f70138be67cf3840d4cc6bcb2ea4ab10034b869806cb6b
+ sha256sums = f583ed8969deef9e890b20c475bc24fd4eed8ef3c3a51544f9183fcc69d38c5c
+ sha256sums = fff96ebdac583c710fa8762e967929b905ed342a0b8977268741e40403a3e31c
sha256sums = 3972dc9744f6499f0f9b2dbf76696f2ae7ad8af9b23dde66d6af86c9dfb36986
sha256sums = 0ee996783ae6848cf3c522de135646cb4b19670a48d6f1bf387b5b304f5506ad
diff --git a/PKGBUILD b/PKGBUILD
index 5fcba26d1cd0..6c81f72a7311 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -3,8 +3,8 @@
_pkgname=cups-print-to-programme
pkgname="${_pkgname}"
-pkgver=0.1
-pkgrel=4
+pkgver=0.1.5
+pkgrel=1
pkgdesc="Print filter for cups which prints to a file (type: ) and opens that with a programme the user can choose in the settings. For example, print to gimp."
arch=('any')
depends=(
@@ -22,6 +22,10 @@ source=(
"${install}"
)
optdepends=(
+ "ghostscript: To convert to PDF (also needed for SVG)."
+ "pdf2svg: To convert to SVG."
+ "imagemagick: To convert to PNG, TIFF, GIF or JPEG."
+ "graphicsmagick: To convert to PNG, TIFF, GIF or JPEG."
"kde-cli-tools: For 'kdesu' graphical frontend to run command as different user."
"kdesudo: For 'kdesudo' graphical frontend to run command as different user."
"x11-ssh-askpass: For a graphical password input frontend for sudo."
@@ -33,13 +37,12 @@ optdepends=(
)
provides=("${_pkgname}-doc=${pkgver}")
backup=('etc/cups/cups-programme.conf')
-sha256sums=(
- '3d5116b2cf83c1b4057f5d2081d598446225531a32424ba70a1d148d835b9853'
- 'e7cb6af420fb7fdfa860a94480ddd75d89033f1f66c9b0489719e525a2b4c0cc'
- '31f6a2fd3d34dde35415681450057c5c8a730df8b1f015989018696ccd22cd3b'
- '3972dc9744f6499f0f9b2dbf76696f2ae7ad8af9b23dde66d6af86c9dfb36986'
- '0ee996783ae6848cf3c522de135646cb4b19670a48d6f1bf387b5b304f5506ad'
-)
+sha256sums=('4f1cbf645a85df7b52f70138be67cf3840d4cc6bcb2ea4ab10034b869806cb6b'
+ 'f583ed8969deef9e890b20c475bc24fd4eed8ef3c3a51544f9183fcc69d38c5c'
+ 'fff96ebdac583c710fa8762e967929b905ed342a0b8977268741e40403a3e31c'
+ '3972dc9744f6499f0f9b2dbf76696f2ae7ad8af9b23dde66d6af86c9dfb36986'
+ '0ee996783ae6848cf3c522de135646cb4b19670a48d6f1bf387b5b304f5506ad')
+
pkgver() {
cd "${srcdir}"
diff --git a/README.md b/README.md
index 60268019f6f3..51bc65f7c8ac 100644
--- a/README.md
+++ b/README.md
@@ -47,21 +47,22 @@ it's information which programme to execute.
The Device URI to print to The Gimp is, for example:
- cups-programme:/usr/bin/gimp?u=<user>&D=:0.0&%s
+ cups-programme:/usr/bin/gimp?u=<user>&D=%C0.0&%s
where `<user>` is the name of the user `gimp` should be run as. If not
specified, it will be run as the user CUPS is running as, which might
-not be desired. The `D=:0.0` specifies the X11 display to use. In most
-cases, `:0.0` is correct. (See section "Device URI syntax" for a full
-description of the Device URI.)
+not be desired. The `D=%C0.0` specifies the X11 display to use. In most
+cases, `:0.0` is correct (`%C` will be replaced by `:`). (See section
+"Device URI syntax" for a full description of the Device URI.)
After the Device URI has been entered, select a name and optionally a
description and a location note for the printer. Then you are presented
with a list of printer manufacturers and models to choose from. Select
the manufacturer `Generic` and any generic PostScript printer. (This
-selects a PPD file for the printer. Since we just want to open the raw
-CUPS PostScript output, just select any PostScript printer and then
-ignore the printer options is fine.)
+selects a PPD file for the printer. Since we just want to open (or later
+convert -- see section "Configuration File") the raw CUPS PostScript
+output, just select any PostScript printer and then ignore the printer
+options is fine.)
Adding the printer might fail with an error like
@@ -98,27 +99,41 @@ where:
separated by `&`, are allowed).
an `<option>` can be of the form:
- `u=<user>` -- specifies that the executable should be run as user
- `<user>` (see explanation of `su_variant` in the section
+ `<user>`. If `<user>` is the special word '.CUPSUSER', then the
+ executable will be run as the print-job invocing user CUPS reports
+ to the backend. (see also explanation of `su_variant` in the section
"Configuration File"),
- `g=<group>` -- specifies that the executable should be run with
primary group `<group>` (see section "Notes"),
- `D=<DISPLAY>` -- if set, the environment variable `DISPLAY` will be
set to `<DISPLAY>` and exported prior execution of
`<path-to-executable>`,
+ - `t=<filetype>` -- if set, CUPS' PostScript output will be converted
+ to `<filetype>`. Possible values for `<filetype>` are:
+ + 'ps' (PostScript; retain CUPS' default),
+ + 'pdf' (PDF; uses 'ps2pdf'),
+ + 'svg' (SVG; uses 'pdf2svg'),
+ + 'png' (PNG; uses ImageMagick or GraphicsMagick),
+ + 'gif' (GIF; uses ImageMagick or GraphicsMagick),
+ + 'jpg' (JPEG; uses ImageMagick or GraphicsMagick),
+ + 'tif' (TIFF; uses ImageMagick or GraphicsMagick),
- anything else will be passed as positional arguments to
- the executable, with the following string substitutions applied (see
- also "Notes"):
- + `%s` -> The file where the CUPS print output is saved. Use `%s` to
- pass the printed file to be opened to the executable.
- + `%.` -> ` `,
- + `%_` -> `-`,
- + `%P` -> `|`,
- + `%B` -> `\`,
- + `%H` -> `#`,
- + `%Q` -> `?`,
- + `%A` -> `&`,
- + `%%` -> `%`.
-
+ the executable.
+* On `<path-to-executable>` and all the options (except `t=<filetype>`),
+ the following string substitutions are applied (see also "Notes"):
+ - `%s` -> The file where the CUPS print output is saved. Use `%s` to
+ pass the printed file to be opened to the executable.
+ - `%.` -> ` `,
+ - `%_` -> `-`,
+ - `%P` -> `|`,
+ - `%B` -> `\`,
+ - `%H` -> `#`,
+ - `%Q` -> `?`,
+ - `%A` -> `&`,
+ - `%C` -> `:`,
+ - `%T` -> `'`,
+ - `%G` -> `"`,
+ - `%%` -> `%`.
Configuration File
@@ -128,8 +143,8 @@ The printer backend expects a configuration file to be present at
`/etc/cups/cups-programme.conf`. This file will be bash-sourced, so
beware what you do there.
-Currently, there are two variables to be set in the configuration file:
-`su_variant` and `askpass_cmd`.
+The following variables are to be set in the configuration file:
+`su_variant`, `askpass_cmd` and `image_converter`.
* When a programme should be run as a specific user (`u=<user>` in the
Device URI) or with a specific primary group (`g=<group>` in the
@@ -147,7 +162,13 @@ Currently, there are two variables to be set in the configuration file:
* When `su_variant=sudo-askpass` is set, then `askpass_cmd` needs to be
set and needs to contain a full path to an askpass executable.
-Read the comments in the configuration file for further details.
+* If the `t=<filetype>` option in the Device URI specifies a pixel
+ graphic format, then `image_converter` must be set. It specifies if
+ ImageMagick or GraphicsMagick is to be used when converting to a pixel
+ graphic.
+
+Read the comments in the configuration file for further details, e.g.
+supported values.
Notes
@@ -169,9 +190,9 @@ Notes
When this happens, add the printer with a very basic Device URI like
`cups-programme:/usr/bin/gimp`, and after adding the printer, edit
- /etc/cups/printers.conf manually and change the DeviceURI to the
+ `/etc/cups/printers.conf` manually and change the `DeviceURI` to the
desired value. (Stop `cupsd` before, and restart cupsd after editing
- /etc/cups/printers.conf.)
+ `/etc/cups/printers.conf`.)
[1]: http://cups.org/
diff --git a/cups-programme.conf b/cups-programme.conf
index ceb277b41b04..220d4c0b3f90 100644
--- a/cups-programme.conf
+++ b/cups-programme.conf
@@ -58,3 +58,18 @@ askpass_cmd=/usr/lib/ssh/x11-ssh-askpass
# askpass_cmd=/usr/bin/ksshaskpass
# askpass_cmd=/usr/bin/qt4-ssh-askpass
# askpass_cmd=/usr/lib/seahorse/ssh-askpass
+
+
+
+# image_converter:
+#
+# Specifies if 'ImageMagick' or 'GraphicsMagick' should be used to
+# convert to pixel graphic. If the 't=<filetype>' option is present in
+# the Device URI and it specifies a pixel graphic, 'ìmage_converter' is
+# required.
+#
+# Possible values:
+# - 'im': ImageMagick
+# - 'gm': GraphicsMagick
+
+image_converter=im
diff --git a/cups-programme.sh b/cups-programme.sh
index c4e0ecfe5877..27d27e8f2d94 100755
--- a/cups-programme.sh
+++ b/cups-programme.sh
@@ -29,7 +29,7 @@
#
# The version.
-VERSION=0.1
+VERSION=0.1.5
# Abort on error
set -e
@@ -43,6 +43,14 @@ conffile=/etc/cups/cups-programme.conf
# Timestamp
tstamp="$(date '+%Y-%m-%d_%T%z')"
+case "$1" in
+ '-V'|'-version'|'--version')
+ echo "${VERSION}"
+ exit 0
+ ;;
+esac
+
+
# Write to stderr:
stderr() {
echo "$@" 1>&2
@@ -111,29 +119,106 @@ jobcopies=${4}
joboptions=${5}
jobfile=${6}
-# Setting, and creating/clearing our logfile:
+
+# Creating our logfile name:
logfile="/tmp/${backend_name}.${jobid}.log"
-echo -n '' > "${logfile}"
-# Make it world read- and writeable, in case we are run under varying user IDs.
-chmod 666 "${logfile}" 2>/dev/null || true
-log_local "$0 started at ${tstamp}. Our log file is: ${logfile}."
+# Creating the name of the temporary file, for CUPS print output:
+tmpfile="/tmp/${backend_name}_job-${jobid}_${jobtitle}.ps"
+
+# The final outpuf file name before executing the programme; without suffix (suffix will be determined later):
+outfile_prefix="/tmp/${backend_name}.${jobid}"
+
+
+### Determine our operation mode, and handle the modes.
+case ${#} in
+ 0)
+ # This case is for "backend discovery mode."
+ echo "direct ${backend_name} \"Unknown\" \"cups-programme: Print to file and open in programme. DeviceURI example: ${backend_name}:/usr/bin/gimp?u=<user>&D=%C0.0&%s\". See documentation!"
+ exit 0
+ ;;
+ 5)
+ # Backend needs to read from stdin if number of arguments is 5.
+ log_cups "Writing print job ${jobid} output to ${tmpfile} ..."
+ cat - > "${tmpfile}"
+ # Make it world read- and writeable, to be able to work with it as another user.
+ chmod 666 "${tmpfile}"
+ log_cups "Print job ${jobid} output written to ${tmpfile}."
+ ;;
+ 6)
+ # Backend needs to read from file if number of arguments is 6.
+ log_cups "Writing print job ${jobid} output to ${tmpfile} ..."
+ cat "${6}" > "${tmpfile}"
+ # Make it world read- and writeable, to be able to work with it as another user.
+ chmod 666 "${tmpfile}"
+ log_cups "Print job ${jobid} output written to ${tmpfile}."
+ ;;
+ *)
+ # This one should not be reached in normal operation.
+ mode='unsupported'
+ stderr "$0: Error: Called with incorrect number ($#) of arguments."
+ stderr ""
+ stderr "Usage:"
+ stderr ""
+ stderr " $0 <job-ID> <user> <jobtitle> <copies> <joboptions> [<jobfile>]"
+ stderr ""
+ stderr "OR (for backend discovery mode):"
+ stderr ""
+ stderr " $0"
+ stderr ""
+ stderr "OR (to retrieve the version number):"
+ stderr ""
+ stderr " $0 -V | -version | --version"
+ stderr ""
+ stderr ""
+ stderr "(Install this as CUPS backend to '/usr/lib/cups/backend/${backend_name}'.)"
+ stderr "(Read the comment in $0 to see how to set this up.)"
+
+ error "$0 called with $# arguments. Unsupported mode. Aborting."
+
+ exit 11
+ ;;
+esac
+
+# If possible, make the tmpfile write- and readable by all, for convenience (e.g. to easily remove it in case it is leftover).
+chmod a+wr "${tmpfile}" || true
+
+# Postpone this logging to here, because only when we reached up to here we are correctly be running as a filter.
log_local ""
+info "$0 started at ${tstamp}. Our log file is: ${logfile}."
+log_local ""
+# If possible, make the logfile write- and readable by all, for convenience (e.g. to easily remove it in case it is leftover).
+chmod a+wr "${logfile}" || true
+### Read the configuration file.
+# Unset variables to be read from $conffile first, to be safe from cases when they hang around in the environment.
+unset su_variant
+unset askpass_cmd
+unset image_converter
+if [ -e "${conffile}" ]; then
+ log_local "Reading configuration file '${conffile}' ..."
+ . "${conffile}" || {
+ error "$0: Error while reading ${conffile}."
+ }
+ log_local "Configuration file read."
+else
+ warn "$0: Configuration file '${conffile}' not present."
+fi
+log_local ""
-### The command we want to execute will be fetched from the environment variable 'DEVICE_URI'.
+### Parse $DEVICE_URI.
#
# $DEVICE_URI format:
#
-# ${backend_name}:<command>?u=<user>&g=<group>&D=<display>&<argument>&<argument>...
+# ${backend_name}:<command>?u=<user>&g=<group>&D=<display>&t=<filetype>&<argument>&<argument>...
#
-# All arguments, including the 'u=<user>', 'g=<group>' and 'D=<display>', are optional.
-# Argument delimiter character is '#'.
+# All arguments, including the 'u=<user>', 'g=<group>', 'D=<display>'
+# and 't=<filetype>, are optional.
#
-# In '<argument>', the following replacements will be carried out:
-# - '%s' -> "${tmpfile}" (Where we save the output from CUPS),
+# The following replacements will be carried out for '<command>' and all
+# options except 't=<filetype>':
# - '%.' -> ' '
# - '%_' -> '-'
# - '%P' -> '|'
@@ -141,11 +226,14 @@ log_local ""
# - '%H' -> '#'
# - '%Q' -> '?'
# - '%A' -> '&'
+# - '%C' -> ':'
+# - '%T' -> '''
+# - '%G' -> '"'
# - '%%' -> '%'
+# - '%s' -> "${outfile}" (The, probably converted, output from CUPS to
+# open)
#
-# The programme will be invoked with the specified arguments, and the
-# file name of the print job appended.
-# The print job will be a PostScript file.
+# The programme will be invoked with the specified arguments.
# Getting the command from $DEVICE_URI:
cmd="$(echo "${DEVICE_URI}" | sed "s|^${backend_name}":'||g' | sed 's|?.*$||')"
@@ -153,175 +241,176 @@ cmd="$(echo "${DEVICE_URI}" | sed "s|^${backend_name}":'||g' | sed 's|?.*$||')"
# Getting the raw options, arguments, ... from $DEVICE_URI:
argstr="$(echo "${DEVICE_URI}" | sed -n 's|^[^?]*?\(.*\)$|\1|p')"
-# Creating and retrieving the name of the temporary file, for CUPS print output:
-# tmpfile="$(mktemp --tmpdir=/tmp --suffix=.ps "${backend_name}_job-${jobid}_${jobtitle}.XXXXX")"
-tmpfile="/tmp/${backend_name}_job-${jobid}_${jobtitle}.ps"
-
# Taking the raw options, arguments, ... apart:
IFS='&' read -r -a argv <<< "${argstr}"
-# Parsing the options, arguments, ...:
+# Parse the options:
cmd_args=()
unset user
unset group
unset display
+unset filetype
# Have this as function that we will call, so that we can make use of 'shift'.
parse_argv() {
- log_local "Parsing options ${@@Q}."
+ replace_strings() {
+ cat | replace '%%' '%' '%.' ' ' '%_' '-' '%P' '|' '%B' '\' '%H' '#' '%Q' '?' '%A' '&' '%C' ':' '%G' '"' '%T' "'" '%s' "${outfile}"
+ }
while [ $# -gt 0 ]; do
- case "$1" in
+ _arg="$1"
+ case "${_arg}" in
u=*)
- user="${1#u=}"
- log_local " Parsing 'u='-option. User: '${user}'."
+ user="${_arg#u=}"
+ if [ "X${user}" == "X.CUPSUSER" ]; then
+ user="${cupsuser}"
+ fi
+ log_local " Parsed 'u='-option. User: '${user}'."
;;
g=*)
- group="${1#g=}"
- log_local " Parsing 'g='-option. Group: '${group}'."
+ group="${_arg#g=}"
+ log_local " Parsed 'g='-option. Group: '${group}'."
;;
D=*)
- display="${1#D=}"
- log_local " Parsing 'D='-option. Display: '${display}'."
+ display="${_arg#D=}"
+ log_local " Parsed 'D='-option. Display: '${display}'."
+ ;;
+ t=*)
+ filetype="${_arg#t=}"
+ log_local " Parsed 't='-option. filetype: '${filetype}'."
;;
*)
- arg=("$(echo "$1" | replace '%%' '%' '%s' "${tmpfile}" '%.' ' ' '%_' '-' '%P' '|' '%B' '\' '%H' '#' '%Q' '?' '%A' '&' )")
- log_local " Parsing next argument. It will be: '${arg}'."
+ arg="${_arg}"
+ log_local " Parsed next argument. It is: '${arg}'."
cmd_args+=("${arg}")
;;
esac
shift
done
- log_local ""
}
-
+log_local "Parsing options from DEVICE_URI: ${argv@Q} ..."
parse_argv "${argv[@]}"
+log_local "Parsed options."
+log_local ""
+
+# If filetype has not been specified, then default to PostScript:
+if [ ! -v filetype ]; then
+ filetype='ps'
+fi
+
+# Set outfile:
+outfile="${outfile_prefix}.${filetype}"
+
+# Now, after $outfile is set, do the string replacements:
+if [ -v cmd ]; then cmd="$(echo "${cmd}" | replace_strings)"; fi
+if [ -v user ]; then user="$(echo "${user}" | replace_strings)"; fi
+if [ -v group ]; then group="$(echo "${group}" | replace_strings)"; fi
+if [ -v display ]; then display="$(echo "${display}" | replace_strings)"; fi
+cmd_args_replaced=()
+for _arg in "${cmd_args[@]}"; do
+ cmd_args_replaced+=("$(echo "${_arg}" | replace_strings)")
+done
+
### Log some debugging information
-log_local "--- Some debugging information. ---"
+log_local "--- Some information: ---"
+log_local ""
log_local "We were called as:"
log_local " $0 $@"
log_local ""
log_local "Number of command line arguments passed to us: $#".
log_local ""
log_local "Information passed from CUPS:"
-log_local " - backend=${backend}"
-log_local " - jobid=${jobid}"
-log_local " - cupsuser=${cupsuser}"
-log_local " - jobtitle=${jobtitle}"
-log_local " - jobcopies=${jobcopies}"
-log_local " - joboptions=${joboptions}"
-log_local " - jobfile=${jobfile}"
-log_local " - DEVICE_URI=${DEVICE_URI}"
+log_local " - backend: ${backend}"
+log_local " - jobid: ${jobid}"
+log_local " - cupsuser: ${cupsuser}"
+log_local " - jobtitle: ${jobtitle}"
+log_local " - jobcopies: ${jobcopies}"
+log_local " - joboptions: ${joboptions}"
+log_local " - jobfile: ${jobfile}"
+log_local " - DEVICE_URI: ${DEVICE_URI}"
log_local ""
-log_local "Information we extracted from \$DEVICE_URI":
-log_local " - command=${cmd}"
-log_local " - argstr=${argstr}"
-log_local " - #argv=${#argv[@]}"
-log_local " - argv=${argv[@]}"
-log_local " - #cmd_args=${#cmd_args[@]}"
-log_local " - cmd_args=${cmd_args[@]}"
-log_local " - tmpfile=${tmpfile}"
+log_local "Executable and arguments specified via \$DEVICE_URI":
+log_local " - command: ${cmd}"
+log_local " - command arguments: ${cmd_args_replaced[@]}"
+log_local " - # of command arguments: ${#cmd_args_replaced[@]}"
log_local ""
-log_local "Environment:"
-log_local "$(env)"
+log_local "Variables extracted from \$DEVICE_URI:"
+if [ -v user ]; then log_local " - user: ${user}"; else log_local " - (Variable 'user' is not set.)"; fi
+if [ -v group ]; then log_local " - group: ${group}"; else log_local " - (Variable 'group' is not set.)"; fi
+if [ -v display ]; then log_local " - display: ${display}"; else log_local " - (Variable 'display' is not set.)"; fi
+if [ -v filetype ]; then log_local " - filetype: ${filetype}"; else log_local " - (Variable 'filetype' is not set.)"; fi
+log_local ""
+log_local "Variables specified in the configuration file:"
+log_local " - su_variant: ${su_variant}"
+log_local " - askpass_cmd: ${askpass_cmd}"
+log_local " - image_converter: ${image_converter}"
+log_local ""
+log_local "Files:"
+log_local " - conffile: ${conffile}"
+log_local " - logfile: ${logfile}"
+log_local " - tmpfile: ${tmpfile}"
+log_local " - outfile: ${outfile}"
+log_local ""
+# log_local "Environment:"
+# log_local "$(env)"
+# log_local ""
+log_local "--- End of some information. ---"
log_local ""
-### Determine our operation mode, and handle the modes.
-case ${#} in
- 0)
- # This case is for "backend discovery mode."
- echo "direct ${backend_name} \"Unknown\" \"cups-programme: Print to file and open in programme. DeviceURI example: ${backend_name}:/usr/bin/gimp?u=<user>&D=:0.0&%s\". See documentation!"
- exit 0
- ;;
- 5)
- # Backend needs to read from stdin if number of arguments is 5.
- log_cups "Writing print job ${jobid} output to ${tmpfile} ..."
- cat - > "${tmpfile}"
- # Make it world read- and writeable, to be able to work with it as another user.
- chmod 666 "${tmpfile}"
- log_cups "Print job ${jobid} output written to ${tmpfile}."
- ;;
- 6)
- # Backend needs to read from file if number of arguments is 6.
- log_cups "Writing print job ${jobid} output to ${tmpfile} ..."
- cat "${6}" > "${tmpfile}"
- # Make it world read- and writeable, to be able to work with it as another user.
- chmod 666 "${tmpfile}"
- log_cups "Print job ${jobid} output written to ${tmpfile}."
- ;;
- 1)
- case "$1" in
- '-V'|'-version'|'--version')
- echo "${VERSION}"
- exit 0
- ;;
- *)
- # This is dirty, since this section is twice in this script. Fix it.
- stderr "$0: Error: Called with incorrect number ($#) of arguments."
- stderr ""
- stderr "Usage:"
- stderr ""
- stderr " $0 <job-ID> <user> <jobtitle> <copies> <joboptions> [<jobfile>]"
- stderr ""
- stderr "OR (for backend discovery mode):"
- stderr ""
- stderr " $0"
- stderr ""
- stderr "OR (to retrieve the version number):"
- stderr ""
- stderr " $0 -V | -version | --version"
- stderr ""
- stderr ""
- stderr "(Install this as CUPS backend to '/usr/lib/cups/backend/${backend_name}'.)"
- stderr "(Read the comment in $0 to see how to set this up.)"
-
- error "$0 called with $# arguments. Unsupported mode. Aborting."
-
- exit 11
- ;;
- esac
- ;;
- 2|3|4|*)
- # This is dirty, since this section is twice in this script. Fix it.
- stderr "$0: Error: Called with incorrect number ($#) of arguments."
- stderr ""
- stderr "Usage:"
- stderr ""
- stderr " $0 <job-ID> <user> <jobtitle> <copies> <joboptions> [<jobfile>]"
- stderr ""
- stderr "OR (for backend discovery mode):"
- stderr ""
- stderr " $0"
- stderr ""
- stderr "OR (to retrieve the version number):"
- stderr ""
- stderr " $0 -V | -version | --version"
- stderr ""
- stderr ""
- stderr "(Install this as CUPS backend to '/usr/lib/cups/backend/${backend_name}'.)"
- stderr "(Read the comment in $0 to see how to set this up.)"
-
- error "$0 called with $# arguments. Unsupported mode. Aborting."
-
- exit 11
- ;;
+### Convert CUPS' output. $outfile should already be set (directly after parsing $DEVICE_URI).
+case "${filetype}" in
+ 'ps')
+ notice "Keeping PostScript format, '${outfile}'."
+ mv -f "${tmpfile}" "${outfile}"
+ ;;
+ 'pdf')
+ notice "Converting to pdf '${outfile}' ..."
+ ps2pdf "${tmpfile}" "${outfile}"
+ rm -f "${tmpfile}"
+ ;;
+ 'svg')
+ notice "Converting to svg '${outfile}' ..."
+ ps2pdf "${tmpfile}" "${tmpfile}.pdf"
+ pdf2svg "${tmpfile}.pdf" "${outfile}"
+ rm -f "${tmpfile}" "${tmpfile}.pdf"
+ ;;
+ 'png'|'jpg'|'gif'|'tif')
+ if [ ! -v image_converter ]; then
+ error "$0: A pixel graphic file type, '${filetype}', was requested to convert to. When converting to pixel graphic, 'image_converter' needs to be specified in '${conffile}', but it is not. Aborting."
+ exit 63
+ fi
+ case "${image_converter}" in
+ 'im')
+ _convert="convert"
+ ;;
+ 'gm')
+ _convert="gm convert"
+ ;;
+ *)
+ error "$0: Unsupported image_converter '${image_converter}'. Aborting."
+ exit 62
+ ;;
+ esac
+ case "${filetype}" in
+ 'png'|'jpg')
+ # ImageMagick will convert multiple page input to different layers in tif and gif, but not in png and jpg. To ensure only one output file with png and jpg, just process the first page of input in this case.
+ _pages='[0]'
+ ;;
+ esac
+ notice "Converting to ${filetype} '${outfile}' with '${_convert}' ..."
+ ${_convert} -density 300 "${tmpfile}${_pages}" "${outfile}"
+ rm -f "${tmpfile}"
+ ;;
+ *)
+ error "$0: File format '${filetype}' not supported. Aborting."
+ exit 61
+ ;;
esac
+log_local ""
-# Postpone the logging to cups until here, because only when we reached up to here we are correctly be running as a filter.
-info "$0 started at ${tstamp}. Our log file is: ${logfile}."
-
-# Unset variables to be read from $conffile first, to be safe from cases when they hang around in the environment.
-unset su_variant
-unset askpass_cmd
-if [ -e "${conffile}" ]; then
- log_local "Reading ${conffile}"
- . "${conffile}" || {
- error "$0: Error while reading ${conffile}."
- }
-else
- warn "$0: ${conffile} not present."
-fi
+# If possible, make the outfile write- and readable by all, for convenience (e.g. to easily remove it in case it is leftover).
+chmod a+wr "${outfile}" || true
@@ -361,7 +450,7 @@ if [ -v user ] || [ -v group ]; then
case "${su_variant}" in
'su')
su_cmd='su'
- su_opts="-c \"${cmd@Q} ${cmd_args[@]@Q}\""
+ su_opts="-c \"${cmd@Q} ${cmd_args_replaced[@]@Q}\""
if [ -v group ]; then
check_su_group_root
su_opts+=" -g ${group@Q}"
@@ -380,7 +469,7 @@ if [ -v user ] || [ -v group ]; then
if [ -v user ]; then
su_opts+=" -u ${user@Q}"
fi
- su_opts+=" ${cmd@Q} ${cmd_args[@]@Q}"
+ su_opts+=" ${cmd@Q} ${cmd_args_replaced[@]@Q}"
;;
'sudo-askpass')
if [ ! -v askpass_cmd ]; then
@@ -397,7 +486,7 @@ if [ -v user ] || [ -v group ]; then
if [ -v user ]; then
su_opts+=" -u ${user@Q}"
fi
- su_opts+=" ${cmd@Q} ${cmd_args[@]@Q}"
+ su_opts+=" ${cmd@Q} ${cmd_args_replaced[@]@Q}"
;;
'kdesu')
su_cmd='kdesu'
@@ -408,7 +497,7 @@ if [ -v user ] || [ -v group ]; then
if [ -v user ]; then
su_opts+=" -u ${user@Q}"
fi
- su_opts+=" -c ${cmd@Q} ${cmd_args[@]@Q}"
+ su_opts+=" -c ${cmd@Q} ${cmd_args_replaced[@]@Q}"
;;
'kdesudo')
su_cmd='kdesudo'
@@ -419,7 +508,7 @@ if [ -v user ] || [ -v group ]; then
if [ -v user ]; then
su_opts+=" -u ${user@Q}"
fi
- su_opts+=" -c ${cmd@Q} ${cmd_args[@]@Q}"
+ su_opts+=" -c ${cmd@Q} ${cmd_args_replaced[@]@Q}"
;;
*)
error "$0: Error: Unsupported su_variant '${su_variant}' provided. Aborting."
@@ -428,11 +517,12 @@ if [ -v user ] || [ -v group ]; then
esac
else
su_cmd='sh'
- su_opts="-c ${cmd@Q} ${cmd_args[@]@Q}"
+ su_opts="-c ${cmd@Q} ${cmd_args_replaced[@]@Q}"
fi
if [ -v display ]; then
log_local "exporting DISPLAY=${display}"
+ log_local ""
export DISPLAY="${display}"
fi
@@ -450,13 +540,16 @@ log_local ""
if [ ${cmd_exitcode} -eq 0 ]; then
notice "$0: Run of 'bash -c \"${su_cmd} ${su_opts} >> ${logfile@Q} 2>&1\"' finished with exitcode ${cmd_exitcode}."
notice "See '${logfile}' for combined stdout and stderr."
+ log_local ""
else
error "Run of 'bash -c \"${su_cmd} ${su_opts} >> ${logfile@Q} 2>&1\"' FAILED with exitcode ${cmd_exitcode}."
error "See '${logfile}' for combined stdout and stderr."
+ log_local ""
fi
-log_local "Removing ${tmpfile}."
-rm -f "${tmpfile}"
+log_local "Removing ${outfile}."
+log_local ""
+rm -f "${outfile}"
info "End of $0 run."
log_cups ''