Config/Reader.pm

filter: Statementstmt 91.6% 772 / 842 Branchbran 80.9% 377 / 466 Conditioncond 77.7% 140 / 180 MC/DCmcdc 64.3% 65 / 101 Subroutinesub 96.7% 30 / 31 Podpod 100% 14 / 14 totaltotal 85.5% 1398 / 1634 CC 299 SCAR 34.3
CC
299
Cov
87%
_execute_backquoted_cmd
56.0
apply_modifier
44.6
parse_line
44.3
CRAP 30.9
« Config/Options.pm ↑ Summary Config.pm »
1 ###
2 ### Copyright (c) 2007 - 2026 Curtis Leach. All rights reserved.
3 ###
4 ### Module: Advanced::Config::Reader
5  
6 =head1 NAME
7  
8 Advanced::Config::Reader - Reader manager for L<Advanced::Config>.
9  
10 =head1 SYNOPSIS
11  
12 use Advanced::Config::Reader;
13 or
14 require Advanced::Config::Reader;
15  
16 =head1 DESCRIPTION
17  
18 F<Advanced::Config::Reader> is a helper module to L<Advanced::Config>. So it
19 should be very rare to directly call any methods defined by this module.
20  
21 This module manages reading the requested config file into memory and parsing
22 it for use by L<Advanced::Config>.
23  
24 Each config file is highly customizable. Where you are allowed to alter the
25 comment char from B<#> to anything you like, such as B<;;>. The same is true
26 for things like the assignment operator (B<=>), and many other character
27 sequences with special meaning to this module.
28  
29 So to avoid confusion, when I talk about a feature, I'll talk about it's default
30 appearance and let it be safely assumed that the same will hold true if you've
31 overridden it's default character sequence with something else. Such as when
32 discussing comments as 'B<#>', even though you could have overridden it as
33 'B<;*;>'. See L<Advanced::Config::Options> for a list of symbols you can
34 overrides.
35  
36 You are also allowed to surround your values with balanced quotes or leave them
37 off entirely. The only time you must surround your value with quotes is when
38 you want to preserve leading or trailing spaces in your value. Without balanced
39 quotes these spaces are trimmed off. Also if you need a comment symbol in your
40 tag's value, the entire value must be surrounded by quotes! Finally, unbalanced
41 quotes can behave very strangly and are not stripped off.
42  
43 So in general white space in your config file is basically ignored unless it's
44 surrounded by printable chars or quotes.
45  
46 Sorry you can't use a comment symbol as part of your tag's name.
47  
48 See L<Advanced::Config::Examples> for some sample config files. You may also
49 find a lot of example config files in the package you downloaded from CPAN to
50 install this module from under I<t/config>.
51  
52 =head1 FUNCTIONS
53  
54 =over 4
55  
56 =cut
57  
58 package Advanced::Config::Reader;
59  
60 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 27 ▶use vars qw( @ISA @EXPORT @EXPORT_OK $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
64 27 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 27 ▶use Advanced::Config::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 27 ▶use Advanced::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 27 ▶use Fred::Fish::DBUG 2.09 qw / on_if_set ADVANCED_CONFIG_FISH /;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 27 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 27 $VERSION = "1.15";
74 27 @ISA = qw( Exporter );
75  
76 27 @EXPORT = qw( read_config source_file make_new_section parse_line
77 expand_variables apply_modifier parse_for_variables
78 format_section_line format_tag_value_line format_encrypt_cmt
79 encrypt_config_file_details decrypt_config_file_details );
80  
81 27 @EXPORT_OK = qw( );
82  
83 27 my $skip_warns_due_to_make_test;
84 my %global_sections;
85 27 my $gUserName;
86  
87 # ==============================================================
88 # NOTE: It is extreemly dangerous to reference Advanced::Config
89 # internals in this code. Avoid where possible!!!
90 # Ask for copies from the module instead.
91 # ==============================================================
92 # Any other module initialization done here ...
93 # This block references initializations done in my other modules.
94 BEGIN
95 27 ▶{
Subroutine BEGIN: called CC=4 SCAR=15.2
96 27 DBUG_ENTER_FUNC ();
97  
98 # What we call our default section ...
99 27 $global_sections{DEFAULT} = Advanced::Config::Options::DEFAULT_SECTION_NAME;
100 27 $global_sections{OVERRIDE} = $global_sections{DEFAULT};
101  
102 27 $gUserName = Advanced::Config::Options::_get_user_id ();
103  
104 # Is the code being run via "make test" environment ...
105 27 ▶ if ( $ENV{PERL_DL_NONLAZY} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PERL_DL_NONLAZY"} or $ENV{"PERL_USE_UNSAFE_INC"} or $ENV{"HARNESS_ACTIVE"}) 27 0 27
Condition33%2/6
$ENV{"PERL_DL_NONLAZY"} or $ENV{"PERL_USE_UNSAFE_INC"} or $ENV{"HARNESS_ACTIVE"}
l!l&&r!l&&!r
2700
$ENV{"PERL_DL_NONLAZY"} or $ENV{"PERL_USE_UNSAFE_INC"}
l!l&&r!l&&!r
2700
MC/DC0%0/3
$ENV{"PERL_DL_NONLAZY"} or $ENV{"PERL_USE_UNSAFE_INC"} or $ENV{"HARNESS_ACTIVE"}
$ENV{"PERL_DL_NONLAZY"} $ENV{"PERL_USE_UNSAFE_INC"} $ENV{"HARNESS_ACTIVE"}
Truth table25%1/4
A or B or C
A= $ENV{"PERL_DL_NONLAZY"}B= $ENV{"PERL_USE_UNSAFE_INC"}C= $ENV{"HARNESS_ACTIVE"}
ABCresult
1XX1
01X1
0011
0000
106 $ENV{PERL_USE_UNSAFE_INC} ||
107 $ENV{HARNESS_ACTIVE} ) {
108 27 $skip_warns_due_to_make_test = 1;
109 }
110  
111 27 DBUG_VOID_RETURN ();
112 }
113  
114  
115 # ==============================================================
116 # No fish please ... (called way too often)
117 # This method is called in 2 ways:
118 # 1) By parse_line() to determine if ${ln} is a tag/value pair.
119 # 2) By everyone else to parse a known tag/value pair in ${ln}.
120 #
121 # ${ln} is in one of these 3 formats if it's a tag/value pair.
122 # tag = value
123 # export tag = value # Unix shell scripts
124 # set tag = value # Windows Batch files
125  
126 sub _split_assign
127 105549 ▶{
Subroutine _split_assign: called CC=7 SCAR=19.5
128 105549 my $rOpts = shift; # The read options ...
129 105549 my $ln = shift; # The value to split ...
130 105549 my $skip = shift; # Skip massaging the tag?
131  
132 105549 my ( $tag, $value );
133 105549 ▶ if ( is_assign_spaces ( $rOpts ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_assign_spaces($rOpts)) { } 396 105153 105549
134 396 ( $tag, $value ) = split ( " ", $ln, 2 );
135 396 $skip = 1; # This separator doesn't support the prefixes.
136 } else {
137 105153 my $assign_str = convert_to_regexp_string ($rOpts->{assign}, 1);
138 105153 ( $tag, $value ) = split ( /\s*${assign_str}\s*/, $ln, 2 );
139 }
140  
141 105549 my $export_prefix = "";
142  
143 105549 ▶ unless ( $skip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip) 47531 58018 105549
144 # Check if one of the export/set variable prefixes were used!
145 47531 ▶ if ( $tag =~ m/^(export\s+)(\S.*)$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag =~ /^(export\s+)(\S.*)$/i) { } 2 47529 47531
elsif ($tag =~ /^(set\s+)(\S.*)$/i) { } 2 47527 47529
146 2 $tag = $2; # Remove the leading "export" keyword ...
147 2 $export_prefix = $1;
148 } elsif ( $tag =~ m/^(set\s+)(\S.*)$/i ) {
149 2 $tag = $2; # Remove the leading "set" keyword ...
150 2 $export_prefix = $1;
151 }
152 }
153  
154 # Did we request case insensitive tags ... ?
155 105549 ▶ my $ci_tag = ( $rOpts->{tag_case} && defined $tag ) ? lc ($tag) : $tag;
Branch: 1 branch
BranchTrueFalseTotal
$rOpts->{'tag_case'} && defined $tag ? : 246 105303 105549
Condition100%3/3
$rOpts->{"tag_case"} and defined $tag
!ll&&!rl&&r
10524558246
MC/DC100%2/2
$rOpts->{"tag_case"} and defined $tag
$rOpts->{"tag_case"} defined $tag
Truth table100%3/3
A and B
A= $rOpts->{"tag_case"}B= defined $tag
ABresult
0X0
100
111
156  
157 105549 return ( $ci_tag, $value, $export_prefix, $tag );
158 }
159  
160  
161 # ==============================================================
162  
163 =item $sts = read_config ( $file, $config )
164  
165 This method performs the reading and parsing of the given config file and puts
166 the results into the L<Advanced::Config> object I<$config>. This object
167 provides the necessary parsing rules to use.
168  
169 If a line was too badly mangled to be parsed, it will be ignored and a warning
170 will be written to your screen.
171  
172 It returns B<1> on success and B<0> on failure.
173  
174 Please note that comments are just thrown away by this process and only
175 tag/value pairs remain afterwards. Everything else is just instructions to
176 the parser or how to group together these tag/value pairs.
177  
178 If it sees something like: export tag = value, it will export tag's value
179 to the %ENV hash for you just like it does in a Unix shell script!
180  
181 Additional modifiers can be found in the comments after a tag/value pair
182 as well.
183  
184 =cut
185  
186 # ==============================================================
187 sub read_config
188 199 ▶{
Subroutine read_config: called CC=38 SCAR=36.7
Pod documented
189 199 DBUG_ENTER_FUNC ( @_ );
190 199 my $file = shift; # The filename to read ...
191 199 my $cfg = shift; # The Advanced::Config object ...
192  
193 199 my $opts = $cfg->get_cfg_settings (); # The Read Options ...
194  
195 # Locate the parent section of the config file.
196 199 my $pcfg = $cfg->get_section ();
197  
198 # Using a variable so that we can be recursive in reading config files.
199 199 my $READ_CONFIG;
200  
201 199 DBUG_PRINT ("INFO", "Opening the config file named: %s", $file);
202  
203 199 ▶ unless ( open ($READ_CONFIG, "<", $file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $READ_CONFIG, "<", $file) 0 199 199
204 0 return DBUG_RETURN ( croak_helper ($opts,
205 "Unable to open the config file.", 0) );
206 }
207  
208 # Misuse of this option makes the config file unreadable ...
209 199 ▶ if ( $opts->{use_utf8} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"use_utf8"}) 3 196 199
210 3 binmode ($READ_CONFIG, "encoding(UTF-8)");
211 3 $pcfg->_allow_utf8 (); # Tells get_date() that wide char languages are OK!
212 }
213  
214 # Some common RegExp strings ... Done here to avoid asking repeatably ...
215 199 my $decrypt_str = convert_to_regexp_string ($opts->{decrypt_lbl});
216 199 my $encrypt_str = convert_to_regexp_string ($opts->{encrypt_lbl});
217 199 my $hide_str = convert_to_regexp_string ($opts->{hide_lbl});
218 199 my $sect_str = convert_to_regexp_string ($opts->{source_file_section_lbl});
219  
220 199 my $export_str = convert_to_regexp_string ($opts->{export_lbl});
221 my ($lb, $rb) = ( convert_to_regexp_string ($opts->{section_left}),
222 199 convert_to_regexp_string ($opts->{section_right}) );
223 199 my $assign_str = convert_to_regexp_string ($opts->{assign});
224 199 my $src_str = convert_to_regexp_string ($opts->{source});
225 my ($lv, $rv) = ( convert_to_regexp_string ($opts->{variable_left}),
226 199 convert_to_regexp_string ($opts->{variable_right}) );
227  
228 # The label separators used when searching for option labels in a comment ...
229 199 my $lbl_sep = '[\s.,$!()-]';
230  
231 # Initialize to the default secion ...
232 199 my $section = make_new_section ( $cfg, "" );
233  
234 199 my %hide_section;
235  
236 199 ▶ while ( <$READ_CONFIG> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $READ_CONFIG)) 42800 199 42999
237 42800 chomp;
238 42800 my $line = $_; # Save so can use in fish logging later on.
239  
240 42800 my ($tv, $ln, $cmt, $lq, $rq) = parse_line ( $line, $opts );
241  
242 42800 ▶ if ( $ln eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ln eq "") 8018 34782 42800
243 8018 DBUG_PRINT ("READ", "READ LINE: %s", $line);
244 8018 next; # Skip to the next line if only comments found.
245 }
246  
247 # Check for lines with no tag/value pairs in them ...
248 34782 ▶ if ( ! $tv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tv) 725 34057 34782
249 725 DBUG_PRINT ("READ", "READ LINE: %s", $line);
250  
251 # EX: . ${file} --- Sourcing in ${file} ...
252 725 ▶ if ( $ln =~ m/^${src_str}\s+(.+)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ln =~ /^$src_str\s+(.+)$/i) 54 671 725
253 54 my $src = $1;
254 54 my $def_section = "";
255 54 ▶ if ( $cmt =~ m/(^|${lbl_sep})${sect_str}(${lbl_sep}|$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmt =~ /(^|$lbl_sep)$sect_str($lbl_sep|$)/) 17 37 54
256 17 $def_section = $section;
257 }
258 54 my $res = source_file ( $cfg, $def_section, $src, $file );
259 54 ▶ return DBUG_RETURN (0) unless ( $res );
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 54 54
260 54 next;
261 }
262  
263 # EX: [ ${section} ] --- Starting a new section ...
264 671 ▶ if ( $ln =~ m/^${lb}\s*(.+?)\s*${rb}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ln =~ /^$lb\s*(.+?)\s*$rb$/) 657 14 671
265 657 $section = make_new_section ( $cfg, $1 );
266  
267 657 $hide_section{$section} = 0; # Assume not sensitive ...
268  
269 657 ▶ if ( $cmt =~ m/(^|${lbl_sep})${hide_str}(${lbl_sep}|$)/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/ or should_we_hide_sensitive_data($section)) 8 649 657
Condition66%2/3
$cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/ or should_we_hide_sensitive_data($section)
l!l&&r!l&&!r
08649
MC/DC50%1/2
$cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/ or should_we_hide_sensitive_data($section)
$cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/ should_we_hide_sensitive_data($section)
Truth table66%2/3
A or B
A= $cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/B= should_we_hide_sensitive_data($section)
ABresult
1X1
011
000
270 should_we_hide_sensitive_data ( $section ) ) {
271 8 $hide_section{$section} = 1;
272 }
273 657 next;
274 }
275  
276 # Don't know what the config file was thinking of ...
277 # Don't bother expanding any variables encountered.
278 14 DBUG_PRINT ("error", "<Previous line ignored. Unknown format!>");
279 14 next;
280 }
281  
282 # ------------------------------------------------------------------
283 # If you get here, you know it's a tag/value pair to parse ...
284 # Don't forget that any comment can include processing instructions!
285 # ------------------------------------------------------------------
286  
287 # Go to the requested section ...
288 34057 $cfg = $pcfg->get_section ( $section, 1 );
289  
290 34057 my ($tag, $value, $prefix, $t2) = _split_assign ( $opts, $ln );
291  
292 # Don't export individually if doing a batch export ...
293 # If the export option is used, invert the meaning ...
294 34057 my $export_flag = 0; # Assume not exporting this tag to %ENV ...
295 34057 ▶ if ( $prefix ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prefix) { } 4 34053 34057
elsif ($cmt =~ /(^|$lbl_sep)$export_str($lbl_sep|$)/) { } 0 34053 34053
296 4 ▶ $export_flag = $opts->{export} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'export'} ? : 0 4 4
297 } elsif ( $cmt =~ m/(^|${lbl_sep})${export_str}(${lbl_sep}|$)/ ) {
298 0 ▶ $export_flag = $opts->{export} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'export'} ? : 0 0 0
299 }
300  
301 # Is the line info sensitive & should it be hidden/masked in fish ???
302 34057 my $hide = 0;
303 34057 ▶ if ( $hide_section{$section} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($hide_section{$section} or $cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/ or $cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/ or should_we_hide_sensitive_data($tag, 1)) 13029 21028 34057
Condition100%9/9
$hide_section{$section} or $cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/ or $cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/ or should_we_hide_sensitive_data($tag, 1)
l!l&&r!l&&!r
1283419521028
$hide_section{$section} or $cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/ or $cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/
l!l&&r!l&&!r
127864821223
$hide_section{$section} or $cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/
l!l&&r!l&&!r
2641252221271
MC/DC100%4/4
$hide_section{$section} or $cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/ or $cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/ or should_we_hide_sensitive_data($tag, 1)
$hide_section{$section} $cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/ $cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/ should_we_hide_sensitive_data($tag, 1)
Truth table100%5/5
A or B or C or D
A= $hide_section{$section}B= $cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/C= $cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/D= should_we_hide_sensitive_data($tag, 1)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
304 $cmt =~ m/(^|${lbl_sep})${encrypt_str}(${lbl_sep}|$)/ ||
305 $cmt =~ m/(^|${lbl_sep})${hide_str}(${lbl_sep}|$)/ ||
306 should_we_hide_sensitive_data ( $tag, 1 ) ) {
307 13029 ▶ $hide = 1 unless ( $opts->{dbug_test_use_case_hide_override} );
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"dbug_test_use_case_hide_override"} 589 12440 13029
308 }
309  
310 34057 ▶ if ( $hide ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hide) { } 589 33468 34057
elsif ($cmt =~ /(^|$lbl_sep)$decrypt_str($lbl_sep|$)/) { } 6326 27142 33468
311 # Some random length so we can't assume the value from the mask used!
312 589 my $mask = "*"x8;
313 589 ▶ if ( $value eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq '') { } 0 589 589
314 0 ▶ if ( is_assign_spaces ( $opts ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_assign_spaces($opts)) { } 0 0 0
315 0 $line =~ s/^(\s*\S+\s+)/${1}${mask} /;
316 } else {
317 0 $line =~ s/(\s*${assign_str})\s*/${1} ${mask} /;
318 }
319 } else {
320 589 my $hide_value = convert_to_regexp_string ( $value, 1 );
321 589 ▶ if ( is_assign_spaces ( $opts ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_assign_spaces($opts)) { } 3 586 589
322 3 $line =~ s/^(\s*\S+\s+)${hide_value}/${1}${mask}/;
323 } else {
324 586 $line =~ s/(\s*${assign_str}\s*)${hide_value}/${1}${mask}/;
325 }
326 }
327  
328 } elsif ( $cmt =~ m/(^|${lbl_sep})${decrypt_str}(${lbl_sep}|$)/ ) {
329 # Don't hide the line in fish, but hide it's value processing ...
330 6326 ▶ $hide = 1 unless ( $opts->{dbug_test_use_case_hide_override} );
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"dbug_test_use_case_hide_override"} 106 6220 6326
331 }
332  
333 34057 DBUG_PRINT ("READ", "READ LINE: %s", $line);
334  
335 # Remove any balanced quotes ... (must do after hide)
336 34057 ▶ $value =~ s/^${lq}(.*)${rq}$/$1/ if ( $lq );
Branch: 1 branch
BranchTrueFalseTotal
if $lq 21072 12985 34057
337  
338 34057 ▶ if ( $tag =~ m/^(shft3+)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^(shft3+)$/i) 116 33941 34057
339 116 my $m = "You can't override special variable '${1}'."
340 . " Ignoring this line in the config file.\n";
341 116 ▶ if ( $skip_warns_due_to_make_test ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_warns_due_to_make_test) { } 116 0 116
342 116 DBUG_PRINT ("WARN", $m);
343 } else {
344 0 warn $m;
345 }
346 116 next;
347 }
348  
349 # Was the tag's value encryped?? Then we need to decrypt it ...
350 33941 my $still_encrypted = 0;
351 33941 ▶ if ( $cmt =~ m/(^|${lbl_sep})${decrypt_str}(${lbl_sep}|$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmt =~ /(^|$lbl_sep)$decrypt_str($lbl_sep|$)/) 6350 27591 33941
352 6350 $value = _reverse_escape_sequences ( $value, $opts );
353  
354 6350 ▶ if ( $opts->{disable_decryption} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'disable_decryption'}) { } 14 6336 6350
355 14 $still_encrypted = 1; # Doesn't get decrypted.
356 } else {
357 6336 $value = decrypt_value ( $value, $t2, $opts, $file );
358 }
359 }
360  
361 # See if we can expand variables in $value ???
362 33941 my $still_variables = 0;
363 33941 ▶ if ( $opts->{disable_variables} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'disable_variables'}) { } 254 33687 33941
elsif (not $still_encrypted) { } 33673 14 33687
364 254 ▶ $still_variables = ( $value =~ m/${lv}.+${rv}/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /$lv.+$rv/ ? : 84 170 254
365 } elsif ( ! $still_encrypted ) {
366 33673 ▶ ($value, $hide) = expand_variables ( $cfg, $value, $file, $hide, ($lq ? 0 : 1) );
Branch: 1 branch
BranchTrueFalseTotal
$lq ? : 20951 12722 33673
367 33673 ▶ if ( $hide == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hide == -1) 29 33644 33673
368 # $still_encrypted = $still_variables = 1;
369 29 $still_variables = 1; # Variable(s) points to encrypted data.
370 }
371 }
372  
373 # Export one value to %ENV ... (once set, can't back it out again!)
374 33941 ▶ $cfg->export_tag_value_to_ENV ( $tag, $value, $hide ) if ($export_flag);
Branch: 1 branch
BranchTrueFalseTotal
if $export_flag 4 33937 33941
375  
376 # Add to the current section in the Advanced::Config object ...
377 33941 $cfg->_base_set ($tag, $value, $file, $hide, $still_encrypted, $still_variables);
378 } # End while reading the config file ...
379  
380 199 close ( $READ_CONFIG );
381  
382 199 DBUG_RETURN (1);
383 }
384  
385  
386 # ==============================================================
387  
388 =item $boolean = source_file ($config, $def_sct, $new_file, $curr_file)
389  
390 This is a private method called by I<read_config> to source in the requested
391 config file and merge the results into the current config file.
392  
393 If I<$def_sct> is given, it will be the name of the current section that the
394 sourced in file is to use for it's default unlabeled section. If the default
395 section name has been hard coded in the config file, this value overrides it.
396  
397 The I<$new_file> may contain variables and after they are expanded the
398 source callback function is called before I<load_config()> is called.
399 See L<Advanced::Config::lookup_one_variable> for rules on variable expansion.
400  
401 If I<$new_file> is a relative path, it's a relative path from the location
402 of I<$curr_file>, not the program's current directory!
403  
404 If a source callback was set up, it will call it here.
405  
406 If I<set_config_rules ()> was called against this config object, it looks in
407 that config file for how to parse the sourced file instead of using the callback
408 code.
409  
410 This method will also handle the removal of decryption related options if new
411 ones weren't provided by the callback function. See Advanced::Config::Options
412 for more details.
413  
414 Returns B<1> if the new file successfully loaded. Else B<0> if something went
415 wrong during the load!
416  
417 =cut
418  
419 sub source_file
420 54 ▶{
Subroutine source_file: called CC=15 SCAR=30.9
Pod documented
421 54 DBUG_ENTER_FUNC (@_);
422 54 my $cfg = shift;
423 54 my $defaultSection = shift; # The new default section if not "".
424 54 my $new_file = shift; # May contain variables to expand ...
425 54 my $old_file = shift; # File we're currently parsing. (has abs path)
426  
427 54 my $ruleObj = $cfg->_get_rule_object (); # Is there a rule config file?
428  
429 54 ▶ local $global_sections{OVERRIDE} = $defaultSection if ( $defaultSection );
Branch: 1 branch
BranchTrueFalseTotal
if $defaultSection 17 37 54
430  
431 54 my $pcfg = $cfg->get_section (); # Back to the main/default section ...
432  
433 54 my $file = $new_file = expand_variables ($pcfg, $new_file, undef, undef, 1);
434  
435 54 my ( $rOpts, $sdOpts );
436 54 ▶ if ( $ruleObj ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ruleObj) { } 8 46 54
437 # Get the Read & Date options from the correct section in rule config file
438 8 my $ruleSect = $ruleObj->_get_rule_section ($new_file);
439 8 ( $rOpts, $sdOpts ) = ( $ruleSect->_get_rules_from_cfg () )[0,2];
440  
441 } else {
442 # The Current Read Options ...
443 46 $rOpts = $cfg->get_cfg_settings ();
444 }
445  
446 # Get the full name of the file we're sourcing in ...
447 54 $file = $pcfg->_fix_path ( $file, dirname ( $old_file ) );
448  
449 54 ▶ unless ( -f $file && -r _ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file and -r _) 0 54 54
Condition33%1/3
-f $file and -r _
!ll&&!rl&&r
0054
MC/DC0%0/2
-f $file and -r _
-f $file -r _
Truth table33%1/3
A and B
A= -f $fileB= -r _
ABresult
0X0
100
111
450 0 my $msg = "No such file to source in or it's unreadable ( $file )";
451 0 return DBUG_RETURN ( croak_helper ( $rOpts, $msg, 0 ) );
452 }
453  
454 54 ▶ if ( $cfg->_recursion_check ( $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->_recursion_check($file)) 2 52 54
455 2 my $msg = "Recursion detected while sourcing in file ( $new_file )";
456 2 ▶ if ( $rOpts->{trap_recursion} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{'trap_recursion'}) { } 0 2 2
457 # The request is a fatal error!
458 0 return DBUG_RETURN ( croak_helper ( $rOpts, $msg, 0 ) );
459 } else {
460 2 DBUG_PRINT ("RECURSION", $msg);
461 2 return DBUG_RETURN ( 1 ); # Just ignore the request ...
462 }
463 }
464  
465 52 my $res;
466 52 ▶ if ( $ruleObj ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ruleObj) { } 8 44 52
467 8 $res = $pcfg->_load_config_with_new_date_opts ( $file, $rOpts, $sdOpts );
468  
469 } else {
470 # The returned callback option(s) will be applied to the current
471 # settings, not the default settings if not a compete set!
472 44 my ($r_opts, $d_opts);
473 44 ▶ if ( exists $rOpts->{source_cb} && ref ( $rOpts->{source_cb} ) eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rOpts->{"source_cb"} and ref $rOpts->{"source_cb"} eq "CODE") 44 0 44
Condition33%1/3
exists $rOpts->{"source_cb"} and ref $rOpts->{"source_cb"} eq "CODE"
!ll&&!rl&&r
0044
MC/DC0%0/2
exists $rOpts->{"source_cb"} and ref $rOpts->{"source_cb"} eq "CODE"
exists $rOpts->{"source_cb"} ref $rOpts->{"source_cb"} eq "CODE"
Truth table33%1/3
A and B
A= exists $rOpts->{"source_cb"}B= ref $rOpts->{"source_cb"} eq "CODE"
ABresult
0X0
100
111
474 44 ($r_opts, $d_opts) = $rOpts->{source_cb}->( $file, $rOpts->{source_cb_opts} );
475 }
476  
477 44 ▶ if ( $rOpts->{inherit_pass_phase} && $rOpts->{pass_phrase} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"inherit_pass_phase"} and $rOpts->{"pass_phrase"}) 0 44 44
Condition33%1/3
$rOpts->{"inherit_pass_phase"} and $rOpts->{"pass_phrase"}
!ll&&!rl&&r
4400
MC/DC0%0/2
$rOpts->{"inherit_pass_phase"} and $rOpts->{"pass_phrase"}
$rOpts->{"inherit_pass_phase"} $rOpts->{"pass_phrase"}
Truth table33%1/3
A and B
A= $rOpts->{"inherit_pass_phase"}B= $rOpts->{"pass_phrase"}
ABresult
0X0
100
111
478 0 my %empty;
479 0 ▶ $r_opts = \%empty unless ( defined $r_opts );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r_opts 0 0 0
480 0 ▶ $r_opts->{pass_phrase} = $rOpts->{pass_phrase} unless ( $r_opts->{pass_phrase} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r_opts->{"pass_phrase"} 0 0 0
481 }
482  
483 44 $res = $pcfg->_load_config_with_new_date_opts ( $file, $r_opts, $d_opts );
484 }
485  
486 52 ▶ DBUG_RETURN ( (defined $res) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
defined $res ? : 52 0 52
487 }
488  
489  
490 # ==============================================================
491  
492 =item $name = make_new_section ($config, $section)
493  
494 This is a private method called by I<read_config> to create a new section
495 in the L<Advanced::Config> object if a section of that name doesn't already
496 exist.
497  
498 The I<$section> name is allowed to contain variables to expand before the
499 string is used. But those variables must be defined in the I<main> section.
500  
501 Returns the name of the section found/created in lower case.
502  
503 =cut
504  
505 sub make_new_section
506 856 ▶{
Subroutine make_new_section: called CC=6 SCAR=18.0
Pod documented
507 856 DBUG_ENTER_FUNC (@_);
508 856 my $config = shift;
509 856 my $new_name = shift;
510  
511 # Check if overriding the default section with a new name ...
512 856 ▶ if ( $new_name eq "" || $new_name eq $global_sections{DEFAULT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_name eq "" or $new_name eq $global_sections{"DEFAULT"}) 235 621 856
Condition100%3/3
$new_name eq "" or $new_name eq $global_sections{"DEFAULT"}
l!l&&r!l&&!r
19936621
MC/DC100%2/2
$new_name eq "" or $new_name eq $global_sections{"DEFAULT"}
$new_name eq "" $new_name eq $global_sections{"DEFAULT"}
Truth table100%3/3
A or B
A= $new_name eq ""B= $new_name eq $global_sections{"DEFAULT"}
ABresult
1X1
011
000
513 235 ▶ if ( $global_sections{DEFAULT} ne $global_sections{OVERRIDE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global_sections{"DEFAULT"} ne $global_sections{"OVERRIDE"}) 35 200 235
514 DBUG_PRINT ("OVERRIDE", "Overriding section '%s' with section '%s'",
515 35 $new_name, $global_sections{OVERRIDE});
516 35 $new_name = $global_sections{OVERRIDE};
517 }
518 }
519  
520 856 my $pcfg = $config->get_section (); # Back to the main section ...
521  
522 856 my $val = expand_variables ($pcfg, $new_name, undef, undef, 1);
523 856 $new_name = lc ( $val );
524  
525 # Check if the section name is already in use ...
526 856 my $old = $pcfg->get_section ( $new_name );
527 856 ▶ if ( $old ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old) 506 350 856
528 506 return DBUG_RETURN ( $old->section_name() );
529 }
530  
531 # Create the new section now that we know it's name is unique ...
532 350 my $scfg = $pcfg->create_section ( $new_name );
533  
534 350 ▶ if ( $scfg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scfg) 350 0 350
535 350 return DBUG_RETURN ( $scfg->section_name () );
536 }
537  
538 # Should never, ever happen ...
539 0 DBUG_PRINT ("WARN", "Failed to create the new section: %s.", $new_name);
540  
541 0 DBUG_RETURN (""); # This is the main/default section being returned.
542 }
543  
544  
545 # ==============================================================
546 # Allows a config file to run a random command when it's loaded into memory.
547 # Only allowed if explicity enabled & configured!
548 # Decided it's too dangerous to use, so never called outside of a POC example!
549 sub _execute_backquoted_cmd
550 0 ▶{
Subroutine _execute_backquoted_cmd: not called CC=7 SCAR=40.3
551 0 my $rOpts = shift;
552 0 my $hide = shift;
553 0 my $tag = shift;
554 0 my $value = shift;
555  
556 0 ▶ return ( $value ) unless ( $rOpts->{enable_backquotes} );
Branch: 1 branch
BranchTrueFalseTotal
unless $rOpts->{"enable_backquotes"} 0 0 0
557  
558 # Left & right backquotes ...
559 my ($lbq, $rbq) = ( convert_to_regexp_string ($rOpts->{backquote_left}, 1),
560 0 convert_to_regexp_string ($rOpts->{backquote_right}, 1) );
561  
562 0 ▶ unless ( $value =~ m/^${lbq}(.*)${rbq}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^$lbq(.*)$rbq$/) 0 0 0
563 0 return ( $value ); # No balanced backquotes detected ...
564 }
565 0 my $cmd = $1; # The command to run ...
566  
567 # DBUG_MASK_NEXT_FUNC_CALL (3) if ( $hide ); # Never hide value (cmd to run)
568 0 DBUG_ENTER_FUNC ($rOpts, $hide, $tag, $value, @_);
569 0 ▶ DBUG_MASK (0) if ( $hide ); # OK to hide the results.
Branch: 1 branch
BranchTrueFalseTotal
if $hide 0 0 0
570  
571 0 ▶ if ( $cmd =~ m/[`]/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd =~ /[`]/) { } 0 0 0
elsif ($cmd =~ /^\s*$/) { } 0 0 0
572 0 DBUG_PRINT ('INFO', 'Your command may not have backquotes (`) in it!');
573 } elsif ( $cmd =~ m/^\s*$/ ) {
574 0 DBUG_PRINT ('INFO', 'Your command must have a value!');
575  
576 } else {
577 0 die ("Someone tried to run cmd: $cmd\n");
578 # $value = `$cmd`;
579 0 ▶ $value = "" unless ( defined $value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
580 0 chomp ($value);
581 }
582  
583 0 DBUG_RETURN ($value);
584 }
585  
586  
587 # ==============================================================
588  
589 =item @ret[0..4] = parse_line ( $input, \%opts )
590  
591 This is a private method called by I<read_config> to parse each line of the
592 config file as it's read in. It's main purpose is to strip off leading and
593 trailing spaces and any comments it might find on the input line. It also
594 tells if the I<$input> contains a tag/value pair.
595  
596 It returns 5 values: ($tv_flg, $line, $comment, $lQuote, $rQuote)
597  
598 B<$tv_flg> - True if I<$line> contains a tag/value pair in it, else false.
599  
600 B<$line> - The trimmed I<$input> line minus any comments.
601  
602 B<$comment> - The comment stripped out of the original input line minus the
603 leading comment symbol(s).
604  
605 B<$lQuote> & B<rQuote> - Only set if I<$tv_flg> is true and I<$lQuote> was
606 the 1st char of the value and I<$rQuote> was the last char of the tag's value.
607 If the value wasn't surrounded by balanced quotes, both return values will be
608 the empty string B<"">.
609  
610 If these quotes are returned, it expects the caller to remove them from the
611 tag's value. The returned values for these quote chars are suitable for use as
612 is in a RegExpr. The caller must do this in order to preserve potential
613 leading/trailing spaces.
614  
615 =cut
616  
617 sub parse_line
618 57898 ▶{
Subroutine parse_line: called CC=43 SCAR=37.9
Pod documented
619 57898 DBUG_MASK_NEXT_FUNC_CALL (0); # Masks ${line}!
620 57898 DBUG_ENTER_FUNC ( @_ );
621 57898 my $line = shift;
622 57898 ▶ my $opts = (ref ($_[0]) eq "HASH" ) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 57898 0 57898
623  
624 # Mask the ${line} return value in fish ...
625 # Only gets unmasked in the test scripts: t/*.t.
626 # Always pause since by the time we detect if it should be
627 # hidden or not it's too late. We've already written it to fish!
628 57898 ▶ unless ( $opts->{dbug_test_use_case_parse_override} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"dbug_test_use_case_parse_override"}) 20732 37166 57898
629 20732 DBUG_MASK ( 1 );
630 20732 DBUG_PAUSE ();
631 }
632  
633 # Strip of leading & trailing spaces ...
634 57898 $line =~ s/^\s+//;
635 57898 $line =~ s/\s+$//;
636  
637 57898 my $default_quotes = using_default_quotes ( $opts );
638  
639 57898 my $comment = convert_to_regexp_string ($opts->{comment}, 1);
640  
641 57898 my ($tag, $value) = _split_assign ( $opts, $line, 1 );
642  
643 57898 my ($l_quote, $r_quote, $tv_pair_flag) = ("", "", 0);
644 57898 my $var_line = $line;
645  
646 57898 ▶ unless ( defined $tag && defined $value ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $tag and defined $value) { } 47933 9965 57898
if (do { $tag eq '' or $tag =~ /$comment/ }) { } 190 47743 47933
Condition100%3/3
defined $tag and defined $value
!ll&&!rl&&r
5872409347933
MC/DC100%2/2
defined $tag and defined $value
defined $tag defined $value
Truth table100%3/3
A and B
A= defined $tagB= defined $value
ABresult
0X0
100
111
647 9965 $tag = $value = undef; # It's not a tag/value pair ...
648  
649 ▶ } elsif ( $tag eq "" || $tag =~ m/${comment}/ ) {
Condition66%2/3
$tag eq "" or $tag =~ /$comment/
l!l&&r!l&&!r
019047743
MC/DC50%1/2
$tag eq "" or $tag =~ /$comment/
$tag eq "" $tag =~ /$comment/
Truth table66%2/3
A or B
A= $tag eq ""B= $tag =~ /$comment/
ABresult
1X1
011
000
650 190 $tag = $value = undef; # It's not a valid tag ...
651  
652 } else {
653 # It looks like a tag/value pair to me ...
654 47743 $tv_pair_flag = 1;
655  
656 47743 ▶ if ( $opts->{disable_quotes} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'disable_quotes'}) { } 2 47741 47743
elsif ($default_quotes) { } 44079 3662 47741
657 ; # Don't do anything ...
658  
659 } elsif ( $default_quotes ) {
660 44079 ▶ if ( $value =~ m/^(['"])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(['"])/) 26761 17318 44079
661 26761 $l_quote = $r_quote = $1; # A ' or ". (Never both)
662 }
663  
664 # User defined quotes ...
665 } else {
666 3662 my $q = convert_to_regexp_string ($opts->{quote_left}, 1);
667 3662 ▶ if ( $value =~ m/^(${q})/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^($q)/) 706 2956 3662
668 706 $l_quote = $q;
669 706 $r_quote = convert_to_regexp_string ($opts->{quote_right}, 1);
670 }
671 }
672  
673 47743 $var_line = $value;
674 }
675  
676 # Comment still in value, but still haven't proved any quotes are balanced.
677 57898 DBUG_PRINT ("DEBUG", "Tag (%s), Value (%s), Proposed Left (%s), Right (%s)",
678 $tag, $value, $l_quote, $r_quote);
679  
680 57898 my $cmts = "";
681  
682 # Was the value in the tag/value pair starting with a left quote?
683 57898 ▶ if ( $tv_pair_flag && $l_quote ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tv_pair_flag and $l_quote ne "") 27467 30431 57898
Condition100%3/3
$tv_pair_flag and $l_quote ne ""
!ll&&!rl&&r
101552027627467
MC/DC100%2/2
$tv_pair_flag and $l_quote ne ""
$tv_pair_flag $l_quote ne ""
Truth table100%3/3
A and B
A= $tv_pair_flagB= $l_quote ne ""
ABresult
0X0
100
111
684 27467 my ($q1, $val2, $q2);
685  
686 # Now check if they were balanced ...
687 27467 ▶ if ( $value =~ m/^(${l_quote})(.*)(${r_quote})(\s*${comment}.*$)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^($l_quote)(.*)($r_quote)(\s*$comment.*$)/) { } 19456 8011 27467
elsif ($value =~ /^($l_quote)(.*)($r_quote)\s*$/) { } 7947 64 8011
688 19456 ($q1, $val2, $q2, $cmts) = ($1, $2, $3, $4); # Has a comment ...
689 } elsif ( $value =~ m/^(${l_quote})(.*)(${r_quote})\s*$/ ) {
690 7947 ($q1, $val2, $q2, $cmts) = ($1, $2, $3, ""); # Has no comment ...
691 }
692  
693 # If balanced quotes were found ...
694 27467 ▶ if ( $q1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q1) 27403 64 27467
695 # If the surrounding quotes don't have quotes inside them ...
696 # IE not malformed ...
697 27403 ▶ unless ( $val2 =~ m/${l_quote}/ || $val2 =~ m/${r_quote}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val2 =~ /$l_quote/ or $val2 =~ /$r_quote/) 27399 4 27403
Condition66%2/3
$val2 =~ /$l_quote/ or $val2 =~ /$r_quote/
l!l&&r!l&&!r
4027399
MC/DC50%1/2
$val2 =~ /$l_quote/ or $val2 =~ /$r_quote/
$val2 =~ /$l_quote/ $val2 =~ /$r_quote/
Truth table66%2/3
A or B
A= $val2 =~ /$l_quote/B= $val2 =~ /$r_quote/
ABresult
1X1
011
000
698 27399 my $cmt2 = convert_to_regexp_string ($cmts);
699 27399 $cmts =~ s/^\s*${comment}\s*//; # Remove comment symbol ...
700 27399 ▶ $line =~ s/${cmt2}$// if ($cmt2 ne "" ); # Remove the comments ...
Branch: 1 branch
BranchTrueFalseTotal
if $cmt2 ne "" 19452 7947 27399
701  
702 27399 DBUG_PRINT ("LINE", "Balanced Quotes encountered for removal ...");
703 27399 return DBUG_RETURN ( $tv_pair_flag, $line, $cmts, $l_quote, $r_quote);
704 }
705 }
706 }
707  
708 # The Quotes weren't balanced, so they can no longer be removed from
709 # arround the value of what's returned!
710 30499 $l_quote = $r_quote = "";
711  
712 # ----------------------------------------------------------------------
713 # If no comments in the line, just return the trimmed string ...
714 # Both tests are needed due to custom comment/assign strings!
715 # ----------------------------------------------------------------------
716 30499 ▶ if ( $line !~ m/${comment}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /$comment/) 13403 17096 30499
717 13403 DBUG_PRINT ("LINE", "Simply no comments to worry about ...");
718 13403 return DBUG_RETURN ( $tv_pair_flag, $line, "", "", "" );
719 }
720  
721 # Handles case where a comment char embedded in the assignment string.
722 17096 ▶ if ( $tv_pair_flag && $value !~ m/${comment}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tv_pair_flag and not $value =~ /$comment/) 65 17031 17096
Condition100%3/3
$tv_pair_flag and not $value =~ /$comment/
!ll&&!rl&&r
33521367965
MC/DC100%2/2
$tv_pair_flag and not $value =~ /$comment/
$tv_pair_flag not $value =~ /$comment/
Truth table100%3/3
A and not(B)
A= $tv_pair_flagB= not $value =~ /$comment/
ABresult
0X0
100
111
723 65 DBUG_PRINT ("LINE", "Simply no comments in the value to worry about ...");
724 65 return DBUG_RETURN ( $tv_pair_flag, $line, "", "", "" );
725 }
726  
727 # ----------------------------------------------------------------------
728 # If not protected by balanced quotes, verify the comment symbol detected
729 # isn't actually a variable modifier. Variables are allowed in most places
730 # in the config file, not just in tag/value pairs.
731 # ----------------------------------------------------------------------
732  
733 # The left & right anchor points for variable substitution ...
734 17031 my $lvar = convert_to_regexp_string ($opts->{variable_left}, 1);
735 17031 my $rvar = convert_to_regexp_string ($opts->{variable_right}, 1);
736  
737 # Determine what value to use in variable substitutions that doesn't include
738 # a variable tag, or a comment tag, or a value in the $line.
739 17031 my $has_no_cmt;
740 17031 foreach ("A" .. "Z", "@") {
741 17031 $has_no_cmt = ${_}x10;
742 17031 ▶ last unless ( $has_no_cmt =~ m/${comment}/ ||
Branch: 1 branch
BranchTrueFalseTotal
unless $has_no_cmt =~ /$comment/ or $has_no_cmt =~ /$lvar/ or $has_no_cmt =~ /$rvar/ or $line =~ /$has_no_cmt/ 17031 0 17031
Condition33%3/9
$has_no_cmt =~ /$comment/ or $has_no_cmt =~ /$lvar/ or $has_no_cmt =~ /$rvar/ or $line =~ /$has_no_cmt/
l!l&&r!l&&!r
0017031
$has_no_cmt =~ /$comment/ or $has_no_cmt =~ /$lvar/ or $has_no_cmt =~ /$rvar/
l!l&&r!l&&!r
0017031
$has_no_cmt =~ /$comment/ or $has_no_cmt =~ /$lvar/
l!l&&r!l&&!r
0017031
MC/DC0%0/4
$has_no_cmt =~ /$comment/ or $has_no_cmt =~ /$lvar/ or $has_no_cmt =~ /$rvar/ or $line =~ /$has_no_cmt/
$has_no_cmt =~ /$comment/ $has_no_cmt =~ /$lvar/ $has_no_cmt =~ /$rvar/ $line =~ /$has_no_cmt/
Truth table20%1/5
A or B or C or D
A= $has_no_cmt =~ /$comment/B= $has_no_cmt =~ /$lvar/C= $has_no_cmt =~ /$rvar/D= $line =~ /$has_no_cmt/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
743 $has_no_cmt =~ m/${lvar}/ ||
744 $has_no_cmt =~ m/${rvar}/ ||
745 $line =~ m/${has_no_cmt}/ );
746 }
747 17031 ▶ if ( $has_no_cmt eq "@"x10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_no_cmt eq "\@\@\@\@\@\@\@\@\@\@") 0 17031 17031
748 0 warn ("May be having variable substitution issues in parse_line()!\n");
749 }
750  
751 # Strip out all the variables from the value ...
752 # Assumes processing variables from left to right ...
753 # Need to evaluate even if variables are disabled to parse correctly ...
754 17031 my @parts = parse_for_variables ($var_line, 1, $opts);
755 17031 my $cmt_found = 0;
756 17031 my $count_var = 0;
757 17031 my @data;
758 17031 ▶ while (defined $parts[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parts[0]) 1667 16816 18483
759 1667 $cmt_found = $parts[3];
760 1667 push (@data, $var_line);
761 1667 ▶ last if ($cmt_found);
Branch: 1 branch
BranchTrueFalseTotal
if $cmt_found 215 1452 1667
762 1452 $var_line = $parts[0] . $has_no_cmt . $parts[2];
763 1452 @parts = parse_for_variables ($var_line, 1, $opts);
764 1452 ++$count_var;
765 }
766 17031 push (@data, $var_line);
767  
768 17031 my $unbalanced_leading_var_anchor_with_comments = 0;
769 17031 ▶ if ( $cmt_found && $parts[0] =~ m/(\s*${comment}\s*)(.*$)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmt_found and $parts[0] =~ /(\s*$comment\s*)(.*$)/) { } 215 16816 17031
elsif ($count_var) { } 1011 15805 16816
Condition66%2/3
$cmt_found and $parts[0] =~ /(\s*$comment\s*)(.*$)/
!ll&&!rl&&r
168160215
MC/DC50%1/2
$cmt_found and $parts[0] =~ /(\s*$comment\s*)(.*$)/
$cmt_found $parts[0] =~ /(\s*$comment\s*)(.*$)/
Truth table66%2/3
A and B
A= $cmt_foundB= $parts[0] =~ /(\s*$comment\s*)(.*$)/
ABresult
0X0
100
111
770 # parts[1] is parts[7] trimmed ... so join back together with untrimmed.
771 $cmts = $2 . $opts->{variable_left} . $parts[7]
772 215 . $opts->{variable_right} . $parts[2];
773 215 my $str = convert_to_regexp_string ( $1 . $cmts );
774 215 $line =~ s/${str}$//;
775 215 DBUG_PRINT ("LINE", "Variables encountered with variables in comment ...");
776 215 return DBUG_RETURN ( $tv_pair_flag, $line, $cmts, "", "");
777 } elsif ( $count_var ) {
778 1011 ▶ if ( $var_line =~ m/(\s*${comment}\s*)(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_line =~ /(\s*$comment\s*)(.*)$/) { } 1005 6 1011
779 1005 $cmts = $2;
780 1005 ▶ if ( $cmts =~ m/${has_no_cmt}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmts =~ /$has_no_cmt/) { } 5 1000 1005
781 5 $unbalanced_leading_var_anchor_with_comments = 1;
782 } else {
783 1000 my $cmt2 = convert_to_regexp_string ($1 . $cmts);
784 1000 $line =~ s/${cmt2}$//;
785 1000 DBUG_PRINT ("LINE", "Variables encountered with constant comment ...");
786 }
787 } else {
788 6 $cmts = "";
789 6 DBUG_PRINT ("LINE", "Variables encountered without comments ...");
790 }
791  
792 1011 ▶ unless ( $unbalanced_leading_var_anchor_with_comments ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($unbalanced_leading_var_anchor_with_comments) 1006 5 1011
793 1006 return DBUG_RETURN ( $tv_pair_flag, $line, $cmts, "", "");
794 }
795 }
796  
797 # ---------------------------------------------------------------------------
798 # Corrupted variable definition with variables in the comments ...
799 # Boy things are getting difficult to parse. Reverse the previous variable
800 # substitutions until the all variables in the comments are unexpanded again!
801 # Does a greedy RegExp to grab the 1st comment string encountered.
802 # ---------------------------------------------------------------------------
803 15810 ▶ if ( $unbalanced_leading_var_anchor_with_comments ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unbalanced_leading_var_anchor_with_comments) 5 15805 15810
804 5 $cmts = "";
805 5 foreach my $l (reverse @data) {
806 17 ▶ if ( $l =~ m/\s*${comment}\s*(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /\s*$comment\s*(.*)$/) 17 0 17
807 17 $cmts = $1;
808 17 ▶ last unless ( $cmts =~ m/${has_no_cmt}/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $cmts =~ /$has_no_cmt/ 5 12 17
809 12 $cmts = "";
810 }
811 }
812  
813 5 ▶ if ( $cmts ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmts ne "") 5 0 5
814 5 my $cmt2 = convert_to_regexp_string ($cmts);
815 5 $line =~ s/\s*${comment}\s*${cmt2}$//;
816 5 DBUG_PRINT ("LINE", "Unbalanced var def encountered with var comments ...");
817 5 return DBUG_RETURN ( $tv_pair_flag, $line, $cmts, "", "");
818 }
819  
820 # If you get here, assume it's not a tag/value pair even if it is!
821 # I know I can no longer hope to parse it correctly without a test case.
822 # But I really don't think it's possible to get here anymore ...
823 0 warn ("Corrupted variable definition encountered. Can't split out the comment with variables in it correctly!\n");
824 0 return DBUG_RETURN ( 0, $line, "", "", "");
825 }
826  
827 # ----------------------------------------------------------------------
828 # No variables, no balanced quotes ...
829 # But I still think there's a comment to remove!
830 # ----------------------------------------------------------------------
831  
832 15805 ▶ if ( $tv_pair_flag && $value =~ m/(\s*${comment}\s*)(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tv_pair_flag and $value =~ /(\s*$comment\s*)(.*)$/) 12534 3271 15805
Condition66%2/3
$tv_pair_flag and $value =~ /(\s*$comment\s*)(.*)$/
!ll&&!rl&&r
3271012534
MC/DC50%1/2
$tv_pair_flag and $value =~ /(\s*$comment\s*)(.*)$/
$tv_pair_flag $value =~ /(\s*$comment\s*)(.*)$/
Truth table66%2/3
A and B
A= $tv_pair_flagB= $value =~ /(\s*$comment\s*)(.*)$/
ABresult
0X0
100
111
833 12534 $cmts = $2;
834 12534 my $cmt2 = convert_to_regexp_string ($1 . $cmts);
835 12534 $line =~ s/${cmt2}$//; # Remove the comment from the line.
836 12534 DBUG_PRINT ("LINE", "Last ditch effort to remove the comment from the value ...");
837 12534 return DBUG_RETURN ( $tv_pair_flag, $line, $cmts, "", "");
838 }
839  
840 3271 $cmts = $line;
841 3271 $line =~ s/\s*${comment}.*$//; # Strip off any comments ....
842 3271 $cmts = substr ( $cmts, length ($line) ); # Grab the comments ...
843 3271 $cmts =~ s/^\s*${comment}\s*//; # Remove comment symbol ...
844  
845 3271 DBUG_PRINT ("LINE", "Last ditch effort to remove the comment from the line ...");
846 3271 DBUG_RETURN ( $tv_pair_flag, $line, $cmts, "", "");
847 }
848  
849  
850 # ==============================================================
851  
852 =item ($v[, $h]) = expand_variables ( $config, $string[, $file[, $sensitive[, trim]]] )
853  
854 This function takes the provided I<$string> and expands any embedded variables
855 in this string similar to how it's handled by a Unix shell script.
856  
857 The optional I<$file> tells which file the string was read in from.
858  
859 The optional I<$sensitive> when set to a non-zero value is used to disable
860 B<fish> logging when it's turned on because the I<$string> being passed contains
861 sensitive information.
862  
863 The optional I<$trim> tells if you may trim the results before it's returned.
864  
865 It returns the new value $v, once all the variable substitution(s) have
866 occurred. And optionally a second return value $h that tells if B<fish> was
867 paused during the expansion of that value due to something being sensitive.
868 This 2nd return value $h is meaningless in most situations, so don't ask for it.
869  
870 All variables are defined as B<${>I<...>B<}>, where I<...> is the variable you
871 wish to substitute. If something isn't surrounded by a B<${> + B<}> pair, it's
872 not a variable.
873  
874 A config file exampe:
875 tmp1 = /tmp/work-1
876 tmp2 = /tmp/work-2
877 opt = 1
878 date = 2011-02-03
879 logs = ${tmp${opt}}/log-${date}.txt
880 date = 2012-12-13
881  
882 So when passed "${tmp${opt}}/log-${date}.txt", it would return:
883 /tmp/work-1/log-2011-02-03.txt
884 And assigned it to B<logs>.
885  
886 As you can see multiple variable substitutions may be expanded in a single
887 string as well as nested substitutions. And when the variable substitution is
888 done while reading in the config file, all the values used were defined before
889 the tag was referenced.
890  
891 Should you call this method after the config file was loaded you get slightly
892 different results. In that case the final tag value is used instead and the
893 2nd date in the above example would have been used in it's place.
894  
895 See L<Advanced::Config::lookup_one_variable> for more details on how it
896 evaluates individual variables.
897  
898 As a final note, if one or more of the referenced variables holds encrypted
899 values that haven't yet been decrypted, those variables are not resolved. But
900 all variables that don't contain encrypted data are resolved.
901  
902 =cut
903  
904 # ==============================================================
905 sub expand_variables
906 34583 ▶{
Subroutine expand_variables: called CC=25 SCAR=32.2
Pod documented
907 34583 my $config = shift; # For the current section of config obj ...
908 34583 my $value = shift; # The value to parse for variables ...
909 34583 ▶ my $file = shift || ""; # The config file the value came from ...
Condition100%2/2
shift() || ''
l!l
33673910
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
910 34583 ▶ my $mask_flag = shift || 0; # Hide/mask sensitive info written to fish?
Condition100%2/2
shift() || 0
l!l
58134002
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
911 34583 ▶ my $trim_flag = shift || 0; # Tells if we should trim the result or not.
Condition100%2/2
shift() || 0
l!l
1363220951
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
912  
913 # Only mask ${value} if ${mask_flag} is true ...
914 34583 ▶ DBUG_MASK_NEXT_FUNC_CALL (1) if ( $mask_flag );
Branch: 1 branch
BranchTrueFalseTotal
if $mask_flag 581 34002 34583
915 34583 DBUG_ENTER_FUNC ( $config, $value, $file, $mask_flag, $trim_flag, @_);
916  
917 34583 my $opts = $config->get_cfg_settings (); # The Read Options ...
918  
919 34583 my $pcfg = $config->get_section(); # Get the main/parent section to work with!
920  
921 # Don't write to Fish if we're hiding any values ...
922 34583 ▶ if ( $mask_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask_flag) 581 34002 34583
923 581 DBUG_PAUSE ();
924 581 DBUG_MASK ( 0 );
925 }
926  
927 # The 1st split of the value into it's component parts ...
928 34583 my ($left, $tag, $right, $cmt_flag, $mod_tag, $mod_opt, $mod_val, $ot) =
929 parse_for_variables ( $value, 0, $opts );
930  
931 # Any variables to substitute ???
932 34583 ▶ unless ( defined $tag ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag) 29821 4762 34583
933 29821 return DBUG_RETURN ( $value, $mask_flag ); # nope ...
934 }
935  
936 4762 my $output = $value;
937  
938 4762 my %encrypt_vars;
939 4762 my $encrypt_cnt = 0;
940 4762 my $encrypt_fmt = "_"x50 . "ENCRYPT_%02d" . "-"x50;
941  
942 my ($lv, $rv) = ( convert_to_regexp_string ($opts->{variable_left}),
943 4762 convert_to_regexp_string ($opts->{variable_right}) );
944  
945 # While there are still variables to process ...
946 4762 ▶ while ( defined $tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) 7755 4762 12517
947 7755 my ( $val, $mask );
948 7755 my $do_mod_lookup = 0; # Very rarely set to true ...
949  
950 # ${tag} and ${mod_tag} will never have the same value ...
951 # ${mod_tag} will amost always be undefinded.
952 # If both are defined, we'll almost always end up using ${mod_tag} as
953 # the real variable to expand! But we check to be sure 1st.
954  
955 7755 ( $val, $mask ) = $config->lookup_one_variable ( $tag );
956  
957 # It's extreemly rare to have this "if statement" evalate to true ...
958 7755 ▶ if ( (! defined $val) && defined $mod_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val and defined $mod_tag) 184 7571 7755
Condition100%3/3
not defined $val and defined $mod_tag
!ll&&!rl&&r
7109462184
MC/DC100%2/2
not defined $val and defined $mod_tag
not defined $val defined $mod_tag
Truth table100%3/3
not(A) and B
A= not defined $valB= defined $mod_tag
ABresult
0X0
100
111
959 184 ( $val, $mask ) = $config->lookup_one_variable ( $mod_tag );
960  
961 # -----------------------------------------------------------------
962 # If we're using variable modifiers, it doesn't matter if the
963 # varible exists or not. The modifier gets evaluated!
964 # So checking if the undefined $mod_tag needs to be masked or not ...
965 # -----------------------------------------------------------------
966 184 ▶ unless ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 13 171 184
967 13 $mask = should_we_hide_sensitive_data ( $mod_tag );
968 }
969  
970 184 $do_mod_lookup = 1; # Yes, apply the modifiers!
971 }
972  
973 # Use a place holder if the variable references data that is still encrypted.
974 7755 ▶ if ( $mask == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask == -1) 35 7720 7755
975 35 $mask_flag = -1;
976 35 $val = sprintf ($encrypt_fmt, ++$encrypt_cnt);
977  
978 # If the place holder contains variable anchors abort the substitutions.
979 35 ▶ last if ( $val =~ m/${lv}/ || $val =~ m/${rv}/ );
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /$lv/ or $val =~ /$rv/ 0 35 35
Condition33%1/3
$val =~ /$lv/ or $val =~ /$rv/
l!l&&r!l&&!r
0035
MC/DC0%0/2
$val =~ /$lv/ or $val =~ /$rv/
$val =~ /$lv/ $val =~ /$rv/
Truth table33%1/3
A or B
A= $val =~ /$lv/B= $val =~ /$rv/
ABresult
1X1
011
000
980  
981 35 $encrypt_vars{$val} = $tag;
982 35 $do_mod_lookup = 0;
983 }
984  
985 # Doing some accounting to make sure any sensitive data doesn't
986 # show up in the fish logs from now on.
987 7755 ▶ if ( $mask && ! $mask_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask and not $mask_flag) 126 7629 7755
Condition100%3/3
$mask and not $mask_flag
!ll&&!rl&&r
7420209126
MC/DC100%2/2
$mask and not $mask_flag
$mask not $mask_flag
Truth table100%3/3
A and not(B)
A= $maskB= not $mask_flag
ABresult
0X0
100
111
988 126 $mask_flag = 1;
989 126 DBUG_PAUSE ();
990 126 DBUG_MASK ( 0 );
991 }
992  
993 7755 ▶ if ( $do_mod_lookup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_mod_lookup) 175 7580 7755
994 175 my $m;
995 175 ($val, $m) = apply_modifier ( $config, $val, $mod_tag, $mod_opt, $mod_val, $file );
996 175 ▶ if ( $m == -2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($m == -2) { } 8 167 175
elsif ($m and not $mask_flag) { } 21 265 286
997 # The name of the variable changed & points to an encrypted value.
998 8 $val = $opts->{variable_left} . ${val} . $opts->{variable_right};
999 ▶ } elsif ( $m && ! $mask_flag ) {
Condition100%3/3
$m and not $mask_flag
!ll&&!rl&&r
1192721
MC/DC100%2/2
$m and not $mask_flag
$m not $mask_flag
Truth table100%3/3
A and not(B)
A= $mB= not $mask_flag
ABresult
0X0
100
111
1000 21 $mask_flag = 1;
1001 21 DBUG_PAUSE ();
1002 21 DBUG_MASK ( 0 );
1003 }
1004 }
1005  
1006 # Rebuild the output string so we can look for more variables ...
1007 7755 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 7270 485 7755
1008 7270 $output = $left . $val . $right;
1009 } else {
1010 485 $output = $left . $right;
1011 }
1012  
1013 # Get the next variable to evaluate ...
1014 7755 ($left, $tag, $right, $cmt_flag, $mod_tag, $mod_opt, $mod_val, $ot) =
1015 parse_for_variables ( $output, 0, $opts );
1016 } # End while ( defined $tag ) loop ...
1017  
1018  
1019 # Restore all place holders back into the output string with the
1020 # proper variable name. Have to assume still sensitive even if
1021 # all the placeholders drop out. Since can't tell what else may
1022 # have triggered it.
1023 4762 ▶ if ( $mask_flag == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask_flag == -1) 30 4732 4762
1024 30 $mask_flag = 1; # Mark sensitive ...
1025 30 foreach ( keys %encrypt_vars ) {
1026 35 my $val = $opts->{variable_left} . $encrypt_vars{$_} . $opts->{variable_right};
1027 35 ▶ $mask_flag = -1 if ( $output =~ s/$_/$val/ );
Branch: 1 branch
BranchTrueFalseTotal
if $output =~ s/$_/$val/ 34 1 35
1028 }
1029 }
1030  
1031 # Did the variable substitution result in the need to trim things?
1032 4762 ▶ if ( $trim_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trim_flag) 3936 826 4762
1033 3936 $output =~ s/^\s+//;
1034 3936 $output =~ s/\s+$//;
1035 }
1036  
1037 4762 DBUG_RETURN ( $output, $mask_flag );
1038 }
1039  
1040  
1041 # ==============================================================
1042  
1043 =item ($v[, $s]) = apply_modifier ( $config, $value, $tag, $rule, $sub_rule, $file )
1044  
1045 This is a helper method to F<expand_variables>. Not for public use.
1046  
1047 This function takes the rule specified by I<$rule> and applies it against
1048 the I<$value> with assistance from the I<$sub_rule>.
1049  
1050 It returns the edited I<value> and whether applying the modifier made it
1051 I<sensitive>. (-1 means it's an encrypted value. -2 means it's the variable
1052 name that resolves to an encrypted value. 0 - Non-sensitive, 1 - Sensitive.)
1053  
1054 See L<https://web.archive.org/web/20200309072646/https://wiki.bash-hackers.org/syntax/pe>
1055 for information on how this can work. This module supports most of the
1056 parameter expansions listed there except for those dealing with arrays. Other
1057 modifier rules may be added upon request.
1058  
1059 =cut
1060  
1061 # NOTE1: Fish has already been paused if $tag is sensitive. Since this method
1062 # has no idea if the current tag is sensitive or not.
1063  
1064 # NOTE2: But still need to mask the return value if referencing sensitive data
1065 # in case the original $tag wasn't sensitive. So in most cases it will
1066 # return not-sensitive even if fish has already been paused!
1067 #
1068 # NOTE3: If sensitive/mask is -1, it's sensitive and not decrypted. In this
1069 # case the returned value is the tag's name, not it's value!
1070  
1071 sub apply_modifier
1072 175 ▶{
Subroutine apply_modifier: called CC=44 SCAR=38.0
Pod documented
1073 175 DBUG_ENTER_FUNC ( @_ );
1074 175 my $cfg = shift;
1075 175 my $value = shift; # The value for ${mod_tag} ...
1076 175 my $mod_tag = shift; # The tag to apply the rule against!
1077 175 my $mod_opt = shift; # The rule ...
1078 175 my $mod_val = shift; # The sub-rule ...
1079 175 my $file = shift; # The file the tag's from.
1080  
1081 175 ▶ my $alt_val = (defined $value) ? $value : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 162 13 175
1082  
1083 # The values to return ...
1084 175 my $output;
1085  
1086 # Values: 0 - Normal non-sensitive return value (99.9% of the time)
1087 # 1 - Sensitive return value.
1088 # -1 - Return value is encrypted.
1089 # -2 - Return value is variable name of encrypted value.
1090 175 my $mask = 0;
1091  
1092 # If looking for a default value ...
1093 175 ▶ if ( ( $mod_opt eq ":+" && $alt_val ne "" ) ||
Branch: 14 branches
BranchTrueFalseTotal
if ($mod_opt eq ':+' and $alt_val ne '' or $mod_opt =~ /^:[-=?]$/ and $alt_val eq '' or $mod_opt eq '+' and defined $value or $mod_opt =~ /^[-=?]$/ and not defined $value) { } 9 166 175
elsif ($mod_opt eq '##' or $mod_opt eq '#' or $mod_opt eq '%%' or $mod_opt eq '%') { } 11 155 166
elsif ($mod_opt eq 'LENGTH') { } 1 154 155
elsif ($mod_opt eq 'LIST') { } 2 152 154
elsif ($mod_opt eq '!') { } 108 44 152
elsif ($mod_opt eq '//') { } 4 40 44
elsif ($mod_opt eq '/') { } 8 32 40
elsif ($mod_opt eq ':') { } 8 24 32
elsif ($mod_opt eq '^^') { } 2 22 24
elsif ($mod_opt eq ',,') { } 1 21 22
elsif ($mod_opt eq '~~') { } 1 20 21
elsif ($mod_opt eq '^') { } 1 19 20
elsif ($mod_opt eq ',') { } 1 18 19
elsif ($mod_opt eq '~') { } 1 17 18
Condition100%21/21
$mod_opt eq ":+" and $alt_val ne "" or $mod_opt =~ /^:[-=?]$/ and $alt_val eq "" or $mod_opt eq "+" and defined $value or $mod_opt =~ /^[-=?]$/ and not defined $value
l!l&&r!l&&!r
72166
$mod_opt eq ":+" and $alt_val ne "" or $mod_opt =~ /^:[-=?]$/ and $alt_val eq "" or $mod_opt eq "+" and defined $value
l!l&&r!l&&!r
52168
$mod_opt eq ":+" and $alt_val ne "" or $mod_opt =~ /^:[-=?]$/ and $alt_val eq ""
l!l&&r!l&&!r
32170
$mod_opt eq ":+" and $alt_val ne ""
!ll&&!rl&&r
16933
$mod_opt =~ /^:[-=?]$/ and $alt_val eq ""
!ll&&!rl&&r
16372
$mod_opt eq "+" and defined $value
!ll&&!rl&&r
16622
$mod_opt =~ /^[-=?]$/ and not defined $value
!ll&&!rl&&r
16152
MC/DC100%8/8
$mod_opt eq ":+" and $alt_val ne "" or $mod_opt =~ /^:[-=?]$/ and $alt_val eq "" or $mod_opt eq "+" and defined $value or $mod_opt =~ /^[-=?]$/ and not defined $value
$mod_opt eq ":+" $alt_val ne "" $mod_opt =~ /^:[-=?]$/ $alt_val eq "" $mod_opt eq "+" defined $value $mod_opt =~ /^[-=?]$/ not defined $value
Truth table29%9/31
A and B or C and D or E and F or G and not(H)
A= $mod_opt eq ":+"B= $alt_val ne ""C= $mod_opt =~ /^:[-=?]$/D= $alt_val eq ""E= $mod_opt eq "+"F= defined $valueG= $mod_opt =~ /^[-=?]$/H= not defined $value
ABCDEFGHresult
11XXXXXX1
0X11XXXX1
1011XXXX1
0X0X11XX1
0X1011XX1
100X11XX1
101011XX1
0X0X0X111
0X0X10111
0X100X111
0X1010111
100X0X111
100X10111
10100X111
101010111
0X0X0X0X0
0X0X0X100
0X0X100X0
0X0X10100
0X100X0X0
0X100X100
0X10100X0
0X1010100
100X0X0X0
100X0X100
100X100X0
100X10100
10100X0X0
10100X100
1010100X0
101010100
1094 ( $mod_opt =~ m/^:[-=?]$/ && $alt_val eq "" ) ||
1095 ( $mod_opt eq "+" && defined $value ) ||
1096 ( $mod_opt =~ m/^[-=?]$/ && ! defined $value ) ) {
1097 9 $output = $mod_val; # Now uses this value as it's default!
1098  
1099 9 ▶ if ( $mod_opt eq ":=" || $mod_opt eq "=" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mod_opt eq ':=' or $mod_opt eq '=') { } 2 7 9
elsif ($mod_opt eq ':?' or $mod_opt eq '?') { } 0 7 7
Condition100%3/3
$mod_opt eq ":=" or $mod_opt eq "="
l!l&&r!l&&!r
117
MC/DC100%2/2
$mod_opt eq ":=" or $mod_opt eq "="
$mod_opt eq ":=" $mod_opt eq "="
Truth table100%3/3
A or B
A= $mod_opt eq ":="B= $mod_opt eq "="
ABresult
1X1
011
000
1100 # The variable either doesn't exist or it resolved to "".
1101 # This variant rule says to also set the variable to this value!
1102 2 $cfg->_base_set ( $mod_tag, $output, $file );
1103  
1104 ▶ } elsif ( $mod_opt eq ":?" || $mod_opt eq "?" ) {
Condition33%1/3
$mod_opt eq ":?" or $mod_opt eq "?"
l!l&&r!l&&!r
007
MC/DC0%0/2
$mod_opt eq ":?" or $mod_opt eq "?"
$mod_opt eq ":?" $mod_opt eq "?"
Truth table33%1/3
A or B
A= $mod_opt eq ":?"B= $mod_opt eq "?"
ABresult
1X1
011
000
1105 # In shell scripts, ":?" would cause your script to die with the
1106 # default value as the error message if your var had no value.
1107 # Repeating that logic here.
1108 0 my $msg = "Encounterd undefined variable ($mod_tag) using shell modifier ${mod_opt}";
1109 0 ▶ $msg .= " in config file: " . basename ($file) if ( $file ne "" );
Branch: 1 branch
BranchTrueFalseTotal
if $file ne "" 0 0 0
1110 0 DBUG_PRINT ("MOD", $msg);
1111 0 die ( basename ($0) . ": ${mod_tag}: ${output}.\n" );
1112 }
1113  
1114 9 DBUG_PRINT ("MOD",
1115 "The modifier (%s) is overriding the variable with a default value!",
1116 $mod_opt);
1117  
1118 # Sub-string removal ...
1119 ▶ } elsif ( $mod_opt eq "##" || $mod_opt eq "#" || # From beginning
Condition100%9/9
$mod_opt eq "##" or $mod_opt eq "#" or $mod_opt eq "%%" or $mod_opt eq "%"
l!l&&r!l&&!r
83155
$mod_opt eq "##" or $mod_opt eq "#" or $mod_opt eq "%%"
l!l&&r!l&&!r
53158
$mod_opt eq "##" or $mod_opt eq "#"
l!l&&r!l&&!r
14161
MC/DC100%4/4
$mod_opt eq "##" or $mod_opt eq "#" or $mod_opt eq "%%" or $mod_opt eq "%"
$mod_opt eq "##" $mod_opt eq "#" $mod_opt eq "%%" $mod_opt eq "%"
Truth table100%5/5
A or B or C or D
A= $mod_opt eq "##"B= $mod_opt eq "#"C= $mod_opt eq "%%"D= $mod_opt eq "%"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1120 $mod_opt eq "%%" || $mod_opt eq "%" ) { # From end
1121 11 ▶ my $greedy = ( $mod_opt eq "##" || $mod_opt eq "%%" );
Condition100%3/3
$mod_opt eq '##' || $mod_opt eq '%%'
l!l&&r!l&&!r
137
MC/DC100%2/2
$mod_opt eq '##' || $mod_opt eq '%%'
$mod_opt eq '##' $mod_opt eq '%%'
Truth table100%3/3
A || B
A= $mod_opt eq '##'B= $mod_opt eq '%%'
ABresult
1X1
011
000
1122 11 ▶ my $leading = ( $mod_opt eq "#" || $mod_opt eq "##" );
Condition100%3/3
$mod_opt eq '#' || $mod_opt eq '##'
l!l&&r!l&&!r
416
MC/DC100%2/2
$mod_opt eq '#' || $mod_opt eq '##'
$mod_opt eq '#' $mod_opt eq '##'
Truth table100%3/3
A || B
A= $mod_opt eq '#'B= $mod_opt eq '##'
ABresult
1X1
011
000
1123 11 my $reverse_msg = ""; # Both the message & control flag ...
1124  
1125 11 $output = $alt_val;
1126  
1127 # Now replace shell script wildcards with their Perl equivalents.
1128 # A RegExp can't do non-greedy replaces anchored to the end of string!
1129 # So we need the reverse logic to do so.
1130 11 my $regExpVal = convert_to_regexp_modifier ($mod_val);
1131 11 $regExpVal =~ s/[?]/./g; # ? --> . (any one char)
1132 11 ▶ if ( $greedy ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($greedy) { } 4 7 11
elsif ($leading) { } 4 3 7
elsif ($regExpVal =~ /[*]/) { } 3 0 3
1133 4 $regExpVal =~ s/[*]/.*/g; # * --> .* (zero or more greedy chars)
1134 } elsif ( $leading ) {
1135 4 $regExpVal =~ s/[*]/(.*?)/g; # * --> (.*?) (zero or more chars)
1136 } elsif ( $regExpVal =~ m/[*]/ ) {
1137 # Non-Greedy with one or more wild cards present ("*")!
1138 3 $leading = 1; # Was false before.
1139 3 $regExpVal = reverse ($regExpVal);
1140 3 $regExpVal =~ s/[*]/(.*?)/g; # * --> (.*?) (zero or more chars)
1141 3 $output = reverse ($output);
1142 3 $reverse_msg = " Reversed for non-greedy strip.";
1143 }
1144  
1145 11 ▶ if ( $leading ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading) { } 8 3 11
1146 8 $regExpVal = '^' . $regExpVal;
1147 } else {
1148 # Either greedy trailing or no *'s in trailing regular expression!
1149 3 $regExpVal .= '$';
1150 }
1151  
1152 11 $output =~ s/${regExpVal}//; # Strip off the matching values ...
1153 11 ▶ $output = reverse ($output) if ( $reverse_msg ne "" );
Branch: 1 branch
BranchTrueFalseTotal
if $reverse_msg ne "" 3 8 11
1154  
1155 11 DBUG_PRINT ("MOD",
1156 "The modifier (%s) converted \"%s\" to \"%s\".%s\nTo trim the value to: %s",
1157 $mod_opt, $mod_val, $regExpVal, $reverse_msg, $output);
1158  
1159 } elsif ( $mod_opt eq "LENGTH" ) {
1160 1 $output = length ( $alt_val );
1161 1 DBUG_PRINT ("MOD", "Setting the length of variable \${#%s} to: %d.",
1162 $mod_tag, $output);
1163  
1164 } elsif ( $mod_opt eq "LIST" ) {
1165 2 my @lst = $cfg->_find_variables ( $mod_val );
1166 2 $output = join (" ", @lst);
1167 2 DBUG_PRINT ("MOD", "Getting all varriables starting with %s", $mod_val);
1168  
1169 } elsif ( $mod_opt eq "!" ) {
1170 108 ($output, $mask) = $cfg->lookup_one_variable ( $alt_val );
1171 108 ▶ if ( $mask == -1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mask == -1) { } 8 100 108
elsif ($mask) { } 48 52 100
1172 8 $mask = -2; # Indirect reference to encrypted value
1173 8 $output = $alt_val; # Replace with new variable name
1174 } elsif ( $mask ) {
1175 48 DBUG_MASK (0);
1176 }
1177 108 DBUG_PRINT ("MOD", "Indirectly referencing variable %s (%s)", $alt_val, $mask);
1178  
1179 } elsif ( $mod_opt eq "//" ) {
1180 4 my ($ptrn, $val) = split ("/", $mod_val);
1181 4 $output = $alt_val;
1182 4 $output =~ s/${ptrn}/${val}/g;
1183 4 DBUG_PRINT ("MOD", "Global replacement in %s", $alt_val);
1184  
1185 } elsif ( $mod_opt eq "/" ) {
1186 8 my ($ptrn, $val) = split ("/", $mod_val);
1187 8 $output = $alt_val;
1188 8 $output =~ s/${ptrn}/${val}/;
1189 8 DBUG_PRINT ("MOD", "1st replacement in %s", $alt_val);
1190  
1191 } elsif ( $mod_opt eq ":" ) {
1192 8 my ($offset, $length) = split (":", $mod_val);
1193 8 ▶ if ( defined $length && $length ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length and $length ne '') { } 7 1 8
Condition66%2/3
defined $length and $length ne ""
!ll&&!rl&&r
017
MC/DC50%1/2
defined $length and $length ne ""
defined $length $length ne ""
Truth table66%2/3
A and B
A= defined $lengthB= $length ne ""
ABresult
0X0
100
111
1194 7 $output = substr ( $alt_val, $offset, $length);
1195 } else {
1196 1 $output = substr ( $alt_val, $offset);
1197 }
1198 8 DBUG_PRINT ("MOD", "Substring (%s)", $output);
1199  
1200 # The 6 case manipulation modifiers ...
1201 } elsif ( $mod_opt eq "^^" ) {
1202 2 $output = uc ($alt_val);
1203 2 DBUG_PRINT ("MOD", "Upshift string (%s)", $output);
1204 } elsif ( $mod_opt eq ",," ) {
1205 1 $output = lc ($alt_val);
1206 1 DBUG_PRINT ("MOD", "Downshift string (%s)", $output);
1207 } elsif ( $mod_opt eq "~~" ) {
1208 1 $output = $alt_val;
1209 1 ▶ $output =~ s/([A-Z])|([a-z])/defined $1 ? lc($1) : uc($2)/gex;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 12 11 23
1210 1 DBUG_PRINT ("MOD", "Reverse case of each char in string (%s)", $output);
1211 } elsif ( $mod_opt eq "^" ) {
1212 1 $output = ucfirst ($alt_val);
1213 1 DBUG_PRINT ("MOD", "Upshift 1st char in string (%s)", $output);
1214 } elsif ( $mod_opt eq "," ) {
1215 1 $output = lcfirst ($alt_val);
1216 1 DBUG_PRINT ("MOD", "Downshift 1st char in string (%s)", $output);
1217 } elsif ( $mod_opt eq "~" ) {
1218 1 $output = ucfirst ($alt_val);
1219 1 ▶ $output = lcfirst ($alt_val) if ( $alt_val eq $output );
Branch: 1 branch
BranchTrueFalseTotal
if $alt_val eq $output 1 0 1
1220 1 DBUG_PRINT ("MOD", "Reverse case of 1st char in string (%s)", $output);
1221  
1222 } else {
1223 17 DBUG_PRINT ("MOD",
1224 "The modifier (%s) didn't affect the variable's value!",
1225 $mod_opt);
1226 17 $output = $value;
1227 }
1228  
1229 175 DBUG_RETURN ( $output, $mask );
1230 }
1231  
1232  
1233 # ==============================================================
1234  
1235 =item @ret[0..7] = parse_for_variables ( $value, $ignore_disable_flag, $rOpts )
1236  
1237 This is a helper method to F<expand_variables> and B<parse_line>.
1238  
1239 This method parses the I<$value> to see if any variables are defined in it
1240 and returns the information about it. If there is more than one variable
1241 present in the I<$value>, only the 1st variable/tag to evaluate is returned.
1242  
1243 By default, a variable is the tag in the I<$value> between B<${> and B<}>, which
1244 can be overridden with other anchor patterns. See L<Advanced::Config::Options>
1245 for more details on this.
1246  
1247 If you've configured the module to ignore variables, it will never find any.
1248 Unless you also set I<$ignore_disable_flag> to a non-zero value.
1249  
1250 Returns B<8> values. ( $left, $tag, $right, $cmt, $sub_tag, $sub_opr, $sub_val,
1251 $otag )
1252  
1253 All B<8> values will be I<undef> if no variables were found in I<$value>.
1254  
1255 Otherwise it returns at least the 1st four values. Where I<$tag> is the
1256 variable that needs to be looked up. And the caller can join things back
1257 together as "B<$left . $look_up_value . $right>" after the variable substitution
1258 is done and before this method is called again to locate additional variables in
1259 the resulting new I<$value>.
1260  
1261 The 4th value I<$cmt>, will be true/false based on if B<$left> has a comment
1262 symbol in it! This flag only has meaning to B<parse_line>. And is terribly
1263 misleading to other users.
1264  
1265 Should the I<$tag> definition have one of the supported shell script variable
1266 modifiers embedded inside it, then the I<$tag> will be parsed and the 3 B<sub_*>
1267 return values will be calculated as well. See
1268 L<http://wiki.bash-hackers.org/syntax/pe> for more details. Most of the
1269 modifiers listed there are supported except for those dealing with arrays.
1270 See I<apply_modifier> for applying these rules against the returned I<$tag>.
1271 Other modifier rules may be added upon request.
1272  
1273 These 3 B<sub_*> return values will always be I<undef> should the variable
1274 left/right anchors be overridden with the same value. Or if no modifiers
1275 are detected in the tag's name.
1276  
1277 If you've configured the module to be case insensitive (option B<tag_case>),
1278 then both I<$tag> and I<$sub_tag> will be shifted to lower case for case
1279 insensitive variable lookups.
1280  
1281 Finally there is an 8th return value, I<$otag>, that contains the original
1282 I<$tag> value before it was edited. Needed by F<parse_line> logic.
1283  
1284 =cut
1285  
1286 # WARNING: If (${lvar} == ${rvar}), nested variables are not supported.
1287 # : And neither are variable modifiers. (The sub_* return values.)
1288 # : So evaluate tags left to right.
1289 # : If (${lvar} != ${rvar}), nested variables are supported.
1290 # : So evaluate inner most tags first. And then left to right.
1291 #
1292 # RETURNS: 8 values. ( $left, $tag, $right, $cmt, $sub_tag, $sub_opr, $sub_val, $otag )
1293 # : The 3 sub_* vars are usually undef.
1294 # : But when set, all 3 sub_* vars are set! And $tag != $sub_tag.
1295 #
1296 # NOTE 1 : If the 3 sub_* vars are populated, you'd get something like this
1297 # : for the tag & sub_* vars.
1298 # : tag : "abc:-Default Value" - the ${...} was removed.
1299 # : sub_tag : "abc" - the ${...} & modifier were removed.
1300 # : sub_opr : ":-"
1301 # : sub_val : "Default Value"
1302 # : So if the "tag" exists as a variable, the sub_* values are ignored.
1303 # : But if "tag" doesn't exist as a variable, then we apply the
1304 # : sub_* rules!
1305 #
1306 # NOTE 2 : If the sub_* vars undef, you'd get something like this without any
1307 # : modifiers.
1308 # : tag : tag - the ${...} was removed.
1309 #
1310 # NOTE 3 : For some alternate variable anchors, the sub_* vars will almost
1311 # : always be undef. Since the code base won't allow you to redefine
1312 # : these modifiers when they conflict with the variable anchors.
1313  
1314 sub parse_for_variables
1315 60821 ▶{
Subroutine parse_for_variables: called CC=37 SCAR=36.1
Pod documented
1316 60821 DBUG_ENTER_FUNC ( @_ );
1317 60821 my $value = shift;
1318 60821 my $disable_flag = shift;
1319 60821 my $opts = shift;
1320  
1321 60821 my ($left, $s1, $tag, $s2, $right, $otag);
1322 60821 my $cmt_flg = 0;
1323 60821 my ($sub_tag, $sub_opr, $sub_val, $sub_extra);
1324  
1325 60821 ▶ if ( $opts->{disable_variables} && (! $disable_flag) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"disable_variables"} and not $disable_flag) 25 60796 60821
Condition100%3/3
$opts->{"disable_variables"} and not $disable_flag
!ll&&!rl&&r
6055124525
MC/DC100%2/2
$opts->{"disable_variables"} and not $disable_flag
$opts->{"disable_variables"} not $disable_flag
Truth table100%3/3
A and not(B)
A= $opts->{"disable_variables"}B= not $disable_flag
ABresult
0X0
100
111
1326 25 DBUG_PRINT ("INFO", "Variable substitution has been disabled.");
1327 25 return DBUG_RETURN ( $left, $tag, $right, $cmt_flg,
1328 $sub_tag, $sub_opr, $sub_val, $otag );
1329 }
1330  
1331 60796 my $lvar = convert_to_regexp_string ($opts->{variable_left}, 1);
1332 60796 my $rvar = convert_to_regexp_string ($opts->{variable_right}, 1);
1333  
1334 # Break up the value into it's component parts. (Non-greedy RegExpr)
1335 60796 ▶ if ( $value =~ m/(^.*?)(${lvar})(.*?)(${rvar})(.*$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /(^.*?)($lvar)(.*?)($rvar)(.*$)/) 9422 51374 60796
1336 9422 ($left, $s1, $tag, $s2, $right) = ($1, $2, $3, $4, $5);
1337 9422 $otag = $tag;
1338  
1339 # Did a comment symbol apear before the 1st ${lvar} in the line?
1340 9422 my $cmt_str = convert_to_regexp_string ($opts->{comment}, 1);
1341 9422 ▶ $cmt_flg = 1 if ( $left =~ m/${cmt_str}/ );
Branch: 1 branch
BranchTrueFalseTotal
if $left =~ /$cmt_str/ 227 9195 9422
1342  
1343 9422 DBUG_PRINT ("XXXX", "%s ===> %s <=== %s -- %d",
1344 $left, $tag, $right, $cmt_flg);
1345  
1346 # We know we found the 1st right hand anchor in the string's value.
1347 # But since variables may be nested, we might not be at the correct
1348 # left hand anchor. But at least we know they're going to balance!
1349  
1350 # Check for nested variables ... (trim left side)
1351 9422 ▶ while ( $tag =~ m/(^.*)${lvar}(.*?$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /(^.*)$lvar(.*?$)/) 1022 9422 10444
1352 1022 my ($l, $t) = ($1, $2);
1353 1022 $left .= $s1 . $l;
1354 1022 $tag = $t;
1355 }
1356  
1357 # Strip off leading spaces from the tag's name.
1358 # No tag may have leading spaces in it.
1359 # Defering the stripping of trailing spaces until later on purpose!
1360 9422 $tag =~ s/^\s+//;
1361  
1362 # -----------------------------------------------------------
1363 # We have a variable! Now check if there are modifiers
1364 # in it that we are supporting ...
1365 # See: http://wiki.bash-hackers.org/syntax/pe
1366 # -----------------------------------------------------------
1367  
1368 # The variable modifier tags. Needed to avoid using the wrong rule.
1369 # A variable name can use anything except for what's in this list!
1370 9422 my $not = "[^-:?+#%/\^,~]";
1371  
1372 9422 ▶ if ( $lvar eq $rvar ) {
Branch: 13 branches
BranchTrueFalseTotal
if ($lvar eq $rvar) { } 210 9212 9422
elsif ($opts->{'disable_variable_modifiers'}) { } 1136 8076 9212
elsif ($tag =~ /(^$not+)(:?[-=+])(.+)$/) { } 59 8017 8076
elsif ($tag =~ /(^$not+)(:?[?])(.*)$/) { } 7 8010 8017
elsif ($tag =~ /^($not+)(##)(.+)$/ or $tag =~ /^($not+)(%%)(.+)$/ or $tag =~ /^($not+)(#)(.+)$/ or $tag =~ /^($not+)(%)(.+)$/) { } 42 7968 8010
elsif ($tag =~ /^#(.+)$/) { } 16 7952 7968
elsif ($tag =~ /^!(.+)[\@*]$/) { } 2 7950 7952
elsif ($tag =~ /^!(.+)$/) { } 140 7810 7950
elsif ($tag =~ m[^($not+)(//?)([^/]+)/([^/]*)$]) { } 14 7796 7810
elsif ($tag =~ m[^($not+)(//?)([^/]+)$]) { } 6 7790 7796
elsif ($tag =~ /^($not+):([0-9]+)$/ or $tag =~ /^($not+):\s+(-[0-9]+)$/ or $tag =~ /^($not+):[(](-[0-9]+)[)]$/) { } 1 7789 7790
elsif ($tag =~ /^($not+):([0-9]+):(-?[0-9]+)$/ or $tag =~ /^($not+):\s+(-[0-9]+):(-?[0-9]+)$/ or $tag =~ /^($not+):[(](-[0-9]+)[)]:(-?[0-9]+)$/) { } 7 7782 7789
elsif ($tag =~ /^($not+)([\^]{1,2})$/ or $tag =~ /^($not+)([,]{1,2})$/ or $tag =~ /^($not+)([~]{1,2})$/) { } 13 7769 7782
1373 ; # No modifiers are supported if the left/right anchors are the same!
1374 # Since there are too many modifier/anchor pairs that no longer
1375 # work. Behaving more like a Windows *.bat file now.
1376  
1377 } elsif ( $opts->{disable_variable_modifiers} ) {
1378 ; # Explicitly told not to use this feature.
1379  
1380 # Rule: :-, :=, :+, -, =, or +
1381 } elsif ( $tag =~ m/(^${not}+)(:?[-=+])(.+)$/) {
1382 59 ($sub_tag, $sub_opr, $sub_val) = ($1, $2, $3);
1383  
1384 # Rule: :? or ?
1385 } elsif ( $tag =~ m/(^${not}+)(:?[?])(.*)$/) {
1386 7 ($sub_tag, $sub_opr, $sub_val) = ($1, $2, $3);
1387 7 ▶ $sub_val = "Parameter null or not set." if ( $sub_val eq "" );
Branch: 1 branch
BranchTrueFalseTotal
if $sub_val eq "" 1 6 7
1388  
1389 # Rule: ##, %%, #, or %
1390 ▶ } elsif ( $tag =~ m/^(${not}+)(##)(.+)$/ ||
Condition100%9/9
$tag =~ /^($not+)(##)(.+)$/ or $tag =~ /^($not+)(%%)(.+)$/ or $tag =~ /^($not+)(#)(.+)$/ or $tag =~ /^($not+)(%)(.+)$/
l!l&&r!l&&!r
3847968
$tag =~ /^($not+)(##)(.+)$/ or $tag =~ /^($not+)(%%)(.+)$/ or $tag =~ /^($not+)(#)(.+)$/
l!l&&r!l&&!r
7317972
$tag =~ /^($not+)(##)(.+)$/ or $tag =~ /^($not+)(%%)(.+)$/
l!l&&r!l&&!r
258003
MC/DC100%4/4
$tag =~ /^($not+)(##)(.+)$/ or $tag =~ /^($not+)(%%)(.+)$/ or $tag =~ /^($not+)(#)(.+)$/ or $tag =~ /^($not+)(%)(.+)$/
$tag =~ /^($not+)(##)(.+)$/ $tag =~ /^($not+)(%%)(.+)$/ $tag =~ /^($not+)(#)(.+)$/ $tag =~ /^($not+)(%)(.+)$/
Truth table100%5/5
A or B or C or D
A= $tag =~ /^($not+)(##)(.+)$/B= $tag =~ /^($not+)(%%)(.+)$/C= $tag =~ /^($not+)(#)(.+)$/D= $tag =~ /^($not+)(%)(.+)$/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1391 $tag =~ m/^(${not}+)(%%)(.+)$/ ||
1392 $tag =~ m/^(${not}+)(#)(.+)$/ ||
1393 $tag =~ m/^(${not}+)(%)(.+)$/ ) {
1394 42 ($sub_tag, $sub_opr, $sub_val) = ($1, $2, $3);
1395  
1396 # Rule: Get length of variable's value ...
1397 } elsif ( $tag =~ m/^#(.+)$/ ) {
1398 # Using LENGTH for ${#var} opt since "#" is already used above!
1399 16 ($sub_tag, $sub_opr, $sub_val) = ($1, "LENGTH", "");
1400 16 $sub_tag =~ s/^\s+//;
1401  
1402 # Rule: ${!var*} & ${!var@} ...
1403 } elsif ( $tag =~ m/^!(.+)[@*]$/ ) {
1404 # Using LIST for ${!var*} & ${!var@} opts since "!" has another meaning.
1405 2 ($sub_tag, $sub_opr, $sub_val) = ($1, "LIST", convert_to_regexp_string ($1));
1406 2 $sub_tag =~ s/^\s+//;
1407  
1408 # Rule: Indirect lookup ...
1409 } elsif ( $tag =~ m/^!(.+)$/ ) {
1410 140 ($sub_tag, $sub_opr, $sub_val) = ($1, "!", "");
1411 140 $sub_tag =~ s/^\s+//;
1412  
1413 # Rule: Substitution logic ... ( / vs // )
1414 # Anchors # or % supported but no RegExp wildcards are.
1415 } elsif ( $tag =~ m#^(${not}+)(//?)([^/]+)/([^/]*)$# ) {
1416 14 ($sub_tag, $sub_opr, $sub_val, $sub_extra) = ($1, $2, $3, $4);
1417 14 $sub_val = convert_to_regexp_string ($sub_val);
1418  
1419 14 ▶ if ( $sub_val =~ m/^([#%])(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_val =~ /^([#%])(.+)$/) { } 4 10 14
1420 4 $sub_val = $2;
1421 4 ▶ $sub_val = ( $1 eq "#" ) ? "^${sub_val}/${sub_extra}" : "${sub_val}\$/${sub_extra}";
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '#' ? : 2 2 4
1422 } else {
1423 10 $sub_val = "${sub_val}/${sub_extra}";
1424 }
1425 14 $sub_val .= "/x";
1426  
1427 # Rule: Another format for the Substitution logic ... ( / vs // )
1428 } elsif ( $tag =~ m#^(${not}+)(//?)([^/]+)$# ) {
1429 6 ($sub_tag, $sub_opr, $sub_val, $sub_extra) = ($1, $2, $3, "");
1430 6 $sub_val = convert_to_regexp_string ($sub_val);
1431  
1432 6 ▶ if ( $sub_val =~ m/^([#%])(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_val =~ /^([#%])(.+)$/) { } 4 2 6
1433 4 $sub_val = $2;
1434 4 ▶ $sub_val = ( $1 eq "#" ) ? "^${sub_val}/${sub_extra}" : "${sub_val}\$/${sub_extra}";
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '#' ? : 2 2 4
1435 } else {
1436 2 $sub_val = "${sub_val}/${sub_extra}";
1437 }
1438 6 $sub_val .= "/x";
1439  
1440 # Rule: Substring expansion ... ${MSG:OFFSET}
1441 ▶ } elsif ( $tag =~ m#^(${not}+):([0-9]+)$# ||
Condition66%4/6
$tag =~ /^($not+):([0-9]+)$/ or $tag =~ /^($not+):\s+(-[0-9]+)$/ or $tag =~ /^($not+):[(](-[0-9]+)[)]$/
l!l&&r!l&&!r
107789
$tag =~ /^($not+):([0-9]+)$/ or $tag =~ /^($not+):\s+(-[0-9]+)$/
l!l&&r!l&&!r
107789
MC/DC33%1/3
$tag =~ /^($not+):([0-9]+)$/ or $tag =~ /^($not+):\s+(-[0-9]+)$/ or $tag =~ /^($not+):[(](-[0-9]+)[)]$/
$tag =~ /^($not+):([0-9]+)$/ $tag =~ /^($not+):\s+(-[0-9]+)$/ $tag =~ /^($not+):[(](-[0-9]+)[)]$/
Truth table50%2/4
A or B or C
A= $tag =~ /^($not+):([0-9]+)$/B= $tag =~ /^($not+):\s+(-[0-9]+)$/C= $tag =~ /^($not+):[(](-[0-9]+)[)]$/
ABCresult
1XX1
01X1
0011
0000
1442 $tag =~ m#^(${not}+):\s+(-[0-9]+)$# ||
1443 $tag =~ m#^(${not}+):[(](-[0-9]+)[)]$# ) {
1444 1 ($sub_tag, $sub_opr, $sub_val) = ($1, ":", $2);
1445 1 $sub_val .= ":"; # To the end of the string ...
1446  
1447 # Rule: Substring expansion ... ${MSG:OFFSET:LENGTH}
1448 ▶ } elsif ( $tag =~ m#^(${not}+):([0-9]+):(-?[0-9]+)$# ||
Condition100%6/6
$tag =~ /^($not+):([0-9]+):(-?[0-9]+)$/ or $tag =~ /^($not+):\s+(-[0-9]+):(-?[0-9]+)$/ or $tag =~ /^($not+):[(](-[0-9]+)[)]:(-?[0-9]+)$/
l!l&&r!l&&!r
437782
$tag =~ /^($not+):([0-9]+):(-?[0-9]+)$/ or $tag =~ /^($not+):\s+(-[0-9]+):(-?[0-9]+)$/
l!l&&r!l&&!r
317785
MC/DC100%3/3
$tag =~ /^($not+):([0-9]+):(-?[0-9]+)$/ or $tag =~ /^($not+):\s+(-[0-9]+):(-?[0-9]+)$/ or $tag =~ /^($not+):[(](-[0-9]+)[)]:(-?[0-9]+)$/
$tag =~ /^($not+):([0-9]+):(-?[0-9]+)$/ $tag =~ /^($not+):\s+(-[0-9]+):(-?[0-9]+)$/ $tag =~ /^($not+):[(](-[0-9]+)[)]:(-?[0-9]+)$/
Truth table100%4/4
A or B or C
A= $tag =~ /^($not+):([0-9]+):(-?[0-9]+)$/B= $tag =~ /^($not+):\s+(-[0-9]+):(-?[0-9]+)$/C= $tag =~ /^($not+):[(](-[0-9]+)[)]:(-?[0-9]+)$/
ABCresult
1XX1
01X1
0011
0000
1449 $tag =~ m#^(${not}+):\s+(-[0-9]+):(-?[0-9]+)$# ||
1450 $tag =~ m#^(${not}+):[(](-[0-9]+)[)]:(-?[0-9]+)$# ) {
1451 7 ($sub_tag, $sub_opr, $sub_val, $sub_extra) = ($1, ":", $2, $3);
1452 7 $sub_val .= ":${sub_extra}";
1453  
1454 # Rule: Case manipulation ... (6 variants)
1455 ▶ } elsif ( $tag =~ m/^(${not}+)([\^]{1,2})$/ ||
Condition100%6/6
$tag =~ /^($not+)([\^]{1,2})$/ or $tag =~ /^($not+)([,]{1,2})$/ or $tag =~ /^($not+)([~]{1,2})$/
l!l&&r!l&&!r
947769
$tag =~ /^($not+)([\^]{1,2})$/ or $tag =~ /^($not+)([,]{1,2})$/
l!l&&r!l&&!r
547773
MC/DC100%3/3
$tag =~ /^($not+)([\^]{1,2})$/ or $tag =~ /^($not+)([,]{1,2})$/ or $tag =~ /^($not+)([~]{1,2})$/
$tag =~ /^($not+)([\^]{1,2})$/ $tag =~ /^($not+)([,]{1,2})$/ $tag =~ /^($not+)([~]{1,2})$/
Truth table100%4/4
A or B or C
A= $tag =~ /^($not+)([\^]{1,2})$/B= $tag =~ /^($not+)([,]{1,2})$/C= $tag =~ /^($not+)([~]{1,2})$/
ABCresult
1XX1
01X1
0011
0000
1456 $tag =~ m/^(${not}+)([,]{1,2})$/ ||
1457 $tag =~ m/^(${not}+)([~]{1,2})$/ ) {
1458 13 ($sub_tag, $sub_opr, $sub_val) = ($1, $2, "");
1459  
1460 } else {
1461 ; # No variable modifiers were found!
1462 }
1463  
1464 # Strip off any trailing spaces from the tag & sub-tag names ...
1465 9422 $tag =~ s/\s+$//;
1466 9422 ▶ $sub_tag =~ s/\s+$// if ( defined $sub_tag );
Branch: 1 branch
BranchTrueFalseTotal
if defined $sub_tag 307 9115 9422
1467 } # End "if" a tag/variable was found in ${value} ...
1468  
1469 # Are we using case insensitive tags/variables?
1470 # If so, all varibles must be in lower case ...
1471 # Leave $otag alone.
1472 60796 ▶ if ( $opts->{tag_case} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"tag_case"}) 147 60649 60796
1473 147 ▶ $tag = lc ($tag) if ( defined $tag );
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag 2 145 147
1474 147 ▶ $sub_tag = lc ($sub_tag) if ( defined $sub_tag );
Branch: 1 branch
BranchTrueFalseTotal
if defined $sub_tag 0 147 147
1475 }
1476  
1477 60796 DBUG_RETURN ( $left, $tag, $right, $cmt_flg, $sub_tag, $sub_opr, $sub_val,
1478 $otag );
1479 }
1480  
1481  
1482 # ==============================================================
1483  
1484 =item $string = format_section_line ( $name, \%rOpts )
1485  
1486 Uses the given I<Read Options Hash> to generate a section string
1487 from I<$name>.
1488  
1489 =cut
1490  
1491 sub format_section_line
1492 6 ▶{
Subroutine format_section_line: called CC=1 SCAR=0.0
Pod documented
1493 6 DBUG_ENTER_FUNC ( @_ );
1494 6 my $name = shift; # The name of the section ...
1495 6 my $rOpts = shift;
1496  
1497 6 DBUG_RETURN ( $rOpts->{section_left} . " ${name} " . $rOpts->{section_right} );
1498 }
1499  
1500  
1501 # ==============================================================
1502  
1503 =item $string = format_tag_value_line ( $cfg, $tag, \%rOpts )
1504  
1505 It looks up the B<tag> in the I<$cfg> object, then it uses the given
1506 I<Read Options Hash> options to format a tag/value pair string.
1507  
1508 =cut
1509  
1510 sub format_tag_value_line
1511 24 ▶{
Subroutine format_tag_value_line: called CC=16 SCAR=32.9
Pod documented
1512 24 DBUG_ENTER_FUNC ( @_ );
1513 24 my $cfg = shift; # An Advanced::Config object reference.
1514 24 my $tag = shift;
1515 24 my $rOpts = shift;
1516  
1517 24 my ($value, $sensitive) = $cfg->_base_get2 ( $tag, {required => 1} );
1518 24 ▶ DBUG_MASK (0) if ( $sensitive );
Branch: 1 branch
BranchTrueFalseTotal
if $sensitive 9 15 24
1519  
1520 # Determine if we're alowed to surround things with quotes ...
1521 24 my ($quote_l, $quote_r); # Assume no!
1522 24 ▶ if (using_default_quotes ( $rOpts )) {
Branch: 2 branches
BranchTrueFalseTotal
if (using_default_quotes($rOpts)) { } 24 0 24
elsif (not $rOpts->{'disable_quotes'}) { } 0 0 0
1523 24 ▶ if ( $value =~ m/'/ && $value =~ m/"/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value =~ /'/ and $value =~ /"/) { } 3 21 24
elsif (not $value =~ /'/) { } 18 3 21
elsif (not $value =~ /"/) { } 3 0 3
Condition100%3/3
$value =~ /'/ and $value =~ /"/
!ll&&!rl&&r
1833
MC/DC100%2/2
$value =~ /'/ and $value =~ /"/
$value =~ /'/ $value =~ /"/
Truth table100%3/3
A and B
A= $value =~ /'/B= $value =~ /"/
ABresult
0X0
100
111
1524 3 my $noop; # No quotes allowed!
1525 } elsif ( $value !~ m/'/ ) {
1526 18 $quote_l = $quote_r = "'";
1527 } elsif ( $value !~ m/"/ ) {
1528 3 $quote_l = $quote_r = '"';
1529 }
1530  
1531 } elsif ( ! $rOpts->{disable_quotes} ) {
1532 my ($ql, $qr) = ( convert_to_regexp_string ($rOpts->{quote_left}, 1),
1533 0 convert_to_regexp_string ($rOpts->{quote_right}, 1) );
1534 0 ▶ unless ( $value =~ m/${ql}/ || $value =~ m/${qr}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$ql/ or $value =~ /$qr/) 0 0 0
Condition0%0/3
$value =~ /$ql/ or $value =~ /$qr/
l!l&&r!l&&!r
000
MC/DC0%0/2
$value =~ /$ql/ or $value =~ /$qr/
$value =~ /$ql/ $value =~ /$qr/
Truth table0%0/3
A or B
A= $value =~ /$ql/B= $value =~ /$qr/
ABresult
1X1
011
000
1535 0 $quote_l = $rOpts->{quote_left};
1536 0 $quote_r = $rOpts->{quote_right};
1537 }
1538 }
1539  
1540 # Do we have to correct for having comments in the value?
1541 24 my $cmt = convert_to_regexp_string ($rOpts->{comment}, 1);
1542 24 ▶ if ( $value =~ m/${cmt}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$cmt/) 12 12 24
1543 12 my $err = "Can't do toString() due to using comments in the value of '${tag}'\n";
1544  
1545 12 ▶ if ( $rOpts->{disable_variables} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"disable_variables"}) 0 12 12
1546 0 ▶ if ( $rOpts->{disable_quotes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"disable_quotes"}) 0 0 0
1547 0 die ($err, "when you've also disabled both quotes & variables!\n");
1548 }
1549 0 ▶ unless ( $quote_l ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quote_l) 0 0 0
1550 0 die ($err, "when you've disabled variables while there are quotes in the value as well!\n");
1551 }
1552 }
1553  
1554 # Convert the comment symbols to the special variable if no quotes are allowed.
1555 12 ▶ unless ( $quote_l ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quote_l) 3 9 12
1556 3 my $v = $rOpts->{variable_left} . "shft3" . $rOpts->{variable_right};
1557 3 $value =~ s/${cmt}/${v}/g;
1558 }
1559 }
1560  
1561 # Surround the value with quotes!
1562 24 ▶ if ( $quote_l ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quote_l) 21 3 24
1563 21 $value = ${quote_l} . ${value} . ${quote_r};
1564 }
1565  
1566 24 my $line = ${tag} . " " . $rOpts->{assign} . " " . ${value};
1567  
1568 24 DBUG_RETURN ( $line );
1569 }
1570  
1571  
1572 # ==============================================================
1573  
1574 =item $string = format_encrypt_cmt ( \%rOpts )
1575  
1576 Uses the given I<Read Options Hash> to generate a comment suitable for use
1577 in marking a tag/value pair as ready to be encrypted.
1578  
1579 =cut
1580  
1581 sub format_encrypt_cmt
1582 2 ▶{
Subroutine format_encrypt_cmt: called CC=1 SCAR=0.0
Pod documented
1583 2 DBUG_ENTER_FUNC ( @_ );
1584 2 my $rOpts = shift;
1585  
1586 2 DBUG_RETURN ( $rOpts->{comment} . " " . $rOpts->{encrypt_lbl} );
1587 }
1588  
1589  
1590 # ==============================================================
1591  
1592 =item $status = encrypt_config_file_details ( $file, $writeFile, \%rOpts )
1593  
1594 This function encrypts all tag values inside the specified config file that are
1595 marked as ready for encryption and generates a new config file with everything
1596 encrypted. If a tag/value pair isn't marked as ready for encryption it is left
1597 alone. By default this label is B<ENCRYPT>.
1598  
1599 After a tag's value has been encrypted, the label in the comment is updated
1600 from B<ENCRYPT> to B<DECRYPT> in the new file.
1601  
1602 If you are adding new B<ENCRYPT> tags to an existing config file that already
1603 has B<DECRYPT> tags in it, you must use the same encryption related options in
1604 I<%rOpts> as the last time. Otherwise you won't be able to decrypt all
1605 encrypted values.
1606  
1607 This method ignores any request to source in other config files. You must
1608 encrypt each file individually.
1609  
1610 It writes the results of the encryption process to I<$writeFile>.
1611  
1612 See L<Advanced::Config::Options> for some caveats about this process.
1613  
1614 Returns: B<1> if something was encrypted. B<-1> if nothing was encrypted.
1615 Otherwise B<0> on error.
1616  
1617 =cut
1618  
1619 sub encrypt_config_file_details
1620 7 ▶{
Subroutine encrypt_config_file_details: called CC=22 SCAR=32.1
Pod documented
1621 7 DBUG_ENTER_FUNC ( @_ );
1622 7 my $file = shift;
1623 7 my $scratch = shift;
1624 7 my $rOpts = shift;
1625  
1626 7 unlink ( $scratch );
1627  
1628 # The labels to search for ...
1629 7 my $decrypt_str = convert_to_regexp_string ($rOpts->{decrypt_lbl});
1630 7 my $encrypt_str = convert_to_regexp_string ($rOpts->{encrypt_lbl});
1631 7 my $hide_str = convert_to_regexp_string ($rOpts->{hide_lbl});
1632  
1633 7 my $assign_str = convert_to_regexp_string ($rOpts->{assign});
1634 my ($lb, $rb) = ( convert_to_regexp_string ($rOpts->{section_left}),
1635 7 convert_to_regexp_string ($rOpts->{section_right}) );
1636  
1637 # The label separators used when searching for option labels in a comment ...
1638 7 my $lbl_sep = '[\s.,$!-()]';
1639  
1640 7 my $mask = "*"x8;
1641  
1642 7 DBUG_PRINT ("INFO", "Opening for reading the config file named: %s", $file);
1643  
1644 7 ▶ unless ( open (ENCRYPT, "<", $file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open ENCRYPT, "<", $file) 0 7 7
1645 0 return DBUG_RETURN ( croak_helper ($rOpts,
1646 "Unable to open the config file.", 0) );
1647 }
1648  
1649 7 DBUG_PRINT ("INFO", "Creating scratch file named: %s", $scratch);
1650 7 ▶ unless ( open (NEW, ">", $scratch) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open NEW, ">", $scratch) 0 7 7
1651 0 close (ENCRYPT);
1652 0 return DBUG_RETURN ( croak_helper ($rOpts,
1653 "Unable to create the scratch config file.", 0) );
1654 }
1655  
1656 # Misuse of this option makes the config file unreadable ...
1657 7 ▶ if ( $rOpts->{use_utf8} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"use_utf8"}) 1 6 7
1658 1 binmode (ENCRYPT, "encoding(UTF-8)");
1659 1 binmode (NEW, "encoding(UTF-8)");
1660 }
1661  
1662 7 my $errMsg = "Unable to write to the scratch file.";
1663  
1664 7 my $hide_section = 0;
1665 7 my $count = 0;
1666  
1667 7 ▶ while ( <ENCRYPT> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ENCRYPT)) 7455 7 7462
1668 7455 chomp;
1669 7455 my $line = $_;
1670  
1671 7455 my ($tv, $ln, $cmt, $lq, $rq) = parse_line ( $line, $rOpts );
1672  
1673 7455 my ($hide, $encrypt) = (0, 0);
1674 7455 my ($tag, $value, $prefix, $t2);
1675 7455 ▶ if ( $tv ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tv) { } 6782 673 7455
elsif ($ln =~ /^$lb\s*(.+?)\s*$rb$/) { } 111 562 673
1676 6782 ($tag, $value, $prefix, $t2) = _split_assign ( $rOpts, $ln );
1677  
1678 6782 ▶ if ( $cmt =~ m/(^|${lbl_sep})${encrypt_str}(${lbl_sep}|$)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/) { } 6251 531 6782
elsif ($cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/) { } 0 531 531
1679 6251 ($hide, $encrypt) = (1, 1);
1680  
1681 # Don't hide the decrypt string ... (already unreadable)
1682 } elsif ( $cmt =~ m/(^|${lbl_sep})${hide_str}(${lbl_sep}|$)/ ) {
1683 0 $hide = 1;
1684  
1685 } else {
1686 531 ▶ if ( $hide_section || should_we_hide_sensitive_data ( $tag, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hide_section or should_we_hide_sensitive_data($tag, 1)) 2 529 531
Condition66%2/3
$hide_section or should_we_hide_sensitive_data($tag, 1)
l!l&&r!l&&!r
02529
MC/DC50%1/2
$hide_section or should_we_hide_sensitive_data($tag, 1)
$hide_section should_we_hide_sensitive_data($tag, 1)
Truth table66%2/3
A or B
A= $hide_sectionB= should_we_hide_sensitive_data($tag, 1)
ABresult
1X1
011
000
1687 2 $hide = 1;
1688 }
1689 }
1690  
1691 # Is it a section whose contents we need to hide???
1692 } elsif ( $ln =~ m/^${lb}\s*(.+?)\s*${rb}$/ ) {
1693 111 my $section = lc ($1);
1694 111 ▶ $hide_section = should_we_hide_sensitive_data ( $section, 1 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
should_we_hide_sensitive_data($section, 1) ? : 0 111 111
1695 }
1696  
1697 7455 ▶ unless ( $hide ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hide) 1202 6253 7455
1698 1202 DBUG_PRINT ("ENCRYPT", $line);
1699 1202 ▶ unless (print NEW $line, "\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (print NEW $line, "\n") 0 1202 1202
1700 0 return DBUG_RETURN ( croak_helper ($rOpts, $errMsg, 0) );
1701 }
1702 1202 next;
1703 }
1704  
1705 # ------------------------------------------------
1706 # Only Tag/Value pairs get this far ...
1707 # Either needs to be encrypted, hidden, or both.
1708 # ------------------------------------------------
1709  
1710 6253 ▶ my $ass = ( is_assign_spaces ( $rOpts ) ) ? "" : $rOpts->{assign};
Branch: 1 branch
BranchTrueFalseTotal
is_assign_spaces($rOpts) ? : 0 6253 6253
1711 6253 ▶ if ( $cmt ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmt) { } 6253 0 6253
1712 DBUG_PRINT ("ENCRYPT", "%s%s %s %s %s %s",
1713 6253 $prefix, $tag, $ass, $mask, $rOpts->{comment}, $cmt);
1714 } else {
1715 0 DBUG_PRINT ("ENCRYPT", "%s%s %s %s", $prefix, $tag, $ass, $mask);
1716 }
1717  
1718 6253 ▶ unless ( $encrypt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($encrypt) 2 6251 6253
1719 2 ▶ unless (print NEW $line, "\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (print NEW $line, "\n") 0 2 2
1720 0 return DBUG_RETURN ( croak_helper ($rOpts, $errMsg, 0) );
1721 }
1722 2 next;
1723 }
1724  
1725 # --------------------------------------------
1726 # Now let's encrypt the Tag/Value pair ...
1727 # --------------------------------------------
1728  
1729 6251 ++$count;
1730  
1731 # Save the values we need to change safe to use as RegExp strings.
1732 6251 my $old_cmt = convert_to_regexp_string ( $cmt, 1 );
1733 6251 my $old_value = convert_to_regexp_string ( $value, 1 );
1734  
1735 # Modify the label in the comment ...
1736 6251 my $lbl = $rOpts->{decrypt_lbl};
1737 6251 $cmt =~ s/(^|${lbl_sep})${encrypt_str}(${lbl_sep}|$)/$1${lbl}$2/g;
1738  
1739 # Remove any balanced quotes from arround the value ...
1740 6251 ▶ if ( $lq ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lq) 27 6224 6251
1741 27 $value =~ s/^${lq}//;
1742 27 $value =~ s/${rq}$//;
1743 }
1744  
1745 6251 my ($new_value, $nlq, $nrq);
1746 6251 $new_value = encrypt_value ( $value, $t2, $rOpts, $file);
1747 6251 ($new_value, $nlq, $nrq) = _apply_escape_sequences ( $new_value, $rOpts );
1748  
1749 6251 ▶ if ( is_assign_spaces ( $rOpts ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_assign_spaces($rOpts)) { } 0 6251 6251
1750 0 $line =~ s/^(\s*\S+\s+)${old_value}/$1${nlq}${new_value}${nrq}/;
1751 } else {
1752 6251 $line =~ s/(\s*${assign_str}\s*)${old_value}/$1${nlq}${new_value}${nrq}/;
1753 }
1754 6251 $line =~ s/${old_cmt}$/${cmt}/;
1755  
1756 6251 ▶ unless (print NEW $line, "\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (print NEW $line, "\n") 0 6251 6251
1757 0 return DBUG_RETURN ( croak_helper ($rOpts, $errMsg, 0) );
1758 }
1759 } # End the while ENCRYPT loop ...
1760  
1761 7 close (ENCRYPT);
1762 7 close (NEW);
1763  
1764 7 ▶ my $status = ($count == 0) ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$count == 0 ? : 0 7 7
1765  
1766 7 DBUG_RETURN ( $status );
1767 }
1768  
1769  
1770 # ==============================================================
1771  
1772 =item $status = decrypt_config_file_details ( $file, $writeFile, \%rOpts )
1773  
1774 This function decrypts all tag values inside the specified config file that are
1775 marked as encrypted and generates a new file with everything decrypted. If a
1776 tag/value pair isn't marked as being encrypted it is left alone. By default
1777 this label is B<DECRYPT>.
1778  
1779 After a tag's value has been decrypted, the label in the comment is updated
1780 from B<DECRYPT> to B<ENCRYPT> in the config file.
1781  
1782 For this to work, the encryption related options in I<\%rOpts> must match what
1783 was used in the call to I<encrypt_config_file_details> or the decryption will
1784 fail.
1785  
1786 This method ignores any request to source in other config files. You must
1787 decrypt each file individually.
1788  
1789 It writes the results of the decryption process to I<$writeFile>.
1790  
1791 See L<Advanced::Config::Options> for some caveats about this process.
1792  
1793 Returns: B<1> if something was decrypted. B<-1> if nothing was decrypted.
1794 Otherwise B<0> on error.
1795  
1796 =cut
1797  
1798 sub decrypt_config_file_details
1799 9 ▶{
Subroutine decrypt_config_file_details: called CC=27 SCAR=34.8
Pod documented
1800 9 DBUG_ENTER_FUNC ( @_ );
1801 9 my $file = shift;
1802 9 my $scratch = shift;
1803 9 my $rOpts = shift;
1804  
1805 9 unlink ( $scratch );
1806  
1807 # The labels to search for ...
1808 9 my $decrypt_str = convert_to_regexp_string ($rOpts->{decrypt_lbl});
1809 9 my $encrypt_str = convert_to_regexp_string ($rOpts->{encrypt_lbl});
1810 9 my $hide_str = convert_to_regexp_string ($rOpts->{hide_lbl});
1811  
1812 # The label separators used when searching for option labels in a comment ...
1813 9 my $lbl_sep = '[\s.,$!-()]';
1814  
1815 9 my $assign_str = convert_to_regexp_string ($rOpts->{assign});
1816 my ($lb, $rb) = ( convert_to_regexp_string ($rOpts->{section_left}),
1817 9 convert_to_regexp_string ($rOpts->{section_right}) );
1818  
1819 9 my $mask = "*"x8;
1820  
1821 9 DBUG_PRINT ("INFO", "Opening for reading the config file named: %s", $file);
1822  
1823 9 ▶ unless ( open (DECRYPT, "<", $file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open DECRYPT, "<", $file) 0 9 9
1824 0 return DBUG_RETURN ( croak_helper ($rOpts,
1825 "Unable to open the config file.", 0) );
1826 }
1827  
1828 9 DBUG_PRINT ("INFO", "Creating scratch file named: %s", $scratch);
1829 9 ▶ unless ( open (NEW, ">", $scratch) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open NEW, ">", $scratch) 0 9 9
1830 0 close (DECRYPT);
1831 0 return DBUG_RETURN ( croak_helper ($rOpts,
1832 "Unable to create the scratch config file.", 0) );
1833 }
1834  
1835 # Misuse of this option makes the config file unreadable ...
1836 9 ▶ if ( $rOpts->{use_utf8} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"use_utf8"}) 1 8 9
1837 1 binmode (DECRYPT, "encoding(UTF-8)");
1838 1 binmode (NEW, "encoding(UTF-8)");
1839 }
1840  
1841 9 my $errMsg = "Unable to write to the scratch file.";
1842  
1843 9 my $hide_section = 0;
1844 9 my $count = 0;
1845  
1846 9 ▶ while ( <DECRYPT> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DECRYPT)) 7551 9 7560
1847 7551 chomp;
1848 7551 my $line = $_;
1849  
1850 7551 my ($tv, $ln, $cmt, $lq, $rq) = parse_line ( $line, $rOpts );
1851  
1852 7551 my ($hide, $decrypt) = (0, 0);
1853 7551 my ($tag, $value, $prefix, $t2);
1854 7551 ▶ if ( $tv ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tv) { } 6812 739 7551
elsif ($ln =~ /^$lb\s*(.+?)\s*$rb$/) { } 113 626 739
1855 6812 ($tag, $value, $prefix, $t2) = _split_assign ( $rOpts, $ln );
1856  
1857 6812 ▶ if ( $cmt =~ m/(^|${lbl_sep})${decrypt_str}(${lbl_sep}|$)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmt =~ /(^|$lbl_sep)$decrypt_str($lbl_sep|$)/) { } 6263 549 6812
elsif ($cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/ or $cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/) { } 0 549 549
1858 6263 ($hide, $decrypt) = (1, 1);
1859  
1860 ▶ } elsif ( $cmt =~ m/(^|${lbl_sep})${encrypt_str}(${lbl_sep}|$)/ ||
Condition33%1/3
$cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/ or $cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/
l!l&&r!l&&!r
00549
MC/DC0%0/2
$cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/ or $cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/
$cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/ $cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/
Truth table33%1/3
A or B
A= $cmt =~ /(^|$lbl_sep)$encrypt_str($lbl_sep|$)/B= $cmt =~ /(^|$lbl_sep)$hide_str($lbl_sep|$)/
ABresult
1X1
011
000
1861 $cmt =~ m/(^|${lbl_sep})${hide_str}(${lbl_sep}|$)/ ) {
1862 0 $hide = 1;
1863  
1864 } else {
1865 549 ▶ if ( $hide_section || should_we_hide_sensitive_data ( $tag, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hide_section or should_we_hide_sensitive_data($tag, 1)) 4 545 549
Condition66%2/3
$hide_section or should_we_hide_sensitive_data($tag, 1)
l!l&&r!l&&!r
04545
MC/DC50%1/2
$hide_section or should_we_hide_sensitive_data($tag, 1)
$hide_section should_we_hide_sensitive_data($tag, 1)
Truth table66%2/3
A or B
A= $hide_sectionB= should_we_hide_sensitive_data($tag, 1)
ABresult
1X1
011
000
1866 4 $hide = 1;
1867 }
1868 }
1869  
1870 # Is it a section whose contents we need to hide???
1871 } elsif ( $ln =~ m/^${lb}\s*(.+?)\s*${rb}$/ ) {
1872 113 my $section = lc ($1);
1873 113 ▶ $hide_section = should_we_hide_sensitive_data ( $section, 1 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
should_we_hide_sensitive_data($section, 1) ? : 0 113 113
1874 }
1875  
1876 7551 ▶ unless ( $hide ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hide) 1284 6267 7551
1877 1284 DBUG_PRINT ("DECRYPT", $line);
1878 1284 ▶ unless (print NEW $line, "\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (print NEW $line, "\n") 0 1284 1284
1879 0 return DBUG_RETURN ( croak_helper ($rOpts, $errMsg, 0) );
1880 }
1881 1284 next;
1882 }
1883  
1884 # ------------------------------------------------
1885 # Only Tag/Value pairs get this far ...
1886 # Either needs to be decrypted, hidden, or both.
1887 # ------------------------------------------------
1888  
1889 6267 ▶ my $ass = ( is_assign_spaces ( $rOpts ) ) ? "" : $rOpts->{assign};
Branch: 1 branch
BranchTrueFalseTotal
is_assign_spaces($rOpts) ? : 0 6267 6267
1890 6267 ▶ if ( $decrypt ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($decrypt) { } 6263 4 6267
elsif ($cmt) { } 4 0 4
1891 6263 DBUG_PRINT ("DECRYPT", $line);
1892 } elsif ( $cmt ) {
1893 DBUG_PRINT ("DECRYPT", "%s%s %s %s %s %s",
1894 4 $prefix, $tag, $ass, $mask, $rOpts->{comment}, $cmt);
1895 } else {
1896 0 DBUG_PRINT ("DECRYPT", "%s%s %s %s", $prefix, $tag, $ass, $mask);
1897 }
1898  
1899 6267 ▶ unless ( $decrypt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($decrypt) 4 6263 6267
1900 4 ▶ unless (print NEW $line, "\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (print NEW $line, "\n") 0 4 4
1901 0 return DBUG_RETURN ( croak_helper ($rOpts, $errMsg, 0) );
1902 }
1903 4 next;
1904 }
1905  
1906 # --------------------------------------------
1907 # Now let's decrypt the tag/value pair ...
1908 # --------------------------------------------
1909  
1910 6263 ++$count;
1911  
1912 # Save the values we need to change safe to use as RegExp strings.
1913 6263 my $old_cmt = convert_to_regexp_string ( $cmt, 1 );
1914 6263 my $old_value = convert_to_regexp_string ( $value, 1 );
1915  
1916 # Modify the label in the comment ...
1917 6263 my $lbl = $rOpts->{encrypt_lbl};
1918 6263 $cmt =~ s/(^|${lbl_sep})${decrypt_str}(${lbl_sep}|$)/$1${lbl}$2/g;
1919  
1920 # Remove any balanced quotes from arround the value ...
1921 6263 ▶ if ( $lq ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lq) 6263 0 6263
1922 6263 $value =~ s/^${lq}//;
1923 6263 $value =~ s/${rq}$//;
1924 }
1925  
1926 6263 my ($new_value, $nlq, $nrq, $rlq2, $rrq2) = _reverse_escape_sequences ( $value, $rOpts );
1927 6263 $new_value = decrypt_value ( $new_value, $t2, $rOpts, $file);
1928  
1929 6263 ▶ if ( $nlq ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nlq) 6263 0 6263
1930 6263 ▶ if ( $new_value =~ m/${rlq2}/ || $new_value =~ m/${rrq2}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_value =~ /$rlq2/ or $new_value =~ /$rrq2/) 13 6250 6263
Condition66%2/3
$new_value =~ /$rlq2/ or $new_value =~ /$rrq2/
l!l&&r!l&&!r
1306250
MC/DC50%1/2
$new_value =~ /$rlq2/ or $new_value =~ /$rrq2/
$new_value =~ /$rlq2/ $new_value =~ /$rrq2/
Truth table66%2/3
A or B
A= $new_value =~ /$rlq2/B= $new_value =~ /$rrq2/
ABresult
1X1
011
000
1931 13 $nlq = $nrq = ""; # Balanced quotes are not supported for this value!
1932 }
1933 }
1934  
1935 6263 ▶ if ( is_assign_spaces ( $rOpts ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_assign_spaces($rOpts)) { } 0 6263 6263
1936 0 $line =~ s/^(\s*\S+\s+)${old_value}/$1${nlq}${new_value}${nrq}/;
1937 } else {
1938 6263 $line =~ s/(\s*${assign_str}\s*)${old_value}/$1${nlq}${new_value}${nrq}/;
1939 }
1940 6263 $line =~ s/${old_cmt}$/${cmt}/;
1941  
1942 6263 ▶ unless (print NEW $line, "\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (print NEW $line, "\n") 0 6263 6263
1943 0 return DBUG_RETURN ( croak_helper ($rOpts, $errMsg, 0) );
1944 }
1945 } # End the while ENCRYPT loop ...
1946  
1947 9 close (ENCRYPT);
1948 9 close (NEW);
1949  
1950 9 ▶ my $status = ($count == 0) ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$count == 0 ? : 0 9 9
1951  
1952 9 DBUG_RETURN ( $status );
1953 }
1954  
1955  
1956 # ==============================================================
1957  
1958 =item $value = encrypt_value ($value, $tag, $rOpts, $file)
1959  
1960 Takes the I<$value> and encrypts it using the other B<3> args as part of the
1961 encryption key. To successfully decrypt it again you must pass the same B<3>
1962 values for these args to the I<decrypt_value()> call.
1963  
1964 See L<Advanced::Config::Options> for some caveats about this process.
1965  
1966 =cut
1967  
1968 sub encrypt_value
1969 6251 ▶{
Subroutine encrypt_value: called CC=7 SCAR=19.6
Pod documented
1970 6251 DBUG_MASK_NEXT_FUNC_CALL (0); # Masks ${value} ...
1971 6251 DBUG_ENTER_FUNC ( @_ );
1972 6251 my $value = shift; # In clear text ...
1973 6251 my $tag = shift;
1974 6251 my $rOpts = shift;
1975 6251 my $file = shift;
1976  
1977 # Using the file or the alias?
1978 6251 ▶ my $alias = basename ( ( $rOpts->{alias} ) ? $rOpts->{alias} : $file );
Branch: 1 branch
BranchTrueFalseTotal
$rOpts->{'alias'} ? : 11 6240 6251
1979  
1980 # ---------------------------------------------------------------
1981 # Call the custom encryption call back method ...
1982 # ---------------------------------------------------------------
1983 6251 ▶ if ( exists $rOpts->{encrypt_cb} && ref ( $rOpts->{encrypt_cb} ) eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rOpts->{"encrypt_cb"} and ref $rOpts->{"encrypt_cb"} eq "CODE") 6251 0 6251
Condition33%1/3
exists $rOpts->{"encrypt_cb"} and ref $rOpts->{"encrypt_cb"} eq "CODE"
!ll&&!rl&&r
006251
MC/DC0%0/2
exists $rOpts->{"encrypt_cb"} and ref $rOpts->{"encrypt_cb"} eq "CODE"
exists $rOpts->{"encrypt_cb"} ref $rOpts->{"encrypt_cb"} eq "CODE"
Truth table33%1/3
A and B
A= exists $rOpts->{"encrypt_cb"}B= ref $rOpts->{"encrypt_cb"} eq "CODE"
ABresult
0X0
100
111
1984 6251 $value = $rOpts->{encrypt_cb}->( 1, $tag, $value, $alias, $rOpts->{encrypt_cb_opts} );
1985 }
1986  
1987 # ---------------------------------------------------------------
1988 # Pad the value out to a minimum lenth ...
1989 # ---------------------------------------------------------------
1990 6251 my $len1 = length ($value);
1991 6251 my $len2 = length ($tag);
1992 6251 ▶ my $len = ($len1 > $len2) ? $len1 : $len2;
Branch: 1 branch
BranchTrueFalseTotal
$len1 > $len2 ? : 2883 3368 6251
1993 6251 my $len3 = length ($rOpts->{pass_phrase});
1994 6251 ▶ $len = ( $len > $len3) ? $len : $len3;
Branch: 1 branch
BranchTrueFalseTotal
$len > $len3 ? : 6251 0 6251
1995  
1996 # Enforce a minimum length for the value ... (will always end in spaces)
1997 6251 ▶ $len = ($len < 12) ? 15 : ($len + 3);
Branch: 1 branch
BranchTrueFalseTotal
$len < 12 ? : 4787 1464 6251
1998 6251 $value = sprintf ("%-*s", $len, $value . "|");
1999  
2000 # ---------------------------------------------------------------
2001 # Encrypt the value via this module ...
2002 # ---------------------------------------------------------------
2003 6251 $value = _encrypt ( $value, $rOpts->{pass_phrase}, $tag, $alias, $rOpts->{encrypt_by_user} );
2004  
2005 6251 DBUG_RETURN ( $value );
2006 }
2007  
2008 # ==============================================================
2009  
2010 =item $value = decrypt_value ($value, $tag, $rOpts, $file)
2011  
2012 Takes the I<$value> and decrypts it using the other B<3> args as part of the
2013 decryption key. To successfully decrypt it the values for these B<3> args
2014 must match what was passed to I<encryption_value()> when the value was
2015 originally encrypted.
2016  
2017 See L<Advanced::Config::Options> for some caveats about this process.
2018  
2019 =cut
2020  
2021 sub decrypt_value
2022 12599 ▶{
Subroutine decrypt_value: called CC=4 SCAR=14.0
Pod documented
2023 12599 DBUG_ENTER_FUNC ( @_ );
2024 12599 my $value = shift; # It's encrypted ...
2025 12599 my $tag = shift;
2026 12599 my $rOpts = shift;
2027 12599 my $file = shift;
2028  
2029 12599 DBUG_MASK (0); # Mask the return value ... It's sensitive by definition!
2030  
2031 # Using the file or the alias?
2032 12599 ▶ my $alias = basename ( ( $rOpts->{alias} ) ? $rOpts->{alias} : $file );
Branch: 1 branch
BranchTrueFalseTotal
$rOpts->{'alias'} ? : 67 12532 12599
2033  
2034 # ---------------------------------------------------------------
2035 # Decrypt the value via this module ...
2036 # ---------------------------------------------------------------
2037 12599 $value = _encrypt ( $value, $rOpts->{pass_phrase}, $tag, $alias, $rOpts->{encrypt_by_user} );
2038 12599 $value =~ s/\|[\s\0]+$//; # Trim any trailing spaces or NULL chars.
2039  
2040 # ---------------------------------------------------------------
2041 # Call the custom decryption call back method ...
2042 # ---------------------------------------------------------------
2043 12599 ▶ if ( exists $rOpts->{encrypt_cb} && ref ( $rOpts->{encrypt_cb} ) eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rOpts->{"encrypt_cb"} and ref $rOpts->{"encrypt_cb"} eq "CODE") 12599 0 12599
Condition33%1/3
exists $rOpts->{"encrypt_cb"} and ref $rOpts->{"encrypt_cb"} eq "CODE"
!ll&&!rl&&r
0012599
MC/DC0%0/2
exists $rOpts->{"encrypt_cb"} and ref $rOpts->{"encrypt_cb"} eq "CODE"
exists $rOpts->{"encrypt_cb"} ref $rOpts->{"encrypt_cb"} eq "CODE"
Truth table33%1/3
A and B
A= exists $rOpts->{"encrypt_cb"}B= ref $rOpts->{"encrypt_cb"} eq "CODE"
ABresult
0X0
100
111
2044 12599 $value = $rOpts->{encrypt_cb}->( 0, $tag, $value, $alias, $rOpts->{encrypt_cb_opts} );
2045 }
2046  
2047 12599 DBUG_RETURN ( $value );
2048 }
2049  
2050  
2051 # ==============================================================
2052 # Before writing an encrypted value to a config file, all problem
2053 # character sequences must be converted into escape sequences. So
2054 # that when the encrypted value is read back in again it won't cause
2055 # parsing issues.
2056 sub _apply_escape_sequences
2057 6251 ▶{
Subroutine _apply_escape_sequences: called CC=2 SCAR=6.9
2058 6251 DBUG_ENTER_FUNC ( @_ );
2059 6251 my $value = shift; # Encrypted ...
2060 6251 my $rOpts = shift;
2061  
2062 6251 my ( $lq, $rq ) = _get_encryption_quotes ( $rOpts );
2063  
2064 # Strings to use in the regular expressions ...
2065 6251 my ($l_quote, $r_quote) = ( convert_to_regexp_string ($lq, 1),
2066 convert_to_regexp_string ($rq, 1) );
2067 6251 my $cmt = convert_to_regexp_string ($rOpts->{comment}, 1);
2068 6251 my $var = convert_to_regexp_string ($rOpts->{variable_left}, 1);
2069  
2070 # ---------------------------------------------------------------
2071 # Replace any problem char for values with escape sequences ...
2072 # ---------------------------------------------------------------
2073 6251 $value =~ s/\\/\\z/sg; # Done so we can use \ as an escape sequence.
2074 6251 $value =~ s/\n/\\n/sg; # Remove embedded "\n" so no mult-lines.
2075 6251 $value =~ s/%/\\p/sg; # So calls to DBUG_PRINT won't barf ...
2076 6251 $value =~ s/${cmt}/\\3/sg; # Don't want any comment chars ...
2077 6251 ▶ if ( $rq ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rq) 6251 0 6251
2078 6251 $value =~ s/${l_quote}/\\q/sg;
2079 6251 $value =~ s/${r_quote}/\\Q/sg;
2080 }
2081 6251 $value =~ s/${var}/\\v/sg; # So nothing looks like a variable ...
2082 6251 $value =~ s/\0/\\0/sg; # So no embedded null chars ...
2083  
2084 6251 DBUG_RETURN ( $value, $lq, $rq );
2085 }
2086  
2087  
2088 # ==============================================================
2089 # When an encrypted value is read in from the config file, all escape
2090 # secuences need to be removed before the value can be decrypted.
2091 # These escape sequences were required to avoid parsing issues when
2092 # handling encrypted values.
2093 sub _reverse_escape_sequences
2094 12613 ▶{
Subroutine _reverse_escape_sequences: called CC=2 SCAR=6.9
2095 12613 DBUG_ENTER_FUNC ( @_ );
2096 12613 my $value = shift; # Encrypted with escape sequences ...
2097 12613 my $rOpts = shift;
2098  
2099 12613 my ( $lq, $rq ) = _get_encryption_quotes ( $rOpts );
2100 12613 my $cmt = $rOpts->{comment};
2101 12613 my $var = $rOpts->{variable_left};
2102  
2103 # Strings to use in the regular expressions ... (by caller)
2104 12613 my ($l_quote, $r_quote) = ( convert_to_regexp_string ($lq, 1),
2105 convert_to_regexp_string ($rq, 1) );
2106  
2107 # ---------------------------------------------------------------
2108 # Replace the escape sequences to get back the problem chars ...
2109 # Done in reverse order of what was done in: _apply_escape_sequences()!
2110 # ---------------------------------------------------------------
2111 12613 $value =~ s/\\0/\0/sg;
2112 12613 $value =~ s/\\v/${var}/sg;
2113 12613 ▶ if ( $rq ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rq) 12613 0 12613
2114 12613 $value =~ s/\\Q/${rq}/sg;
2115 12613 $value =~ s/\\q/${lq}/sg;
2116 }
2117 12613 $value =~ s/\\3/${cmt}/sg;
2118 12613 $value =~ s/\\p/%/sg;
2119 12613 $value =~ s/\\n/\n/sg;
2120 12613 $value =~ s/\\z/\\/sg;
2121  
2122 12613 DBUG_RETURN ( $value, $lq, $rq, $l_quote, $r_quote );
2123 }
2124  
2125  
2126 # ==============================================================
2127 sub _get_encryption_quotes
2128 18864 ▶{
Subroutine _get_encryption_quotes: called CC=3 SCAR=11.0
2129 18864 my $rOpts = shift;
2130  
2131 18864 my ($lq, $rq) = ("", "");
2132 18864 ▶ if ( using_default_quotes ( $rOpts ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (using_default_quotes($rOpts)) { } 18808 56 18864
elsif (not $rOpts->{'disable_quotes'}) { } 56 0 56
2133 18808 $lq = $rq = "'"; # Chooses ' over " ...
2134 } elsif ( ! $rOpts->{disable_quotes} ) {
2135 56 ($lq, $rq) = ( $rOpts->{quote_left}, $rOpts->{quote_right} );
2136 }
2137  
2138 18864 return ( $lq, $rq );
2139 }
2140  
2141  
2142 # ==============================================================
2143 # USAGE: $val = _encrypt ($value, $pass_code, $tag, $alias, $usr_flg)
2144 #
2145 # Both encrypts & decrypts the value ...
2146  
2147 sub _encrypt
2148 18850 ▶{
Subroutine _encrypt: called CC=5 SCAR=16.8
2149 18850 DBUG_MASK_NEXT_FUNC_CALL (0, 1); # Masks ${val} & ${pass} ...
2150 18850 DBUG_ENTER_FUNC ( @_ );
2151 18850 my $val = shift; # Sensitive ... if not already encrypted.
2152 18850 my $pass = shift; # Very, very sensitive ... always clear text.
2153 18850 my $tag = shift;
2154 18850 my $alias = shift;
2155 18850 my $usr_flg = shift; # 0 - no, 1 - yes
2156 18850 DBUG_MASK (0);
2157  
2158 # Verify lengths are different to prevent repeatable patterns.
2159 18850 ▶ if ( length ( $tag ) == length ( $alias ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tag == length $alias) 0 18850 18850
2160 0 $tag .= "|"; # Make different lengths
2161 }
2162  
2163 18850 my $len = length ( $val );
2164  
2165 18850 my $key1 = _make_key ( $tag, $len );
2166 18850 my $key2 = _make_key ( $alias, $len );
2167 18850 my $res = $key1 ^ $key2;
2168  
2169 18850 ▶ if ( $pass ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass) 0 18850 18850
2170 0 my $key3 = _make_key ( $pass, $len );
2171 0 $res = $res ^ $key3;
2172 }
2173  
2174 18850 ▶ if ( $usr_flg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usr_flg) 0 18850 18850
2175 0 my $key4 = _make_key ( $gUserName, $len );
2176 0 $res = $res ^ $key4;
2177 }
2178  
2179 18850 ▶ unless ( $val =~ m/[^\x00-\xff]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /[^\x00-\xff]/) { } 3036 15814 18850
2180 15814 $res = $res ^ $val; # ascii ...
2181 } else {
2182 # Unicode version of ($res ^ $val) ...
2183 3036 $res = _bitwise_exclusive_or ( $res, $val );
2184 }
2185  
2186 18850 DBUG_RETURN ( $res ); # Sometimes encrypted and other times not!
2187 }
2188  
2189 # ==============================================================
2190 sub _bitwise_exclusive_or
2191 3036 ▶{
Subroutine _bitwise_exclusive_or: called CC=2 SCAR=6.9
2192 3036 DBUG_ENTER_FUNC (); # Dropped @_ on purpose, always sensitive
2193 3036 my $mask = shift;
2194 3036 my $unicode = shift;
2195 3036 DBUG_MASK (0);
2196  
2197 3036 my @m = unpack ("C*", $mask);
2198 3036 my @u = unpack ("U*", $unicode);
2199  
2200 3036 my @ans;
2201 3036 foreach ( 0..$#u ) {
2202 46644 $ans[$_] = $m[$_] ^ $u[$_]; # Exclusive or of 2 integers still supported.
2203 }
2204  
2205 3036 DBUG_RETURN ( pack ("U*", @ans) );
2206 }
2207  
2208 # ==============================================================
2209 # USAGE: $key = _make_key ($target, $len);
2210  
2211 sub _make_key
2212 37700 ▶{
Subroutine _make_key: called CC=3 SCAR=11.0
2213 37700 DBUG_MASK_NEXT_FUNC_CALL (0); # Masks ${target} ...
2214 37700 DBUG_ENTER_FUNC ( @_ );
2215 37700 my $target = shift; # May be ascii or unicode ...
2216 37700 my $len = shift;
2217 37700 DBUG_MASK (0);
2218  
2219 37700 my $phrase;
2220 37700 ▶ unless ( $target =~ m/[^\x00-\xff]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ /[^\x00-\xff]/) { } 42 37658 37700
2221 # Normal text ... (ascii)
2222 37658 $phrase = $target . pack ("C*", reverse (unpack ("C*", $target)));
2223  
2224 } else {
2225 # Unicode strings (utf8 / Wide Chars)
2226 # Strip off the upper byte from each unicode char ...
2227 42 my @ans = map { $_ % 0x100 } unpack ("U*", $target);
2228 42 $phrase = pack ("C*", @ans) . pack ("C*", reverse (@ans));
2229 }
2230  
2231 37700 my $key = $phrase;
2232 37700 ▶ while ( length ( $key ) < $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $key < $len) 12400 37700 50100
2233 12400 $key .= $phrase;
2234 }
2235  
2236 37700 $key = substr ( $key, 0, $len ); # Truncate it to fit ...
2237  
2238 37700 DBUG_RETURN ( $key ); # Always an ascii string ...
2239 }
2240  
2241 # ==============================================================
2242  
2243 =back
2244  
2245 =head1 COPYRIGHT
2246  
2247 Copyright (c) 2007 - 2026 Curtis Leach. All rights reserved.
2248  
2249 This program is free software. You can redistribute it and/or modify it under
2250 the same terms as Perl itself.
2251  
2252 =head1 SEE ALSO
2253  
2254 L<Advanced::Config> - The main user of this module. It defines the Config object.
2255  
2256 L<Advanced::Config::Options> - Handles the configuration of the Config module.
2257  
2258 L<Advanced::Config::Date> - Handles date parsing for get_date().
2259  
2260 L<Advanced::Config::Examples> - Provides some sample config files and commentary.
2261  
2262 =cut
2263  
2264 # ==============================================================
2265 #required if module is included w/ require command;
2266 27 1;
2267  
« Config/Options.pm ↑ Summary Config.pm »