This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
don't bother testing if we can flush all handles when fflush(stdin)
[perl5.git] / lib / Term / ReadLine.pm
index a52b558..8bb8205 100644 (file)
@@ -105,14 +105,33 @@ support reacher set of commands.
 All these commands are callable via method interface and have names
 which conform to standard conventions with the leading C<rl_> stripped.
 
-The stub package included with the perl distribution allows two
-additional methods: C<tkRunning> and C<ornaments>.  The first one
+The stub package included with the perl distribution allows some
+additional methods: 
+
+=over 12
+
+=item C<tkRunning>
+
 makes Tk event loop run when waiting for user input (i.e., during
-C<readline> method), the second one makes the command line stand out
-by using termcap data.  The argument to C<ornaments> should be 0, 1,
-or a string of a form "aa,bb,cc,dd".  Four components of this string
-should be names of I<terminal capacities>, first two will be issued to
-make the prompt standout, last two to make the input line standout.
+C<readline> method).
+
+=item C<ornaments>
+
+makes the command line stand out by using termcap data.  The argument
+to C<ornaments> should be 0, 1, or a string of a form
+C<"aa,bb,cc,dd">.  Four components of this string should be names of
+I<terminal capacities>, first two will be issued to make the prompt
+standout, last two to make the input line standout.
+
+=item C<newTTY>
+
+takes two arguments which are input filehandle and output filehandle.
+Switches to use these filehandles.
+
+=back
+
+One can check whether the currently loaded ReadLine package supports
+these methods by checking for corresponding C<Features>.
 
 =head1 EXPORTS
 
@@ -120,12 +139,23 @@ None
 
 =head1 ENVIRONMENT
 
-The variable C<PERL_RL> governs which ReadLine clone is loaded. If the
-value is false, a dummy interface is used. If the value is true, it
-should be tail of the name of the package to use, such as C<Perl> or
-C<Gnu>. 
+The environment variable C<PERL_RL> governs which ReadLine clone is
+loaded. If the value is false, a dummy interface is used. If the value
+is true, it should be tail of the name of the package to use, such as
+C<Perl> or C<Gnu>.  
+
+As a special case, if the value of this variable is space-separated,
+the tail might be used to disable the ornaments by setting the tail to
+be C<o=0> or C<ornaments=0>.  The head should be as described above, say
+
+If the variable is not set, or if the head of space-separated list is
+empty, the best available package is loaded.
 
-If the variable is not set, the best available package is loaded.
+  export "PERL_RL=Perl o=0"    # Use Perl ReadLine without ornaments
+  export "PERL_RL= o=0"                # Use best available ReadLine without ornaments
+
+(Note that processing of C<PERL_RL> for ornaments is in the discretion of the 
+particular used C<Term::ReadLine::*> package).
 
 =cut
 
@@ -157,13 +187,13 @@ sub findConsole {
 
     if (-e "/dev/tty") {
        $console = "/dev/tty";
-    } elsif (-e "con") {
+    } elsif (-e "con" or $^O eq 'MSWin32') {
        $console = "con";
     } else {
        $console = "sys\$command";
     }
 
-    if ($^O eq 'amigaos') {
+    if (($^O eq 'amigaos') || ($^O eq 'beos') || ($^O eq 'epoc')) {
        $console = undef;
     }
     elsif ($^O eq 'os2') {
@@ -186,7 +216,7 @@ sub new {
   die "method new called with wrong number of arguments" 
     unless @_==2 or @_==4;
   #local (*FIN, *FOUT);
-  my ($FIN, $FOUT);
+  my ($FIN, $FOUT, $ret);
   if (@_==2) {
     ($console, $consoleOUT) = findConsole;
 
@@ -196,27 +226,43 @@ sub new {
     $sel = select(FOUT);
     $| = 1;                            # for DB::OUT
     select($sel);
-    bless [\*FIN, \*FOUT];
+    $ret = bless [\*FIN, \*FOUT];
   } else {                     # Filehandles supplied
     $FIN = $_[2]; $FOUT = $_[3];
     #OUT->autoflush(1);                # Conflicts with debugger?
     $sel = select($FOUT);
     $| = 1;                            # for DB::OUT
     select($sel);
-    bless [$FIN, $FOUT];
+    $ret = bless [$FIN, $FOUT];
+  }
+  if ($ret->Features->{ornaments} 
+      and not ($ENV{PERL_RL} and $ENV{PERL_RL} =~ /\bo\w*=0/)) {
+    local $Term::ReadLine::termcap_nowarn = 1;
+    $ret->ornaments(1);
   }
+  return $ret;
 }
+
+sub newTTY {
+  my ($self, $in, $out) = @_;
+  $self->[0] = $in;
+  $self->[1] = $out;
+  my $sel = select($out);
+  $| = 1;                              # for DB::OUT
+  select($sel);
+}
+
 sub IN { shift->[0] }
 sub OUT { shift->[1] }
 sub MinLine { undef }
 sub Attribs { {} }
 
-my %features = (tkRunning => 1, ornaments => 1);
+my %features = (tkRunning => 1, ornaments => 1, 'newTTY' => 1);
 sub Features { \%features }
 
 package Term::ReadLine;                # So late to allow the above code be defined?
 
-my $which = $ENV{PERL_RL};
+my ($which) = exists $ENV{PERL_RL} ? split /\s+/, $ENV{PERL_RL} : undef;
 if ($which) {
   if ($which =~ /\bgnu\b/i){
     eval "use Term::ReadLine::Gnu;";
@@ -225,7 +271,7 @@ if ($which) {
   } else {
     eval "use Term::ReadLine::$which;";
   }
-} elsif (defined $which) {     # Defined but false
+} elsif (defined $which and $which ne '') {    # Defined but false
   # Do nothing fancy
 } else {
   eval "use Term::ReadLine::Gnu; 1" or eval "use Term::ReadLine::Perl; 1";
@@ -264,10 +310,14 @@ sub ornaments {
   return $rl_term_set unless @_;
   $rl_term_set = shift;
   $rl_term_set ||= ',,,';
-  $rl_term_set = 'us,ue,md,me' if $rl_term_set == 1;
+  $rl_term_set = 'us,ue,md,me' if $rl_term_set eq '1';
   my @ts = split /,/, $rl_term_set, 4;
   eval { LoadTermCap };
-  warn("Cannot find termcap: $@\n"), return unless defined $terminal;
+  unless (defined $terminal) {
+    warn("Cannot find termcap: $@\n") unless $Term::ReadLine::termcap_nowarn;
+    $rl_term_set = ',,,';
+    return;
+  }
   @rl_term_set = map {$_ ? $terminal->Tputs($_,1) || '' : ''} @ts;
   return $rl_term_set;
 }
@@ -307,6 +357,7 @@ sub get_line {
   my $self = shift;
   $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;
   my $in = $self->IN;
+  local ($/) = "\n";
   return scalar <$in>;
 }