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
24print <<EOF;
25#include <zebra.h>
26#include "command.h"
27#include "vtysh.h"
28
29EOF
30
31$ignore{'"interface IFNAME"'} = "ignore";
32$ignore{'"ip vrf NAME"'} = "ignore";
33$ignore{'"router rip"'} = "ignore";
34$ignore{'"router ripng"'} = "ignore";
35$ignore{'"router ospf"'} = "ignore";
36$ignore{'"router ospf <0-65535>"'} = "ignore";
37$ignore{'"router ospf6"'} = "ignore";
38$ignore{'"router bgp <1-65535>"'} = "ignore";
39$ignore{'"router bgp <1-65535> view WORD"'} = "ignore";
40$ignore{'"address-family ipv4"'} = "ignore";
41$ignore{'"address-family ipv4 (unicast|multicast)"'} = "ignore";
42$ignore{'"address-family ipv6"'} = "ignore";
43$ignore{'"address-family ipv6 unicast"'} = "ignore";
44$ignore{'"address-family vpnv4"'} = "ignore";
45$ignore{'"address-family vpnv4 unicast"'} = "ignore";
46$ignore{'"address-family ipv4 vrf NAME"'} = "ignore";
47$ignore{'"exit-address-family"'} = "ignore";
48$ignore{'"key chain WORD"'} = "ignore";
49$ignore{'"key <0-2147483647>"'} = "ignore";
50$ignore{'"route-map WORD (deny|permit) <1-65535>"'} = "ignore";
51$ignore{'"show route-map"'} = "ignore";
52
53foreach (@ARGV) {
54    $file = $_;
55
56    open (FH, "cpp -DHAVE_CONFIG_H -DVTYSH_EXTRACT_PL -I. -I.. -I../lib $file |");
57    local $/; undef $/;
58    $line = <FH>;
59    close (FH);
60
61    @defun = ($line =~ /(?:DEFUN|ALIAS)\s*\((.+?)\);?\s?\s?\n/sg);
62    @install = ($line =~ /install_element \(\s*[0-9A-Z_]+,\s*&[^;]*;\s*\n/sg);
63
64    # $protocol is VTYSH_PROTO format for redirection of user input
65    if ($file =~ /lib/) {
66	if ($file =~ /keychain.c/) {
67	    $protocol = "VTYSH_RIPD";
68	}
69	if ($file =~ /routemap.c/) {
70	    $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_BGPD";
71	}
72	if ($file =~ /filter.c/) {
73	    $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_BGPD";
74	}
75	if ($file =~ /plist.c/) {
76	    $protocol = "VTYSH_RIPD|VTYSH_BGPD";
77	}
78    } else {
79	($protocol) = ($file =~ /\/([a-z0-9]+)/);
80	$protocol = "VTYSH_" . uc $protocol;
81    }
82
83    # DEFUN process
84    foreach (@defun) {
85	my (@defun_array);
86	@defun_array = split (/,/);
87	$defun_array[0] = '';
88
89
90	# Actual input command string.
91	$str = "$defun_array[2]";
92	$str =~ s/^\s+//g;
93	$str =~ s/\s+$//g;
94
95	# Get VTY command structure.  This is needed for searching
96	# install_element() command.
97	$cmd = "$defun_array[1]";
98	$cmd =~ s/^\s+//g;
99	$cmd =~ s/\s+$//g;
100
101	# Append _vtysh to structure then build DEFUN again
102	$defun_array[1] = $cmd . "_vtysh";
103	$defun_body = join (", ", @defun_array);
104
105	# $cmd -> $str hash for lookup
106	$cmd2str{$cmd} = $str;
107	$cmd2defun{$cmd} = $defun_body;
108	$cmd2proto{$cmd} = $protocol;
109    }
110
111    # install_element() process
112    foreach (@install) {
113	my (@element_array);
114	@element_array = split (/,/);
115
116	# Install node
117	$enode = $element_array[0];
118	$enode =~ s/^\s+//g;
119	$enode =~ s/\s+$//g;
120	($enode) = ($enode =~ /([0-9A-Z_]+)$/);
121
122	# VTY command structure.
123	($ecmd) = ($element_array[1] =~ /&([^\)]+)/);
124	$ecmd =~ s/^\s+//g;
125	$ecmd =~ s/\s+$//g;
126
127	# Register $ecmd
128	if (defined ($cmd2str{$ecmd})
129	    && ! defined ($ignore{$cmd2str{$ecmd}})) {
130	    my ($key);
131	    $key = $enode . "," . $cmd2str{$ecmd};
132	    $ocmd{$key} = $ecmd;
133	    $odefun{$key} = $cmd2defun{$ecmd};
134	    push (@{$oproto{$key}}, $cmd2proto{$ecmd});
135	}
136    }
137}
138
139# Check finaly alive $cmd;
140foreach (keys %odefun) {
141    my ($node, $str) = (split (/,/));
142    my ($cmd) = $ocmd{$_};
143    $live{$cmd} = $_;
144}
145
146# Output DEFSH
147foreach (keys %live) {
148    my ($proto);
149    my ($key);
150    $key = $live{$_};
151    $proto = join ("|", @{$oproto{$key}});
152    printf "DEFSH ($proto$odefun{$key})\n\n";
153}
154
155# Output install_element
156print <<EOF;
157void
158vtysh_init_cmd ()
159{
160EOF
161
162foreach (keys %odefun) {
163    my ($node, $str) = (split (/,/));
164    $cmd = $ocmd{$_};
165    $cmd =~ s/_cmd/_cmd_vtysh/;
166    printf "  install_element ($node, &$cmd);\n";
167}
168
169print <<EOF
170}
171EOF
172