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