Plan 9 from Bell Labs’s /usr/web/sources/contrib/quanstro/bcmt

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#!/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 " */"
	}
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.