-# Net::Cmd.pm
+# Net::Cmd.pm $Id: //depot/libnet/Net/Cmd.pm#28 $
#
# Copyright (c) 1995-1997 Graham Barr <gbarr@pobox.com>. All rights reserved.
# This program is free software; you can redistribute it and/or
use strict;
use vars qw(@ISA @EXPORT $VERSION);
use Carp;
+use Symbol 'gensym';
-$VERSION = "2.18";
+BEGIN {
+ if ($^O eq 'os390') {
+ require Convert::EBCDIC;
+# Convert::EBCDIC->import;
+ }
+}
+
+$VERSION = "2.21";
@ISA = qw(Exporter);
@EXPORT = qw(CMD_INFO CMD_OK CMD_MORE CMD_REJECT CMD_ERROR CMD_PENDING);
my %debug = ();
+my $tr = $^O eq 'os390' ? Convert::EBCDIC->new() : undef;
+
+sub toebcdic
+{
+ my $cmd = shift;
+
+ unless (exists ${*$cmd}{'net_cmd_asciipeer'})
+ {
+ my $string = $_[0];
+ my $ebcdicstr = $tr->toebcdic($string);
+ ${*$cmd}{'net_cmd_asciipeer'} = $string !~ /^\d+/ && $ebcdicstr =~ /^\d+/;
+ }
+
+ ${*$cmd}{'net_cmd_asciipeer'}
+ ? $tr->toebcdic($_[0])
+ : $_[0];
+}
+
+sub toascii
+{
+ my $cmd = shift;
+ ${*$cmd}{'net_cmd_asciipeer'}
+ ? $tr->toascii($_[0])
+ : $_[0];
+}
+
sub _print_isa
{
no strict qw(refs);
{
my $cmd = shift;
- return $cmd unless defined fileno($cmd);
-
+ unless (defined fileno($cmd))
+ {
+ $cmd->set_status("599", "Connection closed");
+ return $cmd;
+ }
+
+
$cmd->dataend()
if(exists ${*$cmd}{'net_cmd_lastch'});
if (scalar(@_))
{
- local $SIG{PIPE} = 'IGNORE';
+ local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
+
+ my $str = join(" ", map { /\n/ ? do { my $n = $_; $n =~ tr/\n/ /; $n } : $_; } @_);
+ $str = $cmd->toascii($str) if $tr;
+ $str .= "\015\012";
- my $str = join(" ", map { /\n/ ? do { my $n = $_; $n =~ tr/\n/ /; $n } : $_; } @_) . "\015\012";
my $len = length $str;
my $swlen;
-
+
$cmd->close
unless (defined($swlen = syswrite($cmd,$str,$len)) && $swlen == $len);
my $partial = defined(${*$cmd}{'net_cmd_partial'})
? ${*$cmd}{'net_cmd_partial'} : "";
my $fd = fileno($cmd);
-
+
return undef
unless defined $fd;
${*$cmd}{'net_cmd_partial'} = $partial;
+ if ($tr)
+ {
+ foreach my $ln (@{${*$cmd}{'net_cmd_lines'}})
+ {
+ $ln = $cmd->toebcdic($ln);
+ }
+ }
+
shift @{${*$cmd}{'net_cmd_lines'}};
}
print STDERR $b,join("\n$b",split(/\n/,$line)),"\n";
}
- $line =~ s/\n/\015\012/sgo;
+ # Translate LF => CRLF, but not if the LF is
+ # already preceeded by a CR
+ $line =~ s/\G()\n|([^\r\n])\n/$+\015\012/sgo;
${*$cmd}{'net_cmd_lastch'} ||= " ";
$line = ${*$cmd}{'net_cmd_lastch'} . $line;
$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 (undef,$len,$offset) = @_;
+ 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__
=head1 SYNOPSIS
use Net::Cmd;
-
+
@ISA = qw(Net::Cmd);
=head1 DESCRIPTION
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
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 commmand 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
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#28 $>
+
=cut