line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Config::Secure; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
17656
|
use 5.00503; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
43
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
54
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require Exporter; |
7
|
1
|
|
|
1
|
|
10507
|
use AutoLoader qw(AUTOLOAD); |
|
1
|
|
|
|
|
1896
|
|
|
1
|
|
|
|
|
7
|
|
8
|
1
|
|
|
1
|
|
48
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $OPENED); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
159
|
|
9
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
12
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
13
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# This allows declaration use Config::Secure ':all'; |
16
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
17
|
|
|
|
|
|
|
# will save memory. |
18
|
|
|
|
|
|
|
%EXPORT_TAGS = ( 'all' => [ qw( |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
) ] ); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} }, qw(get_fh get_conf write_conf)); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
@EXPORT = qw( |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
$VERSION = '0.0.1'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Preloaded methods go here. |
31
|
1
|
|
|
1
|
|
5
|
use Fcntl; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
355
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
#F_SETFD w/ fcntl() and $^F |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
BEGIN { |
36
|
1
|
50
|
|
1
|
|
16
|
$0 = $ENV{PSC_CONFIG_CMD} if $ENV{PSC_CONFIG_CMD}; |
37
|
1
|
|
|
|
|
1103
|
$OPENED = 0; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# This routine opens the file, then memoizes itself |
41
|
|
|
|
|
|
|
sub get_fh { |
42
|
0
|
|
|
0
|
0
|
|
local $^W = 0; |
43
|
0
|
|
|
|
|
|
my($fh,$type); |
44
|
|
|
|
|
|
|
|
45
|
0
|
0
|
|
|
|
|
if($ENV{PSC_CONFIG_FH} >= 0) { |
46
|
0
|
|
|
|
|
|
$type = int $ENV{PSC_CONFIG_WRITE}; |
47
|
0
|
0
|
|
|
|
|
my $rw = ($type ? '+' : ''); |
48
|
0
|
0
|
|
|
|
|
open $fh, "${rw}<&=$ENV{PSC_CONFIG_FH}" |
49
|
|
|
|
|
|
|
or die "Could not open conf filehandle: $!"; |
50
|
0
|
|
|
|
|
|
$OPENED = 1; |
51
|
|
|
|
|
|
|
} else { |
52
|
0
|
|
|
0
|
|
|
*get_fh = sub {}; |
|
0
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
warn "No conf file available: $!"; |
54
|
0
|
|
|
|
|
|
return; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
fcntl($fh, F_GETFL, $_); |
58
|
|
|
|
|
|
|
*get_fh = sub { |
59
|
0
|
|
|
0
|
|
|
return($fh,$type); |
60
|
0
|
|
|
|
|
|
}; |
61
|
0
|
|
|
|
|
|
get_fh(); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# read conf and make it a data structure |
65
|
|
|
|
|
|
|
sub get_conf { |
66
|
0
|
|
|
0
|
0
|
|
my ($fh,$type) = get_fh(); |
67
|
0
|
|
|
|
|
|
my (%conf,%loader_comments); |
68
|
0
|
0
|
|
|
|
|
unless($fh) { |
69
|
0
|
|
|
|
|
|
warn("get_conf failed... no filehandle"); |
70
|
0
|
|
|
|
|
|
return; |
71
|
|
|
|
|
|
|
} |
72
|
0
|
|
|
|
|
|
local $_; |
73
|
0
|
|
|
|
|
|
while(<$fh>) { |
74
|
0
|
|
|
|
|
|
my($k,$v); |
75
|
0
|
0
|
|
|
|
|
next if /^\s+\#/; |
76
|
0
|
|
|
|
|
|
chomp; |
77
|
0
|
0
|
|
|
|
|
if(/^\#\s*(?:([^:]+):)?/) { |
|
|
0
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
push @{$loader_comments{$1}}, $_; |
|
0
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
} elsif((($k,$v) = split /:\s*/, $_, 2) == 2) { |
80
|
0
|
0
|
|
|
|
|
if($conf{$k}) { |
81
|
0
|
0
|
|
|
|
|
if(ref $conf{$k}) { |
82
|
0
|
|
|
|
|
|
push @{$conf{$k}}, $v; |
|
0
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
} else { |
84
|
0
|
|
|
|
|
|
$conf{$k} = [$conf{$k}, $v]; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} else { |
87
|
0
|
|
|
|
|
|
$conf{$k} = $v; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
# save the comments for the nice people |
92
|
|
|
|
|
|
|
*_comments = sub { |
93
|
0
|
|
|
0
|
|
|
\%loader_comments; |
94
|
0
|
|
|
|
|
|
}; |
95
|
|
|
|
|
|
|
|
96
|
0
|
0
|
|
|
|
|
return wantarray ? %conf : \%conf; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# write conf data back to file, if it is writeable |
100
|
|
|
|
|
|
|
sub write_conf { |
101
|
0
|
|
|
0
|
0
|
|
my ($fh,$type) = get_fh(); |
102
|
0
|
0
|
|
|
|
|
unless($type) { |
103
|
0
|
|
|
|
|
|
warn("Conf file not writeable\n"); |
104
|
0
|
|
|
|
|
|
return; |
105
|
|
|
|
|
|
|
} |
106
|
0
|
|
|
|
|
|
my %conf; |
107
|
0
|
0
|
|
|
|
|
if(@_ == 1) { |
108
|
0
|
0
|
|
|
|
|
if(ref $_[0] eq 'HASH') { |
109
|
0
|
|
|
|
|
|
%conf = %{$_[0]}; |
|
0
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
} else { |
111
|
0
|
|
|
|
|
|
warn "Need to pass write_conf a hash or hash ref"; |
112
|
0
|
|
|
|
|
|
return; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} else { |
115
|
0
|
0
|
|
|
|
|
if(@_ % 2) { |
116
|
0
|
|
|
|
|
|
warn "Odd number of arguments to write_hash. Need to pass write_conf a hash or hash ref"; |
117
|
0
|
|
|
|
|
|
return; |
118
|
|
|
|
|
|
|
} |
119
|
0
|
|
|
|
|
|
%conf = @_; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
seek($fh, 0, 0); |
123
|
0
|
|
|
|
|
|
my $c = &_comments; |
124
|
|
|
|
|
|
|
# write general comments at top |
125
|
0
|
|
|
|
|
|
for my $comment (@{$c->{''}}) { |
|
0
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
print $fh "$comment\n"; |
127
|
|
|
|
|
|
|
} |
128
|
0
|
|
|
|
|
|
print $fh "\n"; |
129
|
0
|
|
|
|
|
|
for my $k (sort keys %conf) { |
130
|
0
|
|
0
|
|
|
|
$c->{$k}[0] ||= "# $k: "; |
131
|
|
|
|
|
|
|
# write comments for indivisual keys |
132
|
0
|
|
|
|
|
|
for my $comment (@{$c->{$k}}) { |
|
0
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
print $fh "$comment\n"; |
134
|
|
|
|
|
|
|
} |
135
|
0
|
0
|
|
|
|
|
if(ref $conf{$k}) { |
136
|
0
|
|
|
|
|
|
for my $a (@{$conf{$k}}) { |
|
0
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
print $fh "$k: $a\n"; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} else { |
140
|
0
|
|
|
|
|
|
print $fh "$k: $conf{$k}\n"; |
141
|
|
|
|
|
|
|
} |
142
|
0
|
|
|
|
|
|
print $fh "\n"; |
143
|
|
|
|
|
|
|
} |
144
|
0
|
|
|
|
|
|
truncate $fh, tell $fh; |
145
|
0
|
|
|
|
|
|
1; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# close your file handles! |
149
|
|
|
|
|
|
|
END { |
150
|
1
|
50
|
|
1
|
|
205
|
if($OPENED) { |
151
|
0
|
|
|
|
|
|
my $fh = (get_fh())[0]; |
152
|
0
|
0
|
|
|
|
|
close $fh if $fh; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
1; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# Autoload methods go after =cut, and are processed by the autosplit program. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
1; |
162
|
|
|
|
|
|
|
__END__ |