]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/perl | |
2 | # SPDX-License-Identifier: GPL-2.0-or-later | |
3 | ## | |
4 | ## Scan a file of route-type definitions (see eg route_types.txt) and | |
5 | ## generate a corresponding header file with: | |
6 | ## | |
7 | ## - enum of Zserv route-types | |
8 | ## - redistribute strings for the various Quagga daemons | |
9 | ## | |
10 | ## See route_types.txt for the format. | |
11 | ## | |
12 | ## | |
13 | ## Copyright (C) 2009 David Lamparter. | |
14 | ## | |
15 | ||
16 | use strict; | |
17 | use Getopt::Long; | |
18 | ||
19 | # input processing | |
20 | # | |
21 | my @protos; | |
22 | my %protodetail; | |
23 | ||
24 | my %daemons; | |
25 | ||
26 | my @enabled; | |
27 | ||
28 | GetOptions ("enabled=s" => \@enabled); | |
29 | @enabled = split(/,/,join(',',@enabled)); | |
30 | ||
31 | while (<STDIN>) { | |
32 | # skip comments and empty lines | |
33 | next if (/^\s*(#|$)/); | |
34 | ||
35 | # strip whitespace | |
36 | chomp; | |
37 | $_ =~ s/^\s*//; | |
38 | $_ =~ s/\s*$//; | |
39 | ||
40 | # match help strings | |
41 | if (/^(ZEBRA_ROUTE_[^\s]+)\s*,\s*"(.*)"$/) { | |
42 | $protodetail{$1}->{'longhelp'} = $2; | |
43 | next; | |
44 | } | |
45 | ||
46 | $_ =~ s/\s*,\s*/,/g; | |
47 | ||
48 | # else: 8-field line | |
49 | my @f = split(/,/, $_); | |
50 | unless (@f == 9 || @f == 10) { | |
51 | die "invalid input on route_types line $.\n"; | |
52 | } | |
53 | ||
54 | my $proto = $f[0]; | |
55 | $f[3] = $1 if ($f[3] =~ /^'(.*)'$/); | |
56 | $f[7] = $1 if ($f[7] =~ /^"(.*)"$/); | |
57 | ||
58 | $protodetail{$proto} = { | |
59 | "number" => scalar @protos, | |
60 | "type" => $f[0], | |
61 | "cname" => $f[1], | |
62 | "daemon" => $f[2], | |
63 | "char" => $f[3], | |
64 | "ipv4" => int($f[4]), | |
65 | "ipv6" => int($f[5]), | |
66 | "redist" => int($f[6]), | |
67 | "shorthelp" => $f[7], | |
68 | "enabled" => $f[8], | |
69 | "restrict2" => $f[9], | |
70 | }; | |
71 | push @protos, $proto; | |
72 | $daemons{$f[2]} = { | |
73 | "ipv4" => int($f[4]), | |
74 | "ipv6" => int($f[5]) | |
75 | } unless ($f[2] eq "NULL"); | |
76 | } | |
77 | ||
78 | # output | |
79 | printf <<EOF, $ARGV[0]; | |
80 | /* Auto-generated from route_types.txt by %s. */ | |
81 | /* Do not edit! */ | |
82 | ||
83 | #ifndef _FRR_ROUTE_TYPES_H | |
84 | #define _FRR_ROUTE_TYPES_H | |
85 | ||
86 | /* Zebra route's' types. */ | |
87 | EOF | |
88 | ||
89 | push @protos, "ZEBRA_ROUTE_MAX"; | |
90 | my (@protosv4, @protosv6) = ((), ()); | |
91 | for (my $c = 0; $c < @protos; $c++) { | |
92 | my $p = $protos[$c]; | |
93 | printf "#define %-32s %d\n", $p, $c; | |
94 | push @protosv4, $p if ($protodetail{$p}->{"ipv4"}); | |
95 | push @protosv6, $p if ($protodetail{$p}->{"ipv6"}); | |
96 | } | |
97 | pop @protos; | |
98 | ||
99 | sub codelist { | |
100 | my (@protos) = @_; | |
101 | my (@lines) = (); | |
102 | my $str = " \"Codes: "; | |
103 | for my $p (@protos) { | |
104 | next unless (grep $_ eq $protodetail{$p}->{"enabled"}, @enabled); | |
105 | my $s = sprintf("%s - %s, ", | |
106 | $protodetail{$p}->{"char"}, | |
107 | $protodetail{$p}->{"shorthelp"}); | |
108 | if (length($str . $s) > 70) { | |
109 | $str =~ s/ $//; | |
110 | push @lines, $str . "\\n\" \\\n"; | |
111 | $str = " \" "; | |
112 | } | |
113 | $str .= $s; | |
114 | } | |
115 | $str =~ s/ $//; | |
116 | push @lines, $str . "\\n\" \\\n"; | |
117 | push @lines, " \" > - selected route, * - FIB route, q - queued, r - rejected, b - backup\\n\""; | |
118 | push @lines, " \" t - trapped, o - offload failure\\n\\n\""; | |
119 | ||
120 | ||
121 | return join("", @lines); | |
122 | } | |
123 | ||
124 | print "\n"; | |
125 | printf "#define SHOW_ROUTE_V4_HEADER \\\n%s\n", codelist(@protosv4); | |
126 | printf "#define SHOW_ROUTE_V6_HEADER \\\n%s\n", codelist(@protosv6); | |
127 | print "\n"; | |
128 | ||
129 | sub collect { | |
130 | my ($daemon, $ipv4, $ipv6, $any) = @_; | |
131 | my (@names, @help) = ((), ()); | |
132 | for my $p (@protos) { | |
133 | next if ($protodetail{$p}->{"daemon"} eq $daemon && $daemon ne "zebra"); | |
134 | next if ($protodetail{$p}->{"restrict2"} ne "" && | |
135 | $protodetail{$p}->{"restrict2"} ne $daemon); | |
136 | next if ($protodetail{$p}->{"redist"} eq 0); | |
137 | next unless (grep $_ eq $protodetail{$p}->{"enabled"}, @enabled); | |
138 | next unless (($ipv4 && $protodetail{$p}->{"ipv4"}) | |
139 | || ($ipv6 && $protodetail{$p}->{"ipv6"})); | |
140 | push @names, $protodetail{$p}->{"cname"}; | |
141 | push @help, " \"".$protodetail{$p}->{"longhelp"}."\\n\""; | |
142 | } | |
143 | if ($any == 1) { | |
144 | push @names, "any"; | |
145 | push @help, " \"Any of the above protocols\\n\""; | |
146 | } | |
147 | return ("\"<" . join("|", @names) . ">\"", join(" \\\n", @help)); | |
148 | } | |
149 | ||
150 | for my $daemon (sort keys %daemons) { | |
151 | next unless ($daemons{$daemon}->{"ipv4"} || $daemons{$daemon}->{"ipv6"}); | |
152 | printf "/* %s */\n", $daemon; | |
153 | if ($daemons{$daemon}->{"ipv4"} && $daemons{$daemon}->{"ipv6"}) { | |
154 | my ($names, $help) = collect($daemon, 1, 1, 0); | |
155 | printf "#define FRR_REDIST_STR_%s \\\n %s\n", uc $daemon, $names; | |
156 | printf "#define FRR_REDIST_HELP_STR_%s \\\n%s\n", uc $daemon, $help; | |
157 | ||
158 | ($names, $help) = collect($daemon, 1, 0, 0); | |
159 | printf "#define FRR_IP_REDIST_STR_%s \\\n %s\n", uc $daemon, $names; | |
160 | printf "#define FRR_IP_REDIST_HELP_STR_%s \\\n%s\n", uc $daemon, $help; | |
161 | ||
162 | ($names, $help) = collect($daemon, 0, 1, 0); | |
163 | printf "#define FRR_IP6_REDIST_STR_%s \\\n %s\n", uc $daemon, $names; | |
164 | printf "#define FRR_IP6_REDIST_HELP_STR_%s \\\n%s\n", uc $daemon, $help; | |
165 | ||
166 | if ($daemon eq "zebra") { | |
167 | ($names, $help) = collect($daemon, 1, 0, 1); | |
168 | printf "#define FRR_IP_PROTOCOL_MAP_STR_%s \\\n %s\n", uc $daemon, $names; | |
169 | printf "#define FRR_IP_PROTOCOL_MAP_HELP_STR_%s \\\n%s\n", uc $daemon, $help; | |
170 | ||
171 | ($names, $help) = collect($daemon, 0, 1, 1); | |
172 | printf "#define FRR_IP6_PROTOCOL_MAP_STR_%s \\\n %s\n", uc $daemon, $names; | |
173 | printf "#define FRR_IP6_PROTOCOL_MAP_HELP_STR_%s \\\n%s\n", uc $daemon, $help; | |
174 | } | |
175 | } else { | |
176 | my ($names, $help) = collect($daemon, | |
177 | $daemons{$daemon}->{"ipv4"}, $daemons{$daemon}->{"ipv6"}, 0); | |
178 | printf "#define FRR_REDIST_STR_%s \\\n %s\n", uc $daemon, $names; | |
179 | printf "#define FRR_REDIST_HELP_STR_%s \\\n%s\n", uc $daemon, $help; | |
180 | } | |
181 | print "\n"; | |
182 | } | |
183 | ||
184 | print <<EOF; | |
185 | ||
186 | #ifdef FRR_DEFINE_DESC_TABLE | |
187 | ||
188 | struct zebra_desc_table | |
189 | { | |
190 | unsigned int type; | |
191 | const char *string; | |
192 | char chr; | |
193 | }; | |
194 | ||
195 | #define DESC_ENTRY(T,S,C) [(T)] = { (T), (S), (C) } | |
196 | static const struct zebra_desc_table route_types[] = { | |
197 | EOF | |
198 | ||
199 | for (my $c = 0; $c < @protos; $c++) { | |
200 | my $p = $protos[$c]; | |
201 | printf " DESC_ENTRY\t(%s\t \"%s\",\t'%s' ),\n", | |
202 | $p.",", $protodetail{$p}->{"cname"}, $protodetail{$p}->{"char"}; | |
203 | } | |
204 | ||
205 | print <<EOF; | |
206 | }; | |
207 | #undef DESC_ENTRY | |
208 | ||
209 | #endif /* FRR_DEFINE_DESC_TABLE */ | |
210 | ||
211 | #endif /* _FRR_ROUTE_TYPES_H */ | |
212 | EOF | |
213 |