X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;f=lib%2FUtils.pm;h=acebce5074f634f4608c90ddd4b8e64fdc60083c;hb=741c2b2cf2529abe150429f0f9ccd5f4f9b19c90;hp=74c38fa2b91d858f9f1f0a4a07480bf732764fdc;hpb=020a4a5ea2be725b155cae3a2cadc9aba3911b9b;p=clearscm.git diff --git a/lib/Utils.pm b/lib/Utils.pm index 74c38fa..acebce5 100644 --- a/lib/Utils.pm +++ b/lib/Utils.pm @@ -63,6 +63,7 @@ use base 'Exporter'; use POSIX qw (setsid); use File::Spec; use Carp; +use Term::ReadKey; use OSDep; use Display; @@ -71,6 +72,7 @@ our @EXPORT = qw ( EnterDaemonMode Execute GetChildren + GetPassword InArray PageOutput PipeOutput @@ -83,6 +85,15 @@ our @EXPORT = qw ( Usage ); +sub _restoreTerm () { + # In case the user hits Ctrl-C + print "\nControl-C\n"; + + ReadMode 'normal'; + + exit; +} # _restoreTerm + sub EnterDaemonMode (;$$$) { my ($logfile, $errorlog, $pidfile) = @_; @@ -131,13 +142,6 @@ Returns: my $file; - if ($pidfile) { - $pidfile = File::Spec->rel2abs ($pidfile); - - open $file, '>', $pidfile - or warning "Unable to open pidfile $pidfile for writing - $!"; - } # if - # Redirect STDIN to $NULL open STDIN, '<', $NULL or error "Can't read $NULL ($!)", 1; @@ -167,6 +171,11 @@ Returns: # Write pidfile if specified if ($pidfile) { + $pidfile = File::Spec->rel2abs ($pidfile); + + open $file, '>', $pidfile + or warning "Unable to open pidfile $pidfile for writing - $!"; + print $file "$$\n"; close $file; @@ -300,6 +309,79 @@ Returns: return @children; } # GetChildren +sub GetPassword (;$) { + my ($prompt) = @_; + +=pod + +=head2 GetPassword (;$prompt) + +Prompt for a password + +Parameters: + +=for html
+ +=over + +=item $prompt + +Prompt string to use (Default: "Password:") + +=back + +=for html
+ +Returns: + +=for html
+ +=over + +=item $password + +=back + +=for html
+ +=cut + + + $prompt ||= 'Password'; + + my $password; + + local $| = 1; + + print "$prompt:"; + + $SIG{INT} = \&_restoreTerm; + + ReadMode 'cbreak'; + + while () { + my $key; + + while (not defined ($key = ReadKey -1)) { } + + if ($key =~ /(\r|\n)/) { + print "\n"; + + last; + } # if + + print '*'; + + $password .= $key; + } # while + + ReadMode 'restore'; # Reset tty mode before exiting. + + $SIG{INT} = 'DEFAULT'; + + return $password; +} # GetPassword + sub InArray ($@) { my ($item, @array) = @_; @@ -447,7 +529,7 @@ Returns: =cut - open my $pipe, "|$to" + open my $pipe, '|-', $to or error "Unable to open pipe - $!", 1; foreach (@output) { @@ -549,6 +631,8 @@ Returns: $pipeToStop ||= $pipe; close $pipeToStop if $pipeToStop; + + return; } # StopPipe sub PageOutput (@) { @@ -715,7 +799,7 @@ Returns: return @cleansed_lines; } else { - local $/; + local $/ = undef; return <$file>; } # if @@ -743,7 +827,8 @@ and the values of the hash will be the counters. =item $log -Logger object to log stats to (if specified) +Logger object to log stats to (if specified). Note: if the Logger object has +errors or warnings then they will be automatically included in the output. =back @@ -765,7 +850,12 @@ Returns: my $msg = "$FindBin::Script Run Statistics:"; - if (scalar keys %$total) { + if ($log and ref $log eq 'Logger') { + $total->{errors} = $log->{errors}; + $total->{warnings} = $log->{warnings}; + } # if + + if (keys %$total) { # Display statistics (if any) if ($log) { $log->msg ($msg); @@ -774,10 +864,10 @@ Returns: } # if foreach (sort keys %$total) { - $msg = $$total{$_} . "\t $_"; + $msg = $total->{$_} . "\t $_"; if ($log) { - $log->msg ($$total{$_} . "\t $_"); + $log->msg ($total->{$_} . "\t $_"); } else { display $msg; } # if @@ -854,11 +944,11 @@ L =head2 ClearSCM Perl Modules -=for html

Display

+=for html

Display

-=for html

Logger

+=for html

Logger

-=for html

OSDep

+=for html

OSDep

=head1 INCOMPATABILITIES