#!/usr/local/bin/perl5

# path 1

for $file (@ARGV) {
    open(C,"<$file");
    while(<C>) {
	if(/^(\w+)\(/) {
	    $cross{$1} = $file.".html#".$1;
	} elsif (/#define\s+(\w+)/) {
	    $cross{$1} = $file.".html#".$1;
	} 
    }
}

# create cross reference link substitution
#   Should not match partial string

$crossref = "sub crossref { \n";
for (reverse sort keys %cross) {
    $cross = $cross{$_};
    $cross =~ s/(\W)/\\$1/g;
    $crossref .= "s:(\\W)$_(\\W):\$1<a href=\"$cross\">$_</a>\$2:g;\n";
}
$crossref .= "}\n";
eval $crossref;

# path 2

for $file (@ARGV) {
    open(C,"<$file");
    open(HTML,">$file.html");
    print HTML "<html>\n";
    print HTML "<!-- Æþ¸ý -->\n";
    print HTML "<title>$file</title>\n";
    print HTML "<body>\n";
    print HTML "<pre>\n";
    while(<C>) {
	s/&/\&amp;/g;
	s/</\&lt;/g;
	s/>/\&gt;/g;
	# Do not substitute definition line
	if(/^(\w+)\(/) {
	    chop;
	    print HTML "<a name=\"",$1,"\"><font color=\"blue\">";
	    print HTML $_,"</font></a>\n";
	} elsif (/#define\s+(\w+)/) {
	    chop;
	    print HTML "<a name=\"",$1,"\"><font color=\"blue\">";
	    print HTML $_,"</font></a>\n";
	} else {
	    &crossref;    # calling program generated subroutine
	    print HTML;
	}
    }
    print HTML "</pre>\n";
    print HTML "</body>\n";
    print HTML "</html>\n";
}


__END__

=head1 NAME

cref.pl    Cross referenced C source generator

=head1 SYNOPSIS

    cref.pl  *.h *.c

=head1 DESCRIPTION

Generated cross referenced HTML file from C source code.

Function name definitions have to be started from the line top.
Otherwize we have to parse C type definitions in Perl.

=head1 AUTHOR

Shinji Kono, kono@ie.u-ryukyu.ac.jp

=cut
