#! /usr/local/bin/perl -w

# @(#)mkfunclist	1.3 09 Apr 1995 (UKC)

# Make a list of wanted functions for srcmunge, based on the call tree
# generated by mkcalltree.

$conf = 'upsconf';
$zapcalls = "$conf/zapcalls";
$cppcmd_file = "$conf/cppcmd";

%wanted = (
	   'file_command',		'',
	   'core_file_command',		'',
	   'break_command',		'',
	   'delete_command',		'',
	   'step_command',		'',
	   'next_command',		'',
	   'continue_command',		'',
	   'finish_command',		'',
	   'kill_command',		'',
	   'attach_command',		'',
	   'detach_command',		'',
	   'enable_command',		'',
	   'disable_command',		'',

	   'init_malloc',		'',
	   'call_function_by_hand',	'',
	   'allocate_space_in_inferior','',
	   'write_sp',			'',
	   'write_fp',			'',
	   'breakpoint_1',		'',
	   'print_gdb_version',		'',
	   
	   '[unknown_language_defn]',	'', # Used to init current_language

	   'evaluate_expression',	'-',
	   'print_insn',		'-',
	   'parse_and_eval_address_1',	'-',
	   'strsave',			'-',

	   'wrap_here',			'-',
	   'fputs_maybe_filtered',	'-',
	   'command_line_input',	'-',
	   'error',			'-',
);

open(FUNCS, $zapcalls) || die("Can't open $zapcalls ($!)\n");

while (<FUNCS>) {
	chop $_;
	s/\s*#.*//;
	$wanted{$_} = '-' unless /^\s*$/;
}

close(FUNCS);


open(CPPCMD, $cppcmd_file) || die("Can't open $cppcmd_file ($!)\n");
$cppcmd = <CPPCMD>;
close(CPPCMD) || die("Error reading $cppcmd_file ($!)\n");
chop($cppcmd);

open(CPP, "$cppcmd init.c |") || die("Can't run cc -E on init.c ($!)\n");
while (<CPP>) {
	$wanted{$1} = '' if (/(_initialize_\w+)\s*\(\)/);
}
close(CPP) || die("Error running cc -E on init.c\n");

while (<>) {
	die("$.: Bad line\n") unless (/^(<-  |  ->) (\[?\w+]?)\([^)]*\):(.*)/);
	$called_by{$2} = $3 if ($1 eq '<-  ');
}

do {
	$done_one = 0;
	
	foreach $func (keys %wanted) {
		next if $done{$func} || $wanted{$func} eq '-';
		$done{$func} = 1;
		
		$chain = $wanted{$func};
		
		next unless defined $called_by{$func};
		@called = split(' ', $called_by{$func});

		foreach $called(@called) {
			next if defined($wanted{$called});
			$wanted{$called} = "$func:$chain";
			$done_one = 1;
		}
	}
} while ($done_one);

foreach $func (sort keys %wanted) {
	next if ($wanted{$func} eq '-');
	printf("%-30s # %s\n", $func, $wanted{$func});
}

# Local Variables:
# mode: perl
# End:
