use Utils;
use Term::ReadLine;
-use Term::ANSIColor qw(color);
+use Term::ANSIColor qw (color);
# Package globals
my $_pos = 0;
my $_haveGnu;
-my $promptColor = color('bold yellow');
-my $inputColor = color('underline');
-my $resetColor = color('reset');
-
-my (%_cmds, $_attribs);
-
-our $_cmdline;
+my (%_cmds, $_cmdline, $_attribs);
BEGIN {
# See if we can load Term::ReadLine::Gnu
} # BEGIN
# Share %opts
-our %opts = (
- color => 1,
-);
+our %opts;
my %builtin_cmds = (
history => {
- help => 'history [<start> <end>]',
- description => 'Displays cmd history. You can specify where to <start> and where to <end>.
-Default is to list only the last screen full lines of history
-(as denoted by $LINES).'
+ help => 'history [[start] [end]]',
+ description =>
+ "Displays cmd history. You can specify where to <start> and where to <end>\n"
+ . "Default is to list only the last screen full lines of history (as denoted\n"
+ . "by \$LINES).",
},
help => {
description => 'Displays help.',
},
+ helpall => {
+ help => 'helpall',
+ description => 'Display all help, including builtin commands',
+ },
+
savehist => {
- help => 'savehist <file> [<start> <end>]',
- description => 'Saves a section of the history to a file. You can specify where to <start>
-and where to <end>. Default is to save all of the history to
-the specified file.',
+ help => 'savehist [file] [[start] [end]]',
+ description =>
+ "Saves a section of the history to a file. You can specify where to <start>\n"
+ . "and where to <end>. Default is to save all of the history to the specified\n"
+ . "file.",
},
get => {
- help => 'get <var>',
+ help => 'get [var]',
description => 'Gets a variable.',
},
set => {
- help => 'set <var>=<expression>',
- description => 'Sets a variable. Note that expression can be any valid expression.',
+ help => 'set [var]=[expression]',
+ description =>
+ 'Sets a variable. Note that expression can be any valid expression.',
},
vars => {
help => 'vars',
- description => 'Displays all known variables.',
+ description =>
+ 'Displays all known variables.',
},
source => {
- help => 'source <file>',
- description => 'Run commands from a file.',
+ help => 'source [file]',
+ description =>
+ 'Run commands from a file.',
},
color => {
- help => 'color [<on|off>]',
- description => 'Turn on|off color. With no options displays status of color.',
+ help => 'color [(on|off)]',
+ description =>
+ 'Turn on|off color. With no options displays status of color.',
},
trace => {
- help => 'trace [<on|off>]',
- description => 'Turn on|off tracing. With no options displays status of trace.',
+ help => 'trace [(on|off)]',
+ description =>
+ 'Turn on|off tracing. With no options displays status of trace.',
},
);
sub _complete($$$$) {
my ($text, $line, $start, $end) = @_;
- return $_cmdline->completion_matches ($text, \&CmdLine::_cmdCompletion);
+ return $_cmdline->completion_matches($text, \&CmdLine::_cmdCompletion);
} # _complete
sub _gethelp() {
$self->help($line);
} else {
$CmdLine::cmdline->help($line);
- } # if
+ } # if
$_cmdline->on_new_line;
+
+ return;
} # _gethelp
-sub _interpolate ($) {
+sub _interpolate($) {
my ($self, $str) = @_;
# Skip interpolation for the perl command (Note this is raid specific)
- return $str if $str =~ /^\s*perl\s*/i;
+ return $str
+ if $str =~ /^\s*perl\s*/i;
while ($str =~ /\$/) {
if ($str =~ /\$(\w+)/) {
my $varname = $1;
- if ($self->{vars}{$varname}) {
+ if (defined $self->{vars}{$varname}) {
if ($self->{vars}{$varname} =~ / /) {
$str =~ s/\$$varname/\'$self->{vars}{$varname}\'/;
} else {
$str =~ s/\$$varname/$self->{vars}{$varname}/;
} # if
} else {
- $str =~ s/\$$varname//;
+ $str =~ s/\$$varname//;
} # if
} # if
} # while
sub _builtinCmds($) {
my ($self, $line) = @_;
- unless (defined $line) {
+ unless ($line) {
display '';
return 'exit';
} # unless
system $1;
} # if
- #$_cmdline->remove_history($_cmdline->where_history);
+ #$_cmdline->remove_history ($_cmdline->where_history);
return;
} # if
$cmd = $1;
} # if
- return unless $cmd;
-
- my @parms;
+ return
+ unless $cmd;
# Search for matches of partial commands
my $foundCmd;
} # if
if ($builtin_cmds{$cmd}) {
- if ($line =~ /^\s*help\s*(.*)/i) {
+ if ($line =~ /^\s*helpall\s*$/i) {
+ $self->help('', 1);
+ } elsif ($line =~ /^\s*help\s*(.*)/i) {
if ($1 =~ /(.+)$/) {
$self->help($1);
} else {
} # if
} elsif ($line =~ /^\s*history\s*(.*)/i) {
if ($1 =~ /(\d+)\s+(\d+)\s*$/) {
- $self->history ('list', $1, $2);
+ $self->history('list', $1, $2);
} elsif ($1 =~ /^\s*$/) {
$self->history('list');
} else {
$self->help('set');
} # if
} elsif ($line =~ /^\s*source\s+(\S+)/i) {
- $result = $self->source ($1);
+ $result = $self->source($1);
} elsif ($line =~ /^\s*vars\s*/) {
- $self->vars ($line);
+ $self->vars($line);
} elsif ($line =~ /^\s*color\s*(.*)/i) {
if ($1 =~ /(1|on)/i) {
$opts{color} = 1;
- delete $ENV{ANSI_COLORS_DISABLED} if $ENV{ANSI_COLORS_DISABLED};
- $self->set_prompt;
+ delete $ENV{ANSI_COLORS_DISABLED}
+ if $ENV{ANSI_COLORS_DISABLED};
} elsif ($1 =~ /(0|off)/i) {
- $opts{color} = 0;
- $ENV{ANSI_COLORS_DISABLED} = 1;
- $self->set_prompt;
+ $opts{trace} = 0;
+ local $ENV{ANSI_COLORS_DISABLED} = 1;
} elsif ($1 =~ /\s*$/) {
if ($ENV{ANSI_COLORS_DISABLED}) {
display 'Color is currently off';
return ($cmd, $line, $result);
} # _builtinCmds
-sub _interrupt () {
+sub _interrupt() {
# Announce that we have hit an interrupt
- print color('yellow') . "<Control-C>\n" . color('reset');
+ print color ('yellow') . "<Control-C>\n" . color ('reset');
# Free up all of the line state info
$_cmdline->free_line_state;
# scalar" internal errors from perl)
my @Matches;
- push @Matches, $_ for (@$matches);
+ push @Matches, $_ for (@$matches);
my $match = shift @Matches;
unshift @newMatches, $match;
- $_cmdline->display_match_list(\@newMatches);
+ $_cmdline->display_match_list (\@newMatches);
$_cmdline->on_new_line;
$_cmdline->redisplay;
=cut
- my $self = bless {
- histfile => $histfile,
- }, $class;
-
my $me = get_me;
$histfile ||= "$ENV{HOME}/.${me}_hist";
+ my $self = bless {
+ histfile => $histfile,
+ }, $class;
+
error "Creating bogus .${me}_hist file!"
if $me eq '-' or $me eq '';
} # unless
# Instantiate a commandline
- $_cmdline = Term::ReadLine->new($me);
+ $_cmdline = Term::ReadLine->new ($me);
# Store the function pointer of what to call when sourcing a file or
# evaluating an expression.
} # if
} # if
- $self->{promptColor} = $promptColor;
- $self->{inputColor} = $inputColor;
- $self->{resetColor} = $resetColor;
-
# Default prompt is "$me:"
- $self->set_prompt("$me:");
+ $self->{prompt} = "$me:";
# Set commands
$self->set_cmds(%cmds);
# Set some ornamentation
- $_cmdline->ornaments('e,,u') unless $Config{cppflags} =~ /win32/i;
+ $_cmdline->ornaments ('s,e,u,') unless $Config{cppflags} =~ /win32/i;
# Read in history
$self->set_histfile($histfile);
if ($Config{cppflags} =~ /win32/i) {
$opts{trace} = 0;
- $ENV{ANSI_COLORS_DISABLED} = 1;
+ local $ENV{ANSI_COLORS_DISABLED} = 1;
} # if
return $self;
$prompt =~ s/\\\#/$self->{cmdnbr}/g;
- # Now color it if color is on
- $prompt = "$self->{resetColor}$self->{promptColor}$prompt$self->{resetColor}$self->{inputColor}" if $self->{promptColor};
-
use POSIX;
# Term::ReadLine::Gnu restarts whatever system call it is using, such that
$oldaction = POSIX::SigAction->new;
# Set up our unsafe signal handler
- POSIX::sigaction(&POSIX::SIGINT, $sigaction, $oldaction);
+ POSIX::sigaction (&POSIX::SIGINT, $sigaction, $oldaction);
} # if
- $line = $_cmdline->readline($prompt);
-
- display_nolf $resetColor;
+ $line = $_cmdline->readline ($prompt);
# Restore the old signal handler
if ($Config{cppflags} !~ /win32/i) {
- POSIX::sigaction(&POSIX::SIGINT, $oldaction);
+ POSIX::sigaction (&POSIX::SIGINT, $oldaction);
} # if
- $line = $self->_interpolate($line) if $line;
+ $line = $self->_interpolate($line)
+ if $line;
$self->{cmdnbr}++ unless $self->{sourcing};
$line = '' unless $cmd;
} while ($cmd and $builtin_cmds{$cmd});
- if (wantarray) {
- return ($line, $result);
- } else {
- return $result || $line;
- } # if
+ return ($line, $result);
} # get
-sub set_cmds (%) {
+sub set_cmds(%) {
my ($self, %cmds) = @_;
=pod
return;
} # set_cmds
-sub set_prompt ($) {
+sub set_prompt($) {
my ($self, $prompt) = @_;
=pod
=cut
- my $oldPrompt = $self->{prompt};
+ my $return = $self->{prompt};
- $self->{prompt} = $prompt if $prompt;
+ $self->{prompt} = $prompt;
- if ($opts{color}) {
- $self->{promptColor} = $promptColor;
- $self->{resetColor} = $resetColor;
- } else {
- undef $self->{promptColor};
- undef $self->{resetColor};
- } # if
-
- return $oldPrompt;
+ return $return;
} # set_prompt
sub set_histfile($) {
$_cmdline->clear_history;
# Now read histfile
- $_cmdline->ReadHistory($histfile);
+ $_cmdline->ReadHistory ($histfile);
} # if
# Determine the number of lines in the history file
- open my $hist, '<', $histfile;
+ open my $hist, '<', $histfile
+ or croak "Unable to open history file $histfile";
# Set cmdnbr
for (<$hist>) {}
return $returnEval;
} # set_eval
-sub help(;$) {
- my ($self, $cmd) = @_;
+sub help(;$$) {
+ my ($self, $cmd, $builtins) = @_;
=pod
my @help;
+ $builtins ||= 0;
+
$cmd ||= '';
$cmd =~ s/^\s+//;
$cmd =~ s/\s+$//;
if (/$searchStr/i) {
$helpFound = 1;
- my $cmdcolor = $builtin_cmds{$_} ? color('cyan') : color ('magenta');
- my $boldOn = $builtin_cmds{$_} ? color('white on_cyan') : color ('white on_magenta');
- my $boldOff = color('reset') . $cmdcolor;
+ my $cmdcolor = $builtin_cmds{$_} ? color('cyan') : color('magenta');
- $cmd = "$cmdcolor$_";
- $cmd =~ s/($searchStr)/$boldOn$1$boldOff/g;
- $cmd .= " $_cmds{$_}{parms}" if $_cmds{$_}{parms};
- $cmd .= color ('reset');
- $cmd .= " - $_cmds{$_}{help}" if $_cmds{$_}{help};
+ $cmd = "$cmdcolor$_";
+ $cmd .= " $_cmds{$_}{parms}" if $_cmds{$_}{parms};
+ $cmd .= color('reset');
+ $cmd .= " - $_cmds{$_}{help}" if $_cmds{$_}{help};
push @help, $cmd;
} # if
} else {
for (sort keys %_cmds) {
+ next if $builtin_cmds{$_} and not $builtins;
+
my $cmdcolor = $builtin_cmds{$_} ? color ('cyan') : color ('magenta');
my $cmd = "$cmdcolor$_";
$start = $_[3];
$end = $_[4];
} elsif ($action eq 'redo') {
- $_cmdline->remove_history($_cmdline->where_history);
+ $_cmdline->remove_history ($_cmdline->where_history);
my $nbr = $_[2];
- my $line = $_cmdline->history_get($nbr);
+ my $line = $_cmdline->history_get ($nbr);
- $_cmdline->add_history($line);
+ $_cmdline->add_history ($line);
display $line;
my ($cmd, $result) = $self->_builtinCmds($line);
=pod
-=head2 _get($name)
+=head2 _get ($name)
This method gets a variable to a value stored in the CmdLine
object.
my $returnValue = $self->{vars}{$name};
- if ($value) {
+ if (defined $value) {
$value = $self->_interpolate($value);
# Do not call eval if we are setting result - otherwise we recurse
# infinitely.
unless ($name eq 'result') {
no strict;
- $value = $self->{eval}($value)
- if $self->{eval};
+ $value = $self->{eval}($value) if $self->{eval};
use strict;
} # unless
=pod
-=head2 vars($name)
+=head2 vars ($name)
This method will print out all known variables
for (keys %{$self->{vars}});
$self->handleOutput($cmd, @output);
+
+ return;
} # vars
sub handleOutput($@) {
local $SIG{PIPE} = 'IGNORE';
- open $pipe, '|', $pipeToCmd or undef $pipe;
+ open $pipe, '|', $pipeToCmd
+ or undef $pipe;
# TODO: Not handling the output here. Need open2 and then recursively call
# handleOutput.
if ($pipe) {
- print $pipe "$_\n" for (@output);
+ print $pipe "$_\n"
+ for (@output);
- close $pipe or error "Unable to close pipe for $pipeToCmd - $!";
+ close $pipe
+ or error "Unable to close pipe for $pipeToCmd - $!";
} else {
error "Unable to open pipe for $pipeToCmd - $!";
} # if
open my $output, '>', $outToFile;
if ($output) {
- print $output "$_\n" for (@output);
+ print $output "$_\n"
+ for (@output);
close $output;
$_ = $self->_interpolate ($_);
# Check to see if it's a builtin
- my ($cmd, $line, $result) = $self->_builtinCmds($_);
+ my ($cmd, $line, $result) = $self->_builtinCmds ($_);
next if $builtin_cmds{$cmd};