#!/usr/bin/perl # # $Id: prettify,v 1.3 1997/05/24 20:47:34 joi Exp joi $ # $Log: prettify,v $ # Revision 1.3 1997/05/24 20:47:34 joi # Added keyword test. # # Revision 1.2 1997/05/24 13:38:50 joi # *** empty log message *** # # Revision 1.1 1997/04/26 15:28:20 joi # Initial revision # # # this perl script reads a tf log file with the following assumptions: # /set wraplog=on # /set wrapspace=2 # # it produces a log file compatible with Kelson's parser, or with # one I'll write after I write *this* thing. # # The wrapspace setting is required because this tool expects to see # all continuation lines indented by that amount. # $prevline = ""; $iscomment = 0; $indent_step = 2; $wrap_width = 74; unless (@ARGV) { @ARGV[0]=".*"; } while () { $match = 0; foreach $pattern (@ARGV) { if (/$pattern/) { $match = 1; last; } } next unless $match; chop; $current_indent = 0; &prettyprint ($prevline,$current_indent) if $prevline ne ""; if (/^[&@]/) { # new code line? print "-\n" if $prevline ne ""; # print separator $iscomment = 0; } elsif (/^#/) { $iscomment = 1; } elsif (/^ / && $iscomment) { print "# " if $prevline ne ""; } $prevline = $_; } if ($prevline ne "") { &prettyprint($prevline,$current_indent); print "-\n"; } sub prettyprint { local($line) = shift; local($current_indent) = shift; local($out_buffer,$command,$rest,$name,$rline); # # is it a simple command? # if ($line =~ /^\@[^=]*/ && length($line) < $wrap_width) { print $line,"\n"; return; } # # pick off the attribute name # # ($name,$line) = ($line =~ /([&@]\w+\s+\w+\s*=)(.*)/); # ($attrib,$name,$line) = ($line =~ /([&@]\S+)\s+(\S+\s*)*=(.*)/); ($attrib,$object,$code) = ($line =~ /([&@]\S+)\s+([^=]+)=(.*)/); print "$attrib $object="; unless ($code ne "") { print "\n"; return; } else { $line = $code; } # # is there a command in this one? # ($command,$rest) = ($line =~ /(\$[^:]+):(.*)/); if ($command) { print $command,":\n"; $line = $rest; } else { print "\n"; } $current_indent += $indent_step; # # ok, just print it. # $rline = reverse $line; # print &indent($current_indent); $out_buffer = ""; while ($rline ne "") { # read one char at a time. $char = chop($rline); if ($char eq "{" || $char eq "[") { &print_wrap($out_buffer,$current_indent,$wrap_width); $out_buffer = ""; $current_indent += $indent_step; &print_wrap($char,$current_indent,$wrap_width); $current_indent += $indent_step; } elsif ($char eq "}" || $char eq "]") { &print_wrap($out_buffer,$current_indent,$wrap_width); $out_buffer = ""; $current_indent -= $indent_step; &print_wrap($char,$current_indent,$wrap_width); $current_indent -= $indent_step; } elsif ($char eq ";") { $out_buffer .= ";"; &print_wrap($out_buffer,$current_indent,$wrap_width); $out_buffer = ""; } else { # print $char; $out_buffer .= $char; } } &print_wrap( "$out_buffer",$current_indent,$wrap_width) if $out_buffer ne ""; } sub print_wrap { local($line)=shift; local($current_indent) = shift; local($width) = shift; local($count,$i)=0; local($piece)=""; # # check for simple cases # unless (( $line =~ /%r/i) || ((length($line)+$current_indent) >= $wrap_width)) { &print_width($line,$current_indent,$wrap_width); return; } # # ok, tougher case, line will need to be wrapped. # while ($line ne "") { $line =~ s/\%R/\%r/g; # lowercase the %R $line = "$line"; $i = index($line,"%r",0); if ($i >= 0) { &print_width(substr($line,0,$i+2) ,$current_indent, $wrap_width); $line = substr($line,$i+2); } else { &print_width($line,$current_indent,$wrap_width); $line = ""; } } } sub print_width { local($line)=shift; local($ci)=shift; local($width)=shift; local($i,$c,$munge); # # simple case first # while ($line ne "") { if ((length($line)+$ci) <= $width) { $line =~ s/^ /\\ /; # prepend \ if needed. $line =~ s/ $/ \\/; # append \ if needed. print &indent($ci),$line,"\n"; $line = ""; } else { # $i = $wrap_width + $ci; $i = $wrap_width - $ci; $c = substr($line,$i,1); while ( $i && $c =~ /\w/) { $i--; $c = substr($line,$i,1); } $i = $wrap_width-$ci unless $i; # $c = substr($line,$i,1); # if ($c eq " ") { # $munge = "\\"; # } else { # $munge = "\\"; # } $piece = substr($line,0,$i); $line = substr($line,$i); $piece =~ s/^ /\\ /; # prepend \ if needed. $piece =~ s/ $/ \\/; # append \ if needed. print &indent($ci),$piece,"\n"; } } } sub indent { local($current_indent)=shift; local($l)=""; $l = ' ' x $current_indent; return $l; }