Add tab command-line completion

(Closes issue #12428)


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@114098 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.6.1
Tilghman Lesher 17 years ago
parent dc3e185e96
commit c716e8de53

@ -12,6 +12,7 @@ use Getopt::Long;
# #
my ($user, $pw, $host, $port, $interactive, $save) = (undef, undef, 'localhost', 5038, 0, 0); my ($user, $pw, $host, $port, $interactive, $save) = (undef, undef, 'localhost', 5038, 0, 0);
my $EOL = "\r\n"; # Standard End of Line my $EOL = "\r\n"; # Standard End of Line
my @commands;
process_credentials('/etc/astcli.conf'); process_credentials('/etc/astcli.conf');
process_credentials("$ENV{HOME}/.astcli") if defined $ENV{HOME}; process_credentials("$ENV{HOME}/.astcli") if defined $ENV{HOME};
GetOptions("username=s" => \$user, "secret=s" => \$pw, "host=s" => \$host, "port=s" => \$port, "readline" => \$interactive, "write" => \$save); GetOptions("username=s" => \$user, "secret=s" => \$pw, "host=s" => \$host, "port=s" => \$port, "readline" => \$interactive, "write" => \$save);
@ -51,12 +52,16 @@ sub send_command($) {
$tc->send($EOL); $tc->send($EOL);
my $response = ''; my $response = '';
while (<$tc>) { while (<$tc>) {
last if $_ =~ /--END COMMAND--/; if ($_ =~ /--END COMMAND--/) {
$_ =~ s/--END COMMAND--\s*//;
$response .= $_;
last;
}
$response .= $_; $response .= $_;
} }
$response =~ s/Privilege: Command$EOL//; $response =~ s/Privilege: Command$EOL//;
$response =~ s/Response: Follows$EOL//; $response =~ s/Response: Follows$EOL//;
print $response; return $response;
} }
sub login { sub login {
@ -93,27 +98,23 @@ if ($action eq '-' || !defined $action || $action eq '') {
my $term = new Term::ReadLine 'Command Line Interface'; my $term = new Term::ReadLine 'Command Line Interface';
my $prompt = "$host*CLI> "; my $prompt = "$host*CLI> ";
my $attribs = $term->Attribs; my $attribs = $term->Attribs;
$attribs->{completion_function} = sub { $attribs->{completion_function} = \&tab_completion;
my ($text, $line, $start) = @_;
# Stub function for tab auto completion for those feeling adventurous
return;
};
while (defined($_ = $term->readline($prompt))) { while (defined($_ = $term->readline($prompt))) {
(logoff() and exit) if $_ =~ /exit|quit/; # Give them a way to exit the "terminal" (logoff() and exit) if $_ =~ /exit|quit/; # Give them a way to exit the "terminal"
send_command($_); print send_command($_);
} }
} else { } else {
while (<>) { while (<>) {
chomp; chomp;
(logoff() and exit) if $_ =~ /exit|quit/; # If someone accidentally ends up here, let them exit (logoff() and exit) if $_ =~ /exit|quit/; # If someone accidentally ends up here, let them exit
send_command($_); print send_command($_);
} }
} }
exit 0; exit 0;
} }
# Otherwise just send the command: # Otherwise just send the command:
send_command($action); print send_command($action);
# parses a configuration file into the global $user and $pw. # parses a configuration file into the global $user and $pw.
sub process_credentials { sub process_credentials {
@ -144,3 +145,23 @@ sub usage {
exit; exit;
} }
sub tab_completion {
my ($word, $buffer, $offset) = @_;
my %items;
my $lastword = '';
if ($word eq '') {
$buffer =~ m/(\S+)\s?$/;
$lastword = $1;
print STDERR "\n\nlastword=\"$lastword\"\n";
}
my $res = send_command("_command matchesarray \"$buffer\" \"$word\"\n");
foreach my $item (split /\s+/, $res) {
$items{$item}++ unless ($item eq '_EOF_' or $item eq '' or $item eq $lastword);
}
#print STDERR "\nword=\"$word\" buffer=\"$buffer\" offset=\"$offset\" res=\"$res\"\n";
return sort keys %items;
}

Loading…
Cancel
Save