xv6/runoff1

103 lines
2.1 KiB
Text
Raw Normal View History

2006-09-08 13:53:18 +00:00
#!/usr/bin/perl
$n = 0;
2006-09-08 14:40:59 +00:00
$v = 0;
if($ARGV[0] eq "-v") {
$v = 1;
shift @ARGV;
}
2006-09-08 13:53:18 +00:00
if($ARGV[0] eq "-n") {
$n = $ARGV[1];
shift @ARGV;
shift @ARGV;
}
$n = int(($n+49)/50)*50 - 1;
2007-08-10 17:45:14 +00:00
$file = $ARGV[0];
2006-09-08 13:53:18 +00:00
@lines = <>;
2007-08-10 17:45:14 +00:00
$linenum = 0;
2006-09-08 13:53:18 +00:00
foreach (@lines) {
2007-08-10 17:45:14 +00:00
$linenum++;
2006-09-08 13:53:18 +00:00
chomp;
s/\s+$//;
if(length() >= 75){
2007-08-10 17:45:14 +00:00
print STDERR "$file:$linenum: line too long\n";
2006-09-08 13:53:18 +00:00
}
}
@outlines = ();
$nextout = 0;
for($i=0; $i<@lines; ){
# Skip leading blank lines.
$i++ while $i<@lines && $lines[$i] =~ /^$/;
last if $i>=@lines;
# If the rest of the file fits, use the whole thing.
if(@lines <= $i+50){
$breakbefore = @lines;
}else{
# Find a good next page break;
# Hope for end of function.
# but settle for a blank line (but not first blank line
# in function, which comes after variable declarations).
$breakbefore = $i;
$lastblank = $i;
$sawbrace = 0;
$breaksize = 15; # 15 lines to get to function
for($j=$i; $j<$i+50 && $j < @lines; $j++){
if($lines[$j] =~ /PAGEBREAK:\s*([0-9]+)/){
2006-09-08 14:40:59 +00:00
$breaksize = $1;
2006-09-08 13:53:18 +00:00
$breakbefore = $j;
$lines[$j] = "";
}
2006-09-08 14:19:30 +00:00
if($lines[$j] =~ /^};?$/){
2006-09-08 13:53:18 +00:00
$breakbefore = $j+1;
2006-09-08 14:40:59 +00:00
$breaksize = 15;
2006-09-08 13:53:18 +00:00
}
if($lines[$j] =~ /^{$/){
$sawbrace = 1;
}
if($lines[$j] =~ /^$/){
if($sawbrace){
$sawbrace = 0;
}else{
$lastblank = $j;
}
}
}
if($j<@lines && $lines[$j] =~ /^$/){
$lastblank = $j;
}
2006-09-08 14:40:59 +00:00
2006-09-08 13:53:18 +00:00
# If we are not putting enough on a page, try a blank line.
if($breakbefore - $i < 50 - $breaksize && $lastblank > $breakbefore && $lastblank >= $i+50 - 5){
2006-09-08 14:40:59 +00:00
if($v){
print STDERR "breakbefore $breakbefore i $i breaksize $breaksize\n";
}
2006-09-08 13:53:18 +00:00
$breakbefore = $lastblank;
$breaksize = 5; # only 5 lines to get to blank line
}
# If we are not putting enough on a page, force a full page.
if($breakbefore - $i < 50 - $breaksize && $breakbefore != @lines){
$breakbefore = $i + 50;
$breakbefore = @lines if @lines < $breakbefore;
}
if($breakbefore < $i+2){
$breakbefore = $i+2;
}
}
# Emit the page.
$i50 = $i + 50;
for(; $i<$breakbefore; $i++){
printf "%04d %s\n", ++$n, $lines[$i];
}
# Finish page
for($j=$i; $j<$i50; $j++){
printf "%04d \n", ++$n;
}
}