home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-osu / up / parr < prev    next >
Encoding:
Text File  |  1993-07-14  |  2.6 KB  |  130 lines

  1. #!/usr/bin/perl
  2. #parr:
  3. # rearrange conforming PS code to print the pages in an arbitrary
  4. # order.  The -[sS] options (for signature order) assume two-up, left
  5. # to right.  The -o option takes a list of ranges, like this:
  6. #    1-5    1-10,11-20    11-,1-10
  7. # usage: parr [-o list] [-s] [-S n] [file]
  8. #
  9. # jgreely@cis.ohio-state.edu, 89/10/23
  10.  
  11. $order='';
  12. $signFlag='';
  13. $signCount=0;
  14. $DEBUG=0;
  15. $rangePrint=0;
  16. $TMPDIR='/tmp';
  17.  
  18. while ($_ = $ARGV[0],/^-/) {
  19.     shift;
  20.     last if /^-\-$/;
  21.     /^-o/ && ($order = shift,next);
  22.     /^-S/ && ($signCount = shift,$signFlag++,next);
  23.     /^-s/ && ($signFlag++,next);
  24.     /^-d/ && ($DEBUG++,next);
  25.     /^-r/ && ($rangePrint++,next);
  26.     die "usage: parr [-d] [-r] [-o list] [-s] [-S n] [file]\n";
  27. }
  28. if ($signFlag && $order) {
  29.     die "parr: -s and -o cannot be used together\n";
  30. }
  31.  
  32. $file = "$TMPDIR/p$$.header";
  33. @files = ($file);
  34. $sheet=0;
  35. open(file,">$file") ||
  36.   die "$file: $!, stopped";
  37. while (<>) {
  38.     #
  39.     # hack to use NeXT Preview: strip old '%%Pages:' lines
  40.     #
  41.     next if /^%%Pages:/;
  42.     if (/^%%Page:/) {
  43.         close(file);
  44.         $sheet++;
  45.         $file = "$TMPDIR/p$$.$sheet";
  46.         push(@files,$file);
  47.         open(file,">$file") ||
  48.           die "$file: $!, stopped";
  49.     }
  50.     if (/^%%Trailer/) {
  51.         close(file);
  52.         $file = "$TMPDIR/p$$.trailer";
  53.         push(@files,$file);
  54.         open(file,">$file") ||
  55.           die "$file: $!, stopped";
  56.     }
  57.     print file $_;
  58. }
  59. close(file);
  60.  
  61. @order = ();
  62. if ($order) {
  63.     foreach $range (split(/,/,$order)) {
  64.         ($start,$sep,$end) = split(/(-)/,$range);
  65.         $start = 1 unless $start;
  66.         $end = $sheet unless $end;
  67.         if ($sep) {
  68.             push(@order,$start..$end);
  69.         }else{
  70.             push(@order,$start);
  71.         }
  72.     }
  73. }elsif ($signFlag) {
  74.     if (! $signCount) {
  75.         $signCount = $sheet;
  76.         $signCount += (4 - $sheet % 4) if ($sheet % 4);
  77.     }else{
  78.         $signCount *=4;
  79.     }
  80.     for($base=0;$base<$sheet;$base+=$signCount) {
  81.         @tmp = ($signCount/2+$base);
  82.         push(@tmp,$tmp[0]+1,$tmp[0]+2,$tmp[0]-1);
  83.         while ($tmp[3] > $base) {
  84.             push(@order,@tmp);
  85.             @tmp = ($tmp[0]-2,$tmp[1]+2,$tmp[2]+2,$tmp[3]-2);
  86.         }
  87.     }
  88. }else{
  89.     @order = (1..$sheet);
  90. }
  91.  
  92. @tmp=@order;
  93. @order=();
  94. foreach $page (@tmp) {
  95.     push(@order,$page > $sheet ? "B" : $page);
  96. }
  97.  
  98. if ($rangePrint) {
  99.     print join(',',@order),"\n";
  100.     unlink @files unless $DEBUG;
  101.     exit(0);
  102. }
  103.  
  104. open(file,"$TMPDIR/p$$.header");
  105. $_ = <file>;
  106. print $_,"%%Pages: (atend)\n";
  107. print while <file>;
  108. close(file);
  109.  
  110. foreach $page (@order) {
  111.     $count++;
  112.     print "%%Page: ? $count\n%%OldPage: $page\n";
  113.     if ($page eq "B") {
  114.         print "showpage\n";
  115.     }else{
  116.         open(file,"$TMPDIR/p$$.$page");
  117.         while (<file>) {
  118.             print unless /^%%Page:/;
  119.         }
  120.         close(file);
  121.     }
  122. }
  123. open(file,"$TMPDIR/p$$.trailer");
  124. print while <file>;
  125. close(file);
  126. print "%%Pages: $count\n";
  127.  
  128. unlink @files unless $DEBUG;
  129. exit(0);
  130.