Upgrade to PathTools 3.25
[p5sagit/p5-mst-13.2.git] / lib / Net / Cmd.pm
index 40510e5..aa1a193 100644 (file)
@@ -1,6 +1,6 @@
-# Net::Cmd.pm $Id: //depot/libnet/Net/Cmd.pm#26 $
+# Net::Cmd.pm $Id: //depot/libnet/Net/Cmd.pm#34 $
 #
-# Copyright (c) 1995-1997 Graham Barr <gbarr@pobox.com>. All rights reserved.
+# Copyright (c) 1995-2006 Graham Barr <gbarr@pobox.com>. All rights reserved.
 # This program is free software; you can redistribute it and/or
 # modify it under the same terms as Perl itself.
 
@@ -12,6 +12,7 @@ require Exporter;
 use strict;
 use vars qw(@ISA @EXPORT $VERSION);
 use Carp;
+use Symbol 'gensym';
 
 BEGIN {
   if ($^O eq 'os390') {
@@ -20,7 +21,7 @@ BEGIN {
   }
 }
 
-$VERSION = "2.20";
+$VERSION = "2.27_01";
 @ISA     = qw(Exporter);
 @EXPORT  = qw(CMD_INFO CMD_OK CMD_MORE CMD_REJECT CMD_ERROR CMD_PENDING);
 
@@ -72,7 +73,6 @@ sub _print_isa
  my @do   = ($pkg);
  my %spc = ( $pkg , "");
 
- print STDERR "\n";
  while ($pkg = shift @do)
   {
    next if defined $done{$pkg};
@@ -84,7 +84,7 @@ sub _print_isa
                 : "";
 
    my $spc = $spc{$pkg};
-   print STDERR "$cmd: ${spc}${pkg}${v}\n";
+   $cmd->debug_print(1,"${spc}${pkg}${v}\n");
 
    if(@{"${pkg}::ISA"})
     {
@@ -92,8 +92,6 @@ sub _print_isa
      unshift(@do, @{"${pkg}::ISA"});
     }
   }
-
- print STDERR "\n";
 }
 
 sub debug
@@ -200,7 +198,7 @@ sub command
 
 
  $cmd->dataend()
-    if(exists ${*$cmd}{'net_cmd_lastch'});
+    if(exists ${*$cmd}{'net_cmd_last_ch'});
 
  if (scalar(@_))
   {
@@ -268,7 +266,9 @@ sub getline
   {
    my $timeout = $cmd->timeout || undef;
    my $rout;
-   if (select($rout=$rin, undef, undef, $timeout))
+
+   my $select_ret = select($rout=$rin, undef, undef, $timeout);
+   if ($select_ret > 0)
     {
      unless (sysread($cmd, $buf="", 1024))
       {
@@ -289,7 +289,8 @@ sub getline
     }
    else
     {
-     carp("$cmd: Timeout") if($cmd->debug);
+     my $msg = $select_ret ? "Error or Interrupted: $!" : "Timeout";
+     carp("$cmd: $msg") if($cmd->debug);
      return undef;
     }
   }
@@ -394,36 +395,47 @@ sub datasend
 
  return 0 unless defined(fileno($cmd));
 
- return 1
-    unless length($line);
+ my $last_ch = ${*$cmd}{'net_cmd_last_ch'};
+ $last_ch = ${*$cmd}{'net_cmd_last_ch'} = "\012" unless defined $last_ch;
 
- if($cmd->debug)
-  {
-   my $b = "$cmd>>> ";
-   print STDERR $b,join("\n$b",split(/\n/,$line)),"\n";
+ return 1 unless length $line;
+
+ if($cmd->debug) {
+   foreach my $b (split(/\n/,$line)) {
+     $cmd->debug_print(1, "$b\n");
+   }
   }
 
- # Translate LF => CRLF, but not if the LF is
- # already preceeded by a CR
- $line =~ s/\G()\n|([^\r\n])\n/$+\015\012/sgo;
+ $line =~ tr/\r\n/\015\012/ unless "\r" eq "\015";
 
- ${*$cmd}{'net_cmd_lastch'} ||= " ";
- $line = ${*$cmd}{'net_cmd_lastch'} . $line;
+  my $first_ch = '';
 
- $line =~ s/(\012\.)/$1./sog;
+  if ($last_ch eq "\015") {
+    $first_ch = "\012" if $line =~ s/^\012//;
+  }
+  elsif ($last_ch eq "\012") {
+    $first_ch = "." if $line =~ /^\./;
+  }
 
- ${*$cmd}{'net_cmd_lastch'} = substr($line,-1,1);
+ $line =~ s/\015?\012(\.?)/\015\012$1$1/sg;
 
- my $len = length($line) - 1;
- my $offset = 1;
+ substr($line,0,0) = $first_ch;
+
+ ${*$cmd}{'net_cmd_last_ch'} = substr($line,-1,1);
+
+ my $len = length($line);
+ my $offset = 0;
  my $win = "";
  vec($win,fileno($cmd),1) = 1;
  my $timeout = $cmd->timeout || undef;
 
+ local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
+
  while($len)
   {
    my $wout;
-   if (select(undef,$wout=$win, undef, $timeout) > 0)
+   my $s = select(undef,$wout=$win, undef, $timeout);
+   if ((defined $s and $s > 0) or -f $cmd) # -f for testing on win32
     {
      my $w = syswrite($cmd, $line, $len, $offset);
      unless (defined($w))
@@ -444,38 +456,148 @@ sub datasend
  1;
 }
 
-sub dataend
+sub rawdatasend
 {
  my $cmd = shift;
+ my $arr = @_ == 1 && ref($_[0]) ? $_[0] : \@_;
+ my $line = join("" ,@$arr);
 
  return 0 unless defined(fileno($cmd));
 
  return 1
-    unless(exists ${*$cmd}{'net_cmd_lastch'});
+    unless length($line);
 
- if(${*$cmd}{'net_cmd_lastch'} eq "\015")
+ if($cmd->debug)
   {
-   syswrite($cmd,"\012",1);
-   print STDERR "\n"
-    if($cmd->debug);
+   my $b = "$cmd>>> ";
+   print STDERR $b,join("\n$b",split(/\n/,$line)),"\n";
   }
- elsif(${*$cmd}{'net_cmd_lastch'} ne "\012")
+
+ my $len = length($line);
+ my $offset = 0;
+ my $win = "";
+ vec($win,fileno($cmd),1) = 1;
+ my $timeout = $cmd->timeout || undef;
+
+ local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
+ while($len)
   {
-   syswrite($cmd,"\015\012",2);
-   print STDERR "\n"
-    if($cmd->debug);
+   my $wout;
+   if (select(undef,$wout=$win, undef, $timeout) > 0)
+    {
+     my $w = syswrite($cmd, $line, $len, $offset);
+     unless (defined($w))
+      {
+       carp("$cmd: $!") if $cmd->debug;
+       return undef;
+      }
+     $len -= $w;
+     $offset += $w;
+    }
+   else
+    {
+     carp("$cmd: Timeout") if($cmd->debug);
+     return undef;
+    }
   }
 
- print STDERR "$cmd>>> .\n"
+ 1;
+}
+
+sub dataend
+{
+ my $cmd = shift;
+
+ return 0 unless defined(fileno($cmd));
+
+ my $ch = ${*$cmd}{'net_cmd_last_ch'};
+ my $tosend;
+
+ if (!defined $ch) {
+   return 1;
+ }
+ elsif ($ch ne "\012") {
+   $tosend = "\015\012";
+ }
+
+ $tosend .= ".\015\012";
+
+ local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
+
+ $cmd->debug_print(1, ".\n")
     if($cmd->debug);
 
- syswrite($cmd,".\015\012",3);
+ syswrite($cmd,$tosend, length $tosend);
 
- delete ${*$cmd}{'net_cmd_lastch'};
+ delete ${*$cmd}{'net_cmd_last_ch'};
 
  $cmd->response() == CMD_OK;
 }
 
+# read and write to tied filehandle
+sub tied_fh {
+  my $cmd = shift;
+  ${*$cmd}{'net_cmd_readbuf'} = '';
+  my $fh = gensym();
+  tie *$fh,ref($cmd),$cmd;
+  return $fh;
+}
+
+# tie to myself
+sub TIEHANDLE {
+  my $class = shift;
+  my $cmd = shift;
+  return $cmd;
+}
+
+# Tied filehandle read.  Reads requested data length, returning
+# end-of-file when the dot is encountered.
+sub READ {
+  my $cmd = shift;
+  my ($len,$offset) = @_[1,2];
+  return unless exists ${*$cmd}{'net_cmd_readbuf'};
+  my $done = 0;
+  while (!$done and length(${*$cmd}{'net_cmd_readbuf'}) < $len) {
+     ${*$cmd}{'net_cmd_readbuf'} .= $cmd->getline() or return;
+     $done++ if ${*$cmd}{'net_cmd_readbuf'} =~ s/^\.\r?\n\Z//m;
+  }
+
+  $_[0] = '';
+  substr($_[0],$offset+0) = substr(${*$cmd}{'net_cmd_readbuf'},0,$len);
+  substr(${*$cmd}{'net_cmd_readbuf'},0,$len) = '';
+  delete ${*$cmd}{'net_cmd_readbuf'} if $done;
+
+  return length $_[0];
+}
+
+sub READLINE {
+  my $cmd = shift;
+  # in this context, we use the presence of readbuf to
+  # indicate that we have not yet reached the eof
+  return unless exists ${*$cmd}{'net_cmd_readbuf'};
+  my $line = $cmd->getline;
+  return if $line =~ /^\.\r?\n/;
+  $line;
+}
+
+sub PRINT {
+  my $cmd = shift;
+  my ($buf,$len,$offset) = @_;
+  $len    ||= length ($buf);
+  $offset += 0;
+  return unless $cmd->datasend(substr($buf,$offset,$len));
+  ${*$cmd}{'net_cmd_sending'}++;  # flag that we should call dataend()
+  return $len;
+}
+
+sub CLOSE {
+  my $cmd = shift;
+  my $r = exists(${*$cmd}{'net_cmd_sending'}) ? $cmd->dataend : 1; 
+  delete ${*$cmd}{'net_cmd_readbuf'};
+  delete ${*$cmd}{'net_cmd_sending'};
+  $r;
+}
+
 1;
 
 __END__
@@ -509,10 +631,8 @@ Set the level of debug information for this object. If C<VALUE> is not given
 then the current state is returned. Otherwise the state is changed to 
 C<VALUE> and the previous state returned. 
 
-Set the level of debug information for this object. If no argument is
-given then the current state is returned. Otherwise the state is
-changed to C<$value>and the previous state returned.  Different packages
-may implement different levels of debug but, a  non-zero value result in
+Different packages
+may implement different levels of debug but a non-zero value results in 
 copies of all commands and responses also being sent to STDERR.
 
 If C<VALUE> is C<undef> then the debug level will be set to the default
@@ -614,6 +734,11 @@ some C<debug_print> calls into your method.
 
 Unget a line of text from the server.
 
+=item rawdatasend ( DATA )
+
+Send data to the remote server without performing any conversions. C<DATA>
+is a scalar.
+
 =item read_until_dot ()
 
 Read data from the remote server until a line consisting of a single '.'.
@@ -621,12 +746,22 @@ Any lines starting with '..' will have one of the '.'s removed.
 
 Returns a reference to a list containing the lines, or I<undef> upon failure.
 
+=item tied_fh ()
+
+Returns a filehandle tied to the Net::Cmd object.  After issuing a
+command, you may read from this filehandle using read() or <>.  The
+filehandle will return EOF when the final dot is encountered.
+Similarly, you may write to the filehandle in order to send data to
+the server after issuing a command that expects data to be written.
+
+See the Net::POP3 and Net::SMTP modules for examples of this.
+
 =back
 
 =head1 EXPORTS
 
 C<Net::Cmd> exports six subroutines, five of these, C<CMD_INFO>, C<CMD_OK>,
-C<CMD_MORE>, C<CMD_REJECT> and C<CMD_ERROR> ,correspond to possible results
+C<CMD_MORE>, C<CMD_REJECT> and C<CMD_ERROR>, correspond to possible results
 of C<response> and C<status>. The sixth is C<CMD_PENDING>.
 
 =head1 AUTHOR
@@ -635,12 +770,8 @@ Graham Barr <gbarr@pobox.com>
 
 =head1 COPYRIGHT
 
-Copyright (c) 1995-1997 Graham Barr. All rights reserved.
+Copyright (c) 1995-2006 Graham Barr. All rights reserved.
 This program is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself.
 
-=for html <hr>
-
-I<$Id: //depot/libnet/Net/Cmd.pm#26 $>
-
 =cut