#!/usr/bin/perl -n
# Converts Hollerith comments to quoted strings.  For constants
# spanning > 1 line, an interactive mode is entered in which the
# user makes the conversion.
# usage hollerith.pl input-file > output-file
#
# Print comments without processing. 
#
(/^[cC]/) && (print);
(/^[cC]/) && (next);
#
# Don't print empty lines.
#
(/^[ \t\n]+$/) && (next);
#
# Translate every line into a lower case.
#
$_=~ tr/A-Z/a-z/;
#
# change all \w continuation sysmbols to $ .
#
$_=~ s/^(\s{5,5})\w/$1\$/;
#
# Consider lines starting with "format" or continuation of format lines.
#
if (/^[\s\d]+format/ || $contd){
#
# Print out a prompt for a user to make corrections manually.
#
  if($manual){
   print STDERR "Change this line manually (End editing with Ctrl-D):\n";
   print STDERR;
   read(STDIN,$mline,73);
   print $mline;
   $manual=0;
   next;
   }
  $contd = 1;
   chop;
   $matched="";
   $line = $_;
   while ($line){
#
# find the first Hollerith constant in a line.
#
   if ($line =~ /([,\/]\s*|\W)(\d+)h/){
#   if a Hollerith constant extends for more than a line change it of the
#   next line manually.
	 ($2 > length($')) && ($hlen=length($')) || ($hlen=$2);
	 ($2 > length($')) && ($manual=1);
#
# implement changes of Hollerith constant.
# 
	 $changed = '"'.substr($',0,$hlen).'"';
     $matched = $matched.$`.$1.$changed;
#
	 ($manual and length($matched)<73)&&($matched .= ',');
#
# assign to $line everithing after a Hollerith constunt just processed.
#
	 $line=substr($',$hlen);
   }
#
# the rest of the line is  unchanged if no more Hollerith constant in a
# line.
#
   else { $matched = $matched.$line;
		  $line = 0;} 
   }
   print "$matched\n";
#
# Check whether format ends on the current line.
#
  ($_ =~ /\)\s+$/) && ($contd = 0);
  next;
}   
print;
