#!/bin/awk -f
function pad(){
printf padstr
}
function gcolumn(s){
return substr(s, 1, index(s, "//")-1)
}
function expand(s, t){
t = s
gsub(" ", " ", t);
gsub(" +\t", " ", t);
return t
}
function prblock(end){
push = expand(push);
sub("^" expand(padstr), "", push)
sub("^//", "", push)
# prevent blank lines
if(end == 0 || !match(push, "^[ \t]*$")){
pad()
print " * " push
}
}
$0 ~ /\/\// {
if(lines > 0 && padstr !~ /^[ ]*$/){
sub("\/\/", "/*", push)
print push " */"
push = ""
lines = 0;
}
if(lines == 1){
padstr = gcolumn(push)
if(gcolumn($0) !~ /^[ ]*$/){
# comments on successive lines with code.
sub("\/\/", "/*", push)
print push " */"
push = ""
lines = 0;
}else{
pad(); print "/*"
prblock(1)
}
}else if(lines > 1)
prblock(0)
push = $0
lines++
next
}
lines > 0 {
if(lines == 1){
sub("\/\/", "/*", push)
print push " */"
}else{
prblock(1)
pad(); print " */"
}
lines = 0
}
{
print
}
END{
if(lines == 1){
gsub("\/\/", "/*", push)
print push " */"
}else if(lines>0){
prblock(2)
pad(); print " */"
}
}
|