1#!/usr/bin/perl 2 3$n = 0; 4$v = 0; 5if($ARGV[0] eq "-v") { 6 $v = 1; 7 shift @ARGV; 8} 9if($ARGV[0] eq "-n") { 10 $n = $ARGV[1]; 11 shift @ARGV; 12 shift @ARGV; 13} 14$n = int(($n+49)/50)*50 - 1; 15 16$file = $ARGV[0]; 17@lines = <>; 18$linenum = 0; 19foreach (@lines) { 20 $linenum++; 21 chomp; 22 s/\s+$//; 23 if(length() >= 75){ 24 print STDERR "$file:$linenum: line too long\n"; 25 } 26} 27@outlines = (); 28$nextout = 0; 29 30for($i=0; $i<@lines; ){ 31 # Skip leading blank lines. 32 $i++ while $i<@lines && $lines[$i] =~ /^$/; 33 last if $i>=@lines; 34 35 # If the rest of the file fits, use the whole thing. 36 if(@lines <= $i+50 && !grep { /PAGEBREAK/ } @lines){ 37 $breakbefore = @lines; 38 }else{ 39 # Find a good next page break; 40 # Hope for end of function. 41 # but settle for a blank line (but not first blank line 42 # in function, which comes after variable declarations). 43 $breakbefore = $i; 44 $lastblank = $i; 45 $sawbrace = 0; 46 $breaksize = 15; # 15 lines to get to function 47 for($j=$i; $j<$i+50 && $j < @lines; $j++){ 48 if($lines[$j] =~ /PAGEBREAK!/){ 49 $lines[$j] = ""; 50 $breakbefore = $j; 51 $breaksize = 100; 52 last; 53 } 54 if($lines[$j] =~ /PAGEBREAK:\s*([0-9]+)/){ 55 $breaksize = $1; 56 $breakbefore = $j; 57 $lines[$j] = ""; 58 } 59 if($lines[$j] =~ /^};?$/){ 60 $breakbefore = $j+1; 61 $breaksize = 15; 62 } 63 if($lines[$j] =~ /^{$/){ 64 $sawbrace = 1; 65 } 66 if($lines[$j] =~ /^$/){ 67 if($sawbrace){ 68 $sawbrace = 0; 69 }else{ 70 $lastblank = $j; 71 } 72 } 73 } 74 if($j<@lines && $lines[$j] =~ /^$/){ 75 $lastblank = $j; 76 } 77 78 # If we are not putting enough on a page, try a blank line. 79 if($breakbefore - $i < 50 - $breaksize && $lastblank > $breakbefore && $lastblank >= $i+50 - 5){ 80 if($v){ 81 print STDERR "breakbefore $breakbefore i $i breaksize $breaksize\n"; 82 } 83 $breakbefore = $lastblank; 84 $breaksize = 5; # only 5 lines to get to blank line 85 } 86 87 # If we are not putting enough on a page, force a full page. 88 if($breakbefore - $i < 50 - $breaksize && $breakbefore != @lines){ 89 $breakbefore = $i + 50; 90 $breakbefore = @lines if @lines < $breakbefore; 91 } 92 93 if($breakbefore < $i+2){ 94 $breakbefore = $i+2; 95 } 96 } 97 98 # Emit the page. 99 $i50 = $i + 50; 100 for(; $i<$breakbefore; $i++){ 101 printf "%04d %s\n", ++$n, $lines[$i]; 102 } 103 104 # Finish page 105 for($j=$i; $j<$i50; $j++){ 106 printf "%04d \n", ++$n; 107 } 108} 109