]> git.proxmox.com Git - mirror_frr.git/blob - vtysh/extract.pl.in
Merge pull request #476 from qlyoung/abort-select-fd
[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_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA|VTYSH_EIGRPD";
89 }
90 elsif ($file =~ /lib\/filter\.c$/) {
91 $protocol = "VTYSH_ALL";
92 }
93 elsif ($file =~ /lib\/ns\.c$/) {
94 $protocol = "VTYSH_ZEBRA";
95 }
96 elsif ($file =~ /lib\/plist\.c$/) {
97 if ($defun_array[1] =~ m/ipv6/) {
98 $protocol = "VTYSH_RIPNGD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA";
99 } else {
100 $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_BGPD|VTYSH_ZEBRA|VTYSH_PIMD|VTYSH_EIGRPD";
101 }
102 }
103 elsif ($file =~ /lib\/distribute\.c$/) {
104 if ($defun_array[1] =~ m/ipv6/) {
105 $protocol = "VTYSH_RIPNGD";
106 } else {
107 $protocol = "VTYSH_RIPD";
108 }
109 }
110 elsif ($file =~ /lib\/if_rmap\.c$/) {
111 if ($defun_array[1] =~ m/ipv6/) {
112 $protocol = "VTYSH_RIPNGD";
113 } else {
114 $protocol = "VTYSH_RIPD";
115 }
116 }
117 elsif ($file =~ /lib\/vty\.c$/) {
118 $protocol = "VTYSH_ALL";
119 }
120 elsif ($file =~ /librfp\/.*\.c$/ || $file =~ /rfapi\/.*\.c$/) {
121 $protocol = "VTYSH_BGPD";
122 }
123 else {
124 ($protocol) = ($file =~ /^.*\/([a-z0-9]+)\/[a-zA-Z0-9_\-]+\.c$/);
125 $protocol = "VTYSH_" . uc $protocol;
126 }
127
128 # Append _vtysh to structure then build DEFUN again
129 $defun_array[1] = $cmd . "_vtysh";
130 $defun_body = join (", ", @defun_array);
131
132 # $cmd -> $str hash for lookup
133 if (exists($cmd2str{$cmd})) {
134 warn "Duplicate CLI Function: $cmd\n";
135 warn "\tFrom cli: $cmd2str{$cmd} to New cli: $str\n";
136 warn "\tOriginal Protocol: $cmd2proto{$cmd} to New Protocol: $protocol\n";
137 $cli_stomp++;
138 }
139 $cmd2str{$cmd} = $str;
140 $cmd2defun{$cmd} = $defun_body;
141 $cmd2proto{$cmd} = $protocol;
142 $cmd2hidden{$cmd} = $hidden;
143 }
144
145 # install_element() process
146 foreach (@install) {
147 my (@element_array);
148 @element_array = split (/,/);
149
150 # Install node
151 $enode = $element_array[0];
152 $enode =~ s/^\s+//g;
153 $enode =~ s/\s+$//g;
154 ($enode) = ($enode =~ /([0-9A-Z_]+)$/);
155
156 # VTY command structure.
157 ($ecmd) = ($element_array[1] =~ /&([^\)]+)/);
158 $ecmd =~ s/^\s+//g;
159 $ecmd =~ s/\s+$//g;
160
161 # Register $ecmd
162 if (defined ($cmd2str{$ecmd})) {
163 my ($key);
164 $key = $enode . "," . $cmd2str{$ecmd};
165 $ocmd{$key} = $ecmd;
166 $odefun{$key} = $cmd2defun{$ecmd};
167
168 if ($cmd2hidden{$ecmd}) {
169 $defsh{$key} = "DEFSH_HIDDEN"
170 } else {
171 $defsh{$key} = "DEFSH"
172 }
173 push (@{$oproto{$key}}, $cmd2proto{$ecmd});
174 }
175 }
176 }
177
178 # When we have cli commands that map to the same function name, we
179 # can introduce subtle bugs due to code not being called when
180 # we think it is.
181 #
182 # If extract.pl fails with a error message and you've been
183 # modifying the cli, then go back and fix your code to
184 # not have cli command function collisions.
185 # please fix your code before submittal
186 if ($cli_stomp) {
187 warn "There are $cli_stomp command line stomps\n";
188 }
189
190 # Check finaly alive $cmd;
191 foreach (keys %odefun) {
192 my ($node, $str) = (split (/,/));
193 my ($cmd) = $ocmd{$_};
194 $live{$cmd} = $_;
195 }
196
197 # Output DEFSH
198 foreach (keys %live) {
199 my ($proto);
200 my ($key);
201 $key = $live{$_};
202 $proto = join ("|", @{$oproto{$key}});
203 printf "$defsh{$key} ($proto$odefun{$key})\n\n";
204 }
205
206 # Output install_element
207 print <<EOF;
208 void
209 vtysh_init_cmd ()
210 {
211 EOF
212
213 foreach (keys %odefun) {
214 my ($node, $str) = (split (/,/));
215 $cmd = $ocmd{$_};
216 $cmd =~ s/_cmd/_cmd_vtysh/;
217 printf " install_element ($node, &$cmd);\n";
218 }
219
220 print <<EOF
221 }
222 EOF