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