%Psh::Util
::command_hash
=();
%Psh::Util
::path_hash
=();
# Print unclassified debug output
print STDERR
@_ if $Psh::debugging
&& $Psh::debugging
=~ /o/;
# Print classified debug output
print STDERR
@_ if $Psh::debugging
and
($Psh::debugging
eq '1' or
$Psh::debugging
=~ /$class/);
# print_i18n( stream, key, args)
# print_out_i18n( key, args)
# print_error_i18n( key, args)
# The print..._i18n suite of functions will fetch the
# text from the %text hash, replace %1 with the first arg,
# %2 with the second and so on and then print it out
my( $stream, $text, @rest) = @_;
$text= Psh
::Locale
::get_text
($text);
for( my $i=1; $i<=@rest; $i++)
$text=~ s/\%$i/$rest[$i-1]/g;
# Copied from readline.pl - pretty prints a list in columns
my ($lines, $columns, $mark, $index);
## find width of widest entry
my $screen_width=$ENV{COLUMNS
};
if (ref $list[0] and ref $list[0] eq 'ARRAY') {
grep(length > $maxwidth && ($maxwidth = length), @list);
$columns = $maxwidth >= $screen_width?
1:int($screen_width / $maxwidth);
## if there's enough margin to interspurse among the columns, do so.
$maxwidth += int(($screen_width % $maxwidth) / $columns);
$lines = int((@list + $columns - 1) / $columns);
$columns-- while ((($lines * $columns) - @list + 1) > $lines);
for (my $l = 0; $l < $lines; $l++) {
for ($index = $l; $index <= $mark; $index += $lines) {
my $tmp= my $item= $list[$index];
$tmp=~ s/\001(.*?)\002//g;
my $diff= length($item)-length($tmp);
my $dispsize= $maxwidth+$diff;
print_out
(sprintf("%-${dispsize}s", $item));
$item=~s/\001//g; $item=~s/\002//g;
return undef unless $dir;
return $Psh::Util
::path_hash
{$dir} if $Psh::Util
::path_hash
{$dir};
my $result= Psh
::OS
::abs_path
($dir);
$result= Psh
::OS
::get_home_dir
();
} elsif ( substr($dir,0,2) eq '~/') {
substr($dir,0,1)= Psh
::OS
::get_home_dir
();
} elsif ( substr($dir,0,1) eq '~' ) {
my $fs= $Psh::OS
::FILE_SEPARATOR
;
my ($user)= $dir=~/^\~(.*?)$fs/;
substr($dir,0,length($user)+1)= Psh
::OS
::get_home_dir
($user);
my $tmp= Psh
::OS
::rel2abs
($dir,$ENV{PWD
});
if ( CORE
::chdir($tmp)) {
$result = Psh
::OS
::getcwd_psh
();
if (!CORE
::chdir($old)) {
print STDERR
"Could not change directory back to $old!\n";
CORE
::chdir(Psh
::OS
::get_home_dir
())
# if ($tmp and !$result) {
# local $SIG{__WARN__}= {};
# $result= Cwd::abs_path($tmp);
# print_debug_class('e',"(abs_path) Error: $@") if $@;
return undef unless $result;
$result.='/' unless $result=~ m
:[/\\]:; # abs_path strips / from letter: on Win
$Psh::Util
::path_hash
{$dir}= $result if Psh
::OS
::file_name_is_absolute
($dir);
%Psh::Util
::command_hash
= ();
my @path = split($Psh::OS
::PATH_SEPARATOR
, $ENV{PATH
});
foreach my $dir (@path) {
my $dir= Psh
::Util
::abs_path
($dir);
next unless -r
$dir and -x _
;
push @Psh::absed_path
, $dir;
print_debug_class
('e',"(recalc_absed_path) Error: $@") if $@
;
# Without the eval Psh might crash if the directory
# string which(string FILENAME)
# search for an occurrence of FILENAME in the current path as given by
# $ENV{PATH}. Return the absolute filename if found, or undef if not.
# "static variables" for which() :
my $FS=$Psh::OS
::FILE_SEPARATOR
;
my $re2="^(.*)$tmp([^$tmp]+)\$";
print_debug_class
('e',"(util::before which) Error: $@") if $@
;
return undef unless $cmd;
my $path_element= $1 || '';
my $cmd_element= $2 || '';
return undef unless $path_element and $cmd_element;
$path_element=Psh
::Util
::abs_path
($path_element);
return undef unless $path_element;
my $try= Psh
::OS
::catfile
($path_element,$cmd_element);
if ((-x
$try) and (! -d _
)) { return $try; }
return $Psh::Util
::command_hash
{$cmd} if exists $Psh::Util
::command_hash
{$cmd} and !$all;
if ($cmd !~ m/$Psh::which_regexp/) { return undef; }
if ($last_path_cwd ne ($ENV{PATH
} . $ENV{PWD
})) {
$last_path_cwd = $ENV{PATH
} . $ENV{PWD
};
my @path_extension=Psh
::OS
::get_path_extension
();
foreach my $dir (@Psh::absed_path
) {
my $try = Psh
::OS
::catfile
($dir,$cmd);
foreach my $ext (@path_extension) {
if ((-x
$try.$ext) and (!-d _
)) {
$Psh::Util
::command_hash
{$cmd} = $try.$ext unless $all;
return $try.$ext unless $all;
$Psh::Util
::command_hash
{$cmd} = undef; # no delete by purpose
# starts_with( text, prefix)
# Returns true if text starts with prefix
my ($text, $prefix) = @_;
return length($text)>=length($prefix) &&
substr($text,0,length($prefix)) eq $prefix;
# ends_with( text, suffix)
# Returns true if text ends with suffix
my ( $text, $suffix) = @_;
return length($text)>=length($suffix) &&
substr($text,-length($suffix)) eq $suffix;
# list parse_hosts_file( text)
# Gets a standard hosts file as input and returns
# a list of the hostnames mentioned in the file
my @lines= split( /\n|\r|\r\n/, $text);
foreach my $line (@lines) {
next if $line=~/^\s*$/; # Skip blank lines
next if $line=~/^\s*\#/; # Skip comment lines
push @result, grep { length($_)>0 } split( /\s/, $rest);
# char prompt( string allowedchars, string prompt)
# prompts the user until he answers with one of the
$allowed= "^[$allowed]\$";
} while (!$line || lc($line) !~ $allowed);