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