extract.pl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #! /usr/bin/perl
  2. ##
  3. ## Virtual terminal interface shell command extractor.
  4. ## Copyright (C) 2000 Kunihiro Ishiguro
  5. ##
  6. ## This file is part of GNU Zebra.
  7. ##
  8. ## GNU Zebra is free software; you can redistribute it and/or modify it
  9. ## under the terms of the GNU General Public License as published by the
  10. ## Free Software Foundation; either version 2, or (at your option) any
  11. ## later version.
  12. ##
  13. ## GNU Zebra is distributed in the hope that it will be useful, but
  14. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. ## General Public License for more details.
  17. ##
  18. ## You should have received a copy of the GNU General Public License
  19. ## along with GNU Zebra; see the file COPYING. If not, write to the Free
  20. ## Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  21. ## 02111-1307, USA.
  22. ##
  23. print <<EOF;
  24. #include <zebra.h>
  25. #include "command.h"
  26. #include "vtysh.h"
  27. EOF
  28. $ignore{'"interface IFNAME"'} = "ignore";
  29. $ignore{'"ip vrf NAME"'} = "ignore";
  30. $ignore{'"router rip"'} = "ignore";
  31. $ignore{'"router ripng"'} = "ignore";
  32. $ignore{'"router ospf"'} = "ignore";
  33. $ignore{'"router ospf <0-65535>"'} = "ignore";
  34. $ignore{'"router ospf6"'} = "ignore";
  35. $ignore{'"router bgp <1-65535>"'} = "ignore";
  36. $ignore{'"router bgp <1-65535> view WORD"'} = "ignore";
  37. $ignore{'"router isis WORD"'} = "ignore";
  38. $ignore{'"address-family ipv4"'} = "ignore";
  39. $ignore{'"address-family ipv4 (unicast|multicast)"'} = "ignore";
  40. $ignore{'"address-family ipv6"'} = "ignore";
  41. $ignore{'"address-family ipv6 unicast"'} = "ignore";
  42. $ignore{'"address-family vpnv4"'} = "ignore";
  43. $ignore{'"address-family vpnv4 unicast"'} = "ignore";
  44. $ignore{'"address-family ipv4 vrf NAME"'} = "ignore";
  45. $ignore{'"exit-address-family"'} = "ignore";
  46. $ignore{'"key chain WORD"'} = "ignore";
  47. $ignore{'"key <0-2147483647>"'} = "ignore";
  48. $ignore{'"route-map WORD (deny|permit) <1-65535>"'} = "ignore";
  49. $ignore{'"show route-map"'} = "ignore";
  50. foreach (@ARGV) {
  51. $file = $_;
  52. open (FH, "cpp -DHAVE_CONFIG_H -DVTYSH_EXTRACT_PL -DHAVE_IPV6 -I. -I.. -I../lib $file |");
  53. local $/; undef $/;
  54. $line = <FH>;
  55. close (FH);
  56. @defun = ($line =~ /(?:DEFUN|ALIAS)\s*\((.+?)\);?\s?\s?\n/sg);
  57. @install = ($line =~ /install_element \(\s*[0-9A-Z_]+,\s*&[^;]*;\s*\n/sg);
  58. # DEFUN process
  59. foreach (@defun) {
  60. my (@defun_array);
  61. @defun_array = split (/,/);
  62. $defun_array[0] = '';
  63. # Actual input command string.
  64. $str = "$defun_array[2]";
  65. $str =~ s/^\s+//g;
  66. $str =~ s/\s+$//g;
  67. # Get VTY command structure. This is needed for searching
  68. # install_element() command.
  69. $cmd = "$defun_array[1]";
  70. $cmd =~ s/^\s+//g;
  71. $cmd =~ s/\s+$//g;
  72. # $protocol is VTYSH_PROTO format for redirection of user input
  73. if ($file =~ /lib/) {
  74. if ($file =~ /keychain.c/) {
  75. $protocol = "VTYSH_RIPD";
  76. }
  77. if ($file =~ /routemap.c/) {
  78. $protocol = "VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD";
  79. }
  80. if ($file =~ /filter.c/) {
  81. if ($defun_array[1] =~ m/ipv6/) {
  82. $protocol = "VTYSH_RIPNGD|VTYSH_OSPF6D|VTYSH_BGPD";
  83. } else {
  84. $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_BGPD";
  85. }
  86. }
  87. if ($file =~ /plist.c/) {
  88. if ($defun_array[1] =~ m/ipv6/) {
  89. $protocol = "VTYSH_RIPNGD|VTYSH_OSPF6D|VTYSH_BGPD";
  90. } else {
  91. $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_BGPD";
  92. }
  93. }
  94. if ($file =~ /distribute.c/) {
  95. if ($defun_array[1] =~ m/ipv6/) {
  96. $protocol = "VTYSH_RIPNGD";
  97. } else {
  98. $protocol = "VTYSH_RIPD";
  99. }
  100. }
  101. if ($file =~ /if_rmap.c/) {
  102. if ($defun_array[1] =~ m/ipv6/) {
  103. $protocol = "VTYSH_RIPNGD";
  104. } else {
  105. $protocol = "VTYSH_RIPD";
  106. }
  107. }
  108. } else {
  109. ($protocol) = ($file =~ /\/([a-z0-9]+)/);
  110. $protocol = "VTYSH_" . uc $protocol;
  111. }
  112. # Append _vtysh to structure then build DEFUN again
  113. $defun_array[1] = $cmd . "_vtysh";
  114. $defun_body = join (", ", @defun_array);
  115. # $cmd -> $str hash for lookup
  116. $cmd2str{$cmd} = $str;
  117. $cmd2defun{$cmd} = $defun_body;
  118. $cmd2proto{$cmd} = $protocol;
  119. }
  120. # install_element() process
  121. foreach (@install) {
  122. my (@element_array);
  123. @element_array = split (/,/);
  124. # Install node
  125. $enode = $element_array[0];
  126. $enode =~ s/^\s+//g;
  127. $enode =~ s/\s+$//g;
  128. ($enode) = ($enode =~ /([0-9A-Z_]+)$/);
  129. # VTY command structure.
  130. ($ecmd) = ($element_array[1] =~ /&([^\)]+)/);
  131. $ecmd =~ s/^\s+//g;
  132. $ecmd =~ s/\s+$//g;
  133. # Register $ecmd
  134. if (defined ($cmd2str{$ecmd})
  135. && ! defined ($ignore{$cmd2str{$ecmd}})) {
  136. my ($key);
  137. $key = $enode . "," . $cmd2str{$ecmd};
  138. $ocmd{$key} = $ecmd;
  139. $odefun{$key} = $cmd2defun{$ecmd};
  140. push (@{$oproto{$key}}, $cmd2proto{$ecmd});
  141. }
  142. }
  143. }
  144. # Check finaly alive $cmd;
  145. foreach (keys %odefun) {
  146. my ($node, $str) = (split (/,/));
  147. my ($cmd) = $ocmd{$_};
  148. $live{$cmd} = $_;
  149. }
  150. # Output DEFSH
  151. foreach (keys %live) {
  152. my ($proto);
  153. my ($key);
  154. $key = $live{$_};
  155. $proto = join ("|", @{$oproto{$key}});
  156. printf "DEFSH ($proto$odefun{$key})\n\n";
  157. }
  158. # Output install_element
  159. print <<EOF;
  160. void
  161. vtysh_init_cmd ()
  162. {
  163. EOF
  164. foreach (keys %odefun) {
  165. my ($node, $str) = (split (/,/));
  166. $cmd = $ocmd{$_};
  167. $cmd =~ s/_cmd/_cmd_vtysh/;
  168. printf " install_element ($node, &$cmd);\n";
  169. }
  170. print <<EOF
  171. }
  172. EOF