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