]> git.proxmox.com Git - mirror_frr.git/blob - vtysh/extract.pl.in
*: Remove unused variables
[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 use Getopt::Long;
27
28 print <<EOF;
29 #include <zebra.h>
30
31 #include "command.h"
32 #include "linklist.h"
33
34 #include "vtysh/vtysh.h"
35
36 EOF
37
38 my $cli_stomp = 0;
39
40 sub scan_file {
41 my ( $file, $fabricd) = @_;
42
43 $cppadd = $fabricd ? "-DFABRICD=1" : "";
44
45 $command_line = "@CPP@ -P -std=gnu11 -DHAVE_CONFIG_H -DVTYSH_EXTRACT_PL -Ivtysh/@top_builddir@ -Ivtysh/@top_srcdir@ -Ivtysh/@top_srcdir@/lib -Ivtysh/@top_builddir@/lib -Ivtysh/@top_srcdir@/bgpd -Ivtysh/@top_srcdir@/bgpd/rfapi @LUA_INCLUDE@ @CPPFLAGS@ @LIBYANG_CFLAGS@ $cppadd $file |";
46 open (FH, $command_line)
47 || die "Open to the pipeline failed: $!\n\nCommand Issued:\n$command_line";
48 local $/; undef $/;
49 $line = <FH>;
50 if (!close (FH)) {
51 die "File: $file failed to compile:\n$!\nwhen extracting cli from it please inspect\n"
52 }
53
54 # ?: makes a group non-capturing
55 @defun = ($line =~ /((?:DEFUN|DEFUN_HIDDEN|DEFUN_YANG|ALIAS|ALIAS_HIDDEN|ALIAS_YANG|DEFPY|DEFPY_HIDDEN|DEFPY_YANG)\s*\(.+?\));?\s?\s?\n/sg);
56 @install = ($line =~ /install_element\s*\(\s*[0-9A-Z_]+,\s*&[^;]*;\s*\n/sg);
57
58 # DEFUN process
59 foreach (@defun) {
60 # $_ will contain the entire string including the DEFUN, ALIAS, etc.
61 # We need to extract the DEFUN/ALIAS from everything in ()s.
62 # The /s at the end tells the regex to allow . to match newlines.
63 $_ =~ /^(.*?)\s*\((.*)\)$/s;
64
65 my (@defun_array);
66 $defun_or_alias = $1;
67 @defun_array = split (/,/, $2);
68
69 if ($defun_or_alias =~ /_HIDDEN/) {
70 $hidden = 1;
71 } else {
72 $hidden = 0;
73 }
74
75 $defun_array[0] = '';
76
77 # Actual input command string.
78 $str = "$defun_array[2]";
79 $str =~ s/^\s+//g;
80 $str =~ s/\s+$//g;
81
82 # Get VTY command structure. This is needed for searching
83 # install_element() command.
84 $cmd = "$defun_array[1]";
85 $cmd =~ s/^\s+//g;
86 $cmd =~ s/\s+$//g;
87
88 if ($fabricd) {
89 $cmd = "fabricd_" . $cmd;
90 }
91
92 # $protocol is VTYSH_PROTO format for redirection of user input
93 if ($file =~ /lib\/keychain\.c$/) {
94 $protocol = "VTYSH_RIPD|VTYSH_EIGRPD";
95 }
96 elsif ($file =~ /lib\/routemap\.c$/ || $file =~ /lib\/routemap_cli\.c$/) {
97 $protocol = "VTYSH_RMAP";
98 }
99 elsif ($file =~ /lib\/vrf\.c$/) {
100 $protocol = "VTYSH_VRF";
101 }
102 elsif ($file =~ /lib\/if\.c$/) {
103 $protocol = "VTYSH_INTERFACE";
104 }
105 elsif ($file =~ /lib\/(filter|filter_cli)\.c$/) {
106 $protocol = "VTYSH_ACL";
107 }
108 elsif ($file =~ /lib\/(lib|log)_vty\.c$/) {
109 $protocol = "VTYSH_ALL";
110 }
111 elsif ($file =~ /lib\/agentx\.c$/) {
112 $protocol = "VTYSH_ISISD|VTYSH_RIPD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA";
113 }
114 elsif ($file =~ /lib\/nexthop_group\.c$/) {
115 $protocol = "VTYSH_NH_GROUP";
116 }
117 elsif ($file =~ /lib\/plist\.c$/) {
118 if ($defun_array[1] =~ m/ipv6/) {
119 $protocol = "VTYSH_RIPNGD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA|VTYSH_BABELD|VTYSH_ISISD|VTYSH_FABRICD";
120 } else {
121 $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_BGPD|VTYSH_ZEBRA|VTYSH_PIMD|VTYSH_EIGRPD|VTYSH_BABELD|VTYSH_ISISD|VTYSH_FABRICD";
122 }
123 }
124 elsif ($file =~ /lib\/if_rmap\.c$/) {
125 if ($defun_array[1] =~ m/ipv6/) {
126 $protocol = "VTYSH_RIPNGD";
127 } else {
128 $protocol = "VTYSH_RIPD";
129 }
130 }
131 elsif ($file =~ /lib\/resolver\.c$/) {
132 $protocol = "VTYSH_NHRPD|VTYSH_BGPD";
133 }
134 elsif ($file =~ /lib\/spf_backoff\.c$/) {
135 $protocol = "VTYSH_ISISD";
136 }
137 elsif ($file =~ /lib\/(vty|thread)\.c$/) {
138 $protocol = "VTYSH_ALL";
139 }
140 elsif ($file =~ /librfp\/.*\.c$/ || $file =~ /rfapi\/.*\.c$/) {
141 $protocol = "VTYSH_BGPD";
142 }
143 elsif ($fabricd) {
144 $protocol = "VTYSH_FABRICD";
145 }
146 else {
147 ($protocol) = ($file =~ /^(?:.*\/)?([a-z0-9]+)\/[a-zA-Z0-9_\-]+\.c$/);
148 $protocol = "VTYSH_" . uc $protocol;
149 }
150
151 # Append _vtysh to structure then build DEFUN again
152 $defun_array[1] = $cmd . "_vtysh";
153 $defun_body = join (", ", @defun_array);
154
155 # $cmd -> $str hash for lookup
156 if (exists($cmd2str{$cmd})) {
157 warn "Duplicate CLI Function: $cmd\n";
158 warn "\tFrom cli: $cmd2str{$cmd} to New cli: $str\n";
159 warn "\tOriginal Protocol: $cmd2proto{$cmd} to New Protocol: $protocol\n";
160 $cli_stomp++;
161 }
162 $cmd2str{$cmd} = $str;
163 $cmd2defun{$cmd} = $defun_body;
164 $cmd2proto{$cmd} = $protocol;
165 $cmd2hidden{$cmd} = $hidden;
166 }
167
168 # install_element() process
169 foreach (@install) {
170 my (@element_array);
171 @element_array = split (/,/);
172
173 # Install node
174 $enode = $element_array[0];
175 $enode =~ s/^\s+//g;
176 $enode =~ s/\s+$//g;
177 ($enode) = ($enode =~ /([0-9A-Z_]+)$/);
178
179 # VTY command structure.
180 ($ecmd) = ($element_array[1] =~ /&([^\)]+)/);
181 $ecmd =~ s/^\s+//g;
182 $ecmd =~ s/\s+$//g;
183
184 if ($fabricd) {
185 $ecmd = "fabricd_" . $ecmd;
186 }
187
188 # Register $ecmd
189 if (defined ($cmd2str{$ecmd})) {
190 my ($key);
191 $key = $enode . "," . $cmd2str{$ecmd};
192 $ocmd{$key} = $ecmd;
193 $odefun{$key} = $cmd2defun{$ecmd};
194
195 if ($cmd2hidden{$ecmd}) {
196 $defsh{$key} = "DEFSH_HIDDEN"
197 } else {
198 $defsh{$key} = "DEFSH"
199 }
200 push (@{$oproto{$key}}, $cmd2proto{$ecmd});
201 }
202 }
203 }
204
205 my $have_isisd = 0;
206 my $have_fabricd = 0;
207
208 GetOptions('have-isisd' => \$have_isisd, 'have-fabricd' => \$have_fabricd);
209
210 foreach (@ARGV) {
211 if (/(^|\/)isisd\//) {
212 # We scan all the IS-IS files twice, once for isisd,
213 # once for fabricd. Exceptions are made for the files
214 # that are not shared between the two.
215 if (/isis_vty_isisd.c/) {
216 if ( $have_isisd ) {
217 scan_file($_, 0);
218 }
219 } elsif (/isis_vty_fabricd.c/) {
220 if ( $have_fabricd ) {
221 scan_file($_, 1);
222 }
223 } else {
224 if ( $have_isisd ) {
225 scan_file($_, 0);
226 }
227 if ( $have_fabricd ) {
228 scan_file($_, 1);
229 }
230 }
231 } else {
232 scan_file($_, 0);
233 }
234 }
235
236 # When we have cli commands that map to the same function name, we
237 # can introduce subtle bugs due to code not being called when
238 # we think it is.
239 #
240 # If extract.pl fails with a error message and you've been
241 # modifying the cli, then go back and fix your code to
242 # not have cli command function collisions.
243 # please fix your code before submittal
244 if ($cli_stomp) {
245 warn "There are $cli_stomp command line stomps\n";
246 }
247
248 # Check finaly alive $cmd;
249 foreach (keys %odefun) {
250 my ($node, $str) = (split (/,/));
251 my ($cmd) = $ocmd{$_};
252 $live{$cmd} = $_;
253 }
254
255 # Output DEFSH
256 foreach (sort keys %live) {
257 my ($proto);
258 my ($key);
259 $key = $live{$_};
260 $proto = join ("|", @{$oproto{$key}});
261 printf "$defsh{$key} ($proto$odefun{$key})\n\n";
262 }
263
264 # Output install_element
265 print <<EOF;
266 void vtysh_init_cmd(void)
267 {
268 EOF
269
270 foreach (sort keys %odefun) {
271 my ($node, $str) = (split (/,/));
272 $cmd = $ocmd{$_};
273 $cmd =~ s/_cmd$/_cmd_vtysh/;
274 printf " install_element ($node, &$cmd);\n";
275 }
276
277 print <<EOF
278 }
279 EOF