2 ;# @(#)complete.pl 1.0 (sun!waynet) 11/11/88
4 ;# Author: Wayne Thompson
7 ;# This routine provides word completion.
8 ;# (TAB) attempts word completion.
9 ;# (^D) prints completion list.
12 ;# Bell when word completion fails.
15 ;# The tty driver is put into raw mode.
18 ;# The erase and kill characters are hard coded.
21 ;# $input = do Complete('prompt_string', @completion_list);
25 local ($prompt) = shift (@_);
26 local ($c, $cmp, $l, $r, $ret, $return, $test);
28 system 'stty raw -echo';
30 print $prompt, $return;
31 while (($c = getc(stdin)) ne "\r") {
32 if ($c eq "\t") { # (TAB) attempt completion
35 push (@_match, $cmp) if $cmp =~ /^$return/;
39 unless ($#_match == 0) {
41 foreach $cmp (@_match) {
42 until (substr ($cmp, 0, $l) eq substr ($test, 0, $l)) {
48 print $test = substr ($test, $r, $l - $r);
49 $r = length ($return .= $test);
51 elsif ($c eq "\004") { # (^D) completion list
54 print "$cmp\r\n" if $cmp =~ /^$return/;
58 elsif ($c eq "\025" && $r) { # (^U) kill
65 elsif ($c eq "\177" || $c eq "\010") {
72 elsif ($c =~ /\S/) { # printable char
79 system 'stty -raw echo';