home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / bin / foomatic-gswrapper < prev    next >
Encoding:
Text File  |  2006-06-19  |  2.2 KB  |  79 lines

  1. #!/usr/bin/perl
  2. # -*- perl -*-
  3. # $Revision: 3.4.2.6 $
  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. grep (m!\-sOutputFile=\-! 
  29.       && do {
  30.       # Send the job to fd 3; errors will be on 2(stderr) and job
  31.       # ps program interpreter output on 1(stdout).
  32.       $_ = '-sOutputFile=/dev/fd/3';
  33.       # quoted properly below...
  34.       }, @ARGV);
  35.  
  36. grep (((m!^\-$!) || (m!^\-_$!))
  37.       && do {
  38.       # Get the input from fd 0.
  39.       $_ = "/dev/fd/0";
  40.       }, @ARGV);
  41.  
  42. # Turn *off* -q (quiet!); now that stderr is useful! :)
  43. my @myargs = grep (! m!^\-q$!, @ARGV);
  44.  
  45. # Escape any quotes, and then quote everything just to be sure...
  46.  
  47. # Escaping a single quote inside single quotes is a bit complex as the shell
  48. # takes everything literal there. So we have to assemble it by concatinating
  49. # different quoted strings.
  50. # Finally we get e.g.: 'x'"'"'y' or ''"'"'xy' or 'xy'"'"'' or ...
  51. grep (s/\'/\'\"\'\"\'/g, @myargs);
  52. my $args = "'" . join("' '", @myargs) . "'";
  53.  
  54. # Execute Ghostscript, with both job and gs errors on stderr, and job
  55. # output on stdout...
  56.  
  57. print STDERR "foomatic-gswrapper: $gspath $args 3>&1 1>&2\n";
  58. exec "$gspath $args 3>&1 1>&2";
  59.  
  60. die "Failed to execute Ghostscript?!";
  61.  
  62. # Read the config file
  63.  
  64. sub readConfFile {
  65.     my ($file) = @_;
  66.  
  67.     my %conf;
  68.     # Read config file if present
  69.     if (open CONF, "< $file") {
  70.     while (<CONF>)
  71.     {
  72.         $conf{$1}="$2" if (m/^\s*([^\#\s]\S*)\s*:\s*(.*?)\s*$/);
  73.     }
  74.     close CONF;
  75.     }
  76.  
  77.     return %conf;
  78. }
  79.