dbm_read_dot.pl 1.2 KB

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