Free the right thing.
[openssl.git] / dep / gen.pl
1 #!/usr/local/bin/perl
2
3 require 'getopts.pl';
4
5 $files="files";
6 %have=();
7 %missing=();
8 %name=();
9 %func=();
10
11 &Getopts('Ff:');
12
13 &load_file("files");
14 foreach $file (@ARGV)
15         { &do_nm($file); }
16
17 if (defined($opt_f))
18         {
19         %a=();
20         $r=&list_files($opt_f,"",*a);
21         if ($opt_F)
22                 {
23                 foreach (sort split(/\n/,$r))
24                         { print "$_\n"; }
25                 }
26         else
27                 { print $r; }
28         }
29 else
30         {
31         for (sort keys %have)
32                 {
33                 print "$_:$have{$_}\n";
34                 }
35         }
36
37 sub list_files
38         {
39         local($f,$o,*done)=@_;
40         local($a,$_,$ff,$ret);
41
42         return if $f =~ /^\s*$/;
43
44         $done{$f}=1;
45         $ret.=$f."\n" if $opt_F;
46         foreach (split(/ /,$have{$f}))
47                 {
48                 $ret.="$o$f:$_\n" unless $opt_F;
49                 }
50
51         foreach (split(/ /,$missing{$f}))
52                 {
53                 $ff=$func{$_};
54                 next if defined($done{$ff});
55                 $ret.=&list_files($ff,$o."      ");
56                 }
57         $ret;
58         }
59
60 sub do_nm
61         {
62         local($file)=@_;
63         local($fname)="";
64
65         open(IN,"nm $file|") || die "unable to run 'nm $file|':$!\n";
66         while (<IN>)
67                 {
68                 chop;
69                 next if /^\s*$/;
70                 if (/^(.*)\.o:\s*$/)
71                         {
72                         $fname="$1.c";
73                         next;
74                         }
75                 ($type,$name)=/^.{8} (.) (.+)/;
76 #               print "$fname $type $name\n";
77
78                 if ($type eq "T")
79                         {
80                         $have{$fname}.="$name ";
81                         $func{$name}=$fname;
82                         }
83                 elsif ($type eq "U")
84                         {
85                         $missing{$fname}.="$name ";
86                         }
87                 }
88         close(IN);
89         }
90
91 sub load_file
92         {
93         local($file)=@_;
94
95         open(IN,"<$files") || die "unable to open $files:$!\n";
96
97         while (<IN>)
98                 {
99                 chop;
100                 next if /^\s*$/;
101                 ($n)=/\/([^\/\s]+)\s+/;
102                 ($fn)=/^(\S+)\s/;
103 #               print "$n - $fn\n";
104                 if (defined($name{$n}))
105                         { print "$n already exists\n"; }
106                 else
107                         { $name{$n}=$fn; }
108                 }
109         close(IN);
110         @name=%name;
111         }
112
113