Scott,
I found a small bug in my patch this AM. Please accept the attached file
which fixes that and also removes the duplicate trailing "1;" ...
--
Lester
--- ShellUI-orig.pm 2010-12-03 20:13:35.000000000 -0500
+++ ShellUI.pm 2010-12-04 11:25:51.000000000 -0500
@@ -12,7 +12,7 @@
use Text::Shellwords::Cursor;
use vars qw($VERSION);
-$VERSION = '0.86';
+$VERSION = '0.87';
=head1 NAME
@@ -839,6 +839,8 @@
$self->{OUT} = $self->{term}->OUT || \*STDOUT;
$self->{prevcmd} = ""; # cmd to run again if user hits return
+ @{$self->{eof_exit_hooks}} = ();
+
return $self;
}
@@ -858,7 +860,7 @@
my $OUT = $self->{'OUT'};
my $rawline = "";
- for(;;) {
+ INPUT_LOOP: for(;;) {
my $prompt = $self->prompt();
$prompt = $prompt->[length $rawline ? 1 : 0] if ref $prompt eq 'ARRAY';
$prompt = $prompt->($self, $rawline) if ref $prompt eq 'CODE';
@@ -866,6 +868,14 @@
# EOF exits
unless(defined $newline) {
+ # If we have eof_exit_hooks let them have a say
+ if (scalar(@{$self->{eof_exit_hooks}})) {
+ foreach my $sub (@{$self->{eof_exit_hooks}}) {
+ if (&$sub()) {
+ next INPUT_LOOP;
+ }
+ }
+ }
print $OUT "\n";
$self->exit_requested(1);
return undef;
@@ -1047,6 +1057,30 @@
sub exit_requested { return shift->getset('done', shift); }
+=item add_eof_exit_hook(subroutine_reference)
+
+Call this method to add a subroutine as a hook into Term::ShellUI's
+"exit on EOF" (Ctrl-D) functionality. When a user enters Ctrl-D,
+Term::ShellUI will call each function in this hook list, in order,
+and will exit only if all of them return 0. The first function to
+return a non-zero value will stop further processing of these hooks
+and prevent the program from exiting.
+
+The return value of this method is the placement of the hook routine
+in the hook list (1 is first) or 0 (zero) on failure.
+
+=cut
+
+sub add_eof_exit_hook {
+ my $self=shift @_;
+ my $refcode=shift @_;
+ if (ref($refcode) eq 'CODE') {
+ push (@{$self->{eof_exit_hooks}}, $refcode);
+ return scalar @{$self->{eof_exit_hooks}};
+ }
+ return 0;
+}
+
=item get_cname(cname)
This is a tiny utility function that turns the cname (array ref
@@ -1064,13 +1098,15 @@
return join(" ", @$cname);
}
-
+=back
=head1 OVERRIDES
These are routines that probably already do the right thing.
If not, however, they are designed to be overridden.
+=over
+
=item blank_line()
This routine is called when the user inputs a blank line.
@@ -1112,7 +1148,7 @@
print STDERR @_;
}
-
+=back
=head1 WRITING A COMPLETION ROUTINE
@@ -1216,6 +1252,8 @@
The following are utility routines that your completion function
can call.
+=over
+
=item completemsg(msg)
your completion routine should call this to display text onscreen
@@ -1327,12 +1365,16 @@
return $results;
}
+=back
+
=head1 INTERNALS
These commands are internal to ShellUI.
They are documented here only for completeness -- you
should never need to call them.
+=over
+
=item get_deep_command
Looks up the supplied command line in a command hash.
@@ -2022,4 +2064,10 @@
=cut
+=head1 OTHER CONTRIBUTORS
+
+Lester Hightower E<lt>hightowe@cpan.orgE<gt>
+
+=cut
+
1;