#! /home/edsdev/net/bin/perl

sub Max {
    local($max) = pop(@_) ;
    foreach $foo (@_) {
	$max = $foo if $max < $foo ;
    }

    $max ;
}

sub DumpFunction {
    local($type, $func, $arglist) = @_ ;
    local($funclen) = length($func) ;
    local($spaces) = ' ' x $funclen ;
    local($argLen) = 0 ;
    local($incr) = 0 ;

    local(@args) = split( /,/, $arglist ) ;

    foreach $tmp (@args) {
	$elem = &Trim($tmp) ;
	$varIndx = &Max(rindex($elem, " "), rindex($elem, "*")) ;

	$varType = substr($elem, 0, $varIndx + 1) ;
	$varType =~ s/ \*/\*/g ;

	$argLen = &Max($argLen, length($varType)) + 3 ;
    }

    print "#ifdef __STDC__\n" ;
    print "$type\n" ;
    print "$func(" ;

    foreach $tmp (@args) {
	$elem = &Trim($tmp) ;
	$varIndx = &Max(rindex($elem, " "), rindex($elem, "*")) ;

	$varType = substr($elem, 0, $varIndx + 1) ;
	$varType =~ s/ \*/\*/g ;
	$varName = substr($elem, $varIndx + 1) ;
	
	if (! $incr) {
	    print " " ;
	} else {
	    print "\n${spaces}  " ;
	}

	print "${varType}" ;
	print " " x ($argLen - length($varType)) ;

	if ($incr == $#args) {
	    print "${varName}" ;
	} else {
	    print "${varName}," ;
	}
	
	$incr++ ;
    }

    print " )\n" ;
    print "#else /** __STDC__ **/\n" ;

    $incr = 0 ;

    print "$type\n" ;
    print "$func( " ;

    foreach $tmp (@args) {
	$elem = &Trim($tmp) ;
	$trailIndx = rindex($elem, "[");

	$varIndx = &Max(rindex($elem, " "), rindex($elem, "*")) ;
	if ($trailIndx == -1) {
	    $varName = substr($elem, $varIndx + 1) ;
	} else {
	    $varName = substr($elem, $varIndx + 1, $trailIndx - $varIndx - 1) ;
	}
	
	if ($incr == $#args) {
	    print "${varName}" ;
	} else {
	    print "${varName}, " ;
	}

	$incr++ ;
    }

    print " )\n" ;

    foreach $tmp (@args) {
	$elem = &Trim($tmp) ;
	$varIndx = &Max(rindex($elem, " "), rindex($elem, "*")) ;

	$varType = substr($elem, 0, $varIndx + 1) ;
	$varType =~ s/ \*/\*/g ;
	$varName = substr($elem, $varIndx + 1) ;
	
	print "    ${varType}" ;
	print " " x ($argLen - length($varType)) ;
	print "${varName} ;\n" ;
    }

    print "#endif /** __STDC__ **/\n" ;
}

sub Trim {
    local($trimString) = pop(@_) ;

    $trimString =~ s/(^[ ]+)// ;
    $trimString =~ s/([ ]+$)// ;

    return $trimString ;
}

sub ParseFile {
    local($fileName) = pop(@_) ;
    local($scope) = 0 ;
    local($comment) = 0 ;
    local($cpp) = 0 ;
    local($args) = 0 ;
    local($type) ;
    local($arglist) ;

    while(<STDIN>) {
	chop ;

	$cpp = 0 ;

	if (/^\#/) {
	    $cpp = 1 ;
	};

	if (/\/\*.*\*\//) {
 	    $comment = 0 ;
	}

	if (/\/\*/) {
	    $comment = 1 ;
	}
	
	if (/^[ \t]*\{/) {
	    $scope++ ;
	}

	if (!$scope && !$comment && !$cpp && !/;|=|struct|typedef/ && /[^ \t]/) {
	    if (/\(/) {
		if (! $type) {
		    $indx = index($_, "(") ;
		    $rindx = rindex($_, " ", $indx) ;

		    $type = &Trim(substr($_, 0, $rindx)) ;
		    $func = &Trim(substr($_, $rindx + 1, $indx - $rindx - 1)) ;
		    $arglist .= substr($_, $indx + 1) ;
		} else {
		    $indx = index($_, "(") ;
		    $func = &Trim(substr($_, 0, $indx)) ;
		    $arglist .= substr($_, $indx + 1) ;
		}

		$args++ ;
	    } elsif (! $args) {
		$type = $_ ;
	    } elsif ($args && !/\)/) {
		$arglist .=  $_ ;
	    }

	    if (/\)/) {
		if (! /\(/) {
		    $arglist .= $_ ;
		}

		$arglist =~ s/\)//g ;
		$arglist =~ s/[ \t]/ /g ;
		$arglist =~ tr/ //s ;

		&DumpFunction($type, $func, &Trim($arglist)) ;

		$args-- ;
		$type = "" ;
		$arglist = "" ;
	    }

	    next ;
	}

	if (/\*\//) {
	    $comment = 0 ;
	}

	if (/^[ \t]*\}/) {
	    $scope-- ;
	}

	print "$_\n" ;
    }
}

&ParseFile ;

