-
Notifications
You must be signed in to change notification settings - Fork 0
/
makedb.pl
executable file
·177 lines (153 loc) · 3.63 KB
/
makedb.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/perl
use DB_File;
my $asfile = "as.txt";
my $communityfile = "communities.txt";
my $dbfile = "as.db";
my %GLOBALVAR;
my %LOCALVAR;
my %COMMUNITY;
my %ANYNUM = ("0=0" => "", "1=1" => "", "2=2" => "", "3=3" => "", "4=4" => "",
"5=5" => "", "6=6" => "", "7=7" => "", "8=8" => "", "9=9" => "");
my %AS;
tie (%AS, 'DB_File', $dbfile, O_RDWR|O_CREAT, 0644, $DB_HASH) or
die "Can\'t write AS database $dbfile: $!";
undef %AS;
print "Reading AS names..\n";
%AS = &read_as_list($asfile);
print " OK\n";
print "Reading community names..\n";
my %COMMUNITY = &read_community_list($communityfile);
foreach my $key (keys (%COMMUNITY)) {
$AS{$key} = $COMMUNITY{$key};
}
print " OK\n";
print "Setting up database..\n";
untie %AS;
print " OK\n";
sub read_as_list {
my ($fn) = @_;
local *F;
my %AS;
if (! open(F, $fn)) {
print "ERROR: Can't read AS list from $fn: $!\n";
return;
}
print " Read AS list from $fn..\n";
while (<F>) {
chop;
if (/^#include\s+(.+)$/) {
my %AS2 = &read_as_list($1);
foreach my $key (keys (%AS2)) {
$AS{$key} = $AS2{$key};
}
undef %AS2;
next;
}
next if (/^$/ || /^\s*#/);
my ($asnum, $descr) = split /\t+/;
$asnum =~ s/^[^\d]*(\d+)[^\d]*$/$1/;
$AS{$asnum} = $descr;
}
close(F);
return (%AS);
}
sub read_community_list {
my ($fn) = @_;
local *F;
if (! open(F, $fn)) {
print "ERROR: Can't read community list from $fn: $!\n";
return;
}
print " Read community list from $fn..\n";
my $asnum = "";
while (<F>) {
chop;
if (/^#include\s+(.+)$/) {
my %COMMUNITY2 = &read_community_list($1);
foreach my $key (keys (%COMMUNITY2)) {
$COMMUNITY{$key} = $COMMUNITY2{$key};
}
undef %COMMUNITY2;
next;
}
next if (/^$/ || /^\s*#/);
s/#.*$//;
if (/^AS(\d+)$/) {
$asnum = $1;
undef %LOCALVAR;
next;
}
my ($community, $descr) = split /\t+/;
if ($community =~ /^(\d+):/) {
die "ERROR! AS $1 begin tag missing at line $." unless ($1 == $asnum);
}
if ($community =~ /^([A-Z]+=\d+)$/) {
$GLOBALVAR{$community} = $descr;
next;
}
if ($community =~ /^([a-z]+=\d+)$/) {
$LOCALVAR{$community} = $descr;
next;
}
if ($community =~ /^\d+:\d+$/) {
$COMMUNITY{$community} = $descr;
next;
}
if ($community =~ /^URL$/) {
$COMMUNITY{$asnum . ":URL"} = $descr;
next;
}
if ($community =~ /^(\d+):(\d*)([a-zA-Z\.].*)$/) {
&complete_community($community, $descr, %LOCALVAR);
next;
}
die "ERROR: Illegal community line $.: \"$_\"";
}
close(F);
return (%COMMUNITY);
}
sub complete_community {
my ($community, $descr) = @_;
if ($community =~ /^\d+:\d+$/) {
$COMMUNITY{$community} = $descr;
return;
}
my @clist;
if ($community =~ /^\d+:\d*([a-zA-Z\.]).*$/) {
my $var1 = $1;
$var1 .= "+" if ($var1 ne ".");
$community =~ /^(\d+):(\d*)(($var1))(.*)$/;
my $asnum = $1;
my $commpref = $2;
my $commvar = $3;
my $commsuf = $5;
my $varref;
if ($commvar =~ /^[A-Z]+$/) {
$varref = \%GLOBALVAR;
} elsif ($commvar =~ /^[a-z]+$/) {
$varref = \%LOCALVAR;
} elsif ($commvar eq ".") {
$varref = \%ANYNUM;
} else {
die "Illegal community variable \"$commvar\" in \"$community\"";
}
my $c = 0;
foreach my $key (sort keys (%{$varref})) {
if ($key !~ /^($commvar)=(.+)$/) {
next;
}
my $repl = $2;
my $newcomm = $asnum . ":" . $commpref . $repl . $commsuf;
my $descr2 = ${$varref}{$key};
$descr2 = $key if ($commvar eq ".");
(my $newdescr = $descr) =~ s/(\$$commvar)/$descr2/g;
&complete_community($newcomm, $newdescr);
$c++;
}
if ($c == 0) {
die "$community - no match for \"$commvar\"";
}
return;
}
die "complete_community() called without proper pattern";
}