Fix a2p manpage (from Debian)
[p5sagit/p5-mst-13.2.git] / lib / Net / Cmd.pm
index f89914e..a84b4da 100644 (file)
@@ -1,4 +1,4 @@
-# Net::Cmd.pm $Id: //depot/libnet/Net/Cmd.pm#30 $
+# Net::Cmd.pm $Id: //depot/libnet/Net/Cmd.pm#34 $
 #
 # Copyright (c) 1995-1997 Graham Barr <gbarr@pobox.com>. All rights reserved.
 # This program is free software; you can redistribute it and/or
@@ -21,7 +21,7 @@ BEGIN {
   }
 }
 
-$VERSION = "2.23";
+$VERSION = "2.26";
 @ISA     = qw(Exporter);
 @EXPORT  = qw(CMD_INFO CMD_OK CMD_MORE CMD_REJECT CMD_ERROR CMD_PENDING);
 
@@ -198,7 +198,7 @@ sub command
 
 
  $cmd->dataend()
-    if(exists ${*$cmd}{'net_cmd_lastch'});
+    if(exists ${*$cmd}{'net_cmd_last_ch'});
 
  if (scalar(@_))
   {
@@ -392,8 +392,10 @@ 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;
+
+ return 1 unless length $line;
 
  if($cmd->debug) {
    foreach my $b (split(/\n/,$line)) {
@@ -401,27 +403,35 @@ sub datasend
    }
   }
 
- # 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";
+
+  my $first_ch = '';
+
+  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'} ||= " ";
- $line = ${*$cmd}{'net_cmd_lastch'} . $line;
+ $line =~ s/\015?\012(\.?)/\015\012$1$1/sg;
 
- $line =~ s/(\012\.)/$1./sog;
+ substr($line,0,0) = $first_ch;
 
- ${*$cmd}{'net_cmd_lastch'} = substr($line,-1,1);
+ ${*$cmd}{'net_cmd_last_ch'} = substr($line,-1,1);
 
- my $len = length($line) - 1;
- my $offset = 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)
+   if (select(undef,$wout=$win, undef, $timeout) > 0 or -f $cmd) # -f for testing on win32
     {
      my $w = syswrite($cmd, $line, $len, $offset);
      unless (defined($w))
@@ -442,30 +452,80 @@ 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);
+   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);
+   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;
+    }
   }
 
+ 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;
 }
@@ -670,6 +730,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 '.'.
@@ -707,6 +772,6 @@ it under the same terms as Perl itself.
 
 =for html <hr>
 
-I<$Id: //depot/libnet/Net/Cmd.pm#30 $>
+I<$Id: //depot/libnet/Net/Cmd.pm#34 $>
 
 =cut