[asterisk-commits] tilghman: trunk r114098 - /trunk/contrib/scripts/astcli

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Apr 13 21:55:42 CDT 2008


Author: tilghman
Date: Sun Apr 13 21:55:41 2008
New Revision: 114098

URL: http://svn.digium.com/view/asterisk?view=rev&rev=114098
Log:
Add tab command-line completion
(Closes issue #12428)

Modified:
    trunk/contrib/scripts/astcli

Modified: trunk/contrib/scripts/astcli
URL: http://svn.digium.com/view/asterisk/trunk/contrib/scripts/astcli?view=diff&rev=114098&r1=114097&r2=114098
==============================================================================
--- trunk/contrib/scripts/astcli (original)
+++ trunk/contrib/scripts/astcli Sun Apr 13 21:55:41 2008
@@ -12,6 +12,7 @@
 #
 my ($user, $pw, $host, $port, $interactive, $save) = (undef, undef, 'localhost', 5038, 0, 0);
 my $EOL = "\r\n"; # Standard End of Line
+my @commands;
 process_credentials('/etc/astcli.conf');
 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);
@@ -51,12 +52,16 @@
 	$tc->send($EOL);
 	my $response = '';
 	while (<$tc>) {
-		last if $_ =~ /--END COMMAND--/;
+		if ($_ =~ /--END COMMAND--/) {
+			$_ =~ s/--END COMMAND--\s*//;
+			$response .= $_;
+			last;
+		}
 		$response .= $_;
 	}
 	$response =~ s/Privilege: Command$EOL//;
 	$response =~ s/Response: Follows$EOL//;
-	print $response;
+	return $response;
 }
 
 sub login {
@@ -93,27 +98,23 @@
 		my $term = new Term::ReadLine 'Command Line Interface';
 		my $prompt = "$host*CLI> ";
 		my $attribs = $term->Attribs;
-		$attribs->{completion_function} = sub {
-			my ($text, $line, $start) = @_;
-			# Stub function for tab auto completion for those feeling adventurous
-			return;
-		};
+		$attribs->{completion_function} = \&tab_completion;
 		while (defined($_ = $term->readline($prompt))) {
 			(logoff() and exit) if $_ =~ /exit|quit/; # Give them a way to exit the "terminal"
-			send_command($_);
+			print send_command($_);
 		}	
 	} else {
 		while (<>) {
 			chomp;
 			(logoff() and exit) if $_ =~ /exit|quit/; # If someone accidentally ends up here, let them exit
-			send_command($_);
+			print send_command($_);
 		}
 	}
 	exit 0;
 }
 
 # Otherwise just send the command:
-send_command($action);
+print send_command($action);
 
 # parses a configuration file into the global $user and $pw.
 sub process_credentials {
@@ -144,3 +145,23 @@
 	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;
+}
+




More information about the asterisk-commits mailing list