PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Runtime/Tests/LinqDlrTests/testenv/perl/site/lib/net/Cmd.pm

#
Perl | 591 lines | 414 code | 172 blank | 5 comment | 57 complexity | af1c5ddf1d6f4f39e54ac2f2df41446e MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. # Net::Cmd.pm
  2. #
  3. # Copyright (c) 1995-1997 Graham Barr <gbarr@pobox.com>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6. package Net::Cmd;
  7. require 5.001;
  8. require Exporter;
  9. use strict;
  10. use vars qw(@ISA @EXPORT $VERSION);
  11. use Carp;
  12. $VERSION = "2.18";
  13. @ISA = qw(Exporter);
  14. @EXPORT = qw(CMD_INFO CMD_OK CMD_MORE CMD_REJECT CMD_ERROR CMD_PENDING);
  15. sub CMD_INFO { 1 }
  16. sub CMD_OK { 2 }
  17. sub CMD_MORE { 3 }
  18. sub CMD_REJECT { 4 }
  19. sub CMD_ERROR { 5 }
  20. sub CMD_PENDING { 0 }
  21. my %debug = ();
  22. sub _print_isa
  23. {
  24. no strict qw(refs);
  25. my $pkg = shift;
  26. my $cmd = $pkg;
  27. $debug{$pkg} ||= 0;
  28. my %done = ();
  29. my @do = ($pkg);
  30. my %spc = ( $pkg , "");
  31. print STDERR "\n";
  32. while ($pkg = shift @do)
  33. {
  34. next if defined $done{$pkg};
  35. $done{$pkg} = 1;
  36. my $v = defined ${"${pkg}::VERSION"}
  37. ? "(" . ${"${pkg}::VERSION"} . ")"
  38. : "";
  39. my $spc = $spc{$pkg};
  40. print STDERR "$cmd: ${spc}${pkg}${v}\n";
  41. if(@{"${pkg}::ISA"})
  42. {
  43. @spc{@{"${pkg}::ISA"}} = (" " . $spc{$pkg}) x @{"${pkg}::ISA"};
  44. unshift(@do, @{"${pkg}::ISA"});
  45. }
  46. }
  47. print STDERR "\n";
  48. }
  49. sub debug
  50. {
  51. @_ == 1 or @_ == 2 or croak 'usage: $obj->debug([LEVEL])';
  52. my($cmd,$level) = @_;
  53. my $pkg = ref($cmd) || $cmd;
  54. my $oldval = 0;
  55. if(ref($cmd))
  56. {
  57. $oldval = ${*$cmd}{'net_cmd_debug'} || 0;
  58. }
  59. else
  60. {
  61. $oldval = $debug{$pkg} || 0;
  62. }
  63. return $oldval
  64. unless @_ == 2;
  65. $level = $debug{$pkg} || 0
  66. unless defined $level;
  67. _print_isa($pkg)
  68. if($level && !exists $debug{$pkg});
  69. if(ref($cmd))
  70. {
  71. ${*$cmd}{'net_cmd_debug'} = $level;
  72. }
  73. else
  74. {
  75. $debug{$pkg} = $level;
  76. }
  77. $oldval;
  78. }
  79. sub message
  80. {
  81. @_ == 1 or croak 'usage: $obj->message()';
  82. my $cmd = shift;
  83. wantarray ? @{${*$cmd}{'net_cmd_resp'}}
  84. : join("", @{${*$cmd}{'net_cmd_resp'}});
  85. }
  86. sub debug_text { $_[2] }
  87. sub debug_print
  88. {
  89. my($cmd,$out,$text) = @_;
  90. print STDERR $cmd,($out ? '>>> ' : '<<< '), $cmd->debug_text($out,$text);
  91. }
  92. sub code
  93. {
  94. @_ == 1 or croak 'usage: $obj->code()';
  95. my $cmd = shift;
  96. ${*$cmd}{'net_cmd_code'} = "000"
  97. unless exists ${*$cmd}{'net_cmd_code'};
  98. ${*$cmd}{'net_cmd_code'};
  99. }
  100. sub status
  101. {
  102. @_ == 1 or croak 'usage: $obj->status()';
  103. my $cmd = shift;
  104. substr(${*$cmd}{'net_cmd_code'},0,1);
  105. }
  106. sub set_status
  107. {
  108. @_ == 3 or croak 'usage: $obj->set_status(CODE, MESSAGE)';
  109. my $cmd = shift;
  110. my($code,$resp) = @_;
  111. $resp = [ $resp ]
  112. unless ref($resp);
  113. (${*$cmd}{'net_cmd_code'},${*$cmd}{'net_cmd_resp'}) = ($code, $resp);
  114. 1;
  115. }
  116. sub command
  117. {
  118. my $cmd = shift;
  119. return $cmd unless defined fileno($cmd);
  120. $cmd->dataend()
  121. if(exists ${*$cmd}{'net_cmd_lastch'});
  122. if (scalar(@_))
  123. {
  124. local $SIG{PIPE} = 'IGNORE';
  125. my $str = join(" ", map { /\n/ ? do { my $n = $_; $n =~ tr/\n/ /; $n } : $_; } @_) . "\015\012";
  126. my $len = length $str;
  127. my $swlen;
  128. $cmd->close
  129. unless (defined($swlen = syswrite($cmd,$str,$len)) && $swlen == $len);
  130. $cmd->debug_print(1,$str)
  131. if($cmd->debug);
  132. ${*$cmd}{'net_cmd_resp'} = []; # the response
  133. ${*$cmd}{'net_cmd_code'} = "000"; # Made this one up :-)
  134. }
  135. $cmd;
  136. }
  137. sub ok
  138. {
  139. @_ == 1 or croak 'usage: $obj->ok()';
  140. my $code = $_[0]->code;
  141. 0 < $code && $code < 400;
  142. }
  143. sub unsupported
  144. {
  145. my $cmd = shift;
  146. ${*$cmd}{'net_cmd_resp'} = [ 'Unsupported command' ];
  147. ${*$cmd}{'net_cmd_code'} = 580;
  148. 0;
  149. }
  150. sub getline
  151. {
  152. my $cmd = shift;
  153. ${*$cmd}{'net_cmd_lines'} ||= [];
  154. return shift @{${*$cmd}{'net_cmd_lines'}}
  155. if scalar(@{${*$cmd}{'net_cmd_lines'}});
  156. my $partial = defined(${*$cmd}{'net_cmd_partial'})
  157. ? ${*$cmd}{'net_cmd_partial'} : "";
  158. my $fd = fileno($cmd);
  159. return undef
  160. unless defined $fd;
  161. my $rin = "";
  162. vec($rin,$fd,1) = 1;
  163. my $buf;
  164. until(scalar(@{${*$cmd}{'net_cmd_lines'}}))
  165. {
  166. my $timeout = $cmd->timeout || undef;
  167. my $rout;
  168. if (select($rout=$rin, undef, undef, $timeout))
  169. {
  170. unless (sysread($cmd, $buf="", 1024))
  171. {
  172. carp(ref($cmd) . ": Unexpected EOF on command channel")
  173. if $cmd->debug;
  174. $cmd->close;
  175. return undef;
  176. }
  177. substr($buf,0,0) = $partial; ## prepend from last sysread
  178. my @buf = split(/\015?\012/, $buf, -1); ## break into lines
  179. $partial = pop @buf;
  180. push(@{${*$cmd}{'net_cmd_lines'}}, map { "$_\n" } @buf);
  181. }
  182. else
  183. {
  184. carp("$cmd: Timeout") if($cmd->debug);
  185. return undef;
  186. }
  187. }
  188. ${*$cmd}{'net_cmd_partial'} = $partial;
  189. shift @{${*$cmd}{'net_cmd_lines'}};
  190. }
  191. sub ungetline
  192. {
  193. my($cmd,$str) = @_;
  194. ${*$cmd}{'net_cmd_lines'} ||= [];
  195. unshift(@{${*$cmd}{'net_cmd_lines'}}, $str);
  196. }
  197. sub parse_response
  198. {
  199. return ()
  200. unless $_[1] =~ s/^(\d\d\d)(.?)//o;
  201. ($1, $2 eq "-");
  202. }
  203. sub response
  204. {
  205. my $cmd = shift;
  206. my($code,$more) = (undef) x 2;
  207. ${*$cmd}{'net_cmd_resp'} ||= [];
  208. while(1)
  209. {
  210. my $str = $cmd->getline();
  211. return CMD_ERROR
  212. unless defined($str);
  213. $cmd->debug_print(0,$str)
  214. if ($cmd->debug);
  215. ($code,$more) = $cmd->parse_response($str);
  216. unless(defined $code)
  217. {
  218. $cmd->ungetline($str);
  219. last;
  220. }
  221. ${*$cmd}{'net_cmd_code'} = $code;
  222. push(@{${*$cmd}{'net_cmd_resp'}},$str);
  223. last unless($more);
  224. }
  225. substr($code,0,1);
  226. }
  227. sub read_until_dot
  228. {
  229. my $cmd = shift;
  230. my $fh = shift;
  231. my $arr = [];
  232. while(1)
  233. {
  234. my $str = $cmd->getline() or return undef;
  235. $cmd->debug_print(0,$str)
  236. if ($cmd->debug & 4);
  237. last if($str =~ /^\.\r?\n/o);
  238. $str =~ s/^\.\././o;
  239. if (defined $fh)
  240. {
  241. print $fh $str;
  242. }
  243. else
  244. {
  245. push(@$arr,$str);
  246. }
  247. }
  248. $arr;
  249. }
  250. sub datasend
  251. {
  252. my $cmd = shift;
  253. my $arr = @_ == 1 && ref($_[0]) ? $_[0] : \@_;
  254. my $line = join("" ,@$arr);
  255. return 0 unless defined(fileno($cmd));
  256. return 1
  257. unless length($line);
  258. if($cmd->debug)
  259. {
  260. my $b = "$cmd>>> ";
  261. print STDERR $b,join("\n$b",split(/\n/,$line)),"\n";
  262. }
  263. $line =~ s/\n/\015\012/sgo;
  264. ${*$cmd}{'net_cmd_lastch'} ||= " ";
  265. $line = ${*$cmd}{'net_cmd_lastch'} . $line;
  266. $line =~ s/(\012\.)/$1./sog;
  267. ${*$cmd}{'net_cmd_lastch'} = substr($line,-1,1);
  268. my $len = length($line) - 1;
  269. my $offset = 1;
  270. my $win = "";
  271. vec($win,fileno($cmd),1) = 1;
  272. my $timeout = $cmd->timeout || undef;
  273. while($len)
  274. {
  275. my $wout;
  276. if (select(undef,$wout=$win, undef, $timeout) > 0)
  277. {
  278. my $w = syswrite($cmd, $line, $len, $offset);
  279. unless (defined($w))
  280. {
  281. carp("$cmd: $!") if $cmd->debug;
  282. return undef;
  283. }
  284. $len -= $w;
  285. $offset += $w;
  286. }
  287. else
  288. {
  289. carp("$cmd: Timeout") if($cmd->debug);
  290. return undef;
  291. }
  292. }
  293. 1;
  294. }
  295. sub dataend
  296. {
  297. my $cmd = shift;
  298. return 0 unless defined(fileno($cmd));
  299. return 1
  300. unless(exists ${*$cmd}{'net_cmd_lastch'});
  301. if(${*$cmd}{'net_cmd_lastch'} eq "\015")
  302. {
  303. syswrite($cmd,"\012",1);
  304. print STDERR "\n"
  305. if($cmd->debug);
  306. }
  307. elsif(${*$cmd}{'net_cmd_lastch'} ne "\012")
  308. {
  309. syswrite($cmd,"\015\012",2);
  310. print STDERR "\n"
  311. if($cmd->debug);
  312. }
  313. print STDERR "$cmd>>> .\n"
  314. if($cmd->debug);
  315. syswrite($cmd,".\015\012",3);
  316. delete ${*$cmd}{'net_cmd_lastch'};
  317. $cmd->response() == CMD_OK;
  318. }
  319. 1;
  320. __END__
  321. =head1 NAME
  322. Net::Cmd - Network Command class (as used by FTP, SMTP etc)
  323. =head1 SYNOPSIS
  324. use Net::Cmd;
  325. @ISA = qw(Net::Cmd);
  326. =head1 DESCRIPTION
  327. C<Net::Cmd> is a collection of methods that can be inherited by a sub class
  328. of C<IO::Handle>. These methods implement the functionality required for a
  329. command based protocol, for example FTP and SMTP.
  330. =head1 USER METHODS
  331. These methods provide a user interface to the C<Net::Cmd> object.
  332. =over 4
  333. =item debug ( VALUE )
  334. Set the level of debug information for this object. If C<VALUE> is not given
  335. then the current state is returned. Otherwise the state is changed to
  336. C<VALUE> and the previous state returned.
  337. Set the level of debug information for this object. If no argument is
  338. given then the current state is returned. Otherwise the state is
  339. changed to C<$value>and the previous state returned. Different packages
  340. may implement different levels of debug but, a non-zero value result in
  341. copies of all commands and responses also being sent to STDERR.
  342. If C<VALUE> is C<undef> then the debug level will be set to the default
  343. debug level for the class.
  344. This method can also be called as a I<static> method to set/get the default
  345. debug level for a given class.
  346. =item message ()
  347. Returns the text message returned from the last command
  348. =item code ()
  349. Returns the 3-digit code from the last command. If a command is pending
  350. then the value 0 is returned
  351. =item ok ()
  352. Returns non-zero if the last code value was greater than zero and
  353. less than 400. This holds true for most command servers. Servers
  354. where this does not hold may override this method.
  355. =item status ()
  356. Returns the most significant digit of the current status code. If a command
  357. is pending then C<CMD_PENDING> is returned.
  358. =item datasend ( DATA )
  359. Send data to the remote server, converting LF to CRLF. Any line starting
  360. with a '.' will be prefixed with another '.'.
  361. C<DATA> may be an array or a reference to an array.
  362. =item dataend ()
  363. End the sending of data to the remote server. This is done by ensuring that
  364. the data already sent ends with CRLF then sending '.CRLF' to end the
  365. transmission. Once this data has been sent C<dataend> calls C<response> and
  366. returns true if C<response> returns CMD_OK.
  367. =back
  368. =head1 CLASS METHODS
  369. These methods are not intended to be called by the user, but used or
  370. over-ridden by a sub-class of C<Net::Cmd>
  371. =over 4
  372. =item debug_print ( DIR, TEXT )
  373. Print debugging information. C<DIR> denotes the direction I<true> being
  374. data being sent to the server. Calls C<debug_text> before printing to
  375. STDERR.
  376. =item debug_text ( TEXT )
  377. This method is called to print debugging information. TEXT is
  378. the text being sent. The method should return the text to be printed
  379. This is primarily meant for the use of modules such as FTP where passwords
  380. are sent, but we do not want to display them in the debugging information.
  381. =item command ( CMD [, ARGS, ... ])
  382. Send a command to the command server. All arguments a first joined with
  383. a space character and CRLF is appended, this string is then sent to the
  384. command server.
  385. Returns undef upon failure
  386. =item unsupported ()
  387. Sets the status code to 580 and the response text to 'Unsupported command'.
  388. Returns zero.
  389. =item response ()
  390. Obtain a response from the server. Upon success the most significant digit
  391. of the status code is returned. Upon failure, timeout etc., I<undef> is
  392. returned.
  393. =item parse_response ( TEXT )
  394. This method is called by C<response> as a method with one argument. It should
  395. return an array of 2 values, the 3-digit status code and a flag which is true
  396. when this is part of a multi-line response and this line is not the list.
  397. =item getline ()
  398. Retrieve one line, delimited by CRLF, from the remote server. Returns I<undef>
  399. upon failure.
  400. B<NOTE>: If you do use this method for any reason, please remember to add
  401. some C<debug_print> calls into your method.
  402. =item ungetline ( TEXT )
  403. Unget a line of text from the server.
  404. =item read_until_dot ()
  405. Read data from the remote server until a line consisting of a single '.'.
  406. Any lines starting with '..' will have one of the '.'s removed.
  407. Returns a reference to a list containing the lines, or I<undef> upon failure.
  408. =back
  409. =head1 EXPORTS
  410. C<Net::Cmd> exports six subroutines, five of these, C<CMD_INFO>, C<CMD_OK>,
  411. C<CMD_MORE>, C<CMD_REJECT> and C<CMD_ERROR> ,correspond to possible results
  412. of C<response> and C<status>. The sixth is C<CMD_PENDING>.
  413. =head1 AUTHOR
  414. Graham Barr <gbarr@pobox.com>
  415. =head1 COPYRIGHT
  416. Copyright (c) 1995-1997 Graham Barr. All rights reserved.
  417. This program is free software; you can redistribute it and/or modify
  418. it under the same terms as Perl itself.
  419. =cut