home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / PSUTILS / EXTRACTR.CMD < prev    next >
OS/2 REXX Batch file  |  1994-01-14  |  3KB  |  99 lines

  1. extproc perl -Sx 
  2. #!perl
  3. # extractres: extract resources from PostScript file
  4. #
  5.  
  6. # feed this into perl
  7. eval 'exec perl -S $0 "$@"'
  8.    if $running_under_some_shell;
  9.  
  10. $prog = ($0 =~ s=.*/==);
  11.  
  12. %resources = ();        # list of resources included
  13. %merge = ();            # list of resources extracted this time
  14. %extn = ("font", ".pfa", "file", ".ps", "procset", ".ps", # resource extns
  15.      "pattern", ".pat", "form", ".frm", "encoding", ".enc");
  16. %type = ("%%BeginFile:", "file", "%%BeginProcSet:", "procset",
  17.      "%%BeginFont:", "font"); # resource types
  18.  
  19. while (@ARGV) {
  20.    $_ = shift;
  21.    if (/^-m(erge)?$/) { $merge = 1; }
  22.    elsif (/^-/) {
  23.       print STDERR "Usage: $prog [-merge] [file]\n";
  24.       exit 1;
  25.    } else {
  26.       unshift(@ARGV, $_);
  27.       last;
  28.    }
  29. }
  30.  
  31. if (defined($ENV{TMPDIR})) {    # set body file name
  32.    $body = "$ENV{TMPDIR}/body$$.ps";
  33. } else {
  34.    $body = "body$$.ps";
  35. }
  36.  
  37. open(BODY, $body) && die "Temporary file $body already exists";
  38. open(BODY, ">$body") || die "Can't write file $body";
  39.  
  40. sub filename {            # make filename for resource in @_
  41.    local($name);
  42.    foreach (@_) {        # sanitise name
  43.       s/[!()\$\#*&\\\|\`\'\"\~\{\}\[\]\<\>\?]//g;
  44.       $name .= $_;
  45.    }
  46.    $name =~ s@.*/@@;        # drop directories
  47.    die "Filename not found for resource ", join(" ", @_), "\n"
  48.       if $name =~ /^$/;
  49.    $name;
  50. }
  51.  
  52. $output = STDOUT;        # start writing header out
  53. while (<>) {
  54.    if (/^%%BeginResource:/ || /^%%BeginFont:/ || /^%%BeginProcSet:/) {
  55.       local($comment, @res) = split(/\s+/); # look at resource type
  56.       local($type) = defined($type{$comment}) ? $type{$comment} : shift(@res);
  57.       local($name) = &filename(@res, $extn{$type}); # make file name
  58.       $saveout = $output;
  59.       if (!$resources{$name}) {
  60.      print "%%IncludeResource: $type ", join(" ", @res), "\n";
  61.      if (!open(RES, $name)) {
  62.         open(RES, ">$name") || die "Can't write file $name";
  63.         $resources{$name} = $name;
  64.         $merge{$name} = $merge;
  65.         $output = RES;
  66.      } else {        # resource already exists
  67.         close(RES);
  68.         undef $output;
  69.      }
  70.       } elsif ($merge{$name}) {
  71.      open(RES, ">>$name") || die "Can't append to file $name";
  72.      $output = RES;
  73.       } else {            # resource already included
  74.      undef $output;
  75.       }
  76.    } elsif (/^%%EndResource/ || /^%%EndFont/ || /^%%EndProcSet/) {
  77.       if (defined $output) {
  78.      print $output $_;
  79.      close($output);
  80.       }
  81.       $output = $saveout;
  82.       next;
  83.    } elsif ((/^%%EndProlog/ || /^%%BeginSetup/ || /^%%Page:/)) {
  84.       $output = BODY;
  85.    }
  86.    print $output $_
  87.       if defined $output;
  88. }
  89.  
  90. close(BODY);            # close body output file
  91.  
  92. open(BODY, $body);        # reopen body for input
  93. while (<BODY>) {        # print it all
  94.    print $_;
  95. }
  96. close(BODY);
  97.  
  98. unlink($body);            # dispose of body file
  99.