home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / foomatic-gswrapper < prev    next >
Encoding:
Text File  |  2007-03-27  |  3.0 KB  |  98 lines

  1. #!/usr/bin/perl
  2. # -*- perl -*-
  3. # $Revision$
  4.  
  5. # This is a little Ghostscript regularization script.  It massages
  6. # arguments to make Ghostscript execute properly as a filter, with
  7. # output on stdout and errors etc on stderr.
  8.  
  9. # Arbitrary other option processing could happen here, too.
  10.  
  11. # IT WOULD BE WRONG to have this file do any processing of the input
  12. # or output data.  Such job transforms belong in actual filters, or
  13. # inside Ghostscript itself.
  14.  
  15. my $prefix = "/usr";
  16. my $configpath = "/etc/foomatic";
  17. # Read config file if present
  18. %conf = readConfFile("$configpath/filter.conf");
  19.  
  20. # Set GhostScript path
  21. my $gspath = "gs";
  22. $gspath = $conf{gspath} if defined(%conf) and defined $conf{gspath};
  23. my $execpath = "/usr/bin:/usr/local/bin:/usr/bin:/bin";
  24. # Get execution path from config file
  25. $execpath = $conf{execpath} if defined(%conf) and defined $conf{execpath};
  26. $ENV{'PATH'} = $execpath;
  27.  
  28. # Check whether we have a GhostScript version with redirection of the
  29. # standard output of the PostScript programs via '-sstdout=%stderr'
  30. my $gswithstdoutredirection = 0;
  31. if (`$gspath -dQUIET -dPARANOIDSAFER -dNOPAUSE -dBATCH -dNOMEDIAATTRS -sDEVICE=pswrite -sstdout=%stderr -sOutputFile=/dev/null -c '(hello\n) print flush' 2>&1` =~ /hello/) {
  32.     $gswithstdoutredirection = 1;
  33. }
  34.  
  35. grep (m!\-sOutputFile=\-! 
  36.       && do {
  37.       # If GhostScript does not support redirecting the standard output
  38.       # of the PostScript program to standard error with
  39.       # '-sstdout=%stderr', sen the job output data to fd 3; errors 
  40.       # will be on 2(stderr) and job ps program interpreter output on 
  41.       # 1(stdout).
  42.       $_ = ($gswithstdoutredirection ?
  43.         '-sOutputFile=%stdout' : '-sOutputFile=/dev/fd/3');
  44.       # quoted properly below...
  45.       }, @ARGV);
  46.  
  47. if (!$gswithstdoutredirection) {
  48.     grep (((m!^\-$!) || (m!^\-_$!))
  49.       && do {
  50.           # Get the input from fd 0.
  51.           $_ = "/dev/fd/0";
  52.       }, @ARGV);
  53. }
  54.  
  55. # Turn *off* -q (quiet!); now that stderr is useful! :)
  56. my @myargs = grep (! m!^\-q$!, @ARGV);
  57.  
  58. # Escape any quotes, and then quote everything just to be sure...
  59.  
  60. # Escaping a single quote inside single quotes is a bit complex as the shell
  61. # takes everything literal there. So we have to assemble it by concatinating
  62. # different quoted strings.
  63. # Finally we get e.g.: 'x'"'"'y' or ''"'"'xy' or 'xy'"'"'' or ...
  64. grep (s/\'/\'\"\'\"\'/g, @myargs);
  65. my $args = "'" . join("' '", @myargs) . "'";
  66.  
  67. # Execute Ghostscript, with both job and gs errors on stderr, and job
  68. # output on stdout...
  69.  
  70. if ($gswithstdoutredirection) {
  71.     $args = "'-sstdout=\%stderr' $args";
  72.     print STDERR "foomatic-gswrapper: $gspath $args\n";
  73.     exec "$gspath $args";
  74. } else {
  75.     print STDERR "foomatic-gswrapper: $gspath $args 3>&1 1>&2\n";
  76.     exec "$gspath $args 3>&1 1>&2";
  77. }
  78.  
  79. die "Failed to execute Ghostscript?!";
  80.  
  81. # Read the config file
  82.  
  83. sub readConfFile {
  84.     my ($file) = @_;
  85.  
  86.     my %conf;
  87.     # Read config file if present
  88.     if (open CONF, "< $file") {
  89.     while (<CONF>)
  90.     {
  91.         $conf{$1}="$2" if (m/^\s*([^\#\s]\S*)\s*:\s*(.*?)\s*$/);
  92.     }
  93.     close CONF;
  94.     }
  95.  
  96.     return %conf;
  97. }
  98.