#!/usr/bin/perl
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Given a C function header, this routine does some rewriting to put it  into #
# a  more  tractable form.  It may also add the ":2 Fenter(...)" line, if the #
# appropriate instruction below isn't commented out. This routine is somewhat #
# ad hoc; it doesn't do a complete parse.  You can usually feed it the entire #
# header (function prototype, arg specs, and local declarations),  but  weird #
# instances may produce even weirder results.                                 #
#                                                                             #
# Some things we don't handle: The function's type can be on the same line or #
# on a separate line, but if you put only a '*' on the same  line,  it  won't #
# work.  There can't be any blank lines within the local variables; we insert #
# the Fenter() call at the first blank line, and it expands to a  declaration #
# plus  some  statements.   The  list of args must be on the same line as the #
# function name.  (We might have to handle this if ANSI C becomes  much  more #
# popular.)                                                                   #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$F = "FCT\t";	# Prefix to prepend to function header.
#$F = "";		# Alternative if you don't like "FCT".
print "\n";
while (<>) {
	if (/^( *\*)/) {
		s/^ *//;
		print;
		next;
	}
	if (/(.*)\s(\**)\s*(\w+)\s*\((.*)\)\s*$/) {	# type + fct.
		$fct = $3;
		$hdr = 1;
		print $F, $1, " ", $2, $3, "(", $4, ")\n";
		next;
	} 
	if (/^([A-Za-z_0-9]+)+\s*\((.*)\)\s*$/) {	# Fct name without type.
		$fct = $1;
		$hdr = 1;
		print $F, $1, "(", $2, ")\n";
		next;
	} 
	s/^\s*register\s+/\t/;	# Discard register declarations.
	if (/^\s*$/) {		# Blank line ends declarations.
		if ($fct && !$hdr) {	# Is this past the function header?
			print ":f	Fenter(\"$fct\");\n";
			print ":7	D7(m_Called);\n";
			$fct = 0;	# Don't do it again.
			$bdy = 1;	# Note we're in the function body.
		}
		next;
	} 
	if (/^:[0-9]	Fenter\(/) {
		next;
	}
	if (/^:[0-9]	D[0-9]\(/) {
		next;
	}
	if (/^\{\s*(.*)/) {	# Note start of function body.
		print "{\t$1\n";
		$hdr = 0;
		next;
	} 
	if ($hdr) {	# Adjust indentation to one tab.
		s/^\s*/\t/;
		print;
		next;
	} 
	if (/^\s*([*\/].*)/) {	# Don't indent comments.
		print "$1\n";
		next;
	} 
	if (/^\s*(.*)/) {	# Indent all declarations after the fct header.
		print "\t" if ($fct || $bdy);
		print "$1\n";
		next;
	}
	print;
}
if ($fct) {
	print ":f	Fenter(\"$fct\");\n";
	print ":5	D5(\"Called.\");\n";
	print ":f	Fexit;	# Move this to just before return.\n";
}
