extract.pl.in 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. print <<EOF;
  26. #include <zebra.h>
  27. #include "command.h"
  28. #include "vtysh.h"
  29. EOF
  30. $ignore{'"interface IFNAME"'} = "ignore";
  31. $ignore{'"interface IFNAME " "vrf <0-65535>"'} = "ignore";
  32. $ignore{'"link-params"'} = "ignore";
  33. $ignore{'"ip vrf NAME"'} = "ignore";
  34. $ignore{'"router rip"'} = "ignore";
  35. $ignore{'"router ripng"'} = "ignore";
  36. $ignore{'"router ospf"'} = "ignore";
  37. $ignore{'"router ospf <0-65535>"'} = "ignore";
  38. $ignore{'"router ospf6"'} = "ignore";
  39. $ignore{'"router bgp " "<1-4294967295>"'} = "ignore";
  40. $ignore{'"router bgp " "<1-4294967295>" " view WORD"'} = "ignore";
  41. $ignore{'"router isis WORD"'} = "ignore";
  42. $ignore{'"router zebra"'} = "ignore";
  43. $ignore{'"address-family ipv4"'} = "ignore";
  44. $ignore{'"address-family ipv4 (unicast|multicast)"'} = "ignore";
  45. $ignore{'"address-family ipv6"'} = "ignore";
  46. $ignore{'"address-family ipv6 (unicast|multicast)"'} = "ignore";
  47. $ignore{'"address-family vpnv4"'} = "ignore";
  48. $ignore{'"address-family vpnv4 unicast"'} = "ignore";
  49. $ignore{'"address-family vpnv6"'} = "ignore";
  50. $ignore{'"address-family vpnv6 unicast"'} = "ignore";
  51. $ignore{'"address-family ipv4 vrf NAME"'} = "ignore";
  52. $ignore{'"address-family encap"'} = "ignore";
  53. $ignore{'"address-family encapv4"'} = "ignore";
  54. $ignore{'"address-family encapv6"'} = "ignore";
  55. $ignore{'"exit-address-family"'} = "ignore";
  56. $ignore{'"exit-link-params"'} = "ignore";
  57. $ignore{'"vnc defaults"'} = "ignore";
  58. $ignore{'"vnc nve-group NAME"'} = "ignore";
  59. $ignore{'"exit-vnc"'} = "ignore";
  60. $ignore{'"key chain WORD"'} = "ignore";
  61. $ignore{'"key <0-2147483647>"'} = "ignore";
  62. $ignore{'"route-map WORD (deny|permit) <1-65535>"'} = "ignore";
  63. $ignore{'"show route-map"'} = "ignore";
  64. $ignore{'"line vty"'} = "ignore";
  65. $ignore{'"who"'} = "ignore";
  66. $ignore{'"terminal monitor"'} = "ignore";
  67. $ignore{'"terminal no monitor"'} = "ignore";
  68. $ignore{'"show history"'} = "ignore";
  69. my $cli_stomp = 0;
  70. foreach (@ARGV) {
  71. $file = $_;
  72. open (FH, "@CPP@ -DHAVE_CONFIG_H -DVTYSH_EXTRACT_PL -DHAVE_IPV6 -I@top_builddir@ -I@srcdir@/ -I@srcdir@/.. -I@top_srcdir@/lib -I@top_builddir@/lib -I@top_srcdir@/isisd/topology @CPPFLAGS@ $file |");
  73. local $/; undef $/;
  74. $line = <FH>;
  75. close (FH);
  76. @defun = ($line =~ /(?:DEFUN|ALIAS)\s*\((.+?)\);?\s?\s?\n/sg);
  77. @install = ($line =~ /install_element\s*\(\s*[0-9A-Z_]+,\s*&[^;]*;\s*\n/sg);
  78. # DEFUN process
  79. foreach (@defun) {
  80. my (@defun_array);
  81. @defun_array = split (/,/);
  82. $defun_array[0] = '';
  83. # Actual input command string.
  84. $str = "$defun_array[2]";
  85. $str =~ s/^\s+//g;
  86. $str =~ s/\s+$//g;
  87. # Get VTY command structure. This is needed for searching
  88. # install_element() command.
  89. $cmd = "$defun_array[1]";
  90. $cmd =~ s/^\s+//g;
  91. $cmd =~ s/\s+$//g;
  92. # $protocol is VTYSH_PROTO format for redirection of user input
  93. if ($file =~ /lib\/keychain\.c$/) {
  94. $protocol = "VTYSH_RIPD";
  95. }
  96. elsif ($file =~ /lib\/routemap\.c$/) {
  97. $protocol = "VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA";
  98. }
  99. elsif ($file =~ /lib\/filter\.c$/) {
  100. $protocol = "VTYSH_ALL";
  101. }
  102. elsif ($file =~ /lib\/vrf\.c$/) {
  103. $protocol = "VTYSH_ZEBRA";
  104. }
  105. elsif ($file =~ /lib\/plist\.c$/) {
  106. if ($defun_array[1] =~ m/ipv6/) {
  107. $protocol = "VTYSH_RIPNGD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA";
  108. } else {
  109. $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_BGPD|VTYSH_ZEBRA";
  110. }
  111. }
  112. elsif ($file =~ /lib\/distribute\.c$/) {
  113. if ($defun_array[1] =~ m/ipv6/) {
  114. $protocol = "VTYSH_RIPNGD";
  115. } else {
  116. $protocol = "VTYSH_RIPD";
  117. }
  118. }
  119. elsif ($file =~ /lib\/if_rmap\.c$/) {
  120. if ($defun_array[1] =~ m/ipv6/) {
  121. $protocol = "VTYSH_RIPNGD";
  122. } else {
  123. $protocol = "VTYSH_RIPD";
  124. }
  125. }
  126. elsif ($file =~ /lib\/vty\.c$/) {
  127. $protocol = "VTYSH_ALL";
  128. }
  129. else {
  130. ($protocol) = ($file =~ /^.*\/([a-z0-9]+)\/[a-zA-Z0-9_\-]+\.c$/);
  131. $protocol = "VTYSH_" . uc $protocol;
  132. }
  133. # Append _vtysh to structure then build DEFUN again
  134. $defun_array[1] = $cmd . "_vtysh";
  135. $defun_body = join (", ", @defun_array);
  136. # $cmd -> $str hash for lookup
  137. if (exists($cmd2str{$cmd})) {
  138. warn "Duplicate CLI Function: $cmd\n";
  139. warn "\tFrom cli: $cmd2str{$cmd} to New cli: $str\n";
  140. warn "\tOriginal Protocol: $cmd2proto{$cmd} to New Protocol: $protocol\n";
  141. $cli_stomp++;
  142. }
  143. $cmd2str{$cmd} = $str;
  144. $cmd2defun{$cmd} = $defun_body;
  145. $cmd2proto{$cmd} = $protocol;
  146. }
  147. # install_element() process
  148. foreach (@install) {
  149. my (@element_array);
  150. @element_array = split (/,/);
  151. # Install node
  152. $enode = $element_array[0];
  153. $enode =~ s/^\s+//g;
  154. $enode =~ s/\s+$//g;
  155. ($enode) = ($enode =~ /([0-9A-Z_]+)$/);
  156. # VTY command structure.
  157. ($ecmd) = ($element_array[1] =~ /&([^\)]+)/);
  158. $ecmd =~ s/^\s+//g;
  159. $ecmd =~ s/\s+$//g;
  160. # Register $ecmd
  161. if (defined ($cmd2str{$ecmd})
  162. && ! defined ($ignore{$cmd2str{$ecmd}})) {
  163. my ($key);
  164. $key = $enode . "," . $cmd2str{$ecmd};
  165. $ocmd{$key} = $ecmd;
  166. $odefun{$key} = $cmd2defun{$ecmd};
  167. push (@{$oproto{$key}}, $cmd2proto{$ecmd});
  168. }
  169. }
  170. }
  171. my $bad_cli_stomps = 102;
  172. # Currently we have $bad_cli_stomps. This was determined by
  173. # running this script and counting up the collisions from what
  174. # was returned.
  175. #
  176. # When we have cli commands that map to the same function name, we
  177. # can introduce subtle bugs due to code not being called when
  178. # we think it is.
  179. #
  180. # If extract.pl fails with a error message and you've been
  181. # modifying the cli, then go back and fix your code to
  182. # not have cli command function collisions.
  183. #
  184. # If you've removed a cli overwrite, you can safely subtract
  185. # one from $bad_cli_stomps. If you've added to the problem
  186. # please fix your code before submittal
  187. if ($cli_stomp != $bad_cli_stomps) {
  188. warn "Expected $bad_cli_stomps command line stomps, but got $cli_stomp instead\n";
  189. exit $cli_stomp;
  190. }
  191. # Check finaly alive $cmd;
  192. foreach (keys %odefun) {
  193. my ($node, $str) = (split (/,/));
  194. my ($cmd) = $ocmd{$_};
  195. $live{$cmd} = $_;
  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 ($proto$odefun{$key})\n\n";
  204. }
  205. # Output install_element
  206. print <<EOF;
  207. void
  208. vtysh_init_cmd (void)
  209. {
  210. EOF
  211. foreach (keys %odefun) {
  212. my ($node, $str) = (split (/,/));
  213. $cmd = $ocmd{$_};
  214. $cmd =~ s/_cmd/_cmd_vtysh/;
  215. printf " install_element ($node, &$cmd);\n";
  216. }
  217. print <<EOF
  218. }
  219. EOF