]> git.proxmox.com Git - mirror_frr.git/blame - vtysh/extract.pl.in
vtysh: null dereference (Coverity 1469896)
[mirror_frr.git] / vtysh / extract.pl.in
CommitLineData
fc9d0745 1#! @PERL@
718e3744 2##
b63dc1f3 3## @configure_input@
4##
718e3744 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
26print <<EOF;
27#include <zebra.h>
988225dd 28
718e3744 29#include "command.h"
988225dd
DL
30#include "linklist.h"
31
718e3744 32#include "vtysh.h"
33
34EOF
35
b623cda4
DS
36my $cli_stomp = 0;
37
718e3744 38foreach (@ARGV) {
39 $file = $_;
40
56c1f7d8 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 |");
718e3744 42 local $/; undef $/;
43 $line = <FH>;
44 close (FH);
45
abaaab4e
DW
46 # ?: makes a group non-capturing
47 @defun = ($line =~ /((?:DEFUN|DEFUN_HIDDEN|ALIAS|ALIAS_HIDDEN)\s*\(.+?\));?\s?\s?\n/sg);
0b42c708 48 @install = ($line =~ /install_element\s*\(\s*[0-9A-Z_]+,\s*&[^;]*;\s*\n/sg);
718e3744 49
718e3744 50 # DEFUN process
51 foreach (@defun) {
abaaab4e
DW
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.
8d9e99a6 55 $_ =~ /^(.*?)\s*\((.*)\)$/s;
abaaab4e
DW
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 }
718e3744 66
abaaab4e 67 $defun_array[0] = '';
718e3744 68
abaaab4e
DW
69 # Actual input command string.
70 $str = "$defun_array[2]";
71 $str =~ s/^\s+//g;
72 $str =~ s/\s+$//g;
718e3744 73
abaaab4e
DW
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;
718e3744 79
68980084 80 # $protocol is VTYSH_PROTO format for redirection of user input
844ee104
JT
81 if ($file =~ /lib\/keychain\.c$/) {
82 $protocol = "VTYSH_RIPD";
83 }
84 elsif ($file =~ /lib\/routemap\.c$/) {
1753f727 85 $protocol = "VTYSH_RMAP";
844ee104 86 }
abaaab4e 87 elsif ($file =~ /lib\/vrf\.c$/) {
6f2f0be8 88 $protocol = "VTYSH_ALL";
abaaab4e 89 }
f5d20fdb
PG
90 elsif ($file =~ /lib\/logicalrouter\.c$/) {
91 $protocol = "VTYSH_ALL";
92 }
844ee104
JT
93 elsif ($file =~ /lib\/filter\.c$/) {
94 $protocol = "VTYSH_ALL";
95 }
62ff928b
DS
96 elsif ($file =~ /lib\/agentx\.c$/) {
97 $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA";
98 }
13460c44
FL
99 elsif ($file =~ /lib\/ns\.c$/) {
100 $protocol = "VTYSH_ZEBRA";
101 }
e5c83d9b
DS
102 elsif ($file =~ /lib\/nexthop_group\.c$/) {
103 $protocol = "VTYSH_PBRD";
104 }
844ee104
JT
105 elsif ($file =~ /lib\/plist\.c$/) {
106 if ($defun_array[1] =~ m/ipv6/) {
1753f727 107 $protocol = "VTYSH_RIPNGD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ZEBRA|VTYSH_BABELD|VTYSH_ISISD";
844ee104 108 } else {
1753f727 109 $protocol = "VTYSH_RIPD|VTYSH_OSPFD|VTYSH_BGPD|VTYSH_ZEBRA|VTYSH_PIMD|VTYSH_EIGRPD|VTYSH_BABELD|VTYSH_ISISD";
844ee104
JT
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 }
65efcfce
LB
129 elsif ($file =~ /librfp\/.*\.c$/ || $file =~ /rfapi\/.*\.c$/) {
130 $protocol = "VTYSH_BGPD";
131 }
abaaab4e 132 else {
0e82d0e1 133 ($protocol) = ($file =~ /^.*\/([a-z0-9]+)\/[a-zA-Z0-9_\-]+\.c$/);
68980084 134 $protocol = "VTYSH_" . uc $protocol;
135 }
136
abaaab4e
DW
137 # Append _vtysh to structure then build DEFUN again
138 $defun_array[1] = $cmd . "_vtysh";
139 $defun_body = join (", ", @defun_array);
718e3744 140
b623cda4
DS
141 # $cmd -> $str hash for lookup
142 if (exists($cmd2str{$cmd})) {
143 warn "Duplicate CLI Function: $cmd\n";
144 warn "\tFrom cli: $cmd2str{$cmd} to New cli: $str\n";
145 warn "\tOriginal Protocol: $cmd2proto{$cmd} to New Protocol: $protocol\n";
146 $cli_stomp++;
147 }
abaaab4e
DW
148 $cmd2str{$cmd} = $str;
149 $cmd2defun{$cmd} = $defun_body;
150 $cmd2proto{$cmd} = $protocol;
151 $cmd2hidden{$cmd} = $hidden;
718e3744 152 }
153
154 # install_element() process
155 foreach (@install) {
abaaab4e
DW
156 my (@element_array);
157 @element_array = split (/,/);
158
159 # Install node
160 $enode = $element_array[0];
161 $enode =~ s/^\s+//g;
162 $enode =~ s/\s+$//g;
163 ($enode) = ($enode =~ /([0-9A-Z_]+)$/);
164
165 # VTY command structure.
166 ($ecmd) = ($element_array[1] =~ /&([^\)]+)/);
167 $ecmd =~ s/^\s+//g;
168 $ecmd =~ s/\s+$//g;
169
170 # Register $ecmd
16807aab 171 if (defined ($cmd2str{$ecmd})) {
abaaab4e
DW
172 my ($key);
173 $key = $enode . "," . $cmd2str{$ecmd};
174 $ocmd{$key} = $ecmd;
175 $odefun{$key} = $cmd2defun{$ecmd};
176
177 if ($cmd2hidden{$ecmd}) {
178 $defsh{$key} = "DEFSH_HIDDEN"
179 } else {
180 $defsh{$key} = "DEFSH"
181 }
182 push (@{$oproto{$key}}, $cmd2proto{$ecmd});
183 }
718e3744 184 }
185}
186
b623cda4
DS
187# When we have cli commands that map to the same function name, we
188# can introduce subtle bugs due to code not being called when
189# we think it is.
190#
191# If extract.pl fails with a error message and you've been
192# modifying the cli, then go back and fix your code to
193# not have cli command function collisions.
b623cda4 194# please fix your code before submittal
82f97584
DW
195if ($cli_stomp) {
196 warn "There are $cli_stomp command line stomps\n";
b623cda4
DS
197}
198
718e3744 199# Check finaly alive $cmd;
200foreach (keys %odefun) {
201 my ($node, $str) = (split (/,/));
202 my ($cmd) = $ocmd{$_};
203 $live{$cmd} = $_;
204}
205
206# Output DEFSH
74818bb1 207foreach (sort keys %live) {
718e3744 208 my ($proto);
209 my ($key);
210 $key = $live{$_};
211 $proto = join ("|", @{$oproto{$key}});
abaaab4e 212 printf "$defsh{$key} ($proto$odefun{$key})\n\n";
718e3744 213}
214
215# Output install_element
216print <<EOF;
217void
218vtysh_init_cmd ()
219{
220EOF
221
74818bb1 222foreach (sort keys %odefun) {
718e3744 223 my ($node, $str) = (split (/,/));
224 $cmd = $ocmd{$_};
225 $cmd =~ s/_cmd/_cmd_vtysh/;
226 printf " install_element ($node, &$cmd);\n";
227}
228
229print <<EOF
230}
231EOF