summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem.moulin@chalmers.se>2010-09-23 09:15:51 +0200
committerGuilhem Moulin <guilhem.moulin@chalmers.se>2010-09-23 09:15:51 +0200
commitfcd4b6491cda9843ea44b6cd65959b3cf14126e6 (patch)
tree229c2ebd04944e4293a482c87b77a4b4e2269b51
parent7c2073be62cd72779291943ead8beed5b7876d36 (diff)
No more seek problem :)
-rwxr-xr-xpdftool.pl119
1 files changed, 47 insertions, 72 deletions
diff --git a/pdftool.pl b/pdftool.pl
index 55b13bb..c5acd4a 100755
--- a/pdftool.pl
+++ b/pdftool.pl
@@ -7,22 +7,23 @@ use strict;
Getopt::Long::Configure ("bundling");
-my $pdf2ps = 'pdftops';
-my $pscrop = 'psnup2.pl -s1 -l1';
-my $psresize = 'psresize';
-my $psnup = 'psnup';
-my $psbook = 'psbook';
-my $ps2pdf = 'ps2pdf';
+# Give an array if the command has any argument.
+my @pdf2ps = 'pdftops';
+my @pscrop = ('psnup2.pl', '-s1', '-l1');
+my @psresize = 'psresize';
+my @psnup = 'psnup';
+my @psbook = 'psbook';
+my @ps2pdf = 'ps2pdf';
-my $tmpdir = '/tmp';
+my $tmpdir = '/tmp';
-my $papersize;
my $nup = 1;
my $margin = 1;
#TODO: units
my $crop;
my $book;
+my $papersize;
GetOptions( "nup|n=i" => \$nup,
"1" => sub { $nup = 1 },
@@ -93,15 +94,16 @@ if (defined $fstline && $fstline =~ /%!PS.*/) {
die "Can't recognize the type of `$filename2'";
}
-# auxiliary files, to remove
+# Auxiliary files, to remove
my @auxfiles;
-# auxiliary file descriptors, to close
+# Auxiliary file descriptors, to close
my @auxfds;
# Pids, to waid for
my @pids;
+my $pid;
if ($filename eq "-") {
# Need to copy the whole input to an auxiliary file, since
@@ -109,7 +111,7 @@ if ($filename eq "-") {
# TODO: only for pdfs
$filename = "$tmpdir/pdftool-stdin-" . int(rand 2**16) . lc ".$filetype";
- open(AUXFD, '>', "$filename")
+ open AUXFD, '>', "$filename"
or die "Can't read write into `$filename'";
push @auxfiles, "$filename";
@@ -118,94 +120,67 @@ if ($filename eq "-") {
while (<FILE>) {
print AUXFD $_;
}
- close(AUXFD);
+ close AUXFD;
}
-close(FILE);
+close FILE;
# Convert to ps, or read the input file
if ($filetype eq "PDF") {
- open *PSFILE, '|-', "$pdf2ps", "$filename", "-"
-# open2 PSFILE, '<&STDIN', "$pdf2ps $filename -"
- or die "Error with `$pdf2ps $filename -'";
+ $pid = open *INPS, "-|", @pdf2ps, "$filename", '-'
+ or die "Error with `@pdf2ps $filename -'";
+ push @pids, $pid;
} else {
- open *PSFILE, "$filename"
+ open *INPS, '<', "$filename"
or die "Can't open `$filename'";
}
-push @auxfds, fileno PSFILE;
+push @auxfds, fileno INPS;
-my $pid;
# Resize file to our papersize
-$pid = open2 *PSRESIZE, "<&PSFILE", "$psresize", "-p$papersize"
- or die "Error with `$psresize -p$papersize'";
-#push @auxfds, fileno PSRESIZE;
+$pid = open2 *PSRESIZE, "<&INPS", @psresize, "-p$papersize"
+ or die "Error with `@psresize -p$papersize'";
+# Note: open2 closes the filehandles for us :)
push @pids, $pid;
-# pscrop
-*PSCROP = *PSRESIZE;
+# Pscrop
+if (defined $crop) {
+ $pid = open2 *PSCROP, "<&PSRESIZE", @pscrop, "-p$papersize", "-m${mnup}"
+ or die "Error with `@pscrop -p$papersize -m${mnup}cm'";
+ push @pids, $pid;
+} else {
+ *PSCROP = *PSRESIZE;
+}
-# psbook
-*PSBOOK = *PSCROP;
+# Psbook
+if (defined $crop) {
+ $pid = open2 *PSBOOK, "<&PSCROP", "@psbook"
+ or die "Error with `@psbook";
+ push @pids, $pid;
+} else {
+ *PSBOOK = *PSCROP;
+}
-# psnup
+# Psnup
# TODO: sometimes unecessary
-$pid = open2 *PSNUP, "<&PSBOOK", "$psnup", "-p$papersize", "-m$mnup", "-$nup"
- or die "Error with `$psnup -p$papersize -m$mnup -$nup'";
-#push @auxfds, fileno PSNUP;
+$pid = open2 *PSNUP, "<&PSBOOK", @psnup, "-p$papersize", "-m${mnup}cm", "-$nup"
+ or die "Error with `@psnup -p$papersize -m${mnup}cm -$nup'";
push @pids, $pid;
# TODO: not always stdout
-$pid = open2 ">&STDOUT", "<&PSNUP", "$ps2pdf -sPAPERSIZE=$papersize - -"
- or die "Error with `$ps2pdf -sPAPERSIZE=$papersize - -'";
+$pid = open2 ">&STDOUT", "<&PSNUP", @ps2pdf, "-sPAPERSIZE=$papersize", '-', '-'
+ or die "Error with `@ps2pdf -sPAPERSIZE=$papersize - -'";
push @pids, $pid;
-# avoid zombies
+# Avoid zombies
map {waitpid $_, 0} @pids;
-# close auxiliary filehandles
+# Close auxiliary filehandles
map {close $_} @auxfds;
-# delete auxiliary files
+# Delete auxiliary files
unlink @auxfiles;
-
-
-#TODO: better do forget stdin, I guess
-#if ($filename eq "-" && $filetype eq "PDF") {
-#}
-
-#if ($filetype eq "PDF") {
-# $command .= "$pdf2ps $filename - | ";
-#} else {
-# $command .= "cat $filename | ";
-#}
-#
-#
-##$command .= "$psresize -p$papersize /tmp/paf.ps | ";
-## useless, since the argument of $pscrop has to be seekable
-#
-#if (defined $crop) {
-# $command .= "$pscrop -m$margin /tmp/paf.ps | ";
-#} else {
-# $command .= "cat /tmp/paf.ps | ";
-#}
-#
-#$command .= "$psnup -p$papersize -m${margin}cm -$nup | ";
-#
-#$command .= "$psbook | " if defined $book;
-#
-#$command .= "$ps2pdf -sPAPERSIZE=$papersize - - ";
-#
-#
-#$command .= "> /tmp/pouf.pdf";
-#
-#print "$command", "\n";
-
-#system $command;
-
-
-#system ($pdfviewer, "/tmp/pouf.pdf");