MailBot/IniConf.pm

filter: Statementstmt 7.8% 15 / 190 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 23.0% 3 / 13 Podpod 0.0% 0 / 10 totaltotal 5.7% 18 / 315 CC 51 SCAR 53.6
CC
51
Cov
5%
ReadConfig
420.0
new
210.0
OutputConfig
156.0
CRAP 212.3
↑ Summary blib/lib/GetWeb/Parser.pm »
1 package MailBot::IniConf; # modified from Hutton's original IniConf. --rhn
2 # package IniConf;
3  
4 # below is original copyright notice. --rhn
5  
6 # AUTHOR
7  
8 # Scott Hutton (shutton@indiana.edu)
9  
10 # COPYRIGHT
11  
12 # Copyright (c) 1996 Scott Hutton. All rights reserved. This program is
13 # free software; you can redistribute it and/or modify it under the same
14 # terms as Perl itself.
15  
16 # patched 1/97. --rhn
17  
18 26 require 5.002;
19 # $VERSION = 0.91;
20  
21 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 26 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 26 ▶use vars qw( $VERSION @instance $instnum @oldhandler @errors );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26 #
27 # Package variables
28 #
29 26 @instance = ( );
30 26 $instnum = 0;
31 26 @oldhandler = ( );
32 26 @errors = ( );
33  
34  
35 0 ▶sub new {
Subroutine new: not called CC=14 SCAR=53.5
Pod undocumented
36 0 my $class = shift;
37 0 my %parms = @_;
38  
39 0 my $errs = 0;
40 0 my @groups = ( );
41  
42 0 my $self = {};
43 0 $self->{cf} = '';
44 0 $self->{firstload} = 1;
45 0 $self->{default} = '';
46  
47 # Parse options
48 0 my($k, $v);
49 0 local $_;
50 0 ▶ while (($k, $v) = each %parms) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %parms) 0 0 0
51 0 ▶ if ($k eq '-file') {
Branch: 5 branches
BranchTrueFalseTotal
if ($k eq '-file') { } 0 0 0
elsif ($k eq '-reloadsig') { } 0 0 0
elsif ($k eq '-default') { } 0 0 0
elsif ($k eq '-nocase') { } 0 0 0
elsif ($k eq '-reloadwarn') { } 0 0 0
52 0 $self->{cf} = $v;
53 }
54 elsif ($k eq '-reloadsig') {
55 0 $v =~ s/^SIG//;
56 0 $self->{reloadsig} = uc($v);
57 }
58 elsif ($k eq '-default') {
59 0 $self->{default} = $v;
60 }
61 elsif ($k eq '-nocase') {
62 0 ▶ $self->{nocase} = $v ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 0 0 0
63 }
64 elsif ($k eq '-reloadwarn') {
65 0 ▶ $self->{reloadwarn} = $v ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 0 0 0
66 }
67 else {
68 0 carp "Unknown named parameter $k=>$v";
69 0 $errs++;
70 }
71 }
72  
73 croak "must specify -file parameter for new $class"
74 0 ▶ unless $self->{cf};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cf"} 0 0 0
75  
76 0 ▶ return undef if $errs;
Branch: 1 branch
BranchTrueFalseTotal
if $errs 0 0 0
77  
78 # Set up a signal handler if requested
79 0 my($sig, $oldhandler, $newhandler);
80 0 ▶ if ($sig = $self->{reloadsig}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig = $self->{"reloadsig"}) 0 0 0
81 0 $oldhandler[$instnum] = $SIG{$sig};
82 0 $newhandler = "${class}::SigHand_$instnum";
83 0 my $toeval = <<"EOT";
84  
85 sub $newhandler {
86 \$SIG{$sig} = 'IGNORE';
87 \$${class}::instance[$instnum]->ReadConfig;
88 if (\$oldhandler[$instnum] && \$oldhandler[$instnum] ne 'IGNORE') {
89 eval '&$oldhandler[$instnum];';
90 }
91 \$SIG{$sig} = '$newhandler'
92 }
93  
94 EOT
95
96 0 eval $toeval;
97 }
98  
99 0 bless $self, $class;
100  
101 0 $instance[$instnum++] = $self;
102  
103 0 ▶ if ($self->ReadConfig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ReadConfig) { } 0 0 0
104 0 ▶ $SIG{$sig} = $newhandler if $sig;
Branch: 1 branch
BranchTrueFalseTotal
if $sig 0 0 0
105 0 return $self;
106 } else {
107 0 return undef;
108 }
109 }
110  
111  
112 0 ▶sub val {
Subroutine val: not called CC=4 SCAR=30.0
Pod undocumented
113 0 my $self = shift;
114 0 my $sect = shift;
115 0 my $parm = shift;
116  
117 0 ▶ if ($self->{nocase}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"nocase"}) 0 0 0
118 0 $sect = lc($sect);
119 0 $parm = lc($parm);
120 }
121 # my $val = $self->{v}{$sect}{$parm} || $self->{v}{$self->{default}}{$parm};
122 0 my $val = $self->{v}{$sect}{$parm}; # --rhn
123 0 ▶ if (ref($val) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 0 0
124 0 ▶ return wantarray ? @$val : join($/, @$val);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
125 } else {
126 0 return $val;
127 }
128 }
129  
130 0 ▶sub setval {
Subroutine setval: not called CC=3 SCAR=24.8
Pod undocumented
131 0 my $self = shift;
132 0 my $sect = shift;
133 0 my $parm = shift;
134 0 my @val = @_;
135  
136 0 ▶ if (defined($self->{v}{$sect}{$parm})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'v'}{$sect}{$parm}) { } 0 0 0
137 0 ▶ if (@val > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@val > 1) { } 0 0 0
138 0 $self->{v}{$sect}{$parm} = \@val;
139 } else {
140 0 $self->{v}{$sect}{$parm} = shift @val;
141 }
142 0 return 1;
143 } else {
144 0 return undef;
145 }
146 }
147  
148 0 ▶sub ReadConfig {
Subroutine ReadConfig: not called CC=20 SCAR=60.4
Pod undocumented
149 0 my $self = shift;
150  
151 0 local *CF;
152 0 my($lineno, $sect);
153 0 my($group, $groupmem);
154 0 my($parm, $value);
155 0 my @cmts;
156 0 @errors = ( );
157  
158 # Initialize (and clear out) storage hashes
159 0 $self->{sects} = []; # Sections
160 0 $self->{groups} = {}; # Subsection lists
161 0 $self->{v} = {}; # Parameter values
162 0 $self->{sCMT} = {}; # Comments above section
163  
164 0 my $nocase = $self->{nocase};
165  
166 0 my ($ss, $mm, $hh, $DD, $MM, $YY) = (localtime(time))[0..5];
167 printf STDERR
168 "PID %d reloading config file %s at %d.%02d.%02d %02d:%02d:%02d\n",
169 $$, $self->{cf}, $YY+1900, $MM+1, $DD, $hh, $mm, $ss
170 0 ▶ unless $self->{firstload} || !$self->{reloadwarn};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"firstload"} or not $self->{"reloadwarn"} 0 0 0
Condition0%0/3
$self->{"firstload"} or not $self->{"reloadwarn"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"firstload"} or not $self->{"reloadwarn"}
$self->{"firstload"} not $self->{"reloadwarn"}
Truth table0%0/3
A or not(B)
A= $self->{"firstload"}B= not $self->{"reloadwarn"}
ABresult
1X1
011
000
171  
172 0 $self->{firstload} = 0;
173  
174 0 ▶ open(CF, $self->{cf}) || carp "open $self->{cf}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open CF, $self->{"cf"} or carp(qq[open $self->{"cf"}: $!]) 0 0 0
175 0 local $_;
176 0 my ($parm, $val);
177 0 ▶ while (<CF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CF)) 0 0 0
178 0 chop;
179 0 $lineno++;
180  
181 0 ▶ if (/^\s*$/) { # ignore blank lines
Branch: 4 branches
BranchTrueFalseTotal
if (/^\s*$/) { } 0 0 0
elsif (/^\s*[\#\;]/) { } 0 0 0
elsif (/^\s*\[([^\]]+)\]\s*$/) { } 0 0 0
elsif (() = /\s*(\S+)\s*=\s*(.*)/) { } 0 0 0
182 0 next;
183 }
184 elsif (/^\s*[\#\;]/) { # collect comments
185 0 push(@cmts, $_);
186 0 next;
187 }
188 elsif (/^\s*\[([^\]]+)\]\s*$/) { # New Section
189 0 $sect = $1;
190 0 ▶ $sect = lc($sect) if $nocase;
Branch: 1 branch
BranchTrueFalseTotal
if $nocase 0 0 0
191 0 push(@{$self->{sects}}, $sect);
192 0 ▶ if ($sect =~ /(\S+)\s+(\S+)/) { # New Group Member
Branch: 1 branch
BranchTrueFalseTotal
if ($sect =~ /(\S+)\s+(\S+)/) 0 0 0
193 0 ($group, $groupmem) = ($1, $2);
194 0 ▶ if (!defined($self->{group}{$group})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"group"}{$group}) 0 0 0
195 0 $self->{group}{$group} = [];
196 }
197 0 push(@{$self->{group}{$group}}, $groupmem);
198 }
199 0 ▶ if (!defined($self->{v}{$sect})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"v"}{$sect}) 0 0 0
200 0 ▶ $self->{sCMT}{$sect} = [@cmts] if @cmts > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @cmts > 0 0 0 0
201 0 $self->{pCMT}{$sect} = {}; # Comments above parameters
202 0 $self->{parms}{$sect} = [];
203 0 @cmts = ( );
204 0 $self->{v}{$sect} = {};
205 }
206 }
207 elsif (($parm, $val) = /\s*(\S+)\s*=\s*(.*)/) { # new parameter
208 0 ▶ $parm = lc($parm) if $nocase;
Branch: 1 branch
BranchTrueFalseTotal
if $nocase 0 0 0
209 0 $self->{pCMT}{$sect}{$parm} = [@cmts];
210 0 @cmts = ( );
211 0 ▶ if ($val =~ /^<<(.*)/) { # "here" value
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^<<(.*)/) { } 0 0 0
212 0 my $eotmark = $1;
213 0 my $foundeot = 0;
214 0 my $startline = $lineno;
215 0 my @val = ( );
216 0 ▶ while (<CF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CF)) 0 0 0
217 0 chop;
218 0 $lineno++;
219 0 ▶ if ($_ eq $eotmark) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq $eotmark) { } 0 0 0
220 0 $foundeot = 1;
221 0 last;
222 } else {
223 0 push(@val, $_);
224 }
225 }
226 0 ▶ if ($foundeot) {
Branch: 1 branch
BranchTrueFalseTotal
if ($foundeot) { } 0 0 0
227 0 $self->{v}{$sect}{$parm} = \@val;
228 0 $self->{EOT}{$sect}{$parm} = $eotmark;
229 } else {
230 0 push(@errors, sprintf('%d: %s', $startline,
231 qq#no end marker ("$eotmark") found#));
232 }
233 } else {
234 0 $self->{v}{$sect}{$parm} = $val;
235 }
236 0 push(@{$self->{parms}{$sect}}, $parm);
237 }
238 else {
239 0 push(@errors, sprintf('%d: %s', $lineno, $_));
240 }
241 }
242 0 close(CF);
243 0 ▶ @errors ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
@MailBot::IniConf::errors ? : 0 0 0
244 }
245  
246 0 ▶sub Sections {
Subroutine Sections: not called CC=1 SCAR=6.9
Pod undocumented
247 0 my $self = shift;
248 0 @{$self->{sects}};
249 }
250  
251 0 ▶sub Parameters {
Subroutine Parameters: not called CC=1 SCAR=6.9
Pod undocumented
252 0 my $self = shift;
253 0 my $sect = shift;
254 0 @{$self->{parms}{$sect}};
255 }
256  
257 0 ▶sub GroupMembers {
Subroutine GroupMembers: not called CC=1 SCAR=6.9
Pod undocumented
258 0 my $self = shift;
259 0 my $group = shift;
260  
261 0 @{$self->{group}{$group}};
262 }
263  
264 0 ▶sub WriteConfig {
Subroutine WriteConfig: not called CC=3 SCAR=24.8
Pod undocumented
265 0 my $self = shift;
266 0 my $file = shift;
267  
268 0 local(*F);
269 0 ▶ open(F, "> $file.new") || do {
Branch: 1 branch
BranchTrueFalseTotal
open F, "> $file.new" or do { carp("Unable to write temp config file ${file}: $!"); return undef } 0 0 0
270 0 carp "Unable to write temp config file $file: $!";
271 0 return undef;
272 };
273 0 my $oldfh = select(F);
274 0 $self->OutputConfig;
275 0 close(F);
276 0 select($oldfh);
277 0 ▶ rename "$file.new", $file || do {
Condition0%0/3
$file || do { carp("Unable to rename temp config file to ${file}: $!"); return undef }
l!l&&r!l&&!r
000
MC/DC0%0/2
$file || do { carp("Unable to rename temp config file to ${file}: $!"); return undef }
$file do { carp("Unable to rename temp config file to ${file}: $!"); return undef }
Truth table0%0/3
A || B
A= $fileB= do { carp("Unable to rename temp config file to ${file}: $!"); return undef }
ABresult
1X1
011
000
278 0 carp "Unable to rename temp config file to $file: $!";
279 0 return undef;
280 };
281 0 return 1;
282 }
283  
284 0 ▶sub RewriteConfig {
Subroutine RewriteConfig: not called CC=1 SCAR=6.9
Pod undocumented
285 0 my $self = shift;
286 0 $self->WriteConfig($self->{cf});
287 }
288  
289 0 ▶sub OutputConfig {
Subroutine OutputConfig: not called CC=12 SCAR=50.5
Pod undocumented
290 0 my $self = shift;
291  
292 0 my($sect, $parm, @cmts);
293 0 my $notfirst = 0;
294 0 local $_;
295 0 foreach $sect (@{$self->{sects}}) {
296 0 ▶ print "\n" if $notfirst;
Branch: 1 branch
BranchTrueFalseTotal
if $notfirst 0 0 0
297 0 $notfirst = 1;
298 0 ▶ if ((ref($self->{sCMT}{$sect}) eq 'ARRAY') &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"sCMT"}{$sect} eq "ARRAY" and @cmts = @{$self->{"sCMT"}{$sect};}) 0 0 0
Condition0%0/3
ref $self->{"sCMT"}{$sect} eq "ARRAY" and @cmts = @{$self->{"sCMT"}{$sect};}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"sCMT"}{$sect} eq "ARRAY" and @cmts = @{$self->{"sCMT"}{$sect};}
ref $self->{"sCMT"}{$sect} eq "ARRAY" @cmts = @{$self->{"sCMT"}{$sect};}
Truth table0%0/3
A and B
A= ref $self->{"sCMT"}{$sect} eq "ARRAY"B= @cmts = @{$self->{"sCMT"}{$sect};}
ABresult
0X0
100
111
299 0 (@cmts = @{$self->{sCMT}{$sect}})) {
300 0 foreach (@cmts) {
301 0 print "$_\n";
302 }
303 }
304 0 print "[$sect]\n";
305  
306 0 foreach $parm (@{$self->{parms}{$sect}}) {
307 0 ▶ if ((ref($self->{pCMT}{$sect}{$parm}) eq 'ARRAY') &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"pCMT"}{$sect}{$parm} eq "ARRAY" and @cmts = @{$self->{"pCMT"}{$sect}{$parm};}) 0 0 0
Condition0%0/3
ref $self->{"pCMT"}{$sect}{$parm} eq "ARRAY" and @cmts = @{$self->{"pCMT"}{$sect}{$parm};}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"pCMT"}{$sect}{$parm} eq "ARRAY" and @cmts = @{$self->{"pCMT"}{$sect}{$parm};}
ref $self->{"pCMT"}{$sect}{$parm} eq "ARRAY" @cmts = @{$self->{"pCMT"}{$sect}{$parm};}
Truth table0%0/3
A and B
A= ref $self->{"pCMT"}{$sect}{$parm} eq "ARRAY"B= @cmts = @{$self->{"pCMT"}{$sect}{$parm};}
ABresult
0X0
100
111
308 0 (@cmts = @{$self->{pCMT}{$sect}{$parm}})) {
309 0 foreach (@cmts) {
310 0 print "$_\n";
311 }
312 }
313 0 my $val = $self->{v}{$sect}{$parm};
314 0 ▶ if (ref($val) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 0 0
315 0 my $eotmark = $self->{EOT}{$sect}{$parm};
316 0 print "$parm= <<$eotmark\n";
317 0 foreach (@{$val}) {
318 0 print "$_\n";
319 }
320 0 print "$eotmark\n";
321 } else {
322 0 print "$parm=", $self->{v}{$sect}{$parm}, "\n";
323 }
324 }
325 }
326 }
327  
328 26 1;
↑ Summary blib/lib/GetWeb/Parser.pm »