| 1 | #!/usr/local/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | ###################################################################### | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # DNS/Config/File/Bind9.pm | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # $Id: Bind9.pm,v 1.8 2003/02/16 10:15:32 awolf Exp $ | ||||||||||||||||||||||||||||
| 7 | # $Revision: 1.8 $ | ||||||||||||||||||||||||||||
| 8 | # $Author: awolf $ | ||||||||||||||||||||||||||||
| 9 | # $Date: 2003/02/16 10:15:32 $ | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | # Copyright (C)2001-2003 Andy Wolf. All rights reserved. | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # This library is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 14 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | ###################################################################### | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package DNS::Config::File::Bind9; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | no warnings 'portable'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use 5.6.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use vars qw(@ISA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use DNS::Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | use DNS::Config::Server; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use DNS::Config::Statement; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | @ISA = qw(DNS::Config::File); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | my $VERSION = '0.66'; | |||||||||||||||||||||||||||
| 34 | 2 | my $REVISION = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 1 | my($pkg, $file, $config) = @_; | |||||||||||||||||||||||||||
| 38 | 1 | ▶ | my $class = ref($pkg) || $pkg; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $pkg || $pkg
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg
$pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | my $self = { | |||||||||||||||||||||||||||
| 41 | 'FILE' => $file | ||||||||||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | $self->{'CONFIG'} = $config if($config); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | return $self; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=12 SCAR=38.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 1 | my($self, $file) = @_; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | my @lines = $self->read($file); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # # substitute include statements completely | ||||||||||||||||||||||||||||
| 57 | # for(my $i=0 ; defined $lines[$i] ; $i++) { | ||||||||||||||||||||||||||||
| 58 | # if($lines[$i] =~ /^\s*include\s+\"*(.+)\"*\s*\;/i) { | ||||||||||||||||||||||||||||
| 59 | # my @included = $self->read($1); | ||||||||||||||||||||||||||||
| 60 | # splice @lines, $i, 1, @included | ||||||||||||||||||||||||||||
| 61 | # } | ||||||||||||||||||||||||||||
| 62 | # } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | return undef unless(scalar @lines); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | $self->{'CONFIG'} = new DNS::Config() if(!$self->{'CONFIG'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1 | my $result; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Keep track of whether we're in a multi-line comment or not. | ||||||||||||||||||||||||||||
| 71 | 1 | my $in_long_comment = 0; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # Keep track of which line we're on. Since we increment this at | ||||||||||||||||||||||||||||
| 74 | # the end of the loop, don't use 'next' in here. | ||||||||||||||||||||||||||||
| 75 | 1 | my $cntr = 0; | |||||||||||||||||||||||||||
| 76 | 1 | for my $line (@lines) { | |||||||||||||||||||||||||||
| 77 | 7 | ▶ | if( $in_long_comment ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | # Remove stray '*' characters. | ||||||||||||||||||||||||||||
| 79 | 0 | $line =~ s/^[^\*]*\*[^\/]//g; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # See if we find the end. | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if( $line =~ /^[^\*]*\*\// ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | # We've found the end. Stip off stuff | ||||||||||||||||||||||||||||
| 84 | # leading to it, and reset the flag. | ||||||||||||||||||||||||||||
| 85 | 0 | $line =~ s/^[^\*]*\*\///; | |||||||||||||||||||||||||||
| 86 | 0 | $in_long_comment=0; | |||||||||||||||||||||||||||
| 87 | }else{ | ||||||||||||||||||||||||||||
| 88 | # We're still in the comment. Make it a | ||||||||||||||||||||||||||||
| 89 | # normal comment for now. | ||||||||||||||||||||||||||||
| 90 | 0 | $line = "# $line"; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # replace lots of space with one space. | ||||||||||||||||||||||||||||
| 95 | 7 | $line =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Remove '//' style comments. | ||||||||||||||||||||||||||||
| 98 | 7 | $line =~ s/\/\/.*$//g; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Remove '#' style comments. | ||||||||||||||||||||||||||||
| 101 | 7 | $line =~ s/\#.*$//g; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # See if we start a possibly long comment | ||||||||||||||||||||||||||||
| 104 | 7 | ▶ | if( $line =~ /\/\*/ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | # This is irritating. | ||||||||||||||||||||||||||||
| 106 | 0 | $in_long_comment = 1; | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | if( $line =~ /\/\*[^\*]*\*\// ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | $in_long_comment = 0; | |||||||||||||||||||||||||||
| 109 | 0 | $line =~ s/\/\*[^\*]*\*\///g; | |||||||||||||||||||||||||||
| 110 | }else{ | ||||||||||||||||||||||||||||
| 111 | # The end isn't on this line. Cleanup | ||||||||||||||||||||||||||||
| 112 | # this line and let it be added. | ||||||||||||||||||||||||||||
| 113 | 0 | $line =~ s/\/\*.*$//g; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # We need to insert include statements at this point, but | ||||||||||||||||||||||||||||
| 118 | # we need to know which 'directory' these possible relative- | ||||||||||||||||||||||||||||
| 119 | # path files live in. So we partially parse the lines that | ||||||||||||||||||||||||||||
| 120 | # we've got so far. Fortunately, you cannot invoke include | ||||||||||||||||||||||||||||
| 121 | # within a statement. I hope. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # This regex is also overly greedy. | ||||||||||||||||||||||||||||
| 124 | 7 | ▶ | if( $line =~ /^(.*)(include)\s+(\S+.*)\;(.*)$/ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | my $laststuff = $1; | |||||||||||||||||||||||||||
| 126 | 0 | my $incfile = $3; | |||||||||||||||||||||||||||
| 127 | 0 | my $nextstuff = $4; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # Put the final stuff to the @result. | ||||||||||||||||||||||||||||
| 130 | 0 | $result .= $laststuff; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Put this lot of stuff to the CONFIG | ||||||||||||||||||||||||||||
| 133 | 0 | $self->parse_real( $result ); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # reset $result | ||||||||||||||||||||||||||||
| 136 | 0 | $result = undef; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # Get the directory now. | ||||||||||||||||||||||||||||
| 139 | 0 | my $tdir = $self->_options_dir(); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Clean up the included file. | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if( $incfile =~ /^\"(.+)\"$/ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | $incfile = $1; | |||||||||||||||||||||||||||
| 144 | }else{ | ||||||||||||||||||||||||||||
| 145 | # Might need to revisit this. | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # Finally, why we're doing this. If this isn't | ||||||||||||||||||||||||||||
| 149 | # an absolute path, then it must be a relative | ||||||||||||||||||||||||||||
| 150 | # path. If it is relative, prepend the directory | ||||||||||||||||||||||||||||
| 151 | # name so read() can actually find the file. | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if( $incfile !~ /^\s*\// && defined( $tdir ) ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $incfile =~ m[^\s*/] and defined $tdir
MC/DC0%0/2
not $incfile =~ m[^\s*/] and defined $tdir
not $incfile =~ m[^\s*/]
defined $tdir
Truth table0%0/3
not(A) and B
A= not $incfile =~ m[^\s*/]B= defined $tdir
| |||||||||||||||||||||||||||||
| 153 | 0 | $incfile = $tdir . "/" . $incfile; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Read in the included file, and put it at the start | ||||||||||||||||||||||||||||
| 157 | # of the @lines that we have. | ||||||||||||||||||||||||||||
| 158 | 0 | my @included = $self->read($incfile); | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # I think this splice is right - insert after the | ||||||||||||||||||||||||||||
| 161 | # current line. | ||||||||||||||||||||||||||||
| 162 | 0 | splice @lines, $cntr+1, 0, @included; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # Restore the stuff after the include line. | ||||||||||||||||||||||||||||
| 165 | 0 | $line = $nextstuff; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Add the current line to the meta-results. | ||||||||||||||||||||||||||||
| 169 | 7 | $result .= $line; | |||||||||||||||||||||||||||
| 170 | 7 | $cntr++; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # Parse the remaining stuff (we might have already done this with | ||||||||||||||||||||||||||||
| 174 | # stuff before an include file.) | ||||||||||||||||||||||||||||
| 175 | 1 | $self->parse_real( $result ); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 1 | return $self; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 1 | ▶ | sub parse_real() { | ||||||||||||||||||||||||||
|
Subroutine
parse_real: called CC=5 SCAR=16.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 181 | 1 | my( $self, $result ) = (@_); | |||||||||||||||||||||||||||
| 182 | 1 | ▶ | return( undef ) unless( defined( $result ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 1 | my $tree = &analyze_brackets($result); | |||||||||||||||||||||||||||
| 185 | 1 | my @res = &analyze_statements(@$tree); | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 1 | foreach my $temp (@res) { | |||||||||||||||||||||||||||
| 188 | 1 | my @temp = @$temp; | |||||||||||||||||||||||||||
| 189 | 1 | my $type = shift @temp; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 1 | my $statement; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 1 | eval { | |||||||||||||||||||||||||||
| 194 | 1 | my $tmp = 'DNS::Config::Statement::' . ucfirst(lc $type); | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 1 | ▶ | if ( eval "require $tmp" ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 1 | $statement = $tmp->new(); | |||||||||||||||||||||||||||
| 198 | 1 | $statement->parse_tree(@temp); | |||||||||||||||||||||||||||
| 199 | }else{ | ||||||||||||||||||||||||||||
| 200 | # Doesn't exist. | ||||||||||||||||||||||||||||
| 201 | 0 | warn "Require of $tmp failed\n"; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | }; | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 1 | ▶ | if($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | #warn $@; | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | $statement = DNS::Config::Statement->new(); | |||||||||||||||||||||||||||
| 209 | 0 | $statement->parse_tree($type, @temp); | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 1 | $self->{'CONFIG'}->add($statement); | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # Iterate through the config, and pull the directory statement. | ||||||||||||||||||||||||||||
| 217 | 0 | ▶ | sub _options_dir() { | ||||||||||||||||||||||||||
|
Subroutine
_options_dir: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 0 | my @statements = $self->config->statements(); | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 0 | my $retdir = undef; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | foreach my $statement( @statements ){ | |||||||||||||||||||||||||||
| 226 | 0 | my $tref = ref( $statement ); | |||||||||||||||||||||||||||
| 227 | 0 | ▶ | next unless( $tref eq "DNS::Config::Statement::Options" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 0 | $retdir = $statement->directory(); | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | return( $retdir ); | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 236 | 0 | my($self, $file) = @_; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | ▶ | $file = $file || $self->{'FILE'}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$file || $self->{'FILE'}
MC/DC0%0/2
$file || $self->{'FILE'}
$file
$self->{'FILE'}
Truth table0%0/3
A || B
A= $fileB= $self->{'FILE'}
| |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 0 | ▶ | return undef unless($file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 0 | ▶ | return undef unless($self->{'CONFIG'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 0 | ▶ | if($file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 0 | ▶ | if(open(FILE, ">$file")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 0 | my $old_fh = select(FILE); | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | map { $_->dump() } $self->config()->statements(); | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | select($old_fh); | |||||||||||||||||||||||||||
| 250 | 0 | close FILE; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | 0 | else { return undef; } | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | else { | ||||||||||||||||||||||||||||
| 255 | 0 | map { $_->dump() } $self->config()->statements(); | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 0 | return $self; | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 1 | ▶ | sub config { | ||||||||||||||||||||||||||
|
Subroutine
config: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 262 | 1 | my($self) = @_; | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 1 | return($self->{'CONFIG'}); | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 1 | ▶ | sub analyze_brackets { | ||||||||||||||||||||||||||
|
Subroutine
analyze_brackets: called CC=10 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 268 | 1 | my($string) = @_; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 1 | my @chars = split //, $string; | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 1 | my $tree = []; | |||||||||||||||||||||||||||
| 273 | 1 | my @chunks; | |||||||||||||||||||||||||||
| 274 | my @stack; | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 1 | my %matching = ( | |||||||||||||||||||||||||||
| 277 | '(' => ')', | ||||||||||||||||||||||||||||
| 278 | '[' => ']', | ||||||||||||||||||||||||||||
| 279 | '<' => '>', | ||||||||||||||||||||||||||||
| 280 | '{' => '}', | ||||||||||||||||||||||||||||
| 281 | ); | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 1 | for my $char (@chars) { | |||||||||||||||||||||||||||
| 284 | 91 | ▶ | if(grep {$char eq $_} keys(%matching)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 285 | 2 | my $temp = []; | |||||||||||||||||||||||||||
| 286 | 2 | push @$tree, $temp; | |||||||||||||||||||||||||||
| 287 | 2 | push @chunks, $tree; | |||||||||||||||||||||||||||
| 288 | 2 | push @stack, $matching{$char}; | |||||||||||||||||||||||||||
| 289 | 2 | $tree = $temp; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | 356 | elsif(grep {$char eq $_} values(%matching)) { | |||||||||||||||||||||||||||
| 292 | 2 | my $expected = pop @stack; | |||||||||||||||||||||||||||
| 293 | 2 | ▶ | die "Invalid order !\n" if((!defined $expected) || ($char ne $expected)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $expected or $char ne $expected
MC/DC0%0/2
not defined $expected or $char ne $expected
not defined $expected
$char ne $expected
Truth table33%1/3
not(A) or B
A= not defined $expectedB= $char ne $expected
| |||||||||||||||||||||||||||||
| 294 | 2 | $tree = pop @chunks; | |||||||||||||||||||||||||||
| 295 | 2 | ▶ | die "Unmatched closing !\n" if(!ref($tree)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | else { | ||||||||||||||||||||||||||||
| 298 | 87 | my $noe = scalar(@$tree); | |||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 87 | ▶ | if((!$noe) || (ref($$tree[$noe-1]) eq 'ARRAY')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $noe or ref $$tree[$noe - 1] eq "ARRAY"
MC/DC100%2/2
not $noe or ref $$tree[$noe - 1] eq "ARRAY"
not $noe
ref $$tree[$noe - 1] eq "ARRAY"
Truth table100%3/3
not(A) or B
A= not $noeB= ref $$tree[$noe - 1] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 301 | 5 | push @$tree, ($char); | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | else { | ||||||||||||||||||||||||||||
| 304 | 82 | $$tree[$noe-1] .= $char; | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 1 | ▶ | die "Unbalanced !\n" if(scalar @stack); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 1 | return($tree); | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 3 | ▶ | sub analyze_statements { | ||||||||||||||||||||||||||
|
Subroutine
analyze_statements: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 315 | 3 | my(@array) = @_; | |||||||||||||||||||||||||||
| 316 | 3 | my @result; | |||||||||||||||||||||||||||
| 317 | my $full; | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 3 | for my $line (@array) { | |||||||||||||||||||||||||||
| 320 | 7 | ▶ | if(!ref($line)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | 5 | $line =~ s/\s*\;\s*/\;/g; | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 5 | my(@parts) = split /;/, $line, -1; | |||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 5 | ▶ | shift @parts if(!$parts[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 5 | ▶ | if($parts[$#parts-1] eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 1 | $full = 1; | |||||||||||||||||||||||||||
| 329 | 1 | pop @parts; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | else { | ||||||||||||||||||||||||||||
| 332 | 4 | $full = 0; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 5 | for my $temp (@parts) { | |||||||||||||||||||||||||||
| 336 | 7 | ▶ | if($temp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | 5 | $temp =~ s/^\s*//g; | |||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 5 | my @chunks = split / /, $temp; | |||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 5 | push @result, (\@chunks); | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | else { | ||||||||||||||||||||||||||||
| 346 | 2 | my @statements = &analyze_statements(@$line); | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 2 | my @temp; | |||||||||||||||||||||||||||
| 349 | 2 | ▶ | if(!$full) { my $temp = pop @result; @temp = @$temp; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 2 | push @temp, (\@statements); | |||||||||||||||||||||||||||
| 351 | 2 | push @result, (\@temp); | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 3 | return(@result); | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 2 | 1; | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | __END__ | ||||||||||||||||||||||||||||