]> git.proxmox.com Git - mirror_frr.git/blob - vtysh/extract.pl.in
Merge pull request #964 from opensourcerouting/plist-trie-corruption-3.0
[mirror_frr.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
29 #include "command.h"
30 #include "linklist.h"
31
32 #include "vtysh.h"
33
34 EOF
35
36 my $cli_stomp = 0;
37
38 foreach (@ARGV) {
39 $file = $_;
40
41 open (FH, "@CPP@ -DHAVE_CONFIG_H -DVTYSH_EXTRACT_PL -I@top_builddir@ -I@srcdir@/ -I@srcdir@/.. -I@top_srcdir@/lib -I@top_builddir@/lib -I@top_srcdir@/bgpd -I@top_srcdir@/@LIBRFP@ -I@top_srcdir@/bgpd/rfapi @CPPFLAGS@ $file |");
42 local $/; undef $/;
43 $line = <FH>;
44 close (FH);
45
46 # ?: makes a group non-capturing
47 @defun = ($line =~ /((?:DEFUN|DEFUN_HIDDEN|ALIAS|ALIAS_HIDDEN)\s*\(.+?\));?\s?\s?\n/sg);
48 @install = ($line =~ /install_element\s*\(\s*[0-9A-Z_]+,\s*&[^;]*;\s*\n/sg);
49
50 # DEFUN process
51 foreach (@defun) {
52 # $_ will contain the entire string including the DEFUN, ALIAS, etc.
53 # We need to extract the DEFUN/ALIAS from everything in ()s.
54 # The /s at the end tells the regex to allow . to match newlines.
55 $_ =~ /^(.*?)\s*\((.*)\)$/s;
56
57 my (@defun_array);
58 $defun_or_alias = $1;
59 @defun_array = split (/,/, $2);
60
61 if ($defun_or_alias =~ /_HIDDEN/) {
62 $hidden = 1;
63 } else {
64 $hidden = 0;
65 }
66
67 $defun_array[0] = '';
68
69 # Actual input command string.
70 $str = "$defun_array[2]";
71 $str =~ s/^\s+//g;
72 $str =~ s/\s+$//g;
73
74 # Get VTY command structure. This is needed for searching
75 # install_element() command.
76 $cmd = "$defun_array[1]";
77 $cmd =~ s/^\s+//g;
78 $cmd =~ s/\s+$//g;
79
80 # $protocol is VTYSH_PROTO format for redirection of user input
81 if ($file =~ /lib\/keychain\.c$/) {
82 $protocol = "VTYSH_RIPD";
83 }
84 elsif ($file =~ /lib\/routemap\.c$/) {
85 $protocol = "VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA|VTYSH_PIMD|VTYSH_EIGRPD";
86 }
87 elsif ($file =~ /lib\/vrf\.c$/) {
88 $protocol = "VTYSH_ALL";
89 }
90 elsif ($file =~ /lib\/filter\.c$/) {
91 $protocol = "VTYSH_ALL";
92 }
93 elsif ($file =~ /lib\/agentx\.c$/) {
94 $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA";
95 }
96 elsif ($file =~ /lib\/ns\.c$/) {
97 $protocol = "VTYSH_ZEBRA";
98 }
99 elsif ($file =~ /lib\/plist\.c$/) {
100 if ($defun_array[1] =~ m/ipv6/) {
101 $protocol = "VTYSH_RIPNGD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA|VTYSH_BABELD";
102 } else {
103 $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_BGPD|VTYSH_ZEBRA|VTYSH_PIMD|VTYSH_EIGRPD|VTYSH_BABELD";
104 }
105 }
106 elsif ($file =~ /lib\/distribute\.c$/) {
107 if ($defun_array[1] =~ m/ipv6/) {
108 $protocol = "VTYSH_RIPNGD";
109 } else {
110 $protocol = "VTYSH_RIPD";
111 }
112 }
113 elsif ($file =~ /lib\/if_rmap\.c$/) {
114 if ($defun_array[1] =~ m/ipv6/) {
115 $protocol = "VTYSH_RIPNGD";
116 } else {
117 $protocol = "VTYSH_RIPD";
118 }
119 }
120 elsif ($file =~ /lib\/vty\.c$/) {
121 $protocol = "VTYSH_ALL";
122 }
123 elsif ($file =~ /librfp\/.*\.c$/ || $file =~ /rfapi\/.*\.c$/) {
124 $protocol = "VTYSH_BGPD";
125 }
126 else {
127 ($protocol) = ($file =~ /^.*\/([a-z0-9]+)\/[a-zA-Z0-9_\-]+\.c$/);
128 $protocol = "VTYSH_" . uc $protocol;
129 }
130
131 # Append _vtysh to structure then build DEFUN again
132 $defun_array[1] = $cmd . "_vtysh";
133 $defun_body = join (", ", @defun_array);
134
135 # $cmd -> $str hash for lookup
136 if (exists($cmd2str{$cmd})) {
137 warn "Duplicate CLI Function: $cmd\n";
138 warn "\tFrom cli: $cmd2str{$cmd} to New cli: $str\n";
139 warn "\tOriginal Protocol: $cmd2proto{$cmd} to New Protocol: $protocol\n";
140 $cli_stomp++;
141 }
142 $cmd2str{$cmd} = $str;
143 $cmd2defun{$cmd} = $defun_body;
144 $cmd2proto{$cmd} = $protocol;
145 $cmd2hidden{$cmd} = $hidden;
146 }
147
148 # install_element() process
149 foreach (@install) {
150 my (@element_array);
151 @element_array = split (/,/);
152
153 # Install node
154 $enode = $element_array[0];
155 $enode =~ s/^\s+//g;
156 $enode =~ s/\s+$//g;
157 ($enode) = ($enode =~ /([0-9A-Z_]+)$/);
158
159 # VTY command structure.
160 ($ecmd) = ($element_array[1] =~ /&([^\)]+)/);
161 $ecmd =~ s/^\s+//g;
162 $ecmd =~ s/\s+$//g;
163
164 # Register $ecmd
165 if (defined ($cmd2str{$ecmd})) {
166 my ($key);
167 $key = $enode . "," . $cmd2str{$ecmd};
168 $ocmd{$key} = $ecmd;
169 $odefun{$key} = $cmd2defun{$ecmd};
170
171 if ($cmd2hidden{$ecmd}) {
172 $defsh{$key} = "DEFSH_HIDDEN"
173 } else {
174 $defsh{$key} = "DEFSH"
175 }
176 push (@{$oproto{$key}}, $cmd2proto{$ecmd});
177 }
178 }
179 }
180
181 # When we have cli commands that map to the same function name, we
182 # can introduce subtle bugs due to code not being called when
183 # we think it is.
184 #
185 # If extract.pl fails with a error message and you've been
186 # modifying the cli, then go back and fix your code to
187 # not have cli command function collisions.
188 # please fix your code before submittal
189 if ($cli_stomp) {
190 warn "There are $cli_stomp command line stomps\n";
191 }
192
193 # Check finaly alive $cmd;
194 foreach (keys %odefun) {
195 my ($node, $str) = (split (/,/));
196 my ($cmd) = $ocmd{$_};
197 $live{$cmd} = $_;
198 }
199
200 # Output DEFSH
201 foreach (sort keys %live) {
202 my ($proto);
203 my ($key);
204 $key = $live{$_};
205 $proto = join ("|", @{$oproto{$key}});
206 printf "$defsh{$key} ($proto$odefun{$key})\n\n";
207 }
208
209 # Output install_element
210 print <<EOF;
211 void
212 vtysh_init_cmd ()
213 {
214 EOF
215
216 foreach (sort keys %odefun) {
217 my ($node, $str) = (split (/,/));
218 $cmd = $ocmd{$_};
219 $cmd =~ s/_cmd/_cmd_vtysh/;
220 printf " install_element ($node, &$cmd);\n";
221 }
222
223 print <<EOF
224 }
225 EOF