summaryrefslogtreecommitdiffstats
path: root/vtysh/extract.pl.in
blob: d0b0c701a778c2ed488a0160fc96046e496c01fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#! @PERL@
##
## @configure_input@
##
## Virtual terminal interface shell command extractor.
## Copyright (C) 2000 Kunihiro Ishiguro
## 
## This file is part of GNU Zebra.
## 
## GNU Zebra is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by the
## Free Software Foundation; either version 2, or (at your option) any
## later version.
## 
## GNU Zebra is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with GNU Zebra; see the file COPYING.  If not, write to the Free
## Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.  
##

print <<EOF;
#include <zebra.h>

#include "command.h"
#include "linklist.h"

#include "vtysh/vtysh.h"

EOF

my $cli_stomp = 0;

sub scan_file {
    my ( $file, $fabricd) = @_;

    $cppadd = $fabricd ? "-DFABRICD=1" : "";

    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 |");
    local $/; undef $/;
    $line = <FH>;
    close (FH);

    # ?: makes a group non-capturing
    @defun = ($line =~ /((?:DEFUN|DEFUN_HIDDEN|ALIAS|ALIAS_HIDDEN)\s*\(.+?\));?\s?\s?\n/sg);
    @install = ($line =~ /install_element\s*\(\s*[0-9A-Z_]+,\s*&[^;]*;\s*\n/sg);

    # DEFUN process
    foreach (@defun) {
        # $_ will contain the entire string including the DEFUN, ALIAS, etc.
        # We need to extract the DEFUN/ALIAS from everything in ()s.
        # The /s at the end tells the regex to allow . to match newlines.
        $_ =~ /^(.*?)\s*\((.*)\)$/s;

        my (@defun_array);
        $defun_or_alias = $1;
        @defun_array = split (/,/, $2);

        if ($defun_or_alias =~ /_HIDDEN/) {
            $hidden = 1;
        } else {
            $hidden = 0;
        }

        $defun_array[0] = '';

        # Actual input command string.
        $str = "$defun_array[2]";
        $str =~ s/^\s+//g;
        $str =~ s/\s+$//g;

        # Get VTY command structure.  This is needed for searching
        # install_element() command.
        $cmd = "$defun_array[1]";
        $cmd =~ s/^\s+//g;
        $cmd =~ s/\s+$//g;

        if ($fabricd) {
            $cmd = "fabricd_" . $cmd;
        }

        # $protocol is VTYSH_PROTO format for redirection of user input
        if ($file =~ /lib\/keychain\.c$/) {
            $protocol = "VTYSH_RIPD|VTYSH_EIGRPD";
        }
        elsif ($file =~ /lib\/routemap\.c$/) {
            $protocol = "VTYSH_RMAP";
        }
        elsif ($file =~ /lib\/vrf\.c$/) {
            $protocol = "VTYSH_VRF";
        }
        elsif ($file =~ /lib\/if\.c$/) {
            $protocol = "VTYSH_INTERFACE";
        }
        elsif ($file =~ /lib\/filter\.c$/) {
            $protocol = "VTYSH_ALL";
        }
	elsif ($file =~ /lib\/agentx\.c$/) {
	    $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA";
	}
	elsif ($file =~ /lib\/nexthop_group\.c$/) {
	    $protocol = "VTYSH_PBRD | VTYSH_SHARPD";
	}
        elsif ($file =~ /lib\/plist\.c$/) {
            if ($defun_array[1] =~ m/ipv6/) {
                $protocol = "VTYSH_RIPNGD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA|VTYSH_BABELD|VTYSH_ISISD|VTYSH_FABRICD";
            } else {
                $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_BGPD|VTYSH_ZEBRA|VTYSH_PIMD|VTYSH_EIGRPD|VTYSH_BABELD|VTYSH_ISISD|VTYSH_FABRICD";
            }
        }
        elsif ($file =~ /lib\/distribute\.c$/) {
            if ($defun_array[1] =~ m/ipv6/) {
                $protocol = "VTYSH_RIPNGD";
            } else {
                $protocol = "VTYSH_RIPD";
            }
        }
        elsif ($file =~ /lib\/if_rmap\.c$/) {
            if ($defun_array[1] =~ m/ipv6/) {
                $protocol = "VTYSH_RIPNGD";
            } else {
                $protocol = "VTYSH_RIPD";
            }
        }
        elsif ($file =~ /lib\/vty\.c$/) {
           $protocol = "VTYSH_ALL";
        }
        elsif ($file =~ /librfp\/.*\.c$/ || $file =~ /rfapi\/.*\.c$/) {
           $protocol = "VTYSH_BGPD";
        }
        elsif ($fabricd) {
           $protocol = "VTYSH_FABRICD";
        }
        else {
           ($protocol) = ($file =~ /^.*\/([a-z0-9]+)\/[a-zA-Z0-9_\-]+\.c$/);
           $protocol = "VTYSH_" . uc $protocol;
        }

        # Append _vtysh to structure then build DEFUN again
        $defun_array[1] = $cmd . "_vtysh";
        $defun_body = join (", ", @defun_array);

	# $cmd -> $str hash for lookup
	if (exists($cmd2str{$cmd})) {
	    warn "Duplicate CLI Function: $cmd\n";
	    warn "\tFrom cli: $cmd2str{$cmd} to New cli: $str\n";
	    warn "\tOriginal Protocol: $cmd2proto{$cmd} to New Protocol: $protocol\n";
	    $cli_stomp++;
	}
        $cmd2str{$cmd} = $str;
        $cmd2defun{$cmd} = $defun_body;
        $cmd2proto{$cmd} = $protocol;
        $cmd2hidden{$cmd} = $hidden;
    }

    # install_element() process
    foreach (@install) {
        my (@element_array);
        @element_array = split (/,/);

        # Install node
        $enode = $element_array[0];
        $enode =~ s/^\s+//g;
        $enode =~ s/\s+$//g;
        ($enode) = ($enode =~ /([0-9A-Z_]+)$/);

        # VTY command structure.
        ($ecmd) = ($element_array[1] =~ /&([^\)]+)/);
        $ecmd =~ s/^\s+//g;
        $ecmd =~ s/\s+$//g;

        if ($fabricd) {
            $ecmd = "fabricd_" . $ecmd;
        }

        # Register $ecmd
        if (defined ($cmd2str{$ecmd})) {
            my ($key);
            $key = $enode . "," . $cmd2str{$ecmd};
            $ocmd{$key} = $ecmd;
            $odefun{$key} = $cmd2defun{$ecmd};

            if ($cmd2hidden{$ecmd}) {
                $defsh{$key} = "DEFSH_HIDDEN"
            } else {
                $defsh{$key} = "DEFSH"
            }
            push (@{$oproto{$key}}, $cmd2proto{$ecmd});
        }
    }
}

foreach (@ARGV) {
    if (/\/isisd\//) {
        # We scan all the IS-IS files twice, once for isisd,
        # once for fabricd. Exceptions are made for the files
        # that are not shared between the two.
        if (/isis_vty_isisd.c/) {
            scan_file($_, 0);
        } elsif (/isis_vty_fabricd.c/) {
            scan_file($_, 1);
        } else {
            scan_file($_, 0);
            scan_file($_, 1);
        }
    } else {
        scan_file($_, 0);
    }
}

# When we have cli commands that map to the same function name, we
# can introduce subtle bugs due to code not being called when
# we think it is.
#
# If extract.pl fails with a error message and you've been
# modifying the cli, then go back and fix your code to
# not have cli command function collisions.
# please fix your code before submittal
if ($cli_stomp) {
    warn "There are $cli_stomp command line stomps\n";
}

# Check finaly alive $cmd;
foreach (keys %odefun) {
    my ($node, $str) = (split (/,/));
    my ($cmd) = $ocmd{$_};
    $live{$cmd} = $_;
}

# Output DEFSH
foreach (sort keys %live) {
    my ($proto);
    my ($key);
    $key = $live{$_};
    $proto = join ("|", @{$oproto{$key}});
    printf "$defsh{$key} ($proto$odefun{$key})\n\n";
}

# Output install_element
print <<EOF;
void vtysh_init_cmd(void)
{
EOF

foreach (sort keys %odefun) {
    my ($node, $str) = (split (/,/));
    $cmd = $ocmd{$_};
    $cmd =~ s/_cmd/_cmd_vtysh/;
    printf "  install_element ($node, &$cmd);\n";
}

print <<EOF
}
EOF