Listtree.mcc: rename variables in Listtree_GetEntry to match the template of other...
[AROS.git] / tools / cxref / contrib / tree.pl
blobca1be19d0186abe311e1b18bc3a625ff6eee44ff
1 #!/bin/sh
3 # $Header$
5 # Converts the cxref.function output file into a format that can display
6 # a simple tree structure
8 # Warning: This is my first perl program. It can probably be improved on !
10 # This file Copyright 2000 Ian A. Gilmour.
11 # (ian@igilmour.freeserve.co.uk / ian.gilmour.ffei.co.uk)
13 # It may be distributed under the GNU Public License, version 2, or
14 # any higher version. See section COPYING of the GNU Public license
15 # for conditions under which this file may be redistributed.
17 exec perl -w -x $0 $*
19 #!perl
21 $level = 0;
22 $func = "main";
23 $max_level = 3;
24 $found = 0;
26 # generates the tree going down
27 # i.e. displays which functions are called by the given function
29 sub find_func {
30 my $func_reqd = $_[0];
32 if ($level > $max_level) {
33 return;
34 } else {
35 $level++;
38 for ($z=0; $z < ($level-1)*3; $z++) {
39 print " ";
41 print "+- ";
43 if (defined ($functions{$func_reqd})) {
44 print "$func_reqd() \t[$file{$func_reqd}]";
45 if($scope{$func_reqd}==2) {
46 print " \t(static)";
48 if ($fshown{$func_reqd}) {
49 print " \t...referenced above\n";
50 } else {
51 print "\n";
52 $fshown{$func_reqd} = 1;
54 for (my $y=0; $y <= $fcount{$func_reqd}; $y++) {
55 find_func ($fcalls{$func_reqd}[$y]);
58 } else {
59 if (!defined ($fundefshown{$func_reqd})) {
60 print "$func_reqd() \t[undefined]\n";
61 $fundefshown{$func_reqd} = 1;
62 } else {
63 print "$func_reqd() \t[undefined] \t...referenced above\n";
66 $level--;
70 # generates the tree going up
71 # i.e finds which functions call the specified function
73 sub find_called_func {
74 my $func_reqd = $_[0];
76 if ($level <= $max_level) {
77 return;
78 } else {
79 $level--;
82 foreach my $func (keys %functions) {
83 if (defined ($fcount{$func})) {
84 for (my $y=0; $y <= $fcount{$func}; $y++) {
85 if ($func_reqd eq $fcalls{$func}[$y]) {
86 if (!$found) {
87 $found = 1;
88 print "+- $func_reqd()\n";
91 for (my $z=0; $z > (($level)*3); $z--) {
92 print " ";
94 print "+- ";
95 print "$functions{$func} \t[$file{$func}]";
96 if($scope{$func}==1) {
97 print " \t(static)";
99 if ($fshown{$func}) {
100 print " \t...referenced above\n";
101 } else {
102 print "\n";
103 $fshown{$func} = 1;
104 find_called_func ($functions{$func});
110 $level++;
112 if (!$found) {
113 print "$func_reqd() not referenced.\n";
115 return;
118 die "Usage: $0 cxref_function_file root_function max_level\n".
119 "max_level +ve for functions called by specified function\n".
120 "max_level -ve for functions that call specified function\n" if($#ARGV==-1);
122 if (defined ($ARGV[2])) {
123 $max_level = $ARGV[2];
126 if (defined ($ARGV[1])) {
127 $func = $ARGV[1];
130 open(FUNCTION,"<$ARGV[0]") || die "Cannot open $ARGV[0]\n";
132 # read all function names in once to set up database
134 while(<FUNCTION>) {
135 s/\%//g;
136 s/\&//g;
137 chop;
139 ($file,$function,$scope,@calls)=split(/ /);
141 if((defined ($scope)) && (($scope==2) || ($scope==1))) {
142 $scope{$function}="$scope";
143 $functions{$function}="$function";
144 $file{$function}="$file";
145 if (defined (@calls)) {
146 $fcalls{$function} = [@calls];
147 $fcount{$function} = $#calls;
148 $fshown{$function} = 0;
149 } else {
150 $fcount{$function} = -1;
155 close(FUNCTION);
157 # now display reqd info....
159 if ($max_level >= 0) {
160 print "Looking for functions that $func() calls...\n";
161 find_func ($func);
162 } else {
163 print "Looking for functions that call $func()...\n";
164 find_called_func ($func);