diff options
author | Guilhem Moulin <guilhem.moulin@chalmers.se> | 2010-09-26 00:49:04 +0200 |
---|---|---|
committer | Guilhem Moulin <guilhem.moulin@chalmers.se> | 2010-09-26 00:49:04 +0200 |
commit | 857b93ab22d1edc2d73505d51462684a90851e64 (patch) | |
tree | 3cd23f0b18e9795ac5ca6d2d95ad48988c905b8e | |
parent | 25517524c4dc59513647a401e4c4a87a11a94bde (diff) |
Ugly trick, to avoid to seek into the input
-rwxr-xr-x | psresize2.pl | 57 |
1 files changed, 40 insertions, 17 deletions
diff --git a/psresize2.pl b/psresize2.pl index 483b413..b0e6748 100755 --- a/psresize2.pl +++ b/psresize2.pl @@ -1,5 +1,14 @@ #!/usr/bin/perl -w + +use Getopt::Long qw(:config no_ignore_case bundling); +use Pod::Usage; +use IPC::Open2; +use IPC::Open3; +use POSIX qw(floor mkfifo); +use strict; + + =head1 NAME psresize2.pl - a better I<psresize> @@ -105,14 +114,7 @@ Version: 0.1, 25 September 2010 =cut -use Getopt::Long qw(:config no_ignore_case bundling); -use Pod::Usage; -use IPC::Open2; -use IPC::Open3; -use POSIX qw(floor); -use strict; - - +my $tmpdir = "/tmp"; # # Options & arguments @@ -163,7 +165,7 @@ unless (defined $margin) { &papersize ("a4", \$outwidth, \$outheight) unless (defined $outwidth and defined $outheight); -# Default unit: point +# Default unit: PostScript point map {&topoints ($_)} ( \$outwidth, \$outheight, \$inwidth, \$inheight, \$margin ); @@ -177,17 +179,35 @@ if (lc $rotdir eq "left" or uc $rotdir eq 'L') { die "Unknown rotation direction: `$rotdir'" } +# Auxiliary files, to remove +my @auxfiles; # Open input and output files if (defined $infile && $infile ne "-") { - open *FIN, '<', "$infile" or die "Can't read `$infile': $!"; - + open FIN, '<', "$infile" or die "Can't read `$infile': $!"; } else { *FIN = *STDIN; + + # TODO: no need to such an ugly auxiliary file? + + $infile = "$tmpdir/psresize-stdin-" . int(rand 2**16) . "ps"; + + open FIN2, '>', "$infile" + or die "Can't write into `$infile': $!"; + push @auxfiles, "$infile"; + + # cat > $filename + while (<FIN>) { + print FIN2 $_ or die "Can't print: $!"; + } + close FIN2; + close FIN; + + open FIN, '<', "$infile" or die "Can't read `$infile': $!"; } if (defined $outfile && $outfile ne "-") { - open *FOUT, '>', "$outfile" or die "Can't create `$outfile': $!"; + open FOUT, '>', "$outfile" or die "Can't create `$outfile': $!"; } else { *FOUT = *STDOUT; } @@ -245,17 +265,15 @@ if (defined $crop) { @bbox = (0, 0, $inwidth, $inheight); } else { # Guess page size from the input file - - # Duplicate the input filehandle, to avoid to seek into it - open *FIN2, "<&FIN" or die "Can't dup FIN: $!"; - while (<FIN2>) { + + while (<FIN>) { if ($_ =~ m/^\%\%BoundingBox: (\d+) (\d+) (\d+) (\d+)/) { @bbox = ($1, $2, $3, $4); last; } } die "Cannot guess input page size!" unless @bbox; - close FIN2; + seek FIN, 0, 0 or die "$!"; } @@ -320,6 +338,9 @@ close FOUT; # No zombie processes waitpid $pid, 0; +# Delete auxiliary files +unlink @auxfiles; + # Useless, but Perl doesn't see that this filehandle is used more than # one time (and even automatically closed by `open3') exit 0; @@ -460,6 +481,8 @@ sub papersize { } } + + # # Print a command just like you'd do it in a shell # |