diff options
-rwxr-xr-x | pscrop.pl | 32 |
1 files changed, 23 insertions, 9 deletions
@@ -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 = <FINPS2PS>) && $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 (<FINPS2PS>) { print FOUT $_ or die "Can't print: $!"; |