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