forked from ruanjue/wtdbg2
/
dbm_read_dot.pl
executable file
·74 lines (58 loc) · 1.2 KB
/
dbm_read_dot.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/perl -w
#
#Author: Ruan Jue
#
use strict;
use Getopt::Std;
use DB_File;
our ($opt_l, $opt_h);
getopts("l:h");
my $level = $opt_l || 0;
&usage if($opt_h);
my $dbf = shift or &usage;
if($dbf!~/\.dbm$/){
$dbf .= ".dbm" if(-e "$dbf.dbm");
}
my @nodes = @ARGV;
if(@nodes == 0){
while(<>){
chomp;
push(@nodes, $_);
}
}
my %hash;
tie %hash, 'DB_File', $dbf, O_RDONLY or die "Cannot open $dbf: $!";
my %levels = map {$_=>0} @nodes;
print "digraph {\n";
print "rank=$hash{rank}\n" if(exists $hash{"rank"});
print "node $hash{node}\n" if(exists $hash{"node"});
print "edge $hash{edge}\n" if(exists $hash{"edge"});
while(@nodes){
my $nd = shift @nodes;
my $str = $hash{$nd};
my @rs = split /\n/, $str;
print "$nd $rs[0]";
if($levels{$nd} == 0){
print " [style=filled fillcolor=yellow]\n"
} else {
print "\n";
}
my $n2 = '';
for(my $i=1;$i<@rs;$i++){
if($rs[$i]=~/^N\s(\S+)/){
$n2 = $1;
if($levels{$nd} < $level and not exists $levels{$n2}){
$levels{$n2} = $levels{$nd} + 1;
push(@nodes, $n2);
}
} elsif($rs[$i]=~/^L\s(.+)$/){
print " $nd -> $n2 $1\n"
}
}
}
print "}\n";
untie %hash;
1;
sub usage {
die("Usage: $0 [-l TRACE_LEVEL:0] <dot_dbm_file> <node1> ...\n");
}