From e6387ded99090017be2663a8639a9e8c7ef3978e Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Fri, 24 Sep 2010 00:46:12 +0200 Subject: Regular files as input --- pscrop.pl | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/pscrop.pl b/pscrop.pl index c5cea04..37b3613 100755 --- a/pscrop.pl +++ b/pscrop.pl @@ -53,11 +53,23 @@ my ($opt_w, $opt_h) = (21.0, 29.7); # in centimeters, A4 my $units_per_cm = 72 * .3937; # 1 centimeters = .393700787 inches, 1 inch = 72 PostScript units5 -# TODO: not always STDIN -open *FIN, "<&STDIN"; +die "Too many arguments" if $#ARGV > 1; -#TODO: not always stdout -open FOUT, ">&STDOUT"; +my ($infile, $outfile) = @ARGV; + + +if (defined $infile && $infile ne "-") { + open *FIN, '<', "$infile" or die "Can't read `$infile': $!"; + +} else { + *FIN = *STDIN; +} + +if (defined $outfile && $outfile ne "-") { + open *FOUT, '>', "$outfile" or die "Can't create `$outfile': $!"; +} else { + *FOUT = *STDOUT; +} @@ -67,8 +79,8 @@ open FOUT, ">&STDOUT"; my $pid; -# need to duplicate STDIN, since it will be closed in the parent process -open *KIDFIN, "<&FIN"; +# need to duplicate FIN, since it will be closed in the parent process +open *KIDFIN, "<&FIN" or die "Can't dup FIN: $!"; $pid = open3 "<&KIDFIN", ">&FINGS", *FINGS, 'gs', '-sDEVICE=bbox', '-dBATCH', '-dNOPAUSE', '-' @@ -90,12 +102,12 @@ print STDERR "\n"; # No zombie processes waitpid $pid, 0; -die "Error calculating bounding box" if ($x0 >= $x1 || $y0 >= $y1); +die "Error when calculating bounding box" if ($x0 >= $x1 || $y0 >= $y1); my @bbox = ($x0, $y0, $x1, $y1); # Let's go back to the beginning of the input -seek FIN, 0, 0 or die "Can't seek!"; +seek FIN, 0, 0 or die "$!"; @@ -133,13 +145,15 @@ while (defined ($l = ) && $l ne "\%\%EndComments\n") { } # Important to print the document right -print FOUT << "EOF"; +# TODO: die "Can't print: $!" +print FOUT << "EOF"; \%\%EndComments \%\%BeginFeature: *PageSize ($w $h) << /PageSize [$w $h] >> setpagedevice \%\%EndFeature EOF + # Body while () { print FOUT $_ or die "Can't print: $!"; -- cgit v1.2.3