Import Upstream version 1.2.2
[quagga-debian.git] / vtysh / extract.pl.in
1 #! @PERL@
2 ##
3 ## @configure_input@
4 ##
5 ## Virtual terminal interface shell command extractor.
6 ## Copyright (C) 2000 Kunihiro Ishiguro
7 ## 
8 ## This file is part of GNU Zebra.
9 ## 
10 ## GNU Zebra is free software; you can redistribute it and/or modify it
11 ## under the terms of the GNU General Public License as published by the
12 ## Free Software Foundation; either version 2, or (at your option) any
13 ## later version.
14 ## 
15 ## GNU Zebra is distributed in the hope that it will be useful, but
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ## General Public License for more details.
19 ## 
20 ## You should have received a copy of the GNU General Public License
21 ## along with GNU Zebra; see the file COPYING.  If not, write to the Free
22 ## Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 ## 02111-1307, USA.  
24 ##
25
26 print <<EOF;
27 #include <zebra.h>
28 #include "command.h"
29 #include "vtysh.h"
30
31 EOF
32
33 $ignore{'"interface IFNAME"'} = "ignore";
34 $ignore{'"interface IFNAME " "vrf <0-65535>"'} = "ignore";
35 $ignore{'"link-params"'} = "ignore";
36 $ignore{'"ip vrf NAME"'} = "ignore";
37 $ignore{'"router rip"'} = "ignore";
38 $ignore{'"router ripng"'} = "ignore";
39 $ignore{'"router ospf"'} = "ignore";
40 $ignore{'"router ospf <0-65535>"'} = "ignore";
41 $ignore{'"router ospf6"'} = "ignore";
42 $ignore{'"router bgp " "<1-4294967295>"'} = "ignore";
43 $ignore{'"router bgp " "<1-4294967295>" " view WORD"'} = "ignore";
44 $ignore{'"router isis WORD"'} = "ignore";
45 $ignore{'"router zebra"'} = "ignore";
46 $ignore{'"address-family ipv4"'} = "ignore";
47 $ignore{'"address-family ipv4 (unicast|multicast)"'} = "ignore";
48 $ignore{'"address-family ipv6"'} = "ignore";
49 $ignore{'"address-family ipv6 (unicast|multicast)"'} = "ignore";
50 $ignore{'"address-family vpnv4"'} = "ignore";
51 $ignore{'"address-family vpnv4 unicast"'} = "ignore";
52 $ignore{'"address-family vpnv6"'} = "ignore";
53 $ignore{'"address-family vpnv6 unicast"'} = "ignore";
54 $ignore{'"address-family ipv4 vrf NAME"'} = "ignore";
55 $ignore{'"address-family encap"'} = "ignore";
56 $ignore{'"address-family encapv4"'} = "ignore";
57 $ignore{'"address-family encapv6"'} = "ignore";
58 $ignore{'"exit-address-family"'} = "ignore";
59 $ignore{'"exit-link-params"'} = "ignore";
60 $ignore{'"vnc defaults"'} = "ignore";
61 $ignore{'"vnc nve-group NAME"'} = "ignore";
62 $ignore{'"exit-vnc"'} = "ignore";
63 $ignore{'"key chain WORD"'} = "ignore";
64 $ignore{'"key <0-2147483647>"'} = "ignore";
65 $ignore{'"route-map WORD (deny|permit) <1-65535>"'} = "ignore";
66 $ignore{'"show route-map"'} = "ignore";
67 $ignore{'"line vty"'} = "ignore";
68 $ignore{'"who"'} = "ignore";
69 $ignore{'"terminal monitor"'} = "ignore";
70 $ignore{'"terminal no monitor"'} = "ignore";
71 $ignore{'"show history"'} = "ignore";
72
73 my $cli_stomp = 0;
74
75 foreach (@ARGV) {
76     $file = $_;
77
78     open (FH, "@CPP@ -DHAVE_CONFIG_H -DVTYSH_EXTRACT_PL -DHAVE_IPV6 -I@top_builddir@ -I@srcdir@/ -I@srcdir@/.. -I@top_srcdir@/lib -I@top_builddir@/lib -I@top_srcdir@/isisd/topology @CPPFLAGS@ $file |");
79     local $/; undef $/;
80     $line = <FH>;
81     close (FH);
82
83     @defun = ($line =~ /(?:DEFUN|ALIAS)\s*\((.+?)\);?\s?\s?\n/sg);
84     @install = ($line =~ /install_element\s*\(\s*[0-9A-Z_]+,\s*&[^;]*;\s*\n/sg);
85
86     # DEFUN process
87     foreach (@defun) {
88         my (@defun_array);
89         @defun_array = split (/,/);
90         $defun_array[0] = '';
91
92
93         # Actual input command string.
94         $str = "$defun_array[2]";
95         $str =~ s/^\s+//g;
96         $str =~ s/\s+$//g;
97
98         # Get VTY command structure.  This is needed for searching
99         # install_element() command.
100         $cmd = "$defun_array[1]";
101         $cmd =~ s/^\s+//g;
102         $cmd =~ s/\s+$//g;
103
104         # $protocol is VTYSH_PROTO format for redirection of user input
105         if ($file =~ /lib\/keychain\.c$/) {
106             $protocol = "VTYSH_RIPD";
107         }
108         elsif ($file =~ /lib\/routemap\.c$/) {
109             $protocol = "VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA";
110         }
111         elsif ($file =~ /lib\/filter\.c$/) {
112             $protocol = "VTYSH_ALL";
113         }
114         elsif ($file =~ /lib\/vrf\.c$/) {
115             $protocol = "VTYSH_ZEBRA";
116         }
117         elsif ($file =~ /lib\/plist\.c$/) {
118             if ($defun_array[1] =~ m/ipv6/) {
119                 $protocol = "VTYSH_RIPNGD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA";
120             } else {
121                 $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_BGPD|VTYSH_ZEBRA";
122             }
123         }
124         elsif ($file =~ /lib\/distribute\.c$/) {
125             if ($defun_array[1] =~ m/ipv6/) {
126                 $protocol = "VTYSH_RIPNGD";
127             } else {
128                 $protocol = "VTYSH_RIPD";
129             }
130         }
131         elsif ($file =~ /lib\/if_rmap\.c$/) {
132             if ($defun_array[1] =~ m/ipv6/) {
133                 $protocol = "VTYSH_RIPNGD";
134             } else {
135                 $protocol = "VTYSH_RIPD";
136             }
137         }
138         elsif ($file =~ /lib\/vty\.c$/) {
139            $protocol = "VTYSH_ALL";
140         }
141         else {
142            ($protocol) = ($file =~ /^.*\/([a-z0-9]+)\/[a-zA-Z0-9_\-]+\.c$/);
143            $protocol = "VTYSH_" . uc $protocol;
144         }
145
146         # Append _vtysh to structure then build DEFUN again
147         $defun_array[1] = $cmd . "_vtysh";
148         $defun_body = join (", ", @defun_array);
149
150         # $cmd -> $str hash for lookup
151        if (exists($cmd2str{$cmd})) {
152            warn "Duplicate CLI Function: $cmd\n";
153            warn "\tFrom cli: $cmd2str{$cmd} to New cli: $str\n";
154            warn "\tOriginal Protocol: $cmd2proto{$cmd} to New Protocol: $protocol\n";
155            $cli_stomp++;
156        }
157         $cmd2str{$cmd} = $str;
158         $cmd2defun{$cmd} = $defun_body;
159         $cmd2proto{$cmd} = $protocol;
160     }
161
162     # install_element() process
163     foreach (@install) {
164         my (@element_array);
165         @element_array = split (/,/);
166
167         # Install node
168         $enode = $element_array[0];
169         $enode =~ s/^\s+//g;
170         $enode =~ s/\s+$//g;
171         ($enode) = ($enode =~ /([0-9A-Z_]+)$/);
172
173         # VTY command structure.
174         ($ecmd) = ($element_array[1] =~ /&([^\)]+)/);
175         $ecmd =~ s/^\s+//g;
176         $ecmd =~ s/\s+$//g;
177
178         # Register $ecmd
179         if (defined ($cmd2str{$ecmd})
180             && ! defined ($ignore{$cmd2str{$ecmd}})) {
181             my ($key);
182             $key = $enode . "," . $cmd2str{$ecmd};
183             $ocmd{$key} = $ecmd;
184             $odefun{$key} = $cmd2defun{$ecmd};
185             push (@{$oproto{$key}}, $cmd2proto{$ecmd});
186         }
187     }
188 }
189
190 my $bad_cli_stomps = 102;
191 # Currently we have $bad_cli_stomps.  This was determined by
192 # running this script and counting up the collisions from what
193 # was returned.
194 #
195 # When we have cli commands that map to the same function name, we
196 # can introduce subtle bugs due to code not being called when
197 # we think it is.
198 #
199 # If extract.pl fails with a error message and you've been
200 # modifying the cli, then go back and fix your code to
201 # not have cli command function collisions.
202 #
203 # If you've removed a cli overwrite, you can safely subtract
204 # one from $bad_cli_stomps.  If you've added to the problem
205 # please fix your code before submittal
206 if ($cli_stomp != $bad_cli_stomps) {
207     warn "Expected $bad_cli_stomps command line stomps, but got $cli_stomp instead\n";
208     exit $cli_stomp;
209 }
210
211 # Check finaly alive $cmd;
212 foreach (keys %odefun) {
213     my ($node, $str) = (split (/,/));
214     my ($cmd) = $ocmd{$_};
215     $live{$cmd} = $_;
216 }
217
218 # Output DEFSH
219 foreach (keys %live) {
220     my ($proto);
221     my ($key);
222     $key = $live{$_};
223     $proto = join ("|", @{$oproto{$key}});
224     printf "DEFSH ($proto$odefun{$key})\n\n";
225 }
226
227 # Output install_element
228 print <<EOF;
229 void
230 vtysh_init_cmd (void)
231 {
232 EOF
233
234 foreach (keys %odefun) {
235     my ($node, $str) = (split (/,/));
236     $cmd = $ocmd{$_};
237     $cmd =~ s/_cmd/_cmd_vtysh/;
238     printf "  install_element ($node, &$cmd);\n";
239 }
240
241 print <<EOF
242 }
243 EOF