#!/usr/local/bin/perl

# Read header from stdin, print function names to stdout

while (<STDIN>)
{
    if ($line ne '') {
	$_ = $line . $_;
	$line = '';
    }

    if (/\\$/) {
	$line = $_;
	next;
    }

    $cpp = 1 if /^#.*ifdef.*cplusplus/;  # skip "C" declaration
    if ($cpp) {
	$cpp = 0 if /^#.*endif/;
	next;
    }

    next if (/^#/);                      # skip preprocessor directives

    s/\/\*.*?\*\///gs;                   # ignore comments
    s/{[^{}]*}//gs;                      # ignore {} blocks

    if (/{|\/\*/) {
	$line = $_;
    } else {
	$def .= $_;
    }
}

foreach (split /;/, $def) {
	s/^[\n\s]*//g;
    s/[\n\s]*$//g;
	if (/\(\*(\w*)\([^\)]+/) {
		print "$1\n";
    } elsif (/\w+\W+\w+\W*\(\s*\)$/s) {
	print STDERR "K&R declaration: $_;\n";
    } elsif (/\w+\W+\w+\W*\(.*\)$/s) {
	s/\)[\s\n]*$/\)/s;
	while (not /\(\)$/s) {
	    s/[^\(\)]*\)$/\)/s;
	    s/\([^\(\)]*\)\)$/\)/s;
	}
	s/\(void\)//;
	/(\w+)\W*\(\)/s;
	print "$1\n";
    } elsif (/\(/ and not (/=/ or /DECLARE_STACK/)) {
	print STDERR "Cannot parse: $_;\n";
    }
}
