diff options
Diffstat (limited to 'Master/texmf-dist/scripts/psutils/extractres.pl')
-rwxr-xr-x | Master/texmf-dist/scripts/psutils/extractres.pl | 170 |
1 files changed, 93 insertions, 77 deletions
diff --git a/Master/texmf-dist/scripts/psutils/extractres.pl b/Master/texmf-dist/scripts/psutils/extractres.pl index adee938c95d..87f8377b34d 100755 --- a/Master/texmf-dist/scripts/psutils/extractres.pl +++ b/Master/texmf-dist/scripts/psutils/extractres.pl @@ -1,101 +1,117 @@ #!/usr/bin/env perl # extractres: extract resources from PostScript file +# includeres: add resources to a PostScript file # -# Copyright (C) Angus J. C. Duggan 1991-1995 +# (c) Reuben Thomas 2012 +# (c) Angus J. C. Duggan 1991-1997 # See file LICENSE for details. -$0 =~ s=.*/==; -$prog = $0; +use strict; +use warnings; -%resources = (); # list of resources included -%merge = (); # list of resources extracted this time -%extn = ("font", ".pfa", "file", ".ps", "procset", ".ps", # resource extns - "pattern", ".pat", "form", ".frm", "encoding", ".enc"); -%type = ("%%BeginFile:", "file", "%%BeginProcSet:", "procset", - "%%BeginFont:", "font"); # resource types +use File::Basename; +use Getopt::Long; -while (@ARGV) { - $_ = shift; - if (/^-m(erge)?$/) { $merge = 1; } - elsif (/^-/) { - print STDERR "Usage: $prog [-merge] [file]\n"; - exit 1; - } else { - unshift(@ARGV, $_); - last; - } -} - -if (defined($ENV{TMPDIR})) { # set body file name - $body = "$ENV{TMPDIR}/body$$.ps"; -} elsif (defined($ENV{TEMP})) { - $body = "$ENV{TEMP}/body$$.ps"; -} elsif (defined($ENV{TMP})) { - $body = "$ENV{TMP}/body$$.ps"; -} else { - $body = "body$$.ps"; -} +my $prog = basename($0); -open(BODY, $body) && die "Temporary file $body already exists"; -open(BODY, ">$body") || die "Can't write file $body"; +# Resource extensions +my %extn = ("font" => ".pfa", "file" => ".ps", "procset" => ".ps", + "pattern" => ".pat", "form" => ".frm", "encoding" => ".enc"); +# Resource types +my %type = ("%%BeginFile:" => "file", "%%BeginProcSet:" => "procset", + "%%BeginFont:" => "font"); sub filename { # make filename for resource in @_ - local($name); - foreach (@_) { # sanitise name - s/[!()\$\#*&\\\|\`\'\"\~\{\}\[\]\<\>\?]//g; - $name .= $_; - } - $name =~ s@.*/@@; # drop directories - die "Filename not found for resource ", join(" ", @_), "\n" - if $name =~ /^$/; - $name; + my $name; + foreach (@_) { # sanitise name + s/[!()\$\#*&\\\|\`\'\"\~\{\}\[\]\<\>\?]//g; + $name .= $_; + } + $name =~ s@.*/@@; # drop directories + die "$prog: filename not found for resource ", join(" ", @_), "\n" + if $name =~ /^$/; + return $name; } -$output = STDOUT; # start writing header out -while (<>) { - if (/^%%BeginResource:/ || /^%%BeginFont:/ || /^%%BeginProcSet:/) { - local($comment, @res) = split(/\s+/); # look at resource type - local($type) = defined($type{$comment}) ? $type{$comment} : shift(@res); - local($name) = &filename(@res, $extn{$type}); # make file name +sub extract { + my %resources = (); # list of resources included + my %merge = (); # list of resources extracted this time + + my $merge; + unless (GetOptions('merge' => \$merge)) { + print STDERR "Usage: $prog [-merge] [FILE]\n"; + exit 1; + } + + my $prolog = ""; + my $body = ""; + my $resource = ""; + my $output = \$prolog; + my $saveout; + while (<>) { + if (/^%%BeginResource:/ || /^%%BeginFont:/ || /^%%BeginProcSet:/) { + my ($comment, @res) = split(/\s+/); # look at resource type + my $type = defined($type{$comment}) ? $type{$comment} : shift(@res); + my $name = filename(@res, $extn{$type}); # make file name $saveout = $output; - if (!$resources{$name}) { - print "%%IncludeResource: $type ", join(" ", @res), "\n"; - if (!open(RES, $name)) { - open(RES, ">$name") || die "Can't write file $name"; - $resources{$name} = $name; - $merge{$name} = $merge; - $output = RES; - } else { # resource already exists - close(RES); - undef $output; - } + if (!defined($resources{$name})) { + $prolog .= "%%IncludeResource: $type " . join(" ", @res) . "\n"; + if (!-e $name) { + open RES, ">$name" || die "$prog: can't write file `$name'\n"; + $resources{$name} = ""; + $merge{$name} = $merge; + $output = \$resources{$name}; + } else { # resource already exists + close(RES); + undef $output; + } } elsif ($merge{$name}) { - open(RES, ">>$name") || die "Can't append to file $name"; - $output = RES; + open RES, ">>$name" || die "$prog: can't append to file `$name'\n"; + $resources{$name} = ""; + $output = \$resources{$name}; } else { # resource already included - undef $output; + undef $output; } - } elsif (/^%%EndResource/ || /^%%EndFont/ || /^%%EndProcSet/) { + } elsif (/^%%EndResource/ || /^%%EndFont/ || /^%%EndProcSet/) { if (defined $output) { - print $output $_; - close($output); + $$output .= $_; + print RES $$output; } $output = $saveout; next; - } elsif ((/^%%EndProlog/ || /^%%BeginSetup/ || /^%%Page:/)) { - $output = BODY; - } - print $output $_ - if defined $output; -} - -close(BODY); # close body output file + } elsif ((/^%%EndProlog/ || /^%%BeginSetup/ || /^%%Page:/)) { + $output = \$body; + } + $$output .= $_ if defined $output; + } -open(BODY, $body); # reopen body for input -while (<BODY>) { # print it all - print $_; + print $prolog . $body; } -close(BODY); -unlink($body); # dispose of body file +sub include { + while (<>) { + if (/^%%IncludeResource:/ || /^%%IncludeFont:/ || /^%%IncludeProcSet:/) { + my ($comment, @res) = split(/\s+/); + my $type = defined($type{$comment}) ? $type{$comment} : shift(@res); + my $name = filename(@res); + if (open(RES, $name) || open(RES, "$name$extn{$type}")) { + print do { local $/; <RES> }; + close(RES); + } else { + print "%%IncludeResource: ", join(" ", $type, @res), "\n"; + print STDERR "$prog: resource `$name' not found\n"; + } + } else { + print $_; + } + } +} +# Perform function according to name +if ($prog eq "extractres") { + extract(); +} elsif ($prog eq "includeres") { + include(); +} else { + die "$prog: I need to be called `extractres' or `includeres'\n"; +} |