summaryrefslogtreecommitdiff
path: root/foomatic-gswrapper.in
blob: e6b104937b393964d9e4dd0e92d083d9045e8ddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!@PERL@
# -*- perl -*-
# $Revision$

# This is a little Ghostscript regularization script.  It massages
# arguments to make Ghostscript execute properly as a filter, with
# output on stdout and errors etc on stderr.

# Arbitrary other option processing could happen here, too.

# IT WOULD BE WRONG to have this file do any processing of the input
# or output data.  Such job transforms belong in actual filters, or
# inside Ghostscript itself.

my $prefix = "@prefix@";
my $configpath = "@sysconfdir@/foomatic";
# Read config file if present
%conf = readConfFile("$configpath/filter.conf");

# Set GhostScript path
my $gspath = "gs";
$gspath = $conf{gspath} if defined(%conf) and defined $conf{gspath};
my $execpath = "@EXECPATH@";
# Get execution path from config file
$execpath = $conf{execpath} if defined(%conf) and defined $conf{execpath};
$ENV{'PATH'} = $execpath;

# Check whether we have a GhostScript version with redirection of the
# standard output of the PostScript programs via '-sstdout=%stderr'
my $gswithstdoutredirection = 0;
if (`$gspath -dQUIET -dPARANOIDSAFER -dNOPAUSE -dBATCH -dNOMEDIAATTRS -sDEVICE=pswrite -sstdout=%stderr -sOutputFile=/dev/null -c '(hello\n) print flush' 2>&1` =~ /hello/) {
    $gswithstdoutredirection = 1;
}

grep (m!\-sOutputFile=\-! 
      && do {
	  # If GhostScript does not support redirecting the standard output
	  # of the PostScript program to standard error with
	  # '-sstdout=%stderr', sen the job output data to fd 3; errors 
	  # will be on 2(stderr) and job ps program interpreter output on 
	  # 1(stdout).
	  $_ = ($gswithstdoutredirection ?
		'-sOutputFile=%stdout' : '-sOutputFile=/dev/fd/3');
	  # quoted properly below...
      }, @ARGV);

if (!$gswithstdoutredirection) {
    grep (((m!^\-$!) || (m!^\-_$!))
	  && do {
	      # Get the input from fd 0.
	      $_ = "/dev/fd/0";
	  }, @ARGV);
}

# Turn *off* -q (quiet!); now that stderr is useful! :)
my @myargs = grep (! m!^\-q$!, @ARGV);

# Escape any quotes, and then quote everything just to be sure...

# Escaping a single quote inside single quotes is a bit complex as the shell
# takes everything literal there. So we have to assemble it by concatinating
# different quoted strings.
# Finally we get e.g.: 'x'"'"'y' or ''"'"'xy' or 'xy'"'"'' or ...
grep (s/\'/\'\"\'\"\'/g, @myargs);
my $args = "'" . join("' '", @myargs) . "'";

# Execute Ghostscript, with both job and gs errors on stderr, and job
# output on stdout...

if ($gswithstdoutredirection) {
    $args = "'-sstdout=\%stderr' $args";
    print STDERR "foomatic-gswrapper: $gspath $args\n";
    exec "$gspath $args";
} else {
    print STDERR "foomatic-gswrapper: $gspath $args 3>&1 1>&2\n";
    exec "$gspath $args 3>&1 1>&2";
}

die "Failed to execute Ghostscript?!";

# Read the config file

sub readConfFile {
    my ($file) = @_;

    my %conf;
    # Read config file if present
    if (open CONF, "< $file") {
	while (<CONF>)
	{
	    $conf{$1}="$2" if (m/^\s*([^\#\s]\S*)\s*:\s*(.*?)\s*$/);
	}
	close CONF;
    }

    return %conf;
}