Merge branch 'master' of git+ssh://github.com/adefaria/clearscm
[clearscm.git] / lib / Utils.pm
index 78af171..b9be1e4 100644 (file)
@@ -79,6 +79,7 @@ our @EXPORT = qw (
   PipeOutput
   PipeOutputArray
   ReadFile
+  RequiredFields
   RedirectOutput
   StartPipe
   Stats
@@ -156,7 +157,7 @@ Returns:
     or error "Can't write to $errorlog ($!)", 1;
 
   # Change the current directory to /
-  my $ROOT = $ARCH eq "windows" ? "C:\\" : "/";
+  my $ROOT = $ARCHITECTURE eq "windows" ? "C:\\" : "/";
   chdir $ROOT
     or error "Can't chdir to $ROOT ($!), 1";
 
@@ -232,17 +233,11 @@ STDOUT then do so in the $command passed in.
 
 =cut
 
-  # Save $SIG{CHLD} so we can set it to 'DEFAULT' and then restore it later.
-  # Helps when you are doing process handling.
-  my $sigchld = $SIG{CHLD};
-
   local $SIG{CHLD} = 'DEFAULT';
 
   my @output = `$cmd`;
   my $status = $?;
 
-  local $SIG{CHLD} = $sigchld;
-
   chomp @output;
 
   return ($status, @output);
@@ -347,10 +342,9 @@ Returns:
 
 =cut  
 
-  
   $prompt ||= 'Password';
 
-  my $password;
+  my $password = '';
 
   local $| = 1;
 
@@ -371,9 +365,18 @@ Returns:
        last;
     } # if
 
-    print '*';
+    # Handle backspaces
+    if ($key eq chr(127)) {
+      unless ($password eq '') {
+        chop $password;
 
-    $password .= $key;
+        print "\b \b";
+      } # unless
+    } else {
+      print '*';
+
+      $password .= $key;
+    } # if
   } # while
 
   ReadMode 'restore'; # Reset tty mode before exiting.
@@ -469,7 +472,7 @@ In a scalar context just the 1 minute load average.
   # TODO: Make it work on Windows...
   return if $^O =~ /win/i;
 
-  open my $loadAvg, '/proc/loadavg'
+  open my $loadAvg, '<', '/proc/loadavg'
     or croak "Unable to open /proc/loadavg\n";
 
   my $load = <$loadAvg>;
@@ -977,6 +980,67 @@ Returns:
   exit 1;
 } # Usage
 
+sub RequiredFields($$) {
+
+=pod
+
+=head2 RequiredFields($total, $log)
+
+Check if a list of fields are contained in a hash
+
+Parameters:
+
+=for html <blockquote>
+
+=over
+
+=item $fields
+
+Array reference to a list of field names that are required
+
+=item $rec
+
+Hash reference whose key values we are checking
+
+=back
+
+=for html </blockquote>
+
+Returns:
+
+=for html <blockquote>
+
+=over
+
+=item Message
+
+Returns either an empty string or a string naming the first missing required
+field
+
+=back
+
+=for html </blockquote>
+
+=cut
+
+  my ($fields, $rec) = @_;
+
+  for my $fieldname (@$fields) {
+    my $found = 0;
+
+    for (keys %$rec) {
+      if ($fieldname eq $_) {
+        $found = 1;
+        last;
+      } # if
+    } # for
+
+    return "$fieldname is required" unless $found;
+  } # for
+
+  return;
+} # RequiredFields
+
 END {
   StopPipe;
 } # END