summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem.moulin@chalmers.se>2010-09-24 00:46:12 +0200
committerGuilhem Moulin <guilhem.moulin@chalmers.se>2010-09-24 00:46:12 +0200
commite6387ded99090017be2663a8639a9e8c7ef3978e (patch)
treefaf17d5630b991944348918056d057c7aecfae4b
parent6a2940bcf58d32c6d634e57ca67cb29f79e4b8b0 (diff)
Regular files as input
-rwxr-xr-xpscrop.pl32
1 files 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 = <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: $!";