[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [OSSTEST PATCH 10/32] readglobalconfig: Break out $readcfg as a sub



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 <ian.jackson@xxxxxxxxxxxxx>
---
 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 (<C>) {
-               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 (<C>) {
-                       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


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.