From: Ian Jackson Subject: [OSSTEST PATCH 10/32] readglobalconfig: Break out $readcfg as a sub Date: Tue, 24 Mar 2015 19:02:33 +0000 Message-ID: <1427223775-8592-11-git-send-email-ian.jackson@eu.citrix.com> References: <1427223775-8592-1-git-send-email-ian.jackson@eu.citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1YaU6W-0008Sn-9L for xen-devel@lists.xenproject.org; Tue, 24 Mar 2015 19:03:08 +0000 In-Reply-To: <1427223775-8592-1-git-send-email-ian.jackson@eu.citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xenproject.org Cc: Ian Jackson , Ian Campbell List-Id: xen-devel@lists.xenproject.org And make it use anonymous filehandles, and be able to not tolerate ENOENT. This is preparation for making it recursive (`Include'). No functional change. Signed-off-by: Ian Jackson --- Osstest.pm | 75 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/Osstest.pm b/Osstest.pm index aaaaf7a..8e63389 100644 --- a/Osstest.pm +++ b/Osstest.pm @@ -106,43 +106,50 @@ sub readglobalconfig () { my $cfgfiles = $ENV{'OSSTEST_CONFIG'} || "$ENV{'HOME'}/.xen-osstest/config"; - foreach my $cfgfile (split /\:/, $cfgfiles) { - if (!open C, '<', "$cfgfile") { - die "$cfgfile $!" unless $!==&ENOENT; - } else { - while () { - die "missing newline" unless chomp; - s/^\s*//; - s/\s+$//; - next if m/^\#/; - next unless m/\S/; - if (m/^($cfgvar_re)\s+(\S.*)$/) { - $c{$1} = $2; - } elsif (m/^($cfgvar_re)=\s*\<\<(\'?)(.*)\2\s*$/) { - my ($vn,$qu,$delim) = ($1,$2,$3); - my $val = ''; - $!=0; while () { - last if $_ eq "$delim\n"; - $val .= $_; - } - die $! unless length $_; - die unless !length $val || $val =~ m/\n$/; - if ($qu eq '') { - my $reconstruct = - "\$val = <<${qu}${delim}${qu}; 1;\n". - "${val}${delim}\n"; - eval $reconstruct or - die "$1 here doc ($reconstruct) $@"; - } - $c{$vn} = $val; - } elsif (m/^($cfgvar_re)=(.*)$/) { - eval "\$c{$1} = ( $2 ); 1;" or die "$1 parsed val ($2) $@"; - } else { - die "bad syntax"; + my $readcfg; + $readcfg = sub ($$) { + my ($cfgfile,$enoent_ok) = @_; + my $fh = new IO::File $cfgfile, '<'; + if (!$fh) { + die "$cfgfile $!" unless $enoent_ok && $!==&ENOENT; + return; + } + while (<$fh>) { + die "missing newline" unless chomp; + s/^\s*//; + s/\s+$//; + next if m/^\#/; + next unless m/\S/; + if (m/^($cfgvar_re)\s+(\S.*)$/) { + $c{$1} = $2; + } elsif (m/^($cfgvar_re)=\s*\<\<(\'?)(.*)\2\s*$/) { + my ($vn,$qu,$delim) = ($1,$2,$3); + my $val = ''; + $!=0; while (<$fh>) { + last if $_ eq "$delim\n"; + $val .= $_; + } + die $! unless length $_; + die unless !length $val || $val =~ m/\n$/; + if ($qu eq '') { + my $reconstruct = + "\$val = <<${qu}${delim}${qu}; 1;\n". + "${val}${delim}\n"; + eval $reconstruct or + die "$1 here doc ($reconstruct) $@"; } + $c{$vn} = $val; + } elsif (m/^($cfgvar_re)=(.*)$/) { + eval "\$c{$1} = ( $2 ); 1;" or die "$1 parsed val ($2) $@"; + } else { + die "bad syntax"; } - close C or die "$cfgfile $!"; } + close $fh or die "$cfgfile $!"; + }; + + foreach my $cfgfile (split /\:/, $cfgfiles) { + $readcfg->($cfgfile, 1); } # dynamic default config settings -- 1.7.10.4