| 1 |
#!/usr/bin/env perl |
| 2 |
# |
| 3 |
# Copyright (c) 1997 Hiroo Hayashi. All Rights Reserved. |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or |
| 6 |
# modify it under the same terms as Perl itself. |
| 7 |
|
| 8 |
=head1 NAME |
| 9 |
|
| 10 |
pftp - an ftp client with the GNU Readline support |
| 11 |
|
| 12 |
=head1 SYNOPSIS |
| 13 |
|
| 14 |
B<pftp> [B<-u>] [B<-g>] [B<-M>] [B<-h>] [B<-d>] [I<host>] |
| 15 |
|
| 16 |
=head1 DESCRIPTION |
| 17 |
|
| 18 |
This is an ftp client which has the GNU Readline support. It can |
| 19 |
complete not only local file name but also remote file name and host |
| 20 |
name to which login. |
| 21 |
|
| 22 |
This is a sample program of Perl Term::ReadLine::Gnu module. |
| 23 |
|
| 24 |
=cut |
| 25 |
|
| 26 |
use Term::ReadLine; |
| 27 |
use strict; |
| 28 |
use warnings; |
| 29 |
use Net::Domain qw(hostdomain); # libnet |
| 30 |
use Net::FTP; # libnet-1.05 or later is recommended |
| 31 |
use File::Listing; # libwww (for parse_dir) |
| 32 |
use Getopt::Std; |
| 33 |
use Cwd; # for getcwd |
| 34 |
|
| 35 |
use vars qw($AUTOLOAD |
| 36 |
$opt_d $opt_u $opt_g $opt_M $opt_h); |
| 37 |
|
| 38 |
sub usage { |
| 39 |
print STDERR <<"EOM"; |
| 40 |
usage : $0 [-d] [-i] [-u] [-g] [-M] [-h] host |
| 41 |
-d : debug mode |
| 42 |
-i : interactive mode (not implemented) |
| 43 |
-u : disable autologin |
| 44 |
-g : turn off glob |
| 45 |
-M : show manual page |
| 46 |
-h : show this message |
| 47 |
EOM |
| 48 |
exit 0; |
| 49 |
} |
| 50 |
|
| 51 |
getopts('dugMh') or &usage; |
| 52 |
&man if $opt_M; |
| 53 |
&usage if $opt_h; |
| 54 |
|
| 55 |
# |
| 56 |
# setup Term::ReadLine::GNU |
| 57 |
# |
| 58 |
my $HOSTFILE = ($ENV{HOME} || (getpwuid($<))[7]) . "/.pftp_hosts"; |
| 59 |
|
| 60 |
my $term = Term::ReadLine->new('PFTP'); |
| 61 |
my $attribs = $term->Attribs; |
| 62 |
$term->ornaments('md,me,,'); # bold face prompt |
| 63 |
|
| 64 |
# |
| 65 |
# read hostname to which login |
| 66 |
# |
| 67 |
my $host; |
| 68 |
my @hosts = read_hosts($HOSTFILE); |
| 69 |
if (@ARGV) { |
| 70 |
$host = shift; |
| 71 |
} else { |
| 72 |
$attribs->{completion_word} = \@hosts; |
| 73 |
$attribs->{completion_append_character} = ''; |
| 74 |
$attribs->{completion_entry_function} = |
| 75 |
$attribs->{'list_completion_function'}; |
| 76 |
no warnings 'uninitialized'; # in case of typing ^D |
| 77 |
$host = $term->readline('hostname> ') . ''; |
| 78 |
$host =~ s/^\s+//; |
| 79 |
$host =~ s/\s+$//; |
| 80 |
$attribs->{completion_append_character} = ' '; |
| 81 |
$attribs->{completion_entry_function} = undef; |
| 82 |
} |
| 83 |
|
| 84 |
# |
| 85 |
# make ftp connection |
| 86 |
# |
| 87 |
my $ftp = Net::FTP->new($host, |
| 88 |
Debug => $opt_d); |
| 89 |
die "$0: cannot connect \`$host\'\n" unless $ftp; |
| 90 |
|
| 91 |
print STDERR $ftp->message; |
| 92 |
write_hosts($HOSTFILE, $host, @hosts); |
| 93 |
|
| 94 |
# |
| 95 |
# login |
| 96 |
# |
| 97 |
my $login = 'anonymous'; |
| 98 |
my $password = (getpwuid($<))[0] . '@' . hostdomain; |
| 99 |
if ($opt_u) { |
| 100 |
$login = $term->readline('login name> ', $login); |
| 101 |
|
| 102 |
# mask typed characters for password |
| 103 |
$attribs->{redisplay_function} = $attribs->{shadow_redisplay}; |
| 104 |
$password = $term->readline('password> ', $password); |
| 105 |
undef $attribs->{redisplay_function}; |
| 106 |
} |
| 107 |
|
| 108 |
$ftp->login($login, $password) or die "$0: cannot login: " . $ftp->message; |
| 109 |
print STDERR $ftp->message; |
| 110 |
|
| 111 |
$ftp->binary; # default binary |
| 112 |
print STDERR $ftp->message; |
| 113 |
|
| 114 |
my $pwd = $ftp->pwd; |
| 115 |
print STDERR $ftp->message; |
| 116 |
|
| 117 |
# |
| 118 |
# setup completion function |
| 119 |
# |
| 120 |
my @ftp_cmd_list = qw(cwd cd pwd ls dir get mget put mput lcd help); |
| 121 |
|
| 122 |
# completion_display_match_hook is supported by GNU Readline Library |
| 123 |
# 4.0 and later. Earlier versions ignore it. |
| 124 |
|
| 125 |
$attribs->{attempted_completion_function} = sub { |
| 126 |
my ($text, $line, $start, $end) = @_; |
| 127 |
if (substr($line, 0, $start) =~ /^\s*$/) { |
| 128 |
$attribs->{completion_word} = \@ftp_cmd_list; |
| 129 |
undef $attribs->{completion_display_matches_hook}; |
| 130 |
return $term->completion_matches($text, |
| 131 |
$attribs->{'list_completion_function'}); |
| 132 |
} elsif ($line =~ /^\s*(ls|dir|get|mget)\s/) { |
| 133 |
$attribs->{completion_display_matches_hook} = \&ftp_display_match_list; |
| 134 |
return $term->completion_matches($text, |
| 135 |
\&ftp_filename_completion_function); |
| 136 |
} elsif ($line =~ /^\s*(cd|cwd)\s/) { |
| 137 |
$attribs->{completion_display_matches_hook} = \&ftp_display_match_list; |
| 138 |
return $term->completion_matches($text, |
| 139 |
\&ftp_dirname_completion_function); |
| 140 |
} else { # put mput lcd |
| 141 |
undef $attribs->{completion_display_matches_hook}; |
| 142 |
return (); # local file name completion |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
# |
| 147 |
# Command loop |
| 148 |
# |
| 149 |
$SIG{INT} = 'IGNORE'; # ignore Control-C |
| 150 |
|
| 151 |
while (defined($_ = $term->readline("$login\@$host:$pwd> "))) { |
| 152 |
no strict 'refs'; |
| 153 |
next if /^\s*$/; |
| 154 |
my ($cmd, @args) = $term->history_tokenize($_); |
| 155 |
if ($cmd eq 'quit' || $cmd eq 'bye') { |
| 156 |
last; |
| 157 |
} |
| 158 |
my $func = "cmd_" . $cmd; |
| 159 |
&$func(@args); |
| 160 |
$attribs->{completion_append_character} = ' '; |
| 161 |
} |
| 162 |
$ftp->quit; |
| 163 |
print STDERR $ftp->message; |
| 164 |
|
| 165 |
exit (0); |
| 166 |
|
| 167 |
######################################################################## |
| 168 |
# |
| 169 |
# complete remote filename |
| 170 |
# |
| 171 |
sub ftp_filename_completion_function ( $$ ) { |
| 172 |
my($text, $state) = @_; |
| 173 |
ftp_completion_function($text, $state, 0); |
| 174 |
} |
| 175 |
|
| 176 |
sub ftp_dirname_completion_function ( $$ ) { |
| 177 |
my($text, $state) = @_; |
| 178 |
ftp_completion_function($text, $state, 1); |
| 179 |
} |
| 180 |
|
| 181 |
{ |
| 182 |
my ($i, $file, $dir, $fdir, $cw); |
| 183 |
|
| 184 |
sub ftp_completion_function ( $$$ ) { |
| 185 |
my($text, $state, $dironly) = @_; |
| 186 |
my $entry; |
| 187 |
|
| 188 |
unless ($state) { |
| 189 |
$i = 0; # clear counter at the first call |
| 190 |
($dir, $file) = ($text =~ m|(.*/)?(.*)$|); |
| 191 |
$dir = '' unless defined $dir; # to piecify -w |
| 192 |
$fdir = ($dir =~ m|^/|) ? $dir : "$pwd/$dir"; # full path name |
| 193 |
$fdir =~ s|//|/|g; |
| 194 |
$attribs->{completion_append_character} = ' '; |
| 195 |
return undef unless defined ($cw = rdir($fdir)); |
| 196 |
} else { |
| 197 |
$i++; |
| 198 |
} |
| 199 |
for (; $i <= $#{$cw}; $i++) { |
| 200 |
if (($entry = $cw->[$i]) =~ /^$file/ |
| 201 |
&& !($dironly && ($entry !~ m|/$|))) { |
| 202 |
$attribs->{completion_append_character} = '' |
| 203 |
if $entry =~ m|/$|; |
| 204 |
return ($dir . $entry); |
| 205 |
} |
| 206 |
} |
| 207 |
return undef; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
sub ftp_display_match_list { |
| 212 |
my($matches, $num_matches, $max_length) = @_; |
| 213 |
map { $_ =~ s|.*/([^/])|$1|; }(@{$matches}); |
| 214 |
$term->display_match_list($matches); |
| 215 |
$term->forced_update_display; |
| 216 |
} |
| 217 |
|
| 218 |
######################################################################## |
| 219 |
|
| 220 |
sub AUTOLOAD { |
| 221 |
# tell a lie to Domain.pm |
| 222 |
goto &SYS_gethostname if $AUTOLOAD =~/SYS_gethostname$/; |
| 223 |
|
| 224 |
$AUTOLOAD =~ s/.*::cmd_//; |
| 225 |
warn "command \`$AUTOLOAD\' is not defined or not implemented.\n"; |
| 226 |
} |
| 227 |
|
| 228 |
my %rdir; |
| 229 |
|
| 230 |
sub rdir { # get remote dir info and save it in %rdir |
| 231 |
my $dir = shift; |
| 232 |
return $rdir{$dir} if defined $rdir{$dir}; |
| 233 |
|
| 234 |
my $d = $ftp->ls('-F', $dir); |
| 235 |
if ($d) { |
| 236 |
foreach (@{$d}) { |
| 237 |
s|.*/(.)|$1|; # remove directory name |
| 238 |
s/[*@]$//; |
| 239 |
} |
| 240 |
return $rdir{$dir} = $d; |
| 241 |
} else { |
| 242 |
return undef; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
sub cmd_cwd { |
| 247 |
if ($ftp->cwd(@_)) { |
| 248 |
$pwd = $ftp->pwd(); |
| 249 |
} else { |
| 250 |
print STDERR "cwd: cannot chdir to \`$_\'\n" |
| 251 |
} |
| 252 |
print STDERR $ftp->message; |
| 253 |
} |
| 254 |
|
| 255 |
# Why this does not work? |
| 256 |
#*cmd_cd = \&cmd_cwd; |
| 257 |
|
| 258 |
sub cmd_cd { |
| 259 |
&cmd_cwd; |
| 260 |
} |
| 261 |
|
| 262 |
sub cmd_pwd { |
| 263 |
$pwd = $ftp->pwd(); |
| 264 |
if ($pwd) { |
| 265 |
#print STDERR "$pwd\n"; |
| 266 |
} else { |
| 267 |
print STDERR "pwd failed.\n"; |
| 268 |
} |
| 269 |
print STDERR $ftp->message; |
| 270 |
} |
| 271 |
|
| 272 |
sub cmd_ls { |
| 273 |
# strip ls option |
| 274 |
return &cmd_dir if defined $_[0] && $_[0] =~ /^-/ && shift =~ /l/; |
| 275 |
|
| 276 |
my $dir = shift || $pwd; |
| 277 |
my $d = rdir($dir); |
| 278 |
if (defined $d) { |
| 279 |
dump_list($d); |
| 280 |
} else { |
| 281 |
print STDERR "ls failed\n"; |
| 282 |
} |
| 283 |
print STDERR $ftp->message; |
| 284 |
} |
| 285 |
|
| 286 |
# from bash-2.0/lib/readline/complete.c:display_matches() |
| 287 |
# bash-4.0 and later has rl_display_match_list. Ignore it for compativility. |
| 288 |
sub dump_list { |
| 289 |
use integer; |
| 290 |
my @list = sort @{$_[0]}; |
| 291 |
my ($len, $max, $limit, $count, $i, $j, $l, $tmp); |
| 292 |
my $screenwidth = $ENV{COLUMNS} || 80; |
| 293 |
$max = 0; |
| 294 |
foreach (@list) { |
| 295 |
$len = length; |
| 296 |
$max = $len if $len > $max; |
| 297 |
} |
| 298 |
$max += 2; |
| 299 |
$limit = $screenwidth / $max; |
| 300 |
$limit-- if ($limit != 1 && ($limit * $max == $screenwidth)); |
| 301 |
$limit = 1 if $limit == 0; |
| 302 |
$count = (@list + ($limit - 1))/ $limit; |
| 303 |
for $i (0..$count - 1) { |
| 304 |
$l = $i; |
| 305 |
for $j (0..$limit - 1) { |
| 306 |
$tmp = $list[$l]; |
| 307 |
last if $l > @list || ! $tmp; |
| 308 |
print $tmp; |
| 309 |
print ' ' x ($max - length $tmp) if $j + 1 < $limit; |
| 310 |
$l += $count; |
| 311 |
} |
| 312 |
print "\n"; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
sub cmd_dir { |
| 317 |
# strip ls option |
| 318 |
shift if defined $_[0] && $_[0] =~ /^-/; |
| 319 |
|
| 320 |
my $dir = $ftp->dir('-F', @_); |
| 321 |
print STDERR $ftp->message; |
| 322 |
|
| 323 |
my @dir; |
| 324 |
if ($dir) { |
| 325 |
foreach (@{$dir}) { |
| 326 |
print STDERR "$_\n"; |
| 327 |
|
| 328 |
my $info = (parse_dir($_, '+0000'))[0]; # GMT |
| 329 |
next unless $info; # ignore if parse_dir() can not phase. |
| 330 |
next if $$info[0] =~ m|^\.\.?/$|; # ignore '.' and '..' |
| 331 |
$$info[0] =~ s|.*/(.)|$1|; # remove directory name |
| 332 |
$$info[0] =~ s/[*@]$//; |
| 333 |
push(@dir, $$info[0]); |
| 334 |
} |
| 335 |
$rdir{$pwd} = \@dir; |
| 336 |
} else { |
| 337 |
print STDERR "dir failed\n"; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
sub cmd_get { |
| 342 |
$ftp->get(@_); |
| 343 |
print STDERR $ftp->message; |
| 344 |
} |
| 345 |
|
| 346 |
sub cmd_mget { |
| 347 |
if ($opt_g) { |
| 348 |
foreach (@_) { |
| 349 |
$ftp->get($_); |
| 350 |
print STDERR $ftp->message; |
| 351 |
} |
| 352 |
} else { |
| 353 |
my $d = $ftp->ls(@_); |
| 354 |
print STDERR $ftp->message; |
| 355 |
foreach (sort @{$d}) { |
| 356 |
$ftp->get($_); |
| 357 |
print STDERR $ftp->message; |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
sub cmd_put { |
| 363 |
$ftp->put(@_); |
| 364 |
print STDERR $ftp->message; |
| 365 |
} |
| 366 |
|
| 367 |
sub cmd_mput { |
| 368 |
my $f; |
| 369 |
foreach $f (@_) { |
| 370 |
foreach ($opt_g ? $f : glob $f) { |
| 371 |
$ftp->put($_); |
| 372 |
print STDERR $ftp->message; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
sub cmd_lcd { |
| 378 |
my $dir = shift; |
| 379 |
no warnings 'uninitialized'; |
| 380 |
if ($dir) { |
| 381 |
chdir $dir or warn "cannot chdir to $dir: $!\n"; |
| 382 |
} |
| 383 |
printf STDERR "local current directory is \`%s\'\n", getcwd(); |
| 384 |
} |
| 385 |
|
| 386 |
sub cmd_help { |
| 387 |
print STDERR "@ftp_cmd_list\n"; |
| 388 |
} |
| 389 |
|
| 390 |
################################################################ |
| 391 |
sub read_hosts { |
| 392 |
my $file = shift; |
| 393 |
return () unless -f $file; |
| 394 |
open(F, $file) or die "$0: cannot open file \`$file\'\n"; |
| 395 |
my @l = <F>; |
| 396 |
close(F); |
| 397 |
chomp @l; |
| 398 |
return @l; |
| 399 |
} |
| 400 |
|
| 401 |
sub write_hosts { |
| 402 |
my $file = shift; |
| 403 |
my $lastline = ''; |
| 404 |
open(F, ">$file") or die "$0: cannot open file \`$file\'\n"; |
| 405 |
foreach (sort @_) { |
| 406 |
print F ($_, "\n") if $_ ne $lastline; |
| 407 |
$lastline = $_; |
| 408 |
} |
| 409 |
close(F); |
| 410 |
} |
| 411 |
|
| 412 |
################################################################ |
| 413 |
# show man page |
| 414 |
sub man { |
| 415 |
my $pager = $ENV{'PAGER'} || 'more'; |
| 416 |
exec "pod2man $0|nroff -man|$pager"; |
| 417 |
die "cannot exec pod2man, nroff, or $pager : $!\n"; |
| 418 |
} |
| 419 |
|
| 420 |
__END__ |
| 421 |
|
| 422 |
=pod |
| 423 |
|
| 424 |
=head1 OPTIONS |
| 425 |
|
| 426 |
=over 4 |
| 427 |
|
| 428 |
=item B<-u> |
| 429 |
|
| 430 |
disable autologin. |
| 431 |
|
| 432 |
=item B<-g> |
| 433 |
|
| 434 |
turn off glob. |
| 435 |
|
| 436 |
=item B<-h> |
| 437 |
|
| 438 |
show usage. |
| 439 |
|
| 440 |
=item B<-M> |
| 441 |
|
| 442 |
show thie manual. |
| 443 |
|
| 444 |
=item B<-d> |
| 445 |
|
| 446 |
debug mode. |
| 447 |
|
| 448 |
=item I<host> |
| 449 |
|
| 450 |
remote host name. |
| 451 |
|
| 452 |
=back |
| 453 |
|
| 454 |
=head1 FILES |
| 455 |
|
| 456 |
=over 4 |
| 457 |
|
| 458 |
=item I<~/.pftp_hosts> |
| 459 |
|
| 460 |
This file contains the list of host names. These name are used for |
| 461 |
completing of remote host name. If the host name which you login is |
| 462 |
not contained in this file, it will be added automatically. |
| 463 |
|
| 464 |
=back |
| 465 |
|
| 466 |
=head1 AUTHOR |
| 467 |
|
| 468 |
Hiroo Hayashi <[email protected]> |
| 469 |
|
| 470 |
=head1 BUGS |
| 471 |
|
| 472 |
Commands which the author does not know are not supported. |
| 473 |
|
| 474 |
=cut |